PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/forum/user.php

http://github.com/moodle/moodle
PHP | 335 lines | 238 code | 34 blank | 63 comment | 69 complexity | fa8f55f29beb5a80c8fb72a95baa541f MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Display user activity reports for a course
  18. *
  19. * @package mod_forum
  20. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require(__DIR__.'/../../config.php');
  24. require_once($CFG->dirroot.'/mod/forum/lib.php');
  25. require_once($CFG->dirroot.'/rating/lib.php');
  26. require_once($CFG->dirroot.'/user/lib.php');
  27. $courseid = optional_param('course', null, PARAM_INT); // Limit the posts to just this course
  28. $userid = optional_param('id', $USER->id, PARAM_INT); // User id whose posts we want to view
  29. $mode = optional_param('mode', 'posts', PARAM_ALPHA); // The mode to use. Either posts or discussions
  30. $page = optional_param('page', 0, PARAM_INT); // The page number to display
  31. $perpage = optional_param('perpage', 5, PARAM_INT); // The number of posts to display per page
  32. if (empty($userid)) {
  33. if (!isloggedin()) {
  34. require_login();
  35. }
  36. $userid = $USER->id;
  37. }
  38. $discussionsonly = ($mode !== 'posts');
  39. $isspecificcourse = !is_null($courseid);
  40. $iscurrentuser = ($USER->id == $userid);
  41. $url = new moodle_url('/mod/forum/user.php', array('id' => $userid));
  42. if ($isspecificcourse) {
  43. $url->param('course', $courseid);
  44. }
  45. if ($discussionsonly) {
  46. $url->param('mode', 'discussions');
  47. }
  48. $PAGE->set_url($url);
  49. $PAGE->set_pagelayout('standard');
  50. if ($page != 0) {
  51. $url->param('page', $page);
  52. }
  53. if ($perpage != 5) {
  54. $url->param('perpage', $perpage);
  55. }
  56. $user = $DB->get_record("user", array("id" => $userid), '*', MUST_EXIST);
  57. $usercontext = context_user::instance($user->id, MUST_EXIST);
  58. // Check if the requested user is the guest user
  59. if (isguestuser($user)) {
  60. // The guest user cannot post, so it is not possible to view any posts.
  61. // May as well just bail aggressively here.
  62. print_error('invaliduserid');
  63. }
  64. // Make sure the user has not been deleted
  65. if ($user->deleted) {
  66. $PAGE->set_title(get_string('userdeleted'));
  67. $PAGE->set_context(context_system::instance());
  68. echo $OUTPUT->header();
  69. echo $OUTPUT->heading($PAGE->title);
  70. echo $OUTPUT->footer();
  71. die;
  72. }
  73. $isloggedin = isloggedin();
  74. $isguestuser = $isloggedin && isguestuser();
  75. $isparent = !$iscurrentuser && $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id));
  76. $hasparentaccess = $isparent && has_all_capabilities(array('moodle/user:viewdetails', 'moodle/user:readuserposts'), $usercontext);
  77. // Check whether a specific course has been requested
  78. if ($isspecificcourse) {
  79. // Get the requested course and its context
  80. $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  81. $coursecontext = context_course::instance($courseid, MUST_EXIST);
  82. // We have a specific course to search, which we will also assume we are within.
  83. if ($hasparentaccess) {
  84. // A `parent` role won't likely have access to the course so we won't attempt
  85. // to enter it. We will however still make them jump through the normal
  86. // login hoops
  87. require_login();
  88. $PAGE->set_context($coursecontext);
  89. $PAGE->set_course($course);
  90. } else {
  91. // Enter the course we are searching
  92. require_login($course);
  93. }
  94. // Get the course ready for access checks
  95. $courses = array($courseid => $course);
  96. } else {
  97. // We are going to search for all of the users posts in all courses!
  98. // a general require login here as we arn't actually within any course.
  99. require_login();
  100. $PAGE->set_context(context_user::instance($user->id));
  101. // Now we need to get all of the courses to search.
  102. // All courses where the user has posted within a forum will be returned.
  103. $courses = forum_get_courses_user_posted_in($user, $discussionsonly);
  104. }
  105. $params = array(
  106. 'context' => $PAGE->context,
  107. 'relateduserid' => $user->id,
  108. 'other' => array('reportmode' => $mode),
  109. );
  110. $event = \mod_forum\event\user_report_viewed::create($params);
  111. $event->trigger();
  112. // Get the posts by the requested user that the current user can access.
  113. $result = forum_get_posts_by_user($user, $courses, $isspecificcourse, $discussionsonly, ($page * $perpage), $perpage);
  114. // Check whether there are not posts to display.
  115. if (empty($result->posts)) {
  116. // Ok no posts to display means that either the user has not posted or there
  117. // are no posts made by the requested user that the current user is able to
  118. // see.
  119. // In either case we need to decide whether we can show personal information
  120. // about the requested user to the current user so we will execute some checks
  121. $canviewuser = user_can_view_profile($user, null, $usercontext);
  122. // Prepare the page title
  123. $pagetitle = get_string('noposts', 'mod_forum');
  124. // Get the page heading
  125. if ($isspecificcourse) {
  126. $pageheading = format_string($course->fullname, true, array('context' => $coursecontext));
  127. } else {
  128. $pageheading = get_string('pluginname', 'mod_forum');
  129. }
  130. // Next we need to set up the loading of the navigation and choose a message
  131. // to display to the current user.
  132. if ($iscurrentuser) {
  133. // No need to extend the navigation it happens automatically for the
  134. // current user.
  135. if ($discussionsonly) {
  136. $notification = get_string('nodiscussionsstartedbyyou', 'forum');
  137. } else {
  138. $notification = get_string('nopostsmadebyyou', 'forum');
  139. }
  140. // These are the user's forum interactions.
  141. // Shut down the navigation 'Users' node.
  142. $usernode = $PAGE->navigation->find('users', null);
  143. $usernode->make_inactive();
  144. // Edit navbar.
  145. if (isset($courseid) && $courseid != SITEID) {
  146. // Create as much of the navbar automatically.
  147. $newusernode = $PAGE->navigation->find('user' . $user->id, null);
  148. $newusernode->make_active();
  149. // Check to see if this is a discussion or a post.
  150. if ($mode == 'posts') {
  151. $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
  152. array('id' => $user->id, 'course' => $courseid)));
  153. } else {
  154. $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
  155. array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
  156. }
  157. }
  158. } else if ($canviewuser) {
  159. $PAGE->navigation->extend_for_user($user);
  160. $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
  161. // Edit navbar.
  162. if (isset($courseid) && $courseid != SITEID) {
  163. // Create as much of the navbar automatically.
  164. $usernode = $PAGE->navigation->find('user' . $user->id, null);
  165. $usernode->make_active();
  166. // Check to see if this is a discussion or a post.
  167. if ($mode == 'posts') {
  168. $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
  169. array('id' => $user->id, 'course' => $courseid)));
  170. } else {
  171. $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
  172. array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
  173. }
  174. }
  175. $fullname = fullname($user);
  176. if ($discussionsonly) {
  177. $notification = get_string('nodiscussionsstartedby', 'forum', $fullname);
  178. } else {
  179. $notification = get_string('nopostsmadebyuser', 'forum', $fullname);
  180. }
  181. } else {
  182. // Don't extend the navigation it would be giving out information that
  183. // the current uesr doesn't have access to.
  184. $notification = get_string('cannotviewusersposts', 'forum');
  185. if ($isspecificcourse) {
  186. $url = new moodle_url('/course/view.php', array('id' => $courseid));
  187. } else {
  188. $url = new moodle_url('/');
  189. }
  190. navigation_node::override_active_url($url);
  191. }
  192. // Display a page letting the user know that there's nothing to display;
  193. $PAGE->set_title($pagetitle);
  194. if ($isspecificcourse) {
  195. $PAGE->set_heading($pageheading);
  196. } else if ($canviewuser) {
  197. $PAGE->set_heading(fullname($user));
  198. } else {
  199. $PAGE->set_heading($SITE->fullname);
  200. }
  201. echo $OUTPUT->header();
  202. if (!$isspecificcourse) {
  203. echo $OUTPUT->heading($pagetitle);
  204. } else {
  205. $userheading = array(
  206. 'heading' => fullname($user),
  207. 'user' => $user,
  208. 'usercontext' => $usercontext
  209. );
  210. echo $OUTPUT->context_header($userheading, 2);
  211. }
  212. echo $OUTPUT->notification($notification);
  213. if (!$url->compare($PAGE->url)) {
  214. echo $OUTPUT->continue_button($url);
  215. }
  216. echo $OUTPUT->footer();
  217. die;
  218. }
  219. $discussions = array();
  220. foreach ($result->posts as $post) {
  221. $discussions[] = $post->discussion;
  222. }
  223. $discussions = $DB->get_records_list('forum_discussions', 'id', array_unique($discussions));
  224. $entityfactory = mod_forum\local\container::get_entity_factory();
  225. $rendererfactory = mod_forum\local\container::get_renderer_factory();
  226. $postsrenderer = $rendererfactory->get_user_forum_posts_report_renderer(!$isspecificcourse && !$hasparentaccess);
  227. $postoutput = $postsrenderer->render(
  228. $USER,
  229. array_map(function($forum) use ($entityfactory, $result) {
  230. $cm = $forum->cm;
  231. $context = context_module::instance($cm->id);
  232. $course = $result->courses[$forum->course];
  233. return $entityfactory->get_forum_from_stdclass($forum, $context, $cm, $course);
  234. }, $result->forums),
  235. array_map(function($discussion) use ($entityfactory) {
  236. return $entityfactory->get_discussion_from_stdclass($discussion);
  237. }, $discussions),
  238. array_map(function($post) use ($entityfactory) {
  239. return $entityfactory->get_post_from_stdclass($post);
  240. }, $result->posts)
  241. );
  242. $userfullname = fullname($user);
  243. if ($discussionsonly) {
  244. $inpageheading = get_string('discussionsstartedby', 'mod_forum', $userfullname);
  245. } else {
  246. $inpageheading = get_string('postsmadebyuser', 'mod_forum', $userfullname);
  247. }
  248. if ($isspecificcourse) {
  249. $a = new stdClass;
  250. $a->fullname = $userfullname;
  251. $a->coursename = format_string($course->fullname, true, array('context' => $coursecontext));
  252. $pageheading = $a->coursename;
  253. if ($discussionsonly) {
  254. $pagetitle = get_string('discussionsstartedbyuserincourse', 'mod_forum', $a);
  255. } else {
  256. $pagetitle = get_string('postsmadebyuserincourse', 'mod_forum', $a);
  257. }
  258. } else {
  259. $pagetitle = $inpageheading;
  260. $pageheading = $userfullname;
  261. }
  262. $PAGE->set_title($pagetitle);
  263. $PAGE->set_heading($pageheading);
  264. $PAGE->navigation->extend_for_user($user);
  265. $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
  266. // Edit navbar.
  267. if (isset($courseid) && $courseid != SITEID) {
  268. $usernode = $PAGE->navigation->find('user' . $user->id , null);
  269. $usernode->make_active();
  270. // Check to see if this is a discussion or a post.
  271. if ($mode == 'posts') {
  272. $navbar = $PAGE->navbar->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php',
  273. array('id' => $user->id, 'course' => $courseid)));
  274. } else {
  275. $navbar = $PAGE->navbar->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
  276. array('id' => $user->id, 'course' => $courseid, 'mode' => 'discussions')));
  277. }
  278. }
  279. echo $OUTPUT->header();
  280. echo html_writer::start_tag('div', array('class' => 'user-content'));
  281. if ($isspecificcourse) {
  282. $userheading = array(
  283. 'heading' => fullname($user),
  284. 'user' => $user,
  285. 'usercontext' => $usercontext
  286. );
  287. echo $OUTPUT->context_header($userheading, 2);
  288. } else {
  289. echo $OUTPUT->heading($inpageheading);
  290. }
  291. if (!empty($postoutput)) {
  292. echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
  293. echo $postoutput;
  294. echo $OUTPUT->paging_bar($result->totalcount, $page, $perpage, $url);
  295. } else if ($discussionsonly) {
  296. echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum', $userfullname));
  297. } else {
  298. echo $OUTPUT->heading(get_string('noposts', 'forum'));
  299. }
  300. echo html_writer::end_tag('div');
  301. echo $OUTPUT->footer();