weekly release 4.0dev
[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
1fcf0ca8 26require(__DIR__.'/../../config.php');
4b715423
SH
27require_once($CFG->dirroot.'/mod/forum/lib.php');
28require_once($CFG->dirroot.'/rating/lib.php');
73d78820 29require_once($CFG->dirroot.'/user/lib.php');
4b715423
SH
30
31$courseid = optional_param('course', null, PARAM_INT); // Limit the posts to just this course
32$userid = optional_param('id', $USER->id, PARAM_INT); // User id whose posts we want to view
33$mode = optional_param('mode', 'posts', PARAM_ALPHA); // The mode to use. Either posts or discussions
34$page = optional_param('page', 0, PARAM_INT); // The page number to display
35$perpage = optional_param('perpage', 5, PARAM_INT); // The number of posts to display per page
36
37if (empty($userid)) {
38 if (!isloggedin()) {
39 require_login();
40 }
41 $userid = $USER->id;
cd4e6b17 42}
4b715423
SH
43
44$discussionsonly = ($mode !== 'posts');
45$isspecificcourse = !is_null($courseid);
46$iscurrentuser = ($USER->id == $userid);
47
48$url = new moodle_url('/mod/forum/user.php', array('id' => $userid));
a161d96f
SH
49if ($isspecificcourse) {
50 $url->param('course', $courseid);
51}
52if ($discussionsonly) {
53 $url->param('mode', 'discussions');
54}
4b715423 55
cd4e6b17 56$PAGE->set_url($url);
241ce9c4 57$PAGE->set_pagelayout('standard');
cd4e6b17 58
a161d96f
SH
59if ($page != 0) {
60 $url->param('page', $page);
61}
62if ($perpage != 5) {
63 $url->param('perpage', $perpage);
64}
bb8f3759 65
4b715423 66$user = $DB->get_record("user", array("id" => $userid), '*', MUST_EXIST);
bf0f06b1 67$usercontext = context_user::instance($user->id, MUST_EXIST);
4b715423
SH
68// Check if the requested user is the guest user
69if (isguestuser($user)) {
70 // The guest user cannot post, so it is not possible to view any posts.
71 // May as well just bail aggressively here.
72 print_error('invaliduserid');
bb8f3759 73}
4b715423 74// Make sure the user has not been deleted
bb8f3759 75if ($user->deleted) {
4b715423 76 $PAGE->set_title(get_string('userdeleted'));
0601e0ee 77 $PAGE->set_context(context_system::instance());
15ca5e5e 78 echo $OUTPUT->header();
4b715423 79 echo $OUTPUT->heading($PAGE->title);
396fb912 80 echo $OUTPUT->footer();
bb8f3759 81 die;
82}
f9a0ea69 83
4b715423
SH
84$isloggedin = isloggedin();
85$isguestuser = $isloggedin && isguestuser();
86$isparent = !$iscurrentuser && $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id));
87$hasparentaccess = $isparent && has_all_capabilities(array('moodle/user:viewdetails', 'moodle/user:readuserposts'), $usercontext);
88
89// Check whether a specific course has been requested
90if ($isspecificcourse) {
91 // Get the requested course and its context
74df2951 92 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
bf0f06b1 93 $coursecontext = context_course::instance($courseid, MUST_EXIST);
4b715423
SH
94 // We have a specific course to search, which we will also assume we are within.
95 if ($hasparentaccess) {
96 // A `parent` role won't likely have access to the course so we won't attempt
97 // to enter it. We will however still make them jump through the normal
98 // login hoops
99 require_login();
100 $PAGE->set_context($coursecontext);
101 $PAGE->set_course($course);
102 } else {
103 // Enter the course we are searching
104 require_login($course);
67875aa1 105 }
4b715423
SH
106 // Get the course ready for access checks
107 $courses = array($courseid => $course);
bb8f3759 108} else {
4b715423
SH
109 // We are going to search for all of the users posts in all courses!
110 // a general require login here as we arn't actually within any course.
111 require_login();
880c5073 112 $PAGE->set_context(context_user::instance($user->id));
99fa9889 113
4b715423
SH
114 // Now we need to get all of the courses to search.
115 // All courses where the user has posted within a forum will be returned.
116 $courses = forum_get_courses_user_posted_in($user, $discussionsonly);
117}
65bcf17b 118
22881392
DP
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
4d068a67 138 $canviewuser = user_can_view_profile($user, null, $usercontext);
2011a95b 139
4b715423
SH
140 // Prepare the page title
141 $pagetitle = get_string('noposts', 'mod_forum');
99fa9889 142
4b715423
SH
143 // Get the page heading
144 if ($isspecificcourse) {
e026cc92 145 $pageheading = format_string($course->fullname, true, array('context' => $coursecontext));
4b715423
SH
146 } else {
147 $pageheading = get_string('pluginname', 'mod_forum');
148 }
65bcf17b 149
4b715423
SH
150 // Next we need to set up the loading of the navigation and choose a message
151 // to display to the current user.
152 if ($iscurrentuser) {
153 // No need to extend the navigation it happens automatically for the
154 // current user.
155 if ($discussionsonly) {
156 $notification = get_string('nodiscussionsstartedbyyou', 'forum');
bb8f3759 157 } else {
4b715423 158 $notification = get_string('nopostsmadebyyou', 'forum');
bb8f3759 159 }
f495187d
AG
160 // These are the user's forum interactions.
161 // Shut down the navigation 'Users' node.
162 $usernode = $PAGE->navigation->find('users', null);
163 $usernode->make_inactive();
164 // Edit navbar.
165 if (isset($courseid) && $courseid != SITEID) {
166 // Create as much of the navbar automatically.
167 $newusernode = $PAGE->navigation->find('user' . $user->id, null);
168 $newusernode->make_active();
169 // Check to see if this is a discussion or a post.
170 if ($mode == 'posts') {
171 $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
172 array('id' => $user->id, 'course' => $courseid)));
173 } else {
174 $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
45367bdf 175 array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
f495187d
AG
176 }
177 }
4b715423
SH
178 } else if ($canviewuser) {
179 $PAGE->navigation->extend_for_user($user);
180 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
f495187d
AG
181
182 // Edit navbar.
183 if (isset($courseid) && $courseid != SITEID) {
184 // Create as much of the navbar automatically.
185 $usernode = $PAGE->navigation->find('user' . $user->id, null);
186 $usernode->make_active();
187 // Check to see if this is a discussion or a post.
188 if ($mode == 'posts') {
189 $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
190 array('id' => $user->id, 'course' => $courseid)));
191 } else {
192 $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
45367bdf 193 array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
f495187d
AG
194 }
195 }
196
4b715423
SH
197 $fullname = fullname($user);
198 if ($discussionsonly) {
199 $notification = get_string('nodiscussionsstartedby', 'forum', $fullname);
bb8f3759 200 } else {
4b715423 201 $notification = get_string('nopostsmadebyuser', 'forum', $fullname);
bb8f3759 202 }
4b715423
SH
203 } else {
204 // Don't extend the navigation it would be giving out information that
205 // the current uesr doesn't have access to.
206 $notification = get_string('cannotviewusersposts', 'forum');
207 if ($isspecificcourse) {
208 $url = new moodle_url('/course/view.php', array('id' => $courseid));
209 } else {
210 $url = new moodle_url('/');
211 }
212 navigation_node::override_active_url($url);
213 }
65bcf17b 214
4b715423
SH
215 // Display a page letting the user know that there's nothing to display;
216 $PAGE->set_title($pagetitle);
880c5073
AG
217 if ($isspecificcourse) {
218 $PAGE->set_heading($pageheading);
73d78820 219 } else if ($canviewuser) {
880c5073 220 $PAGE->set_heading(fullname($user));
8d8d00ef
AA
221 } else {
222 $PAGE->set_heading($SITE->fullname);
880c5073 223 }
4b715423 224 echo $OUTPUT->header();
283ea788
AG
225 if (!$isspecificcourse) {
226 echo $OUTPUT->heading($pagetitle);
227 } else {
228 $userheading = array(
229 'heading' => fullname($user),
230 'user' => $user,
231 'usercontext' => $usercontext
232 );
233 echo $OUTPUT->context_header($userheading, 2);
234 }
4b715423 235 echo $OUTPUT->notification($notification);
a161d96f
SH
236 if (!$url->compare($PAGE->url)) {
237 echo $OUTPUT->continue_button($url);
238 }
4b715423
SH
239 echo $OUTPUT->footer();
240 die;
241}
242
4b715423
SH
243$discussions = array();
244foreach ($result->posts as $post) {
245 $discussions[] = $post->discussion;
246}
247$discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions));
248
9ea78fd0
RW
249$entityfactory = mod_forum\local\container::get_entity_factory();
250$rendererfactory = mod_forum\local\container::get_renderer_factory();
251$postsrenderer = $rendererfactory->get_user_forum_posts_report_renderer(!$isspecificcourse && !$hasparentaccess);
252$postoutput = $postsrenderer->render(
253 $USER,
254 array_map(function($forum) use ($entityfactory, $result) {
255 $cm = $forum->cm;
256 $context = context_module::instance($cm->id);
257 $course = $result->courses[$forum->course];
258 return $entityfactory->get_forum_from_stdclass($forum, $context, $cm, $course);
259 }, $result->forums),
260 array_map(function($discussion) use ($entityfactory) {
261 return $entityfactory->get_discussion_from_stdclass($discussion);
262 }, $discussions),
263 array_map(function($post) use ($entityfactory) {
264 return $entityfactory->get_post_from_stdclass($post);
265 }, $result->posts)
266);
2011a95b 267
4b715423
SH
268$userfullname = fullname($user);
269
270if ($discussionsonly) {
271 $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname);
bb8f3759 272} else {
4b715423
SH
273 $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname);
274}
275if ($isspecificcourse) {
276 $a = new stdClass;
277 $a->fullname = $userfullname;
e026cc92 278 $a->coursename = format_string($course->fullname, true, array('context' => $coursecontext));
4b715423
SH
279 $pageheading = $a->coursename;
280 if ($discussionsonly) {
281 $pagetitle = get_string('discussionsstartedbyuserincourse', 'mod_forum', $a);
f9a0ea69 282 } else {
4b715423 283 $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a);
f9a0ea69 284 }
4b715423
SH
285} else {
286 $pagetitle = $inpageheading;
287 $pageheading = $userfullname;
bb8f3759 288}
f9a0ea69 289
4b715423 290$PAGE->set_title($pagetitle);
880c5073
AG
291$PAGE->set_heading($pageheading);
292
4b715423
SH
293$PAGE->navigation->extend_for_user($user);
294$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
295
f495187d
AG
296// Edit navbar.
297if (isset($courseid) && $courseid != SITEID) {
298 $usernode = $PAGE->navigation->find('user' . $user->id , null);
299 $usernode->make_active();
300 // Check to see if this is a discussion or a post.
301 if ($mode == 'posts') {
302 $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
303 array('id' => $user->id, 'course' => $courseid)));
304 } else {
305 $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
45367bdf 306 array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
f495187d
AG
307 }
308}
309
4b715423 310echo $OUTPUT->header();
4b715423
SH
311echo html_writer::start_tag('div', array('class' => 'user-content'));
312
2140cca1
FM
313if ($isspecificcourse) {
314 $userheading = array(
315 'heading' => fullname($user),
316 'user' => $user,
317 'usercontext' => $usercontext
318 );
319 echo $OUTPUT->context_header($userheading, 2);
320} else {
321 echo $OUTPUT->heading($inpageheading);
322}
323
4b715423
SH
324if (!empty($postoutput)) {
325 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
9ea78fd0 326 echo $postoutput;
4b715423
SH
327 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
328} else if ($discussionsonly) {
329 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum', $userfullname));
330} else {
331 echo $OUTPUT->heading(get_string('noposts', 'forum'));
332}
333
334echo html_writer::end_tag('div');
335echo $OUTPUT->footer();