MDL-25805 Roles propagated ugly hack for parent seeing child activities into navigati...
[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);
b9bcad24 89$PAGE->navigation->set_userid_for_parent_nav_hack($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.
241ce9c4 121if ($posts = forum_search_posts($searchterms, $searchcourse, $page*$perpage, $perpage, $totalcount, $extrasql)) {
1adbd2c3 122
99fa9889
AD
123 require_once($CFG->dirroot.'/rating/lib.php');
124
4e412c45 125 $baseurl = new moodle_url('user.php', array('id' => $user->id, 'course' => $course->id, 'mode' => $mode, 'perpage' => $perpage));
929d7a83 126 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $baseurl);
65bcf17b 127
bb8f3759 128 $discussions = array();
129 $forums = array();
130 $cms = 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();
136 $ratingoptions = new stdclass();
137 $ratingoptions->plugintype = 'mod';
138 $ratingoptions->pluginname = 'forum';
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])) {
152 if (! $forum = $DB->get_record('forum', array('id' => $discussion->forum))) {
153 print_error('invalidforumid', 'forum');
f9a0ea69 154 }
99fa9889
AD
155 //hold onto forum cm and context for when we load ratings
156 if ($forumcm = get_coursemodule_from_instance('forum', $forum->id)) {
157 $forum->cm = $forumcm;
158 $forumcontext = get_context_instance(CONTEXT_MODULE, $forum->cm->id);
159 $forum->context = $forumcontext;
160 }
bb8f3759 161 $forums[$discussion->forum] = $forum;
162 } else {
163 $forum = $forums[$discussion->forum];
164 }
65bcf17b 165
99fa9889
AD
166 //load ratings
167 if ($forum->assessed!=RATING_AGGREGATE_NONE) {
168 $ratingoptions->context = $forum->context;
169 $ratingoptions->items = array($post);
170 $ratingoptions->aggregate = $forum->assessed;//the aggregation method
171 $ratingoptions->scaleid = $forum->scale;
172 $ratingoptions->userid = $user->id;
173 if ($forum->type == 'single' or !$discussion->id) {
174 $ratingoptions->returnurl = "$CFG->wwwroot/mod/forum/view.php?id={$forum->cm->id}";
175 } else {
176 $ratingoptions->returnurl = "$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id";
2011a95b 177 }
99fa9889
AD
178 $ratingoptions->assesstimestart = $forum->assesstimestart;
179 $ratingoptions->assesstimefinish = $forum->assesstimefinish;
180
181 $updatedpost = $rm->get_ratings($ratingoptions);
182 //updating the array this way because we're iterating over a collection and updating them one by one
183 $posts[$updatedpost[0]->id] = $updatedpost[0];
bb8f3759 184 }
2011a95b 185
bb8f3759 186 if (!isset($cms[$forum->id])) {
241ce9c4 187 $cm = get_coursemodule_from_instance('forum', $forum->id, 0, false, MUST_EXIST);
bb8f3759 188 $cms[$forum->id] = $cm;
189 unset($cm); // do not use cm directly, it would break caching
190 }
65bcf17b 191
bb8f3759 192 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
193 if ($forum->type != 'single') {
194 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
195 if ($post->parent != 0) {
196 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
f9a0ea69 197 }
bb8f3759 198 }
2011a95b 199
bb8f3759 200 if ($course->id == SITEID && has_capability('moodle/site:config', $syscontext)) {
8c271b29 201 $postcoursename = $DB->get_field('course', 'shortname', array('id'=>$forum->course));
bb8f3759 202 $fullsubject = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$forum->course.'">'.$postcoursename.'</a> -> '. $fullsubject;
203 }
bbbf2d40 204
bb8f3759 205 $post->subject = $fullsubject;
bbbf2d40 206
bb8f3759 207 $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".
208 get_string("postincontext", "forum")."</a>";
f9a0ea69 209
954035a9 210 forum_print_post($post, $discussion, $forum, $cms[$forum->id], $course, false, false, false, $fulllink);
bb8f3759 211 echo "<br />";
212 }
2011a95b 213
929d7a83 214 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $baseurl);
bb8f3759 215} else {
216 if ($mode == 'posts') {
f43f1834 217 echo $OUTPUT->heading(get_string('noposts', 'forum'));
f9a0ea69 218 } else {
f43f1834 219 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum'));
f9a0ea69 220 }
bb8f3759 221}
222echo '</div>';
396fb912 223echo $OUTPUT->footer();
f9a0ea69 224