-
Notifications
You must be signed in to change notification settings - Fork 19
/
locallib.php
276 lines (246 loc) · 9.04 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?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/>.
/**
* Lib file.
*
* @package theme_trema
* @copyright 2022-2024 Trema - {@link https://trema.tech/}
* @copyright 2023-2024 TNG Consulting Inc. - {@link https://www.tngconsulting.ca/}
* @author Rodrigo Mady
* @author Trevor Furtado
* @author Michael Milette
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Return a array of objects containing all cards settings.
*
* @return array of objects
*/
function theme_trema_get_cards_settings() {
$theme = theme_config::load('trema');
$cardssettings = [];
$numberofcards = get_config('theme_trema', 'numberofcards');
if (get_config('theme_trema', 'frontpageenablecards') && $numberofcards > 1) {
for ($i = 1; $i <= $numberofcards; $i++) {
$cardsettings = new stdClass();
$cardsettings->cardicon = $theme->settings->{'cardicon' . $i};
$cardsettings->cardiconcolor = $theme->settings->{'cardiconcolor' . $i};
$cardsettings->cardtitle = format_text($theme->settings->{'cardtitle' . $i}, FORMAT_HTML);
$cardsettings->cardsubtitle = format_text($theme->settings->{'cardsubtitle' . $i}, FORMAT_HTML);
$cardsettings->cardlink = $theme->settings->{'cardlink' . $i};
$cardssettings[] = $cardsettings;
}
return $cardssettings;
} else {
return false;
}
}
/**
* Get the disk usage - Cached.
*
* @return string disk usage plus unit
*/
function theme_trema_get_disk_usage() {
global $CFG;
$bytes = @disk_free_space($CFG->dataroot);
if ($bytes === false || $bytes < 0 || is_null($bytes) || $bytes > 1.0E+26) {
// If invalid number of bytes, or value is more than about 84,703.29 Yottabyte (YB), assume it is infinite.
return '∞'; // Could not determine, assume infinite.
}
$warning = ($bytes <= 104857600); // Warning at 100 MB.
$freespace = display_size($bytes);
if (!$warning) {
$freespace = '<span class="badge badge-danger px-1">' . $freespace . '</span>';
}
return $freespace;
}
/**
* Count active courses with status 1 and startdate less than today - Cached.
*
* @return int number of active courses
*/
function theme_trema_count_active_courses() {
global $DB;
$cache = cache::make('theme_trema', 'dashboardadmin');
$activecourses = $cache->get('countactivecourses');
if (!$activecourses) {
$today = time();
$sql = "SELECT COUNT(id) FROM {course}
WHERE visible = 1 AND startdate <= :today AND (enddate > :today2 OR enddate = 0) AND format != 'site'";
$activecourses = $DB->count_records_sql($sql, ['today' => $today, 'today2' => $today]);
$cache->set('countactivecourses', $activecourses);
}
return $activecourses;
}
/**
* Count all courses - Cached.
*
* @return int number of all courses
*/
function theme_trema_count_courses() {
global $DB;
$cache = cache::make('theme_trema', 'dashboardadmin');
$courses = $cache->get('courses');
if (!$courses) {
$courses = $DB->count_records('course') - 1; // Delete course site.
$cache->set('courses', $courses);
}
return $courses;
}
/**
* Get active courses with status 1 and startdate less than today - Cached.
*
* @return int number of active courses
*/
function theme_trema_get_active_courses() {
global $DB;
$cache = cache::make('theme_trema', 'dashboardadmin');
$activecourses = $cache->get('activecourses');
if (!$activecourses) {
$today = time();
$sql = "SELECT id FROM {course}
WHERE visible = 1 AND startdate <= :today AND (enddate > :today2 OR enddate = 0) AND format != 'site'";
$activecourses = $DB->get_fieldset_sql($sql, ['today' => $today, 'today2' => $today]);;
$cache->set('activecourses', $activecourses);
}
return $activecourses;
}
/**
* Get all active enrolments from actives courses - Cached.
*
* @return void
*/
function theme_trema_count_active_enrolments() {
global $DB;
$cache = cache::make('theme_trema', 'dashboardadmin');
$activeenrolments = $cache->get('activeenrolments');
if (!$activeenrolments) {
$today = time();
$activecourses = theme_trema_get_active_courses();
if ($activecourses) {
list($in, $params) = $DB->get_in_or_equal($activecourses, SQL_PARAMS_NAMED);
$params['today'] = $today;
$sql = "SELECT COUNT(1) FROM {user_enrolments} ue
INNER JOIN {enrol} e ON ue.enrolid = e.id
WHERE ue.status = 0 AND (ue.timeend >= :today OR ue.timeend = 0) AND e.courseid {$in}";
$activeenrolments = $DB->count_records_sql($sql, $params);
$cache->set('activeenrolments', $activeenrolments);
} else {
$activeenrolments = 0;
$cache->set('activeenrolments', $activeenrolments);
}
}
return $activeenrolments;
}
/**
* Get all active enrolments - Cached.
*
* @return void
*/
function theme_trema_count_users_enrolments() {
global $DB;
$cache = cache::make('theme_trema', 'dashboardadmin');
$usersenrolments = $cache->get('usersenrolments');
if (!$usersenrolments) {
$usersenrolments = $DB->count_records('user_enrolments');
$cache->set('$usersenrolments', $usersenrolments);
}
return $usersenrolments;
}
/**
* Environment issues Status - Cached.
*
* @return false|mixed
*/
function theme_trema_get_environment_issues() {
$cache = cache::make('theme_trema', 'dashboardadmin');
$environmentissues = $cache->get('environmentissues');
if (!$environmentissues) {
$issues = \core\check\manager::get_security_checks();
// Prevent warnings.
$environmentissues = [];
$environmentissues["ok"] = 0;
$environmentissues["warning"] = 0;
foreach ($issues as $issue) {
$result = $issue->get_result()->get_status();
if ($result == 'serious' || $result == 'critical' || $result == 'warning') {
$environmentissues['warning']++;
}
}
$cache->set('environmentissues', $environmentissues);
}
return $environmentissues;
}
/**
* Get the URL of files from theme settings.
*
* @param [type] $setting
* @param string $filearea
* @param Theme $theme
* @return void
*/
function theme_trema_setting_file_url($setting, $filearea, $theme) {
global $CFG;
$component = 'theme_trema';
$itemid = 0;
$filepath = !empty($theme->settings->$filearea) ? $theme->settings->$filearea : '';
if (empty($filepath)) {
return false;
}
$syscontext = context_system::instance();
$url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php", "/$syscontext->id/$component/$filearea/$itemid".$filepath);
// Now this is tricky because the we can not hardcode http or https here, lets use the relative link.
// Note: unfortunately moodle_url does not support //urls yet.
$url = preg_replace('|^https?://|i', '//', $url->out(false));
return $url;
}
/**
* MoodlePage init for adding classes to body tag.
*
* @param moodle_page $page
* @throws coding_exception
*/
function theme_trema_page_init(moodle_page $page) {
global $COURSE, $USER;
// Add admin classes.
$page->add_body_class(is_siteadmin() ? "is_siteadmin" : "not_siteadmin");
// Add module idnumber class.
if (in_array($page->pagelayout, ['incourse']) && !empty($page->cm->idnumber)) {
$page->add_body_class("idnumber-{$page->cm->idnumber}");
}
// Add role classes.
if (in_array($page->pagelayout, ['course', 'incourse'])) {
$context = context_course::instance($COURSE->id);
if (user_has_role_assignment($USER->id, 5, $context->id)) {
$page->add_body_class('is_student');
}
if (user_has_role_assignment($USER->id, 4, $context->id)) {
$page->add_body_class('is_teacher');
}
if (user_has_role_assignment($USER->id, 3, $context->id)) {
$page->add_body_class('is_editingteacher');
}
}
// Load course style by shortname from: /style/course/$shortname.css.
if ($COURSE->id > 1) {
$shortname = explode('|', $COURSE->shortname);
$shortname = trim($shortname[0]);
$coursestyle = "/style/course/{$shortname}.css";
if (file_exists($page->theme->dir.$coursestyle)) {
$page->requires->css(new moodle_url("/theme/trema{$coursestyle}"));
}
}
}