MDL-49819 navigation: Admin section in user profile update.
[moodle.git] / lib / myprofilelib.php
CommitLineData
b19cc4ef
AA
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.
13//
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/>.
16
17/**
18 * Defines core nodes for my profile navigation tree.
19 *
20 * @package core
21 * @copyright 2015 onwards Ankit Agarwal
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Defines core nodes for my profile navigation tree.
29 *
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
34 *
35 * @return bool
36 */
37function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
38 global $CFG, $USER, $DB;
39
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;
44
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');
50
51 // Add categories.
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);
57
58 // Add core nodes.
59 // Full profile node.
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);
67 }
68 }
69
70 // Edit profile.
71 if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) {
72 if (($iscurrentuser || is_siteadmin($USER) || !is_siteadmin($user)) && has_capability('moodle/user:update',
73 $systemcontext)) {
74 $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $courseid));
3a13707a 75 $node = new core_user\output\myprofile\node('contact', 'editprofile', get_string('editmyprofile'), null, $url);
b19cc4ef
AA
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);
82 }
83 if ($userauthplugin && $userauthplugin->can_edit_profile()) {
84 $url = $userauthplugin->edit_profile_url();
85 if (empty($url)) {
45367bdf
AG
86 if (empty($course)) {
87 $url = new moodle_url('/user/edit.php', array('userid' => $user->id));
88 } else {
89 $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'course' => $course->id));
90 }
b19cc4ef 91 }
3a13707a 92 $node = new core_user\output\myprofile\node('contact', 'editprofile',
b19cc4ef
AA
93 get_string('editmyprofile'), null, $url);
94 $tree->add_node($node);
95 }
96 }
97 }
3a13707a
AG
98
99 // Preference page. Only visible by administrators.
100 if (is_siteadmin()) {
b19cc4ef
AA
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);
105 }
106
107 // Login as ...
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);
115 }
116
117 // Contact details.
118 if (has_capability('moodle/user:viewhiddendetails', $usercontext)) {
119 $hiddenfields = array();
120 } else {
121 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
122 }
123 if (has_capability('moodle/site:viewuseridentity', $context)) {
124 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
125 } else {
126 $identityfields = array();
127 }
128
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";
134
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;
141
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);
145 }
146
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);
152 }
153 }
154
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);
159 }
160
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);
164 }
165
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);
169 }
170
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);
174 }
175
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);
179 }
180
181 if (isset($identityfields['institution']) && $user->institution) {
182 $node = new core_user\output\myprofile\node('contact', 'institution', get_string('institution'), null, null,
183 $user->institution);
184 $tree->add_node($node);
185 }
186
187 if (isset($identityfields['department']) && $user->department) {
188 $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null,
189 $user->institution);
190 $tree->add_node($node);
191 }
192
193 if (isset($identityfields['idnumber']) && $user->idnumber) {
194 $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null,
195 $user->institution);
196 $tree->add_node($node);
197 }
198
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);
206 }
207
208 if ($user->url && !isset($hiddenfields['webpage'])) {
209 $url = $user->url;
210 if (strpos($user->url, '://') === false) {
211 $url = 'http://'. $url;
212 }
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);
216 }
217
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')) {
221 $shown = 0;
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)) {
231 continue;
232 }
233 $linkattributes['class'] = 'dimmed';
234 }
235 $params = array('id' => $user->id, 'course' => $mycourse->id);
236 if ($showallcourses) {
237 $params['showallcourses'] = 1;
238 }
239 $url = new moodle_url('/user/view.php', $params);
240 $courselisting .= html_writer::tag('li', html_writer::link($url, $ccontext->get_context_name(false),
241 $linkattributes));
242 } else {
243 $courselisting .= html_writer::tag('li', $course->fullname);
244 }
245 }
246 $shown++;
247 if (!$showallcourses && $shown == $CFG->navcourselimit) {
248 $url = null;
249 if (isset($course)) {
250 $url = new moodle_url('/user/view.php',
251 array('id' => $user->id, 'course' => $course->id, 'showallcourses' => 1));
252 } else {
253 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
254 }
255 $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'),
256 array('title' => get_string('viewmore'))));
257 break;
258 }
259 }
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);
266 }
267 }
268 }
269
270 if (!empty($course)) {
271
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);
276 }
277
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)) {
282 $groupstr = '';
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)) {
286 continue;
287 }
288 }
289
290 if ($course->groupmode != NOGROUPS) {
291 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'
292 .format_string($group->name).'</a>,';
293 } else {
294 // The user/index.php shows groups only when course in group mode.
295 $groupstr .= ' '.format_string($group->name);
296 }
297 }
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);
302 }
303 }
304 }
305
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);
311 }
312 }
313
314 echo html_writer::end_tag('dl');
315 }
316
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);
325 }
326
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));
330 if (is_https()) {
331 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
332 $statusicon = '';
333 } else {
334 $statusicon = html_writer::empty_tag('img',
335 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
336 }
337
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);
341 }
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')));
347
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);
351 }
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);
357 }
358 if ($user->msn && !isset($hiddenfields['msnid'])) {
359 $node = new core_user\output\myprofile\node('contact', 'msnid', get_string('msnid'), null, null,
360 s($user->msn));
361 $tree->add_node($node);
362 }
363
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);
375 }
376 }
377 }
378 }
379 }
380
381 // First access. (Why only for sites ?)
382 if (!isset($hiddenfields['firstaccess']) && empty($course)) {
383 if ($user->firstaccess) {
384 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
385 } else {
386 $datestring = get_string("never");
387 }
388 $node = new core_user\output\myprofile\node('miscellaneous', 'firstaccess', get_string('firstsiteaccess'), null, null,
389 $datestring);
390 $tree->add_node($node);
391 }
392
393 // Last access.
394 if (!isset($hiddenfields['lastaccess'])) {
395 if (empty($course)) {
396 $string = get_string('lastsiteaccess');
397 if ($user->lastaccess) {
398 $datestring = userdate($user->lastaccess) . "&nbsp; (" . format_time(time() - $user->lastaccess) . ")";
399 } else {
400 $datestring = get_string("never");
401 }
402 } else {
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)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
406 } else {
407 $datestring = get_string("never");
408 }
409 }
410
411 $node = new core_user\output\myprofile\node('miscellaneous', 'lastaccess', $string, null, null,
412 $datestring);
413 $tree->add_node($node);
414 }
415
416 // Last ip.
417 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
418 if ($user->lastip) {
419 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $USER->id));
420 $ipstring = html_writer::link($iplookupurl, $user->lastip);
421 } else {
422 $ipstring = get_string("none");
423 }
424 $node = new core_user\output\myprofile\node('miscellaneous', 'lastip', get_string('lastip'), null, null,
425 $ipstring);
426 $tree->add_node($node);
427 }
428}