index.json.php 3.1 KB
Newer Older
1 2 3 4 5
<?php
/**
 *
 * @package    mahara
 * @subpackage core
6
 * @author     Catalyst IT Ltd
7 8
 * @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.
9 10 11 12
 *
 */

define('INTERNAL', 1);
13
define('JSON', 1);
14 15 16

require(dirname(dirname(dirname(__FILE__))) . '/init.php');

17
$readone    = param_integer('readone', 0);
18
$markasread = param_integer('markasread', 0);
19
$delete     = param_integer('delete', 0);
20

21 22
if ($readone) {
    set_field('notification_internal_activity', 'read', 1, 'id', $readone, 'usr', $USER->get('id'));
23 24 25
    $unread = $USER->add_unread(-1);
    $data = array(
        'newunreadcount' => $unread,
26
        'newimage' => $THEME->get_url($unread ? 'images/newmail.png' : 'images/message.png'),
27 28
    );
    json_reply(false, array('data' => $data));
29
}
30

31
require_once(get_config('libroot') . 'activity.php');
32

33
$type = param_variable('type', 'all');
34 35 36
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);

37
$message = false;
38

39 40 41 42 43
if ($markasread) {
    $ids = array();
    foreach ($_GET as $k => $v) {
        if (preg_match('/^unread\-(\d+)$/',$k,$m)) {
            $ids[] = $m[1];
44
        }
45
    }
46 47 48 49 50 51
    if ($ids) {
        set_field_select(
            'notification_internal_activity', 'read', 1,
            'id IN (' . join(',', $ids) . ') AND usr = ?',
            array($USER->get('id'))
        );
52
        $newunread = $USER->add_unread(-count($ids));
53 54
    }
    $message = get_string('markedasread', 'activity');
55
}
56 57
else if ($delete) {
    $ids = array();
58
    $deleteunread = 0; // Remember the number of unread messages being deleted
59 60 61
    foreach ($_GET as $k => $v) {
        if (preg_match('/^delete\-(\d+)$/',$k,$m)) {
            $ids[] = $m[1];
62 63 64
            if (isset($_GET['unread-' . $m[1]])) {
                $deleteunread++;
            }
65 66 67 68 69 70
        }
    }
    if ($ids) {
        $strids = join(',', $ids);
        $userid = $USER->get('id');
        db_begin();
71 72
        // Remove parent pointers to messages we're about to delete
        // Use temp table in subselect for Mysql compat.
73 74 75 76 77
        execute_sql("
            UPDATE {notification_internal_activity}
            SET parent = NULL
            WHERE parent IN (
                SELECT id
78 79 80
                FROM (
                   SELECT id FROM {notification_internal_activity} WHERE id IN ($strids) AND usr = ?
                ) AS temp
81 82 83 84 85 86 87 88
            )",
            array($userid)
        );
        delete_records_select(
            'notification_internal_activity',
            "id IN ($strids) AND usr = ?",
            array($userid)
        );
89 90 91
        if ($deleteunread) {
            $newunread = $USER->add_unread(-$deleteunread);
        }
92 93 94
        db_commit();
    }
    $message = get_string('deletednotifications', 'activity', count($ids));
95 96
}

97 98
$newhtml = activitylist_html($type, $limit, $offset);

99 100
if (isset($newunread)) {
    $newhtml['newunreadcount'] = $newunread;
101
    $newhtml['newimage'] = $THEME->get_url($newunread ? 'images/newmail.png' : 'images/message.png');
102 103
}

104
json_reply(false, (object) array('message' => $message, 'data' => $newhtml));