MDL-48931 navigation: Update to context header renderer.
[moodle.git] / mod / forum / user.php
CommitLineData
cd4e6b17 1<?php
f9a0ea69 2
8f685009
SH
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.
14//
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/>.
17
18/**
19 * Display user activity reports for a course
20 *
01030f1b 21 * @package mod_forum
8f685009
SH
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
f9a0ea69 25
4b715423
SH
26require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
27require_once($CFG->dirroot.'/mod/forum/lib.php');
28require_once($CFG->dirroot.'/rating/lib.php');
29
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
35
36if (empty($userid)) {
37 if (!isloggedin()) {
38 require_login();
39 }
40 $userid = $USER->id;
cd4e6b17 41}
4b715423
SH
42
43$discussionsonly = ($mode !== 'posts');
44$isspecificcourse = !is_null($courseid);
45$iscurrentuser = ($USER->id == $userid);
46
47$url = new moodle_url('/mod/forum/user.php', array('id' => $userid));
a161d96f
SH
48if ($isspecificcourse) {
49 $url->param('course', $courseid);
50}
51if ($discussionsonly) {
52 $url->param('mode', 'discussions');
53}
4b715423 54
cd4e6b17 55$PAGE->set_url($url);
241ce9c4 56$PAGE->set_pagelayout('standard');
cd4e6b17 57
a161d96f
SH
58if ($page != 0) {
59 $url->param('page', $page);
60}
61if ($perpage != 5) {
62 $url->param('perpage', $perpage);
63}
bb8f3759 64
4b715423 65$user = $DB->get_record("user", array("id" => $userid), '*', MUST_EXIST);
bf0f06b1 66$usercontext = context_user::instance($user->id, MUST_EXIST);
4b715423
SH
67// Check if the requested user is the guest user
68if (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');
bb8f3759 72}
4b715423 73// Make sure the user has not been deleted
bb8f3759 74if ($user->deleted) {
4b715423 75 $PAGE->set_title(get_string('userdeleted'));
0601e0ee 76 $PAGE->set_context(context_system::instance());
15ca5e5e 77 echo $OUTPUT->header();
4b715423 78 echo $OUTPUT->heading($PAGE->title);
396fb912 79 echo $OUTPUT->footer();
bb8f3759 80 die;
81}
f9a0ea69 82
4b715423
SH
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);
87
88// Check whether a specific course has been requested
89if ($isspecificcourse) {
90 // Get the requested course and its context
74df2951 91 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
bf0f06b1 92 $coursecontext = context_course::instance($courseid, MUST_EXIST);
4b715423
SH
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);
67875aa1 104 }
4b715423
SH
105 // Get the course ready for access checks
106 $courses = array($courseid => $course);
bb8f3759 107} else {
4b715423
SH
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();
880c5073 111 $PAGE->set_context(context_user::instance($user->id));
99fa9889 112
4b715423
SH
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);
116}
65bcf17b 117
22881392
DP
118$params = array(
119 'context' => $PAGE->context,
120 'relateduserid' => $user->id,
121 'other' => array('reportmode' => $mode),
122);
f3d98189 123$event = \mod_forum\event\user_report_viewed::create($params);
22881392
DP
124$event->trigger();
125
4b715423
SH
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);
128
129// Check whether there are not posts to display.
130if (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
136
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)));
145
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
bf0f06b1 153 if (has_capability('moodle/user:viewdetails', context_course::instance($sharedcourse->id))) {
4b715423
SH
154 $canviewuser = true;
155 break;
156 }
157 }
158 unset($sharedcourses);
159 }
2011a95b 160
4b715423
SH
161 // Prepare the page title
162 $pagetitle = get_string('noposts', 'mod_forum');
99fa9889 163
4b715423
SH
164 // Get the page heading
165 if ($isspecificcourse) {
e026cc92 166 $pageheading = format_string($course->fullname, true, array('context' => $coursecontext));
4b715423
SH
167 } else {
168 $pageheading = get_string('pluginname', 'mod_forum');
169 }
65bcf17b 170
4b715423
SH
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');
bb8f3759 178 } else {
4b715423 179 $notification = get_string('nopostsmadebyyou', 'forum');
bb8f3759 180 }
4b715423
SH
181 } else if ($canviewuser) {
182 $PAGE->navigation->extend_for_user($user);
183 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
184 $fullname = fullname($user);
185 if ($discussionsonly) {
186 $notification = get_string('nodiscussionsstartedby', 'forum', $fullname);
bb8f3759 187 } else {
4b715423 188 $notification = get_string('nopostsmadebyuser', 'forum', $fullname);
bb8f3759 189 }
4b715423
SH
190 } else {
191 // Don't extend the navigation it would be giving out information that
192 // the current uesr doesn't have access to.
193 $notification = get_string('cannotviewusersposts', 'forum');
194 if ($isspecificcourse) {
195 $url = new moodle_url('/course/view.php', array('id' => $courseid));
196 } else {
197 $url = new moodle_url('/');
198 }
199 navigation_node::override_active_url($url);
200 }
65bcf17b 201
4b715423
SH
202 // Display a page letting the user know that there's nothing to display;
203 $PAGE->set_title($pagetitle);
880c5073
AG
204 if ($isspecificcourse) {
205 $PAGE->set_heading($pageheading);
206 } else {
207 $PAGE->set_heading(fullname($user));
208 }
4b715423 209 echo $OUTPUT->header();
283ea788
AG
210 if (!$isspecificcourse) {
211 echo $OUTPUT->heading($pagetitle);
212 } else {
213 $userheading = array(
214 'heading' => fullname($user),
215 'user' => $user,
216 'usercontext' => $usercontext
217 );
218 echo $OUTPUT->context_header($userheading, 2);
219 }
4b715423 220 echo $OUTPUT->notification($notification);
a161d96f
SH
221 if (!$url->compare($PAGE->url)) {
222 echo $OUTPUT->continue_button($url);
223 }
4b715423
SH
224 echo $OUTPUT->footer();
225 die;
226}
227
228// Post output will contain an entry containing HTML to display each post by the
229// time we are done.
230$postoutput = array();
99fa9889 231
4b715423
SH
232$discussions = array();
233foreach ($result->posts as $post) {
234 $discussions[] = $post->discussion;
235}
236$discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions));
237
238//todo Rather than retrieving the ratings for each post individually it would be nice to do them in groups
239//however this requires creating arrays of posts with each array containing all of the posts from a particular forum,
240//retrieving the ratings then reassembling them all back into a single array sorted by post.modified (descending)
241$rm = new rating_manager();
242$ratingoptions = new stdClass;
243$ratingoptions->component = 'mod_forum';
244$ratingoptions->ratingarea = 'post';
245foreach ($result->posts as $post) {
246 if (!isset($result->forums[$post->forum]) || !isset($discussions[$post->discussion])) {
247 // Something very VERY dodgy has happened if we end up here
248 continue;
249 }
250 $forum = $result->forums[$post->forum];
251 $cm = $forum->cm;
252 $discussion = $discussions[$post->discussion];
253 $course = $result->courses[$discussion->course];
254
255 $forumurl = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
256 $discussionurl = new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion));
257
258 // load ratings
259 if ($forum->assessed != RATING_AGGREGATE_NONE) {
260 $ratingoptions->context = $cm->context;
261 $ratingoptions->items = array($post);
262 $ratingoptions->aggregate = $forum->assessed;//the aggregation method
263 $ratingoptions->scaleid = $forum->scale;
264 $ratingoptions->userid = $user->id;
265 $ratingoptions->assesstimestart = $forum->assesstimestart;
266 $ratingoptions->assesstimefinish = $forum->assesstimefinish;
267 if ($forum->type == 'single' or !$post->discussion) {
268 $ratingoptions->returnurl = $forumurl;
269 } else {
270 $ratingoptions->returnurl = $discussionurl;
bb8f3759 271 }
2011a95b 272
4b715423
SH
273 $updatedpost = $rm->get_ratings($ratingoptions);
274 //updating the array this way because we're iterating over a collection and updating them one by one
275 $result->posts[$updatedpost[0]->id] = $updatedpost[0];
276 }
277
bf0f06b1 278 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
4b715423
SH
279 $forumname = format_string($forum->name, true, array('context' => $cm->context));
280
281 $fullsubjects = array();
282 if (!$isspecificcourse && !$hasparentaccess) {
283 $fullsubjects[] = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $courseshortname);
284 $fullsubjects[] = html_writer::link($forumurl, $forumname);
285 } else {
286 $fullsubjects[] = html_writer::tag('span', $courseshortname);
287 $fullsubjects[] = html_writer::tag('span', $forumname);
288 }
289 if ($forum->type != 'single') {
290 $discussionname = format_string($discussion->name, true, array('context' => $cm->context));
291 if (!$isspecificcourse && !$hasparentaccess) {
292 $fullsubjects[] .= html_writer::link($discussionurl, $discussionname);
293 } else {
294 $fullsubjects[] .= html_writer::tag('span', $discussionname);
bb8f3759 295 }
4b715423
SH
296 if ($post->parent != 0) {
297 $postname = format_string($post->subject, true, array('context' => $cm->context));
298 if (!$isspecificcourse && !$hasparentaccess) {
299 $fullsubjects[] .= html_writer::link(new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion, 'parent' => $post->id)), $postname);
300 } else {
301 $fullsubjects[] .= html_writer::tag('span', $postname);
f9a0ea69 302 }
bb8f3759 303 }
bb8f3759 304 }
4b715423
SH
305 $post->subject = join(' -> ', $fullsubjects);
306 // This is really important, if the strings are formatted again all the links
307 // we've added will be lost.
308 $post->subjectnoformat = true;
309 $discussionurl->set_anchor('p'.$post->id);
310 $fulllink = html_writer::link($discussionurl, get_string("postincontext", "forum"));
311
312 $postoutput[] = forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, $fulllink, '', null, true, null, true);
313}
2011a95b 314
4b715423
SH
315$userfullname = fullname($user);
316
317if ($discussionsonly) {
318 $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname);
bb8f3759 319} else {
4b715423
SH
320 $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname);
321}
322if ($isspecificcourse) {
323 $a = new stdClass;
324 $a->fullname = $userfullname;
e026cc92 325 $a->coursename = format_string($course->fullname, true, array('context' => $coursecontext));
4b715423
SH
326 $pageheading = $a->coursename;
327 if ($discussionsonly) {
328 $pagetitle = get_string('discussionsstartedbyuserincourse', 'mod_forum', $a);
f9a0ea69 329 } else {
4b715423 330 $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a);
f9a0ea69 331 }
4b715423
SH
332} else {
333 $pagetitle = $inpageheading;
334 $pageheading = $userfullname;
bb8f3759 335}
f9a0ea69 336
4b715423 337$PAGE->set_title($pagetitle);
880c5073
AG
338$PAGE->set_heading($pageheading);
339
4b715423
SH
340$PAGE->navigation->extend_for_user($user);
341$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
342
343echo $OUTPUT->header();
344echo $OUTPUT->heading($inpageheading);
345echo html_writer::start_tag('div', array('class' => 'user-content'));
346
347if (!empty($postoutput)) {
348 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
349 foreach ($postoutput as $post) {
350 echo $post;
351 echo html_writer::empty_tag('br');
352 }
353 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
354} else if ($discussionsonly) {
355 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum', $userfullname));
356} else {
357 echo $OUTPUT->heading(get_string('noposts', 'forum'));
358}
359
360echo html_writer::end_tag('div');
361echo $OUTPUT->footer();