view.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 profile for a particular user
  18. *
  19. * @package core_user
  20. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once("../config.php");
  24. require_once($CFG->dirroot.'/user/profile/lib.php');
  25. require_once($CFG->dirroot.'/user/lib.php');
  26. require_once($CFG->libdir . '/filelib.php');
  27. require_once($CFG->libdir . '/badgeslib.php');
  28. $id = optional_param('id', 0, PARAM_INT); // User id.
  29. $courseid = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site).
  30. $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
  31. // See your own profile by default.
  32. if (empty($id)) {
  33. require_login();
  34. $id = $USER->id;
  35. }
  36. if ($courseid == SITEID) { // Since Moodle 2.0 all site-level profiles are shown by profile.php.
  37. redirect($CFG->wwwroot.'/user/profile.php?id='.$id); // Immediate redirect.
  38. }
  39. $PAGE->set_url('/user/view.php', array('id' => $id, 'course' => $courseid));
  40. $user = $DB->get_record('user', array('id' => $id), '*', MUST_EXIST);
  41. $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  42. $currentuser = ($user->id == $USER->id);
  43. $systemcontext = context_system::instance();
  44. $coursecontext = context_course::instance($course->id);
  45. $usercontext = context_user::instance($user->id, IGNORE_MISSING);
  46. // Check we are not trying to view guest's profile.
  47. if (isguestuser($user)) {
  48. // Can not view profile of guest - thre is nothing to see there.
  49. print_error('invaliduserid');
  50. }
  51. $PAGE->set_context($coursecontext);
  52. if (!empty($CFG->forceloginforprofiles)) {
  53. require_login(); // We can not log in to course due to the parent hack below.
  54. // Guests do not have permissions to view anyone's profile if forceloginforprofiles is set.
  55. if (isguestuser()) {
  56. echo $OUTPUT->header();
  57. echo $OUTPUT->confirm(get_string('guestcantaccessprofiles', 'error'),
  58. get_login_url(),
  59. $CFG->wwwroot);
  60. echo $OUTPUT->footer();
  61. die;
  62. }
  63. }
  64. $PAGE->set_course($course);
  65. $PAGE->set_pagetype('course-view-' . $course->format); // To get the blocks exactly like the course.
  66. $PAGE->add_body_class('path-user'); // So we can style it independently.
  67. $PAGE->set_other_editing_capability('moodle/course:manageactivities');
  68. // Set the Moodle docs path explicitly because the default behaviour
  69. // of inhereting the pagetype will lead to an incorrect docs location.
  70. $PAGE->set_docs_path('user/profile');
  71. $isparent = false;
  72. if (!$currentuser and !$user->deleted
  73. and $DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))
  74. and has_capability('moodle/user:viewdetails', $usercontext)) {
  75. // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
  76. // this way they may access the profile where they get overview of grades and child activity in course,
  77. // please note this is just a guess!
  78. require_login();
  79. $isparent = true;
  80. $PAGE->navigation->set_userid_for_parent_checks($id);
  81. } else {
  82. // Normal course.
  83. require_login($course);
  84. // What to do with users temporary accessing this course? should they see the details?
  85. }
  86. $strpersonalprofile = get_string('personalprofile');
  87. $strparticipants = get_string("participants");
  88. $struser = get_string("user");
  89. $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
  90. // Now test the actual capabilities and enrolment in course.
  91. if ($currentuser) {
  92. if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) {
  93. // Need to have full access to a course to see the rest of own info.
  94. $referer = get_local_referer(false);
  95. if (!empty($referer)) {
  96. redirect($referer, get_string('notenrolled', '', $fullname));
  97. }
  98. echo $OUTPUT->header();
  99. echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
  100. echo $OUTPUT->footer();
  101. die;
  102. }
  103. } else {
  104. // Somebody else.
  105. $PAGE->set_title("$strpersonalprofile: ");
  106. $PAGE->set_heading("$strpersonalprofile: ");
  107. // Check to see if the user can see this user's profile.
  108. if (!user_can_view_profile($user, $course, $usercontext) && !$isparent) {
  109. print_error('cannotviewprofile');
  110. }
  111. if (!is_enrolled($coursecontext, $user->id)) {
  112. // TODO: the only potential problem is that managers and inspectors might post in forum, but the link
  113. // to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody
  114. // or test for course:inspect capability.
  115. if (has_capability('moodle/role:assign', $coursecontext)) {
  116. $PAGE->navbar->add($fullname);
  117. $notice = get_string('notenrolled', '', $fullname);
  118. } else {
  119. $PAGE->navbar->add($struser);
  120. $notice = get_string('notenrolledprofile', '', $fullname);
  121. }
  122. $referer = get_local_referer(false);
  123. if (!empty($referer)) {
  124. redirect($referer, $notice);
  125. }
  126. echo $OUTPUT->header();
  127. echo $OUTPUT->heading($notice);
  128. echo $OUTPUT->footer();
  129. exit;
  130. }
  131. if (!isloggedin() or isguestuser()) {
  132. // Do not use require_login() here because we might have already used require_login($course).
  133. redirect(get_login_url());
  134. }
  135. }
  136. $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
  137. $PAGE->set_heading($course->fullname);
  138. $PAGE->set_pagelayout('standard');
  139. // Locate the users settings in the settings navigation and force it open.
  140. // This MUST be done after we've set up the page as it is going to cause theme and output to initialise.
  141. if (!$currentuser) {
  142. $PAGE->navigation->extend_for_user($user);
  143. if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
  144. $node->forceopen = true;
  145. }
  146. } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
  147. $node->forceopen = true;
  148. }
  149. if ($node = $PAGE->settingsnav->get('courseadmin')) {
  150. $node->forceopen = false;
  151. }
  152. echo $OUTPUT->header();
  153. echo '<div class="userprofile">';
  154. $headerinfo = array('heading' => fullname($user), 'user' => $user, 'usercontext' => $usercontext);
  155. echo $OUTPUT->context_header($headerinfo, 2);
  156. if ($user->deleted) {
  157. echo $OUTPUT->heading(get_string('userdeleted'));
  158. if (!has_capability('moodle/user:update', $coursecontext)) {
  159. echo $OUTPUT->footer();
  160. die;
  161. }
  162. }
  163. // OK, security out the way, now we are showing the user.
  164. // Trigger a user profile viewed event.
  165. profile_view($user, $coursecontext, $course);
  166. if ($user->description && !isset($hiddenfields['description'])) {
  167. echo '<div class="description">';
  168. if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $id))) {
  169. echo get_string('profilenotshown', 'moodle');
  170. } else {
  171. if ($courseid == SITEID) {
  172. $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
  173. } else {
  174. // We have to make a little detour thought the course context to verify the access control for course profile.
  175. $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
  176. }
  177. $options = array('overflowdiv' => true);
  178. echo format_text($user->description, $user->descriptionformat, $options);
  179. }
  180. echo '</div>'; // Description class.
  181. }
  182. // Render custom blocks.
  183. $renderer = $PAGE->get_renderer('core_user', 'myprofile');
  184. $tree = core_user\output\myprofile\manager::build_tree($user, $currentuser, $course);
  185. echo $renderer->render($tree);
  186. echo '</div>'; // Userprofile class.
  187. echo $OUTPUT->footer();