MDL-48934 navigation: Update the default entries in the user menu.
[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();
0601e0ee 111 $PAGE->set_context(context_system::instance());
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
119$params = array(
120 'context' => $PAGE->context,
121 'relateduserid' => $user->id,
122 'other' => array('reportmode' => $mode),
123);
f3d98189 124$event = \mod_forum\event\user_report_viewed::create($params);
22881392
DP
125$event->trigger();
126
4b715423
SH
127// Get the posts by the requested user that the current user can access.
128$result = forum_get_posts_by_user($user, $courses, $isspecificcourse, $discussionsonly, ($page * $perpage), $perpage);
129
130// Check whether there are not posts to display.
131if (empty($result->posts)) {
132 // Ok no posts to display means that either the user has not posted or there
133 // are no posts made by the requested user that the current user is able to
134 // see.
135 // In either case we need to decide whether we can show personal information
136 // about the requested user to the current user so we will execute some checks
137
138 // First check the obvious, its the current user, a specific course has been
139 // provided (require_login has been called), or they have a course contact role.
140 // True to any of those and the current user can see the details of the
141 // requested user.
142 $canviewuser = ($iscurrentuser || $isspecificcourse || empty($CFG->forceloginforprofiles) || has_coursecontact_role($userid));
143 // Next we'll check the caps, if the current user has the view details and a
144 // specific course has been requested, or if they have the view all details
145 $canviewuser = ($canviewuser || ($isspecificcourse && has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewalldetails', $usercontext)));
146
147 // If none of the above was true the next step is to check a shared relation
148 // through some course
149 if (!$canviewuser) {
150 // Get all of the courses that the users have in common
151 $sharedcourses = enrol_get_shared_courses($USER->id, $user->id, true);
152 foreach ($sharedcourses as $sharedcourse) {
153 // Check the view cap within the course context
bf0f06b1 154 if (has_capability('moodle/user:viewdetails', context_course::instance($sharedcourse->id))) {
4b715423
SH
155 $canviewuser = true;
156 break;
157 }
158 }
159 unset($sharedcourses);
160 }
2011a95b 161
4b715423
SH
162 // Prepare the page title
163 $pagetitle = get_string('noposts', 'mod_forum');
99fa9889 164
4b715423
SH
165 // Get the page heading
166 if ($isspecificcourse) {
167 $pageheading = format_string($course->shortname, true, array('context' => $coursecontext));
168 } else {
169 $pageheading = get_string('pluginname', 'mod_forum');
170 }
65bcf17b 171
4b715423
SH
172 // Next we need to set up the loading of the navigation and choose a message
173 // to display to the current user.
174 if ($iscurrentuser) {
175 // No need to extend the navigation it happens automatically for the
176 // current user.
177 if ($discussionsonly) {
178 $notification = get_string('nodiscussionsstartedbyyou', 'forum');
bb8f3759 179 } else {
4b715423 180 $notification = get_string('nopostsmadebyyou', 'forum');
bb8f3759 181 }
4b715423
SH
182 } else if ($canviewuser) {
183 $PAGE->navigation->extend_for_user($user);
184 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
185 $fullname = fullname($user);
186 if ($discussionsonly) {
187 $notification = get_string('nodiscussionsstartedby', 'forum', $fullname);
bb8f3759 188 } else {
4b715423 189 $notification = get_string('nopostsmadebyuser', 'forum', $fullname);
bb8f3759 190 }
4b715423
SH
191 } else {
192 // Don't extend the navigation it would be giving out information that
193 // the current uesr doesn't have access to.
194 $notification = get_string('cannotviewusersposts', 'forum');
195 if ($isspecificcourse) {
196 $url = new moodle_url('/course/view.php', array('id' => $courseid));
197 } else {
198 $url = new moodle_url('/');
199 }
200 navigation_node::override_active_url($url);
201 }
65bcf17b 202
4b715423
SH
203 // Display a page letting the user know that there's nothing to display;
204 $PAGE->set_title($pagetitle);
205 $PAGE->set_heading($pageheading);
206 echo $OUTPUT->header();
207 echo $OUTPUT->heading($pagetitle);
208 echo $OUTPUT->notification($notification);
a161d96f
SH
209 if (!$url->compare($PAGE->url)) {
210 echo $OUTPUT->continue_button($url);
211 }
4b715423
SH
212 echo $OUTPUT->footer();
213 die;
214}
215
216// Post output will contain an entry containing HTML to display each post by the
217// time we are done.
218$postoutput = array();
99fa9889 219
4b715423
SH
220$discussions = array();
221foreach ($result->posts as $post) {
222 $discussions[] = $post->discussion;
223}
224$discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions));
225
226//todo Rather than retrieving the ratings for each post individually it would be nice to do them in groups
227//however this requires creating arrays of posts with each array containing all of the posts from a particular forum,
228//retrieving the ratings then reassembling them all back into a single array sorted by post.modified (descending)
229$rm = new rating_manager();
230$ratingoptions = new stdClass;
231$ratingoptions->component = 'mod_forum';
232$ratingoptions->ratingarea = 'post';
233foreach ($result->posts as $post) {
234 if (!isset($result->forums[$post->forum]) || !isset($discussions[$post->discussion])) {
235 // Something very VERY dodgy has happened if we end up here
236 continue;
237 }
238 $forum = $result->forums[$post->forum];
239 $cm = $forum->cm;
240 $discussion = $discussions[$post->discussion];
241 $course = $result->courses[$discussion->course];
242
243 $forumurl = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
244 $discussionurl = new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion));
245
246 // load ratings
247 if ($forum->assessed != RATING_AGGREGATE_NONE) {
248 $ratingoptions->context = $cm->context;
249 $ratingoptions->items = array($post);
250 $ratingoptions->aggregate = $forum->assessed;//the aggregation method
251 $ratingoptions->scaleid = $forum->scale;
252 $ratingoptions->userid = $user->id;
253 $ratingoptions->assesstimestart = $forum->assesstimestart;
254 $ratingoptions->assesstimefinish = $forum->assesstimefinish;
255 if ($forum->type == 'single' or !$post->discussion) {
256 $ratingoptions->returnurl = $forumurl;
257 } else {
258 $ratingoptions->returnurl = $discussionurl;
bb8f3759 259 }
2011a95b 260
4b715423
SH
261 $updatedpost = $rm->get_ratings($ratingoptions);
262 //updating the array this way because we're iterating over a collection and updating them one by one
263 $result->posts[$updatedpost[0]->id] = $updatedpost[0];
264 }
265
bf0f06b1 266 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
4b715423
SH
267 $forumname = format_string($forum->name, true, array('context' => $cm->context));
268
269 $fullsubjects = array();
270 if (!$isspecificcourse && !$hasparentaccess) {
271 $fullsubjects[] = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $courseshortname);
272 $fullsubjects[] = html_writer::link($forumurl, $forumname);
273 } else {
274 $fullsubjects[] = html_writer::tag('span', $courseshortname);
275 $fullsubjects[] = html_writer::tag('span', $forumname);
276 }
277 if ($forum->type != 'single') {
278 $discussionname = format_string($discussion->name, true, array('context' => $cm->context));
279 if (!$isspecificcourse && !$hasparentaccess) {
280 $fullsubjects[] .= html_writer::link($discussionurl, $discussionname);
281 } else {
282 $fullsubjects[] .= html_writer::tag('span', $discussionname);
bb8f3759 283 }
4b715423
SH
284 if ($post->parent != 0) {
285 $postname = format_string($post->subject, true, array('context' => $cm->context));
286 if (!$isspecificcourse && !$hasparentaccess) {
287 $fullsubjects[] .= html_writer::link(new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion, 'parent' => $post->id)), $postname);
288 } else {
289 $fullsubjects[] .= html_writer::tag('span', $postname);
f9a0ea69 290 }
bb8f3759 291 }
bb8f3759 292 }
4b715423
SH
293 $post->subject = join(' -> ', $fullsubjects);
294 // This is really important, if the strings are formatted again all the links
295 // we've added will be lost.
296 $post->subjectnoformat = true;
297 $discussionurl->set_anchor('p'.$post->id);
298 $fulllink = html_writer::link($discussionurl, get_string("postincontext", "forum"));
299
300 $postoutput[] = forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, $fulllink, '', null, true, null, true);
301}
2011a95b 302
4b715423
SH
303$userfullname = fullname($user);
304
305if ($discussionsonly) {
306 $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname);
bb8f3759 307} else {
4b715423
SH
308 $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname);
309}
310if ($isspecificcourse) {
311 $a = new stdClass;
312 $a->fullname = $userfullname;
313 $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
314 $pageheading = $a->coursename;
315 if ($discussionsonly) {
316 $pagetitle = get_string('discussionsstartedbyuserincourse', 'mod_forum', $a);
f9a0ea69 317 } else {
4b715423 318 $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a);
f9a0ea69 319 }
4b715423
SH
320} else {
321 $pagetitle = $inpageheading;
322 $pageheading = $userfullname;
bb8f3759 323}
f9a0ea69 324
4b715423
SH
325$PAGE->set_title($pagetitle);
326$PAGE->set_heading($pagetitle);
327$PAGE->navigation->extend_for_user($user);
328$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
329
330echo $OUTPUT->header();
331echo $OUTPUT->heading($inpageheading);
332echo html_writer::start_tag('div', array('class' => 'user-content'));
333
334if (!empty($postoutput)) {
335 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
336 foreach ($postoutput as $post) {
337 echo $post;
338 echo html_writer::empty_tag('br');
339 }
340 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
341} else if ($discussionsonly) {
342 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum', $userfullname));
343} else {
344 echo $OUTPUT->heading(get_string('noposts', 'forum'));
345}
346
347echo html_writer::end_tag('div');
348echo $OUTPUT->footer();