index.php 7.25 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * 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 3 of the License, or
 * (at your option) any later version.
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * 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.
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21
 *
 * @package    mahara
 * @subpackage core
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
 *
 */

define('INTERNAL', 1);
29
define('MENUITEM', 'inbox');
30
31
32
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'account');
define('SECTION_PAGE', 'activity');
33
34

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
35
require_once('pieforms/pieform.php');
36
define('TITLE', get_string('inbox'));
37

38
39
40
41
42
43
44
45
46
47
$types = get_records_assoc('activity_type', 'admin', 0, 'plugintype,pluginname,name', 'id,name,plugintype,pluginname');
$types = array_map(create_function('$a', '
    if (!empty($a->plugintype)) { 
        $section = "{$a->plugintype}.{$a->pluginname}";
    }
    else {
        $section = "activity";
    }
    return get_string("type" . $a->name, $section);
    '), $types);
48
if ($USER->get('admin')) {
49
    $types['adminmessages'] = get_string('typeadminmessages', 'activity');
50
}
51

52
$morestr = get_string('more...');
53

54
$star = json_encode($THEME->get_url('images/star.png'));
55
56
$unread = json_encode(get_string('unread', 'activity'));

57
58
59
60
61
62
$javascript = <<<JAVASCRIPT
var activitylist = new TableRenderer(
    'activitylist',
    'index.json.php', 
    [
        function(r) { 
63
            if (r.message) {
64
65
                var messagemore = DIV({'id' : 'message-' + r.id, 'style': 'display:none'});
                messagemore.innerHTML = r.message;
66
                if (r.url) {
67
                    appendChildNodes(messagemore, BR(null), A({'href' : r.url, 'class': 's'}, '{$morestr}'));
68
69
                }
                return TD(null, A({'href': '', 'onclick': 'showHideMessage(' + r.id + '); return false;'}, r.subject),
70
                          messagemore);
71
72
73
            }
            else if (r.url) { 
                return TD(null, A({'href': r.url}, r.subject));
74
            } 
75
76
77
            else {
                return TD(null, r.subject);
            }
78
        },
Penny Leach's avatar
Penny Leach committed
79
        'type',
80
        'date',
81
        function (r, d) {
Penny Leach's avatar
Penny Leach committed
82
            if (r.read == 1) {
Nigel McNie's avatar
Nigel McNie committed
83
                return TD({'class': 'center'},IMG({'src' : d.star, 'alt' : d.unread}));
84
            }
85
            else {
86
                return TD({'class': 'center'}, INPUT({'type' : 'checkbox', 'class' : 'tocheckread', 'name' : 'unread-' + r.id}));
87
            }
88
89
90
        },
        function (r, d) {
            return TD({'class': 'center'}, INPUT({'type' : 'checkbox', 'class' : 'tocheckdel', 'name' : 'delete-' + r.id}));
91
92
93
94
95
96
97
98
        }
    ]
);

activitylist.type = 'all';
activitylist.statevars.push('type');
activitylist.updateOnLoad();

99
100
101
function markread(form, action) {

    var e = getElementsByTagAndClassName(null,'tocheck'+action,form);
102
103
104
105
106
107
108
109
    var pd = {};
    
    for (cb in e) {
        if (e[cb].checked == true) {
            pd[e[cb].name] = 1;
        }
    }

110
111
112
113
114
    if (action == 'read') {
        pd['markasread'] = 1;
    } else if (action == 'del') {
        pd['delete'] = 1;
    }
115
    
116
117
    sendjsonrequest('index.json.php', pd, 'GET', function (data) {
        if (!data.error) {
Penny Leach's avatar
Penny Leach committed
118
119
            if (data.count > 0) {
                activitylist.doupdate();
120
121
122
123
124
                if (data.newunreadcount && typeof(data.newunreadcount) != 'undefined') {
                    updateUnreadCount(data.newunreadcount, 'reset');
                } else {
                    updateUnreadCount(data.count, 'decrement');
                }
Penny Leach's avatar
Penny Leach committed
125
            }
126
        }
127
128
129
    }, function () {
        activitylist.doupdate();
    });
130
131
}

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
function updateUnreadCount(n, decrement) {
    forEach(getElementsByTagAndClassName('span', 'unreadmessagescontainer'), function(message) {
        var countnode = message.firstChild;
        if (decrement == 'decrement') {
            var oldcount = parseInt(countnode.innerHTML);
            var newcount = (oldcount - n);
        }
        else {
            var newcount = n;
        }
        var messagenode = message.lastChild;
        if (newcount == 1) { // jump through hoops to change between plural and singular
            messagenode.innerHTML = get_string('unreadmessage');
        }
        else {
            messagenode.innerHTML = get_string('unreadmessages');
        }
        countnode.innerHTML = newcount;
    });
}

153
154
function showHideMessage(id) {
    if (getStyle('message-' + id, 'display') == 'none') {
155
        var unread = getFirstElementByTagAndClassName('input', 'tocheckread', 
156
157
158
159
160
161
162
163
                                                      $('message-' + id).parentNode.parentNode);
        if (unread) {
            var pd = {'markasread':1, 'quiet':1};
            pd['unread-'+id] = 1;
            sendjsonrequest('index.json.php', pd, 'GET', function(data) {
                return !!data.error;
            });
            swapDOM(unread, IMG({'src' : {$star}, 'alt' : {$unread}}));
164
            updateUnreadCount(1, 'decrement');
165
        }
166
167
168
169
170
171
172
        showElement('message-' + id);
    }
    else {
        hideElement('message-' + id);
    }
}

173
174
JAVASCRIPT;

175
176
177
178
179
180
181
182
183
184
$deleteall = pieform(array(
    'name'        => 'delete_all_notifications',
    'method'      => 'post',
    'plugintype'  => 'core',
    'pluginname'  => 'account',
    'elements'    => array(
        'submit' => array(
            'type' => 'submit',
            'value' => get_string('deleteallnotifications', 'activity'),
            'confirm' => get_string('reallydeleteallnotifications', 'activity'),
185
            'class' => 'btn-del',
186
187
188
189
190
191
192
193
194
195
196
197
        ),
    ),
));

function delete_all_notifications_submit() {
    global $USER, $SESSION;
    $count = count_records('notification_internal_activity', 'usr', $USER->get('id'));
    delete_records('notification_internal_activity', 'usr', $USER->get('id'));
    $SESSION->add_ok_msg(get_string('deletednotifications', 'activity', $count));
    redirect(get_config('wwwroot') . 'account/activity/index.php');
}

198
$smarty = smarty(array('tablerenderer'));
199
200
201
202
$smarty->assign('selectallread', 'toggleChecked(\'tocheckread\'); return false;');
$smarty->assign('selectalldel', 'toggleChecked(\'tocheckdel\'); return false;');
$smarty->assign('markread', 'markread(this, \'read\'); return false;');
$smarty->assign('markdel', 'markread(document.notificationlist, \'del\'); return false;');
203
$smarty->assign('typechange', 'activitylist.type = this.options[this.selectedIndex].value; activitylist.doupdate();');
204
205
$smarty->assign('types', $types);
$smarty->assign('INLINEJAVASCRIPT', $javascript);
206
$smarty->assign('PAGEHEADING', hsc(get_string('inbox')));
207
$smarty->assign('deleteall', $deleteall);
208
$smarty->display('account/activity/index.tpl');
209
?>