Moodle release 3.0rc4
[moodle.git] / lib / myprofilelib.php
blob6b4f719ac0bd3f986eb5ef0b11be4e4e02514cfd
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 * @return bool
37 function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
38 global $CFG, $USER, $DB, $PAGE;
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');
54 // Add categories.
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);
62 // Add core nodes.
63 // Full profile node.
64 if (!empty($course)) {
65 if (empty($CFG->forceloginforprofiles) || $iscurrentuser ||
66 has_capability('moodle/user:viewdetails', $usercontext)
67 || has_coursecontact_role($user->id)) {
68 $url = new moodle_url('/user/profile.php', array('id' => $user->id));
69 $node = new core_user\output\myprofile\node('miscellaneous', 'fullprofile', get_string('fullprofile'), null, $url);
70 $tree->add_node($node);
74 // Edit profile.
75 if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) {
76 if (($iscurrentuser || is_siteadmin($USER) || !is_siteadmin($user)) && has_capability('moodle/user:update',
77 $systemcontext)) {
78 $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $courseid,
79 'returnto' => 'profile'));
80 $node = new core_user\output\myprofile\node('contact', 'editprofile', get_string('editmyprofile'), null, $url,
81 null, null, 'editprofile');
82 $tree->add_node($node);
83 } else if ((has_capability('moodle/user:editprofile', $usercontext) && !is_siteadmin($user))
84 || ($iscurrentuser && has_capability('moodle/user:editownprofile', $systemcontext))) {
85 $userauthplugin = false;
86 if (!empty($user->auth)) {
87 $userauthplugin = get_auth_plugin($user->auth);
89 if ($userauthplugin && $userauthplugin->can_edit_profile()) {
90 $url = $userauthplugin->edit_profile_url();
91 if (empty($url)) {
92 if (empty($course)) {
93 $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'returnto' => 'profile'));
94 } else {
95 $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'course' => $course->id,
96 'returnto' => 'profile'));
99 $node = new core_user\output\myprofile\node('contact', 'editprofile',
100 get_string('editmyprofile'), null, $url, null, null, 'editprofile');
101 $tree->add_node($node);
106 // Preference page.
107 if (!$iscurrentuser && $PAGE->settingsnav->can_view_user_preferences($user->id)) {
108 $url = new moodle_url('/user/preferences.php', array('userid' => $user->id));
109 $title = get_string('preferences', 'moodle');
110 $node = new core_user\output\myprofile\node('administration', 'preferences', $title, null, $url);
111 $tree->add_node($node);
114 // Login as ...
115 if (!$user->deleted && !$iscurrentuser &&
116 !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas',
117 $courseorsystemcontext) && !is_siteadmin($user->id)) {
118 $url = new moodle_url('/course/loginas.php',
119 array('id' => $courseid, 'user' => $user->id, 'sesskey' => sesskey()));
120 $node = new core_user\output\myprofile\node('administration', 'loginas', get_string('loginas'), null, $url);
121 $tree->add_node($node);
124 // Contact details.
125 if (has_capability('moodle/user:viewhiddendetails', $courseorusercontext)) {
126 $hiddenfields = array();
127 } else {
128 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
130 if (has_capability('moodle/site:viewuseridentity', $courseorusercontext)) {
131 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
132 } else {
133 $identityfields = array();
136 if (is_mnet_remote_user($user)) {
137 $sql = "SELECT h.id, h.name, h.wwwroot,
138 a.name as application, a.display_name
139 FROM {mnet_host} h, {mnet_application} a
140 WHERE h.id = ? AND h.applicationid = a.id";
142 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
143 $remoteuser = new stdclass();
144 $remoteuser->remotetype = $remotehost->display_name;
145 $hostinfo = new stdclass();
146 $hostinfo->remotename = $remotehost->name;
147 $hostinfo->remoteurl = $remotehost->wwwroot;
149 $node = new core_user\output\myprofile\node('contact', 'mnet', get_string('remoteuser', 'mnet', $remoteuser), null, null,
150 get_string('remoteuserinfo', 'mnet', $hostinfo), null, 'remoteuserinfo');
151 $tree->add_node($node);
154 if (isset($identityfields['email']) and ($iscurrentuser
155 or $user->maildisplay == 1
156 or has_capability('moodle/course:useremail', $courseorusercontext)
157 or has_capability('moodle/site:viewuseridentity', $courseorusercontext)
158 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
159 $node = new core_user\output\myprofile\node('contact', 'email', get_string('email'), null, null,
160 obfuscate_mailto($user->email, ''));
161 $tree->add_node($node);
164 if (!isset($hiddenfields['country']) && $user->country) {
165 $node = new core_user\output\myprofile\node('contact', 'country', get_string('country'), null, null,
166 get_string($user->country, 'countries'));
167 $tree->add_node($node);
170 if (!isset($hiddenfields['city']) && $user->city) {
171 $node = new core_user\output\myprofile\node('contact', 'city', get_string('city'), null, null, $user->city);
172 $tree->add_node($node);
175 if (isset($identityfields['address']) && $user->address) {
176 $node = new core_user\output\myprofile\node('contact', 'address', get_string('address'), null, null, $user->address);
177 $tree->add_node($node);
180 if (isset($identityfields['phone1']) && $user->phone1) {
181 $node = new core_user\output\myprofile\node('contact', 'phone1', get_string('phone1'), null, null, $user->phone1);
182 $tree->add_node($node);
185 if (isset($identityfields['phone2']) && $user->phone2) {
186 $node = new core_user\output\myprofile\node('contact', 'phone2', get_string('phone2'), null, null, $user->phone2);
187 $tree->add_node($node);
190 if (isset($identityfields['institution']) && $user->institution) {
191 $node = new core_user\output\myprofile\node('contact', 'institution', get_string('institution'), null, null,
192 $user->institution);
193 $tree->add_node($node);
196 if (isset($identityfields['department']) && $user->department) {
197 $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null,
198 $user->department);
199 $tree->add_node($node);
202 if (isset($identityfields['idnumber']) && $user->idnumber) {
203 $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null,
204 $user->idnumber);
205 $tree->add_node($node);
208 if ($user->url && !isset($hiddenfields['webpage'])) {
209 $url = $user->url;
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, null,
215 html_writer::link($url, $webpageurl));
216 $tree->add_node($node);
219 // Printing tagged interests. We want this only for full profile.
220 if (!empty($CFG->usetags) && empty($course)) {
221 if ($interests = tag_get_tags_csv('user', $user->id) ) {
222 $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null, $interests);
223 $tree->add_node($node);
227 if (!isset($hiddenfields['mycourses'])) {
228 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
229 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) {
230 $shown = 0;
231 $courselisting = html_writer::start_tag('ul');
232 foreach ($mycourses as $mycourse) {
233 if ($mycourse->category) {
234 context_helper::preload_from_record($mycourse);
235 $ccontext = context_course::instance($mycourse->id);
236 if (!isset($course) || $mycourse->id != $course->id) {
237 $linkattributes = null;
238 if ($mycourse->visible == 0) {
239 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
240 continue;
242 $linkattributes['class'] = 'dimmed';
244 $params = array('id' => $user->id, 'course' => $mycourse->id);
245 if ($showallcourses) {
246 $params['showallcourses'] = 1;
248 $url = new moodle_url('/user/view.php', $params);
249 $courselisting .= html_writer::tag('li', html_writer::link($url, $ccontext->get_context_name(false),
250 $linkattributes));
251 } else {
252 $courselisting .= html_writer::tag('li', $course->fullname);
255 $shown++;
256 if (!$showallcourses && $shown == $CFG->navcourselimit) {
257 $url = null;
258 if (isset($course)) {
259 $url = new moodle_url('/user/view.php',
260 array('id' => $user->id, 'course' => $course->id, 'showallcourses' => 1));
261 } else {
262 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
264 $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'),
265 array('title' => get_string('viewmore'))), array('class' => 'viewmore'));
266 break;
269 $courselisting .= html_writer::end_tag('ul');
270 if (!empty($mycourses)) {
271 // Add this node only if there are courses to display.
272 $node = new core_user\output\myprofile\node('coursedetails', 'courseprofiles',
273 get_string('courseprofiles'), null, null, rtrim($courselisting, ', '));
274 $tree->add_node($node);
279 if (!empty($course)) {
281 // Show roles in this course.
282 if ($rolestring = get_user_roles_in_course($user->id, $course->id)) {
283 $node = new core_user\output\myprofile\node('coursedetails', 'roles', get_string('roles'), null, null, $rolestring);
284 $tree->add_node($node);
287 // Show groups this user is in.
288 if (!isset($hiddenfields['groups']) && !empty($course)) {
289 $accessallgroups = has_capability('moodle/site:accessallgroups', $courseorsystemcontext);
290 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
291 $groupstr = '';
292 foreach ($usergroups as $group) {
293 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
294 if (!groups_is_member($group->id, $user->id)) {
295 continue;
299 if ($course->groupmode != NOGROUPS) {
300 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'
301 .format_string($group->name).'</a>,';
302 } else {
303 // The user/index.php shows groups only when course in group mode.
304 $groupstr .= ' '.format_string($group->name);
307 if ($groupstr !== '') {
308 $node = new core_user\output\myprofile\node('coursedetails', 'groups',
309 get_string('group'), null, null, rtrim($groupstr, ', '));
310 $tree->add_node($node);
315 if (!isset($hiddenfields['suspended'])) {
316 if ($user->suspended) {
317 $node = new core_user\output\myprofile\node('coursedetails', 'suspended',
318 null, null, null, get_string('suspended', 'auth'));
319 $tree->add_node($node);
324 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
325 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin' => $user->icq) );
326 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5'));
327 $statusicon = html_writer::tag('img', '',
328 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
329 $node = new core_user\output\myprofile\node('contact', 'icqnumber', get_string('icqnumber'), null, null,
330 html_writer::link($imurl, s($user->icq) . $statusicon));
331 $tree->add_node($node);
334 if ($user->skype && !isset($hiddenfields['skypeid'])) {
335 $imurl = 'skype:'.urlencode($user->skype).'?call';
336 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.urlencode($user->skype));
337 if (is_https()) {
338 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
339 $statusicon = '';
340 } else {
341 $statusicon = html_writer::empty_tag('img',
342 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
345 $node = new core_user\output\myprofile\node('contact', 'skypeid', get_string('skypeid'), null, null,
346 html_writer::link($imurl, s($user->skype) . $statusicon));
347 $tree->add_node($node);
349 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
350 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg'));
351 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0'));
352 $statusicon = html_writer::tag('img', '',
353 array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status')));
355 $node = new core_user\output\myprofile\node('contact', 'yahooid', get_string('yahooid'), null, null,
356 html_writer::link($imurl, s($user->yahoo) . $statusicon));
357 $tree->add_node($node);
359 if ($user->aim && !isset($hiddenfields['aimid'])) {
360 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
361 $node = new core_user\output\myprofile\node('contact', 'aimid', get_string('aimid'), null, null,
362 html_writer::link($imurl, s($user->aim)));
363 $tree->add_node($node);
365 if ($user->msn && !isset($hiddenfields['msnid'])) {
366 $node = new core_user\output\myprofile\node('contact', 'msnid', get_string('msnid'), null, null,
367 s($user->msn));
368 $tree->add_node($node);
371 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
372 foreach ($categories as $category) {
373 if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
374 foreach ($fields as $field) {
375 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
376 $newfield = 'profile_field_'.$field->datatype;
377 $formfield = new $newfield($field->id, $user->id);
378 if ($formfield->is_visible() and !$formfield->is_empty()) {
379 $node = new core_user\output\myprofile\node('contact', 'custom_field_' . $formfield->field->shortname,
380 format_string($formfield->field->name), null, null, $formfield->display_data());
381 $tree->add_node($node);
388 // First access. (Why only for sites ?)
389 if (!isset($hiddenfields['firstaccess']) && empty($course)) {
390 if ($user->firstaccess) {
391 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
392 } else {
393 $datestring = get_string("never");
395 $node = new core_user\output\myprofile\node('loginactivity', 'firstaccess', get_string('firstsiteaccess'), null, null,
396 $datestring);
397 $tree->add_node($node);
400 // Last access.
401 if (!isset($hiddenfields['lastaccess'])) {
402 if (empty($course)) {
403 $string = get_string('lastsiteaccess');
404 if ($user->lastaccess) {
405 $datestring = userdate($user->lastaccess) . "&nbsp; (" . format_time(time() - $user->lastaccess) . ")";
406 } else {
407 $datestring = get_string("never");
409 } else {
410 $string = get_string('lastcourseaccess');
411 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
412 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
413 } else {
414 $datestring = get_string("never");
418 $node = new core_user\output\myprofile\node('loginactivity', 'lastaccess', $string, null, null,
419 $datestring);
420 $tree->add_node($node);
423 // Last ip.
424 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
425 if ($user->lastip) {
426 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $USER->id));
427 $ipstring = html_writer::link($iplookupurl, $user->lastip);
428 } else {
429 $ipstring = get_string("none");
431 $node = new core_user\output\myprofile\node('loginactivity', 'lastip', get_string('lastip'), null, null,
432 $ipstring);
433 $tree->add_node($node);