MDL-73233 frontpage: Display link to My courses
[moodle.git] / mod / forum / user.php
blob8fa33aa1294dca38260a393af6cc05c494c08804
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(__DIR__.'/../../config.php');
27 require_once($CFG->dirroot.'/mod/forum/lib.php');
28 require_once($CFG->dirroot.'/rating/lib.php');
29 require_once($CFG->dirroot.'/user/lib.php');
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
37 if (empty($userid)) {
38 if (!isloggedin()) {
39 require_login();
41 $userid = $USER->id;
44 $discussionsonly = ($mode !== 'posts');
45 $isspecificcourse = !is_null($courseid);
46 $iscurrentuser = ($USER->id == $userid);
48 $url = new moodle_url('/mod/forum/user.php', array('id' => $userid));
49 if ($isspecificcourse) {
50 $url->param('course', $courseid);
52 if ($discussionsonly) {
53 $url->param('mode', 'discussions');
56 $PAGE->set_url($url);
57 $PAGE->set_pagelayout('standard');
59 if ($page != 0) {
60 $url->param('page', $page);
62 if ($perpage != 5) {
63 $url->param('perpage', $perpage);
66 $user = $DB->get_record("user", array("id" => $userid), '*', MUST_EXIST);
67 $usercontext = context_user::instance($user->id, MUST_EXIST);
68 // Check if the requested user is the guest user
69 if (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');
74 // Make sure the user has not been deleted
75 if ($user->deleted) {
76 $PAGE->set_title(get_string('userdeleted'));
77 $PAGE->set_context(context_system::instance());
78 echo $OUTPUT->header();
79 echo $OUTPUT->heading($PAGE->title);
80 echo $OUTPUT->footer();
81 die;
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);
89 // Check whether a specific course has been requested
90 if ($isspecificcourse) {
91 // Get the requested course and its context
92 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
93 $coursecontext = context_course::instance($courseid, MUST_EXIST);
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);
106 // Get the course ready for access checks
107 $courses = array($courseid => $course);
108 } else {
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();
112 $PAGE->set_context(context_user::instance($user->id));
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);
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 $canviewuser = user_can_view_profile($user, null, $usercontext);
140 // Prepare the page title
141 $pagetitle = get_string('noposts', 'mod_forum');
143 // Get the page heading
144 if ($isspecificcourse) {
145 $pageheading = format_string($course->fullname, true, array('context' => $coursecontext));
146 } else {
147 $pageheading = get_string('pluginname', 'mod_forum');
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');
157 } else {
158 $notification = get_string('nopostsmadebyyou', 'forum');
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 if ($newusernode = $PAGE->navigation->find('user' . $user->id, null)) {
168 $newusernode->make_active();
170 // Check to see if this is a discussion or a post.
171 if ($mode == 'posts') {
172 $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
173 array('id' => $user->id, 'course' => $courseid)));
174 } else {
175 $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
176 array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
179 } else if ($canviewuser) {
180 $PAGE->navigation->extend_for_user($user);
181 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
183 // Edit navbar.
184 if (isset($courseid) && $courseid != SITEID) {
185 // Create as much of the navbar automatically.
186 if ($usernode = $PAGE->navigation->find('user' . $user->id, null)) {
187 $usernode->make_active();
189 // Check to see if this is a discussion or a post.
190 if ($mode == 'posts') {
191 $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
192 array('id' => $user->id, 'course' => $courseid)));
193 } else {
194 $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
195 array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
199 $fullname = fullname($user);
200 if ($discussionsonly) {
201 $notification = get_string('nodiscussionsstartedby', 'forum', $fullname);
202 } else {
203 $notification = get_string('nopostsmadebyuser', 'forum', $fullname);
205 } else {
206 // Don't extend the navigation it would be giving out information that
207 // the current uesr doesn't have access to.
208 $notification = get_string('cannotviewusersposts', 'forum');
209 if ($isspecificcourse) {
210 $url = new moodle_url('/course/view.php', array('id' => $courseid));
211 } else {
212 $url = new moodle_url('/');
214 navigation_node::override_active_url($url);
217 // Display a page letting the user know that there's nothing to display;
218 $PAGE->set_title($pagetitle);
219 if ($isspecificcourse) {
220 $PAGE->set_secondary_active_tab('participants');
221 $PAGE->set_heading($pageheading);
222 } else if ($canviewuser) {
223 $PAGE->set_heading(fullname($user));
224 } else {
225 $PAGE->set_heading($SITE->fullname);
227 echo $OUTPUT->header();
228 if (isset($courseid) && $courseid != SITEID) {
229 $backurl = new moodle_url('/user/view.php', ['id' => $userid, 'course' => $courseid]);
230 echo $OUTPUT->single_button($backurl, get_string('back'), 'get', ['class' => 'mb-3']);
232 if (!$isspecificcourse) {
233 echo $OUTPUT->heading($pagetitle);
234 } else {
235 $userheading = array(
236 'heading' => fullname($user),
237 'user' => $user,
238 'usercontext' => $usercontext
240 echo $OUTPUT->context_header($userheading, 2);
242 echo $OUTPUT->notification($notification);
243 if (!$url->compare($PAGE->url)) {
244 echo $OUTPUT->continue_button($url);
246 echo $OUTPUT->footer();
247 die;
250 $discussions = array();
251 foreach ($result->posts as $post) {
252 $discussions[] = $post->discussion;
254 $discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions));
256 $entityfactory = mod_forum\local\container::get_entity_factory();
257 $rendererfactory = mod_forum\local\container::get_renderer_factory();
258 $postsrenderer = $rendererfactory->get_user_forum_posts_report_renderer(!$isspecificcourse && !$hasparentaccess);
259 $postoutput = $postsrenderer->render(
260 $USER,
261 array_map(function($forum) use ($entityfactory, $result) {
262 $cm = $forum->cm;
263 $context = context_module::instance($cm->id);
264 $course = $result->courses[$forum->course];
265 return $entityfactory->get_forum_from_stdclass($forum, $context, $cm, $course);
266 }, $result->forums),
267 array_map(function($discussion) use ($entityfactory) {
268 return $entityfactory->get_discussion_from_stdclass($discussion);
269 }, $discussions),
270 array_map(function($post) use ($entityfactory) {
271 return $entityfactory->get_post_from_stdclass($post);
272 }, $result->posts)
275 $userfullname = fullname($user);
277 if ($discussionsonly) {
278 $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname);
279 } else {
280 $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname);
282 if ($isspecificcourse) {
283 $a = new stdClass;
284 $a->fullname = $userfullname;
285 $a->coursename = format_string($course->fullname, true, array('context' => $coursecontext));
286 $pageheading = $a->coursename;
287 if ($discussionsonly) {
288 $pagetitle = get_string('discussionsstartedbyuserincourse', 'mod_forum', $a);
289 } else {
290 $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a);
292 } else {
293 $pagetitle = $inpageheading;
294 $pageheading = $userfullname;
297 $PAGE->set_title($pagetitle);
298 $PAGE->set_heading($pageheading);
300 $PAGE->navigation->extend_for_user($user);
301 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
303 // Edit navbar.
304 if (isset($courseid) && $courseid != SITEID) {
305 if ($usernode = $PAGE->navigation->find('user' . $user->id , null)) {
306 $usernode->make_active();
309 // Check to see if this is a discussion or a post.
310 if ($mode == 'posts') {
311 $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
312 array('id' => $user->id, 'course' => $courseid)));
313 } else {
314 $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
315 array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
317 $PAGE->set_secondary_active_tab('participants');
320 echo $OUTPUT->header();
322 if (isset($courseid) && $courseid != SITEID) {
323 $backurl = new moodle_url('/user/view.php', ['id' => $userid, 'course' => $courseid]);
324 echo $OUTPUT->single_button($backurl, get_string('back'), 'get', ['class' => 'mb-3']);
326 echo html_writer::start_tag('div', array('class' => 'user-content'));
328 if ($isspecificcourse) {
329 $userheading = array(
330 'heading' => fullname($user),
331 'user' => $user,
332 'usercontext' => $usercontext
334 echo $OUTPUT->context_header($userheading, 2);
335 $coursename = format_string($course->fullname, true, array('context' => $coursecontext));
336 $heading = $mode === 'posts' ? get_string('postsmadeincourse', 'mod_forum', $coursename) :
337 get_string('discussionsstartedincourse', 'mod_forum', $coursename);
338 echo $OUTPUT->heading($heading, 2, 'main mt-4 mb-4');
339 } else {
340 echo $OUTPUT->heading($inpageheading);
343 if (!empty($postoutput)) {
344 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
345 echo $postoutput;
346 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
347 } else if ($discussionsonly) {
348 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum', $userfullname));
349 } else {
350 echo $OUTPUT->heading(get_string('noposts', 'forum'));
353 echo html_writer::end_tag('div');
354 echo $OUTPUT->footer();