index.json.php 5.07 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
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);
Penny Leach's avatar
Penny Leach committed
29
30
31
32
33
34

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

json_headers();

$stopmonitoring = param_integer('stopmonitoring', 0);
Penny Leach's avatar
Penny Leach committed
35
$userlist       = param_alpha('userlist', null);
Penny Leach's avatar
Penny Leach committed
36
37
$getartefacts   = param_integer('getartefacts', 0); 

Penny Leach's avatar
Penny Leach committed
38
$userid = $USER->get('id');
39

Penny Leach's avatar
Penny Leach committed
40
41
42
43
44
if ($stopmonitoring) {
    $count = 0;
    db_begin();
    try {
        foreach ($_GET as $k => $v) {
45
            if (preg_match('/^stopviews\-(\d+)$/',$k,$m)) {
46
                delete_records('usr_watchlist_view', 'usr', $userid, 'view', $m[1]);
Penny Leach's avatar
Penny Leach committed
47
48
                $count++;
            }
49
            else if (preg_match('/^stopartefacts\-(\d+)$/',$k,$m)) {
50
                delete_records('usr_watchlist_artefact', 'usr', $userid, 'artefact', $m[1]);
Penny Leach's avatar
Penny Leach committed
51
52
                $count++;
            }
53
54
            else if (preg_match('/^stopgroups\-(\d+)$/',$k,$m)) {
                delete_records('usr_watchlist_group', 'usr', $userid, 'group', $m[1]);
Penny Leach's avatar
Penny Leach committed
55
56
57
58
59
60
                $count++;
            }
        }
    }
    catch (Exception $e) {
        db_rollback();
61
        json_reply('local', get_string('stopmonitoringfailed', 'activity') . ': ' . $e->getMessage);
Penny Leach's avatar
Penny Leach committed
62
63
    }
    db_commit();
64
65
    $message = $count ? get_string('stopmonitoringsuccess', 'activity') : false;
    json_reply(false, array('message' => $message, 'count' => $count));
Penny Leach's avatar
Penny Leach committed
66
}
Penny Leach's avatar
Penny Leach committed
67
68
69
if (!empty($userlist)) {
    if ($userlist == 'views') {
        $sql = 'SELECT DISTINCT u.* 
70
71
72
        FROM {usr} u
        JOIN {view} v ON v.owner = u.id 
        JOIN {usr_watchlist_view} w ON w.view = v.id
Penny Leach's avatar
Penny Leach committed
73
74
75
76
77
78
79
80
        WHERE w.usr = ?';
        
        if (!$users = get_records_sql_array($sql, array($userid))) {
            $users = array();
        }
    }
    else if ($userlist == 'artefacts') {
        $sql = 'SELECT DISTINCT u.* 
81
82
83
        FROM {usr} u
        JOIN {artefact} a ON a.owner = u.id 
        JOIN {usr_watchlist_artefact} w ON w.artefact = a.id
Penny Leach's avatar
Penny Leach committed
84
85
86
87
88
89
90
91
92
        WHERE w.usr = ?';
        
        if (!$users = get_records_sql_array($sql, array($userid))) {
            $users = array();
        }
    }
    else {
        $users = array();
    }
Penny Leach's avatar
Penny Leach committed
93

Penny Leach's avatar
Penny Leach committed
94
95
    $data = array();
    foreach ($users as $u) {
Richard Mansfield's avatar
Richard Mansfield committed
96
        $data[] = array('id' => $u->id, 'name' => display_name($u));
Penny Leach's avatar
Penny Leach committed
97
    }
98
    json_reply(false, array('message' => false, 'users' => $data));
Penny Leach's avatar
Penny Leach committed
99
}
Penny Leach's avatar
Penny Leach committed
100

Penny Leach's avatar
Penny Leach committed
101
// normal processing (fetching tablerenderer results)
Penny Leach's avatar
Penny Leach committed
102
103

$type = param_alpha('type', 'views');
Penny Leach's avatar
Penny Leach committed
104
$owner = param_integer('user', null);
Penny Leach's avatar
Penny Leach committed
105
106
107
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);

108
$userid = $USER->get('id');
Penny Leach's avatar
Penny Leach committed
109

110
111
112
$count = 0;
$records = array();

Penny Leach's avatar
Penny Leach committed
113
114
if ($type == 'views') {
    $count = count_records('usr_watchlist_view', 'usr', $userid);
115
    $sql = 'SELECT v.*, v.title AS name, w.recurse
116
117
            FROM {view} v
            JOIN {usr_watchlist_view} w ON w.view = v.id
Penny Leach's avatar
Penny Leach committed
118
119
120
121
122
123
124
            WHERE w.usr = ?';
    $values = array($userid);
    if (isset($owner)) {
        $sql .= ' AND v.owner = ?';
        $values[] = $owner;
    }
    $sql .= '
Penny Leach's avatar
Penny Leach committed
125
            ORDER BY v.mtime DESC';
Penny Leach's avatar
Penny Leach committed
126
    $records = get_records_sql_array($sql, $values, $offset, $limit);
Penny Leach's avatar
Penny Leach committed
127
}
128
129
else if ($type == 'groups') {
    $count = count_records('usr_watchlist_group', 'usr', $userid);
130
131
132
    $sql = 'SELECT g.* 
            FROM {group} g
            JOIN {usr_watchlist_group} w ON w.group = g.id 
Penny Leach's avatar
Penny Leach committed
133
            WHERE w.usr = ?
134
            ORDER BY g.mtime DESC';
135
    $records = get_records_sql_array($sql, array($userid), $offset, $limit);
Penny Leach's avatar
Penny Leach committed
136
}
137
138
else if ($type == 'artefacts') {
    $count = count_records('usr_watchlist_artefact', 'usr', $userid);
139
    $sql = 'SELECT a.* , a.title AS name, w.view, w.recurse
140
141
            FROM {artefact} a
            JOIN {usr_watchlist_artefact} w ON w.artefact = a.id 
Penny Leach's avatar
Penny Leach committed
142
143
144
145
146
147
148
            WHERE w.usr = ?';
    $values = array($userid);
    if (isset($owner)) {
        $sql .= ' AND a.owner = ?';
        $values[] = $owner;
    }
    $sql .= '
149
            ORDER BY a.mtime DESC';
Penny Leach's avatar
Penny Leach committed
150
    $records = get_records_sql_array($sql, $values, $offset, $limit);
151
}
Penny Leach's avatar
Penny Leach committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

if (empty($records)) {
    $records = array();
}

$activity = array(
    'count'     => $count,
    'offset'    => $offset,
    'limit'     => $limit,
    'data'      => $records,
    'type'      => $type,
);

echo json_encode($activity);

?>