index.json.php 3.62 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
36
<?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); 

if ($stopmonitoring) {
37
    $userid = $USER->get('id');
Penny Leach's avatar
Penny Leach committed
38
39
40
41
42
    $count = 0;
    db_begin();
    try {
        foreach ($_GET as $k => $v) {
            if (preg_match('/^stopview\-(\d+)$/',$k,$m)) {
43
                delete_records('usr_watchlist_view', 'usr', $userid, 'view', $m[1]);
Penny Leach's avatar
Penny Leach committed
44
45
46
                $count++;
            }
            else if (preg_match('/^stopartefact\-(\d+)$/',$k,$m)) {
47
                delete_records('usr_watchlist_artefact', 'usr', $userid, 'artefact', $m[1]);
Penny Leach's avatar
Penny Leach committed
48
49
50
                $count++;
            }
            else if (preg_match('/^stopcommunity\-(\d+)$/',$k,$m)) {
51
                delete_records('usr_watchlist_community', 'usr', $userid, 'community', $m[1]);
Penny Leach's avatar
Penny Leach committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
                $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);

74
$userid = $USER->get('id');
Penny Leach's avatar
Penny Leach committed
75
76
77
78
$prefix = get_config('dbprefix');

if ($type == 'views') {
    $count = count_records('usr_watchlist_view', 'usr', $userid);
Penny Leach's avatar
Penny Leach committed
79
    $sql = 'SELECT v.*, v.title AS name 
Penny Leach's avatar
Penny Leach committed
80
81
            FROM ' . $prefix . 'view v
            JOIN ' . $prefix . 'usr_watchlist_view w ON w.view = v.id
Penny Leach's avatar
Penny Leach committed
82
83
84
            WHERE w.usr = ?
            ORDER BY v.mtime DESC';
    if ($records = get_rows_sql($sql, array($userid), $offset, $limit)) {
Penny Leach's avatar
Penny Leach committed
85
86
87
88
89
90
91
92
93
94
        foreach ($records as &$r) {
            // @todo session expandey stuff
        }
    }
}
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
95
96
97
            WHERE w.usr = ?
            ORDER BY c.mtime DESC';
    $records = get_rows_sql($sql, array($userid), $offset, $limit);
Penny Leach's avatar
Penny Leach committed
98
99
100
101
102
103
104
105
106
107
108
109
}

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

$activity = array(
    'count'     => $count,
    'offset'    => $offset,
    'limit'     => $limit,
    'data'      => $records,
    'type'      => $type,
Penny Leach's avatar
Penny Leach committed
110
111
    'minusicon' => theme_get_image_path('minus.png'),
    'plusicon'  => theme_get_image_path('plus.png'),
Penny Leach's avatar
Penny Leach committed
112
113
114
115
116
117
118
    'minusalt'  => get_string('collapse'),
    'plusalt'   => get_string('expand'),
);

echo json_encode($activity);

?>