MDL-48930 navigation: Update to forum page headers.
[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
SH
209 echo $OUTPUT->header();
210 echo $OUTPUT->heading($pagetitle);
211 echo $OUTPUT->notification($notification);
a161d96f
SH
212 if (!$url->compare($PAGE->url)) {
213 echo $OUTPUT->continue_button($url);
214 }
4b715423
SH
215 echo $OUTPUT->footer();
216 die;
217}
218
219// Post output will contain an entry containing HTML to display each post by the
220// time we are done.
221$postoutput = array();
99fa9889 222
4b715423
SH
223$discussions = array();
224foreach ($result->posts as $post) {
225 $discussions[] = $post->discussion;
226}
227$discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions));
228
229//todo Rather than retrieving the ratings for each post individually it would be nice to do them in groups
230//however this requires creating arrays of posts with each array containing all of the posts from a particular forum,
231//retrieving the ratings then reassembling them all back into a single array sorted by post.modified (descending)
232$rm = new rating_manager();
233$ratingoptions = new stdClass;
234$ratingoptions->component = 'mod_forum';
235$ratingoptions->ratingarea = 'post';
236foreach ($result->posts as $post) {
237 if (!isset($result->forums[$post->forum]) || !isset($discussions[$post->discussion])) {
238 // Something very VERY dodgy has happened if we end up here
239 continue;
240 }
241 $forum = $result->forums[$post->forum];
242 $cm = $forum->cm;
243 $discussion = $discussions[$post->discussion];
244 $course = $result->courses[$discussion->course];
245
246 $forumurl = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
247 $discussionurl = new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion));
248
249 // load ratings
250 if ($forum->assessed != RATING_AGGREGATE_NONE) {
251 $ratingoptions->context = $cm->context;
252 $ratingoptions->items = array($post);
253 $ratingoptions->aggregate = $forum->assessed;//the aggregation method
254 $ratingoptions->scaleid = $forum->scale;
255 $ratingoptions->userid = $user->id;
256 $ratingoptions->assesstimestart = $forum->assesstimestart;
257 $ratingoptions->assesstimefinish = $forum->assesstimefinish;
258 if ($forum->type == 'single' or !$post->discussion) {
259 $ratingoptions->returnurl = $forumurl;
260 } else {
261 $ratingoptions->returnurl = $discussionurl;
bb8f3759 262 }
2011a95b 263
4b715423
SH
264 $updatedpost = $rm->get_ratings($ratingoptions);
265 //updating the array this way because we're iterating over a collection and updating them one by one
266 $result->posts[$updatedpost[0]->id] = $updatedpost[0];
267 }
268
bf0f06b1 269 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
4b715423
SH
270 $forumname = format_string($forum->name, true, array('context' => $cm->context));
271
272 $fullsubjects = array();
273 if (!$isspecificcourse && !$hasparentaccess) {
274 $fullsubjects[] = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $courseshortname);
275 $fullsubjects[] = html_writer::link($forumurl, $forumname);
276 } else {
277 $fullsubjects[] = html_writer::tag('span', $courseshortname);
278 $fullsubjects[] = html_writer::tag('span', $forumname);
279 }
280 if ($forum->type != 'single') {
281 $discussionname = format_string($discussion->name, true, array('context' => $cm->context));
282 if (!$isspecificcourse && !$hasparentaccess) {
283 $fullsubjects[] .= html_writer::link($discussionurl, $discussionname);
284 } else {
285 $fullsubjects[] .= html_writer::tag('span', $discussionname);
bb8f3759 286 }
4b715423
SH
287 if ($post->parent != 0) {
288 $postname = format_string($post->subject, true, array('context' => $cm->context));
289 if (!$isspecificcourse && !$hasparentaccess) {
290 $fullsubjects[] .= html_writer::link(new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion, 'parent' => $post->id)), $postname);
291 } else {
292 $fullsubjects[] .= html_writer::tag('span', $postname);
f9a0ea69 293 }
bb8f3759 294 }
bb8f3759 295 }
4b715423
SH
296 $post->subject = join(' -> ', $fullsubjects);
297 // This is really important, if the strings are formatted again all the links
298 // we've added will be lost.
299 $post->subjectnoformat = true;
300 $discussionurl->set_anchor('p'.$post->id);
301 $fulllink = html_writer::link($discussionurl, get_string("postincontext", "forum"));
302
303 $postoutput[] = forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, $fulllink, '', null, true, null, true);
304}
2011a95b 305
4b715423
SH
306$userfullname = fullname($user);
307
308if ($discussionsonly) {
309 $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname);
bb8f3759 310} else {
4b715423
SH
311 $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname);
312}
313if ($isspecificcourse) {
314 $a = new stdClass;
315 $a->fullname = $userfullname;
e026cc92 316 $a->coursename = format_string($course->fullname, true, array('context' => $coursecontext));
4b715423
SH
317 $pageheading = $a->coursename;
318 if ($discussionsonly) {
319 $pagetitle = get_string('discussionsstartedbyuserincourse', 'mod_forum', $a);
f9a0ea69 320 } else {
4b715423 321 $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a);
f9a0ea69 322 }
4b715423
SH
323} else {
324 $pagetitle = $inpageheading;
325 $pageheading = $userfullname;
bb8f3759 326}
f9a0ea69 327
4b715423 328$PAGE->set_title($pagetitle);
880c5073
AG
329$PAGE->set_heading($pageheading);
330
4b715423
SH
331$PAGE->navigation->extend_for_user($user);
332$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
333
334echo $OUTPUT->header();
335echo $OUTPUT->heading($inpageheading);
336echo html_writer::start_tag('div', array('class' => 'user-content'));
337
338if (!empty($postoutput)) {
339 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
340 foreach ($postoutput as $post) {
341 echo $post;
342 echo html_writer::empty_tag('br');
343 }
344 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
345} else if ($discussionsonly) {
346 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum', $userfullname));
347} else {
348 echo $OUTPUT->heading(get_string('noposts', 'forum'));
349}
350
351echo html_writer::end_tag('div');
352echo $OUTPUT->footer();