Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
25
defined('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
function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
36
    global $CFG, $USER, $DB, $PAGE, $OUTPUT;
37
 
38
    $usercontext = context_user::instance($user->id, MUST_EXIST);
39
    $systemcontext = context_system::instance();
40
    $courseorusercontext = !empty($course) ? context_course::instance($course->id) : $usercontext;
41
    $courseorsystemcontext = !empty($course) ? context_course::instance($course->id) : $systemcontext;
42
    $courseid = !empty($course) ? $course->id : SITEID;
43
 
44
    $contactcategory = new core_user\output\myprofile\category('contact', get_string('userdetails'));
45
    // No after property specified intentionally. It is a hack to make administration block appear towards the end. Refer MDL-49928.
46
    $coursedetailscategory = new core_user\output\myprofile\category('coursedetails', get_string('coursedetails'));
47
    $miscategory = new core_user\output\myprofile\category('miscellaneous', get_string('miscellaneous'), 'coursedetails');
48
    $reportcategory = new core_user\output\myprofile\category('reports', get_string('reports'), 'miscellaneous');
49
    $admincategory = new core_user\output\myprofile\category('administration', get_string('administration'), 'reports');
50
    $loginactivitycategory = new core_user\output\myprofile\category('loginactivity', get_string('loginactivity'), 'administration');
51
 
52
    // Add categories.
53
    $tree->add_category($contactcategory);
54
    $tree->add_category($coursedetailscategory);
55
    $tree->add_category($miscategory);
56
    $tree->add_category($reportcategory);
57
    $tree->add_category($admincategory);
58
    $tree->add_category($loginactivitycategory);
59
 
60
    // Add core nodes.
61
    // Full profile node.
62
    if (!empty($course)) {
63
        if (user_can_view_profile($user, null, $usercontext)) {
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
                'returnto' => 'profile'));
76
            $node = new core_user\output\myprofile\node('contact', 'editprofile', get_string('editmyprofile'), null, $url,
77
                null, null, 'editprofile');
78
            $tree->add_node($node);
79
        } else if ((has_capability('moodle/user:editprofile', $usercontext) && !is_siteadmin($user))
80
                   || ($iscurrentuser && has_capability('moodle/user:editownprofile', $systemcontext))) {
81
            $userauthplugin = false;
82
            if (!empty($user->auth)) {
83
                $userauthplugin = get_auth_plugin($user->auth);
84
            }
85
            if ($userauthplugin && $userauthplugin->can_edit_profile()) {
86
                $url = $userauthplugin->edit_profile_url();
87
                if (empty($url)) {
88
                    if (empty($course)) {
89
                        $url = new moodle_url('/user/edit.php', array('id' => $user->id, 'returnto' => 'profile'));
90
                    } else {
91
                        $url = new moodle_url('/user/edit.php', array('id' => $user->id, 'course' => $course->id,
92
                            'returnto' => 'profile'));
93
                    }
94
                }
95
                $node = new core_user\output\myprofile\node('contact', 'editprofile',
96
                        get_string('editmyprofile'), null, $url, null, null, 'editprofile');
97
                $tree->add_node($node);
98
            }
99
        }
100
    }
101
 
102
    // Preference page.
103
    if (!$iscurrentuser && $PAGE->settingsnav->can_view_user_preferences($user->id)) {
104
        $url = new moodle_url('/user/preferences.php', array('userid' => $user->id));
105
        $title = get_string('preferences', 'moodle');
106
        $node = new core_user\output\myprofile\node('administration', 'preferences', $title, null, $url);
107
        $tree->add_node($node);
108
    }
109
 
110
    // Login as ...
1441 ariadna 111
    if (!empty(\core\session\loginas_helper::get_context_user_can_login_as($USER, $user, $course))) {
1 efrain 112
        $url = new moodle_url('/course/loginas.php',
113
                array('id' => $courseid, 'user' => $user->id, 'sesskey' => sesskey()));
114
        $node = new  core_user\output\myprofile\node('administration', 'loginas', get_string('loginas'), null, $url);
115
        $tree->add_node($node);
116
    }
117
 
118
    // Contact details.
119
    if (has_capability('moodle/user:viewhiddendetails', $courseorusercontext)) {
120
        $hiddenfields = array();
121
    } else {
122
        $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
123
    }
124
    // TODO Does not support custom user profile fields (MDL-70456).
125
    $identityfields = array_flip(\core_user\fields::get_identity_fields($courseorusercontext, false));
126
 
127
    if (is_mnet_remote_user($user)) {
128
        $sql = "SELECT h.id, h.name, h.wwwroot,
129
                       a.name as application, a.display_name
130
                  FROM {mnet_host} h, {mnet_application} a
131
                 WHERE h.id = ? AND h.applicationid = a.id";
132
 
133
        $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
134
        $remoteuser = new stdclass();
135
        $remoteuser->remotetype = $remotehost->display_name;
136
        $hostinfo = new stdclass();
137
        $hostinfo->remotename = $remotehost->name;
138
        $hostinfo->remoteurl  = $remotehost->wwwroot;
139
 
140
        $node = new core_user\output\myprofile\node('contact', 'mnet', get_string('remoteuser', 'mnet', $remoteuser), null, null,
141
            get_string('remoteuserinfo', 'mnet', $hostinfo), null, 'remoteuserinfo');
142
        $tree->add_node($node);
143
    }
144
 
145
    if ($iscurrentuser
146
        or (!isset($hiddenfields['email']) and (
147
            $user->maildisplay == core_user::MAILDISPLAY_EVERYONE
148
            or ($user->maildisplay == core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY and enrol_sharing_course($user, $USER))
149
            or has_capability('moodle/course:useremail', $courseorusercontext) // TODO: Deprecate/remove for MDL-37479.
150
        ))
151
        or (isset($identityfields['email']))
152
       ) {
153
        $maildisplay = obfuscate_mailto($user->email, '');
154
        if ($iscurrentuser) {
155
            if ($user->maildisplay == core_user::MAILDISPLAY_EVERYONE) {
156
                $maildisplay .= ' ' . get_string('emaildisplayeveryone');
157
            } else if ($user->maildisplay == core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY) {
158
                $maildisplay .= ' ' . get_string('emaildisplaycoursemembersonly');
159
            } else {
160
                $maildisplay .= ' ' . get_string('emaildisplayhide');
161
            }
162
        }
163
        $node = new core_user\output\myprofile\node('contact', 'email', get_string('email'),
164
            null, null, $maildisplay);
165
        $tree->add_node($node);
166
    }
167
 
168
    if (!isset($hiddenfields['moodlenetprofile']) && $user->moodlenetprofile) {
169
        $node = new core_user\output\myprofile\node('contact', 'moodlenetprofile', get_string('moodlenetprofile', 'user'), null,
170
                null, $user->moodlenetprofile);
171
        $tree->add_node($node);
172
    }
173
 
174
    if (!isset($hiddenfields['country']) && $user->country) {
175
        $node = new core_user\output\myprofile\node('contact', 'country', get_string('country'), null, null,
176
                get_string($user->country, 'countries'));
177
        $tree->add_node($node);
178
    }
179
 
180
    if (!isset($hiddenfields['city']) && $user->city) {
181
        $node = new core_user\output\myprofile\node('contact', 'city', get_string('city'), null, null, $user->city);
182
        $tree->add_node($node);
183
    }
184
 
185
    if (!isset($hiddenfields['timezone'])) {
186
        $node = new core_user\output\myprofile\node('contact', 'timezone', get_string('timezone'), null, null,
187
            core_date::get_user_timezone($user));
188
        $tree->add_node($node);
189
    }
190
 
191
    if (isset($identityfields['address']) && $user->address) {
192
        $node = new core_user\output\myprofile\node('contact', 'address', get_string('address'), null, null, $user->address);
193
        $tree->add_node($node);
194
    }
195
 
196
    if (isset($identityfields['phone1']) && $user->phone1) {
197
        $node = new core_user\output\myprofile\node('contact', 'phone1', get_string('phone1'), null, null, $user->phone1);
198
        $tree->add_node($node);
199
    }
200
 
201
    if (isset($identityfields['phone2']) && $user->phone2) {
202
        $node = new core_user\output\myprofile\node('contact', 'phone2', get_string('phone2'), null, null, $user->phone2);
203
        $tree->add_node($node);
204
    }
205
 
206
    if (isset($identityfields['institution']) && $user->institution) {
207
        $node = new core_user\output\myprofile\node('contact', 'institution', get_string('institution'), null, null,
208
                $user->institution);
209
        $tree->add_node($node);
210
    }
211
 
212
    if (isset($identityfields['department']) && $user->department) {
213
        $node = new core_user\output\myprofile\node('contact', 'department', get_string('department'), null, null,
214
            $user->department);
215
        $tree->add_node($node);
216
    }
217
 
218
    if (isset($identityfields['idnumber']) && $user->idnumber) {
219
        $node = new core_user\output\myprofile\node('contact', 'idnumber', get_string('idnumber'), null, null,
220
            s($user->idnumber));
221
        $tree->add_node($node);
222
    }
223
 
224
    // Printing tagged interests. We want this only for full profile.
225
    if (empty($course) && ($interests = core_tag_tag::get_item_tags('core', 'user', $user->id))) {
226
        $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null,
227
                $OUTPUT->tag_list($interests, ''));
228
        $tree->add_node($node);
229
    }
230
 
231
    if ($iscurrentuser || !isset($hiddenfields['mycourses'])) {
232
        $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
233
        if ($mycourses = enrol_get_all_users_courses($user->id, true, null)) {
234
            $shown = 0;
235
            $courselisting = html_writer::start_tag('ul');
236
            foreach ($mycourses as $mycourse) {
237
                if ($mycourse->category) {
238
                    context_helper::preload_from_record($mycourse);
239
                    $ccontext = context_course::instance($mycourse->id);
240
                    if (!isset($course) || $mycourse->id != $course->id) {
241
                        $linkattributes = null;
242
                        if ($mycourse->visible == 0) {
243
                            if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
244
                                continue;
245
                            }
246
                            $linkattributes['class'] = 'dimmed';
247
                        }
248
                        $params = array('id' => $user->id, 'course' => $mycourse->id);
249
                        if ($showallcourses) {
250
                            $params['showallcourses'] = 1;
251
                        }
252
                        $url = new moodle_url('/user/view.php', $params);
253
                        $courselisting .= html_writer::tag('li', html_writer::link($url, $ccontext->get_context_name(false),
254
                                $linkattributes));
255
                    } else {
256
                        $courselisting .= html_writer::tag('li', $ccontext->get_context_name(false));
257
                    }
258
                }
259
                $shown++;
260
                if (!$showallcourses && $shown == $CFG->navcourselimit) {
261
                    $url = null;
262
                    if (isset($course)) {
263
                        $url = new moodle_url('/user/view.php',
264
                                array('id' => $user->id, 'course' => $course->id, 'showallcourses' => 1));
265
                    } else {
266
                        $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
267
                    }
268
                    $courselisting .= html_writer::tag('li', html_writer::link($url, get_string('viewmore'),
269
                            array('title' => get_string('viewmore'))), array('class' => 'viewmore'));
270
                    break;
271
                }
272
            }
273
            $courselisting .= html_writer::end_tag('ul');
274
            if (!empty($mycourses)) {
275
                // Add this node only if there are courses to display.
276
                $node = new core_user\output\myprofile\node('coursedetails', 'courseprofiles',
277
                    get_string('courseprofiles'), null, null, rtrim($courselisting, ', '));
278
                $tree->add_node($node);
279
            }
280
        }
281
    }
282
 
283
    if (!empty($course)) {
284
 
285
        // Show roles in this course.
286
        if ($rolestring = get_user_roles_in_course($user->id, $course->id)) {
287
            $node = new core_user\output\myprofile\node('coursedetails', 'roles', get_string('roles'), null, null, $rolestring);
288
            $tree->add_node($node);
289
        }
290
 
291
        // Show groups this user is in.
292
        if (!isset($hiddenfields['groups']) && !empty($course)) {
293
            $accessallgroups = has_capability('moodle/site:accessallgroups', $courseorsystemcontext);
294
            if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
295
                $groupstr = '';
296
                foreach ($usergroups as $group) {
297
                    if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
298
                        // In separate groups mode, I only have to see the groups shared between both users.
299
                        if (!groups_is_member($group->id, $USER->id)) {
300
                            continue;
301
                        }
302
                    }
303
 
304
                    if ($course->groupmode != NOGROUPS) {
305
                        $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'
306
                                     .format_string($group->name).'</a>,';
307
                    } else {
308
                        // The user/index.php shows groups only when course in group mode.
309
                        $groupstr .= ' '.format_string($group->name);
310
                    }
311
                }
312
                if ($groupstr !== '') {
313
                    $node = new core_user\output\myprofile\node('coursedetails', 'groups',
314
                            get_string('group'), null, null, rtrim($groupstr, ', '));
315
                    $tree->add_node($node);
316
                }
317
            }
318
        }
319
 
320
        if (!isset($hiddenfields['suspended'])) {
321
            if ($user->suspended) {
322
                $node = new core_user\output\myprofile\node('coursedetails', 'suspended',
323
                        null, null, null, get_string('suspended', 'auth'));
324
                $tree->add_node($node);
325
            }
326
        }
327
    }
328
 
329
    $categories = profile_get_user_fields_with_data_by_category($user->id);
330
    foreach ($categories as $categoryid => $fields) {
331
        foreach ($fields as $formfield) {
332
            if ($formfield->show_field_content()) {
333
                $node = new core_user\output\myprofile\node('contact', 'custom_field_' . $formfield->field->shortname,
1441 ariadna 334
                    $formfield->display_name(), null, null, $formfield->display_data());
1 efrain 335
                $tree->add_node($node);
336
            }
337
        }
338
    }
339
 
340
    // First access. (Why only for sites ?)
341
    if (!isset($hiddenfields['firstaccess']) && empty($course)) {
342
        if ($user->firstaccess) {
343
            $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
344
        } else {
345
            $datestring = get_string("never");
346
        }
347
        $node = new core_user\output\myprofile\node('loginactivity', 'firstaccess', get_string('firstsiteaccess'), null, null,
348
            $datestring);
349
        $tree->add_node($node);
350
    }
351
 
352
    // Last access.
353
    if (!isset($hiddenfields['lastaccess'])) {
354
        if (empty($course)) {
355
            $string = get_string('lastsiteaccess');
356
            if ($user->lastaccess) {
357
                $datestring = userdate($user->lastaccess) . "&nbsp; (" . format_time(time() - $user->lastaccess) . ")";
358
            } else {
359
                $datestring = get_string("never");
360
            }
361
        } else {
362
            $string = get_string('lastcourseaccess');
363
            if ($lastaccess = $DB->get_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course->id))) {
364
                $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
365
            } else {
366
                $datestring = get_string("never");
367
            }
368
        }
369
 
370
        $node = new core_user\output\myprofile\node('loginactivity', 'lastaccess', $string, null, null,
371
            $datestring);
372
        $tree->add_node($node);
373
    }
374
 
375
    // Last ip.
376
    if (has_capability('moodle/user:viewlastip', $usercontext) && !isset($hiddenfields['lastip'])) {
377
        if ($user->lastip) {
378
            $iplookupurl = new moodle_url('/iplookup/index.php', array('ip' => $user->lastip, 'user' => $user->id));
379
            $ipstring = html_writer::link($iplookupurl, $user->lastip);
380
        } else {
381
            $ipstring = get_string("none");
382
        }
383
        $node = new core_user\output\myprofile\node('loginactivity', 'lastip', get_string('lastip'), null, null,
384
            $ipstring);
385
        $tree->add_node($node);
386
    }
387
}