2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Defines core nodes for my profile navigation tree.
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();
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
37 function core_myprofile_navigation(core_user\output\myprofile\tree
$tree, $user, $iscurrentuser, $course) {
38 global $CFG, $USER, $DB, $PAGE, $OUTPUT;
40 $usercontext = context_user
::instance($user->id
, MUST_EXIST
);
41 $systemcontext = context_system
::instance();
42 $courseorusercontext = !empty($course) ? context_course
::instance($course->id
) : $usercontext;
43 $courseorsystemcontext = !empty($course) ? context_course
::instance($course->id
) : $systemcontext;
44 $courseid = !empty($course) ?
$course->id
: SITEID
;
46 $contactcategory = new core_user\output\myprofile\
category('contact', get_string('userdetails'));
47 // No after property specified intentionally. It is a hack to make administration block appear towards the end. Refer MDL-49928.
48 $coursedetailscategory = new core_user\output\myprofile\
category('coursedetails', get_string('coursedetails'));
49 $miscategory = new core_user\output\myprofile\
category('miscellaneous', get_string('miscellaneous'), 'coursedetails');
50 $reportcategory = new core_user\output\myprofile\
category('reports', get_string('reports'), 'miscellaneous');
51 $admincategory = new core_user\output\myprofile\
category('administration', get_string('administration'), 'reports');
52 $loginactivitycategory = new core_user\output\myprofile\
category('loginactivity', get_string('loginactivity'), 'administration');
55 $tree->add_category($contactcategory);
56 $tree->add_category($coursedetailscategory);
57 $tree->add_category($miscategory);
58 $tree->add_category($reportcategory);
59 $tree->add_category($admincategory);
60 $tree->add_category($loginactivitycategory);
64 if (!empty($course)) {
65 if (user_can_view_profile($user, null, $usercontext)) {
66 $url = new moodle_url('/user/profile.php', array('id' => $user->id
));
67 $node = new core_user\output\myprofile\node
('miscellaneous', 'fullprofile', get_string('fullprofile'), null, $url);
68 $tree->add_node($node);
73 if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) {
74 if (($iscurrentuser ||
is_siteadmin($USER) ||
!is_siteadmin($user)) && has_capability('moodle/user:update',
76 $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id
, 'course' => $courseid,
77 'returnto' => 'profile'));
78 $node = new core_user\output\myprofile\node
('contact', 'editprofile', get_string('editmyprofile'), null, $url,
79 null, null, 'editprofile');
80 $tree->add_node($node);
81 } else if ((has_capability('moodle/user:editprofile', $usercontext) && !is_siteadmin($user))
82 ||
($iscurrentuser && has_capability('moodle/user:editownprofile', $systemcontext))) {
83 $userauthplugin = false;
84 if (!empty($user->auth
)) {
85 $userauthplugin = get_auth_plugin($user->auth
);
87 if ($userauthplugin && $userauthplugin->can_edit_profile()) {
88 $url = $userauthplugin->edit_profile_url();
91 $url = new moodle_url('/user/edit.php', array('id' => $user->id
, 'returnto' => 'profile'));
93 $url = new moodle_url('/user/edit.php', array('id' => $user->id
, 'course' => $course->id
,
94 'returnto' => 'profile'));
97 $node = new core_user\output\myprofile\node
('contact', 'editprofile',
98 get_string('editmyprofile'), null, $url, null, null, 'editprofile');
99 $tree->add_node($node);
105 if (!$iscurrentuser && $PAGE->settingsnav
->can_view_user_preferences($user->id
)) {
106 $url = new moodle_url('/user/preferences.php', array('userid' => $user->id
));
107 $title = get_string('preferences', 'moodle');
108 $node = new core_user\output\myprofile\node
('administration', 'preferences', $title, null, $url);
109 $tree->add_node($node);
113 if (!$user->deleted
&& !$iscurrentuser &&
114 !\core\session\manager
::is_loggedinas() && has_capability('moodle/user:loginas',
115 $courseorsystemcontext) && !is_siteadmin($user->id
)) {
116 $url = new moodle_url('/course/loginas.php',
117 array('id' => $courseid, 'user' => $user->id
, 'sesskey' => sesskey()));
118 $node = new core_user\output\myprofile\node
('administration', 'loginas', get_string('loginas'), null, $url);
119 $tree->add_node($node);
123 if (has_capability('moodle/user:viewhiddendetails', $courseorusercontext)) {
124 $hiddenfields = array();
126 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields
));
128 // TODO Does not support custom user profile fields (MDL-70456).
129 $identityfields = array_flip(\core_user\fields
::get_identity_fields($courseorusercontext, false));
131 if (is_mnet_remote_user($user)) {
132 $sql = "SELECT h.id, h.name, h.wwwroot,
133 a.name as application, a.display_name
134 FROM {mnet_host} h, {mnet_application} a
135 WHERE h.id = ? AND h.applicationid = a.id";
137 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid
));
138 $remoteuser = new stdclass();
139 $remoteuser->remotetype
= $remotehost->display_name
;
140 $hostinfo = new stdclass();
141 $hostinfo->remotename
= $remotehost->name
;
142 $hostinfo->remoteurl
= $remotehost->wwwroot
;
144 $node = new core_user\output\myprofile\node
('contact', 'mnet', get_string('remoteuser', 'mnet', $remoteuser), null, null,
145 get_string('remoteuserinfo', 'mnet', $hostinfo), null, 'remoteuserinfo');
146 $tree->add_node($node);
150 or (!isset($hiddenfields['email']) and (
151 $user->maildisplay
== core_user
::MAILDISPLAY_EVERYONE
152 or ($user->maildisplay
== core_user
::MAILDISPLAY_COURSE_MEMBERS_ONLY
and enrol_sharing_course($user, $USER))
153 or has_capability('moodle/course:useremail', $courseorusercontext) // TODO: Deprecate/remove for MDL-37479.
155 or (isset($identityfields['email']))
157 $maildisplay = obfuscate_mailto($user->email
, '');
158 if ($iscurrentuser) {
159 if ($user->maildisplay
== core_user
::MAILDISPLAY_EVERYONE
) {
160 $maildisplay .= ' ' . get_string('emaildisplayeveryone');
161 } else if ($user->maildisplay
== core_user
::MAILDISPLAY_COURSE_MEMBERS_ONLY
) {
162 $maildisplay .= ' ' . get_string('emaildisplaycoursemembersonly');
164 $maildisplay .= ' ' . get_string('emaildisplayhide');
167 $node = new core_user\output\myprofile\node
('contact', 'email', get_string('email'),
168 null, null, $maildisplay);
169 $tree->add_node($node);
172 if (!isset($hiddenfields['moodlenetprofile']) && $user->moodlenetprofile
) {
173 $node = new core_user\output\myprofile\node
('contact', 'moodlenetprofile', get_string('moodlenetprofile', 'user'), null,
174 null, $user->moodlenetprofile
);
175 $tree->add_node($node);
178 if (!isset($hiddenfields['country']) && $user->country
) {
179 $node = new core_user\output\myprofile\node
('contact', 'country', get_string('country'), null, null,
180 get_string($user->country
, 'countries'));
181 $tree->add_node($node);
184 if (!isset($hiddenfields['city']) && $user->city
) {
185 $node = new core_user\output\myprofile\node
('contact', 'city', get_string('city'), null, null, $user->city
);
186 $tree->add_node($node);
189 if (!isset($hiddenfields['timezone'])) {
190 $node = new core_user\output\myprofile\node
('contact', 'timezone', get_string('timezone'), null, null,
191 core_date
::get_user_timezone($user));
192 $tree->add_node($node);
195 if (isset($identityfields['address']) && $user->address
) {
196 $node = new core_user\output\myprofile\node
('contact', 'address', get_string('address'), null, null, $user->address
);
197 $tree->add_node($node);
200 if (isset($identityfields['phone1']) && $user->phone1
) {
201 $node = new core_user\output\myprofile\node
('contact', 'phone1', get_string('phone1'), null, null, $user->phone1
);
202 $tree->add_node($node);
205 if (isset($identityfields['phone2']) && $user->phone2
) {
206 $node = new core_user\output\myprofile\node
('contact', 'phone2', get_string('phone2'), null, null, $user->phone2
);
207 $tree->add_node($node);
210 if (isset($identityfields['institution']) && $user->institution
) {
211 $node = new core_user\output\myprofile\node
('contact', 'institution', get_string('institution'), null, null,
213 $tree->add_node($node);
216 if (isset($identityfields['department']) && $user->department
) {
217 $node = new core_user\output\myprofile\node
('contact', 'department', get_string('department'), null, null,
219 $tree->add_node($node);
222 if (isset($identityfields['idnumber']) && $user->idnumber
) {
223 $node = new core_user\output\myprofile\node
('contact', 'idnumber', get_string('idnumber'), null, null,
225 $tree->add_node($node);
228 // Printing tagged interests. We want this only for full profile.
229 if (empty($course) && ($interests = core_tag_tag
::get_item_tags('core', 'user', $user->id
))) {
230 $node = new core_user\output\myprofile\node
('contact', 'interests', get_string('interests'), null, null,
231 $OUTPUT->tag_list($interests, ''));
232 $tree->add_node($node);
235 if ($iscurrentuser ||
!isset($hiddenfields['mycourses'])) {
236 $showallcourses = optional_param('showallcourses', 0, PARAM_INT
);
237 if ($mycourses = enrol_get_all_users_courses($user->id
, true, null)) {
239 $courselisting = html_writer
::start_tag('ul');
240 foreach ($mycourses as $mycourse) {
241 if ($mycourse->category
) {
242 context_helper
::preload_from_record($mycourse);
243 $ccontext = context_course
::instance($mycourse->id
);
244 if (!isset($course) ||
$mycourse->id
!= $course->id
) {
245 $linkattributes = null;
246 if ($mycourse->visible
== 0) {
247 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
250 $linkattributes['class'] = 'dimmed';
252 $params = array('id' => $user->id
, 'course' => $mycourse->id
);
253 if ($showallcourses) {
254 $params['showallcourses'] = 1;
256 $url = new moodle_url('/user/view.php', $params);
257 $courselisting .= html_writer
::tag('li', html_writer
::link($url, $ccontext->get_context_name(false),
260 $courselisting .= html_writer
::tag('li', $ccontext->get_context_name(false));
264 if (!$showallcourses && $shown == $CFG->navcourselimit
) {
266 if (isset($course)) {
267 $url = new moodle_url('/user/view.php',
268 array('id' => $user->id
, 'course' => $course->id
, 'showallcourses' => 1));
270 $url = new moodle_url('/user/profile.php', array('id' => $user->id
, 'showallcourses' => 1));
272 $courselisting .= html_writer
::tag('li', html_writer
::link($url, get_string('viewmore'),
273 array('title' => get_string('viewmore'))), array('class' => 'viewmore'));
277 $courselisting .= html_writer
::end_tag('ul');
278 if (!empty($mycourses)) {
279 // Add this node only if there are courses to display.
280 $node = new core_user\output\myprofile\node
('coursedetails', 'courseprofiles',
281 get_string('courseprofiles'), null, null, rtrim($courselisting, ', '));
282 $tree->add_node($node);
287 if (!empty($course)) {
289 // Show roles in this course.
290 if ($rolestring = get_user_roles_in_course($user->id
, $course->id
)) {
291 $node = new core_user\output\myprofile\node
('coursedetails', 'roles', get_string('roles'), null, null, $rolestring);
292 $tree->add_node($node);
295 // Show groups this user is in.
296 if (!isset($hiddenfields['groups']) && !empty($course)) {
297 $accessallgroups = has_capability('moodle/site:accessallgroups', $courseorsystemcontext);
298 if ($usergroups = groups_get_all_groups($course->id
, $user->id
)) {
300 foreach ($usergroups as $group) {
301 if ($course->groupmode
== SEPARATEGROUPS
and !$accessallgroups and $user->id
!= $USER->id
) {
302 // In separate groups mode, I only have to see the groups shared between both users.
303 if (!groups_is_member($group->id
, $USER->id
)) {
308 if ($course->groupmode
!= NOGROUPS
) {
309 $groupstr .= ' <a href="'.$CFG->wwwroot
.'/user/index.php?id='.$course->id
.'&group='.$group->id
.'">'
310 .format_string($group->name
).'</a>,';
312 // The user/index.php shows groups only when course in group mode.
313 $groupstr .= ' '.format_string($group->name
);
316 if ($groupstr !== '') {
317 $node = new core_user\output\myprofile\node
('coursedetails', 'groups',
318 get_string('group'), null, null, rtrim($groupstr, ', '));
319 $tree->add_node($node);
324 if (!isset($hiddenfields['suspended'])) {
325 if ($user->suspended
) {
326 $node = new core_user\output\myprofile\node
('coursedetails', 'suspended',
327 null, null, null, get_string('suspended', 'auth'));
328 $tree->add_node($node);
333 $categories = profile_get_user_fields_with_data_by_category($user->id
);
334 foreach ($categories as $categoryid => $fields) {
335 foreach ($fields as $formfield) {
336 if ($formfield->is_visible() and !$formfield->is_empty()) {
337 $node = new core_user\output\myprofile\node
('contact', 'custom_field_' . $formfield->field
->shortname
,
338 format_string($formfield->field
->name
), null, null, $formfield->display_data());
339 $tree->add_node($node);
344 // First access. (Why only for sites ?)
345 if (!isset($hiddenfields['firstaccess']) && empty($course)) {
346 if ($user->firstaccess
) {
347 $datestring = userdate($user->firstaccess
)." (".format_time(time() - $user->firstaccess
).")";
349 $datestring = get_string("never");
351 $node = new core_user\output\myprofile\node
('loginactivity', 'firstaccess', get_string('firstsiteaccess'), null, null,
353 $tree->add_node($node);
357 if (!isset($hiddenfields['lastaccess'])) {
358 if (empty($course)) {
359 $string = get_string('lastsiteaccess');
360 if ($user->lastaccess
) {
361 $datestring = userdate($user->lastaccess
) . " (" . format_time(time() - $user->lastaccess
) . ")";
363 $datestring = get_string("never");
366 $string = get_string('lastcourseaccess');
367 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id
, 'courseid' => $course->id
))) {
368 $datestring = userdate($lastaccess->timeaccess
)." (".format_time(time() - $lastaccess->timeaccess
).")";
370 $datestring = get_string("never");
374 $node = new core_user\output\myprofile\node
('loginactivity', 'lastaccess', $string, null, null,
376 $tree->add_node($node);
380 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
382 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip
, 'user' => $user->id
));
383 $ipstring = html_writer
::link($iplookupurl, $user->lastip
);
385 $ipstring = get_string("none");
387 $node = new core_user\output\myprofile\node
('loginactivity', 'lastip', get_string('lastip'), null, null,
389 $tree->add_node($node);