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;
40 $usercontext = context_user::instance($user->id, MUST_EXIST);
41 $systemcontext = context_system::instance();
42 $context = !empty($course) ? context_course::instance($course->id) : $systemcontext;
43 $courseid = !empty($course) ? $course->id : SITEID;
45 $contactcategory = new core_user\output\myprofile\category('contact', get_string('userdetails'));
46 $miscategory = new core_user\output\myprofile\category('miscellaneous', get_string('miscellaneous'));
47 $reportcategory = new core_user\output\myprofile\category('reports', get_string('reports'), 'miscellaneous');
48 $admincategory = new core_user\output\myprofile\category('administration', get_string('administration'), 'miscellaneous');
49 $coursedetailscategory = new core_user\output\myprofile\category('coursedetails', get_string('coursedetails'), 'contact');
52 $tree->add_category($contactcategory);
53 $tree->add_category($miscategory);
54 $tree->add_category($reportcategory);
55 $tree->add_category($admincategory);
56 $tree->add_category($coursedetailscategory);
60 if (!empty($course)) {
61 if (empty($CFG->forceloginforprofiles) || $iscurrentuser ||
62 has_capability('moodle/user:viewdetails', context_user::instance($user->id))
63 || has_coursecontact_role($user->id)) {
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);
71 if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) {
72 if (($iscurrentuser || is_siteadmin($USER) || !is_siteadmin($user)) && has_capability('moodle/user:update',
74 $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $courseid));
75 $node = new core_user\output\myprofile\node('administration', 'editprofile', get_string('editmyprofile'), null, $url);
76 $tree->add_node($node);
77 } else if ((has_capability('moodle/user:editprofile', $usercontext) && !is_siteadmin($user))
78 || ($iscurrentuser && has_capability('moodle/user:editownprofile', $systemcontext))) {
79 $userauthplugin = false;
80 if (!empty($user->auth)) {
81 $userauthplugin = get_auth_plugin($user->auth);
83 if ($userauthplugin && $userauthplugin->can_edit_profile()) {
84 $url = $userauthplugin->edit_profile_url();
87 $url = new moodle_url('/user/edit.php', array('userid' => $user->id));
89 $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'course' => $course->id));
92 $node = new core_user\output\myprofile\node('administration', 'editprofile',
93 get_string('editmyprofile'), null, $url);
94 $tree->add_node($node);
99 if ($iscurrentuser || is_siteadmin()) {
100 $url = new moodle_url('/user/preferences.php', array('userid' => $user->id));
101 $title = $iscurrentuser ? get_string('mypreferences') : get_string('userspreferences', 'moodle', fullname($user));
102 $node = new core_user\output\myprofile\node('administration', 'preferences', $title, null, $url);
103 $tree->add_node($node);
107 if (!$user->deleted && !$iscurrentuser &&
108 !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas',
109 $context) && !is_siteadmin($user->id)) {
110 $url = new moodle_url('/course/loginas.php',
111 array('id' => $courseid, 'user' => $user->id, 'sesskey' => sesskey()));
112 $node = new core_user\output\myprofile\node('administration', 'loginas', get_string('loginas'), null, $url);
113 $tree->add_node($node);
117 if (has_capability('moodle/user:viewhiddendetails', $usercontext)) {
118 $hiddenfields = array();
120 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
122 if (has_capability('moodle/site:viewuseridentity', $context)) {
123 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
125 $identityfields = array();
128 if (is_mnet_remote_user($user)) {
129 $sql = "SELECT h.id, h.name, h.wwwroot,
130 a.name as application, a.display_name
131 FROM {mnet_host} h, {mnet_application} a
132 WHERE h.id = ? AND h.applicationid = a.id";
134 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
135 $remoteuser = new stdclass();
136 $remoteuser->remotetype = $remotehost->display_name;
137 $hostinfo = new stdclass();
138 $hostinfo->remotename = $remotehost->name;
139 $hostinfo->remoteurl = $remotehost->wwwroot;
141 $node = new core_user\output\myprofile\node('contact', 'mnet', get_string('remoteuser', 'mnet', $remoteuser), null, null,
142 get_string('remoteuserinfo', 'mnet', $hostinfo), null, 'remoteuserinfo');
143 $tree->add_node($node);
146 // Printing tagged interests. We want this only for full profile.
147 if (!empty($CFG->usetags) && empty($course)) {
148 if ($interests = tag_get_tags_csv('user', $user->id) ) {
149 $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null, $interests);
150 $tree->add_node($node);
154 if (!isset($hiddenfields['country']) && $user->country) {
155 $node = new core_user\output\myprofile\node('contact', 'country', get_string('country'), null, null,
156 get_string($user->country, 'countries'));
157 $tree->add_node($node);
160 if (!isset($hiddenfields['city']) && $user->city) {
161 $node = new core_user\output\myprofile\node('contact', 'city', get_string('city'), null, null, $user->city);
162 $tree->add_node($node);
165 if (isset($identityfields['address']) && $user->address) {
166 $node = new core_user\output\myprofile\node('contact', 'address', get_string('address'), null, null, $user->address);
167 $tree->add_node($node);
170 if (isset($identityfields['phone1']) && $user->phone1) {
171 $node = new core_user\output\myprofile\node('contact', 'phone1', get_string('phone'), null, null, $user->phone1);
172 $tree->add_node($node);
175 if (isset($identityfields['phone2']) && $user->phone2) {
176 $node = new core_user\output\myprofile\node('contact', 'phone2', get_string('phone2'), null, null, $user->phone2);
177 $tree->add_node($node);
180 if (isset($identityfields['institution']) && $user->institution) {
181 $node = new core_user\output\myprofile\node('contact', 'institution', get_string('institution'), null, null,
183 $tree->add_node($node);
186 if (isset($identityfields['department']) && $user->department) {
187 $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null,
189 $tree->add_node($node);
192 if (isset($identityfields['idnumber']) && $user->idnumber) {
193 $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null,
195 $tree->add_node($node);
198 if (isset($identityfields['email']) and ($iscurrentuser
199 or $user->maildisplay == 1
200 or has_capability('moodle/course:useremail', $usercontext)
201 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
202 $node = new core_user\output\myprofile\node('contact', 'email', get_string('email'), null, null,
203 obfuscate_mailto($user->email, ''));
204 $tree->add_node($node);
207 if ($user->url && !isset($hiddenfields['webpage'])) {
209 if (strpos($user->url, '://') === false) {
210 $url = 'http://'. $url;
212 $webpageurl = new moodle_url($url);
213 $node = new core_user\output\myprofile\node('contact', 'webpage', get_string('webpage'), null, $webpageurl);
214 $tree->add_node($node);
217 if (!isset($hiddenfields['mycourses'])) {
218 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
219 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) {
221 $courselisting = html_writer::start_tag('ul');
222 foreach ($mycourses as $mycourse) {
223 if ($mycourse->category) {
224 context_helper::preload_from_record($mycourse);
225 $ccontext = context_course::instance($mycourse->id);
226 if (!isset($course) || $mycourse->id != $course->id) {
227 $linkattributes = null;
228 if ($mycourse->visible == 0) {
229 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
232 $linkattributes['class'] = 'dimmed';
234 $params = array('id' => $user->id, 'course' => $mycourse->id);
235 if ($showallcourses) {
236 $params['showallcourses'] = 1;
238 $url = new moodle_url('/user/view.php', $params);
239 $courselisting .= html_writer::tag('li', html_writer::link($url, $ccontext->get_context_name(false),
242 $courselisting .= html_writer::tag('li', $course->fullname);
246 if (!$showallcourses && $shown == $CFG->navcourselimit) {
248 if (isset($course)) {
249 $url = new moodle_url('/user/view.php',
250 array('id' => $user->id, 'course' => $course->id, 'showallcourses' => 1));
252 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
254 $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'),
255 array('title' => get_string('viewmore'))));
259 $courselisting .= html_writer::end_tag('ul');
260 if (!empty($mycourses)) {
261 // Add this node only if there are courses to display.
262 $node = new core_user\output\myprofile\node('coursedetails', 'courseprofiles',
263 get_string('courseprofiles'), null, null, rtrim($courselisting, ', '));
264 $tree->add_node($node);
269 if (!empty($course)) {
271 // Show roles in this course.
272 if ($rolestring = get_user_roles_in_course($user->id, $course->id)) {
273 $node = new core_user\output\myprofile\node('coursedetails', 'roles', get_string('roles'), null, null, $rolestring);
274 $tree->add_node($node);
277 // Show groups this user is in.
278 if (!isset($hiddenfields['groups']) && !empty($course)) {
279 $accessallgroups = has_capability('moodle/site:accessallgroups', $context);
280 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
282 foreach ($usergroups as $group) {
283 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
284 if (!groups_is_member($group->id, $user->id)) {
289 if ($course->groupmode != NOGROUPS) {
290 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&group='.$group->id.'">'
291 .format_string($group->name).'</a>,';
293 // The user/index.php shows groups only when course in group mode.
294 $groupstr .= ' '.format_string($group->name);
297 if ($groupstr !== '') {
298 $node = new core_user\output\myprofile\node('coursedetails', 'groups',
299 get_string('group'), null, null, rtrim($groupstr, ', '));
300 $tree->add_node($node);
305 if (!isset($hiddenfields['suspended'])) {
306 if ($user->suspended) {
307 $node = new core_user\output\myprofile\node('coursedetails', 'suspended',
308 null, null, null, get_string('suspended', 'auth'));
309 $tree->add_node($node);
313 echo html_writer::end_tag('dl');
316 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
317 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin' => $user->icq) );
318 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5'));
319 $statusicon = html_writer::tag('img', '',
320 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
321 $node = new core_user\output\myprofile\node('contact', 'icqnumber', get_string('icqnumber'), null, null,
322 html_writer::link($imurl, s($user->icq) . $statusicon));
323 $tree->add_node($node);
326 if ($user->skype && !isset($hiddenfields['skypeid'])) {
327 $imurl = 'skype:'.urlencode($user->skype).'?call';
328 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.urlencode($user->skype));
330 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
333 $statusicon = html_writer::empty_tag('img',
334 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
337 $node = new core_user\output\myprofile\node('contact', 'skypeid', get_string('skypeid'), null, null,
338 html_writer::link($imurl, s($user->skype) . $statusicon));
339 $tree->add_node($node);
341 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
342 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg'));
343 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0'));
344 $statusicon = html_writer::tag('img', '',
345 array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status')));
347 $node = new core_user\output\myprofile\node('contact', 'yahooid', get_string('yahooid'), null, null,
348 html_writer::link($imurl, s($user->yahoo) . $statusicon));
349 $tree->add_node($node);
351 if ($user->aim && !isset($hiddenfields['aimid'])) {
352 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
353 $node = new core_user\output\myprofile\node('contact', 'aimid', get_string('aimid'), null, null,
354 html_writer::link($imurl, s($user->aim)));
355 $tree->add_node($node);
357 if ($user->msn && !isset($hiddenfields['msnid'])) {
358 $node = new core_user\output\myprofile\node('contact', 'msnid', get_string('msnid'), null, null,
360 $tree->add_node($node);
363 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
364 foreach ($categories as $category) {
365 if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
366 foreach ($fields as $field) {
367 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
368 $newfield = 'profile_field_'.$field->datatype;
369 $formfield = new $newfield($field->id, $user->id);
370 if ($formfield->is_visible() and !$formfield->is_empty()) {
371 $node = new core_user\output\myprofile\node('contact', $formfield->field->shortname,
372 format_string($formfield->field->name), null, null, $formfield->display_data());
373 $tree->add_node($node);
380 // First access. (Why only for sites ?)
381 if (!isset($hiddenfields['firstaccess']) && empty($course)) {
382 if ($user->firstaccess) {
383 $datestring = userdate($user->firstaccess)." (".format_time(time() - $user->firstaccess).")";
385 $datestring = get_string("never");
387 $node = new core_user\output\myprofile\node('miscellaneous', 'firstaccess', get_string('firstsiteaccess'), null, null,
389 $tree->add_node($node);
393 if (!isset($hiddenfields['lastaccess'])) {
394 if (empty($course)) {
395 $string = get_string('lastsiteaccess');
396 if ($user->lastaccess) {
397 $datestring = userdate($user->lastaccess) . " (" . format_time(time() - $user->lastaccess) . ")";
399 $datestring = get_string("never");
402 $string = get_string('lastcourseaccess');
403 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
404 $datestring = userdate($lastaccess->timeaccess)." (".format_time(time() - $lastaccess->timeaccess).")";
406 $datestring = get_string("never");
410 $node = new core_user\output\myprofile\node('miscellaneous', 'lastaccess', $string, null, null,
412 $tree->add_node($node);
416 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
418 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $USER->id));
419 $ipstring = html_writer::link($iplookupurl, $user->lastip);
421 $ipstring = get_string("none");
423 $node = new core_user\output\myprofile\node('miscellaneous', 'lastip', get_string('lastip'), null, null,
425 $tree->add_node($node);