MDL-40062 mod_forum: remove invalid add_to_log() calls
[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 *
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
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 65add_to_log(($isspecificcourse)?$courseid:SITEID, "forum", "user report", 'user.php?'.$url->get_query_string(), $userid);
bb8f3759 66
4b715423 67$user = $DB->get_record("user", array("id" => $userid), '*', MUST_EXIST);
bf0f06b1 68$usercontext = context_user::instance($user->id, MUST_EXIST);
4b715423
SH
69// Check if the requested user is the guest user
70if (isguestuser($user)) {
71 // The guest user cannot post, so it is not possible to view any posts.
72 // May as well just bail aggressively here.
73 print_error('invaliduserid');
bb8f3759 74}
4b715423 75// Make sure the user has not been deleted
bb8f3759 76if ($user->deleted) {
4b715423 77 $PAGE->set_title(get_string('userdeleted'));
0601e0ee 78 $PAGE->set_context(context_system::instance());
15ca5e5e 79 echo $OUTPUT->header();
4b715423 80 echo $OUTPUT->heading($PAGE->title);
396fb912 81 echo $OUTPUT->footer();
bb8f3759 82 die;
83}
f9a0ea69 84
4b715423
SH
85$isloggedin = isloggedin();
86$isguestuser = $isloggedin && isguestuser();
87$isparent = !$iscurrentuser && $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id));
88$hasparentaccess = $isparent && has_all_capabilities(array('moodle/user:viewdetails', 'moodle/user:readuserposts'), $usercontext);
89
90// Check whether a specific course has been requested
91if ($isspecificcourse) {
92 // Get the requested course and its context
74df2951 93 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
bf0f06b1 94 $coursecontext = context_course::instance($courseid, MUST_EXIST);
4b715423
SH
95 // We have a specific course to search, which we will also assume we are within.
96 if ($hasparentaccess) {
97 // A `parent` role won't likely have access to the course so we won't attempt
98 // to enter it. We will however still make them jump through the normal
99 // login hoops
100 require_login();
101 $PAGE->set_context($coursecontext);
102 $PAGE->set_course($course);
103 } else {
104 // Enter the course we are searching
105 require_login($course);
67875aa1 106 }
4b715423
SH
107 // Get the course ready for access checks
108 $courses = array($courseid => $course);
bb8f3759 109} else {
4b715423
SH
110 // We are going to search for all of the users posts in all courses!
111 // a general require login here as we arn't actually within any course.
112 require_login();
0601e0ee 113 $PAGE->set_context(context_system::instance());
99fa9889 114
4b715423
SH
115 // Now we need to get all of the courses to search.
116 // All courses where the user has posted within a forum will be returned.
117 $courses = forum_get_courses_user_posted_in($user, $discussionsonly);
118}
65bcf17b 119
4b715423
SH
120// Get the posts by the requested user that the current user can access.
121$result = forum_get_posts_by_user($user, $courses, $isspecificcourse, $discussionsonly, ($page * $perpage), $perpage);
122
123// Check whether there are not posts to display.
124if (empty($result->posts)) {
125 // Ok no posts to display means that either the user has not posted or there
126 // are no posts made by the requested user that the current user is able to
127 // see.
128 // In either case we need to decide whether we can show personal information
129 // about the requested user to the current user so we will execute some checks
130
131 // First check the obvious, its the current user, a specific course has been
132 // provided (require_login has been called), or they have a course contact role.
133 // True to any of those and the current user can see the details of the
134 // requested user.
135 $canviewuser = ($iscurrentuser || $isspecificcourse || empty($CFG->forceloginforprofiles) || has_coursecontact_role($userid));
136 // Next we'll check the caps, if the current user has the view details and a
137 // specific course has been requested, or if they have the view all details
138 $canviewuser = ($canviewuser || ($isspecificcourse && has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewalldetails', $usercontext)));
139
140 // If none of the above was true the next step is to check a shared relation
141 // through some course
142 if (!$canviewuser) {
143 // Get all of the courses that the users have in common
144 $sharedcourses = enrol_get_shared_courses($USER->id, $user->id, true);
145 foreach ($sharedcourses as $sharedcourse) {
146 // Check the view cap within the course context
bf0f06b1 147 if (has_capability('moodle/user:viewdetails', context_course::instance($sharedcourse->id))) {
4b715423
SH
148 $canviewuser = true;
149 break;
150 }
151 }
152 unset($sharedcourses);
153 }
2011a95b 154
4b715423
SH
155 // Prepare the page title
156 $pagetitle = get_string('noposts', 'mod_forum');
99fa9889 157
4b715423
SH
158 // Get the page heading
159 if ($isspecificcourse) {
160 $pageheading = format_string($course->shortname, true, array('context' => $coursecontext));
161 } else {
162 $pageheading = get_string('pluginname', 'mod_forum');
163 }
65bcf17b 164
4b715423
SH
165 // Next we need to set up the loading of the navigation and choose a message
166 // to display to the current user.
167 if ($iscurrentuser) {
168 // No need to extend the navigation it happens automatically for the
169 // current user.
170 if ($discussionsonly) {
171 $notification = get_string('nodiscussionsstartedbyyou', 'forum');
bb8f3759 172 } else {
4b715423 173 $notification = get_string('nopostsmadebyyou', 'forum');
bb8f3759 174 }
4b715423
SH
175 } else if ($canviewuser) {
176 $PAGE->navigation->extend_for_user($user);
177 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
178 $fullname = fullname($user);
179 if ($discussionsonly) {
180 $notification = get_string('nodiscussionsstartedby', 'forum', $fullname);
bb8f3759 181 } else {
4b715423 182 $notification = get_string('nopostsmadebyuser', 'forum', $fullname);
bb8f3759 183 }
4b715423
SH
184 } else {
185 // Don't extend the navigation it would be giving out information that
186 // the current uesr doesn't have access to.
187 $notification = get_string('cannotviewusersposts', 'forum');
188 if ($isspecificcourse) {
189 $url = new moodle_url('/course/view.php', array('id' => $courseid));
190 } else {
191 $url = new moodle_url('/');
192 }
193 navigation_node::override_active_url($url);
194 }
65bcf17b 195
4b715423
SH
196 // Display a page letting the user know that there's nothing to display;
197 $PAGE->set_title($pagetitle);
198 $PAGE->set_heading($pageheading);
199 echo $OUTPUT->header();
200 echo $OUTPUT->heading($pagetitle);
201 echo $OUTPUT->notification($notification);
a161d96f
SH
202 if (!$url->compare($PAGE->url)) {
203 echo $OUTPUT->continue_button($url);
204 }
4b715423
SH
205 echo $OUTPUT->footer();
206 die;
207}
208
209// Post output will contain an entry containing HTML to display each post by the
210// time we are done.
211$postoutput = array();
99fa9889 212
4b715423
SH
213$discussions = array();
214foreach ($result->posts as $post) {
215 $discussions[] = $post->discussion;
216}
217$discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions));
218
219//todo Rather than retrieving the ratings for each post individually it would be nice to do them in groups
220//however this requires creating arrays of posts with each array containing all of the posts from a particular forum,
221//retrieving the ratings then reassembling them all back into a single array sorted by post.modified (descending)
222$rm = new rating_manager();
223$ratingoptions = new stdClass;
224$ratingoptions->component = 'mod_forum';
225$ratingoptions->ratingarea = 'post';
226foreach ($result->posts as $post) {
227 if (!isset($result->forums[$post->forum]) || !isset($discussions[$post->discussion])) {
228 // Something very VERY dodgy has happened if we end up here
229 continue;
230 }
231 $forum = $result->forums[$post->forum];
232 $cm = $forum->cm;
233 $discussion = $discussions[$post->discussion];
234 $course = $result->courses[$discussion->course];
235
236 $forumurl = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
237 $discussionurl = new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion));
238
239 // load ratings
240 if ($forum->assessed != RATING_AGGREGATE_NONE) {
241 $ratingoptions->context = $cm->context;
242 $ratingoptions->items = array($post);
243 $ratingoptions->aggregate = $forum->assessed;//the aggregation method
244 $ratingoptions->scaleid = $forum->scale;
245 $ratingoptions->userid = $user->id;
246 $ratingoptions->assesstimestart = $forum->assesstimestart;
247 $ratingoptions->assesstimefinish = $forum->assesstimefinish;
248 if ($forum->type == 'single' or !$post->discussion) {
249 $ratingoptions->returnurl = $forumurl;
250 } else {
251 $ratingoptions->returnurl = $discussionurl;
bb8f3759 252 }
2011a95b 253
4b715423
SH
254 $updatedpost = $rm->get_ratings($ratingoptions);
255 //updating the array this way because we're iterating over a collection and updating them one by one
256 $result->posts[$updatedpost[0]->id] = $updatedpost[0];
257 }
258
bf0f06b1 259 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
4b715423
SH
260 $forumname = format_string($forum->name, true, array('context' => $cm->context));
261
262 $fullsubjects = array();
263 if (!$isspecificcourse && !$hasparentaccess) {
264 $fullsubjects[] = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $courseshortname);
265 $fullsubjects[] = html_writer::link($forumurl, $forumname);
266 } else {
267 $fullsubjects[] = html_writer::tag('span', $courseshortname);
268 $fullsubjects[] = html_writer::tag('span', $forumname);
269 }
270 if ($forum->type != 'single') {
271 $discussionname = format_string($discussion->name, true, array('context' => $cm->context));
272 if (!$isspecificcourse && !$hasparentaccess) {
273 $fullsubjects[] .= html_writer::link($discussionurl, $discussionname);
274 } else {
275 $fullsubjects[] .= html_writer::tag('span', $discussionname);
bb8f3759 276 }
4b715423
SH
277 if ($post->parent != 0) {
278 $postname = format_string($post->subject, true, array('context' => $cm->context));
279 if (!$isspecificcourse && !$hasparentaccess) {
280 $fullsubjects[] .= html_writer::link(new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion, 'parent' => $post->id)), $postname);
281 } else {
282 $fullsubjects[] .= html_writer::tag('span', $postname);
f9a0ea69 283 }
bb8f3759 284 }
bb8f3759 285 }
4b715423
SH
286 $post->subject = join(' -> ', $fullsubjects);
287 // This is really important, if the strings are formatted again all the links
288 // we've added will be lost.
289 $post->subjectnoformat = true;
290 $discussionurl->set_anchor('p'.$post->id);
291 $fulllink = html_writer::link($discussionurl, get_string("postincontext", "forum"));
292
293 $postoutput[] = forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, $fulllink, '', null, true, null, true);
294}
2011a95b 295
4b715423
SH
296$userfullname = fullname($user);
297
298if ($discussionsonly) {
299 $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname);
bb8f3759 300} else {
4b715423
SH
301 $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname);
302}
303if ($isspecificcourse) {
304 $a = new stdClass;
305 $a->fullname = $userfullname;
306 $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
307 $pageheading = $a->coursename;
308 if ($discussionsonly) {
309 $pagetitle = get_string('discussionsstartedbyuserincourse', 'mod_forum', $a);
f9a0ea69 310 } else {
4b715423 311 $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a);
f9a0ea69 312 }
4b715423
SH
313} else {
314 $pagetitle = $inpageheading;
315 $pageheading = $userfullname;
bb8f3759 316}
f9a0ea69 317
4b715423
SH
318$PAGE->set_title($pagetitle);
319$PAGE->set_heading($pagetitle);
320$PAGE->navigation->extend_for_user($user);
321$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
322
323echo $OUTPUT->header();
324echo $OUTPUT->heading($inpageheading);
325echo html_writer::start_tag('div', array('class' => 'user-content'));
326
327if (!empty($postoutput)) {
328 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
329 foreach ($postoutput as $post) {
330 echo $post;
331 echo html_writer::empty_tag('br');
332 }
333 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
334} else if ($discussionsonly) {
335 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum', $userfullname));
336} else {
337 echo $OUTPUT->heading(get_string('noposts', 'forum'));
338}
339
340echo html_writer::end_tag('div');
341echo $OUTPUT->footer();