-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathview.php
356 lines (286 loc) · 13.5 KB
/
view.php
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Prints a particular instance of stamp collection module
*
* The script prints either user's own stamps or all stamps collected in this
* activity.
*
* @todo make the sortby and sorthow default values configurable per instance
*
* @package mod_stampcoll
* @copyright 2007 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/locallib.php');
require_once(dirname(__FILE__).'/addstamp_form.php');
$cmid = required_param('id', PARAM_INT); // Course module id.
$view = optional_param('view', 'all', PARAM_ALPHA); // Display mode all|own|single.
$userid = optional_param('userid', null, PARAM_INT); // View this single user.
$sortby = optional_param('sortby', 'lastname', PARAM_ALPHA); // Sort by this column.
$sorthow = optional_param('sorthow', 'ASC', PARAM_ALPHA); // Sort using this direction.
$page = optional_param('page', 0, PARAM_INT); // Show this page.
$updatepref = optional_param('updatepref', false, PARAM_BOOL); // Is the preferences form being saved?
$perpage = optional_param('perpage', stampcoll::USERS_PER_PAGE, PARAM_INT); // The 'Users per page' preference.
$cm = get_coursemodule_from_id('stampcoll', $cmid, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$stampcollr = $DB->get_record('stampcoll', array('id' => $cm->instance), '*', MUST_EXIST);
if (!in_array($view, array('own', 'all', 'single'))) {
$view = 'all';
}
if ($view == 'single' and is_null($userid)) {
$view = 'own';
}
if (!in_array($sortby, array('firstname', 'lastname', 'count'))) {
$sortby = 'lastname';
}
if ($sorthow != 'ASC' and $sorthow != 'DESC') {
$sorthow = 'ASC';
}
if ($page < 0) {
$page = 0;
}
require_login($course, true, $cm);
$stampcoll = new stampcoll($stampcollr, $cm, $course);
if ($view == 'single' and $userid == $USER->id) {
$view = 'own';
}
$PAGE->set_url(new moodle_url($stampcoll->view_url(), array('view' => $view)));
$PAGE->set_title($stampcoll->name);
$PAGE->set_heading($course->fullname);
$PAGE->set_button(html_writer::empty_tag('span', array('id' => 'mod_stampcoll_viewmode_toggle')));
$PAGE->requires->yui_module('moodle-mod_stampcoll-viewmode', 'M.mod_stampcoll.viewmode.init');
$PAGE->requires->strings_for_js(array('toggleviewmode'), 'mod_stampcoll');
require_capability('mod/stampcoll:view', $stampcoll->context);
$event = \mod_stampcoll\event\course_module_viewed::create(array(
'objectid' => $stampcoll->id,
'context' => $stampcoll->context,
'other' => array(
'viewmode' => $view,
),
));
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('stampcoll', $stampcollr);
$event->trigger();
if ($updatepref) {
require_sesskey();
if ($perpage > 0) {
set_user_preference('stampcoll_perpage', $perpage);
}
redirect($PAGE->url);
}
$canviewownstamps = has_capability('mod/stampcoll:viewownstamps', $stampcoll->context);
$canviewotherstamps = has_capability('mod/stampcoll:viewotherstamps', $stampcoll->context);
$canviewsomestamps = $canviewownstamps || $canviewotherstamps;
$canviewonlyownstamps = $canviewownstamps && (!$canviewotherstamps);
if (!$canviewownstamps and $view == 'own') {
$view = 'all';
}
if ($canviewonlyownstamps and ($view == 'all' or $view == 'single')) {
$view = 'own';
}
$output = $PAGE->get_renderer('mod_stampcoll');
echo $output->header();
echo $output->heading(format_string($stampcoll->name, false, array('context' => $stampcoll->context)));
if (trim($stampcoll->intro)) {
echo $output->box(format_module_intro('stampcoll', $stampcoll, $cmid), 'generalbox');
}
if (!$canviewsomestamps) {
notice(get_string('notallowedtoviewstamps', 'mod_stampcoll'), new moodle_url('/course/view.php', array('id' => $course->id)));
}
// View own stamps.
if ($view == 'own') {
if (!$canviewownstamps) {
throw new coding_exception('error in permission evaluation');
}
// Construct the sql returning all stamp info to display.
$sql = "SELECT s.id AS stampid, s.userid AS holderid, s.text AS stamptext,
s.timecreated AS stamptimecreated, s.timemodified AS stamptimemodified,".
user_picture::fields('gu', null, 'giverid', 'giver')."
FROM {stampcoll_stamps} s
LEFT JOIN {user} gu ON s.giver = gu.id AND gu.deleted = 0
WHERE s.stampcollid = :stampcollid AND s.userid = :holderid
ORDER BY s.timecreated";
$params = array('stampcollid' => $stampcoll->id, 'holderid' => $USER->id);
// Prepare the renderable collection.
$collection = new stampcoll_singleuser_collection($stampcoll, $USER);
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $record) {
if (!empty($record->giverid)) {
$collection->register_user(user_picture::unalias($record, null, 'giverid', 'giver'));
}
if (!empty($record->stampid)) {
$stamp = (object)array(
'id' => $record->stampid,
'userid' => $record->holderid,
'giver' => $record->giverid,
'text' => $record->stamptext,
'timecreated' => $record->stamptimecreated,
'timemodified' => $record->stamptimemodified,
);
$collection->add_stamp($stamp);
}
}
$rs->close();
echo $output->render($collection);
} else if ($view == 'single') {
// View someone else's stamps.
if (!$canviewotherstamps) {
throw new coding_exception('error in permission evaluation');
}
$user = $DB->get_record('user', array('id' => $userid), user_picture::fields(), MUST_EXIST);
if (!is_enrolled($stampcoll->context, $user->id, '', true)) {
notice(get_string('usernotenrolled', 'stampcoll'), new moodle_url('/course/view.php', array('id' => $course->id)));
}
// Construct the sql returning all stamp info to display.
$sql = "SELECT s.id AS stampid, s.userid AS holderid, s.text AS stamptext,
s.timecreated AS stamptimecreated, s.timemodified AS stamptimemodified,".
user_picture::fields('gu', null, 'giverid', 'giver')."
FROM {stampcoll_stamps} s
LEFT JOIN {user} gu ON s.giver = gu.id AND gu.deleted = 0
WHERE s.stampcollid = :stampcollid AND s.userid = :holderid
ORDER BY s.timecreated";
$params = array('stampcollid' => $stampcoll->id, 'holderid' => $user->id);
// Prepare the renderable collection.
$collection = new stampcoll_singleuser_collection($stampcoll, $user);
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $record) {
if (!empty($record->giverid)) {
$collection->register_user(user_picture::unalias($record, null, 'giverid', 'giver'));
}
if (!empty($record->stampid)) {
$stamp = (object)array(
'id' => $record->stampid,
'userid' => $record->holderid,
'giver' => $record->giverid,
'text' => $record->stamptext,
'timecreated' => $record->stamptimecreated,
'timemodified' => $record->stamptimemodified,
);
$collection->add_stamp($stamp);
}
}
$rs->close();
echo $output->render($collection);
// Append a form to give a new stamp.
if (has_capability('mod/stampcoll:collectstamps', $stampcoll->context, $user, false) and
has_capability('mod/stampcoll:givestamps', $stampcoll->context, $USER)) {
$form = new stampcoll_stamp_form(
new moodle_url('/mod/stampcoll/addstamp.php', array('scid' => $stampcoll->id)),
array(
'userfrom' => $USER,
),
'post', '', array('class' => 'stampform'));
$form->set_data(array(
'userfrom' => $USER->id,
'userto' => $user->id,
));
$form->display();
}
} else if ($view == 'all') {
// View all stamps.
if (!$canviewotherstamps) {
throw new coding_exception('error in permission evaluation');
}
$PAGE->url->param('sortby', $sortby);
$PAGE->url->param('sorthow', $sorthow);
$groupmode = groups_get_activity_groupmode($cm);
if ($groupmode == NOGROUPS) {
$groupid = false;
} else {
groups_print_activity_menu($cm, $PAGE->url);
$groupid = groups_get_activity_group($cm);
if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $stampcoll->context)) {
if (!groups_is_member($groupid)) {
// This should not happen but...
notice(get_string('groupusernotmember', 'core_error'),
new moodle_url('/course/view.php', array('id' => $course->id)));
}
}
}
// Get the sql returning all actively enrolled users who can collect stamps.
list($enrolsql, $enrolparams) = get_enrolled_sql($stampcoll->context, 'mod/stampcoll:collectstamps', $groupid, true);
// Determine how to join user and stamps tables.
if ($stampcoll->displayzero) {
$jointype = 'LEFT';
} else {
$jointype = 'INNER';
}
// In the first query, get the list of users to be displayed.
$sql = "SELECT COUNT(*)
FROM (SELECT DISTINCT(u.id)
FROM {user} u
JOIN ($enrolsql) eu ON u.id = eu.id
$jointype JOIN {stampcoll_stamps} s ON s.stampcollid = :stampcollid AND s.userid = u.id) t";
$params = array_merge($enrolparams, array('stampcollid' => $stampcoll->id));
$totalcount = $DB->count_records_sql($sql, $params);
// In the second query, get the list of user ids to display based on the sorting and paginating.
$sql = "SELECT u.id, u.firstname, u.lastname, COUNT(s.id) AS count
FROM {user} u
JOIN ($enrolsql) eu ON u.id = eu.id
$jointype JOIN {stampcoll_stamps} s ON s.stampcollid = :stampcollid AND s.userid = u.id
GROUP BY u.id, u.firstname, u.lastname
ORDER BY $sortby $sorthow";
$params = array_merge($enrolparams, array('stampcollid' => $stampcoll->id));
$perpage = get_user_preferences('stampcoll_perpage', stampcoll::USERS_PER_PAGE);
$userids = array_keys($DB->get_records_sql($sql, $params, $page * $perpage, $perpage));
// Prepare the renderable collection.
$collection = new stampcoll_multiuser_collection($stampcoll, $userids);
$collection->sortedby = $sortby;
$collection->sortedhow = $sorthow;
$collection->page = $page;
$collection->perpage = $perpage;
$collection->totalcount = $totalcount;
if ($userids) {
// In the third query, get all stamps info to display.
list($holdersql, $holderparam) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
$sql = "SELECT ".user_picture::fields('hu', null, 'holderid', 'holder').",
s.id AS stampid, s.text AS stamptext,
s.timecreated AS stamptimecreated, s.timemodified AS stamptimemodified,".
user_picture::fields('gu', null, 'giverid', 'giver')."
FROM {user} hu
$jointype JOIN {stampcoll_stamps} s ON s.stampcollid = :stampcollid AND s.userid = hu.id
LEFT JOIN {user} gu ON s.giver = gu.id AND gu.deleted = 0
WHERE hu.id $holdersql
ORDER BY s.timecreated";
$params = array_merge(array('stampcollid' => $stampcoll->id), $holderparam);
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $record) {
if (!empty($record->holderid)) {
$collection->register_holder(user_picture::unalias($record, null, 'holderid', 'holder'));
}
if (!empty($record->giverid)) {
$collection->register_user(user_picture::unalias($record, null, 'giverid', 'giver'));
}
if (!empty($record->stampid)) {
$stamp = (object)array(
'id' => $record->stampid,
'userid' => $record->holderid,
'giver' => $record->giverid,
'text' => $record->stamptext,
'timecreated' => $record->stamptimecreated,
'timemodified' => $record->stamptimemodified,
);
$collection->add_stamp($stamp);
}
}
$rs->close();
}
echo $output->render($collection);
}
echo $output->footer();