MDL-30012 mod_forum: Fixed bug in forum user script
[moodle.git] / mod / forum / user.php
CommitLineData
f9a0ea69 1<?php // $Id$
2
3// Display user activity reports for a course
4
5 require_once('../../config.php');
6 require_once('lib.php');
3f5636b5 7 // Course ID
8 $course = required_param('course', PARAM_INT);
9 // User ID
d3397f06 10 $id = optional_param('id', 0, PARAM_INT);
da5104e6 11 $mode = optional_param('mode', 'posts', PARAM_ALPHA);
12 $page = optional_param('page', 0, PARAM_INT);
13 $perpage = optional_param('perpage', 5, PARAM_INT);
f9a0ea69 14
42890783 15 if (empty($id)) { // See your own profile by default
16 require_login();
17 $id = $USER->id;
18 }
19
f9a0ea69 20 if (! $user = get_record("user", "id", $id)) {
21 error("User ID is incorrect");
22 }
23
24 if (! $course = get_record("course", "id", $course)) {
25 error("Course id is incorrect.");
26 }
27
5623276d 28 $syscontext = get_context_instance(CONTEXT_SYSTEM);
7034b46c 29 $usercontext = get_context_instance(CONTEXT_USER, $id);
1de384c3 30
7034b46c 31 // do not force parents to enrol
32 if (!get_record('role_assignments', 'userid', $USER->id, 'contextid', $usercontext->id)) {
33 require_course_login($course);
34 }
f9a0ea69 35
3e779c59 36 if ($user->deleted) {
37 print_header();
38 print_heading(get_string('userdeleted'));
39 print_footer($course);
40 die;
41 }
42
36b0ddee
SH
43 switch ($mode) {
44 case 'posts' :
45 $searchterms = array('userid:'.$user->id);
46 $extrasql = '';
47 break;
48
49 default:
50 $searchterms = array('userid:'.$user->id);
51 $extrasql = 'AND p.parent = 0';
52 break;
53 }
54 if ($course->id == SITEID) {
55 if (empty($CFG->forceloginforprofiles) || isloggedin()) {
56 // Search throughout the whole site.
57 $searchcourse = 0;
58 } else {
59 $searchcourse = SITEID;
60 }
61 } else {
62 // Search only for posts the user made in this course.
63 $searchcourse = $course->id;
64 }
65
66 $posts = forum_search_posts($searchterms, $searchcourse, $page*$perpage, $perpage, $totalcount, $extrasql);
67
68 $hasposts = !empty($posts);
69 $iscurrentuser = $user->id == $USER->id;
70 $specificcourseprovided = !empty($searchcourse) && $searchcourse != SITEID;
71 if (!$hasposts && !$iscurrentuser && !$specificcourseprovided) {
72 $mustlogin = (!isloggedin() && $CFG->forceloginforprofiles);
73 $canviewtheuser = (isloggedin() && has_capability('moodle/user:viewdetails', $usercontext));
74 if ($mustlogin || (!isteacherinanycourse() && !isteacherinanycourse($user->id) && !$canviewtheuser)) {
75 // Best to assume that the current user cannot view the requested user
76 // so we are careful not to give out any information.
77 print_header();
78 print_heading(get_string('noposts', 'forum'));
79 print_footer();
80 exit;
81 } else {
82 // Nothing to check here. If a course has been specified then require_course_login
83 // has been called OR the current user is a parent of the requested user.
84 }
85 }
86
9bdb39c7 87 add_to_log($course->id, "forum", "user report",
88 "user.php?course=$course->id&amp;id=$user->id&amp;mode=$mode", "$user->id");
f9a0ea69 89
90 $strforumposts = get_string('forumposts', 'forum');
91 $strparticipants = get_string('participants');
92 $strmode = get_string($mode, 'forum');
951e1073 93 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $syscontext));
f9a0ea69 94
01e38388 95 $navlinks = array();
33d2459c 96 if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_COURSE, $course->id)) || has_capability('moodle/site:viewparticipants', $syscontext)) {
97 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'core');
98 }
3b27b0fe 99 $navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id", 'type' => 'title');
100 $navlinks[] = array('name' => $strforumposts, 'link' => '', 'type' => 'title');
101 $navlinks[] = array('name' => $strmode, 'link' => '', 'type' => 'title');
1de384c3 102
3b27b0fe 103 $navigation = build_navigation($navlinks);
1de384c3 104
e3f58dfb 105 print_header("$course->shortname: $fullname: $strmode", $course->fullname,$navigation);
1de384c3 106
f9a0ea69 107
108 $currenttab = $mode;
b3e900ce 109 $showroles = 1;
f9a0ea69 110 include($CFG->dirroot.'/user/tabs.php'); /// Prints out tabs as part of user page
111
e1ddfa6b 112 echo '<div class="user-content">';
1de384c3 113
67875aa1 114 // Get the posts.
36b0ddee 115 if ($posts) {
1de384c3 116
117 print_paging_bar($totalcount, $page, $perpage,
f9a0ea69 118 "user.php?id=$user->id&amp;course=$course->id&amp;mode=$mode&amp;perpage=$perpage&amp;");
5623276d 119
120 $discussions = array();
121 $forums = array();
122 $cms = array();
1de384c3 123
f9a0ea69 124 foreach ($posts as $post) {
5623276d 125
126 if (!isset($discussions[$post->discussion])) {
127 if (! $discussion = get_record('forum_discussions', 'id', $post->discussion)) {
128 error('Discussion ID was incorrect');
129 }
130 $discussions[$post->discussion] = $discussion;
131 } else {
132 $discussion = $discussions[$post->discussion];
f9a0ea69 133 }
5623276d 134
135 if (!isset($forums[$discussion->forum])) {
136 if (! $forum = get_record('forum', 'id', $discussion->forum)) {
137 error("Could not find forum $discussion->forum");
138 }
139 $forums[$discussion->forum] = $forum;
140 } else {
141 $forum = $forums[$discussion->forum];
f9a0ea69 142 }
5623276d 143
1de384c3 144 $ratings = null;
145 if ($forum->assessed) {
146 if ($scale = make_grades_menu($forum->scale)) {
147 $ratings =new object();
148 $ratings->scale = $scale;
149 $ratings->assesstimestart = $forum->assesstimestart;
150 $ratings->assesstimefinish = $forum->assesstimefinish;
151 $ratings->allow = false;
152 }
153 }
154
5623276d 155 if (!isset($cms[$forum->id])) {
156 if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
157 error('Course Module ID was incorrect');
158 }
159 $cms[$forum->id] = $cm;
160 unset($cm); // do not use cm directly, it would break caching
161 }
162
3849dae8 163 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
f9a0ea69 164 if ($forum->type != 'single') {
c78ac798 165 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
f9a0ea69 166 if ($post->parent != 0) {
17dc3f3c 167 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
f9a0ea69 168 }
169 }
1de384c3 170
5623276d 171 if ($course->id == SITEID && has_capability('moodle/site:config', $syscontext)) {
a702e657 172 $postcoursename = get_field('course', 'shortname', 'id', $forum->course);
173 $fullsubject = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$forum->course.'">'.$postcoursename.'</a> -> '. $fullsubject;
174 }
bbbf2d40 175
f9a0ea69 176 $post->subject = $fullsubject;
bbbf2d40 177
9de96c06 178 $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".
f9a0ea69 179 get_string("postincontext", "forum")."</a>";
180
1de384c3 181 forum_print_post($post, $discussion, $forum, $cms[$forum->id], $course, false, false, false, $ratings, $fulllink);
f9a0ea69 182 echo "<br />";
183 }
1de384c3 184
185 print_paging_bar($totalcount, $page, $perpage,
f9a0ea69 186 "user.php?id=$user->id&amp;course=$course->id&amp;mode=$mode&amp;perpage=$perpage&amp;");
187 } else {
4d463048 188 if ($mode == 'posts') {
189 print_heading(get_string('noposts', 'forum'));
190 } else {
191 print_heading(get_string('nodiscussionsstartedby', 'forum'));
192 }
f9a0ea69 193 }
e1ddfa6b 194 echo '</div>';
f9a0ea69 195 print_footer($course);
196
9de96c06 197?>