MDL-46921 lib: Update get_all_user_name_fields() plus unit tests.
[moodle.git] / mod / forum / user.php
blob87aba79caaf6620ca0709e718ceaa844571bc8a4
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_once(dirname(dirname(dirname(__FILE__))).'/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_system::instance());
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);
119 $params = array(
120 'context' => $PAGE->context,
121 'relateduserid' => $user->id,
122 'other' => array('reportmode' => $mode),
124 $event = \mod_forum\event\user_report_viewed::create($params);
125 $event->trigger();
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);
130 // Check whether there are not posts to display.
131 if (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
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)));
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
154 if (has_capability('moodle/user:viewdetails', context_course::instance($sharedcourse->id))) {
155 $canviewuser = true;
156 break;
159 unset($sharedcourses);
162 // Prepare the page title
163 $pagetitle = get_string('noposts', 'mod_forum');
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');
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');
179 } else {
180 $notification = get_string('nopostsmadebyyou', 'forum');
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);
188 } else {
189 $notification = get_string('nopostsmadebyuser', 'forum', $fullname);
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('/');
200 navigation_node::override_active_url($url);
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);
209 if (!$url->compare($PAGE->url)) {
210 echo $OUTPUT->continue_button($url);
212 echo $OUTPUT->footer();
213 die;
216 // Post output will contain an entry containing HTML to display each post by the
217 // time we are done.
218 $postoutput = array();
220 $discussions = array();
221 foreach ($result->posts as $post) {
222 $discussions[] = $post->discussion;
224 $discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions));
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';
233 foreach ($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;
238 $forum = $result->forums[$post->forum];
239 $cm = $forum->cm;
240 $discussion = $discussions[$post->discussion];
241 $course = $result->courses[$discussion->course];
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));
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;
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];
266 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
267 $forumname = format_string($forum->name, true, array('context' => $cm->context));
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);
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);
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);
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"));
300 $postoutput[] = forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, $fulllink, '', null, true, null, true);
303 $userfullname = fullname($user);
305 if ($discussionsonly) {
306 $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname);
307 } else {
308 $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname);
310 if ($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);
317 } else {
318 $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a);
320 } else {
321 $pagetitle = $inpageheading;
322 $pageheading = $userfullname;
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.
330 echo $OUTPUT->header();
331 echo $OUTPUT->heading($inpageheading);
332 echo html_writer::start_tag('div', array('class' => 'user-content'));
334 if (!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');
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'));
347 echo html_writer::end_tag('div');
348 echo $OUTPUT->footer();