weekly release 2.2dev
[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
bb8f3759 26require_once('../../config.php');
27require_once('lib.php');
28
29// Course ID
246c082a 30$course = optional_param('course', SITEID, PARAM_INT);
bb8f3759 31// User ID
32$id = optional_param('id', 0, PARAM_INT);
33$mode = optional_param('mode', 'posts', PARAM_ALPHA);
34$page = optional_param('page', 0, PARAM_INT);
35$perpage = optional_param('perpage', 5, PARAM_INT);
36
241ce9c4
SH
37$url = new moodle_url('/mod/forum/user.php');
38if ($course !== SITEID) {
39 $url->param('course', $course);
40}
cd4e6b17 41if ($id !== 0) {
42 $url->param('id', $id);
43}
44if ($mode !== 'posts') {
45 $url->param('mode', $mode);
46}
cd4e6b17 47$PAGE->set_url($url);
241ce9c4 48$PAGE->set_pagelayout('standard');
cd4e6b17 49
bb8f3759 50if (empty($id)) { // See your own profile by default
51 require_login();
52 $id = $USER->id;
53}
54
241ce9c4
SH
55$user = $DB->get_record("user", array("id" => $id), '*', MUST_EXIST);
56$course = $DB->get_record("course", array("id" => $course), '*', MUST_EXIST);
bb8f3759 57
58$syscontext = get_context_instance(CONTEXT_SYSTEM);
59$usercontext = get_context_instance(CONTEXT_USER, $id);
60
61// do not force parents to enrol
62if (!$DB->get_record('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))) {
63 require_course_login($course);
241ce9c4
SH
64} else {
65 $PAGE->set_course($course);
bb8f3759 66}
67
68if ($user->deleted) {
15ca5e5e 69 echo $OUTPUT->header();
f43f1834 70 echo $OUTPUT->heading(get_string('userdeleted'));
396fb912 71 echo $OUTPUT->footer();
bb8f3759 72 die;
73}
f9a0ea69 74
bb8f3759 75add_to_log($course->id, "forum", "user report",
76 "user.php?course=$course->id&amp;id=$user->id&amp;mode=$mode", "$user->id");
f5fc83e8 77
bb8f3759 78$strforumposts = get_string('forumposts', 'forum');
79$strparticipants = get_string('participants');
80$strmode = get_string($mode, 'forum');
81$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $syscontext));
f9a0ea69 82
15ca5e5e 83$link = null;
bb8f3759 84if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_COURSE, $course->id)) || has_capability('moodle/site:viewparticipants', $syscontext)) {
a6855934 85 $link = new moodle_url('/user/index.php',array('id'=>$course->id));
bb8f3759 86}
15ca5e5e 87
7a7e209d 88$PAGE->navigation->extend_for_user($user);
870815fa 89$PAGE->navigation->set_userid_for_parent_checks($id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
15ca5e5e 90$PAGE->set_title("$course->shortname: $fullname: $strmode");
91$PAGE->set_heading($course->fullname);
92echo $OUTPUT->header();
03d9401e 93echo $OUTPUT->heading($fullname);
f9a0ea69 94
bb8f3759 95switch ($mode) {
96 case 'posts' :
97 $searchterms = array('userid:'.$user->id);
98 $extrasql = '';
99 break;
fa22fd5f 100
bb8f3759 101 default:
102 $searchterms = array('userid:'.$user->id);
103 $extrasql = 'AND p.parent = 0';
104 break;
105}
f9a0ea69 106
bb8f3759 107echo '<div class="user-content">';
2011a95b 108
bb8f3759 109if ($course->id == SITEID) {
81b58cc2
PS
110 $searchcourse = SITEID;
111 if (empty($CFG->forceloginforprofiles) or (isloggedin() and !isguestuser() and !is_web_crawler())) {
bb8f3759 112 // Search throughout the whole site.
113 $searchcourse = 0;
67875aa1 114 }
bb8f3759 115} else {
116 // Search only for posts the user made in this course.
117 $searchcourse = $course->id;
118}
2011a95b 119
bb8f3759 120// Get the posts.
2b04c41c
SH
121$posts = forum_search_posts($searchterms, $searchcourse, $page*$perpage, $perpage, $totalcount, $extrasql);
122if ($posts) {
1adbd2c3 123
99fa9889
AD
124 require_once($CFG->dirroot.'/rating/lib.php');
125
4e412c45 126 $baseurl = new moodle_url('user.php', array('id' => $user->id, 'course' => $course->id, 'mode' => $mode, 'perpage' => $perpage));
929d7a83 127 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $baseurl);
65bcf17b 128
bb8f3759 129 $discussions = array();
130 $forums = array();
2011a95b 131
99fa9889
AD
132 //todo Rather than retrieving the ratings for each post individually it would be nice to do them in groups
133 //however this requires creating arrays of posts with each array containing all of the posts from a particular forum,
134 //retrieving the ratings then reassembling them all back into a single array sorted by post.modified (descending)
135 $rm = new rating_manager();
2b04c41c
SH
136 $ratingoptions = new stdClass;
137 $ratingoptions->component = 'mod_forum';
138 $ratingoptions->ratingarea = 'post';
99fa9889 139
bb8f3759 140 foreach ($posts as $post) {
65bcf17b 141
bb8f3759 142 if (!isset($discussions[$post->discussion])) {
143 if (! $discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion))) {
144 print_error('invaliddiscussionid', 'forum');
f9a0ea69 145 }
bb8f3759 146 $discussions[$post->discussion] = $discussion;
147 } else {
148 $discussion = $discussions[$post->discussion];
149 }
65bcf17b 150
bb8f3759 151 if (!isset($forums[$discussion->forum])) {
2b04c41c
SH
152 $forum = $DB->get_record('forum', array('id' => $discussion->forum), '*', MUST_EXIST);
153 $forum->cm = get_coursemodule_from_instance('forum', $forum->id, 0, false, MUST_EXIST);
154 $forum->context = get_context_instance(CONTEXT_MODULE, $forum->cm->id);
bb8f3759 155 $forums[$discussion->forum] = $forum;
156 } else {
157 $forum = $forums[$discussion->forum];
158 }
65bcf17b 159
2b04c41c
SH
160 $forumurl = new moodle_url('/mod/forum/view.php', array('id' => $forum->cm->id));
161 $discussionurl = new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
162
163 // load ratings
164 if ($forum->assessed != RATING_AGGREGATE_NONE) {
99fa9889
AD
165 $ratingoptions->context = $forum->context;
166 $ratingoptions->items = array($post);
167 $ratingoptions->aggregate = $forum->assessed;//the aggregation method
168 $ratingoptions->scaleid = $forum->scale;
169 $ratingoptions->userid = $user->id;
2b04c41c
SH
170 $ratingoptions->assesstimestart = $forum->assesstimestart;
171 $ratingoptions->assesstimefinish = $forum->assesstimefinish;
99fa9889 172 if ($forum->type == 'single' or !$discussion->id) {
2b04c41c 173 $ratingoptions->returnurl = $forumurl;
99fa9889 174 } else {
2b04c41c 175 $ratingoptions->returnurl = $discussionurl;
2011a95b 176 }
99fa9889
AD
177
178 $updatedpost = $rm->get_ratings($ratingoptions);
179 //updating the array this way because we're iterating over a collection and updating them one by one
180 $posts[$updatedpost[0]->id] = $updatedpost[0];
bb8f3759 181 }
2011a95b 182
2b04c41c
SH
183 $fullsubjects = array();
184 if ($course->id == SITEID && has_capability('moodle/site:config', $syscontext)) {
185 $postcoursename = $DB->get_field('course', 'shortname', array('id'=>$forum->course));
186 $courseurl = new moodle_url('/course/view.php', array('id' => $forum->course));
187 $fullsubjects[] = html_writer::link($courseurl, $postcoursename);
bb8f3759 188 }
2b04c41c 189 $fullsubjects[] = html_writer::link($forumurl, format_string($forum->name, true));
bb8f3759 190 if ($forum->type != 'single') {
2b04c41c 191 $fullsubjects[] .= html_writer::link($discussionurl, format_string($discussion->name, true));
bb8f3759 192 if ($post->parent != 0) {
2b04c41c
SH
193 $parenturl = new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion, 'parent' => $post->id));
194 $fullsubjects[] .= html_writer::link($parenturl, format_string($post->subject, true));
f9a0ea69 195 }
bb8f3759 196 }
2011a95b 197
2b04c41c
SH
198 $post->subject = join(' -> ', $fullsubjects);
199 $discussionurl->set_anchor('p'.$post->id);
200 $fulllink = html_writer::link($discussionurl, get_string("postincontext", "forum"));
f9a0ea69 201
2b04c41c 202 forum_print_post($post, $discussion, $forum, $forum->cm, $course, false, false, false, $fulllink);
bb8f3759 203 echo "<br />";
204 }
2011a95b 205
929d7a83 206 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $baseurl);
bb8f3759 207} else {
208 if ($mode == 'posts') {
f43f1834 209 echo $OUTPUT->heading(get_string('noposts', 'forum'));
f9a0ea69 210 } else {
f43f1834 211 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum'));
f9a0ea69 212 }
bb8f3759 213}
214echo '</div>';
396fb912 215echo $OUTPUT->footer();
f9a0ea69 216