index.json.php 4.93 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
28
29
30
31
32
33
34
35
<?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);

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

json_headers();

$stopmonitoring = param_integer('stopmonitoring', 0);
$getartefacts   = param_integer('getartefacts', 0); 

36
37
$prefix = get_config('dbprefix');

Penny Leach's avatar
Penny Leach committed
38
if ($stopmonitoring) {
39
    $userid = $USER->get('id');
Penny Leach's avatar
Penny Leach committed
40
41
42
43
    $count = 0;
    db_begin();
    try {
        foreach ($_GET as $k => $v) {
44
            if (preg_match('/^stopviews\-(\d+)$/',$k,$m)) {
45
46
47
48
49
50
51
52
53
54
                $recurse = param_boolean($k . '-recurse', null);
                if (!empty($recurse)) {
                    $sql = 'DELETE FROM ' . $prefix . 'usr_watchlist_artefact 
                            WHERE usr = ? AND (artefact IN (
                                SELECT artefact FROM ' . $prefix . 'artefact_parent_cache WHERE parent IN (
                                    SELECT artefact FROM ' . $prefix . 'view_artefact WHERE view = ? ) )
                            OR artefact IN (
                                SELECT artefact FROM ' . $prefix . 'view_artefact WHERE view = ?))';
                    delete_records_sql($sql, array($userid, $m[1], $m[1]));
                }
55
                delete_records('usr_watchlist_view', 'usr', $userid, 'view', $m[1]);
Penny Leach's avatar
Penny Leach committed
56
57
                $count++;
            }
58
            else if (preg_match('/^stopartefacts\-(\d+)$/',$k,$m)) {
59
60
61
62
63
64
65
                $recurse = param_boolean($k . '-recurse', null);
                if (!empty($recurse)) {
                    $sql = 'DELETE FROM ' . $prefix . 'usr_watchlist_artefact 
                            WHERE usr = ? AND artefact IN (
                                SELECT artefact FROM ' . $prefix . 'artefact_parent_cache WHERE parent = ?)';
                    delete_records_sql($sql, array($userid, $m[1]));
                }
66
                delete_records('usr_watchlist_artefact', 'usr', $userid, 'artefact', $m[1]);
Penny Leach's avatar
Penny Leach committed
67
68
                $count++;
            }
69
            else if (preg_match('/^stopcommunities\-(\d+)$/',$k,$m)) {
70
                delete_records('usr_watchlist_community', 'usr', $userid, 'community', $m[1]);
Penny Leach's avatar
Penny Leach committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
                $count++;
            }
        }
    }
    catch (Exception $e) {
        db_rollback();
        $data = array('error' => $e->getMessage);
        echo json_encode($data);
    }
    db_commit();
    $data = array('success' => 1, 'count' => $count);
    echo json_encode($data);
    exit;
}


// normal processing

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

93
$userid = $USER->get('id');
Penny Leach's avatar
Penny Leach committed
94
95
$prefix = get_config('dbprefix');

96
97
98
$count = 0;
$records = array();

Penny Leach's avatar
Penny Leach committed
99
100
if ($type == 'views') {
    $count = count_records('usr_watchlist_view', 'usr', $userid);
Penny Leach's avatar
Penny Leach committed
101
    $sql = 'SELECT v.*, v.title AS name 
Penny Leach's avatar
Penny Leach committed
102
103
            FROM ' . $prefix . 'view v
            JOIN ' . $prefix . 'usr_watchlist_view w ON w.view = v.id
Penny Leach's avatar
Penny Leach committed
104
105
            WHERE w.usr = ?
            ORDER BY v.mtime DESC';
106
    $records = get_records_sql_array($sql, array($userid), $offset, $limit);
Penny Leach's avatar
Penny Leach committed
107
108
109
110
111
112
}
else if ($type == 'communities') {
    $count = count_records('usr_watchlist_community', 'usr', $userid);
    $sql = 'SELECT c.* 
            FROM ' . $prefix . 'community c
            JOIN ' . $prefix . 'usr_watchlist_community w ON w.community = c.id 
Penny Leach's avatar
Penny Leach committed
113
114
            WHERE w.usr = ?
            ORDER BY c.mtime DESC';
115
    $records = get_records_sql_array($sql, array($userid), $offset, $limit);
Penny Leach's avatar
Penny Leach committed
116
}
117
118
119
120
121
122
123
124
125
else if ($type == 'artefacts') {
    $count = count_records('usr_watchlist_artefact', 'usr', $userid);
    $sql = 'SELECT a.* , a.title AS name
            FROM ' . $prefix . 'artefact a
            JOIN ' . $prefix . 'usr_watchlist_artefact w ON w.artefact = a.id 
            WHERE w.usr = ?
            ORDER BY a.mtime DESC';
    $records = get_records_sql_array($sql, array($userid), $offset, $limit);
}
Penny Leach's avatar
Penny Leach committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

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

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

echo json_encode($activity);

?>