index.json.php 3.51 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
<?php
/**
 * This program is part of Mahara
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage core
 * @author     Penny Leach <penny@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

define('INTERNAL', 1);
28
define('JSON', 1);
29
30
31
32
33
34
35
36

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

json_headers();

$markasread = param_integer('markasread', 0);

if ($markasread) {
Penny Leach's avatar
Penny Leach committed
37
    $count = 0;
38
39
40
41
42
    db_begin();
    try {
        foreach ($_GET as $k => $v) {
            if (preg_match('/^unread\-(\d+)$/',$k,$m)) {
                set_field('notification_internal_activity', 'read', 1, 'id', $m[1]);
Penny Leach's avatar
Penny Leach committed
43
                $count++;
44
45
46
47
48
            }
        }
    }
    catch (Exception $e) {
        db_rollback();
Richard Mansfield's avatar
Richard Mansfield committed
49
        json_reply('local', get_string('failedtomarkasread', 'activity') . ': ' . $e->getMessage);
50
51
    }
    db_commit();
Richard Mansfield's avatar
Richard Mansfield committed
52
    json_reply(false, array('message' => get_string('markedasread', 'activity'), 'count' => $count));
53
54
55
56
57
58
59
60
}

// normal processing

$type = param_alpha('type', 'all');
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);

61
$userid = $USER->get('id');
62
63
64

if ($type == 'all') {
    $count = count_records('notification_internal_activity', 'usr', $userid);
65
    $records = get_records_array('notification_internal_activity', 'usr', $userid,
66
67
68
                                 'ctime DESC', '*', $offset, $limit);
} else if ($type == 'adminmessages' && $USER->get('admin')) {
    $count = count_records_select('notification_internal_activity', 'usr = ? AND type IN (
69
         SELECT name FROM {activity_type} WHERE admin = ?)', 
70
71
                                  array($userid, 1));
    $records = get_records_select_array('notification_internal_activity', 'usr = ? AND type IN (
72
         SELECT name FROM {activity_type} WHERE admin = ?)', 
73
74
75
76
                                  array($userid, 1),
                                  'ctime DESC', '*', $offset, $limit);
}
else {
77
78
    $count = count_records_select('notification_internal_activity', 'usr = ? AND type = ?',
                                  array($userid,$type));
79
    $records = get_records_select_array('notification_internal_activity', 'usr = ? AND type = ?', 
80
81
82
83
84
85
86
87
                                  array($userid, $type), 
                                  'ctime DESC', '*', $offset, $limit);
}

if (empty($records)) {
    $records = array();
}
$data = array();
88
$star = theme_get_url('images/star.png');
89
90
$unread = get_string('unread', 'activity');

Penny Leach's avatar
Penny Leach committed
91
foreach ($records as &$r) {
92
93
    $r->date = format_date(strtotime($r->ctime));
    $r->type = get_string('type' . $r->type, 'activity');
94
95
96
}

$activity = array(
Penny Leach's avatar
Penny Leach committed
97
    'count'  => $count,
98
    'offset' => $offset,
Penny Leach's avatar
Penny Leach committed
99
100
101
    'limit'  => $limit,
    'data'   => $records,
    'star'   => $star,
102
103
104
105
106
    'unread' => $unread,
);

echo json_encode($activity);
?>