3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Display user activity reports for a course
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
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) $url->param('course', $courseid);
49 if ($discussionsonly) $url->param('mode', 'discussions');
52 $PAGE->set_pagelayout('standard');
54 if ($page != 0) $url->param('page', $page);
55 if ($perpage != 5) $url->param('perpage', $perpage);
57 add_to_log(($isspecificcourse)?$courseid:SITEID, "forum", "user report", 'user.php?'.$url->get_query_string(), $userid);
59 $user = $DB->get_record("user", array("id" => $userid), '*', MUST_EXIST);
60 $usercontext = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
61 // Check if the requested user is the guest user
62 if (isguestuser($user)) {
63 // The guest user cannot post, so it is not possible to view any posts.
64 // May as well just bail aggressively here.
65 print_error('invaliduserid');
67 // Make sure the user has not been deleted
69 $PAGE->set_title(get_string('userdeleted'));
70 $PAGE->set_context(get_system_context());
71 echo $OUTPUT->header();
72 echo $OUTPUT->heading($PAGE->title);
73 echo $OUTPUT->footer();
77 $isloggedin = isloggedin();
78 $isguestuser = $isloggedin && isguestuser();
79 $isparent = !$iscurrentuser && $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id));
80 $hasparentaccess = $isparent && has_all_capabilities(array('moodle/user:viewdetails', 'moodle/user:readuserposts'), $usercontext);
82 // Check whether a specific course has been requested
83 if ($isspecificcourse) {
84 // Get the requested course and its context
85 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
86 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
87 // We have a specific course to search, which we will also assume we are within.
88 if ($hasparentaccess) {
89 // A `parent` role won't likely have access to the course so we won't attempt
90 // to enter it. We will however still make them jump through the normal
93 $PAGE->set_context($coursecontext);
94 $PAGE->set_course($course);
96 // Enter the course we are searching
97 require_login($course);
99 // Get the course ready for access checks
100 $courses = array($courseid => $course);
102 // We are going to search for all of the users posts in all courses!
103 // a general require login here as we arn't actually within any course.
105 $PAGE->set_context(get_system_context());
107 // Now we need to get all of the courses to search.
108 // All courses where the user has posted within a forum will be returned.
109 $courses = forum_get_courses_user_posted_in($user, $discussionsonly);
112 // Get the posts by the requested user that the current user can access.
113 $result = forum_get_posts_by_user($user, $courses, $isspecificcourse, $discussionsonly, ($page * $perpage), $perpage);
115 // Check whether there are not posts to display.
116 if (empty($result->posts)) {
117 // Ok no posts to display means that either the user has not posted or there
118 // are no posts made by the requested user that the current user is able to
120 // In either case we need to decide whether we can show personal information
121 // about the requested user to the current user so we will execute some checks
123 // First check the obvious, its the current user, a specific course has been
124 // provided (require_login has been called), or they have a course contact role.
125 // True to any of those and the current user can see the details of the
127 $canviewuser = ($iscurrentuser || $isspecificcourse || empty($CFG->forceloginforprofiles) || has_coursecontact_role($userid));
128 // Next we'll check the caps, if the current user has the view details and a
129 // specific course has been requested, or if they have the view all details
130 $canviewuser = ($canviewuser || ($isspecificcourse && has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewalldetails', $usercontext)));
132 // If none of the above was true the next step is to check a shared relation
133 // through some course
135 // Get all of the courses that the users have in common
136 $sharedcourses = enrol_get_shared_courses($USER->id, $user->id, true);
137 foreach ($sharedcourses as $sharedcourse) {
138 // Check the view cap within the course context
139 if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_COURSE, $sharedcourse->id))) {
144 unset($sharedcourses);
147 // Prepare the page title
148 $pagetitle = get_string('noposts', 'mod_forum');
150 // Get the page heading
151 if ($isspecificcourse) {
152 $pageheading = format_string($course->shortname, true, array('context' => $coursecontext));
154 $pageheading = get_string('pluginname', 'mod_forum');
157 // Next we need to set up the loading of the navigation and choose a message
158 // to display to the current user.
159 if ($iscurrentuser) {
160 // No need to extend the navigation it happens automatically for the
162 if ($discussionsonly) {
163 $notification = get_string('nodiscussionsstartedbyyou', 'forum');
165 $notification = get_string('nopostsmadebyyou', 'forum');
167 } else if ($canviewuser) {
168 $PAGE->navigation->extend_for_user($user);
169 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
170 $fullname = fullname($user);
171 if ($discussionsonly) {
172 $notification = get_string('nodiscussionsstartedby', 'forum', $fullname);
174 $notification = get_string('nopostsmadebyuser', 'forum', $fullname);
177 // Don't extend the navigation it would be giving out information that
178 // the current uesr doesn't have access to.
179 $notification = get_string('cannotviewusersposts', 'forum');
180 if ($isspecificcourse) {
181 $url = new moodle_url('/course/view.php', array('id' => $courseid));
183 $url = new moodle_url('/');
185 navigation_node::override_active_url($url);
188 // Display a page letting the user know that there's nothing to display;
189 $PAGE->set_title($pagetitle);
190 $PAGE->set_heading($pageheading);
191 echo $OUTPUT->header();
192 echo $OUTPUT->heading($pagetitle);
193 echo $OUTPUT->notification($notification);
194 echo $OUTPUT->continue_button($url);
195 echo $OUTPUT->footer();
199 // Post output will contain an entry containing HTML to display each post by the
201 $postoutput = array();
203 $discussions = array();
204 foreach ($result->posts as $post) {
205 $discussions[] = $post->discussion;
207 $discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions));
209 //todo Rather than retrieving the ratings for each post individually it would be nice to do them in groups
210 //however this requires creating arrays of posts with each array containing all of the posts from a particular forum,
211 //retrieving the ratings then reassembling them all back into a single array sorted by post.modified (descending)
212 $rm = new rating_manager();
213 $ratingoptions = new stdClass;
214 $ratingoptions->component = 'mod_forum';
215 $ratingoptions->ratingarea = 'post';
216 foreach ($result->posts as $post) {
217 if (!isset($result->forums[$post->forum]) || !isset($discussions[$post->discussion])) {
218 // Something very VERY dodgy has happened if we end up here
221 $forum = $result->forums[$post->forum];
223 $discussion = $discussions[$post->discussion];
224 $course = $result->courses[$discussion->course];
226 $forumurl = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
227 $discussionurl = new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion));
230 if ($forum->assessed != RATING_AGGREGATE_NONE) {
231 $ratingoptions->context = $cm->context;
232 $ratingoptions->items = array($post);
233 $ratingoptions->aggregate = $forum->assessed;//the aggregation method
234 $ratingoptions->scaleid = $forum->scale;
235 $ratingoptions->userid = $user->id;
236 $ratingoptions->assesstimestart = $forum->assesstimestart;
237 $ratingoptions->assesstimefinish = $forum->assesstimefinish;
238 if ($forum->type == 'single' or !$post->discussion) {
239 $ratingoptions->returnurl = $forumurl;
241 $ratingoptions->returnurl = $discussionurl;
244 $updatedpost = $rm->get_ratings($ratingoptions);
245 //updating the array this way because we're iterating over a collection and updating them one by one
246 $result->posts[$updatedpost[0]->id] = $updatedpost[0];
249 $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
250 $forumname = format_string($forum->name, true, array('context' => $cm->context));
252 $fullsubjects = array();
253 if (!$isspecificcourse && !$hasparentaccess) {
254 $fullsubjects[] = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $courseshortname);
255 $fullsubjects[] = html_writer::link($forumurl, $forumname);
257 $fullsubjects[] = html_writer::tag('span', $courseshortname);
258 $fullsubjects[] = html_writer::tag('span', $forumname);
260 if ($forum->type != 'single') {
261 $discussionname = format_string($discussion->name, true, array('context' => $cm->context));
262 if (!$isspecificcourse && !$hasparentaccess) {
263 $fullsubjects[] .= html_writer::link($discussionurl, $discussionname);
265 $fullsubjects[] .= html_writer::tag('span', $discussionname);
267 if ($post->parent != 0) {
268 $postname = format_string($post->subject, true, array('context' => $cm->context));
269 if (!$isspecificcourse && !$hasparentaccess) {
270 $fullsubjects[] .= html_writer::link(new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion, 'parent' => $post->id)), $postname);
272 $fullsubjects[] .= html_writer::tag('span', $postname);
276 $post->subject = join(' -> ', $fullsubjects);
277 // This is really important, if the strings are formatted again all the links
278 // we've added will be lost.
279 $post->subjectnoformat = true;
280 $discussionurl->set_anchor('p'.$post->id);
281 $fulllink = html_writer::link($discussionurl, get_string("postincontext", "forum"));
283 $postoutput[] = forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, $fulllink, '', null, true, null, true);
286 $userfullname = fullname($user);
288 if ($discussionsonly) {
289 $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname);
291 $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname);
293 if ($isspecificcourse) {
295 $a->fullname = $userfullname;
296 $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
297 $pageheading = $a->coursename;
298 if ($discussionsonly) {
299 $pagetitle = get_string('discussionsstartedbyuserincourse', 'mod_forum', $a);
301 $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a);
304 $pagetitle = $inpageheading;
305 $pageheading = $userfullname;
308 $PAGE->set_title($pagetitle);
309 $PAGE->set_heading($pagetitle);
310 $PAGE->navigation->extend_for_user($user);
311 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
313 echo $OUTPUT->header();
314 echo $OUTPUT->heading($inpageheading);
315 echo html_writer::start_tag('div', array('class' => 'user-content'));
317 if (!empty($postoutput)) {
318 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
319 foreach ($postoutput as $post) {
321 echo html_writer::empty_tag('br');
323 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
324 } else if ($discussionsonly) {
325 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum', $userfullname));
327 echo $OUTPUT->heading(get_string('noposts', 'forum'));
330 echo html_writer::end_tag('div');
331 echo $OUTPUT->footer();