MDL-67460 profile: User profile link to use HTTPS
[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) {
c4e868d5 38 global $CFG, $USER, $DB, $PAGE, $OUTPUT;
b19cc4ef
AA
39
40 $usercontext = context_user::instance($user->id, MUST_EXIST);
41 $systemcontext = context_system::instance();
8290c31c
JO
42 $courseorusercontext = !empty($course) ? context_course::instance($course->id) : $usercontext;
43 $courseorsystemcontext = !empty($course) ? context_course::instance($course->id) : $systemcontext;
b19cc4ef
AA
44 $courseid = !empty($course) ? $course->id : SITEID;
45
46 $contactcategory = new core_user\output\myprofile\category('contact', get_string('userdetails'));
b647db20
AA
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'));
f13a9113
DC
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');
b19cc4ef
AA
53
54 // Add categories.
55 $tree->add_category($contactcategory);
f13a9113 56 $tree->add_category($coursedetailscategory);
b19cc4ef
AA
57 $tree->add_category($miscategory);
58 $tree->add_category($reportcategory);
59 $tree->add_category($admincategory);
f13a9113 60 $tree->add_category($loginactivitycategory);
b19cc4ef
AA
61
62 // Add core nodes.
63 // Full profile node.
64 if (!empty($course)) {
21752161 65 if (user_can_view_profile($user, null, $usercontext)) {
b19cc4ef
AA
66 $url = new moodle_url('/user/profile.php', array('id' => $user->id));
67 $node = new core_user\output\myprofile\node('miscellaneous', 'fullprofile', get_string('fullprofile'), null, $url);
68 $tree->add_node($node);
69 }
70 }
71
72 // Edit profile.
73 if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) {
74 if (($iscurrentuser || is_siteadmin($USER) || !is_siteadmin($user)) && has_capability('moodle/user:update',
75 $systemcontext)) {
69a35871
FM
76 $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $courseid,
77 'returnto' => 'profile'));
1da2e3a6
BR
78 $node = new core_user\output\myprofile\node('contact', 'editprofile', get_string('editmyprofile'), null, $url,
79 null, null, 'editprofile');
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)) {
81475d26 91 $url = new moodle_url('/user/edit.php', array('id' => $user->id, 'returnto' => 'profile'));
45367bdf 92 } else {
81475d26 93 $url = new moodle_url('/user/edit.php', array('id' => $user->id, 'course' => $course->id,
69a35871 94 'returnto' => 'profile'));
45367bdf 95 }
b19cc4ef 96 }
3a13707a 97 $node = new core_user\output\myprofile\node('contact', 'editprofile',
1da2e3a6 98 get_string('editmyprofile'), null, $url, null, null, 'editprofile');
b19cc4ef
AA
99 $tree->add_node($node);
100 }
101 }
102 }
3a13707a 103
0305ee27
AG
104 // Preference page.
105 if (!$iscurrentuser && $PAGE->settingsnav->can_view_user_preferences($user->id)) {
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',
8290c31c 115 $courseorsystemcontext) && !is_siteadmin($user->id)) {
b19cc4ef
AA
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.
8290c31c 123 if (has_capability('moodle/user:viewhiddendetails', $courseorusercontext)) {
b19cc4ef
AA
124 $hiddenfields = array();
125 } else {
126 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
127 }
565f3f25
DB
128 $canviewuseridentity = has_capability('moodle/site:viewuseridentity', $courseorusercontext);
129 if ($canviewuseridentity) {
b19cc4ef
AA
130 $identityfields = array_flip(explode(',', $CFG->showuseridentity));
131 } else {
132 $identityfields = array();
133 }
134
135 if (is_mnet_remote_user($user)) {
136 $sql = "SELECT h.id, h.name, h.wwwroot,
137 a.name as application, a.display_name
138 FROM {mnet_host} h, {mnet_application} a
139 WHERE h.id = ? AND h.applicationid = a.id";
140
141 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
142 $remoteuser = new stdclass();
143 $remoteuser->remotetype = $remotehost->display_name;
144 $hostinfo = new stdclass();
145 $hostinfo->remotename = $remotehost->name;
146 $hostinfo->remoteurl = $remotehost->wwwroot;
147
148 $node = new core_user\output\myprofile\node('contact', 'mnet', get_string('remoteuser', 'mnet', $remoteuser), null, null,
149 get_string('remoteuserinfo', 'mnet', $hostinfo), null, 'remoteuserinfo');
150 $tree->add_node($node);
151 }
152
565f3f25
DB
153 if ($iscurrentuser
154 or (!isset($hiddenfields['email']) and (
155 $user->maildisplay == core_user::MAILDISPLAY_EVERYONE
156 or ($user->maildisplay == core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY and enrol_sharing_course($user, $USER))
157 or has_capability('moodle/course:useremail', $courseorusercontext) // TODO: Deprecate/remove for MDL-37479.
158 ))
159 or (isset($identityfields['email']) and $canviewuseridentity)
160 ) {
08cb34a5
FM
161 $node = new core_user\output\myprofile\node('contact', 'email', get_string('email'), null, null,
162 obfuscate_mailto($user->email, ''));
163 $tree->add_node($node);
b19cc4ef
AA
164 }
165
16d77f18
MM
166 if (!isset($hiddenfields['moodlenetprofile']) && $user->moodlenetprofile) {
167 $node = new core_user\output\myprofile\node('contact', 'moodlenetprofile', get_string('moodlenetprofile', 'user'), null,
168 null, $user->moodlenetprofile);
169 $tree->add_node($node);
170 }
171
b19cc4ef
AA
172 if (!isset($hiddenfields['country']) && $user->country) {
173 $node = new core_user\output\myprofile\node('contact', 'country', get_string('country'), null, null,
174 get_string($user->country, 'countries'));
175 $tree->add_node($node);
176 }
177
178 if (!isset($hiddenfields['city']) && $user->city) {
179 $node = new core_user\output\myprofile\node('contact', 'city', get_string('city'), null, null, $user->city);
180 $tree->add_node($node);
181 }
182
183 if (isset($identityfields['address']) && $user->address) {
184 $node = new core_user\output\myprofile\node('contact', 'address', get_string('address'), null, null, $user->address);
185 $tree->add_node($node);
186 }
187
188 if (isset($identityfields['phone1']) && $user->phone1) {
70fb46c8 189 $node = new core_user\output\myprofile\node('contact', 'phone1', get_string('phone1'), null, null, $user->phone1);
b19cc4ef
AA
190 $tree->add_node($node);
191 }
192
193 if (isset($identityfields['phone2']) && $user->phone2) {
194 $node = new core_user\output\myprofile\node('contact', 'phone2', get_string('phone2'), null, null, $user->phone2);
195 $tree->add_node($node);
196 }
197
198 if (isset($identityfields['institution']) && $user->institution) {
199 $node = new core_user\output\myprofile\node('contact', 'institution', get_string('institution'), null, null,
200 $user->institution);
201 $tree->add_node($node);
202 }
203
204 if (isset($identityfields['department']) && $user->department) {
205 $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null,
edd99023 206 $user->department);
b19cc4ef
AA
207 $tree->add_node($node);
208 }
209
210 if (isset($identityfields['idnumber']) && $user->idnumber) {
211 $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null,
edd99023 212 $user->idnumber);
b19cc4ef
AA
213 $tree->add_node($node);
214 }
215
b19cc4ef
AA
216 if ($user->url && !isset($hiddenfields['webpage'])) {
217 $url = $user->url;
218 if (strpos($user->url, '://') === false) {
219 $url = 'http://'. $url;
220 }
221 $webpageurl = new moodle_url($url);
6592a119
SL
222 $node = new core_user\output\myprofile\node('contact', 'webpage', get_string('webpage'), null, null,
223 html_writer::link($url, $webpageurl));
b19cc4ef
AA
224 $tree->add_node($node);
225 }
226
08cb34a5 227 // Printing tagged interests. We want this only for full profile.
c4e868d5
MG
228 if (empty($course) && ($interests = core_tag_tag::get_item_tags('core', 'user', $user->id))) {
229 $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null,
230 $OUTPUT->tag_list($interests, ''));
231 $tree->add_node($node);
08cb34a5
FM
232 }
233
a009aad4 234 if ($iscurrentuser || !isset($hiddenfields['mycourses'])) {
b19cc4ef 235 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
04985346 236 if ($mycourses = enrol_get_all_users_courses($user->id, true, null)) {
b19cc4ef
AA
237 $shown = 0;
238 $courselisting = html_writer::start_tag('ul');
239 foreach ($mycourses as $mycourse) {
240 if ($mycourse->category) {
241 context_helper::preload_from_record($mycourse);
242 $ccontext = context_course::instance($mycourse->id);
243 if (!isset($course) || $mycourse->id != $course->id) {
244 $linkattributes = null;
245 if ($mycourse->visible == 0) {
246 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
247 continue;
248 }
249 $linkattributes['class'] = 'dimmed';
250 }
251 $params = array('id' => $user->id, 'course' => $mycourse->id);
252 if ($showallcourses) {
253 $params['showallcourses'] = 1;
254 }
255 $url = new moodle_url('/user/view.php', $params);
256 $courselisting .= html_writer::tag('li', html_writer::link($url, $ccontext->get_context_name(false),
257 $linkattributes));
258 } else {
c6745991 259 $courselisting .= html_writer::tag('li', $ccontext->get_context_name(false));
b19cc4ef
AA
260 }
261 }
262 $shown++;
263 if (!$showallcourses && $shown == $CFG->navcourselimit) {
264 $url = null;
265 if (isset($course)) {
266 $url = new moodle_url('/user/view.php',
267 array('id' => $user->id, 'course' => $course->id, 'showallcourses' => 1));
268 } else {
269 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
270 }
5a3fdd33 271 $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'),
1da2e3a6 272 array('title' => get_string('viewmore'))), array('class' => 'viewmore'));
b19cc4ef
AA
273 break;
274 }
275 }
276 $courselisting .= html_writer::end_tag('ul');
277 if (!empty($mycourses)) {
278 // Add this node only if there are courses to display.
279 $node = new core_user\output\myprofile\node('coursedetails', 'courseprofiles',
280 get_string('courseprofiles'), null, null, rtrim($courselisting, ', '));
281 $tree->add_node($node);
282 }
283 }
284 }
285
286 if (!empty($course)) {
287
288 // Show roles in this course.
289 if ($rolestring = get_user_roles_in_course($user->id, $course->id)) {
290 $node = new core_user\output\myprofile\node('coursedetails', 'roles', get_string('roles'), null, null, $rolestring);
291 $tree->add_node($node);
292 }
293
294 // Show groups this user is in.
295 if (!isset($hiddenfields['groups']) && !empty($course)) {
8290c31c 296 $accessallgroups = has_capability('moodle/site:accessallgroups', $courseorsystemcontext);
b19cc4ef
AA
297 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
298 $groupstr = '';
299 foreach ($usergroups as $group) {
300 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
301 if (!groups_is_member($group->id, $user->id)) {
302 continue;
303 }
304 }
305
306 if ($course->groupmode != NOGROUPS) {
307 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'
308 .format_string($group->name).'</a>,';
309 } else {
310 // The user/index.php shows groups only when course in group mode.
311 $groupstr .= ' '.format_string($group->name);
312 }
313 }
314 if ($groupstr !== '') {
315 $node = new core_user\output\myprofile\node('coursedetails', 'groups',
316 get_string('group'), null, null, rtrim($groupstr, ', '));
317 $tree->add_node($node);
318 }
319 }
320 }
321
322 if (!isset($hiddenfields['suspended'])) {
323 if ($user->suspended) {
324 $node = new core_user\output\myprofile\node('coursedetails', 'suspended',
325 null, null, null, get_string('suspended', 'auth'));
326 $tree->add_node($node);
327 }
328 }
b19cc4ef
AA
329 }
330
331 if ($user->icq && !isset($hiddenfields['icqnumber'])) {
10768bbd
SM
332 $imurl = new moodle_url('https://web.icq.com/wwp', array('uin' => $user->icq) );
333 $iconurl = new moodle_url('https://web.icq.com/whitepages/online', array('icq' => $user->icq, 'img' => '5'));
b19cc4ef
AA
334 $statusicon = html_writer::tag('img', '',
335 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
336 $node = new core_user\output\myprofile\node('contact', 'icqnumber', get_string('icqnumber'), null, null,
337 html_writer::link($imurl, s($user->icq) . $statusicon));
338 $tree->add_node($node);
339 }
340
341 if ($user->skype && !isset($hiddenfields['skypeid'])) {
342 $imurl = 'skype:'.urlencode($user->skype).'?call';
343 $iconurl = new moodle_url('http://mystatus.skype.com/smallicon/'.urlencode($user->skype));
344 if (is_https()) {
345 // Bad luck, skype devs are lazy to set up SSL on their servers - see MDL-37233.
346 $statusicon = '';
347 } else {
348 $statusicon = html_writer::empty_tag('img',
349 array('src' => $iconurl, 'class' => 'icon icon-post', 'alt' => get_string('status')));
350 }
351
352 $node = new core_user\output\myprofile\node('contact', 'skypeid', get_string('skypeid'), null, null,
353 html_writer::link($imurl, s($user->skype) . $statusicon));
354 $tree->add_node($node);
355 }
356 if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
10768bbd 357 $imurl = new moodle_url('https://edit.yahoo.com/config/send_webmesg', array('.target' => $user->yahoo, '.src' => 'pg'));
b19cc4ef
AA
358 $iconurl = new moodle_url('http://opi.yahoo.com/online', array('u' => $user->yahoo, 'm' => 'g', 't' => '0'));
359 $statusicon = html_writer::tag('img', '',
360 array('src' => $iconurl, 'class' => 'iconsmall icon-post', 'alt' => get_string('status')));
361
362 $node = new core_user\output\myprofile\node('contact', 'yahooid', get_string('yahooid'), null, null,
363 html_writer::link($imurl, s($user->yahoo) . $statusicon));
364 $tree->add_node($node);
365 }
366 if ($user->aim && !isset($hiddenfields['aimid'])) {
367 $imurl = 'aim:goim?screenname='.urlencode($user->aim);
368 $node = new core_user\output\myprofile\node('contact', 'aimid', get_string('aimid'), null, null,
369 html_writer::link($imurl, s($user->aim)));
370 $tree->add_node($node);
371 }
372 if ($user->msn && !isset($hiddenfields['msnid'])) {
373 $node = new core_user\output\myprofile\node('contact', 'msnid', get_string('msnid'), null, null,
374 s($user->msn));
375 $tree->add_node($node);
376 }
377
860f59b1
MC
378 $categories = profile_get_user_fields_with_data_by_category($user->id);
379 foreach ($categories as $categoryid => $fields) {
b47fda71 380 foreach ($fields as $formfield) {
860f59b1
MC
381 if ($formfield->is_visible() and !$formfield->is_empty()) {
382 $node = new core_user\output\myprofile\node('contact', 'custom_field_' . $formfield->field->shortname,
383 format_string($formfield->field->name), null, null, $formfield->display_data());
384 $tree->add_node($node);
b19cc4ef
AA
385 }
386 }
387 }
388
389 // First access. (Why only for sites ?)
390 if (!isset($hiddenfields['firstaccess']) && empty($course)) {
391 if ($user->firstaccess) {
392 $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
393 } else {
394 $datestring = get_string("never");
395 }
f13a9113 396 $node = new core_user\output\myprofile\node('loginactivity', 'firstaccess', get_string('firstsiteaccess'), null, null,
b19cc4ef
AA
397 $datestring);
398 $tree->add_node($node);
399 }
400
401 // Last access.
402 if (!isset($hiddenfields['lastaccess'])) {
403 if (empty($course)) {
404 $string = get_string('lastsiteaccess');
405 if ($user->lastaccess) {
406 $datestring = userdate($user->lastaccess) . "&nbsp; (" . format_time(time() - $user->lastaccess) . ")";
407 } else {
408 $datestring = get_string("never");
409 }
410 } else {
411 $string = get_string('lastcourseaccess');
412 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
413 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
414 } else {
415 $datestring = get_string("never");
416 }
417 }
418
f13a9113 419 $node = new core_user\output\myprofile\node('loginactivity', 'lastaccess', $string, null, null,
b19cc4ef
AA
420 $datestring);
421 $tree->add_node($node);
422 }
423
424 // Last ip.
425 if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
426 if ($user->lastip) {
546bc40c 427 $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $user->id));
b19cc4ef
AA
428 $ipstring = html_writer::link($iplookupurl, $user->lastip);
429 } else {
430 $ipstring = get_string("none");
431 }
f13a9113 432 $node = new core_user\output\myprofile\node('loginactivity', 'lastip', get_string('lastip'), null, null,
b19cc4ef
AA
433 $ipstring);
434 $tree->add_node($node);
435 }
436}