forked from ULCC/moodle-block_ilp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
315 lines (241 loc) · 9.23 KB
/
lib.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
* Library of assorted functions for the ILP
*
* @copyright © 2011 University of London Computer Centre
* @author http://www.ulcc.ac.uk, http://moodle.ulcc.ac.uk
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package ILP
* @version 2.0
*/
function var_crap($var,$header="") {
echo "<pre> {$header} <br />";
var_dump($var);
echo "</pre>";
}
/**
* Test whether the id is that of an admin user
*
* @return bool true or false
*/
function ilp_is_siteadmin($userid) {
global $CFG;
if (stripos($CFG->release,"2.") !== false) {
return is_siteadmin($userid);
} else {
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
return has_capability('moodle/site:doanything',$sitecontext);
}
}
/**
* Generates a random number for when something needs to be identified
* uniquely.
*
* @return int A random number
*/
function ilp_uniqueNum() {
return rand().time();
}
/**
* Adds a record of an action to the log
*
* @param int $course_id
* @param string $log_action the generic short name for the event
* @param string $log_url (optional, defaults to ASSMGR_LOG_URL_PREFIX)
* @param string $log_info Detailed explanation of what has happened
* @return void
*/
function ilp_add_to_log($course_id, $log_action, $log_url, $log_info) {
// prepend the url prefix if the log_url is not empty
$log_url = empty($log_url) ? '' : ILP_LOG_URL_PREFIX.'/'.$log_url;
// add to the moodle log
add_to_log($course_id, ILP_LOG_MODULE, $log_action, $log_url, $log_info);
}
/**
* Utility function which makes a recordset into an array
* Similar to recordset_to_menu. Array is keyed by the specified field of each record and
* either has the second specified field as the value, or the results of the callback function which
* takes the second field as it's first argument
*
* field1, field2 is needed because the order from get_records_sql is not reliable
* @param records - records from get_records_sql() or get_records()
* @param field1 - field to be used as menu index
* @param field2 - feild to be used as coresponding menu value
* @param string $callback (optional) the name of a function to call in order ot generate the menu item for each record
* @param string $callbackparams (optional) the extra parameters for the callback function
* @return mixed an associative array, or false if an error occured or the RecordSet was empty.
*/
function ilp_records_to_menu($records, $field1, $field2, $callback = null, $callbackparams = null) {
$menu = array();
if(!empty($records)) {
foreach ($records as $record) {
if(empty($callback)) {
$menu[$record->$field1] = $record->$field2;
} else {
// array_unshift($callbackparams, $record->$field2);
$menu[$record->$field1] = call_user_func_array($callback,array($record->$field2,$callbackparams));
}
}
}
return $menu;
}
function ilp_get_user_role_ids($context,$user_id) {
$role_ids = array();
if ($roles = get_user_roles($context, $user_id)) {
foreach ($roles as $role) {
$role_ids[] = $role->roleid;
}
}
return $role_ids;
}
/**
* Wrapper for native ilp_build_navigation() function that truncates the length of
* each of the breadcrumbs to ensure that they all fit neatly on the page
*/
function ilp_build_navigation($breadcrumbs) {
// determine the total length of all the breadcrumbs
$length = 0;
foreach($breadcrumbs as $crumb) {
$length += strlen($crumb['name']);
}
// if it too long then we need to truncate
if($length > ILP_MAXLENGTH_BREADCRUMB) {
// calculate the per crumb limit
$limit = round(ILP_MAXLENGTH_BREADCRUMB/count($breadcrumbs));
// enforce it
foreach($breadcrumbs as $id => $crumb) {
$breadcrumbs[$id]['name'] = ilp_limit_length($crumb['name'], $limit);
}
}
return build_navigation($breadcrumbs);
}
/**
* Take the given object containing block_ilp_reportpermissions records
* and converts them to an object with the role_id and capbility as params.
* This can then be used to populate the reports permission matrix
*
* @param array $permissions recordset of block_ilp_permission records
*
* return mixed object contain param with role_id and capabilty or false
*/
function reportformpermissions($permissions) {
$formpermissions = new stdClass();
//loop through the permissions
foreach($permissions as $perm) {
//create the param name
$param = $perm->role_id."_".$perm->capability_id;
//set the value of our new param to 1
$formpermissions->$param = 1;
}
return $formpermissions;
}
/**
* Truncates long strings and adds a tooltip with a longer verison.
*
* @param string $string The string to truncate
* @param int $maxlength The maximum length the string can be. -1 means unlimited, in case you just want a tooltip
* @param string $tooltip (optional) tooltip to display. defaults to $string
* @param array $special_case (optional) array of characters/entities that if found in string
* stop the truncation and deceoding
* @return string HTML
*/
function ilp_limit_length($html, $maxlength, $tooltip = null) {
// permit only html tags and quotes so we can parse the tags properly
$html = ilp_db::decode_htmlchars(assmgr_db::encode($html));
$printedlength = 0;
$position = 0;
$tags = array();
$return = null;
while ($printedlength < $maxlength && preg_match('{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}', $html, $match, PREG_OFFSET_CAPTURE, $position)) {
list($tag, $tagPosition) = $match[0];
// print text leading up to the tag
$str = substr($html, $position, $tagPosition - $position);
if ($printedlength + strlen($str) > $maxlength) {
$return .= (substr($str, 0, $maxlength - $printedlength));
$printedlength = $maxlength;
break;
}
$return .= ($str);
$printedlength += strlen($str);
if ($tag[0] == '&') {
// handle the entity
$return .= ($tag);
$printedlength++;
} else {
// handle the tag
$tagName = $match[1][0];
if ($tag[1] == '/') {
// this is a closing tag
$openingTag = array_pop($tags);
assert($openingTag == $tagName); // check that tags are properly nested
$return .= ($tag);
} else if ($tag[strlen($tag) - 2] == '/') {
// self-closing tag
$return .= ($tag);
} else {
// opening tag
$return .= ($tag);
$tags[] = $tagName;
}
}
// continue after the tag
$position = $tagPosition + strlen($tag);
}
// print any remaining text
if ($printedlength < $maxlength && $position < strlen($html)) {
$return .= (substr($html, $position, $maxlength - $printedlength));
}
// add the ellipsis, if truncated
$return .= (strip_tags($return) != strip_tags($html)) ? '…' : null;
// close any open tags
while (!empty($tags)) {
$return .= sprintf('</%s>', array_pop($tags));
}
// don't show a tooltip if it's set to false, or if no truncate has been done
if($tooltip === false || ($return == $html && empty($tooltip))) {
return $return;
} else {
// make the tooltip the original string if a specific value was not set
if(empty($tooltip)) {
$tooltip = $html;
}
$tooltip = ilp_db::encode($tooltip);
// generate the unique id needed for the YUI tooltip
$id = 'tootlip'.ilp_uniqueNum();
$script = "<script type='text/javascript'>
//<![CDATA[
new YAHOO.widget.Tooltip('ttA{$id}', {
context:'{$id}',
effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.20}
});
//]]>
</script>";
return "<span id='{$id}' class='tooltip' title='{$tooltip}'>{$return}</span>{$script}";
}
}
function returnNextPosition($takenPos) {
$i = 0;
do {
$i++;
} while (in_array($i,$takenPos));
return $i;
}
function ilp_pluginfile($context, $filearea, $args, $forcedownload) {
if ($context->contextlevel != CONTEXT_SYSTEM) {
send_file_not_found();
}
require_login();
if ($filearea !== 'ilp_element_plugin_file') {
send_file_not_found();
}
$fs = get_file_storage();
$filename = array_pop($args);
$itemid = array_pop($args);
$filepath = $args ? '/'.implode('/', $args).'/' : '/';
if (!$file = $fs->get_file($context->id, 'form_elements', $filearea, $itemid, $filepath, $filename) or $file->is_directory()) {
send_file_not_found();
}
session_get_instance()->write_close();
send_stored_file($file, 60*60, 0, $forcedownload);
}
?>