-
Notifications
You must be signed in to change notification settings - Fork 1
/
locallib.php
125 lines (104 loc) · 4.91 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Global support functions for the UCSF theme.
*
* @package theme_ucsf
* @copyright The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Build the course related hints HTML code.
* This function evaluates and composes all course related hints which may appear on a course page below the course header.
*
* @copyright based on code from theme_boost_union by Alexander Bias, and from theme_boost_campus by Kathrin Osswald.
*
* @return string
*/
function theme_ucsf_get_course_related_hints(): string {
global $CFG, $COURSE, $PAGE, $USER, $OUTPUT;
// Require user library.
require_once($CFG->dirroot.'/user/lib.php');
// Initialize HTML code.
$html = '';
// If the visibility of the course is hidden, a hint for the visibility will be shown.
if ($PAGE->has_set_url()
&& $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)
&& $COURSE->visible == false) {
// Prepare template context.
$templatecontext = ['courseid' => $COURSE->id];
// If the user has the capability to change the course settings, an additional link to the course settings is shown.
if (has_capability('moodle/course:update', context_course::instance($COURSE->id))) {
$templatecontext['showcoursesettingslink'] = true;
} else {
$templatecontext['showcoursesettingslink'] = false;
}
// Render template and add it to HTML code.
$html .= $OUTPUT->render_from_template('theme_ucsf/course-hint-hidden', $templatecontext);
}
// If the user is accessing the course with guest access, a hint for users is shown.
// We also check that the user did not switch the role. This is a special case for roles that can fully access the course
// without being enrolled. A role switch would show the guest access hint additionally in that case and this is not
// intended.
if (is_guest(\context_course::instance($COURSE->id), $USER->id)
&& $PAGE->has_set_url()
&& $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)
&& !is_role_switched($COURSE->id)) {
// Require self enrolment library.
require_once($CFG->dirroot . '/enrol/self/lib.php');
// Prepare template context.
$templatecontext = ['courseid' => $COURSE->id,
'role' => role_get_name(get_guest_role())];
// Search for an available self enrolment link in this course.
$templatecontext['showselfenrollink'] = false;
$instances = enrol_get_instances($COURSE->id, true);
$plugins = enrol_get_plugins(true);
foreach ($instances as $instance) {
// If the enrolment plugin isn't enabled currently, skip it.
if (!isset($plugins[$instance->enrol])) {
continue;
}
// Remember the enrolment plugin.
$plugin = $plugins[$instance->enrol];
// If there is a self enrolment link.
if ($plugin->show_enrolme_link($instance)) {
$templatecontext['showselfenrollink'] = true;
break;
}
}
// Render template and add it to HTML code.
$html .= $OUTPUT->render_from_template('theme_ucsf/course-hint-guestaccess', $templatecontext);
}
// If the user has switched his role, a hint for the role switch will be shown.
if (is_role_switched($COURSE->id) ) {
// Get the role name switched to.
$opts = \user_get_user_navigation_info($USER, $PAGE);
$role = $opts->metadata['rolename'];
// Get the URL to switch back (normal role).
$url = new moodle_url('/course/switchrole.php',
['id' => $COURSE->id,
'sesskey' => sesskey(),
'switchrole' => 0,
'returnurl' => $PAGE->url->out_as_local_url(false), ]);
// Prepare template context.
$templatecontext = ['role' => $role,
'url' => $url->out()];
// Render template and add it to HTML code.
$html .= $OUTPUT->render_from_template('theme_ucsf/course-hint-switchedrole', $templatecontext);
}
// Return HTML code.
return $html;
}