-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.php
409 lines (368 loc) · 17.2 KB
/
user.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?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/>.
/**
* Display user activity reports for a course
*
* @package mod_cybrary
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once($CFG->dirroot.'/mod/cybrary/lib.php');
require_once($CFG->dirroot.'/rating/lib.php');
$courseid = optional_param('course', null, PARAM_INT); // Limit the posts to just this course
$userid = optional_param('id', $USER->id, PARAM_INT); // User id whose posts we want to view
$mode = optional_param('mode', 'posts', PARAM_ALPHA); // The mode to use. Either posts or discussions
$page = optional_param('page', 0, PARAM_INT); // The page number to display
$perpage = optional_param('perpage', 5, PARAM_INT); // The number of posts to display per page
if (empty($userid)) {
if (!isloggedin()) {
require_login();
}
$userid = $USER->id;
}
$discussionsonly = ($mode !== 'posts');
$isspecificcourse = !is_null($courseid);
$iscurrentuser = ($USER->id == $userid);
$url = new moodle_url('/mod/cybrary/user.php', array('id' => $userid));
if ($isspecificcourse) {
$url->param('course', $courseid);
}
if ($discussionsonly) {
$url->param('mode', 'discussions');
}
$PAGE->set_url($url);
$PAGE->set_pagelayout('standard');
if ($page != 0) {
$url->param('page', $page);
}
if ($perpage != 5) {
$url->param('perpage', $perpage);
}
$user = $DB->get_record("user", array("id" => $userid), '*', MUST_EXIST);
$usercontext = context_user::instance($user->id, MUST_EXIST);
// Check if the requested user is the guest user
if (isguestuser($user)) {
// The guest user cannot post, so it is not possible to view any posts.
// May as well just bail aggressively here.
print_error('invaliduserid');
}
// Make sure the user has not been deleted
if ($user->deleted) {
$PAGE->set_title(get_string('userdeleted'));
$PAGE->set_context(context_system::instance());
echo $OUTPUT->header();
echo $OUTPUT->heading($PAGE->title);
echo $OUTPUT->footer();
die;
}
$isloggedin = isloggedin();
$isguestuser = $isloggedin && isguestuser();
$isparent = !$iscurrentuser && $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id));
$hasparentaccess = $isparent && has_all_capabilities(array('moodle/user:viewdetails', 'moodle/user:readuserposts'), $usercontext);
// Check whether a specific course has been requested
if ($isspecificcourse) {
// Get the requested course and its context
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$coursecontext = context_course::instance($courseid, MUST_EXIST);
// We have a specific course to search, which we will also assume we are within.
if ($hasparentaccess) {
// A `parent` role won't likely have access to the course so we won't attempt
// to enter it. We will however still make them jump through the normal
// login hoops
require_login();
$PAGE->set_context($coursecontext);
$PAGE->set_course($course);
} else {
// Enter the course we are searching
require_login($course);
}
// Get the course ready for access checks
$courses = array($courseid => $course);
} else {
// We are going to search for all of the users posts in all courses!
// a general require login here as we arn't actually within any course.
require_login();
$PAGE->set_context(context_user::instance($user->id));
// Now we need to get all of the courses to search.
// All courses where the user has posted within a cybrary will be returned.
$courses = cybrary_get_courses_user_posted_in($user, $discussionsonly);
}
$params = array(
'context' => $PAGE->context,
'relateduserid' => $user->id,
'other' => array('reportmode' => $mode),
);
$event = \mod_cybrary\event\user_report_viewed::create($params);
$event->trigger();
// Get the posts by the requested user that the current user can access.
$result = cybrary_get_posts_by_user($user, $courses, $isspecificcourse, $discussionsonly, ($page * $perpage), $perpage);
// Check whether there are not posts to display.
if (empty($result->posts)) {
// Ok no posts to display means that either the user has not posted or there
// are no posts made by the requested user that the current user is able to
// see.
// In either case we need to decide whether we can show personal information
// about the requested user to the current user so we will execute some checks
// First check the obvious, its the current user, a specific course has been
// provided (require_login has been called), or they have a course contact role.
// True to any of those and the current user can see the details of the
// requested user.
$canviewuser = ($iscurrentuser || $isspecificcourse || empty($CFG->forceloginforprofiles) || has_coursecontact_role($userid));
// Next we'll check the caps, if the current user has the view details and a
// specific course has been requested, or if they have the view all details
$canviewuser = ($canviewuser || ($isspecificcourse && has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewalldetails', $usercontext)));
// If none of the above was true the next step is to check a shared relation
// through some course
if (!$canviewuser) {
// Get all of the courses that the users have in common
$sharedcourses = enrol_get_shared_courses($USER->id, $user->id, true);
foreach ($sharedcourses as $sharedcourse) {
// Check the view cap within the course context
if (has_capability('moodle/user:viewdetails', context_course::instance($sharedcourse->id))) {
$canviewuser = true;
break;
}
}
unset($sharedcourses);
}
// Prepare the page title
$pagetitle = get_string('noposts', 'mod_cybrary');
// Get the page heading
if ($isspecificcourse) {
$pageheading = format_string($course->fullname, true, array('context' => $coursecontext));
} else {
$pageheading = get_string('pluginname', 'mod_cybrary');
}
// Next we need to set up the loading of the navigation and choose a message
// to display to the current user.
if ($iscurrentuser) {
// No need to extend the navigation it happens automatically for the
// current user.
if ($discussionsonly) {
$notification = get_string('nodiscussionsstartedbyyou', 'cybrary');
} else {
$notification = get_string('nopostsmadebyyou', 'cybrary');
}
// These are the user's cybrary interactions.
// Shut down the navigation 'Users' node.
$usernode = $PAGE->navigation->find('users', null);
$usernode->make_inactive();
// Edit navbar.
if (isset($courseid) && $courseid != SITEID) {
// Create as much of the navbar automatically.
$newusernode = $PAGE->navigation->find('user' . $user->id, null);
$newusernode->make_active();
// Check to see if this is a discussion or a post.
if ($mode == 'posts') {
$navbar = $PAGE->navbar->add(get_string('posts', 'cybrary'), new moodle_url('/mod/cybrary/user.php',
array('id' => $user->id, 'course' => $courseid)));
} else {
$navbar = $PAGE->navbar->add(get_string('discussions', 'cybrary'), new moodle_url('/mod/cybrary/user.php',
array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
}
}
} else if ($canviewuser) {
$PAGE->navigation->extend_for_user($user);
$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
// Edit navbar.
if (isset($courseid) && $courseid != SITEID) {
// Create as much of the navbar automatically.
$usernode = $PAGE->navigation->find('user' . $user->id, null);
$usernode->make_active();
// Check to see if this is a discussion or a post.
if ($mode == 'posts') {
$navbar = $PAGE->navbar->add(get_string('posts', 'cybrary'), new moodle_url('/mod/cybrary/user.php',
array('id' => $user->id, 'course' => $courseid)));
} else {
$navbar = $PAGE->navbar->add(get_string('discussions', 'cybrary'), new moodle_url('/mod/cybrary/user.php',
array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
}
}
$fullname = fullname($user);
if ($discussionsonly) {
$notification = get_string('nodiscussionsstartedby', 'cybrary', $fullname);
} else {
$notification = get_string('nopostsmadebyuser', 'cybrary', $fullname);
}
} else {
// Don't extend the navigation it would be giving out information that
// the current uesr doesn't have access to.
$notification = get_string('cannotviewusersposts', 'cybrary');
if ($isspecificcourse) {
$url = new moodle_url('/course/view.php', array('id' => $courseid));
} else {
$url = new moodle_url('/');
}
navigation_node::override_active_url($url);
}
// Display a page letting the user know that there's nothing to display;
$PAGE->set_title($pagetitle);
if ($isspecificcourse) {
$PAGE->set_heading($pageheading);
} else {
$PAGE->set_heading(fullname($user));
}
echo $OUTPUT->header();
if (!$isspecificcourse) {
echo $OUTPUT->heading($pagetitle);
} else {
$userheading = array(
'heading' => fullname($user),
'user' => $user,
'usercontext' => $usercontext
);
echo $OUTPUT->context_header($userheading, 2);
}
echo $OUTPUT->notification($notification);
if (!$url->compare($PAGE->url)) {
echo $OUTPUT->continue_button($url);
}
echo $OUTPUT->footer();
die;
}
// Post output will contain an entry containing HTML to display each post by the
// time we are done.
$postoutput = array();
$discussions = array();
foreach ($result->posts as $post) {
$discussions[] = $post->discussion;
}
$discussions = $DB->get_records_list('cybrary_discussions', 'id', array_unique($discussions));
//todo Rather than retrieving the ratings for each post individually it would be nice to do them in groups
//however this requires creating arrays of posts with each array containing all of the posts from a particular cybrary,
//retrieving the ratings then reassembling them all back into a single array sorted by post.modified (descending)
$rm = new rating_manager();
$ratingoptions = new stdClass;
$ratingoptions->component = 'mod_cybrary';
$ratingoptions->ratingarea = 'post';
foreach ($result->posts as $post) {
if (!isset($result->cybraries[$post->cybrary]) || !isset($discussions[$post->discussion])) {
// Something very VERY dodgy has happened if we end up here
continue;
}
$cybrary = $result->cybraries[$post->cybrary];
$cm = $cybrary->cm;
$discussion = $discussions[$post->discussion];
$course = $result->courses[$discussion->course];
$cybraryurl = new moodle_url('/mod/cybrary/view.php', array('id' => $cm->id));
$discussionurl = new moodle_url('/mod/cybrary/discuss.php', array('d' => $post->discussion));
// load ratings
if ($cybrary->assessed != RATING_AGGREGATE_NONE) {
$ratingoptions->context = $cm->context;
$ratingoptions->items = array($post);
$ratingoptions->aggregate = $cybrary->assessed;//the aggregation method
$ratingoptions->scaleid = $cybrary->scale;
$ratingoptions->userid = $user->id;
$ratingoptions->assesstimestart = $cybrary->assesstimestart;
$ratingoptions->assesstimefinish = $cybrary->assesstimefinish;
if ($cybrary->type == 'single' or !$post->discussion) {
$ratingoptions->returnurl = $cybraryurl;
} else {
$ratingoptions->returnurl = $discussionurl;
}
$updatedpost = $rm->get_ratings($ratingoptions);
//updating the array this way because we're iterating over a collection and updating them one by one
$result->posts[$updatedpost[0]->id] = $updatedpost[0];
}
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
$cybraryname = format_string($cybrary->name, true, array('context' => $cm->context));
$fullsubjects = array();
if (!$isspecificcourse && !$hasparentaccess) {
$fullsubjects[] = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $courseshortname);
$fullsubjects[] = html_writer::link($cybraryurl, $cybraryname);
} else {
$fullsubjects[] = html_writer::tag('span', $courseshortname);
$fullsubjects[] = html_writer::tag('span', $cybraryname);
}
if ($cybrary->type != 'single') {
$discussionname = format_string($discussion->name, true, array('context' => $cm->context));
if (!$isspecificcourse && !$hasparentaccess) {
$fullsubjects[] .= html_writer::link($discussionurl, $discussionname);
} else {
$fullsubjects[] .= html_writer::tag('span', $discussionname);
}
if ($post->parent != 0) {
$postname = format_string($post->subject, true, array('context' => $cm->context));
if (!$isspecificcourse && !$hasparentaccess) {
$fullsubjects[] .= html_writer::link(new moodle_url('/mod/cybrary/discuss.php', array('d' => $post->discussion, 'parent' => $post->id)), $postname);
} else {
$fullsubjects[] .= html_writer::tag('span', $postname);
}
}
}
$post->subject = join(' -> ', $fullsubjects);
// This is really important, if the strings are formatted again all the links
// we've added will be lost.
$post->subjectnoformat = true;
$discussionurl->set_anchor('p'.$post->id);
$fulllink = html_writer::link($discussionurl, get_string("postincontext", "cybrary"));
$postoutput[] = cybrary_print_post($post, $discussion, $cybrary, $cm, $course, false, false, false, $fulllink, '', null, true, null, true);
}
$userfullname = fullname($user);
if ($discussionsonly) {
$inpageheading = get_string('discussionsstartedby', 'mod_cybrary', $userfullname);
} else {
$inpageheading = get_string('postsmadebyuser', 'mod_cybrary', $userfullname);
}
if ($isspecificcourse) {
$a = new stdClass;
$a->fullname = $userfullname;
$a->coursename = format_string($course->fullname, true, array('context' => $coursecontext));
$pageheading = $a->coursename;
if ($discussionsonly) {
$pagetitle = get_string('discussionsstartedbyuserincourse', 'mod_cybrary', $a);
} else {
$pagetitle = get_string('postsmadebyuserincourse', 'mod_cybrary', $a);
}
} else {
$pagetitle = $inpageheading;
$pageheading = $userfullname;
}
$PAGE->set_title($pagetitle);
$PAGE->set_heading($pageheading);
$PAGE->navigation->extend_for_user($user);
$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
// Edit navbar.
if (isset($courseid) && $courseid != SITEID) {
$usernode = $PAGE->navigation->find('user' . $user->id , null);
$usernode->make_active();
// Check to see if this is a discussion or a post.
if ($mode == 'posts') {
$navbar = $PAGE->navbar->add(get_string('posts', 'cybrary'), new moodle_url('/mod/cybrary/user.php',
array('id' => $user->id, 'course' => $courseid)));
} else {
$navbar = $PAGE->navbar->add(get_string('discussions', 'cybrary'), new moodle_url('/mod/cybrary/user.php',
array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
}
}
echo $OUTPUT->header();
echo $OUTPUT->heading($inpageheading);
echo html_writer::start_tag('div', array('class' => 'user-content'));
if (!empty($postoutput)) {
echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
foreach ($postoutput as $post) {
echo $post;
echo html_writer::empty_tag('br');
}
echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
} else if ($discussionsonly) {
echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'cybrary', $userfullname));
} else {
echo $OUTPUT->heading(get_string('noposts', 'cybrary'));
}
echo html_writer::end_tag('div');
echo $OUTPUT->footer();