MDL-51427 user: correctly use strings for phone1 and phone2
[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'));
b647db20
AA
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'));
f13a9113
DC
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');
b19cc4ef
AA
52
53 // Add categories.
54 $tree->add_category($contactcategory);
f13a9113 55 $tree->add_category($coursedetailscategory);
b19cc4ef
AA
56 $tree->add_category($miscategory);
57 $tree->add_category($reportcategory);
58 $tree->add_category($admincategory);
f13a9113 59 $tree->add_category($loginactivitycategory);
b19cc4ef
AA
60
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);
70 }
71 }
72
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)) {
69a35871
FM
77 $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $courseid,
78 'returnto' => 'profile'));
3a13707a 79 $node = new core_user\output\myprofile\node('contact', 'editprofile', get_string('editmyprofile'), null, $url);
b19cc4ef
AA
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);
86 }
87 if ($userauthplugin && $userauthplugin->can_edit_profile()) {
88 $url = $userauthplugin->edit_profile_url();
89 if (empty($url)) {
45367bdf 90 if (empty($course)) {
69a35871 91 $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'returnto' => 'profile'));
45367bdf 92 } else {
69a35871
FM
93 $url = new moodle_url('/user/edit.php', array('userid' => $user->id, 'course' => $course->id,
94 'returnto' => 'profile'));
45367bdf 95 }
b19cc4ef 96 }
3a13707a 97 $node = new core_user\output\myprofile\node('contact', 'editprofile',
b19cc4ef
AA
98 get_string('editmyprofile'), null, $url);
99 $tree->add_node($node);
100 }
101 }
102 }
3a13707a
AG
103
104 // Preference page. Only visible by administrators.
7193ea2c 105 if (!$iscurrentuser && is_siteadmin()) {
b19cc4ef 106 $url = new moodle_url('/user/preferences.php', array('userid' => $user->id));
7193ea2c 107 $title = get_string('preferences', 'moodle');
b19cc4ef
AA
108 $node = new core_user\output\myprofile\node('administration', 'preferences', $title, null, $url);
109 $tree->add_node($node);
110 }
111
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);
120 }
121
122 // Contact details.
123 if (has_capability('moodle/user:viewhiddendetails', $usercontext)) {
124 $hiddenfields = array();
125 } else {
126 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
127 }
128 if (has_capability('moodle/site:viewuseridentity', $context)) {
129 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
130 } else {
131 $identityfields = array();
132 }
133
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";
139
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;
146
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);
150 }
151
08cb34a5
FM
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);
b19cc4ef
AA
159 }
160
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);
165 }
166
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);
170 }
171
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);
175 }
176
177 if (isset($identityfields['phone1']) && $user->phone1) {
70fb46c8 178 $node = new core_user\output\myprofile\node('contact', 'phone1', get_string('phone1'), null, null, $user->phone1);
b19cc4ef
AA
179 $tree->add_node($node);
180 }
181
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);
185 }
186
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);
191 }
192
193 if (isset($identityfields['department']) && $user->department) {
194 $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null,
edd99023 195 $user->department);
b19cc4ef
AA
196 $tree->add_node($node);
197 }
198
199 if (isset($identityfields['idnumber']) && $user->idnumber) {
200 $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null,
edd99023 201 $user->idnumber);
b19cc4ef
AA
202 $tree->add_node($node);
203 }
204
b19cc4ef
AA
205 if ($user->url && !isset($hiddenfields['webpage'])) {
206 $url = $user->url;
207 if (strpos($user->url, '://') === false) {
208 $url = 'http://'. $url;
209 }
210 $webpageurl = new moodle_url($url);
6592a119
SL
211 $node = new core_user\output\myprofile\node('contact', 'webpage', get_string('webpage'), null, null,
212 html_writer::link($url, $webpageurl));
b19cc4ef
AA
213 $tree->add_node($node);
214 }
215
08cb34a5
FM
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);
221 }
222 }
223
b19cc4ef
AA
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;
238 }
239 $linkattributes['class'] = 'dimmed';
240 }
241 $params = array('id' => $user->id, 'course' => $mycourse->id);
242 if ($showallcourses) {
243 $params['showallcourses'] = 1;
244 }
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);
250 }
251 }
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));
260 }
261 $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'),
262 array('title' => get_string('viewmore'))));
263 break;
264 }
265 }
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);
272 }
273 }
274 }
275
276 if (!empty($course)) {
277
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);
282 }
283
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;
293 }
294 }
295
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);
302 }
303 }
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);
308 }
309 }
310 }
311
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);
317 }
318 }
b19cc4ef
AA
319 }
320
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);
329 }
330
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')));
340 }
341
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);
345 }
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')));
351
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);
355 }
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);
361 }
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);
366 }
367
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()) {
2b784805 376 $node = new core_user\output\myprofile\node('contact', 'custom_field_' . $formfield->field->shortname,
b19cc4ef
AA
377 format_string($formfield->field->name), null, null, $formfield->display_data());
378 $tree->add_node($node);
379 }
380 }
381 }
382 }
383 }
384
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");
391 }
f13a9113 392 $node = new core_user\output\myprofile\node('loginactivity', 'firstaccess', get_string('firstsiteaccess'), null, null,
b19cc4ef
AA
393 $datestring);
394 $tree->add_node($node);
395 }
396
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");
405 }
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");
412 }
413 }
414
f13a9113 415 $node = new core_user\output\myprofile\node('loginactivity', 'lastaccess', $string, null, null,
b19cc4ef
AA
416 $datestring);
417 $tree->add_node($node);
418 }
419
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");
427 }
f13a9113 428 $node = new core_user\output\myprofile\node('loginactivity', 'lastip', get_string('lastip'), null, null,
b19cc4ef
AA
429 $ipstring);
430 $tree->add_node($node);
431 }
432}