inbox.php 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-multirecipientnotification
 * @author     David Ballhausen, Tobias Zeuch
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 *
 */

define('INTERNAL', 1);
define('MENUITEM', 'inbox');
define('SECTION_PLUGINTYPE', 'artefact');
define('SECTION_PLUGINNAME', 'multirecipientnotification');
define('SECTION_PAGE', 'inbox');

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('pieforms/pieform.php');
safe_require('artefact', 'multirecipientnotification');

global $USER;
global $THEME;

// Add new title
define('TITLE', get_string('notification', 'artefact.multirecipientnotification'));

// Make sure the unread message count is up to date whenever the
// user hits this page.
$USER->reload_background_fields();

$installedtypes = get_records_assoc(
    'activity_type', '', '',
    'plugintype,pluginname,name',
    'name,admin,plugintype,pluginname'
);

38
$options = array();
39 40 41 42 43 44
// check if user is an admin in at least one group and if so they can see group admin filter options
$groupadmin = false;
$grouproles = $USER->get('grouproles');
if (array_search('admin', $grouproles) !== false) {
    $groupadmin = true;
}
45

46 47
foreach ($installedtypes as $t) {
    if (!$t->admin || $USER->get('admin') || ($groupadmin && $t->pluginname == 'forum')) {
48 49 50 51 52 53 54 55 56
        $section = $t->pluginname ? "{$t->plugintype}.{$t->pluginname}" : 'activity';
        $options[$t->name] = get_string('type' . $t->name, $section);
    }
}

if ($USER->get('admin')) {
    $options['adminmessages'] = get_string('typeadminmessages', 'activity');
}

57 58 59 60
// sort activitytypes now, when they have been translated
uasort($options, 'strcmp');
// ... and add the element for 'all types' to the beginning
$options = array_merge(array('all' => get_string('alltypes', 'activity')), $options);
61 62 63 64 65 66 67
$type = param_variable('type', 'all');
if ($type == '') {
    $type = 'all';
}
if (!isset($options[$type])) {
    // Comma-separated list; filter out anything that's not an installed type
    $type = join(',', array_unique(array_filter(
68
        explode(',', $type),
69 70 71 72 73 74 75 76 77 78 79 80
        function ($a) {global $installedtypes; return isset($installedtypes[$a]);}
    )));
}

require_once(get_config('docroot') . 'lib/activity.php');
// add the new function for outgoing notification
// use the new function to show from - and to user
$activitylist = activitylistin_html($type);

$star = json_encode($THEME->get_url('images/star.png'));
$readicon = json_encode($THEME->get_url('images/readusermessage.png'));
$strread = json_encode(get_string('read', 'activity'));
81
$strnodelete = json_encode(get_string('nodelete', 'activity'));
82 83 84 85 86 87 88
$javascript = <<<JAVASCRIPT

function markread(form, action) {

    var e = getElementsByTagAndClassName(null,'tocheck'+action,form);
    var pd = {};

89
    var havedelete = false;
90 91 92
    for (cb in e) {
        if (e[cb].checked == true) {
            pd[e[cb].name] = 1;
93
            havedelete = true;
94 95
        }
    }
96 97 98 99 100
    // if nothing has been seleced for deletion bail out now with error message
    if (!havedelete && action == 'del') {
        alert($strnodelete);
        return;
    }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    if (action == 'read') {
        pd['markasread'] = 1;
    }
    else if (action == 'del') {
        // If deleting, also pass the ids of unread messages, so we can update
        // the unread message count as accurately as possible.
        forEach(getElementsByTagAndClassName('input', 'tocheckread', form), function(cb) {
            pd[cb.name] = 0;
        });
        pd['delete'] = 1;
    }

    if (paginatorData) {
        for (p in paginatorData.params) {
            pd[p] = paginatorData.params[p];
        }
    }

    sendjsonrequest('indexin.json.php', pd, 'GET', function (data) {
        paginator.updateResults(data);
        updateUnreadCount(data);
    });
}

126 127 128
function toggleMessageDisplay(table, id) {
    var messages = jQuery('#message-' + table + '-' + id);
    if (messages.length <= 0) {
129 130
        return;
    }
131 132
    message = messages[0];
    messages.parents("tr.unread").removeClass("unread");
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    if (hasElementClass(message, 'hidden')) {
        var unread = getFirstElementByTagAndClassName(
            'input', 'tocheckread', message.parentNode.parentNode
        );
        var unreadicon = getFirstElementByTagAndClassName(
            'img', 'unreadmessage', message.parentNode.parentNode
        );
        if (unread) {
            var pd = {'readone':id, 'table':table};
            sendjsonrequest('indexin.json.php', pd, 'GET', function(data) {
                swapDOM(unread, IMG({'src' : {$star}, 'alt' : {$strread}}));
                if (unreadicon) {
                    swapDOM(unreadicon, IMG({'src' : {$readicon}, 'alt' : getNodeAttribute(unreadicon, 'alt') + ' - ' + {$strread}}));
                };
                updateUnreadCount(data);
            });
        }
    }
151 152 153 154 155 156 157 158 159 160
    var rows = messages.parents("tr");
    if (rows.length > 0) {
        if (jQuery(rows[0]).find(".messagedisplaylong.hidden").length > 0) {
            jQuery(rows[0]).find(".messagedisplaylong").removeClass("hidden");
            jQuery(rows[0]).find(".messagedisplayshort").addClass("hidden");
        }
        else {
            jQuery(rows[0]).find(".messagedisplaylong").addClass("hidden");
            jQuery(rows[0]).find(".messagedisplayshort").removeClass("hidden");
        }
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    }
}

function changeactivitytype() {
    var delallform = document.forms['delete_all_notifications'];
    delallform.elements['type'].value = this.options[this.selectedIndex].value;
    var params = {'type': this.options[this.selectedIndex].value};
    sendjsonrequest('indexin.json.php', params, 'GET', function(data) {
        paginator.updateResults(data);
    });
}

// We want the paginator to tell us when a page gets changed.
// @todo: remember checked/unchecked state when changing pages
function PaginatorData() {
    var self = this;
    var params = {};

    this.pageChanged = function(data) {
        self.params = {
            'offset': data.offset,
            'limit': data.limit,
            'type': data.type
        }
    }

    paginatorProxy.addObserver(self);
    connect(self, 'pagechanged', self.pageChanged);
}

var paginator;
var paginatorData = new PaginatorData();

addLoadEvent(function () {
    paginator = {$activitylist['pagination_js']}
    connect('notifications_type', 'onchange', changeactivitytype);
});

199 200 201 202 203 204 205 206 207 208 209
jQuery(function() {
    jQuery('#activitylist tr').hover(
        function() {
                jQuery(this).addClass('highlight');
            },
            function() {
                jQuery(this).removeClass('highlight');
        }
    );
});

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
JAVASCRIPT;

$deleteall = pieform(array(
    'name'        => 'delete_all_notifications',
    'method'      => 'post',
    'plugintype'  => 'core',
    'pluginname'  => 'account',
    'elements'    => array(
        'type' => array(
            'type' => 'hidden',
            'value' => $type,
        ),
        'submit' => array(
            'type' => 'submit',
            'value' => get_string('deleteallnotifications', 'activity'),
            'confirm' => get_string('reallydeleteallnotifications', 'activity'),
        ),
    ),
));

function delete_all_notifications_submit() {
    global $USER, $SESSION;
    $userid = $USER->get('id');
    $type = param_variable('type', 'all');

    db_begin();

    // delete multirecipient-message separately
    $count = 0;
    if (in_array($type, array('all', 'usermessage'))) {
        if ($type !== 'all') {
            $at = activity_locate_typerecord($type);
            $typecond = 'AND {msg}.{type} = ' . $at->id;
        }
        else {
            $typecond = '';
        }

        $query = 'SELECT msg.id AS id
                FROM {artefact_multirecipient_notification} as msg
                INNER JOIN {artefact_multirecipient_userrelation} as rel
                ON msg.id = rel.notification
                AND rel.usr = ?
                AND rel.role = ?
                AND rel.deleted = \'0\'
                ' . $typecond;
        $result = get_records_sql_array($query, array($userid, 'recipient'));
        $msgids = array();
        if (is_array($result)) {
            foreach ($result as $record) {
                $msgids[] = $record->id;
            }
           delete_messages_mr($msgids, $userid);
        }
        $count = count($msgids);
    }

    $typesql = '';
    if ($type != 'all') {
        // Treat as comma-separated list of activity type names
270
        $types = explode(',', preg_replace('/[^a-z,]+/', '', $type));
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
        if ($types) {
            $typesql = ' at.name IN (' . join(',', array_map('db_quote', $types)) . ')';
            if (in_array('adminmessages', $types)) {
                $typesql = '(' . $typesql . ' OR at.admin = 1)';
            }
            $typesql = ' AND ' . $typesql;
        }
    }
    // changed to meesage from usr
    $fromexpression = "FROM {notification_internal_activity} a
        INNER JOIN {activity_type} at ON a.type = at.id
        WHERE a.usr = ? $typesql";
    $values = array($userid);

    $records = get_records_sql_array('SELECT a.id ' . $fromexpression, $values);
    if ($records) {
        $count += sizeof($records);
        $ids = array();
        foreach ($records as $row) {
            $ids[] = $row->id;
        }
        // Remove parent pointers to messages we're about to delete
        execute_sql('
            UPDATE {notification_internal_activity}
            SET parent = NULL
            WHERE parent IN (' . join(',', array_map('db_quote', $ids)) . ')'
        );
        // delete
        execute_sql('
            DELETE FROM {notification_internal_activity}
            WHERE id IN (' . join(',', array_map('db_quote', $ids)) . ')'
        );
        // The update_unread_delete db trigger on notification_internal_activity
        // will update the unread column on the usr table.
    }

    db_commit();
308
    $SESSION->add_ok_msg(get_string('deletednotifications1', 'activity', $count));
309 310 311 312 313 314 315 316 317 318 319 320 321
    redirect(get_config('wwwroot') . 'artefact/multirecipientnotification/inbox.php?type=' . $type);
}

$extrastylesheets = $THEME->get_url('style.css', false, 'artefact/multirecipientnotification');
$smarty = smarty(array('paginator'),
    array('<link rel="stylesheet" type="text/css" href="' . $extrastylesheets . '">')
);
$smarty->assign('options', $options);
$smarty->assign('type', $type);
$smarty->assign('INLINEJAVASCRIPT', $javascript);

// Adding the links to out- and inbox
$smarty->assign('PAGEHEADING', TITLE);
322

323
// show urls and titles
324 325
define('NOTIFICATION_SUBPAGE', 'inbox');
$smarty->assign('SUBPAGENAV', PluginArtefactMultirecipientnotification::submenu_items());
326 327 328 329 330 331

$smarty->assign('deleteall', $deleteall);
$smarty->assign('activitylist', $activitylist);

// Changed to new tpl
$smarty->display('artefact:multirecipientnotification:indexin.tpl');