weekly release 4.5dev
[moodle.git] / lib / myprofilelib.php
blob71b86242389689befbe0d67bafc5bd1ba9bdad3a
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.
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/>.
17 /**
18 * Defines core nodes for my profile navigation tree.
20 * @package core
21 * @copyright 2015 onwards Ankit Agarwal
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Defines core nodes for my profile navigation tree.
30 * @param \core_user\output\myprofile\tree $tree Tree object
31 * @param stdClass $user user object
32 * @param bool $iscurrentuser is the user viewing profile, current user ?
33 * @param stdClass $course course object
35 function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
36 global $CFG, $USER, $DB, $PAGE, $OUTPUT;
38 $usercontext = context_user::instance($user->id, MUST_EXIST);
39 $systemcontext = context_system::instance();
40 $courseorusercontext = !empty($course) ? context_course::instance($course->id) : $usercontext;
41 $courseorsystemcontext = !empty($course) ? context_course::instance($course->id) : $systemcontext;
42 $courseid = !empty($course) ? $course->id : SITEID;
44 $contactcategory = new core_user\output\myprofile\category('contact', get_string('userdetails'));
45 // No after property specified intentionally. It is a hack to make administration block appear towards the end. Refer MDL-49928.
46 $coursedetailscategory = new core_user\output\myprofile\category('coursedetails', get_string('coursedetails'));
47 $miscategory = new core_user\output\myprofile\category('miscellaneous', get_string('miscellaneous'), 'coursedetails');
48 $reportcategory = new core_user\output\myprofile\category('reports', get_string('reports'), 'miscellaneous');
49 $admincategory = new core_user\output\myprofile\category('administration', get_string('administration'), 'reports');
50 $loginactivitycategory = new core_user\output\myprofile\category('loginactivity', get_string('loginactivity'), 'administration');
52 // Add categories.
53 $tree->add_category($contactcategory);
54 $tree->add_category($coursedetailscategory);
55 $tree->add_category($miscategory);
56 $tree->add_category($reportcategory);
57 $tree->add_category($admincategory);
58 $tree->add_category($loginactivitycategory);
60 // Add core nodes.
61 // Full profile node.
62 if (!empty($course)) {
63 if (user_can_view_profile($user, null, $usercontext)) {
64 $url = new moodle_url('/user/profile.php', array('id' => $user->id));
65 $node = new core_user\output\myprofile\node('miscellaneous', 'fullprofile', get_string('fullprofile'), null, $url);
66 $tree->add_node($node);
70 // Edit profile.
71 if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) {
72 if (($iscurrentuser || is_siteadmin($USER) || !is_siteadmin($user)) && has_capability('moodle/user:update',
73 $systemcontext)) {
74 $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $courseid,
75 'returnto' => 'profile'));
76 $node = new core_user\output\myprofile\node('contact', 'editprofile', get_string('editmyprofile'), null, $url,
77 null, null, 'editprofile');
78 $tree->add_node($node);
79 } else if ((has_capability('moodle/user:editprofile', $usercontext) && !is_siteadmin($user))
80 || ($iscurrentuser && has_capability('moodle/user:editownprofile', $systemcontext))) {
81 $userauthplugin = false;
82 if (!empty($user->auth)) {
83 $userauthplugin = get_auth_plugin($user->auth);
85 if ($userauthplugin && $userauthplugin->can_edit_profile()) {
86 $url = $userauthplugin->edit_profile_url();
87 if (empty($url)) {
88 if (empty($course)) {
89 $url = new moodle_url('/user/edit.php', array('id' => $user->id, 'returnto' => 'profile'));
90 } else {
91 $url = new moodle_url('/user/edit.php', array('id' => $user->id, 'course' => $course->id,
92 'returnto' => 'profile'));
95 $node = new core_user\output\myprofile\node('contact', 'editprofile',
96 get_string('editmyprofile'), null, $url, null, null, 'editprofile');
97 $tree->add_node($node);
102 // Preference page.
103 if (!$iscurrentuser && $PAGE->settingsnav->can_view_user_preferences($user->id)) {
104 $url = new moodle_url('/user/preferences.php', array('userid' => $user->id));
105 $title = get_string('preferences', 'moodle');
106 $node = new core_user\output\myprofile\node('administration', 'preferences', $title, null, $url);
107 $tree->add_node($node);
110 // Login as ...
111 if (!$user->deleted && !$iscurrentuser &&
112 !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas',
113 $courseorsystemcontext) && !is_siteadmin($user->id)) {
114 $url = new moodle_url('/course/loginas.php',
115 array('id' => $courseid, 'user' => $user->id, 'sesskey' => sesskey()));
116 $node = new core_user\output\myprofile\node('administration', 'loginas', get_string('loginas'), null, $url);
117 $tree->add_node($node);
120 // Contact details.
121 if (has_capability('moodle/user:viewhiddendetails', $courseorusercontext)) {
122 $hiddenfields = array();
123 } else {
124 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
126 // TODO Does not support custom user profile fields (MDL-70456).
127 $identityfields = array_flip(\core_user\fields::get_identity_fields($courseorusercontext, false));
129 if (is_mnet_remote_user($user)) {
130 $sql = "SELECT h.id, h.name, h.wwwroot,
131 a.name as application, a.display_name
132 FROM {mnet_host} h, {mnet_application} a
133 WHERE h.id = ? AND h.applicationid = a.id";
135 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
136 $remoteuser = new stdclass();
137 $remoteuser->remotetype = $remotehost->display_name;
138 $hostinfo = new stdclass();
139 $hostinfo->remotename = $remotehost->name;
140 $hostinfo->remoteurl = $remotehost->wwwroot;
142 $node = new core_user\output\myprofile\node('contact', 'mnet', get_string('remoteuser', 'mnet', $remoteuser), null, null,
143 get_string('remoteuserinfo', 'mnet', $hostinfo), null, 'remoteuserinfo');
144 $tree->add_node($node);
147 if ($iscurrentuser
148 or (!isset($hiddenfields['email']) and (
149 $user->maildisplay == core_user::MAILDISPLAY_EVERYONE
150 or ($user->maildisplay == core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY and enrol_sharing_course($user, $USER))
151 or has_capability('moodle/course:useremail', $courseorusercontext) // TODO: Deprecate/remove for MDL-37479.
153 or (isset($identityfields['email']))
155 $maildisplay = obfuscate_mailto($user->email, '');
156 if ($iscurrentuser) {
157 if ($user->maildisplay == core_user::MAILDISPLAY_EVERYONE) {
158 $maildisplay .= ' ' . get_string('emaildisplayeveryone');
159 } else if ($user->maildisplay == core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY) {
160 $maildisplay .= ' ' . get_string('emaildisplaycoursemembersonly');
161 } else {
162 $maildisplay .= ' ' . get_string('emaildisplayhide');
165 $node = new core_user\output\myprofile\node('contact', 'email', get_string('email'),
166 null, null, $maildisplay);
167 $tree->add_node($node);
170 if (!isset($hiddenfields['moodlenetprofile']) && $user->moodlenetprofile) {
171 $node = new core_user\output\myprofile\node('contact', 'moodlenetprofile', get_string('moodlenetprofile', 'user'), null,
172 null, $user->moodlenetprofile);
173 $tree->add_node($node);
176 if (!isset($hiddenfields['country']) && $user->country) {
177 $node = new core_user\output\myprofile\node('contact', 'country', get_string('country'), null, null,
178 get_string($user->country, 'countries'));
179 $tree->add_node($node);
182 if (!isset($hiddenfields['city']) && $user->city) {
183 $node = new core_user\output\myprofile\node('contact', 'city', get_string('city'), null, null, $user->city);
184 $tree->add_node($node);
187 if (!isset($hiddenfields['timezone'])) {
188 $node = new core_user\output\myprofile\node('contact', 'timezone', get_string('timezone'), null, null,
189 core_date::get_user_timezone($user));
190 $tree->add_node($node);
193 if (isset($identityfields['address']) && $user->address) {
194 $node = new core_user\output\myprofile\node('contact', 'address', get_string('address'), null, null, $user->address);
195 $tree->add_node($node);
198 if (isset($identityfields['phone1']) && $user->phone1) {
199 $node = new core_user\output\myprofile\node('contact', 'phone1', get_string('phone1'), null, null, $user->phone1);
200 $tree->add_node($node);
203 if (isset($identityfields['phone2']) && $user->phone2) {
204 $node = new core_user\output\myprofile\node('contact', 'phone2', get_string('phone2'), null, null, $user->phone2);
205 $tree->add_node($node);
208 if (isset($identityfields['institution']) && $user->institution) {
209 $node = new core_user\output\myprofile\node('contact', 'institution', get_string('institution'), null, null,
210 $user->institution);
211 $tree->add_node($node);
214 if (isset($identityfields['department']) && $user->department) {
215 $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null,
216 $user->department);
217 $tree->add_node($node);
220 if (isset($identityfields['idnumber']) && $user->idnumber) {
221 $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null,
222 s($user->idnumber));
223 $tree->add_node($node);
226 // Printing tagged interests. We want this only for full profile.
227 if (empty($course) && ($interests = core_tag_tag::get_item_tags('core', 'user', $user->id))) {
228 $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null,
229 $OUTPUT->tag_list($interests, ''));
230 $tree->add_node($node);
233 if ($iscurrentuser || !isset($hiddenfields['mycourses'])) {
234 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
235 if ($mycourses = enrol_get_all_users_courses($user->id, true, null)) {
236 $shown = 0;
237 $courselisting = html_writer::start_tag('ul');
238 foreach ($mycourses as $mycourse) {
239 if ($mycourse->category) {
240 context_helper::preload_from_record($mycourse);
241 $ccontext = context_course::instance($mycourse->id);
242 if (!isset($course) || $mycourse->id != $course->id) {
243 $linkattributes = null;
244 if ($mycourse->visible == 0) {
245 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
246 continue;
248 $linkattributes['class'] = 'dimmed';
250 $params = array('id' => $user->id, 'course' => $mycourse->id);
251 if ($showallcourses) {
252 $params['showallcourses'] = 1;
254 $url = new moodle_url('/user/view.php', $params);
255 $courselisting .= html_writer::tag('li', html_writer::link($url, $ccontext->get_context_name(false),
256 $linkattributes));
257 } else {
258 $courselisting .= html_writer::tag('li', $ccontext->get_context_name(false));
261 $shown++;
262 if (!$showallcourses && $shown == $CFG->navcourselimit) {
263 $url = null;
264 if (isset($course)) {
265 $url = new moodle_url('/user/view.php',
266 array('id' => $user->id, 'course' => $course->id, 'showallcourses' => 1));
267 } else {
268 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
270 $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'),
271 array('title' => get_string('viewmore'))), array('class' => 'viewmore'));
272 break;
275 $courselisting .= html_writer::end_tag('ul');
276 if (!empty($mycourses)) {
277 // Add this node only if there are courses to display.
278 $node = new core_user\output\myprofile\node('coursedetails', 'courseprofiles',
279 get_string('courseprofiles'), null, null, rtrim($courselisting, ', '));
280 $tree->add_node($node);
285 if (!empty($course)) {
287 // Show roles in this course.
288 if ($rolestring = get_user_roles_in_course($user->id, $course->id)) {
289 $node = new core_user\output\myprofile\node('coursedetails', 'roles', get_string('roles'), null, null, $rolestring);
290 $tree->add_node($node);
293 // Show groups this user is in.
294 if (!isset($hiddenfields['groups']) && !empty($course)) {
295 $accessallgroups = has_capability('moodle/site:accessallgroups', $courseorsystemcontext);
296 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
297 $groupstr = '';
298 foreach ($usergroups as $group) {
299 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
300 // In separate groups mode, I only have to see the groups shared between both users.
301 if (!groups_is_member($group->id, $USER->id)) {
302 continue;
306 if ($course->groupmode != NOGROUPS) {
307 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'
308 .format_string($group->name).'</a>,';
309 } else {
310 // The user/index.php shows groups only when course in group mode.
311 $groupstr .= ' '.format_string($group->name);
314 if ($groupstr !== '') {
315 $node = new core_user\output\myprofile\node('coursedetails', 'groups',
316 get_string('group'), null, null, rtrim($groupstr, ', '));
317 $tree->add_node($node);
322 if (!isset($hiddenfields['suspended'])) {
323 if ($user->suspended) {
324 $node = new core_user\output\myprofile\node('coursedetails', 'suspended',
325 null, null, null, get_string('suspended', 'auth'));
326 $tree->add_node($node);
331 $categories = profile_get_user_fields_with_data_by_category($user->id);
332 foreach ($categories as $categoryid => $fields) {
333 foreach ($fields as $formfield) {
334 if ($formfield->show_field_content()) {
335 $node = new core_user\output\myprofile\node('contact', 'custom_field_' . $formfield->field->shortname,
336 format_string($formfield->field->name), null, null, $formfield->display_data());
337 $tree->add_node($node);
342 // First access. (Why only for sites ?)
343 if (!isset($hiddenfields['firstaccess']) && empty($course)) {
344 if ($user->firstaccess) {
345 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
346 } else {
347 $datestring = get_string("never");
349 $node = new core_user\output\myprofile\node('loginactivity', 'firstaccess', get_string('firstsiteaccess'), null, null,
350 $datestring);
351 $tree->add_node($node);
354 // Last access.
355 if (!isset($hiddenfields['lastaccess'])) {
356 if (empty($course)) {
357 $string = get_string('lastsiteaccess');
358 if ($user->lastaccess) {
359 $datestring = userdate($user->lastaccess) . "&nbsp; (" . format_time(time() - $user->lastaccess) . ")";
360 } else {
361 $datestring = get_string("never");
363 } else {
364 $string = get_string('lastcourseaccess');
365 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
366 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
367 } else {
368 $datestring = get_string("never");
372 $node = new core_user\output\myprofile\node('loginactivity', 'lastaccess', $string, null, null,
373 $datestring);
374 $tree->add_node($node);
377 // Last ip.
378 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
379 if ($user->lastip) {
380 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $user->id));
381 $ipstring = html_writer::link($iplookupurl, $user->lastip);
382 } else {
383 $ipstring = get_string("none");
385 $node = new core_user\output\myprofile\node('loginactivity', 'lastip', get_string('lastip'), null, null,
386 $ipstring);
387 $tree->add_node($node);