-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlocallib.php
299 lines (270 loc) · 10.5 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?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/>.
/**
* Helper functions for lw_courses block
*
* @package block_lw_courses
* @copyright 2012 Adam Olley <[email protected]>
* @copyright 2017 Mathew May <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
define('BLOCKS_LW_COURSES_SHOWCATEGORIES_NONE', '0');
define('BLOCKS_LW_COURSES_SHOWCATEGORIES_ONLY_PARENT_NAME', '1');
define('BLOCKS_LW_COURSES_SHOWCATEGORIES_FULL_PATH', '2');
define('BLOCKS_LW_COURSES_IMAGEASBACKGROUND_FALSE', '0');
define('BLOCKS_LW_COURSES_SHOWGRADES_NO', '0');
define('BLOCKS_LW_COURSES_SHOWGRADES_YES', '1');
define('BLOCKS_LW_COURSES_STARTGRID_NO', '0');
define('BLOCKS_LW_COURSES_STARTGRID_YES', '1');
define('BLOCKS_LW_COURSES_DEFAULT_COURSES_ROW', '4');
define('BLOCKS_LW_COURSES_DEFAULT_COL_SIZE', '3');
define('BLOCKS_LW_COURSES_SHOWTEACHERS_NO', '0');
define('BLOCKS_LW_COURSES_SHOWTEACHERS_YES', '1');
require_once($CFG->libdir . '/completionlib.php');
use core_completion\progress;
/**
* Display overview for courses
*
* @param array $courses courses for which overview needs to be shown
* @return array html overview
*/
function block_lw_courses_get_overviews($courses) {
$htmlarray = array();
if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
// Split courses list into batches with no more than MAX_MODINFO_CACHE_SIZE courses in one batch.
// Otherwise we exceed the cache limit in get_fast_modinfo() and rebuild it too often.
if (defined('MAX_MODINFO_CACHE_SIZE') && MAX_MODINFO_CACHE_SIZE > 0 && count($courses) > MAX_MODINFO_CACHE_SIZE) {
$batches = array_chunk($courses, MAX_MODINFO_CACHE_SIZE, true);
} else {
$batches = array($courses);
}
foreach ($batches as $courses) {
foreach ($modules as $fname) {
$fname($courses, $htmlarray);
}
}
}
return $htmlarray;
}
/**
* Sets user preference for maximum courses to be displayed in lw_courses block
*
* @param int $number maximum courses which should be visible
*/
function block_lw_courses_update_mynumber($number) {
set_user_preference('lw_courses_number_of_courses', $number);
}
/**
* Sets user course sorting preference in lw_courses block
*
* @param array $sortorder list of course ids
*/
function block_lw_courses_update_myorder($sortorder) {
$value = implode(',', $sortorder);
if (core_text::strlen($value) > 1333) {
// The value won't fit into the user preference. Remove courses in the end of the list
// (mostly likely user won't even notice).
$value = preg_replace('/,[\d]*$/', '', core_text::substr($value, 0, 1334));
}
set_user_preference('lw_courses_course_sortorder', $value);
}
/**
* Gets user course sorting preference in lw_courses block
*
* @return array list of course ids
*/
function block_lw_courses_get_myorder() {
if ($value = get_user_preferences('lw_courses_course_sortorder')) {
return explode(',', $value);
}
// If preference was not found, look in the old location and convert if found.
$order = array();
if ($value = get_user_preferences('lw_courses_course_order')) {
$order = unserialize_array($value);
block_lw_courses_update_myorder($order);
unset_user_preference('lw_courses_course_order');
}
return $order;
}
/**
* Returns shortname of activities in course
*
* @param int $courseid id of course for which activity shortname is needed
* @return string|bool list of child shortname
*/
function block_lw_courses_get_child_shortnames($courseid) {
global $DB;
$ctxselect = context_helper::get_preload_record_columns_sql('ctx');
$sql = "SELECT c.id, c.shortname, $ctxselect
FROM {enrol} e
JOIN {course} c ON (c.id = e.customint1)
JOIN {context} ctx ON (ctx.instanceid = e.customint1)
WHERE e.courseid = :courseid AND e.enrol = :method AND ctx.contextlevel = :contextlevel ORDER BY e.sortorder";
$params = array('method' => 'meta', 'courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE);
if ($results = $DB->get_records_sql($sql, $params)) {
$shortnames = array();
// Preload the context we will need it to format the category name shortly.
foreach ($results as $res) {
context_helper::preload_from_record($res);
$context = context_course::instance($res->id);
$shortnames[] = format_string($res->shortname, true, $context);
}
$total = count($shortnames);
$suffix = '';
if ($total > 10) {
$shortnames = array_slice($shortnames, 0, 10);
$diff = $total - count($shortnames);
if ($diff > 1) {
$suffix = get_string('shortnamesufixprural', 'block_lw_courses', $diff);
} else {
$suffix = get_string('shortnamesufixsingular', 'block_lw_courses', $diff);
}
}
$shortnames = get_string('shortnameprefix', 'block_lw_courses', implode('; ', $shortnames));
$shortnames .= $suffix;
}
return isset($shortnames) ? $shortnames : false;
}
/**
* Returns maximum number of courses which will be displayed in lw_courses block
*
* @param bool $showallcourses if set true all courses will be visible.
* @return int maximum number of courses
*/
function block_lw_courses_get_max_user_courses($showallcourses = false) {
// Get block configuration.
$config = get_config('block_lw_courses');
$limit = $config->defaultmaxcourses;
// If max course is not set then try get user preference.
if (empty($config->forcedefaultmaxcourses)) {
if ($showallcourses) {
$limit = 0;
} else {
$limit = get_user_preferences('lw_courses_number_of_courses', $limit);
}
}
return $limit;
}
/**
* Return sorted list of user courses
*
* @param bool $showallcourses if set true all courses will be visible.
* @return array list of sorted courses and count of courses.
*/
function block_lw_courses_get_sorted_courses($showallcourses = false) {
global $USER;
$limit = block_lw_courses_get_max_user_courses($showallcourses);
$courses = enrol_get_my_courses();
$site = get_site();
if (array_key_exists($site->id, $courses)) {
unset($courses[$site->id]);
}
foreach ($courses as $c) {
if (isset($USER->lastcourseaccess[$c->id])) {
$courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
} else {
$courses[$c->id]->lastaccess = 0;
}
}
// Get remote courses.
$remotecourses = array();
if (is_enabled_auth('mnet')) {
$remotecourses = get_my_remotecourses();
// Remote courses will have -ve remoteid as key, so it can be differentiated from normal courses.
foreach ($remotecourses as $id => $val) {
$remoteid = $val->remoteid * -1;
$val->id = $remoteid;
$courses[$remoteid] = $val;
}
}
$order = block_lw_courses_get_myorder();
$sortedcourses = array();
$counter = 0;
// Get courses in sort order into list.
foreach ($order as $key => $cid) {
if (($counter >= $limit) && ($limit != 0)) {
break;
}
// Make sure user is still enrolled.
if (isset($courses[$cid])) {
$sortedcourses[$cid] = $courses[$cid];
$counter++;
}
}
// Append unsorted courses if limit allows.
foreach ($courses as $c) {
if (($limit != 0) && ($counter >= $limit)) {
break;
}
if (!in_array($c->id, $order)) {
$sortedcourses[$c->id] = $c;
$counter++;
}
}
return array($sortedcourses, count($courses));
}
// Custom LearningWorks functions.
/**
* Build the Image url
*
* @param string $fileorfilename Name of the image
* @return moodle_url|string
*/
function block_lw_courses_get_course_image_url($fileorfilename) {
// If the fileorfilename param is a file.
if ($fileorfilename instanceof stored_file) {
// Separate each component of the url.
$filecontextid = $fileorfilename->get_contextid();
$filecomponent = $fileorfilename->get_component();
$filearea = $fileorfilename->get_filearea();
$filepath = $fileorfilename->get_filepath();
$filename = $fileorfilename->get_filename();
// Generate a moodle url to the file.
$url = new moodle_url("/pluginfile.php/{$filecontextid}/{$filecomponent}/{$filearea}/{$filepath}/{$filename}");
// Return an img element containing the file.
return html_writer::empty_tag('img', array('src' => $url));
}
// The fileorfilename param is not a stored_file object, assume this is the name of the file in the blocks file area.
// Generate a moodle url to the file in the blocks file area.
return new moodle_url("/pluginfile.php/1/block_lw_courses/courseimagedefault{$fileorfilename}");
}
/**
* The course progress builder
*
* @param object $course The course whose progress we want
* @return string
*/
function block_lw_courses_build_progress($course) {
global $CFG;
require_once($CFG->dirroot.'/grade/querylib.php');
require_once($CFG->dirroot.'/grade/lib.php');
$config = get_config('block_lw_courses');
$completestring = get_string('complete');
if ($config->progressenabled == BLOCKS_LW_COURSES_SHOWGRADES_NO) {
return '';
}
$percentage = progress::get_course_progress_percentage($course);
if (!is_null($percentage)) {
$percentage = floor($percentage);
} else {
$percentage = 0;
}
$bar = html_writer::div('', 'value', array('aria-valuenow' => "$percentage",
'aria-valuemin' => "0", 'aria-valuemax' => "100", 'style' => "width:$percentage%"));
$progress = html_writer::div($bar, 'progress', array('data-label' => "$percentage% $completestring"));
return $progress;
}