Merge branch 'MDL-53674-30-eventmsg' of git://github.com/mudrd8mz/moodle into MOODLE_...
[moodle.git] / lib / myprofilelib.php
blob825753bab22a6c7c9321df4061ff6159e66aaf9b
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();
26 require_once($CFG->dirroot . '/tag/lib.php');
28 /**
29 * Defines core nodes for my profile navigation tree.
31 * @param \core_user\output\myprofile\tree $tree Tree object
32 * @param stdClass $user user object
33 * @param bool $iscurrentuser is the user viewing profile, current user ?
34 * @param stdClass $course course object
36 * @return bool
38 function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
39 global $CFG, $USER, $DB, $PAGE;
41 $usercontext = context_user::instance($user->id, MUST_EXIST);
42 $systemcontext = context_system::instance();
43 $courseorusercontext = !empty($course) ? context_course::instance($course->id) : $usercontext;
44 $courseorsystemcontext = !empty($course) ? context_course::instance($course->id) : $systemcontext;
45 $courseid = !empty($course) ? $course->id : SITEID;
47 $contactcategory = new core_user\output\myprofile\category('contact', get_string('userdetails'));
48 // No after property specified intentionally. It is a hack to make administration block appear towards the end. Refer MDL-49928.
49 $coursedetailscategory = new core_user\output\myprofile\category('coursedetails', get_string('coursedetails'));
50 $miscategory = new core_user\output\myprofile\category('miscellaneous', get_string('miscellaneous'), 'coursedetails');
51 $reportcategory = new core_user\output\myprofile\category('reports', get_string('reports'), 'miscellaneous');
52 $admincategory = new core_user\output\myprofile\category('administration', get_string('administration'), 'reports');
53 $loginactivitycategory = new core_user\output\myprofile\category('loginactivity', get_string('loginactivity'), 'administration');
55 // Add categories.
56 $tree->add_category($contactcategory);
57 $tree->add_category($coursedetailscategory);
58 $tree->add_category($miscategory);
59 $tree->add_category($reportcategory);
60 $tree->add_category($admincategory);
61 $tree->add_category($loginactivitycategory);
63 // Add core nodes.
64 // Full profile node.
65 if (!empty($course)) {
66 if (empty($CFG->forceloginforprofiles) || $iscurrentuser ||
67 has_capability('moodle/user:viewdetails', $usercontext)
68 || has_coursecontact_role($user->id)) {
69 $url = new moodle_url('/user/profile.php', array('id' => $user->id));
70 $node = new core_user\output\myprofile\node('miscellaneous', 'fullprofile', get_string('fullprofile'), null, $url);
71 $tree->add_node($node);
75 // Edit profile.
76 if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) {
77 if (($iscurrentuser || is_siteadmin($USER) || !is_siteadmin($user)) && has_capability('moodle/user:update',
78 $systemcontext)) {
79 $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $courseid,
80 'returnto' => 'profile'));
81 $node = new core_user\output\myprofile\node('contact', 'editprofile', get_string('editmyprofile'), null, $url,
82 null, null, 'editprofile');
83 $tree->add_node($node);
84 } else if ((has_capability('moodle/user:editprofile', $usercontext) && !is_siteadmin($user))
85 || ($iscurrentuser && has_capability('moodle/user:editownprofile', $systemcontext))) {
86 $userauthplugin = false;
87 if (!empty($user->auth)) {
88 $userauthplugin = get_auth_plugin($user->auth);
90 if ($userauthplugin && $userauthplugin->can_edit_profile()) {
91 $url = $userauthplugin->edit_profile_url();
92 if (empty($url)) {
93 if (empty($course)) {
94 $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'returnto' => 'profile'));
95 } else {
96 $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'course' => $course->id,
97 'returnto' => 'profile'));
100 $node = new core_user\output\myprofile\node('contact', 'editprofile',
101 get_string('editmyprofile'), null, $url, null, null, 'editprofile');
102 $tree->add_node($node);
107 // Preference page.
108 if (!$iscurrentuser && $PAGE->settingsnav->can_view_user_preferences($user->id)) {
109 $url = new moodle_url('/user/preferences.php', array('userid' => $user->id));
110 $title = get_string('preferences', 'moodle');
111 $node = new core_user\output\myprofile\node('administration', 'preferences', $title, null, $url);
112 $tree->add_node($node);
115 // Login as ...
116 if (!$user->deleted && !$iscurrentuser &&
117 !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas',
118 $courseorsystemcontext) && !is_siteadmin($user->id)) {
119 $url = new moodle_url('/course/loginas.php',
120 array('id' => $courseid, 'user' => $user->id, 'sesskey' => sesskey()));
121 $node = new core_user\output\myprofile\node('administration', 'loginas', get_string('loginas'), null, $url);
122 $tree->add_node($node);
125 // Contact details.
126 if (has_capability('moodle/user:viewhiddendetails', $courseorusercontext)) {
127 $hiddenfields = array();
128 } else {
129 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
131 if (has_capability('moodle/site:viewuseridentity', $courseorusercontext)) {
132 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
133 } else {
134 $identityfields = array();
137 if (is_mnet_remote_user($user)) {
138 $sql = "SELECT h.id, h.name, h.wwwroot,
139 a.name as application, a.display_name
140 FROM {mnet_host} h, {mnet_application} a
141 WHERE h.id = ? AND h.applicationid = a.id";
143 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
144 $remoteuser = new stdclass();
145 $remoteuser->remotetype = $remotehost->display_name;
146 $hostinfo = new stdclass();
147 $hostinfo->remotename = $remotehost->name;
148 $hostinfo->remoteurl = $remotehost->wwwroot;
150 $node = new core_user\output\myprofile\node('contact', 'mnet', get_string('remoteuser', 'mnet', $remoteuser), null, null,
151 get_string('remoteuserinfo', 'mnet', $hostinfo), null, 'remoteuserinfo');
152 $tree->add_node($node);
155 if (isset($identityfields['email']) and ($iscurrentuser
156 or $user->maildisplay == 1
157 or has_capability('moodle/course:useremail', $courseorusercontext)
158 or has_capability('moodle/site:viewuseridentity', $courseorusercontext)
159 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
160 $node = new core_user\output\myprofile\node('contact', 'email', get_string('email'), null, null,
161 obfuscate_mailto($user->email, ''));
162 $tree->add_node($node);
165 if (!isset($hiddenfields['country']) && $user->country) {
166 $node = new core_user\output\myprofile\node('contact', 'country', get_string('country'), null, null,
167 get_string($user->country, 'countries'));
168 $tree->add_node($node);
171 if (!isset($hiddenfields['city']) && $user->city) {
172 $node = new core_user\output\myprofile\node('contact', 'city', get_string('city'), null, null, $user->city);
173 $tree->add_node($node);
176 if (isset($identityfields['address']) && $user->address) {
177 $node = new core_user\output\myprofile\node('contact', 'address', get_string('address'), null, null, $user->address);
178 $tree->add_node($node);
181 if (isset($identityfields['phone1']) && $user->phone1) {
182 $node = new core_user\output\myprofile\node('contact', 'phone1', get_string('phone1'), null, null, $user->phone1);
183 $tree->add_node($node);
186 if (isset($identityfields['phone2']) && $user->phone2) {
187 $node = new core_user\output\myprofile\node('contact', 'phone2', get_string('phone2'), null, null, $user->phone2);
188 $tree->add_node($node);
191 if (isset($identityfields['institution']) && $user->institution) {
192 $node = new core_user\output\myprofile\node('contact', 'institution', get_string('institution'), null, null,
193 $user->institution);
194 $tree->add_node($node);
197 if (isset($identityfields['department']) && $user->department) {
198 $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null,
199 $user->department);
200 $tree->add_node($node);
203 if (isset($identityfields['idnumber']) && $user->idnumber) {
204 $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null,
205 $user->idnumber);
206 $tree->add_node($node);
209 if ($user->url && !isset($hiddenfields['webpage'])) {
210 $url = $user->url;
211 if (strpos($user->url, '://') === false) {
212 $url = 'http://'. $url;
214 $webpageurl = new moodle_url($url);
215 $node = new core_user\output\myprofile\node('contact', 'webpage', get_string('webpage'), null, null,
216 html_writer::link($url, $webpageurl));
217 $tree->add_node($node);
220 // Printing tagged interests. We want this only for full profile.
221 if (!empty($CFG->usetags) && empty($course)) {
222 if ($interests = tag_get_tags_csv('user', $user->id) ) {
223 $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null, $interests);
224 $tree->add_node($node);
228 if (!isset($hiddenfields['mycourses'])) {
229 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
230 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) {
231 $shown = 0;
232 $courselisting = html_writer::start_tag('ul');
233 foreach ($mycourses as $mycourse) {
234 if ($mycourse->category) {
235 context_helper::preload_from_record($mycourse);
236 $ccontext = context_course::instance($mycourse->id);
237 if (!isset($course) || $mycourse->id != $course->id) {
238 $linkattributes = null;
239 if ($mycourse->visible == 0) {
240 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
241 continue;
243 $linkattributes['class'] = 'dimmed';
245 $params = array('id' => $user->id, 'course' => $mycourse->id);
246 if ($showallcourses) {
247 $params['showallcourses'] = 1;
249 $url = new moodle_url('/user/view.php', $params);
250 $courselisting .= html_writer::tag('li', html_writer::link($url, $ccontext->get_context_name(false),
251 $linkattributes));
252 } else {
253 $courselisting .= html_writer::tag('li', $course->fullname);
256 $shown++;
257 if (!$showallcourses && $shown == $CFG->navcourselimit) {
258 $url = null;
259 if (isset($course)) {
260 $url = new moodle_url('/user/view.php',
261 array('id' => $user->id, 'course' => $course->id, 'showallcourses' => 1));
262 } else {
263 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
265 $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'),
266 array('title' => get_string('viewmore'))), array('class' => 'viewmore'));
267 break;
270 $courselisting .= html_writer::end_tag('ul');
271 if (!empty($mycourses)) {
272 // Add this node only if there are courses to display.
273 $node = new core_user\output\myprofile\node('coursedetails', 'courseprofiles',
274 get_string('courseprofiles'), null, null, rtrim($courselisting, ', '));
275 $tree->add_node($node);
280 if (!empty($course)) {
282 // Show roles in this course.
283 if ($rolestring = get_user_roles_in_course($user->id, $course->id)) {
284 $node = new core_user\output\myprofile\node('coursedetails', 'roles', get_string('roles'), null, null, $rolestring);
285 $tree->add_node($node);
288 // Show groups this user is in.
289 if (!isset($hiddenfields['groups']) && !empty($course)) {
290 $accessallgroups = has_capability('moodle/site:accessallgroups', $courseorsystemcontext);
291 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
292 $groupstr = '';
293 foreach ($usergroups as $group) {
294 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
295 if (!groups_is_member($group->id, $user->id)) {
296 continue;
300 if ($course->groupmode != NOGROUPS) {
301 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'
302 .format_string($group->name).'</a>,';
303 } else {
304 // The user/index.php shows groups only when course in group mode.
305 $groupstr .= ' '.format_string($group->name);
308 if ($groupstr !== '') {
309 $node = new core_user\output\myprofile\node('coursedetails', 'groups',
310 get_string('group'), null, null, rtrim($groupstr, ', '));
311 $tree->add_node($node);
316 if (!isset($hiddenfields['suspended'])) {
317 if ($user->suspended) {
318 $node = new core_user\output\myprofile\node('coursedetails', 'suspended',
319 null, null, null, get_string('suspended', 'auth'));
320 $tree->add_node($node);
325 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
326 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin' => $user->icq) );
327 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5'));
328 $statusicon = html_writer::tag('img', '',
329 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
330 $node = new core_user\output\myprofile\node('contact', 'icqnumber', get_string('icqnumber'), null, null,
331 html_writer::link($imurl, s($user->icq) . $statusicon));
332 $tree->add_node($node);
335 if ($user->skype && !isset($hiddenfields['skypeid'])) {
336 $imurl = 'skype:'.urlencode($user->skype).'?call';
337 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.urlencode($user->skype));
338 if (is_https()) {
339 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
340 $statusicon = '';
341 } else {
342 $statusicon = html_writer::empty_tag('img',
343 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
346 $node = new core_user\output\myprofile\node('contact', 'skypeid', get_string('skypeid'), null, null,
347 html_writer::link($imurl, s($user->skype) . $statusicon));
348 $tree->add_node($node);
350 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
351 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg'));
352 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0'));
353 $statusicon = html_writer::tag('img', '',
354 array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status')));
356 $node = new core_user\output\myprofile\node('contact', 'yahooid', get_string('yahooid'), null, null,
357 html_writer::link($imurl, s($user->yahoo) . $statusicon));
358 $tree->add_node($node);
360 if ($user->aim && !isset($hiddenfields['aimid'])) {
361 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
362 $node = new core_user\output\myprofile\node('contact', 'aimid', get_string('aimid'), null, null,
363 html_writer::link($imurl, s($user->aim)));
364 $tree->add_node($node);
366 if ($user->msn && !isset($hiddenfields['msnid'])) {
367 $node = new core_user\output\myprofile\node('contact', 'msnid', get_string('msnid'), null, null,
368 s($user->msn));
369 $tree->add_node($node);
372 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
373 foreach ($categories as $category) {
374 if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
375 foreach ($fields as $field) {
376 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
377 $newfield = 'profile_field_'.$field->datatype;
378 $formfield = new $newfield($field->id, $user->id);
379 if ($formfield->is_visible() and !$formfield->is_empty()) {
380 $node = new core_user\output\myprofile\node('contact', 'custom_field_' . $formfield->field->shortname,
381 format_string($formfield->field->name), null, null, $formfield->display_data());
382 $tree->add_node($node);
389 // First access. (Why only for sites ?)
390 if (!isset($hiddenfields['firstaccess']) && empty($course)) {
391 if ($user->firstaccess) {
392 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
393 } else {
394 $datestring = get_string("never");
396 $node = new core_user\output\myprofile\node('loginactivity', 'firstaccess', get_string('firstsiteaccess'), null, null,
397 $datestring);
398 $tree->add_node($node);
401 // Last access.
402 if (!isset($hiddenfields['lastaccess'])) {
403 if (empty($course)) {
404 $string = get_string('lastsiteaccess');
405 if ($user->lastaccess) {
406 $datestring = userdate($user->lastaccess) . "&nbsp; (" . format_time(time() - $user->lastaccess) . ")";
407 } else {
408 $datestring = get_string("never");
410 } else {
411 $string = get_string('lastcourseaccess');
412 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
413 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
414 } else {
415 $datestring = get_string("never");
419 $node = new core_user\output\myprofile\node('loginactivity', 'lastaccess', $string, null, null,
420 $datestring);
421 $tree->add_node($node);
424 // Last ip.
425 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
426 if ($user->lastip) {
427 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $USER->id));
428 $ipstring = html_writer::link($iplookupurl, $user->lastip);
429 } else {
430 $ipstring = get_string("none");
432 $node = new core_user\output\myprofile\node('loginactivity', 'lastip', get_string('lastip'), null, null,
433 $ipstring);
434 $tree->add_node($node);