Merge branch 'wip-mdl-55924' of https://github.com/rajeshtaneja/moodle
[moodle.git] / mod / forum / user.php
blob5696c4a5a7dd025092e234101b0a08a7acee46d8
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Display user activity reports for a course
21 * @package mod_forum
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require(__DIR__.'/../../config.php');
27 require_once($CFG->dirroot.'/mod/forum/lib.php');
28 require_once($CFG->dirroot.'/rating/lib.php');
30 $courseid = optional_param('course', null, PARAM_INT); // Limit the posts to just this course
31 $userid = optional_param('id', $USER->id, PARAM_INT); // User id whose posts we want to view
32 $mode = optional_param('mode', 'posts', PARAM_ALPHA); // The mode to use. Either posts or discussions
33 $page = optional_param('page', 0, PARAM_INT); // The page number to display
34 $perpage = optional_param('perpage', 5, PARAM_INT); // The number of posts to display per page
36 if (empty($userid)) {
37 if (!isloggedin()) {
38 require_login();
40 $userid = $USER->id;
43 $discussionsonly = ($mode !== 'posts');
44 $isspecificcourse = !is_null($courseid);
45 $iscurrentuser = ($USER->id == $userid);
47 $url = new moodle_url('/mod/forum/user.php', array('id' => $userid));
48 if ($isspecificcourse) {
49 $url->param('course', $courseid);
51 if ($discussionsonly) {
52 $url->param('mode', 'discussions');
55 $PAGE->set_url($url);
56 $PAGE->set_pagelayout('standard');
58 if ($page != 0) {
59 $url->param('page', $page);
61 if ($perpage != 5) {
62 $url->param('perpage', $perpage);
65 $user = $DB->get_record("user", array("id" => $userid), '*', MUST_EXIST);
66 $usercontext = context_user::instance($user->id, MUST_EXIST);
67 // Check if the requested user is the guest user
68 if (isguestuser($user)) {
69 // The guest user cannot post, so it is not possible to view any posts.
70 // May as well just bail aggressively here.
71 print_error('invaliduserid');
73 // Make sure the user has not been deleted
74 if ($user->deleted) {
75 $PAGE->set_title(get_string('userdeleted'));
76 $PAGE->set_context(context_system::instance());
77 echo $OUTPUT->header();
78 echo $OUTPUT->heading($PAGE->title);
79 echo $OUTPUT->footer();
80 die;
83 $isloggedin = isloggedin();
84 $isguestuser = $isloggedin && isguestuser();
85 $isparent = !$iscurrentuser && $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id));
86 $hasparentaccess = $isparent && has_all_capabilities(array('moodle/user:viewdetails', 'moodle/user:readuserposts'), $usercontext);
88 // Check whether a specific course has been requested
89 if ($isspecificcourse) {
90 // Get the requested course and its context
91 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
92 $coursecontext = context_course::instance($courseid, MUST_EXIST);
93 // We have a specific course to search, which we will also assume we are within.
94 if ($hasparentaccess) {
95 // A `parent` role won't likely have access to the course so we won't attempt
96 // to enter it. We will however still make them jump through the normal
97 // login hoops
98 require_login();
99 $PAGE->set_context($coursecontext);
100 $PAGE->set_course($course);
101 } else {
102 // Enter the course we are searching
103 require_login($course);
105 // Get the course ready for access checks
106 $courses = array($courseid => $course);
107 } else {
108 // We are going to search for all of the users posts in all courses!
109 // a general require login here as we arn't actually within any course.
110 require_login();
111 $PAGE->set_context(context_user::instance($user->id));
113 // Now we need to get all of the courses to search.
114 // All courses where the user has posted within a forum will be returned.
115 $courses = forum_get_courses_user_posted_in($user, $discussionsonly);
118 $params = array(
119 'context' => $PAGE->context,
120 'relateduserid' => $user->id,
121 'other' => array('reportmode' => $mode),
123 $event = \mod_forum\event\user_report_viewed::create($params);
124 $event->trigger();
126 // Get the posts by the requested user that the current user can access.
127 $result = forum_get_posts_by_user($user, $courses, $isspecificcourse, $discussionsonly, ($page * $perpage), $perpage);
129 // Check whether there are not posts to display.
130 if (empty($result->posts)) {
131 // Ok no posts to display means that either the user has not posted or there
132 // are no posts made by the requested user that the current user is able to
133 // see.
134 // In either case we need to decide whether we can show personal information
135 // about the requested user to the current user so we will execute some checks
137 // First check the obvious, its the current user, a specific course has been
138 // provided (require_login has been called), or they have a course contact role.
139 // True to any of those and the current user can see the details of the
140 // requested user.
141 $canviewuser = ($iscurrentuser || $isspecificcourse || empty($CFG->forceloginforprofiles) || has_coursecontact_role($userid));
142 // Next we'll check the caps, if the current user has the view details and a
143 // specific course has been requested, or if they have the view all details
144 $canviewuser = ($canviewuser || ($isspecificcourse && has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewalldetails', $usercontext)));
146 // If none of the above was true the next step is to check a shared relation
147 // through some course
148 if (!$canviewuser) {
149 // Get all of the courses that the users have in common
150 $sharedcourses = enrol_get_shared_courses($USER->id, $user->id, true);
151 foreach ($sharedcourses as $sharedcourse) {
152 // Check the view cap within the course context
153 if (has_capability('moodle/user:viewdetails', context_course::instance($sharedcourse->id))) {
154 $canviewuser = true;
155 break;
158 unset($sharedcourses);
161 // Prepare the page title
162 $pagetitle = get_string('noposts', 'mod_forum');
164 // Get the page heading
165 if ($isspecificcourse) {
166 $pageheading = format_string($course->fullname, true, array('context' => $coursecontext));
167 } else {
168 $pageheading = get_string('pluginname', 'mod_forum');
171 // Next we need to set up the loading of the navigation and choose a message
172 // to display to the current user.
173 if ($iscurrentuser) {
174 // No need to extend the navigation it happens automatically for the
175 // current user.
176 if ($discussionsonly) {
177 $notification = get_string('nodiscussionsstartedbyyou', 'forum');
178 } else {
179 $notification = get_string('nopostsmadebyyou', 'forum');
181 // These are the user's forum interactions.
182 // Shut down the navigation 'Users' node.
183 $usernode = $PAGE->navigation->find('users', null);
184 $usernode->make_inactive();
185 // Edit navbar.
186 if (isset($courseid) && $courseid != SITEID) {
187 // Create as much of the navbar automatically.
188 $newusernode = $PAGE->navigation->find('user' . $user->id, null);
189 $newusernode->make_active();
190 // Check to see if this is a discussion or a post.
191 if ($mode == 'posts') {
192 $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
193 array('id' => $user->id, 'course' => $courseid)));
194 } else {
195 $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
196 array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
199 } else if ($canviewuser) {
200 $PAGE->navigation->extend_for_user($user);
201 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
203 // Edit navbar.
204 if (isset($courseid) && $courseid != SITEID) {
205 // Create as much of the navbar automatically.
206 $usernode = $PAGE->navigation->find('user' . $user->id, null);
207 $usernode->make_active();
208 // Check to see if this is a discussion or a post.
209 if ($mode == 'posts') {
210 $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
211 array('id' => $user->id, 'course' => $courseid)));
212 } else {
213 $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
214 array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
218 $fullname = fullname($user);
219 if ($discussionsonly) {
220 $notification = get_string('nodiscussionsstartedby', 'forum', $fullname);
221 } else {
222 $notification = get_string('nopostsmadebyuser', 'forum', $fullname);
224 } else {
225 // Don't extend the navigation it would be giving out information that
226 // the current uesr doesn't have access to.
227 $notification = get_string('cannotviewusersposts', 'forum');
228 if ($isspecificcourse) {
229 $url = new moodle_url('/course/view.php', array('id' => $courseid));
230 } else {
231 $url = new moodle_url('/');
233 navigation_node::override_active_url($url);
236 // Display a page letting the user know that there's nothing to display;
237 $PAGE->set_title($pagetitle);
238 if ($isspecificcourse) {
239 $PAGE->set_heading($pageheading);
240 } else {
241 $PAGE->set_heading(fullname($user));
243 echo $OUTPUT->header();
244 if (!$isspecificcourse) {
245 echo $OUTPUT->heading($pagetitle);
246 } else {
247 $userheading = array(
248 'heading' => fullname($user),
249 'user' => $user,
250 'usercontext' => $usercontext
252 echo $OUTPUT->context_header($userheading, 2);
254 echo $OUTPUT->notification($notification);
255 if (!$url->compare($PAGE->url)) {
256 echo $OUTPUT->continue_button($url);
258 echo $OUTPUT->footer();
259 die;
262 // Post output will contain an entry containing HTML to display each post by the
263 // time we are done.
264 $postoutput = array();
266 $discussions = array();
267 foreach ($result->posts as $post) {
268 $discussions[] = $post->discussion;
270 $discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions));
272 //todo Rather than retrieving the ratings for each post individually it would be nice to do them in groups
273 //however this requires creating arrays of posts with each array containing all of the posts from a particular forum,
274 //retrieving the ratings then reassembling them all back into a single array sorted by post.modified (descending)
275 $rm = new rating_manager();
276 $ratingoptions = new stdClass;
277 $ratingoptions->component = 'mod_forum';
278 $ratingoptions->ratingarea = 'post';
279 foreach ($result->posts as $post) {
280 if (!isset($result->forums[$post->forum]) || !isset($discussions[$post->discussion])) {
281 // Something very VERY dodgy has happened if we end up here
282 continue;
284 $forum = $result->forums[$post->forum];
285 $cm = $forum->cm;
286 $discussion = $discussions[$post->discussion];
287 $course = $result->courses[$discussion->course];
289 $forumurl = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
290 $discussionurl = new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion));
292 // load ratings
293 if ($forum->assessed != RATING_AGGREGATE_NONE) {
294 $ratingoptions->context = $cm->context;
295 $ratingoptions->items = array($post);
296 $ratingoptions->aggregate = $forum->assessed;//the aggregation method
297 $ratingoptions->scaleid = $forum->scale;
298 $ratingoptions->userid = $user->id;
299 $ratingoptions->assesstimestart = $forum->assesstimestart;
300 $ratingoptions->assesstimefinish = $forum->assesstimefinish;
301 if ($forum->type == 'single' or !$post->discussion) {
302 $ratingoptions->returnurl = $forumurl;
303 } else {
304 $ratingoptions->returnurl = $discussionurl;
307 $updatedpost = $rm->get_ratings($ratingoptions);
308 //updating the array this way because we're iterating over a collection and updating them one by one
309 $result->posts[$updatedpost[0]->id] = $updatedpost[0];
312 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
313 $forumname = format_string($forum->name, true, array('context' => $cm->context));
315 $fullsubjects = array();
316 if (!$isspecificcourse && !$hasparentaccess) {
317 $fullsubjects[] = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $courseshortname);
318 $fullsubjects[] = html_writer::link($forumurl, $forumname);
319 } else {
320 $fullsubjects[] = html_writer::tag('span', $courseshortname);
321 $fullsubjects[] = html_writer::tag('span', $forumname);
323 if ($forum->type != 'single') {
324 $discussionname = format_string($discussion->name, true, array('context' => $cm->context));
325 if (!$isspecificcourse && !$hasparentaccess) {
326 $fullsubjects[] .= html_writer::link($discussionurl, $discussionname);
327 } else {
328 $fullsubjects[] .= html_writer::tag('span', $discussionname);
330 if ($post->parent != 0) {
331 $postname = format_string($post->subject, true, array('context' => $cm->context));
332 if (!$isspecificcourse && !$hasparentaccess) {
333 $fullsubjects[] .= html_writer::link(new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion, 'parent' => $post->id)), $postname);
334 } else {
335 $fullsubjects[] .= html_writer::tag('span', $postname);
339 $post->subject = join(' -> ', $fullsubjects);
340 // This is really important, if the strings are formatted again all the links
341 // we've added will be lost.
342 $post->subjectnoformat = true;
343 $discussionurl->set_anchor('p'.$post->id);
344 $fulllink = html_writer::link($discussionurl, get_string("postincontext", "forum"));
346 $postoutput[] = forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, $fulllink, '', null, true, null, true);
349 $userfullname = fullname($user);
351 if ($discussionsonly) {
352 $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname);
353 } else {
354 $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname);
356 if ($isspecificcourse) {
357 $a = new stdClass;
358 $a->fullname = $userfullname;
359 $a->coursename = format_string($course->fullname, true, array('context' => $coursecontext));
360 $pageheading = $a->coursename;
361 if ($discussionsonly) {
362 $pagetitle = get_string('discussionsstartedbyuserincourse', 'mod_forum', $a);
363 } else {
364 $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a);
366 } else {
367 $pagetitle = $inpageheading;
368 $pageheading = $userfullname;
371 $PAGE->set_title($pagetitle);
372 $PAGE->set_heading($pageheading);
374 $PAGE->navigation->extend_for_user($user);
375 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
377 // Edit navbar.
378 if (isset($courseid) && $courseid != SITEID) {
379 $usernode = $PAGE->navigation->find('user' . $user->id , null);
380 $usernode->make_active();
381 // Check to see if this is a discussion or a post.
382 if ($mode == 'posts') {
383 $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
384 array('id' => $user->id, 'course' => $courseid)));
385 } else {
386 $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
387 array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
391 echo $OUTPUT->header();
392 echo html_writer::start_tag('div', array('class' => 'user-content'));
394 if ($isspecificcourse) {
395 $userheading = array(
396 'heading' => fullname($user),
397 'user' => $user,
398 'usercontext' => $usercontext
400 echo $OUTPUT->context_header($userheading, 2);
401 } else {
402 echo $OUTPUT->heading($inpageheading);
405 if (!empty($postoutput)) {
406 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
407 foreach ($postoutput as $post) {
408 echo $post;
409 echo html_writer::empty_tag('br');
411 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
412 } else if ($discussionsonly) {
413 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum', $userfullname));
414 } else {
415 echo $OUTPUT->heading(get_string('noposts', 'forum'));
418 echo html_writer::end_tag('div');
419 echo $OUTPUT->footer();