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) {
49 $url->param('course', $courseid);
51 if ($discussionsonly) {
52 $url->param('mode', 'discussions');
56 $PAGE->set_pagelayout('standard');
59 $url->param('page', $page);
62 $url->param('perpage', $perpage);
65 add_to_log(($isspecificcourse)?
$courseid:SITEID
, "forum", "user report", 'user.php?'.$url->get_query_string(), $userid);
67 $user = $DB->get_record("user", array("id" => $userid), '*', MUST_EXIST
);
68 $usercontext = context_user
::instance($user->id
, MUST_EXIST
);
69 // Check if the requested user is the guest user
70 if (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');
75 // Make sure the user has not been deleted
77 $PAGE->set_title(get_string('userdeleted'));
78 $PAGE->set_context(get_system_context());
79 echo $OUTPUT->header();
80 echo $OUTPUT->heading($PAGE->title
);
81 echo $OUTPUT->footer();
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);
90 // Check whether a specific course has been requested
91 if ($isspecificcourse) {
92 // Get the requested course and its context
93 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST
);
94 $coursecontext = context_course
::instance($courseid, MUST_EXIST
);
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
101 $PAGE->set_context($coursecontext);
102 $PAGE->set_course($course);
104 // Enter the course we are searching
105 require_login($course);
107 // Get the course ready for access checks
108 $courses = array($courseid => $course);
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.
113 $PAGE->set_context(get_system_context());
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);
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);
123 // Check whether there are not posts to display.
124 if (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
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
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
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)));
140 // If none of the above was true the next step is to check a shared relation
141 // through some course
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
147 if (has_capability('moodle/user:viewdetails', context_course
::instance($sharedcourse->id
))) {
152 unset($sharedcourses);
155 // Prepare the page title
156 $pagetitle = get_string('noposts', 'mod_forum');
158 // Get the page heading
159 if ($isspecificcourse) {
160 $pageheading = format_string($course->shortname
, true, array('context' => $coursecontext));
162 $pageheading = get_string('pluginname', 'mod_forum');
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
170 if ($discussionsonly) {
171 $notification = get_string('nodiscussionsstartedbyyou', 'forum');
173 $notification = get_string('nopostsmadebyyou', 'forum');
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);
182 $notification = get_string('nopostsmadebyuser', 'forum', $fullname);
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));
191 $url = new moodle_url('/');
193 navigation_node
::override_active_url($url);
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);
202 if (!$url->compare($PAGE->url
)) {
203 echo $OUTPUT->continue_button($url);
205 echo $OUTPUT->footer();
209 // Post output will contain an entry containing HTML to display each post by the
211 $postoutput = array();
213 $discussions = array();
214 foreach ($result->posts
as $post) {
215 $discussions[] = $post->discussion
;
217 $discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions));
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';
226 foreach ($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
231 $forum = $result->forums
[$post->forum
];
233 $discussion = $discussions[$post->discussion
];
234 $course = $result->courses
[$discussion->course
];
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
));
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;
251 $ratingoptions->returnurl
= $discussionurl;
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];
259 $courseshortname = format_string($course->shortname
, true, array('context' => context_course
::instance($course->id
)));
260 $forumname = format_string($forum->name
, true, array('context' => $cm->context
));
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);
267 $fullsubjects[] = html_writer
::tag('span', $courseshortname);
268 $fullsubjects[] = html_writer
::tag('span', $forumname);
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);
275 $fullsubjects[] .= html_writer
::tag('span', $discussionname);
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);
282 $fullsubjects[] .= html_writer
::tag('span', $postname);
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"));
293 $postoutput[] = forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, $fulllink, '', null, true, null, true);
296 $userfullname = fullname($user);
298 if ($discussionsonly) {
299 $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname);
301 $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname);
303 if ($isspecificcourse) {
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);
311 $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a);
314 $pagetitle = $inpageheading;
315 $pageheading = $userfullname;
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.
323 echo $OUTPUT->header();
324 echo $OUTPUT->heading($inpageheading);
325 echo html_writer
::start_tag('div', array('class' => 'user-content'));
327 if (!empty($postoutput)) {
328 echo $OUTPUT->paging_bar($result->totalcount
, $page, $perpage, $url);
329 foreach ($postoutput as $post) {
331 echo html_writer
::empty_tag('br');
333 echo $OUTPUT->paging_bar($result->totalcount
, $page, $perpage, $url);
334 } else if ($discussionsonly) {
335 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum', $userfullname));
337 echo $OUTPUT->heading(get_string('noposts', 'forum'));
340 echo html_writer
::end_tag('div');
341 echo $OUTPUT->footer();