|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00026 require_once(dirname(dirname(dirname(__FILE__))).'/config.php'); 00027 require_once($CFG->dirroot.'/mod/forum/lib.php'); 00028 require_once($CFG->dirroot.'/rating/lib.php'); 00029 00030 $courseid = optional_param('course', null, PARAM_INT); // Limit the posts to just this course 00031 $userid = optional_param('id', $USER->id, PARAM_INT); // User id whose posts we want to view 00032 $mode = optional_param('mode', 'posts', PARAM_ALPHA); // The mode to use. Either posts or discussions 00033 $page = optional_param('page', 0, PARAM_INT); // The page number to display 00034 $perpage = optional_param('perpage', 5, PARAM_INT); // The number of posts to display per page 00035 00036 if (empty($userid)) { 00037 if (!isloggedin()) { 00038 require_login(); 00039 } 00040 $userid = $USER->id; 00041 } 00042 00043 $discussionsonly = ($mode !== 'posts'); 00044 $isspecificcourse = !is_null($courseid); 00045 $iscurrentuser = ($USER->id == $userid); 00046 00047 $url = new moodle_url('/mod/forum/user.php', array('id' => $userid)); 00048 if ($isspecificcourse) { 00049 $url->param('course', $courseid); 00050 } 00051 if ($discussionsonly) { 00052 $url->param('mode', 'discussions'); 00053 } 00054 00055 $PAGE->set_url($url); 00056 $PAGE->set_pagelayout('standard'); 00057 00058 if ($page != 0) { 00059 $url->param('page', $page); 00060 } 00061 if ($perpage != 5) { 00062 $url->param('perpage', $perpage); 00063 } 00064 00065 add_to_log(($isspecificcourse)?$courseid:SITEID, "forum", "user report", 'user.php?'.$url->get_query_string(), $userid); 00066 00067 $user = $DB->get_record("user", array("id" => $userid), '*', MUST_EXIST); 00068 $usercontext = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST); 00069 // Check if the requested user is the guest user 00070 if (isguestuser($user)) { 00071 // The guest user cannot post, so it is not possible to view any posts. 00072 // May as well just bail aggressively here. 00073 print_error('invaliduserid'); 00074 } 00075 // Make sure the user has not been deleted 00076 if ($user->deleted) { 00077 $PAGE->set_title(get_string('userdeleted')); 00078 $PAGE->set_context(get_system_context()); 00079 echo $OUTPUT->header(); 00080 echo $OUTPUT->heading($PAGE->title); 00081 echo $OUTPUT->footer(); 00082 die; 00083 } 00084 00085 $isloggedin = isloggedin(); 00086 $isguestuser = $isloggedin && isguestuser(); 00087 $isparent = !$iscurrentuser && $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id)); 00088 $hasparentaccess = $isparent && has_all_capabilities(array('moodle/user:viewdetails', 'moodle/user:readuserposts'), $usercontext); 00089 00090 // Check whether a specific course has been requested 00091 if ($isspecificcourse) { 00092 // Get the requested course and its context 00093 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); 00094 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST); 00095 // We have a specific course to search, which we will also assume we are within. 00096 if ($hasparentaccess) { 00097 // A `parent` role won't likely have access to the course so we won't attempt 00098 // to enter it. We will however still make them jump through the normal 00099 // login hoops 00100 require_login(); 00101 $PAGE->set_context($coursecontext); 00102 $PAGE->set_course($course); 00103 } else { 00104 // Enter the course we are searching 00105 require_login($course); 00106 } 00107 // Get the course ready for access checks 00108 $courses = array($courseid => $course); 00109 } else { 00110 // We are going to search for all of the users posts in all courses! 00111 // a general require login here as we arn't actually within any course. 00112 require_login(); 00113 $PAGE->set_context(get_system_context()); 00114 00115 // Now we need to get all of the courses to search. 00116 // All courses where the user has posted within a forum will be returned. 00117 $courses = forum_get_courses_user_posted_in($user, $discussionsonly); 00118 } 00119 00120 // Get the posts by the requested user that the current user can access. 00121 $result = forum_get_posts_by_user($user, $courses, $isspecificcourse, $discussionsonly, ($page * $perpage), $perpage); 00122 00123 // Check whether there are not posts to display. 00124 if (empty($result->posts)) { 00125 // Ok no posts to display means that either the user has not posted or there 00126 // are no posts made by the requested user that the current user is able to 00127 // see. 00128 // In either case we need to decide whether we can show personal information 00129 // about the requested user to the current user so we will execute some checks 00130 00131 // First check the obvious, its the current user, a specific course has been 00132 // provided (require_login has been called), or they have a course contact role. 00133 // True to any of those and the current user can see the details of the 00134 // requested user. 00135 $canviewuser = ($iscurrentuser || $isspecificcourse || empty($CFG->forceloginforprofiles) || has_coursecontact_role($userid)); 00136 // Next we'll check the caps, if the current user has the view details and a 00137 // specific course has been requested, or if they have the view all details 00138 $canviewuser = ($canviewuser || ($isspecificcourse && has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewalldetails', $usercontext))); 00139 00140 // If none of the above was true the next step is to check a shared relation 00141 // through some course 00142 if (!$canviewuser) { 00143 // Get all of the courses that the users have in common 00144 $sharedcourses = enrol_get_shared_courses($USER->id, $user->id, true); 00145 foreach ($sharedcourses as $sharedcourse) { 00146 // Check the view cap within the course context 00147 if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_COURSE, $sharedcourse->id))) { 00148 $canviewuser = true; 00149 break; 00150 } 00151 } 00152 unset($sharedcourses); 00153 } 00154 00155 // Prepare the page title 00156 $pagetitle = get_string('noposts', 'mod_forum'); 00157 00158 // Get the page heading 00159 if ($isspecificcourse) { 00160 $pageheading = format_string($course->shortname, true, array('context' => $coursecontext)); 00161 } else { 00162 $pageheading = get_string('pluginname', 'mod_forum'); 00163 } 00164 00165 // Next we need to set up the loading of the navigation and choose a message 00166 // to display to the current user. 00167 if ($iscurrentuser) { 00168 // No need to extend the navigation it happens automatically for the 00169 // current user. 00170 if ($discussionsonly) { 00171 $notification = get_string('nodiscussionsstartedbyyou', 'forum'); 00172 } else { 00173 $notification = get_string('nopostsmadebyyou', 'forum'); 00174 } 00175 } else if ($canviewuser) { 00176 $PAGE->navigation->extend_for_user($user); 00177 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed. 00178 $fullname = fullname($user); 00179 if ($discussionsonly) { 00180 $notification = get_string('nodiscussionsstartedby', 'forum', $fullname); 00181 } else { 00182 $notification = get_string('nopostsmadebyuser', 'forum', $fullname); 00183 } 00184 } else { 00185 // Don't extend the navigation it would be giving out information that 00186 // the current uesr doesn't have access to. 00187 $notification = get_string('cannotviewusersposts', 'forum'); 00188 if ($isspecificcourse) { 00189 $url = new moodle_url('/course/view.php', array('id' => $courseid)); 00190 } else { 00191 $url = new moodle_url('/'); 00192 } 00193 navigation_node::override_active_url($url); 00194 } 00195 00196 // Display a page letting the user know that there's nothing to display; 00197 $PAGE->set_title($pagetitle); 00198 $PAGE->set_heading($pageheading); 00199 echo $OUTPUT->header(); 00200 echo $OUTPUT->heading($pagetitle); 00201 echo $OUTPUT->notification($notification); 00202 if (!$url->compare($PAGE->url)) { 00203 echo $OUTPUT->continue_button($url); 00204 } 00205 echo $OUTPUT->footer(); 00206 die; 00207 } 00208 00209 // Post output will contain an entry containing HTML to display each post by the 00210 // time we are done. 00211 $postoutput = array(); 00212 00213 $discussions = array(); 00214 foreach ($result->posts as $post) { 00215 $discussions[] = $post->discussion; 00216 } 00217 $discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions)); 00218 00219 //todo Rather than retrieving the ratings for each post individually it would be nice to do them in groups 00220 //however this requires creating arrays of posts with each array containing all of the posts from a particular forum, 00221 //retrieving the ratings then reassembling them all back into a single array sorted by post.modified (descending) 00222 $rm = new rating_manager(); 00223 $ratingoptions = new stdClass; 00224 $ratingoptions->component = 'mod_forum'; 00225 $ratingoptions->ratingarea = 'post'; 00226 foreach ($result->posts as $post) { 00227 if (!isset($result->forums[$post->forum]) || !isset($discussions[$post->discussion])) { 00228 // Something very VERY dodgy has happened if we end up here 00229 continue; 00230 } 00231 $forum = $result->forums[$post->forum]; 00232 $cm = $forum->cm; 00233 $discussion = $discussions[$post->discussion]; 00234 $course = $result->courses[$discussion->course]; 00235 00236 $forumurl = new moodle_url('/mod/forum/view.php', array('id' => $cm->id)); 00237 $discussionurl = new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion)); 00238 00239 // load ratings 00240 if ($forum->assessed != RATING_AGGREGATE_NONE) { 00241 $ratingoptions->context = $cm->context; 00242 $ratingoptions->items = array($post); 00243 $ratingoptions->aggregate = $forum->assessed;//the aggregation method 00244 $ratingoptions->scaleid = $forum->scale; 00245 $ratingoptions->userid = $user->id; 00246 $ratingoptions->assesstimestart = $forum->assesstimestart; 00247 $ratingoptions->assesstimefinish = $forum->assesstimefinish; 00248 if ($forum->type == 'single' or !$post->discussion) { 00249 $ratingoptions->returnurl = $forumurl; 00250 } else { 00251 $ratingoptions->returnurl = $discussionurl; 00252 } 00253 00254 $updatedpost = $rm->get_ratings($ratingoptions); 00255 //updating the array this way because we're iterating over a collection and updating them one by one 00256 $result->posts[$updatedpost[0]->id] = $updatedpost[0]; 00257 } 00258 00259 $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); 00260 $forumname = format_string($forum->name, true, array('context' => $cm->context)); 00261 00262 $fullsubjects = array(); 00263 if (!$isspecificcourse && !$hasparentaccess) { 00264 $fullsubjects[] = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $courseshortname); 00265 $fullsubjects[] = html_writer::link($forumurl, $forumname); 00266 } else { 00267 $fullsubjects[] = html_writer::tag('span', $courseshortname); 00268 $fullsubjects[] = html_writer::tag('span', $forumname); 00269 } 00270 if ($forum->type != 'single') { 00271 $discussionname = format_string($discussion->name, true, array('context' => $cm->context)); 00272 if (!$isspecificcourse && !$hasparentaccess) { 00273 $fullsubjects[] .= html_writer::link($discussionurl, $discussionname); 00274 } else { 00275 $fullsubjects[] .= html_writer::tag('span', $discussionname); 00276 } 00277 if ($post->parent != 0) { 00278 $postname = format_string($post->subject, true, array('context' => $cm->context)); 00279 if (!$isspecificcourse && !$hasparentaccess) { 00280 $fullsubjects[] .= html_writer::link(new moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion, 'parent' => $post->id)), $postname); 00281 } else { 00282 $fullsubjects[] .= html_writer::tag('span', $postname); 00283 } 00284 } 00285 } 00286 $post->subject = join(' -> ', $fullsubjects); 00287 // This is really important, if the strings are formatted again all the links 00288 // we've added will be lost. 00289 $post->subjectnoformat = true; 00290 $discussionurl->set_anchor('p'.$post->id); 00291 $fulllink = html_writer::link($discussionurl, get_string("postincontext", "forum")); 00292 00293 $postoutput[] = forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, $fulllink, '', null, true, null, true); 00294 } 00295 00296 $userfullname = fullname($user); 00297 00298 if ($discussionsonly) { 00299 $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname); 00300 } else { 00301 $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname); 00302 } 00303 if ($isspecificcourse) { 00304 $a = new stdClass; 00305 $a->fullname = $userfullname; 00306 $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext)); 00307 $pageheading = $a->coursename; 00308 if ($discussionsonly) { 00309 $pagetitle = get_string('discussionsstartedbyuserincourse', 'mod_forum', $a); 00310 } else { 00311 $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a); 00312 } 00313 } else { 00314 $pagetitle = $inpageheading; 00315 $pageheading = $userfullname; 00316 } 00317 00318 $PAGE->set_title($pagetitle); 00319 $PAGE->set_heading($pagetitle); 00320 $PAGE->navigation->extend_for_user($user); 00321 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed. 00322 00323 echo $OUTPUT->header(); 00324 echo $OUTPUT->heading($inpageheading); 00325 echo html_writer::start_tag('div', array('class' => 'user-content')); 00326 00327 if (!empty($postoutput)) { 00328 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url); 00329 foreach ($postoutput as $post) { 00330 echo $post; 00331 echo html_writer::empty_tag('br'); 00332 } 00333 echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url); 00334 } else if ($discussionsonly) { 00335 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum', $userfullname)); 00336 } else { 00337 echo $OUTPUT->heading(get_string('noposts', 'forum')); 00338 } 00339 00340 echo html_writer::end_tag('div'); 00341 echo $OUTPUT->footer();