Merge branch 'MDL-48202-M30' of git://github.com/lazydaisy/moodle
[moodle.git] / lib / myprofilelib.php
bloba00b5ecd0434e7af8766d8f9d651fd71c441faf8
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;
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 // No after property specified intentionally. It is a hack to make administration block appear towards the end. Refer MDL-49928.
47 $coursedetailscategory = new core_user\output\myprofile\category('coursedetails', get_string('coursedetails'));
48 $miscategory = new core_user\output\myprofile\category('miscellaneous', get_string('miscellaneous'), 'coursedetails');
49 $reportcategory = new core_user\output\myprofile\category('reports', get_string('reports'), 'miscellaneous');
50 $admincategory = new core_user\output\myprofile\category('administration', get_string('administration'), 'reports');
51 $loginactivitycategory = new core_user\output\myprofile\category('loginactivity', get_string('loginactivity'), 'administration');
53 // Add categories.
54 $tree->add_category($contactcategory);
55 $tree->add_category($coursedetailscategory);
56 $tree->add_category($miscategory);
57 $tree->add_category($reportcategory);
58 $tree->add_category($admincategory);
59 $tree->add_category($loginactivitycategory);
61 // Add core nodes.
62 // Full profile node.
63 if (!empty($course)) {
64 if (empty($CFG->forceloginforprofiles) || $iscurrentuser ||
65 has_capability('moodle/user:viewdetails', context_user::instance($user->id))
66 || has_coursecontact_role($user->id)) {
67 $url = new moodle_url('/user/profile.php', array('id' => $user->id));
68 $node = new core_user\output\myprofile\node('miscellaneous', 'fullprofile', get_string('fullprofile'), null, $url);
69 $tree->add_node($node);
73 // Edit profile.
74 if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) {
75 if (($iscurrentuser || is_siteadmin($USER) || !is_siteadmin($user)) && has_capability('moodle/user:update',
76 $systemcontext)) {
77 $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $courseid,
78 'returnto' => 'profile'));
79 $node = new core_user\output\myprofile\node('contact', 'editprofile', get_string('editmyprofile'), null, $url);
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();
89 if (empty($url)) {
90 if (empty($course)) {
91 $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'returnto' => 'profile'));
92 } else {
93 $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'course' => $course->id,
94 'returnto' => 'profile'));
97 $node = new core_user\output\myprofile\node('contact', 'editprofile',
98 get_string('editmyprofile'), null, $url);
99 $tree->add_node($node);
104 // Preference page. Only visible by administrators.
105 if (!$iscurrentuser && is_siteadmin()) {
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);
112 // Login as ...
113 if (!$user->deleted && !$iscurrentuser &&
114 !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas',
115 $context) && !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);
122 // Contact details.
123 if (has_capability('moodle/user:viewhiddendetails', $usercontext)) {
124 $hiddenfields = array();
125 } else {
126 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
128 if (has_capability('moodle/site:viewuseridentity', $context)) {
129 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
130 } else {
131 $identityfields = array();
134 if (is_mnet_remote_user($user)) {
135 $sql = "SELECT h.id, h.name, h.wwwroot,
136 a.name as application, a.display_name
137 FROM {mnet_host} h, {mnet_application} a
138 WHERE h.id = ? AND h.applicationid = a.id";
140 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
141 $remoteuser = new stdclass();
142 $remoteuser->remotetype = $remotehost->display_name;
143 $hostinfo = new stdclass();
144 $hostinfo->remotename = $remotehost->name;
145 $hostinfo->remoteurl = $remotehost->wwwroot;
147 $node = new core_user\output\myprofile\node('contact', 'mnet', get_string('remoteuser', 'mnet', $remoteuser), null, null,
148 get_string('remoteuserinfo', 'mnet', $hostinfo), null, 'remoteuserinfo');
149 $tree->add_node($node);
152 if (isset($identityfields['email']) and ($iscurrentuser
153 or $user->maildisplay == 1
154 or has_capability('moodle/course:useremail', $usercontext)
155 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
156 $node = new core_user\output\myprofile\node('contact', 'email', get_string('email'), null, null,
157 obfuscate_mailto($user->email, ''));
158 $tree->add_node($node);
161 if (!isset($hiddenfields['country']) && $user->country) {
162 $node = new core_user\output\myprofile\node('contact', 'country', get_string('country'), null, null,
163 get_string($user->country, 'countries'));
164 $tree->add_node($node);
167 if (!isset($hiddenfields['city']) && $user->city) {
168 $node = new core_user\output\myprofile\node('contact', 'city', get_string('city'), null, null, $user->city);
169 $tree->add_node($node);
172 if (isset($identityfields['address']) && $user->address) {
173 $node = new core_user\output\myprofile\node('contact', 'address', get_string('address'), null, null, $user->address);
174 $tree->add_node($node);
177 if (isset($identityfields['phone1']) && $user->phone1) {
178 $node = new core_user\output\myprofile\node('contact', 'phone1', get_string('phone'), null, null, $user->phone1);
179 $tree->add_node($node);
182 if (isset($identityfields['phone2']) && $user->phone2) {
183 $node = new core_user\output\myprofile\node('contact', 'phone2', get_string('phone2'), null, null, $user->phone2);
184 $tree->add_node($node);
187 if (isset($identityfields['institution']) && $user->institution) {
188 $node = new core_user\output\myprofile\node('contact', 'institution', get_string('institution'), null, null,
189 $user->institution);
190 $tree->add_node($node);
193 if (isset($identityfields['department']) && $user->department) {
194 $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null,
195 $user->institution);
196 $tree->add_node($node);
199 if (isset($identityfields['idnumber']) && $user->idnumber) {
200 $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null,
201 $user->institution);
202 $tree->add_node($node);
205 if ($user->url && !isset($hiddenfields['webpage'])) {
206 $url = $user->url;
207 if (strpos($user->url, '://') === false) {
208 $url = 'http://'. $url;
210 $webpageurl = new moodle_url($url);
211 $node = new core_user\output\myprofile\node('contact', 'webpage', get_string('webpage'), null, null,
212 html_writer::link($url, $webpageurl));
213 $tree->add_node($node);
216 // Printing tagged interests. We want this only for full profile.
217 if (!empty($CFG->usetags) && empty($course)) {
218 if ($interests = tag_get_tags_csv('user', $user->id) ) {
219 $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null, $interests);
220 $tree->add_node($node);
224 if (!isset($hiddenfields['mycourses'])) {
225 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
226 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) {
227 $shown = 0;
228 $courselisting = html_writer::start_tag('ul');
229 foreach ($mycourses as $mycourse) {
230 if ($mycourse->category) {
231 context_helper::preload_from_record($mycourse);
232 $ccontext = context_course::instance($mycourse->id);
233 if (!isset($course) || $mycourse->id != $course->id) {
234 $linkattributes = null;
235 if ($mycourse->visible == 0) {
236 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
237 continue;
239 $linkattributes['class'] = 'dimmed';
241 $params = array('id' => $user->id, 'course' => $mycourse->id);
242 if ($showallcourses) {
243 $params['showallcourses'] = 1;
245 $url = new moodle_url('/user/view.php', $params);
246 $courselisting .= html_writer::tag('li', html_writer::link($url, $ccontext->get_context_name(false),
247 $linkattributes));
248 } else {
249 $courselisting .= html_writer::tag('li', $course->fullname);
252 $shown++;
253 if (!$showallcourses && $shown == $CFG->navcourselimit) {
254 $url = null;
255 if (isset($course)) {
256 $url = new moodle_url('/user/view.php',
257 array('id' => $user->id, 'course' => $course->id, 'showallcourses' => 1));
258 } else {
259 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
261 $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'),
262 array('title' => get_string('viewmore'))));
263 break;
266 $courselisting .= html_writer::end_tag('ul');
267 if (!empty($mycourses)) {
268 // Add this node only if there are courses to display.
269 $node = new core_user\output\myprofile\node('coursedetails', 'courseprofiles',
270 get_string('courseprofiles'), null, null, rtrim($courselisting, ', '));
271 $tree->add_node($node);
276 if (!empty($course)) {
278 // Show roles in this course.
279 if ($rolestring = get_user_roles_in_course($user->id, $course->id)) {
280 $node = new core_user\output\myprofile\node('coursedetails', 'roles', get_string('roles'), null, null, $rolestring);
281 $tree->add_node($node);
284 // Show groups this user is in.
285 if (!isset($hiddenfields['groups']) && !empty($course)) {
286 $accessallgroups = has_capability('moodle/site:accessallgroups', $context);
287 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
288 $groupstr = '';
289 foreach ($usergroups as $group) {
290 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
291 if (!groups_is_member($group->id, $user->id)) {
292 continue;
296 if ($course->groupmode != NOGROUPS) {
297 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'
298 .format_string($group->name).'</a>,';
299 } else {
300 // The user/index.php shows groups only when course in group mode.
301 $groupstr .= ' '.format_string($group->name);
304 if ($groupstr !== '') {
305 $node = new core_user\output\myprofile\node('coursedetails', 'groups',
306 get_string('group'), null, null, rtrim($groupstr, ', '));
307 $tree->add_node($node);
312 if (!isset($hiddenfields['suspended'])) {
313 if ($user->suspended) {
314 $node = new core_user\output\myprofile\node('coursedetails', 'suspended',
315 null, null, null, get_string('suspended', 'auth'));
316 $tree->add_node($node);
321 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
322 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin' => $user->icq) );
323 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5'));
324 $statusicon = html_writer::tag('img', '',
325 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
326 $node = new core_user\output\myprofile\node('contact', 'icqnumber', get_string('icqnumber'), null, null,
327 html_writer::link($imurl, s($user->icq) . $statusicon));
328 $tree->add_node($node);
331 if ($user->skype && !isset($hiddenfields['skypeid'])) {
332 $imurl = 'skype:'.urlencode($user->skype).'?call';
333 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.urlencode($user->skype));
334 if (is_https()) {
335 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
336 $statusicon = '';
337 } else {
338 $statusicon = html_writer::empty_tag('img',
339 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
342 $node = new core_user\output\myprofile\node('contact', 'skypeid', get_string('skypeid'), null, null,
343 html_writer::link($imurl, s($user->skype) . $statusicon));
344 $tree->add_node($node);
346 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
347 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg'));
348 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0'));
349 $statusicon = html_writer::tag('img', '',
350 array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status')));
352 $node = new core_user\output\myprofile\node('contact', 'yahooid', get_string('yahooid'), null, null,
353 html_writer::link($imurl, s($user->yahoo) . $statusicon));
354 $tree->add_node($node);
356 if ($user->aim && !isset($hiddenfields['aimid'])) {
357 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
358 $node = new core_user\output\myprofile\node('contact', 'aimid', get_string('aimid'), null, null,
359 html_writer::link($imurl, s($user->aim)));
360 $tree->add_node($node);
362 if ($user->msn && !isset($hiddenfields['msnid'])) {
363 $node = new core_user\output\myprofile\node('contact', 'msnid', get_string('msnid'), null, null,
364 s($user->msn));
365 $tree->add_node($node);
368 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
369 foreach ($categories as $category) {
370 if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
371 foreach ($fields as $field) {
372 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
373 $newfield = 'profile_field_'.$field->datatype;
374 $formfield = new $newfield($field->id, $user->id);
375 if ($formfield->is_visible() and !$formfield->is_empty()) {
376 $node = new core_user\output\myprofile\node('contact', $formfield->field->shortname,
377 format_string($formfield->field->name), null, null, $formfield->display_data());
378 $tree->add_node($node);
385 // First access. (Why only for sites ?)
386 if (!isset($hiddenfields['firstaccess']) && empty($course)) {
387 if ($user->firstaccess) {
388 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
389 } else {
390 $datestring = get_string("never");
392 $node = new core_user\output\myprofile\node('loginactivity', 'firstaccess', get_string('firstsiteaccess'), null, null,
393 $datestring);
394 $tree->add_node($node);
397 // Last access.
398 if (!isset($hiddenfields['lastaccess'])) {
399 if (empty($course)) {
400 $string = get_string('lastsiteaccess');
401 if ($user->lastaccess) {
402 $datestring = userdate($user->lastaccess) . "&nbsp; (" . format_time(time() - $user->lastaccess) . ")";
403 } else {
404 $datestring = get_string("never");
406 } else {
407 $string = get_string('lastcourseaccess');
408 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
409 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
410 } else {
411 $datestring = get_string("never");
415 $node = new core_user\output\myprofile\node('loginactivity', 'lastaccess', $string, null, null,
416 $datestring);
417 $tree->add_node($node);
420 // Last ip.
421 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
422 if ($user->lastip) {
423 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $USER->id));
424 $ipstring = html_writer::link($iplookupurl, $user->lastip);
425 } else {
426 $ipstring = get_string("none");
428 $node = new core_user\output\myprofile\node('loginactivity', 'lastip', get_string('lastip'), null, null,
429 $ipstring);
430 $tree->add_node($node);