MDL-45898 myprofile: Redesign my profile page
[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));
75 $node = new core_user\output\myprofile\node('administration', '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);
82 }
83 if ($userauthplugin && $userauthplugin->can_edit_profile()) {
84 $url = $userauthplugin->edit_profile_url();
85 if (empty($url)) {
86 $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'course' => $id));
87 }
88 $node = new core_user\output\myprofile\node('administration', 'editprofile',
89 get_string('editmyprofile'), null, $url);
90 $tree->add_node($node);
91 }
92 }
93 }
94 // Preference page.
95 if ($iscurrentuser || is_siteadmin()) {
96 $url = new moodle_url('/user/preferences.php', array('userid' => $user->id));
97 $title = $iscurrentuser ? get_string('mypreferences') : get_string('userspreferences', 'moodle', fullname($user));
98 $node = new core_user\output\myprofile\node('administration', 'preferences', $title, null, $url);
99 $tree->add_node($node);
100 }
101
102 // Login as ...
103 if (!$user->deleted && !$iscurrentuser &&
104 !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas',
105 $context) && !is_siteadmin($user->id)) {
106 $url = new moodle_url('/course/loginas.php',
107 array('id' => $courseid, 'user' => $user->id, 'sesskey' => sesskey()));
108 $node = new core_user\output\myprofile\node('administration', 'loginas', get_string('loginas'), null, $url);
109 $tree->add_node($node);
110 }
111
112 // Contact details.
113 if (has_capability('moodle/user:viewhiddendetails', $usercontext)) {
114 $hiddenfields = array();
115 } else {
116 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
117 }
118 if (has_capability('moodle/site:viewuseridentity', $context)) {
119 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
120 } else {
121 $identityfields = array();
122 }
123
124 if (is_mnet_remote_user($user)) {
125 $sql = "SELECT h.id, h.name, h.wwwroot,
126 a.name as application, a.display_name
127 FROM {mnet_host} h, {mnet_application} a
128 WHERE h.id = ? AND h.applicationid = a.id";
129
130 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
131 $remoteuser = new stdclass();
132 $remoteuser->remotetype = $remotehost->display_name;
133 $hostinfo = new stdclass();
134 $hostinfo->remotename = $remotehost->name;
135 $hostinfo->remoteurl = $remotehost->wwwroot;
136
137 $node = new core_user\output\myprofile\node('contact', 'mnet', get_string('remoteuser', 'mnet', $remoteuser), null, null,
138 get_string('remoteuserinfo', 'mnet', $hostinfo), null, 'remoteuserinfo');
139 $tree->add_node($node);
140 }
141
142 // Printing tagged interests. We want this only for full profile.
143 if (!empty($CFG->usetags) && empty($course)) {
144 if ($interests = tag_get_tags_csv('user', $user->id) ) {
145 $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null, $interests);
146 $tree->add_node($node);
147 }
148 }
149
150 if (!isset($hiddenfields['country']) && $user->country) {
151 $node = new core_user\output\myprofile\node('contact', 'country', get_string('country'), null, null,
152 get_string($user->country, 'countries'));
153 $tree->add_node($node);
154 }
155
156 if (!isset($hiddenfields['city']) && $user->city) {
157 $node = new core_user\output\myprofile\node('contact', 'city', get_string('city'), null, null, $user->city);
158 $tree->add_node($node);
159 }
160
161 if (isset($identityfields['address']) && $user->address) {
162 $node = new core_user\output\myprofile\node('contact', 'address', get_string('address'), null, null, $user->address);
163 $tree->add_node($node);
164 }
165
166 if (isset($identityfields['phone1']) && $user->phone1) {
167 $node = new core_user\output\myprofile\node('contact', 'phone1', get_string('phone'), null, null, $user->phone1);
168 $tree->add_node($node);
169 }
170
171 if (isset($identityfields['phone2']) && $user->phone2) {
172 $node = new core_user\output\myprofile\node('contact', 'phone2', get_string('phone2'), null, null, $user->phone2);
173 $tree->add_node($node);
174 }
175
176 if (isset($identityfields['institution']) && $user->institution) {
177 $node = new core_user\output\myprofile\node('contact', 'institution', get_string('institution'), null, null,
178 $user->institution);
179 $tree->add_node($node);
180 }
181
182 if (isset($identityfields['department']) && $user->department) {
183 $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null,
184 $user->institution);
185 $tree->add_node($node);
186 }
187
188 if (isset($identityfields['idnumber']) && $user->idnumber) {
189 $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null,
190 $user->institution);
191 $tree->add_node($node);
192 }
193
194 if (isset($identityfields['email']) and ($iscurrentuser
195 or $user->maildisplay == 1
196 or has_capability('moodle/course:useremail', $usercontext)
197 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
198 $node = new core_user\output\myprofile\node('contact', 'email', get_string('email'), null, null,
199 obfuscate_mailto($user->email, ''));
200 $tree->add_node($node);
201 }
202
203 if ($user->url && !isset($hiddenfields['webpage'])) {
204 $url = $user->url;
205 if (strpos($user->url, '://') === false) {
206 $url = 'http://'. $url;
207 }
208 $webpageurl = new moodle_url($url);
209 $node = new core_user\output\myprofile\node('contact', 'webpage', get_string('webpage'), null, $webpageurl);
210 $tree->add_node($node);
211 }
212
213 if (!isset($hiddenfields['mycourses'])) {
214 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
215 if ($mycourses = enrol_get_all_users_courses($user->id, true, null, 'visible DESC, sortorder ASC')) {
216 $shown = 0;
217 $courselisting = html_writer::start_tag('ul');
218 foreach ($mycourses as $mycourse) {
219 if ($mycourse->category) {
220 context_helper::preload_from_record($mycourse);
221 $ccontext = context_course::instance($mycourse->id);
222 if (!isset($course) || $mycourse->id != $course->id) {
223 $linkattributes = null;
224 if ($mycourse->visible == 0) {
225 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
226 continue;
227 }
228 $linkattributes['class'] = 'dimmed';
229 }
230 $params = array('id' => $user->id, 'course' => $mycourse->id);
231 if ($showallcourses) {
232 $params['showallcourses'] = 1;
233 }
234 $url = new moodle_url('/user/view.php', $params);
235 $courselisting .= html_writer::tag('li', html_writer::link($url, $ccontext->get_context_name(false),
236 $linkattributes));
237 } else {
238 $courselisting .= html_writer::tag('li', $course->fullname);
239 }
240 }
241 $shown++;
242 if (!$showallcourses && $shown == $CFG->navcourselimit) {
243 $url = null;
244 if (isset($course)) {
245 $url = new moodle_url('/user/view.php',
246 array('id' => $user->id, 'course' => $course->id, 'showallcourses' => 1));
247 } else {
248 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
249 }
250 $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'),
251 array('title' => get_string('viewmore'))));
252 break;
253 }
254 }
255 $courselisting .= html_writer::end_tag('ul');
256 if (!empty($mycourses)) {
257 // Add this node only if there are courses to display.
258 $node = new core_user\output\myprofile\node('coursedetails', 'courseprofiles',
259 get_string('courseprofiles'), null, null, rtrim($courselisting, ', '));
260 $tree->add_node($node);
261 }
262 }
263 }
264
265 if (!empty($course)) {
266
267 // Show roles in this course.
268 if ($rolestring = get_user_roles_in_course($user->id, $course->id)) {
269 $node = new core_user\output\myprofile\node('coursedetails', 'roles', get_string('roles'), null, null, $rolestring);
270 $tree->add_node($node);
271 }
272
273 // Show groups this user is in.
274 if (!isset($hiddenfields['groups']) && !empty($course)) {
275 $accessallgroups = has_capability('moodle/site:accessallgroups', $context);
276 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
277 $groupstr = '';
278 foreach ($usergroups as $group) {
279 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
280 if (!groups_is_member($group->id, $user->id)) {
281 continue;
282 }
283 }
284
285 if ($course->groupmode != NOGROUPS) {
286 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'
287 .format_string($group->name).'</a>,';
288 } else {
289 // The user/index.php shows groups only when course in group mode.
290 $groupstr .= ' '.format_string($group->name);
291 }
292 }
293 if ($groupstr !== '') {
294 $node = new core_user\output\myprofile\node('coursedetails', 'groups',
295 get_string('group'), null, null, rtrim($groupstr, ', '));
296 $tree->add_node($node);
297 }
298 }
299 }
300
301 if (!isset($hiddenfields['suspended'])) {
302 if ($user->suspended) {
303 $node = new core_user\output\myprofile\node('coursedetails', 'suspended',
304 null, null, null, get_string('suspended', 'auth'));
305 $tree->add_node($node);
306 }
307 }
308
309 echo html_writer::end_tag('dl');
310 }
311
312 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
313 $imurl = new moodle_url('http://web.icq.com/wwp', array('uin' => $user->icq) );
314 $iconurl = new moodle_url('http://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5'));
315 $statusicon = html_writer::tag('img', '',
316 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
317 $node = new core_user\output\myprofile\node('contact', 'icqnumber', get_string('icqnumber'), null, null,
318 html_writer::link($imurl, s($user->icq) . $statusicon));
319 $tree->add_node($node);
320 }
321
322 if ($user->skype && !isset($hiddenfields['skypeid'])) {
323 $imurl = 'skype:'.urlencode($user->skype).'?call';
324 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.urlencode($user->skype));
325 if (is_https()) {
326 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
327 $statusicon = '';
328 } else {
329 $statusicon = html_writer::empty_tag('img',
330 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
331 }
332
333 $node = new core_user\output\myprofile\node('contact', 'skypeid', get_string('skypeid'), null, null,
334 html_writer::link($imurl, s($user->skype) . $statusicon));
335 $tree->add_node($node);
336 }
337 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
338 $imurl = new moodle_url('http://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg'));
339 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0'));
340 $statusicon = html_writer::tag('img', '',
341 array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status')));
342
343 $node = new core_user\output\myprofile\node('contact', 'yahooid', get_string('yahooid'), null, null,
344 html_writer::link($imurl, s($user->yahoo) . $statusicon));
345 $tree->add_node($node);
346 }
347 if ($user->aim && !isset($hiddenfields['aimid'])) {
348 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
349 $node = new core_user\output\myprofile\node('contact', 'aimid', get_string('aimid'), null, null,
350 html_writer::link($imurl, s($user->aim)));
351 $tree->add_node($node);
352 }
353 if ($user->msn && !isset($hiddenfields['msnid'])) {
354 $node = new core_user\output\myprofile\node('contact', 'msnid', get_string('msnid'), null, null,
355 s($user->msn));
356 $tree->add_node($node);
357 }
358
359 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
360 foreach ($categories as $category) {
361 if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
362 foreach ($fields as $field) {
363 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
364 $newfield = 'profile_field_'.$field->datatype;
365 $formfield = new $newfield($field->id, $user->id);
366 if ($formfield->is_visible() and !$formfield->is_empty()) {
367 $node = new core_user\output\myprofile\node('contact', $formfield->field->shortname,
368 format_string($formfield->field->name), null, null, $formfield->display_data());
369 $tree->add_node($node);
370 }
371 }
372 }
373 }
374 }
375
376 // First access. (Why only for sites ?)
377 if (!isset($hiddenfields['firstaccess']) && empty($course)) {
378 if ($user->firstaccess) {
379 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
380 } else {
381 $datestring = get_string("never");
382 }
383 $node = new core_user\output\myprofile\node('miscellaneous', 'firstaccess', get_string('firstsiteaccess'), null, null,
384 $datestring);
385 $tree->add_node($node);
386 }
387
388 // Last access.
389 if (!isset($hiddenfields['lastaccess'])) {
390 if (empty($course)) {
391 $string = get_string('lastsiteaccess');
392 if ($user->lastaccess) {
393 $datestring = userdate($user->lastaccess) . "&nbsp; (" . format_time(time() - $user->lastaccess) . ")";
394 } else {
395 $datestring = get_string("never");
396 }
397 } else {
398 $string = get_string('lastcourseaccess');
399 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
400 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
401 } else {
402 $datestring = get_string("never");
403 }
404 }
405
406 $node = new core_user\output\myprofile\node('miscellaneous', 'lastaccess', $string, null, null,
407 $datestring);
408 $tree->add_node($node);
409 }
410
411 // Last ip.
412 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
413 if ($user->lastip) {
414 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $USER->id));
415 $ipstring = html_writer::link($iplookupurl, $user->lastip);
416 } else {
417 $ipstring = get_string("none");
418 }
419 $node = new core_user\output\myprofile\node('miscellaneous', 'lastip', get_string('lastip'), null, null,
420 $ipstring);
421 $tree->add_node($node);
422 }
423}