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('contact', '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('contact', 'editprofile',
93 get_string('editmyprofile'), null, $url);
94 $tree->add_node($node);
99 // Preference page. Only visible by administrators.
100 if (is_siteadmin()) {
101 $url = new moodle_url('/user/preferences.php', array('userid' => $user->id));
102 $title = $iscurrentuser ? get_string('mypreferences') : get_string('userspreferences', 'moodle', fullname($user));
103 $node = new core_user\output\myprofile\node('administration', 'preferences', $title, null, $url);
104 $tree->add_node($node);
108 if (!$user->deleted && !$iscurrentuser &&
109 !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas',
110 $context) && !is_siteadmin($user->id)) {
111 $url = new moodle_url('/course/loginas.php',
112 array('id' => $courseid, 'user' => $user->id, 'sesskey' => sesskey()));
113 $node = new core_user\output\myprofile\node('administration', 'loginas', get_string('loginas'), null, $url);
114 $tree->add_node($node);
118 if (has_capability('moodle/user:viewhiddendetails', $usercontext)) {
119 $hiddenfields = array();
121 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
123 if (has_capability('moodle/site:viewuseridentity', $context)) {
124 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
126 $identityfields = array();
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 // Printing tagged interests. We want this only for full profile.
148 if (!empty($CFG->usetags) && empty($course)) {
149 if ($interests = tag_get_tags_csv('user', $user->id) ) {
150 $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null, $interests);
151 $tree->add_node($node);
155 if (!isset($hiddenfields['country']) && $user->country) {
156 $node = new core_user\output\myprofile\node('contact', 'country', get_string('country'), null, null,
157 get_string($user->country, 'countries'));
158 $tree->add_node($node);
161 if (!isset($hiddenfields['city']) && $user->city) {
162 $node = new core_user\output\myprofile\node('contact', 'city', get_string('city'), null, null, $user->city);
163 $tree->add_node($node);
166 if (isset($identityfields['address']) && $user->address) {
167 $node = new core_user\output\myprofile\node('contact', 'address', get_string('address'), null, null, $user->address);
168 $tree->add_node($node);
171 if (isset($identityfields['phone1']) && $user->phone1) {
172 $node = new core_user\output\myprofile\node('contact', 'phone1', get_string('phone'), null, null, $user->phone1);
173 $tree->add_node($node);
176 if (isset($identityfields['phone2']) && $user->phone2) {
177 $node = new core_user\output\myprofile\node('contact', 'phone2', get_string('phone2'), null, null, $user->phone2);
178 $tree->add_node($node);
181 if (isset($identityfields['institution']) && $user->institution) {
182 $node = new core_user\output\myprofile\node('contact', 'institution', get_string('institution'), null, null,
184 $tree->add_node($node);
187 if (isset($identityfields['department']) && $user->department) {
188 $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null,
190 $tree->add_node($node);
193 if (isset($identityfields['idnumber']) && $user->idnumber) {
194 $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null,
196 $tree->add_node($node);
199 if (isset($identityfields['email']) and ($iscurrentuser
200 or $user->maildisplay == 1
201 or has_capability('moodle/course:useremail', $usercontext)
202 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
203 $node = new core_user\output\myprofile\node('contact', 'email', get_string('email'), null, null,
204 obfuscate_mailto($user->email, ''));
205 $tree->add_node($node);
208 if ($user->url && !isset($hiddenfields['webpage'])) {
210 if (strpos($user->url, '://') === false) {
211 $url = 'http://'. $url;
213 $webpageurl = new moodle_url($url);
214 $node = new core_user\output\myprofile\node('contact', 'webpage', get_string('webpage'), null, $webpageurl);
215 $tree->add_node($node);
218 if (!isset($hiddenfields['mycourses'])) {
219 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
220 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) {
222 $courselisting = html_writer::start_tag('ul');
223 foreach ($mycourses as $mycourse) {
224 if ($mycourse->category) {
225 context_helper::preload_from_record($mycourse);
226 $ccontext = context_course::instance($mycourse->id);
227 if (!isset($course) || $mycourse->id != $course->id) {
228 $linkattributes = null;
229 if ($mycourse->visible == 0) {
230 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
233 $linkattributes['class'] = 'dimmed';
235 $params = array('id' => $user->id, 'course' => $mycourse->id);
236 if ($showallcourses) {
237 $params['showallcourses'] = 1;
239 $url = new moodle_url('/user/view.php', $params);
240 $courselisting .= html_writer::tag('li', html_writer::link($url, $ccontext->get_context_name(false),
243 $courselisting .= html_writer::tag('li', $course->fullname);
247 if (!$showallcourses && $shown == $CFG->navcourselimit) {
249 if (isset($course)) {
250 $url = new moodle_url('/user/view.php',
251 array('id' => $user->id, 'course' => $course->id, 'showallcourses' => 1));
253 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
255 $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'),
256 array('title' => get_string('viewmore'))));
260 $courselisting .= html_writer::end_tag('ul');
261 if (!empty($mycourses)) {
262 // Add this node only if there are courses to display.
263 $node = new core_user\output\myprofile\node('coursedetails', 'courseprofiles',
264 get_string('courseprofiles'), null, null, rtrim($courselisting, ', '));
265 $tree->add_node($node);
270 if (!empty($course)) {
272 // Show roles in this course.
273 if ($rolestring = get_user_roles_in_course($user->id, $course->id)) {
274 $node = new core_user\output\myprofile\node('coursedetails', 'roles', get_string('roles'), null, null, $rolestring);
275 $tree->add_node($node);
278 // Show groups this user is in.
279 if (!isset($hiddenfields['groups']) && !empty($course)) {
280 $accessallgroups = has_capability('moodle/site:accessallgroups', $context);
281 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
283 foreach ($usergroups as $group) {
284 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
285 if (!groups_is_member($group->id, $user->id)) {
290 if ($course->groupmode != NOGROUPS) {
291 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&group='.$group->id.'">'
292 .format_string($group->name).'</a>,';
294 // The user/index.php shows groups only when course in group mode.
295 $groupstr .= ' '.format_string($group->name);
298 if ($groupstr !== '') {
299 $node = new core_user\output\myprofile\node('coursedetails', 'groups',
300 get_string('group'), null, null, rtrim($groupstr, ', '));
301 $tree->add_node($node);
306 if (!isset($hiddenfields['suspended'])) {
307 if ($user->suspended) {
308 $node = new core_user\output\myprofile\node('coursedetails', 'suspended',
309 null, null, null, get_string('suspended', 'auth'));
310 $tree->add_node($node);
314 echo html_writer::end_tag('dl');
317 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
318 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin' => $user->icq) );
319 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5'));
320 $statusicon = html_writer::tag('img', '',
321 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
322 $node = new core_user\output\myprofile\node('contact', 'icqnumber', get_string('icqnumber'), null, null,
323 html_writer::link($imurl, s($user->icq) . $statusicon));
324 $tree->add_node($node);
327 if ($user->skype && !isset($hiddenfields['skypeid'])) {
328 $imurl = 'skype:'.urlencode($user->skype).'?call';
329 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.urlencode($user->skype));
331 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
334 $statusicon = html_writer::empty_tag('img',
335 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
338 $node = new core_user\output\myprofile\node('contact', 'skypeid', get_string('skypeid'), null, null,
339 html_writer::link($imurl, s($user->skype) . $statusicon));
340 $tree->add_node($node);
342 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
343 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg'));
344 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0'));
345 $statusicon = html_writer::tag('img', '',
346 array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status')));
348 $node = new core_user\output\myprofile\node('contact', 'yahooid', get_string('yahooid'), null, null,
349 html_writer::link($imurl, s($user->yahoo) . $statusicon));
350 $tree->add_node($node);
352 if ($user->aim && !isset($hiddenfields['aimid'])) {
353 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
354 $node = new core_user\output\myprofile\node('contact', 'aimid', get_string('aimid'), null, null,
355 html_writer::link($imurl, s($user->aim)));
356 $tree->add_node($node);
358 if ($user->msn && !isset($hiddenfields['msnid'])) {
359 $node = new core_user\output\myprofile\node('contact', 'msnid', get_string('msnid'), null, null,
361 $tree->add_node($node);
364 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
365 foreach ($categories as $category) {
366 if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
367 foreach ($fields as $field) {
368 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
369 $newfield = 'profile_field_'.$field->datatype;
370 $formfield = new $newfield($field->id, $user->id);
371 if ($formfield->is_visible() and !$formfield->is_empty()) {
372 $node = new core_user\output\myprofile\node('contact', $formfield->field->shortname,
373 format_string($formfield->field->name), null, null, $formfield->display_data());
374 $tree->add_node($node);
381 // First access. (Why only for sites ?)
382 if (!isset($hiddenfields['firstaccess']) && empty($course)) {
383 if ($user->firstaccess) {
384 $datestring = userdate($user->firstaccess)." (".format_time(time() - $user->firstaccess).")";
386 $datestring = get_string("never");
388 $node = new core_user\output\myprofile\node('miscellaneous', 'firstaccess', get_string('firstsiteaccess'), null, null,
390 $tree->add_node($node);
394 if (!isset($hiddenfields['lastaccess'])) {
395 if (empty($course)) {
396 $string = get_string('lastsiteaccess');
397 if ($user->lastaccess) {
398 $datestring = userdate($user->lastaccess) . " (" . format_time(time() - $user->lastaccess) . ")";
400 $datestring = get_string("never");
403 $string = get_string('lastcourseaccess');
404 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
405 $datestring = userdate($lastaccess->timeaccess)." (".format_time(time() - $lastaccess->timeaccess).")";
407 $datestring = get_string("never");
411 $node = new core_user\output\myprofile\node('miscellaneous', 'lastaccess', $string, null, null,
413 $tree->add_node($node);
417 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
419 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $USER->id));
420 $ipstring = html_writer::link($iplookupurl, $user->lastip);
422 $ipstring = get_string("none");
424 $node = new core_user\output\myprofile\node('miscellaneous', 'lastip', get_string('lastip'), null, null,
426 $tree->add_node($node);