7cf1c7bd |
1 | <?php |
f9903ed0 |
2 | |
9fa49e22 |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // NOTICE OF COPYRIGHT // |
6 | // // |
7 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
abc3b857 |
8 | // http://moodle.org // |
9fa49e22 |
9 | // // |
abc3b857 |
10 | // Copyright (C) 1999-2004 Martin Dougiamas http://dougiamas.com // |
9fa49e22 |
11 | // // |
12 | // This program is free software; you can redistribute it and/or modify // |
13 | // it under the terms of the GNU General Public License as published by // |
14 | // the Free Software Foundation; either version 2 of the License, or // |
15 | // (at your option) any later version. // |
16 | // // |
17 | // This program is distributed in the hope that it will be useful, // |
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
20 | // GNU General Public License for more details: // |
21 | // // |
22 | // http://www.gnu.org/copyleft/gpl.html // |
23 | // // |
24 | /////////////////////////////////////////////////////////////////////////// |
65ccdd8c |
25 | |
7cf1c7bd |
26 | /** |
89dcb99d |
27 | * moodlelib.php - Moodle main library |
7cf1c7bd |
28 | * |
29 | * Main library file of miscellaneous general-purpose Moodle functions. |
30 | * Other main libraries: |
8c3dba73 |
31 | * - weblib.php - functions that produce web output |
32 | * - datalib.php - functions that access the database |
7cf1c7bd |
33 | * @author Martin Dougiamas |
34 | * @version $Id$ |
89dcb99d |
35 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
7cf1c7bd |
36 | * @package moodlecore |
37 | */ |
f374fb10 |
38 | /// CONSTANTS ///////////////////////////////////////////////////////////// |
39 | |
7cf1c7bd |
40 | /** |
41 | * No groups used? |
42 | */ |
d8ba183c |
43 | define('NOGROUPS', 0); |
7cf1c7bd |
44 | |
45 | /** |
46 | * Groups used? |
47 | */ |
f374fb10 |
48 | define('SEPARATEGROUPS', 1); |
7cf1c7bd |
49 | |
50 | /** |
51 | * Groups visible? |
52 | */ |
f374fb10 |
53 | define('VISIBLEGROUPS', 2); |
54 | |
7a5672c9 |
55 | /** |
2f87145b |
56 | * Time constant - the number of seconds in a week |
7a5672c9 |
57 | */ |
7a5672c9 |
58 | define('WEEKSECS', 604800); |
2f87145b |
59 | |
60 | /** |
61 | * Time constant - the number of seconds in a day |
62 | */ |
7a5672c9 |
63 | define('DAYSECS', 86400); |
2f87145b |
64 | |
65 | /** |
66 | * Time constant - the number of seconds in an hour |
67 | */ |
7a5672c9 |
68 | define('HOURSECS', 3600); |
2f87145b |
69 | |
70 | /** |
71 | * Time constant - the number of seconds in a minute |
72 | */ |
7a5672c9 |
73 | define('MINSECS', 60); |
2f87145b |
74 | |
75 | /** |
76 | * Time constant - the number of minutes in a day |
77 | */ |
7a5672c9 |
78 | define('DAYMINS', 1440); |
2f87145b |
79 | |
80 | /** |
81 | * Time constant - the number of minutes in an hour |
82 | */ |
7a5672c9 |
83 | define('HOURMINS', 60); |
f9903ed0 |
84 | |
9fa49e22 |
85 | /// PARAMETER HANDLING //////////////////////////////////////////////////// |
6b174680 |
86 | |
7cf1c7bd |
87 | /** |
88 | * Ensure that a variable is set or display error |
89 | * |
90 | * If $var is undefined display an error message using the {@link error()} function. |
7ecd7c61 |
91 | * This function will soon be made obsolete by {@link parameter()} |
7cf1c7bd |
92 | * |
c6d15803 |
93 | * @param mixed $var the variable which may not be set |
7cf1c7bd |
94 | */ |
9fa49e22 |
95 | function require_variable($var) { |
96 | /// Variable must be present |
97 | if (! isset($var)) { |
b0ccd3fb |
98 | error('A required parameter was missing'); |
6b174680 |
99 | } |
100 | } |
101 | |
7cf1c7bd |
102 | |
103 | /** |
104 | * Ensure that a variable is set |
105 | * |
106 | * If $var is undefined set it (by reference), otherwise return $var. |
107 | * This function is very similar to {@link nvl()} |
7ecd7c61 |
108 | * This function will soon be made obsolete by {@link parameter()} |
7cf1c7bd |
109 | * |
110 | * @param mixed $var the variable which may be unset |
111 | * @param mixed $default the value to return if $var is unset |
112 | */ |
9fa49e22 |
113 | function optional_variable(&$var, $default=0) { |
114 | /// Variable may be present, if not then set a default |
115 | if (! isset($var)) { |
116 | $var = $default; |
6b174680 |
117 | } |
118 | } |
119 | |
7ecd7c61 |
120 | /** |
121 | * Returns a particular value for the named variable, taken from |
122 | * POST or GET, otherwise returning a given default. |
123 | * |
124 | * This function should be used to initialise all values in a script |
125 | * that are based on parameters. Usually it will be used like this: |
126 | * |
127 | * $id = (int)parameter('id'); |
128 | * |
129 | * @param string $varname the name of the parameter variable we want |
130 | * @param mixed $default the default value to return if nothing is found |
131 | * @return mixed |
132 | */ |
133 | function parameter($varname, $default=NULL) { |
134 | |
135 | if (isset($_POST[$varname])) { // POST has precedence |
136 | return $_POST[$varname]; |
137 | } |
138 | |
139 | if (isset($_GET[$varname])) { |
140 | return $_GET[$varname]; |
141 | } |
142 | |
143 | return $default; |
144 | } |
145 | |
7cf1c7bd |
146 | /** |
147 | * Set a key in global configuration |
148 | * |
89dcb99d |
149 | * Set a key/value pair in both this session's {@link $CFG} global variable |
7cf1c7bd |
150 | * and in the 'config' database table for future sessions. |
151 | * |
152 | * @param string $name the key to set |
153 | * @param string $value the value to set |
154 | * @uses $CFG |
155 | * @return bool |
156 | */ |
9fa49e22 |
157 | function set_config($name, $value) { |
158 | /// No need for get_config because they are usually always available in $CFG |
70812e39 |
159 | |
42282810 |
160 | global $CFG; |
161 | |
7cf1c7bd |
162 | |
42282810 |
163 | $CFG->$name = $value; // So it's defined for this invocation at least |
dfc9ba9b |
164 | |
b0ccd3fb |
165 | if (get_field('config', 'name', 'name', $name)) { |
166 | return set_field('config', 'value', $value, 'name', $name); |
d897cae4 |
167 | } else { |
9fa49e22 |
168 | $config->name = $name; |
169 | $config->value = $value; |
b0ccd3fb |
170 | return insert_record('config', $config); |
39917a09 |
171 | } |
39917a09 |
172 | } |
173 | |
7cf1c7bd |
174 | /** |
175 | * Refresh current $USER session global variable with all their current preferences. |
176 | * @uses $USER |
177 | */ |
70812e39 |
178 | function reload_user_preferences() { |
70812e39 |
179 | |
180 | global $USER; |
181 | |
d8ba183c |
182 | unset($USER->preference); |
70812e39 |
183 | |
184 | if ($preferences = get_records('user_preferences', 'userid', $USER->id)) { |
185 | foreach ($preferences as $preference) { |
186 | $USER->preference[$preference->name] = $preference->value; |
187 | } |
4586d60c |
188 | } else { |
189 | //return empty preference array to hold new values |
190 | $USER->preference = array(); |
c6d15803 |
191 | } |
70812e39 |
192 | } |
193 | |
7cf1c7bd |
194 | /** |
195 | * Sets a preference for the current user |
196 | * Optionally, can set a preference for a different user object |
197 | * @uses $USER |
198 | * @todo Add a better description and include usage examples. |
199 | * @param string $name The key to set as preference for the specified user |
200 | * @param string $value The value to set forthe $name key in the specified user's record |
c6d15803 |
201 | * @param int $userid A moodle user ID |
7cf1c7bd |
202 | * @todo Add inline links to $USER and user functions in above line. |
203 | * @return boolean |
204 | */ |
a3f1f815 |
205 | function set_user_preference($name, $value, $userid=NULL) { |
70812e39 |
206 | |
207 | global $USER; |
208 | |
a3f1f815 |
209 | if (empty($userid)){ |
210 | $userid = $USER->id; |
d35757eb |
211 | } |
212 | |
70812e39 |
213 | if (empty($name)) { |
214 | return false; |
215 | } |
216 | |
a3f1f815 |
217 | if ($preference = get_record('user_preferences', 'userid', $userid, 'name', $name)) { |
b0ccd3fb |
218 | if (set_field('user_preferences', 'value', $value, 'id', $preference->id)) { |
d35757eb |
219 | $user->preference[$name] = $value; |
066af654 |
220 | return true; |
221 | } else { |
222 | return false; |
223 | } |
70812e39 |
224 | |
225 | } else { |
a3f1f815 |
226 | $preference->userid = $userid; |
70812e39 |
227 | $preference->name = $name; |
228 | $preference->value = (string)$value; |
066af654 |
229 | if (insert_record('user_preferences', $preference)) { |
d35757eb |
230 | $user->preference[$name] = $value; |
70812e39 |
231 | return true; |
232 | } else { |
233 | return false; |
234 | } |
235 | } |
236 | } |
237 | |
6eb3e776 |
238 | /** |
239 | * Unsets a preference completely by deleting it from the database |
240 | * Optionally, can set a preference for a different user id |
241 | * @uses $USER |
242 | * @param string $name The key to unset as preference for the specified user |
c6d15803 |
243 | * @param int $userid A moodle user ID |
6eb3e776 |
244 | * @return boolean |
245 | */ |
246 | function unset_user_preference($name, $userid=NULL) { |
247 | |
248 | global $USER; |
249 | |
250 | if (empty($userid)){ |
251 | $userid = $USER->id; |
252 | } |
253 | |
254 | return delete_records('user_preferences', 'userid', $userid, 'name', $name); |
255 | } |
256 | |
257 | |
7cf1c7bd |
258 | /** |
259 | * Sets a whole array of preferences for the current user |
260 | * @param array $prefarray An array of key/value pairs to be set |
c6d15803 |
261 | * @param int $userid A moodle user ID |
7cf1c7bd |
262 | * @return boolean |
263 | */ |
a3f1f815 |
264 | function set_user_preferences($prefarray, $userid=NULL) { |
265 | |
266 | global $USER; |
70812e39 |
267 | |
268 | if (!is_array($prefarray) or empty($prefarray)) { |
269 | return false; |
270 | } |
271 | |
a3f1f815 |
272 | if (empty($userid)){ |
273 | $userid = $USER->id; |
274 | } |
275 | |
70812e39 |
276 | $return = true; |
277 | foreach ($prefarray as $name => $value) { |
278 | // The order is important; if the test for return is done first, |
279 | // then if one function call fails all the remaining ones will |
280 | // be "optimized away" |
a3f1f815 |
281 | $return = set_user_preference($name, $value, $userid) and $return; |
70812e39 |
282 | } |
283 | return $return; |
284 | } |
285 | |
7cf1c7bd |
286 | /** |
287 | * If no arguments are supplied this function will return |
288 | * all of the current user preferences as an array. |
289 | * If a name is specified then this function |
290 | * attempts to return that particular preference value. If |
291 | * none is found, then the optional value $default is returned, |
292 | * otherwise NULL. |
293 | * @param string $name Name of the key to use in finding a preference value |
294 | * @param string $default Value to be returned if the $name key is not set in the user preferences |
c6d15803 |
295 | * @param int $userid A moodle user ID |
7cf1c7bd |
296 | * @uses $USER |
297 | * @return string |
298 | */ |
a3f1f815 |
299 | function get_user_preferences($name=NULL, $default=NULL, $userid=NULL) { |
70812e39 |
300 | |
301 | global $USER; |
302 | |
a3f1f815 |
303 | if (empty($userid)) { // assume current user |
304 | if (empty($USER->preference)) { |
305 | return $default; // Default value (or NULL) |
306 | } |
307 | if (empty($name)) { |
308 | return $USER->preference; // Whole array |
309 | } |
310 | if (!isset($USER->preference[$name])) { |
311 | return $default; // Default value (or NULL) |
312 | } |
313 | return $USER->preference[$name]; // The single value |
314 | |
315 | } else { |
316 | $preference = get_records_menu('user_preferences', 'userid', $userid, 'name', 'name,value'); |
317 | |
318 | if (empty($name)) { |
319 | return $preference; |
320 | } |
321 | if (!isset($preference[$name])) { |
322 | return $default; // Default value (or NULL) |
323 | } |
324 | return $preference[$name]; // The single value |
70812e39 |
325 | } |
70812e39 |
326 | } |
327 | |
328 | |
9fa49e22 |
329 | /// FUNCTIONS FOR HANDLING TIME //////////////////////////////////////////// |
39917a09 |
330 | |
7cf1c7bd |
331 | /** |
c6d15803 |
332 | * Given date parts in user time produce a GMT timestamp. |
7cf1c7bd |
333 | * |
c6d15803 |
334 | * @param int $year The year part to create timestamp of. |
335 | * @param int $month The month part to create timestamp of. |
336 | * @param int $day The day part to create timestamp of. |
337 | * @param int $hour The hour part to create timestamp of. |
338 | * @param int $minute The minute part to create timestamp of. |
339 | * @param int $second The second part to create timestamp of. |
340 | * @param int $timezone ? |
341 | * @return ? |
7cf1c7bd |
342 | * @todo Finish documenting this function |
343 | */ |
3db75c62 |
344 | function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0, $timezone=99) { |
39917a09 |
345 | |
f30fe8d0 |
346 | $timezone = get_user_timezone($timezone); |
94e34118 |
347 | |
348 | if (abs($timezone) > 13) { |
03c17ddf |
349 | return mktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year); |
350 | } else { |
351 | $time = gmmktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year); |
352 | return usertime($time, $timezone); // This is GMT |
353 | } |
39917a09 |
354 | } |
355 | |
7cf1c7bd |
356 | /** |
357 | * Given an amount of time in seconds, returns string |
358 | * formatted nicely as months, days, hours etc as needed |
359 | * |
2f87145b |
360 | * @uses MINSECS |
361 | * @uses HOURSECS |
362 | * @uses DAYSECS |
c6d15803 |
363 | * @param int $totalsecs ? |
364 | * @param array $str ? |
89dcb99d |
365 | * @return string |
7cf1c7bd |
366 | * @todo Finish documenting this function |
367 | */ |
368 | function format_time($totalsecs, $str=NULL) { |
c7e3ac2a |
369 | |
6b174680 |
370 | $totalsecs = abs($totalsecs); |
c7e3ac2a |
371 | |
8dbed6be |
372 | if (!$str) { // Create the str structure the slow way |
b0ccd3fb |
373 | $str->day = get_string('day'); |
374 | $str->days = get_string('days'); |
375 | $str->hour = get_string('hour'); |
376 | $str->hours = get_string('hours'); |
377 | $str->min = get_string('min'); |
378 | $str->mins = get_string('mins'); |
379 | $str->sec = get_string('sec'); |
380 | $str->secs = get_string('secs'); |
8dbed6be |
381 | } |
382 | |
7a5672c9 |
383 | $days = floor($totalsecs/DAYSECS); |
384 | $remainder = $totalsecs - ($days*DAYSECS); |
385 | $hours = floor($remainder/HOURSECS); |
386 | $remainder = $remainder - ($hours*HOURSECS); |
387 | $mins = floor($remainder/MINSECS); |
388 | $secs = $remainder - ($mins*MINSECS); |
8dbed6be |
389 | |
390 | $ss = ($secs == 1) ? $str->sec : $str->secs; |
391 | $sm = ($mins == 1) ? $str->min : $str->mins; |
392 | $sh = ($hours == 1) ? $str->hour : $str->hours; |
393 | $sd = ($days == 1) ? $str->day : $str->days; |
394 | |
b0ccd3fb |
395 | $odays = ''; |
396 | $ohours = ''; |
397 | $omins = ''; |
398 | $osecs = ''; |
9c9f7d77 |
399 | |
b0ccd3fb |
400 | if ($days) $odays = $days .' '. $sd; |
401 | if ($hours) $ohours = $hours .' '. $sh; |
402 | if ($mins) $omins = $mins .' '. $sm; |
403 | if ($secs) $osecs = $secs .' '. $ss; |
6b174680 |
404 | |
b0ccd3fb |
405 | if ($days) return $odays .' '. $ohours; |
406 | if ($hours) return $ohours .' '. $omins; |
407 | if ($mins) return $omins .' '. $osecs; |
408 | if ($secs) return $osecs; |
409 | return get_string('now'); |
6b174680 |
410 | } |
f9903ed0 |
411 | |
7cf1c7bd |
412 | /** |
413 | * Returns a formatted string that represents a date in user time |
414 | * <b>WARNING: note that the format is for strftime(), not date().</b> |
415 | * Because of a bug in most Windows time libraries, we can't use |
416 | * the nicer %e, so we have to use %d which has leading zeroes. |
417 | * A lot of the fuss in the function is just getting rid of these leading |
418 | * zeroes as efficiently as possible. |
419 | * |
8c3dba73 |
420 | * If parameter fixday = true (default), then take off leading |
7cf1c7bd |
421 | * zero from %d, else mantain it. |
422 | * |
2f87145b |
423 | * @uses HOURSECS |
c6d15803 |
424 | * @param int $date ? |
425 | * @param string $format ? |
426 | * @param int $timezone ? |
427 | * @param boolean $fixday If true (default) then the leading |
428 | * zero from %d is removed. If false then the leading zero is mantained. |
429 | * @return string |
7cf1c7bd |
430 | * @todo Finish documenting this function |
431 | */ |
b0ccd3fb |
432 | function userdate($date, $format='', $timezone=99, $fixday = true) { |
7a302afc |
433 | |
b0ccd3fb |
434 | if ($format == '') { |
435 | $format = get_string('strftimedaydatetime'); |
5fa51a39 |
436 | } |
035cdbff |
437 | |
b0ccd3fb |
438 | $formatnoday = str_replace('%d', 'DD', $format); |
61ae5d36 |
439 | if ($fixday) { |
440 | $fixday = ($formatnoday != $format); |
441 | } |
dcde9f02 |
442 | |
f30fe8d0 |
443 | $timezone = get_user_timezone($timezone); |
90207a06 |
444 | |
0431bd7c |
445 | if (abs($timezone) > 13) { |
035cdbff |
446 | if ($fixday) { |
447 | $datestring = strftime($formatnoday, $date); |
b0ccd3fb |
448 | $daystring = str_replace(' 0', '', strftime(" %d", $date)); |
449 | $datestring = str_replace('DD', $daystring, $datestring); |
035cdbff |
450 | } else { |
451 | $datestring = strftime($format, $date); |
452 | } |
bea7a51e |
453 | } else { |
7a5672c9 |
454 | $date = $date + (int)($timezone * HOURSECS); |
035cdbff |
455 | if ($fixday) { |
70d4cf82 |
456 | $datestring = gmstrftime($formatnoday, $date); |
b0ccd3fb |
457 | $daystring = str_replace(' 0', '', gmstrftime(" %d", $date)); |
458 | $datestring = str_replace('DD', $daystring, $datestring); |
035cdbff |
459 | } else { |
70d4cf82 |
460 | $datestring = gmstrftime($format, $date); |
035cdbff |
461 | } |
873960de |
462 | } |
bea7a51e |
463 | |
035cdbff |
464 | return $datestring; |
873960de |
465 | } |
466 | |
7cf1c7bd |
467 | /** |
c6d15803 |
468 | * Given a $date timestamp in GMT (seconds since epoch), |
469 | * returns an array that represents the date in user time |
7cf1c7bd |
470 | * |
2f87145b |
471 | * @uses HOURSECS |
c6d15803 |
472 | * @param int $date Timestamp in GMT |
473 | * @param int $timezone ? |
474 | * @return array An array that represents the date in user time |
7cf1c7bd |
475 | * @todo Finish documenting this function |
476 | */ |
5fa51a39 |
477 | function usergetdate($date, $timezone=99) { |
6b174680 |
478 | |
f30fe8d0 |
479 | $timezone = get_user_timezone($timezone); |
a36166d3 |
480 | |
0431bd7c |
481 | if (abs($timezone) > 13) { |
873960de |
482 | return getdate($date); |
483 | } |
d2d6171f |
484 | //There is no gmgetdate so I have to fake it... |
7a5672c9 |
485 | $date = $date + (int)($timezone * HOURSECS); |
b0ccd3fb |
486 | $getdate['seconds'] = gmstrftime("%S", $date); |
487 | $getdate['minutes'] = gmstrftime("%M", $date); |
488 | $getdate['hours'] = gmstrftime("%H", $date); |
489 | $getdate['mday'] = gmstrftime("%d", $date); |
490 | $getdate['wday'] = gmstrftime("%u", $date); |
491 | $getdate['mon'] = gmstrftime("%m", $date); |
492 | $getdate['year'] = gmstrftime("%Y", $date); |
493 | $getdate['yday'] = gmstrftime("%j", $date); |
494 | $getdate['weekday'] = gmstrftime("%A", $date); |
495 | $getdate['month'] = gmstrftime("%B", $date); |
d2d6171f |
496 | return $getdate; |
d552ead0 |
497 | } |
498 | |
7cf1c7bd |
499 | /** |
500 | * Given a GMT timestamp (seconds since epoch), offsets it by |
501 | * the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds |
502 | * |
2f87145b |
503 | * @uses HOURSECS |
c6d15803 |
504 | * @param int $date Timestamp in GMT |
505 | * @param int $timezone ? |
506 | * @return int |
7cf1c7bd |
507 | * @todo Finish documenting this function |
508 | */ |
d552ead0 |
509 | function usertime($date, $timezone=99) { |
a36166d3 |
510 | |
f30fe8d0 |
511 | $timezone = get_user_timezone($timezone); |
0431bd7c |
512 | if (abs($timezone) > 13) { |
d552ead0 |
513 | return $date; |
514 | } |
7a5672c9 |
515 | return $date - (int)($timezone * HOURSECS); |
d552ead0 |
516 | } |
517 | |
8c3dba73 |
518 | /** |
519 | * Given a time, return the GMT timestamp of the most recent midnight |
520 | * for the current user. |
521 | * |
c6d15803 |
522 | * @param int $date Timestamp in GMT |
523 | * @param int $timezone ? |
524 | * @return ? |
525 | * @todo Finish documenting this function. Is timezone an int or float? |
8c3dba73 |
526 | */ |
edf7fe8c |
527 | function usergetmidnight($date, $timezone=99) { |
edf7fe8c |
528 | |
f30fe8d0 |
529 | $timezone = get_user_timezone($timezone); |
edf7fe8c |
530 | $userdate = usergetdate($date, $timezone); |
4606d9bb |
531 | |
0431bd7c |
532 | if (abs($timezone) > 13) { |
b0ccd3fb |
533 | return mktime(0, 0, 0, $userdate['mon'], $userdate['mday'], $userdate['year']); |
4606d9bb |
534 | } |
535 | |
b0ccd3fb |
536 | $timemidnight = gmmktime (0, 0, 0, $userdate['mon'], $userdate['mday'], $userdate['year']); |
edf7fe8c |
537 | return usertime($timemidnight, $timezone); // Time of midnight of this user's day, in GMT |
538 | |
539 | } |
540 | |
7cf1c7bd |
541 | /** |
542 | * Returns a string that prints the user's timezone |
543 | * |
544 | * @param float $timezone The user's timezone |
545 | * @return string |
c6d15803 |
546 | * @todo is $timezone an int or a float? |
7cf1c7bd |
547 | */ |
d552ead0 |
548 | function usertimezone($timezone=99) { |
d552ead0 |
549 | |
f30fe8d0 |
550 | $timezone = get_user_timezone($timezone); |
551 | |
0431bd7c |
552 | if (abs($timezone) > 13) { |
b0ccd3fb |
553 | return 'server time'; |
d552ead0 |
554 | } |
555 | if (abs($timezone) < 0.5) { |
b0ccd3fb |
556 | return 'GMT'; |
d552ead0 |
557 | } |
558 | if ($timezone > 0) { |
b0ccd3fb |
559 | return 'GMT+'. $timezone; |
d552ead0 |
560 | } else { |
b0ccd3fb |
561 | return 'GMT'. $timezone; |
d552ead0 |
562 | } |
f9903ed0 |
563 | } |
564 | |
7cf1c7bd |
565 | /** |
566 | * Returns a float which represents the user's timezone difference from GMT in hours |
567 | * Checks various settings and picks the most dominant of those which have a value |
568 | * |
7cf1c7bd |
569 | * @uses $CFG |
570 | * @uses $USER |
c6d15803 |
571 | * @param int $tz The user's timezone |
572 | * @return int |
573 | * @todo is $tz an int or a float? |
7cf1c7bd |
574 | */ |
f30fe8d0 |
575 | function get_user_timezone($tz = 99) { |
f30fe8d0 |
576 | |
577 | // Variables declared explicitly global here so that if we add |
578 | // something later we won't forget to global it... |
579 | $timezones = array( |
580 | isset($GLOBALS['USER']->timezone) ? $GLOBALS['USER']->timezone : 99, |
581 | isset($GLOBALS['CFG']->timezone) ? $GLOBALS['CFG']->timezone : 99, |
582 | ); |
583 | while($tz == 99 && $next = each($timezones)) { |
584 | $tz = (float)$next['value']; |
585 | } |
586 | |
587 | return $tz; |
588 | } |
f9903ed0 |
589 | |
9fa49e22 |
590 | /// USER AUTHENTICATION AND LOGIN //////////////////////////////////////// |
f9903ed0 |
591 | |
7cf1c7bd |
592 | /** |
593 | * This function checks that the current user is logged in, and optionally |
594 | * whether they are "logged in" or allowed to be in a particular course. |
595 | * If not, then it redirects them to the site login or course enrolment. |
596 | * $autologinguest determines whether visitors should automatically be |
89dcb99d |
597 | * logged in as guests provide {@link $CFG}->autologinguests is set to 1 |
7cf1c7bd |
598 | * |
7cf1c7bd |
599 | * @uses $CFG |
c6d15803 |
600 | * @uses $SESSION |
7cf1c7bd |
601 | * @uses $USER |
602 | * @uses $FULLME |
c6d15803 |
603 | * @uses SITEID |
7cf1c7bd |
604 | * @uses $MoodleSession |
c6d15803 |
605 | * @param int $courseid The course in question |
606 | * @param boolean $autologinguest ? |
7cf1c7bd |
607 | * @todo Finish documenting this function |
608 | */ |
8e8d0524 |
609 | function require_login($courseid=0, $autologinguest=true) { |
f9903ed0 |
610 | |
73047f2f |
611 | global $CFG, $SESSION, $USER, $FULLME, $MoodleSession; |
d8ba183c |
612 | |
da5c172a |
613 | // First check that the user is logged in to the site. |
c21c671d |
614 | if (! (isset($USER->loggedin) and $USER->confirmed and ($USER->site == $CFG->wwwroot)) ) { // They're not |
f9903ed0 |
615 | $SESSION->wantsurl = $FULLME; |
b0ccd3fb |
616 | if (!empty($_SERVER['HTTP_REFERER'])) { |
617 | $SESSION->fromurl = $_SERVER['HTTP_REFERER']; |
9f44d972 |
618 | } |
c21c671d |
619 | $USER = NULL; |
8e8d0524 |
620 | if ($autologinguest and $CFG->autologinguests and $courseid and get_field('course','guest','id',$courseid)) { |
621 | $loginguest = '?loginguest=true'; |
622 | } else { |
623 | $loginguest = ''; |
a2ebe6a5 |
624 | } |
8a33e371 |
625 | if (empty($CFG->loginhttps)) { |
b0ccd3fb |
626 | redirect($CFG->wwwroot .'/login/index.php'. $loginguest); |
8a33e371 |
627 | } else { |
b0ccd3fb |
628 | $wwwroot = str_replace('http','https', $CFG->wwwroot); |
629 | redirect($wwwroot .'/login/index.php'. $loginguest); |
8a33e371 |
630 | } |
f9903ed0 |
631 | die; |
f9903ed0 |
632 | } |
808a3baa |
633 | |
d35757eb |
634 | // check whether the user should be changing password |
635 | reload_user_preferences(); |
a3f1f815 |
636 | if (!empty($USER->preference['auth_forcepasswordchange'])){ |
d35757eb |
637 | if (is_internal_auth() || $CFG->{'auth_'.$USER->auth.'_stdchangepassword'}){ |
b0ccd3fb |
638 | redirect($CFG->wwwroot .'/login/change_password.php'); |
d35757eb |
639 | } elseif($CFG->changepassword) { |
640 | redirect($CFG->changepassword); |
641 | } else { |
b0ccd3fb |
642 | error('You cannot proceed without changing your password. |
d35757eb |
643 | However there is no available page for changing it. |
b0ccd3fb |
644 | Please contact your Moodle Administrator.'); |
d35757eb |
645 | } |
646 | } |
647 | |
808a3baa |
648 | // Check that the user account is properly set up |
649 | if (user_not_fully_set_up($USER)) { |
b0ccd3fb |
650 | redirect($CFG->wwwroot .'/user/edit.php?id='. $USER->id .'&course='. SITEID); |
808a3baa |
651 | die; |
652 | } |
d8ba183c |
653 | |
366dfa60 |
654 | // Make sure current IP matches the one for this session (if required) |
655 | if (!empty($CFG->tracksessionip)) { |
656 | if ($USER->sessionIP != md5(getremoteaddr())) { |
657 | error(get_string('sessionipnomatch', 'error')); |
658 | } |
659 | } |
660 | |
da5c172a |
661 | // Next, check if the user can be in a particular course |
662 | if ($courseid) { |
e3512050 |
663 | if ($courseid == SITEID) { |
664 | return; // Anyone can be in the site course |
665 | } |
9c9f7d77 |
666 | if (!empty($USER->student[$courseid]) or !empty($USER->teacher[$courseid]) or !empty($USER->admin)) { |
cb909d74 |
667 | if (isset($USER->realuser)) { // Make sure the REAL person can also access this course |
668 | if (!isteacher($courseid, $USER->realuser)) { |
669 | print_header(); |
b0ccd3fb |
670 | notice(get_string('studentnotallowed', '', fullname($USER, true)), $CFG->wwwroot .'/'); |
cb909d74 |
671 | } |
3ce2f1e0 |
672 | } |
da5c172a |
673 | return; // user is a member of this course. |
674 | } |
b0ccd3fb |
675 | if (! $course = get_record('course', 'id', $courseid)) { |
676 | error('That course doesn\'t exist'); |
da5c172a |
677 | } |
1efa27fd |
678 | if (!$course->visible) { |
679 | print_header(); |
b0ccd3fb |
680 | notice(get_string('studentnotallowed', '', fullname($USER, true)), $CFG->wwwroot .'/'); |
1efa27fd |
681 | } |
b0ccd3fb |
682 | if ($USER->username == 'guest') { |
7363ff91 |
683 | switch ($course->guest) { |
684 | case 0: // Guests not allowed |
685 | print_header(); |
b0ccd3fb |
686 | notice(get_string('guestsnotallowed', '', $course->fullname)); |
7363ff91 |
687 | break; |
688 | case 1: // Guests allowed |
7363ff91 |
689 | return; |
690 | case 2: // Guests allowed with key (drop through) |
691 | break; |
692 | } |
da5c172a |
693 | } |
f9903ed0 |
694 | |
7363ff91 |
695 | // Currently not enrolled in the course, so see if they want to enrol |
da5c172a |
696 | $SESSION->wantsurl = $FULLME; |
b0ccd3fb |
697 | redirect($CFG->wwwroot .'/course/enrol.php?id='. $courseid); |
da5c172a |
698 | die; |
699 | } |
f9903ed0 |
700 | } |
701 | |
7cf1c7bd |
702 | /** |
703 | * This is a weaker version of {@link require_login()} which only requires login |
704 | * when called from within a course rather than the site page, unless |
705 | * the forcelogin option is turned on. |
706 | * |
707 | * @uses $CFG |
c6d15803 |
708 | * @param int $courseid The course in question |
709 | * @param boolean $autologinguest ? |
7cf1c7bd |
710 | * @todo Finish documenting this function |
711 | */ |
f950af3c |
712 | function require_course_login($course, $autologinguest=true) { |
f950af3c |
713 | global $CFG; |
714 | if ($CFG->forcelogin) { |
715 | require_login(); |
716 | } |
717 | if ($course->category) { |
718 | require_login($course->id, $autologinguest); |
719 | } |
720 | } |
721 | |
7cf1c7bd |
722 | /** |
723 | * Modify the user table by setting the currently logged in user's |
724 | * last login to now. |
725 | * |
726 | * @uses $USER |
727 | * @return boolean |
728 | */ |
1d881d92 |
729 | function update_user_login_times() { |
730 | global $USER; |
731 | |
732 | $USER->lastlogin = $user->lastlogin = $USER->currentlogin; |
2a2f5f11 |
733 | $USER->currentlogin = $user->lastaccess = $user->currentlogin = time(); |
1d881d92 |
734 | |
735 | $user->id = $USER->id; |
736 | |
b0ccd3fb |
737 | return update_record('user', $user); |
1d881d92 |
738 | } |
739 | |
7cf1c7bd |
740 | /** |
741 | * Determines if a user has completed setting up their account. |
742 | * |
89dcb99d |
743 | * @param user $user A {@link $USER} object to test for the existance of a valid name and email |
7cf1c7bd |
744 | * @return boolean |
745 | */ |
808a3baa |
746 | function user_not_fully_set_up($user) { |
b0ccd3fb |
747 | return ($user->username != 'guest' and (empty($user->firstname) or empty($user->lastname) or empty($user->email))); |
808a3baa |
748 | } |
f9903ed0 |
749 | |
7cf1c7bd |
750 | /** |
751 | * Keeps track of login attempts |
752 | * |
753 | * @uses $SESSION |
754 | */ |
f9903ed0 |
755 | function update_login_count() { |
9fa49e22 |
756 | |
f9903ed0 |
757 | global $SESSION; |
758 | |
759 | $max_logins = 10; |
760 | |
761 | if (empty($SESSION->logincount)) { |
762 | $SESSION->logincount = 1; |
763 | } else { |
764 | $SESSION->logincount++; |
765 | } |
766 | |
767 | if ($SESSION->logincount > $max_logins) { |
9fa49e22 |
768 | unset($SESSION->wantsurl); |
b0ccd3fb |
769 | error(get_string('errortoomanylogins')); |
d578afc8 |
770 | } |
771 | } |
772 | |
7cf1c7bd |
773 | /** |
774 | * Resets login attempts |
775 | * |
776 | * @uses $SESSION |
777 | */ |
9fa49e22 |
778 | function reset_login_count() { |
9fa49e22 |
779 | global $SESSION; |
d578afc8 |
780 | |
9fa49e22 |
781 | $SESSION->logincount = 0; |
d578afc8 |
782 | } |
783 | |
7cf1c7bd |
784 | /** |
785 | * check_for_restricted_user |
786 | * |
89dcb99d |
787 | * @uses $CFG |
788 | * @uses $USER |
789 | * @param string $username ? |
790 | * @param string $redirect ? |
7cf1c7bd |
791 | * @todo Finish documenting this function |
792 | */ |
b0ccd3fb |
793 | function check_for_restricted_user($username=NULL, $redirect='') { |
cb98d312 |
794 | global $CFG, $USER; |
795 | |
796 | if (!$username) { |
797 | if (!empty($USER->username)) { |
798 | $username = $USER->username; |
799 | } else { |
800 | return false; |
801 | } |
802 | } |
803 | |
804 | if (!empty($CFG->restrictusers)) { |
805 | $names = explode(',', $CFG->restrictusers); |
806 | if (in_array($username, $names)) { |
b0ccd3fb |
807 | error(get_string('restricteduser', 'error', fullname($USER)), $redirect); |
cb98d312 |
808 | } |
809 | } |
810 | } |
811 | |
7cf1c7bd |
812 | /** |
813 | * Determines if a user an admin |
814 | * |
815 | * @uses $USER |
c6d15803 |
816 | * @param int $userid The id of the user as is found in the 'user' table |
89dcb99d |
817 | * @staticvar array $admin ? |
818 | * @staticvar array $nonadmins ? |
7cf1c7bd |
819 | * @return boolean |
89dcb99d |
820 | * @todo Complete documentation for this function |
7cf1c7bd |
821 | */ |
581d7b49 |
822 | function isadmin($userid=0) { |
f9903ed0 |
823 | global $USER; |
aa095969 |
824 | static $admins = array(); |
825 | static $nonadmins = array(); |
f9903ed0 |
826 | |
581d7b49 |
827 | if (!$userid){ |
828 | if (empty($USER->id)) { |
829 | return false; |
830 | } |
831 | $userid = $USER->id; |
9bd2c874 |
832 | } |
833 | |
581d7b49 |
834 | if (in_array($userid, $admins)) { |
aa095969 |
835 | return true; |
581d7b49 |
836 | } else if (in_array($userid, $nonadmins)) { |
aa095969 |
837 | return false; |
b0ccd3fb |
838 | } else if (record_exists('user_admins', 'userid', $userid)){ |
581d7b49 |
839 | $admins[] = $userid; |
aa095969 |
840 | return true; |
841 | } else { |
581d7b49 |
842 | $nonadmins[] = $userid; |
aa095969 |
843 | return false; |
f9903ed0 |
844 | } |
f9903ed0 |
845 | } |
846 | |
7cf1c7bd |
847 | /** |
848 | * Determines if a user is a teacher or an admin |
849 | * |
850 | * @uses $USER |
c6d15803 |
851 | * @param int $courseid The id of the course that is being viewed, if any |
852 | * @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user. |
7cf1c7bd |
853 | * @param boolean $includeadmin If true this function will return true when it encounters an admin user. |
854 | * @return boolean |
855 | * @todo Finish documenting this function |
856 | */ |
9d3c795c |
857 | function isteacher($courseid=0, $userid=0, $includeadmin=true) { |
f9903ed0 |
858 | global $USER; |
859 | |
9788367b |
860 | if ($includeadmin and isadmin($userid)) { // admins can do anything the teacher can |
d115a57f |
861 | return true; |
862 | } |
863 | |
f9903ed0 |
864 | if (!$userid) { |
71f9abf9 |
865 | if ($courseid) { |
866 | return !empty($USER->teacher[$courseid]); |
867 | } |
868 | if (!isset($USER->id)) { |
869 | return false; |
870 | } |
871 | $userid = $USER->id; |
f9903ed0 |
872 | } |
873 | |
9d3c795c |
874 | if (!$courseid) { |
b0ccd3fb |
875 | return record_exists('user_teachers', 'userid', $userid); |
9d3c795c |
876 | } |
877 | |
b0ccd3fb |
878 | return record_exists('user_teachers', 'userid', $userid, 'course', $courseid); |
f9903ed0 |
879 | } |
880 | |
7cf1c7bd |
881 | /** |
882 | * Determines if a user is allowed to edit a given course |
883 | * |
884 | * @uses $USER |
c6d15803 |
885 | * @param int $courseid The id of the course that is being edited |
886 | * @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user. |
7cf1c7bd |
887 | * @return boolean |
888 | */ |
73047f2f |
889 | function isteacheredit($courseid, $userid=0) { |
73047f2f |
890 | global $USER; |
891 | |
d8ba183c |
892 | if (isadmin($userid)) { // admins can do anything |
73047f2f |
893 | return true; |
894 | } |
895 | |
896 | if (!$userid) { |
897 | return !empty($USER->teacheredit[$courseid]); |
898 | } |
899 | |
b0ccd3fb |
900 | return get_field('user_teachers', 'editall', 'userid', $userid, 'course', $courseid); |
73047f2f |
901 | } |
902 | |
7cf1c7bd |
903 | /** |
904 | * Determines if a user can create new courses |
905 | * |
906 | * @uses $USER |
c6d15803 |
907 | * @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user. |
7cf1c7bd |
908 | * @return boolean |
909 | */ |
1924074c |
910 | function iscreator ($userid=0) { |
1924074c |
911 | global $USER; |
8a205861 |
912 | if (empty($USER->id)) { |
913 | return false; |
914 | } |
1924074c |
915 | if (isadmin($userid)) { // admins can do anything |
916 | return true; |
917 | } |
8a205861 |
918 | if (empty($userid)) { |
b0ccd3fb |
919 | return record_exists('user_coursecreators', 'userid', $USER->id); |
1924074c |
920 | } |
921 | |
b0ccd3fb |
922 | return record_exists('user_coursecreators', 'userid', $userid); |
1924074c |
923 | } |
924 | |
7cf1c7bd |
925 | /** |
926 | * Determines if a user is a student in the specified course |
927 | * |
928 | * If the course id specifies the site then the function determines |
929 | * if the user is a confirmed and valid user of this site. |
930 | * |
931 | * @uses $USER |
932 | * @uses $CFG |
c6d15803 |
933 | * @uses SITEID |
934 | * @param int $courseid The id of the course being tested |
935 | * @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user. |
7cf1c7bd |
936 | * @return boolean |
937 | */ |
8a9e3fd7 |
938 | function isstudent($courseid, $userid=0) { |
71f9abf9 |
939 | global $USER, $CFG; |
f9903ed0 |
940 | |
2700d113 |
941 | if (empty($USER->id) and !$userid) { |
7064e18f |
942 | return false; |
943 | } |
944 | |
222ac91b |
945 | if ($courseid == SITEID) { |
2cc72e84 |
946 | if (!$userid) { |
947 | $userid = $USER->id; |
948 | } |
949 | if (isguest($userid)) { |
950 | return false; |
951 | } |
71f9abf9 |
952 | // a site teacher can never be a site student |
953 | if (isteacher($courseid, $userid)) { |
954 | return false; |
955 | } |
2700d113 |
956 | if ($CFG->allusersaresitestudents) { |
957 | return record_exists('user', 'id', $userid); |
958 | } else { |
959 | return (record_exists('user_students', 'userid', $userid) |
71f9abf9 |
960 | or record_exists('user_teachers', 'userid', $userid)); |
2700d113 |
961 | } |
8f0cd6ef |
962 | } |
2cc72e84 |
963 | |
f9903ed0 |
964 | if (!$userid) { |
346b1a24 |
965 | return !empty($USER->student[$courseid]); |
f9903ed0 |
966 | } |
967 | |
ebc3bd2b |
968 | // $timenow = time(); // todo: add time check below |
f9903ed0 |
969 | |
b0ccd3fb |
970 | return record_exists('user_students', 'userid', $userid, 'course', $courseid); |
f9903ed0 |
971 | } |
972 | |
7cf1c7bd |
973 | /** |
974 | * Determines if the specified user is logged in as guest. |
975 | * |
976 | * @uses $USER |
c6d15803 |
977 | * @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user. |
7cf1c7bd |
978 | * @return boolean |
979 | */ |
da5c172a |
980 | function isguest($userid=0) { |
981 | global $USER; |
982 | |
983 | if (!$userid) { |
b35e8568 |
984 | if (empty($USER->username)) { |
985 | return false; |
986 | } |
b0ccd3fb |
987 | return ($USER->username == 'guest'); |
da5c172a |
988 | } |
989 | |
b0ccd3fb |
990 | return record_exists('user', 'id', $userid, 'username', 'guest'); |
da5c172a |
991 | } |
992 | |
7cf1c7bd |
993 | /** |
994 | * Determines if the currently logged in user is in editing mode |
995 | * |
996 | * @uses $USER |
c6d15803 |
997 | * @param int $courseid The id of the course being tested |
89dcb99d |
998 | * @param user $user A {@link $USER} object. If null then the currently logged in user is used. |
7cf1c7bd |
999 | * @return boolean |
1000 | */ |
2c309dc2 |
1001 | function isediting($courseid, $user=NULL) { |
1002 | global $USER; |
1003 | if (!$user){ |
1004 | $user = $USER; |
1005 | } |
9c9f7d77 |
1006 | if (empty($user->editing)) { |
1007 | return false; |
1008 | } |
2c309dc2 |
1009 | return ($user->editing and isteacher($courseid, $user->id)); |
1010 | } |
1011 | |
7cf1c7bd |
1012 | /** |
1013 | * Determines if the logged in user is currently moving an activity |
1014 | * |
1015 | * @uses $USER |
c6d15803 |
1016 | * @param int $courseid The id of the course being tested |
7cf1c7bd |
1017 | * @return boolean |
1018 | */ |
7977cffd |
1019 | function ismoving($courseid) { |
7977cffd |
1020 | global $USER; |
1021 | |
1022 | if (!empty($USER->activitycopy)) { |
1023 | return ($USER->activitycopycourse == $courseid); |
1024 | } |
1025 | return false; |
1026 | } |
1027 | |
7cf1c7bd |
1028 | /** |
1029 | * Given an object containing firstname and lastname |
1030 | * values, this function returns a string with the |
1031 | * full name of the person. |
1032 | * The result may depend on system settings |
1033 | * or language. 'override' will force both names |
1034 | * to be used even if system settings specify one. |
1035 | * @uses $CFG |
1036 | * @uses $SESSION |
1037 | * @param type description |
1038 | * @todo Finish documenting this function |
1039 | */ |
e2cd5065 |
1040 | function fullname($user, $override=false) { |
b5cbb64d |
1041 | |
f374fb10 |
1042 | global $CFG, $SESSION; |
1043 | |
6527c077 |
1044 | if (!isset($user->firstname) and !isset($user->lastname)) { |
1045 | return ''; |
1046 | } |
1047 | |
f374fb10 |
1048 | if (!empty($SESSION->fullnamedisplay)) { |
1049 | $CFG->fullnamedisplay = $SESSION->fullnamedisplay; |
1050 | } |
e2cd5065 |
1051 | |
b5cbb64d |
1052 | if ($CFG->fullnamedisplay == 'firstname lastname') { |
b0ccd3fb |
1053 | return $user->firstname .' '. $user->lastname; |
b5cbb64d |
1054 | |
1055 | } else if ($CFG->fullnamedisplay == 'lastname firstname') { |
b0ccd3fb |
1056 | return $user->lastname .' '. $user->firstname; |
e2cd5065 |
1057 | |
b5cbb64d |
1058 | } else if ($CFG->fullnamedisplay == 'firstname') { |
1059 | if ($override) { |
1060 | return get_string('fullnamedisplay', '', $user); |
1061 | } else { |
1062 | return $user->firstname; |
1063 | } |
1064 | } |
e2cd5065 |
1065 | |
b5cbb64d |
1066 | return get_string('fullnamedisplay', '', $user); |
e2cd5065 |
1067 | } |
1068 | |
7cf1c7bd |
1069 | /** |
1070 | * Sets a moodle cookie with an encrypted string |
1071 | * |
1072 | * @uses $CFG |
2f87145b |
1073 | * @uses DAYSECS |
1074 | * @uses HOURSECS |
7cf1c7bd |
1075 | * @param string $thing The string to encrypt and place in a cookie |
1076 | */ |
f9903ed0 |
1077 | function set_moodle_cookie($thing) { |
7185e073 |
1078 | global $CFG; |
482b6e6e |
1079 | |
1080 | $cookiename = 'MOODLEID_'.$CFG->sessioncookie; |
f9903ed0 |
1081 | |
1082 | $days = 60; |
7a5672c9 |
1083 | $seconds = DAYSECS*$days; |
f9903ed0 |
1084 | |
7a5672c9 |
1085 | setCookie($cookiename, '', time() - HOURSECS, '/'); |
b0ccd3fb |
1086 | setCookie($cookiename, rc4encrypt($thing), time()+$seconds, '/'); |
f9903ed0 |
1087 | } |
1088 | |
7cf1c7bd |
1089 | /** |
1090 | * Gets a moodle cookie with an encrypted string |
1091 | * |
1092 | * @uses $CFG |
1093 | * @return string |
1094 | */ |
f9903ed0 |
1095 | function get_moodle_cookie() { |
7185e073 |
1096 | global $CFG; |
1097 | |
482b6e6e |
1098 | $cookiename = 'MOODLEID_'.$CFG->sessioncookie; |
7185e073 |
1099 | |
1079c8a8 |
1100 | if (empty($_COOKIE[$cookiename])) { |
b0ccd3fb |
1101 | return ''; |
1079c8a8 |
1102 | } else { |
1103 | return rc4decrypt($_COOKIE[$cookiename]); |
1104 | } |
f9903ed0 |
1105 | } |
1106 | |
7cf1c7bd |
1107 | /** |
1108 | * Returns true if an internal authentication method is being used. |
1109 | * if method not specified then, global default is assumed |
1110 | * |
1111 | * @uses $CFG |
1112 | * @param string $auth Form of authentication required |
1113 | * @return boolean |
1114 | * @todo Outline auth types and provide code example |
1115 | */ |
39a5a35d |
1116 | function is_internal_auth($auth='') { |
ba7166c3 |
1117 | /// Returns true if an internal authentication method is being used. |
a3f1f815 |
1118 | /// If auth not specified then global default is assumed |
ba7166c3 |
1119 | |
1120 | global $CFG; |
1121 | |
a3f1f815 |
1122 | if (empty($auth)) { |
1123 | $auth = $CFG->auth; |
39a5a35d |
1124 | } |
1125 | |
a3f1f815 |
1126 | return ($auth == "email" || $auth == "none" || $auth == "manual"); |
1127 | } |
1128 | |
8c3dba73 |
1129 | /** |
1130 | * Returns an array of user fields |
1131 | * |
c6d15803 |
1132 | * @uses $CFG |
1133 | * @uses $db |
1134 | * @return array User field/column names |
8c3dba73 |
1135 | * @todo Finish documenting this function |
1136 | */ |
a3f1f815 |
1137 | function get_user_fieldnames() { |
a3f1f815 |
1138 | |
1139 | global $CFG, $db; |
1140 | |
1141 | $fieldarray = $db->MetaColumnNames($CFG->prefix.'user'); |
1142 | unset($fieldarray['ID']); |
1143 | |
1144 | return $fieldarray; |
ba7166c3 |
1145 | } |
f9903ed0 |
1146 | |
7cf1c7bd |
1147 | /** |
1148 | * Creates a bare-bones user record |
1149 | * |
1150 | * @uses $CFG |
7cf1c7bd |
1151 | * @param string $username New user's username to add to record |
1152 | * @param string $password New user's password to add to record |
1153 | * @param string $auth Form of authentication required |
89dcb99d |
1154 | * @return user A {@link $USER} object |
7cf1c7bd |
1155 | * @todo Outline auth types and provide code example |
1156 | */ |
71f9abf9 |
1157 | function create_user_record($username, $password, $auth='') { |
366dfa60 |
1158 | global $CFG; |
71f9abf9 |
1159 | |
1e22bc9c |
1160 | //just in case check text case |
1161 | $username = trim(moodle_strtolower($username)); |
71f9abf9 |
1162 | |
3271b70f |
1163 | if (function_exists('auth_get_userinfo')) { |
e858f9da |
1164 | if ($newinfo = auth_get_userinfo($username)) { |
34daec9b |
1165 | foreach ($newinfo as $key => $value){ |
9f44d972 |
1166 | $newuser->$key = addslashes(stripslashes($value)); // Just in case |
e858f9da |
1167 | } |
1168 | } |
1169 | } |
f9903ed0 |
1170 | |
85a1d4c9 |
1171 | if (!empty($newuser->email)) { |
1172 | if (email_is_not_allowed($newuser->email)) { |
1173 | unset($newuser->email); |
1174 | } |
1175 | } |
1176 | |
71f9abf9 |
1177 | $newuser->auth = (empty($auth)) ? $CFG->auth : $auth; |
faebaf0f |
1178 | $newuser->username = $username; |
1179 | $newuser->password = md5($password); |
a0bac19d |
1180 | $newuser->lang = $CFG->lang; |
faebaf0f |
1181 | $newuser->confirmed = 1; |
59619427 |
1182 | $newuser->lastIP = getremoteaddr(); |
faebaf0f |
1183 | $newuser->timemodified = time(); |
f9903ed0 |
1184 | |
b0ccd3fb |
1185 | if (insert_record('user', $newuser)) { |
1186 | $user = get_user_info_from_db('username', $newuser->username); |
d35757eb |
1187 | if($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'}){ |
1188 | set_user_preference('auth_forcepasswordchange', 1, $user); |
1189 | } |
1190 | return $user; |
faebaf0f |
1191 | } |
1192 | return false; |
1193 | } |
1194 | |
7cf1c7bd |
1195 | /** |
1196 | * Will update a local user record from an external source |
1197 | * |
1198 | * @uses $CFG |
1199 | * @param string $username New user's username to add to record |
89dcb99d |
1200 | * @return user A {@link $USER} object |
7cf1c7bd |
1201 | */ |
d35757eb |
1202 | function update_user_record($username) { |
d35757eb |
1203 | global $CFG; |
1204 | |
1205 | if (function_exists('auth_get_userinfo')) { |
1206 | $username = trim(moodle_strtolower($username)); /// just in case check text case |
1207 | |
1208 | if ($newinfo = auth_get_userinfo($username)) { |
1209 | foreach ($newinfo as $key => $value){ |
1210 | if (!empty($CFG->{'auth_user_' . $key. '_updatelocal'})) { |
1211 | $value = addslashes(stripslashes($value)); // Just in case |
1212 | set_field('user', $key, $value, 'username', $username); |
1213 | } |
1214 | } |
1215 | } |
1216 | } |
b0ccd3fb |
1217 | return get_user_info_from_db('username', $username); |
d35757eb |
1218 | } |
0609562b |
1219 | |
7cf1c7bd |
1220 | /** |
1221 | * Retrieve the guest user object |
1222 | * |
1223 | * @uses $CFG |
89dcb99d |
1224 | * @return user A {@link $USER} object |
7cf1c7bd |
1225 | */ |
0609562b |
1226 | function guest_user() { |
1227 | global $CFG; |
1228 | |
b0ccd3fb |
1229 | if ($newuser = get_record('user', 'username', 'guest')) { |
0609562b |
1230 | $newuser->loggedin = true; |
1231 | $newuser->confirmed = 1; |
1232 | $newuser->site = $CFG->wwwroot; |
1233 | $newuser->lang = $CFG->lang; |
366dfa60 |
1234 | $newuser->lastIP = getremoteaddr(); |
0609562b |
1235 | } |
1236 | |
1237 | return $newuser; |
1238 | } |
1239 | |
7cf1c7bd |
1240 | /** |
1241 | * Given a username and password, this function looks them |
1242 | * up using the currently selected authentication mechanism, |
1243 | * and if the authentication is successful, it returns a |
1244 | * valid $user object from the 'user' table. |
1245 | * |
1246 | * Uses auth_ functions from the currently active auth module |
1247 | * |
1248 | * @uses $CFG |
c6d15803 |
1249 | * @param string $username User's username |
1250 | * @param string $password User's password |
89dcb99d |
1251 | * @return user|flase A {@link $USER} object or false if error |
7cf1c7bd |
1252 | */ |
faebaf0f |
1253 | function authenticate_user_login($username, $password) { |
faebaf0f |
1254 | |
1255 | global $CFG; |
1256 | |
466558e3 |
1257 | $md5password = md5($password); |
1258 | |
27286aeb |
1259 | // First try to find the user in the database |
466558e3 |
1260 | |
b0ccd3fb |
1261 | $user = get_user_info_from_db('username', $username); |
39a5a35d |
1262 | |
27286aeb |
1263 | // Sort out the authentication method we are using. |
39a5a35d |
1264 | |
27286aeb |
1265 | if (empty($CFG->auth)) { |
b0ccd3fb |
1266 | $CFG->auth = 'manual'; // Default authentication module |
27286aeb |
1267 | } |
39a5a35d |
1268 | |
27286aeb |
1269 | if (empty($user->auth)) { // For some reason it isn't set yet |
1270 | if (isadmin($user->id) or isguest($user->id)) { |
71f9abf9 |
1271 | $auth = 'manual'; // Always assume these guys are internal |
27286aeb |
1272 | } else { |
71f9abf9 |
1273 | $auth = $CFG->auth; // Normal users default to site method |
27286aeb |
1274 | } |
d35757eb |
1275 | // update user record from external DB |
1276 | if ($user->auth != 'manual' && $user->auth != 'email') { |
1277 | $user = update_user_record($username); |
1278 | } |
71f9abf9 |
1279 | } else { |
1280 | $auth = $user->auth; |
27286aeb |
1281 | } |
8f0cd6ef |
1282 | |
ce791f88 |
1283 | if (detect_munged_arguments($auth, 0)) { // For safety on the next require |
1284 | return false; |
1285 | } |
1286 | |
b0ccd3fb |
1287 | if (!file_exists($CFG->dirroot .'/auth/'. $auth .'/lib.php')) { |
1288 | $auth = 'manual'; // Can't find auth module, default to internal |
466558e3 |
1289 | } |
1290 | |
b0ccd3fb |
1291 | require_once($CFG->dirroot .'/auth/'. $auth .'/lib.php'); |
faebaf0f |
1292 | |
1293 | if (auth_user_login($username, $password)) { // Successful authentication |
71f9abf9 |
1294 | if ($user) { // User already exists in database |
1295 | if (empty($user->auth)) { // For some reason auth isn't set yet |
1296 | set_field('user', 'auth', $auth, 'username', $username); |
1297 | } |
92710226 |
1298 | if ($md5password <> $user->password) { // Update local copy of password for reference |
71f9abf9 |
1299 | set_field('user', 'password', $md5password, 'username', $username); |
faebaf0f |
1300 | } |
366dfa60 |
1301 | if (!is_internal_auth()) { // update user record from external DB |
d35757eb |
1302 | $user = update_user_record($username); |
1303 | } |
faebaf0f |
1304 | } else { |
71f9abf9 |
1305 | $user = create_user_record($username, $password, $auth); |
faebaf0f |
1306 | } |
89b54325 |
1307 | |
e582b65e |
1308 | if (function_exists('auth_iscreator')) { // Check if the user is a creator |
1309 | if (auth_iscreator($username)) { |
b0ccd3fb |
1310 | if (! record_exists('user_coursecreators', 'userid', $user->id)) { |
39a5a35d |
1311 | $cdata->userid = $user->id; |
b0ccd3fb |
1312 | if (! insert_record('user_coursecreators', $cdata)) { |
1313 | error('Cannot add user to course creators.'); |
39a5a35d |
1314 | } |
1315 | } |
e582b65e |
1316 | } else { |
366dfa60 |
1317 | if (record_exists('user_coursecreators', 'userid', $user->id)) { |
b0ccd3fb |
1318 | if (! delete_records('user_coursecreators', 'userid', $user->id)) { |
1319 | error('Cannot remove user from course creators.'); |
39a5a35d |
1320 | } |
1321 | } |
e582b65e |
1322 | } |
39a5a35d |
1323 | } |
366dfa60 |
1324 | $user->sessionIP = md5(getremoteaddr()); // Store the current IP in the session |
e582b65e |
1325 | return $user; |
9d3c795c |
1326 | |
e582b65e |
1327 | } else { |
b0ccd3fb |
1328 | add_to_log(0, 'login', 'error', $_SERVER['HTTP_REFERER'], $username); |
9d3c795c |
1329 | $date = date('Y-m-d H:i:s'); |
b0ccd3fb |
1330 | error_log($date ."\tfailed login\t". getremoteaddr() ."\t". $_SERVER['HTTP_USER_AGENT'] ."\t". $username); |
e582b65e |
1331 | return false; |
1332 | } |
f9903ed0 |
1333 | } |
1334 | |
7cf1c7bd |
1335 | /** |
1336 | * Enrols (or re-enrols) a student in a given course |
1337 | * |
c6d15803 |
1338 | * @param int $courseid The id of the course that is being viewed |
1339 | * @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user. |
1340 | * @param int $timestart ? |
1341 | * @param int $timeend ? |
7cf1c7bd |
1342 | * @return boolean |
1343 | * @todo Finish documenting this function |
1344 | */ |
b40bc478 |
1345 | function enrol_student($userid, $courseid, $timestart=0, $timeend=0) { |
b40bc478 |
1346 | |
b0ccd3fb |
1347 | if (!$course = get_record('course', 'id', $courseid)) { // Check course |
3041b0f8 |
1348 | return false; |
4d312bbe |
1349 | } |
b0ccd3fb |
1350 | if (!$user = get_record('user', 'id', $userid)) { // Check user |
631cf796 |
1351 | return false; |
1352 | } |
b0ccd3fb |
1353 | if ($student = get_record('user_students', 'userid', $userid, 'course', $courseid)) { |
631cf796 |
1354 | $student->timestart = $timestart; |
1355 | $student->timeend = $timeend; |
1356 | $student->time = time(); |
b0ccd3fb |
1357 | return update_record('user_students', $student); |
8f0cd6ef |
1358 | |
631cf796 |
1359 | } else { |
1360 | $student->userid = $userid; |
1361 | $student->course = $courseid; |
1362 | $student->timestart = $timestart; |
1363 | $student->timeend = $timeend; |
1364 | $student->time = time(); |
b0ccd3fb |
1365 | return insert_record('user_students', $student); |
631cf796 |
1366 | } |
d7facad8 |
1367 | } |
1368 | |
7cf1c7bd |
1369 | /** |
1370 | * Unenrols a student from a given course |
1371 | * |
c6d15803 |
1372 | * @param int $courseid The id of the course that is being viewed, if any |
1373 | * @param int $userid The id of the user that is being tested against. |
7cf1c7bd |
1374 | * @return boolean |
1375 | */ |
9fa62805 |
1376 | function unenrol_student($userid, $courseid=0) { |
d7facad8 |
1377 | |
9fa62805 |
1378 | if ($courseid) { |
9fa49e22 |
1379 | /// First delete any crucial stuff that might still send mail |
b0ccd3fb |
1380 | if ($forums = get_records('forum', 'course', $courseid)) { |
9fa49e22 |
1381 | foreach ($forums as $forum) { |
b0ccd3fb |
1382 | delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid); |
9fa62805 |
1383 | } |
1384 | } |
1385 | if ($groups = get_groups($courseid, $userid)) { |
1386 | foreach ($groups as $group) { |
b0ccd3fb |
1387 | delete_records('groups_members', 'groupid', $group->id, 'userid', $userid); |
bb09fb11 |
1388 | } |
f9903ed0 |
1389 | } |
b0ccd3fb |
1390 | return delete_records('user_students', 'userid', $userid, 'course', $courseid); |
9fa49e22 |
1391 | |
f9903ed0 |
1392 | } else { |
b0ccd3fb |
1393 | delete_records('forum_subscriptions', 'userid', $userid); |
1394 | delete_records('groups_members', 'userid', $userid); |
1395 | return delete_records('user_students', 'userid', $userid); |
f9903ed0 |
1396 | } |
1397 | } |
1398 | |
7cf1c7bd |
1399 | /** |
1400 | * Add a teacher to a given course |
1401 | * |
1402 | * @uses $USER |
c6d15803 |
1403 | * @param int $courseid The id of the course that is being viewed, if any |
1404 | * @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user. |
1405 | * @param int $editall ? |
7cf1c7bd |
1406 | * @param string $role ? |
c6d15803 |
1407 | * @param int $timestart ? |
1408 | * @param int $timeend ? |
7cf1c7bd |
1409 | * @return boolean |
1410 | * @todo Finish documenting this function |
1411 | */ |
b0ccd3fb |
1412 | function add_teacher($userid, $courseid, $editall=1, $role='', $timestart=0, $timeend=0) { |
7b5944cd |
1413 | global $CFG; |
3041b0f8 |
1414 | |
61451a36 |
1415 | if ($teacher = get_record('user_teachers', 'userid', $userid, 'course', $courseid)) { |
b40bc478 |
1416 | $newteacher = NULL; |
1417 | $newteacher->id = $teacher->id; |
1418 | $newteacher->editall = $editall; |
1419 | if ($role) { |
1420 | $newteacher->role = $role; |
1421 | } |
1422 | if ($timestart) { |
1423 | $newteacher->timestart = $timestart; |
3041b0f8 |
1424 | } |
b40bc478 |
1425 | if ($timeend) { |
1426 | $newteacher->timeend = $timeend; |
1427 | } |
1428 | return update_record('user_teachers', $newteacher); |
3041b0f8 |
1429 | } |
61451a36 |
1430 | |
b0ccd3fb |
1431 | if (!record_exists('user', 'id', $userid)) { |
61451a36 |
1432 | return false; // no such user |
1433 | } |
1434 | |
b0ccd3fb |
1435 | if (!record_exists('course', 'id', $courseid)) { |
61451a36 |
1436 | return false; // no such course |
1437 | } |
1438 | |
1439 | $teacher = NULL; |
1440 | $teacher->userid = $userid; |
1441 | $teacher->course = $courseid; |
1442 | $teacher->editall = $editall; |
1443 | $teacher->role = $role; |
5a2dea02 |
1444 | $teacher->timemodified = time(); |
1445 | $newteacher->timestart = $timestart; |
1446 | $newteacher->timeend = $timeend; |
b0ccd3fb |
1447 | if ($student = get_record('user_students', 'userid', $userid, 'course', $courseid)) { |
5a2dea02 |
1448 | $teacher->timestart = $student->timestart; |
1449 | $teacher->timeend = $student->timeend; |
1450 | $teacher->timeaccess = $student->timeaccess; |
1451 | } |
61451a36 |
1452 | |
b0ccd3fb |
1453 | if (record_exists('user_teachers', 'course', $courseid)) { |
61451a36 |
1454 | $teacher->authority = 2; |
1455 | } else { |
1456 | $teacher->authority = 1; |
1457 | } |
b0ccd3fb |
1458 | delete_records('user_students', 'userid', $userid, 'course', $courseid); // Unenrol as student |
8f0cd6ef |
1459 | |
709f0ec8 |
1460 | /// Add forum subscriptions for new users |
7b5944cd |
1461 | require_once('../mod/forum/lib.php'); |
1462 | forum_add_user($userid, $courseid); |
61451a36 |
1463 | |
b0ccd3fb |
1464 | return insert_record('user_teachers', $teacher); |
61451a36 |
1465 | |
3041b0f8 |
1466 | } |
1467 | |
7cf1c7bd |
1468 | /** |
1469 | * Removes a teacher from a given course (or ALL courses) |
1470 | * Does not delete the user account |
1471 | * |
c6d15803 |
1472 | * @param int $courseid The id of the course that is being viewed, if any |
1473 | * @param int $userid The id of the user that is being tested against. |
7cf1c7bd |
1474 | * @return boolean |
1475 | */ |
3041b0f8 |
1476 | function remove_teacher($userid, $courseid=0) { |
3041b0f8 |
1477 | if ($courseid) { |
9fa49e22 |
1478 | /// First delete any crucial stuff that might still send mail |
b0ccd3fb |
1479 | if ($forums = get_records('forum', 'course', $courseid)) { |
9fa49e22 |
1480 | foreach ($forums as $forum) { |
b0ccd3fb |
1481 | delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid); |
9fa49e22 |
1482 | } |
1483 | } |
b02193e6 |
1484 | |
1485 | /// Next if the teacher is not registered as a student, but is |
1486 | /// a member of a group, remove them from the group. |
1487 | if (!isstudent($courseid, $userid)) { |
1488 | if ($groups = get_groups($courseid, $userid)) { |
1489 | foreach ($groups as $group) { |
b0ccd3fb |
1490 | delete_records('groups_members', 'groupid', $group->id, 'userid', $userid); |
b02193e6 |
1491 | } |
1492 | } |
1493 | } |
1494 | |
b0ccd3fb |
1495 | return delete_records('user_teachers', 'userid', $userid, 'course', $courseid); |
57507290 |
1496 | } else { |
b0ccd3fb |
1497 | delete_records('forum_subscriptions', 'userid', $userid); |
1498 | return delete_records('user_teachers', 'userid', $userid); |
57507290 |
1499 | } |
f9903ed0 |
1500 | } |
1501 | |
7cf1c7bd |
1502 | /** |
1503 | * Add a creator to the site |
1504 | * |
c6d15803 |
1505 | * @param int $userid The id of the user that is being tested against. |
7cf1c7bd |
1506 | * @return boolean |
1507 | */ |
3041b0f8 |
1508 | function add_creator($userid) { |
3041b0f8 |
1509 | |
b0ccd3fb |
1510 | if (!record_exists('user_admins', 'userid', $userid)) { |
1511 | if (record_exists('user', 'id', $userid)) { |
3041b0f8 |
1512 | $creator->userid = $userid; |
b0ccd3fb |
1513 | return insert_record('user_coursecreators', $creator); |
3041b0f8 |
1514 | } |
1515 | return false; |
1516 | } |
1517 | return true; |
1518 | } |
1519 | |
7cf1c7bd |
1520 | /** |
1521 | * Remove a creator from a site |
1522 | * |
1523 | * @uses $db |
c6d15803 |
1524 | * @param int $userid The id of the user that is being tested against. |
7cf1c7bd |
1525 | * @return boolean |
1526 | */ |
3041b0f8 |
1527 | function remove_creator($userid) { |
3041b0f8 |
1528 | global $db; |
1529 | |
b0ccd3fb |
1530 | return delete_records('user_coursecreators', 'userid', $userid); |
3041b0f8 |
1531 | } |
1532 | |
7cf1c7bd |
1533 | /** |
1534 | * Add an admin to a site |
1535 | * |
1536 | * @uses SITEID |
c6d15803 |
1537 | * @param int $userid The id of the user that is being tested against. |
7cf1c7bd |
1538 | * @return boolean |
1539 | */ |
3041b0f8 |
1540 | function add_admin($userid) { |
3041b0f8 |
1541 | |
b0ccd3fb |
1542 | if (!record_exists('user_admins', 'userid', $userid)) { |
1543 | if (record_exists('user', 'id', $userid)) { |
3041b0f8 |
1544 | $admin->userid = $userid; |
8f0cd6ef |
1545 | |
f950af3c |
1546 | // any admin is also a teacher on the site course |
222ac91b |
1547 | if (!record_exists('user_teachers', 'course', SITEID, 'userid', $userid)) { |
1548 | if (!add_teacher($userid, SITEID)) { |
f950af3c |
1549 | return false; |
1550 | } |
1551 | } |
8f0cd6ef |
1552 | |
b0ccd3fb |
1553 | return insert_record('user_admins', $admin); |
3041b0f8 |
1554 | } |
1555 | return false; |
1556 | } |
1557 | return true; |
1558 | } |
1559 | |
7cf1c7bd |
1560 | /** |
1561 | * Removes an admin from a site |
1562 | * |
1563 | * @uses $db |
1564 | * @uses SITEID |
c6d15803 |
1565 | * @param int $userid The id of the user that is being tested against. |
7cf1c7bd |
1566 | * @return boolean |
1567 | */ |
3041b0f8 |
1568 | function remove_admin($userid) { |
9fa49e22 |
1569 | global $db; |
f9903ed0 |
1570 | |
f950af3c |
1571 | // remove also from the list of site teachers |
222ac91b |
1572 | remove_teacher($userid, SITEID); |
f950af3c |
1573 | |
b0ccd3fb |
1574 | return delete_records('user_admins', 'userid', $userid); |
f9903ed0 |
1575 | } |
1576 | |
7cf1c7bd |
1577 | /** |
1578 | * Clear a course out completely, deleting all content |
1579 | * but don't delete the course itself |
1580 | * |
1581 | * @uses $USER |
1582 | * @uses $SESSION |
1583 | * @uses $CFG |
c6d15803 |
1584 | * @param int $courseid The id of the course that is being viewed |
7cf1c7bd |
1585 | * @param boolean $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps. |
1586 | * @return boolean |
1587 | */ |
07aeb7b0 |
1588 | function remove_course_contents($courseid, $showfeedback=true) { |
07aeb7b0 |
1589 | |
ee23f384 |
1590 | global $CFG, $THEME, $USER, $SESSION; |
07aeb7b0 |
1591 | |
1592 | $result = true; |
1593 | |
b0ccd3fb |
1594 | if (! $course = get_record('course', 'id', $courseid)) { |
1595 | error('Course ID was incorrect (can\'t find it)'); |
07aeb7b0 |
1596 | } |
1597 | |
b0ccd3fb |
1598 | $strdeleted = get_string('deleted'); |
07aeb7b0 |
1599 | |
1600 | // First delete every instance of every module |
d8ba183c |
1601 | |
b0ccd3fb |
1602 | if ($allmods = get_records('modules') ) { |
07aeb7b0 |
1603 | foreach ($allmods as $mod) { |
1604 | $modname = $mod->name; |
b0ccd3fb |
1605 | $modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php'; |
1606 | $moddelete = $modname .'_delete_instance'; // Delete everything connected to an instance |
1607 | $moddeletecourse = $modname .'_delete_course'; // Delete other stray stuff (uncommon) |
07aeb7b0 |
1608 | $count=0; |
1609 | if (file_exists($modfile)) { |
1610 | include_once($modfile); |
1611 | if (function_exists($moddelete)) { |
b0ccd3fb |
1612 | if ($instances = get_records($modname, 'course', $course->id)) { |
07aeb7b0 |
1613 | foreach ($instances as $instance) { |
1614 | if ($moddelete($instance->id)) { |
1615 | $count++; |
1616 | } else { |
b0ccd3fb |
1617 | notify('Could not delete '. $modname .' instance '. $instance->id .' ('. $instance->name .')'); |
07aeb7b0 |
1618 | $result = false; |
1619 | } |
1620 | } |
1621 | } |
1622 | } else { |
b0ccd3fb |
1623 | notify('Function '. $moddelete() .'doesn\'t exist!'); |
07aeb7b0 |
1624 | $result = false; |
1625 | } |
1626 | |
ca952b03 |
1627 | if (function_exists($moddeletecourse)) { |
1628 | $moddeletecourse($course); |
1629 | } |
07aeb7b0 |
1630 | } |
1631 | if ($showfeedback) { |
b0ccd3fb |
1632 | notify($strdeleted .' '. $count .' x '. $modname); |
07aeb7b0 |
1633 | } |
1634 | } |
1635 | } else { |
b0ccd3fb |
1636 | error('No modules are installed!'); |
07aeb7b0 |
1637 | } |
1638 | |
1639 | // Delete any user stuff |
1640 | |
b0ccd3fb |
1641 | if (delete_records('user_students', 'course', $course->id)) { |
07aeb7b0 |
1642 | if ($showfeedback) { |
b0ccd3fb |
1643 | notify($strdeleted .' user_students'); |
07aeb7b0 |
1644 | } |
1645 | } else { |
1646 | $result = false; |
1647 | } |
1648 | |
b0ccd3fb |
1649 | if (delete_records('user_teachers', 'course', $course->id)) { |
07aeb7b0 |
1650 | if ($showfeedback) { |
b0ccd3fb |
1651 | notify($strdeleted .' user_teachers'); |
07aeb7b0 |
1652 | } |
1653 | } else { |
1654 | $result = false; |
1655 | } |
1656 | |
082e3ebc |
1657 | // Delete any groups |
1658 | |
b0ccd3fb |
1659 | if ($groups = get_records('groups', 'courseid', $course->id)) { |
082e3ebc |
1660 | foreach ($groups as $group) { |
b0ccd3fb |
1661 | if (delete_records('groups_members', 'groupid', $group->id)) { |
082e3ebc |
1662 | if ($showfeedback) { |
b0ccd3fb |
1663 | notify($strdeleted .' groups_members'); |
082e3ebc |
1664 | } |
1665 | } else { |
1666 | $result = false; |
1667 | } |
b0ccd3fb |
1668 | if (delete_records('groups', 'id', $group->id)) { |
082e3ebc |
1669 | if ($showfeedback) { |
b0ccd3fb |
1670 | notify($strdeleted .' groups'); |
082e3ebc |
1671 | } |
1672 | } else { |
1673 | $result = false; |
1674 | } |
1675 | } |
1676 | } |
1677 | |
1678 | // Delete events |
1679 | |
b0ccd3fb |
1680 | if (delete_records('event', 'courseid', $course->id)) { |
082e3ebc |
1681 | if ($showfeedback) { |
b0ccd3fb |
1682 | notify($strdeleted .' event'); |
082e3ebc |
1683 | } |
1684 | } else { |
1685 | $result = false; |
1686 | } |
1687 | |
07aeb7b0 |
1688 | // Delete logs |
1689 | |
b0ccd3fb |
1690 | if (delete_records('log', 'course', $course->id)) { |
07aeb7b0 |
1691 | if ($showfeedback) { |
b0ccd3fb |
1692 | notify($strdeleted .' log'); |
07aeb7b0 |
1693 | } |
1694 | } else { |
1695 | $result = false; |
1696 | } |
1697 | |
1698 | // Delete any course stuff |
1699 | |
b0ccd3fb |
1700 | if (delete_records('course_sections', 'course', $course->id)) { |
07aeb7b0 |
1701 | if ($showfeedback) { |
b0ccd3fb |
1702 | notify($strdeleted .' course_sections'); |
07aeb7b0 |
1703 | } |
1704 | } else { |
1705 | $result = false; |
1706 | } |
1707 | |
b0ccd3fb |
1708 | if (delete_records('course_modules', 'course', $course->id)) { |
07aeb7b0 |
1709 | if ($showfeedback) { |
b0ccd3fb |
1710 | notify($strdeleted .' course_modules'); |
07aeb7b0 |
1711 | } |
1712 | } else { |
1713 | $result = false; |
1714 | } |
1715 | |
1716 | return $result; |
1717 | |
1718 | } |
1719 | |
7cf1c7bd |
1720 | /** |
1721 | * This function will empty a course of USER data as much as |
1722 | /// possible. It will retain the activities and the structure |
1723 | /// of the course. |
1724 | * |
1725 | * @uses $USER |
1726 | * @uses $THEME |
1727 | * @uses $SESSION |
1728 | * @uses $CFG |
c6d15803 |
1729 | * @param int $courseid The id of the course that is being viewed |
7cf1c7bd |
1730 | * @param boolean $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps. |
1731 | * @param boolean $removestudents ? |
1732 | * @param boolean $removeteachers ? |
1733 | * @param boolean $removegroups ? |
1734 | * @param boolean $removeevents ? |
1735 | * @param boolean $removelogs ? |
1736 | * @return boolean |
1737 | * @todo Finish documenting this function |
1738 | */ |
3831de52 |
1739 | function remove_course_userdata($courseid, $showfeedback=true, |
1740 | $removestudents=true, $removeteachers=false, $removegroups=true, |
1741 | $removeevents=true, $removelogs=false) { |
3831de52 |
1742 | |
1743 | global $CFG, $THEME, $USER, $SESSION; |
1744 | |
1745 | $result = true; |
1746 | |
b0ccd3fb |
1747 | if (! $course = get_record('course', 'id', $courseid)) { |
1748 | error('Course ID was incorrect (can\'t find it)'); |
3831de52 |
1749 | } |
1750 | |
b0ccd3fb |
1751 | $strdeleted = get_string('deleted'); |
3831de52 |
1752 | |
1753 | // Look in every instance of every module for data to delete |
1754 | |
b0ccd3fb |
1755 | if ($allmods = get_records('modules') ) { |
3831de52 |
1756 | foreach ($allmods as $mod) { |
1757 | $modname = $mod->name; |
b0ccd3fb |
1758 | $modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php'; |
1759 | $moddeleteuserdata = $modname .'_delete_userdata'; // Function to delete user data |
3831de52 |
1760 | $count=0; |
1761 | if (file_exists($modfile)) { |
1762 | @include_once($modfile); |
1763 | if (function_exists($moddeleteuserdata)) { |
1764 | $moddeleteuserdata($course, $showfeedback); |
1765 | } |
1766 | } |
1767 | } |
1768 | } else { |
b0ccd3fb |
1769 | error('No modules are installed!'); |
3831de52 |
1770 | } |
1771 | |
1772 | // Delete other stuff |
1773 | |
1774 | if ($removestudents) { |
1775 | /// Delete student enrolments |
b0ccd3fb |
1776 | if (delete_records('user_students', 'course', $course->id)) { |
3831de52 |
1777 | if ($showfeedback) { |
b0ccd3fb |
1778 | notify($strdeleted .' user_students'); |
3831de52 |
1779 | } |
1780 | } else { |
1781 | $result = false; |
1782 | } |
1783 | /// Delete group members (but keep the groups) |
b0ccd3fb |
1784 | if ($groups = get_records('groups', 'courseid', $course->id)) { |
3831de52 |
1785 | foreach ($groups as $group) { |
b0ccd3fb |
1786 | if (delete_records('groups_members', 'groupid', $group->id)) { |
3831de52 |
1787 | if ($showfeedback) { |
b0ccd3fb |
1788 | notify($strdeleted .' groups_members'); |
3831de52 |
1789 | } |
1790 | } else { |
1791 | $result = false; |
1792 | } |
1793 | } |
1794 | } |
1795 | } |
1796 | |
1797 | if ($removeteachers) { |
b0ccd3fb |
1798 | if (delete_records('user_teachers', 'course', $course->id)) { |
3831de52 |
1799 | if ($showfeedback) { |
b0ccd3fb |
1800 | notify($strdeleted .' user_teachers'); |
3831de52 |
1801 | } |
1802 | } else { |
1803 | $result = false; |
1804 | } |
1805 | } |
1806 | |
1807 | if ($removegroups) { |
b0ccd3fb |
1808 | if ($groups = get_records('groups', 'courseid', $course->id)) { |
3831de52 |
1809 | foreach ($groups as $group) { |
b0ccd3fb |
1810 | if (delete_records('groups', 'id', $group->id)) { |
3831de52 |
1811 | if ($showfeedback) { |
b0ccd3fb |
1812 | notify($strdeleted .' groups'); |
3831de52 |
1813 | } |
1814 | } else { |
1815 | $result = false; |
1816 | } |
1817 | } |
1818 | } |
1819 | } |
1820 | |
1821 | if ($removeevents) { |
b0ccd3fb |
1822 | if (delete_records('event', 'courseid', $course->id)) { |
3831de52 |
1823 | if ($showfeedback) { |
b0ccd3fb |
1824 | notify($strdeleted .' event'); |
3831de52 |
1825 | } |
1826 | } else { |
1827 | $result = false; |
1828 | } |
1829 | } |
1830 | |
1831 | if ($removelogs) { |
b0ccd3fb |
1832 | if (delete_records('log', 'course', $course->id)) { |
3831de52 |
1833 | if ($showfeedback) { |
b0ccd3fb |
1834 | notify($strdeleted .' log'); |
3831de52 |
1835 | } |
1836 | } else { |
1837 | $result = false; |
1838 | } |
1839 | } |
1840 | |
1841 | return $result; |
1842 | |
1843 | } |
1844 | |
1845 | |
f9903ed0 |
1846 | |
f374fb10 |
1847 | /// GROUPS ///////////////////////////////////////////////////////// |
d8ba183c |
1848 | |
f374fb10 |
1849 | |
1850 | /** |
1851 | * Returns a boolean: is the user a member of the given group? |
d8ba183c |
1852 | * |
dcd338ff |
1853 | * @param type description |
7cf1c7bd |
1854 | * @todo Finish documenting this function |
f374fb10 |
1855 | */ |
1856 | function ismember($groupid, $userid=0) { |
1857 | global $USER; |
1858 | |
8a2c9076 |
1859 | if (!$groupid) { // No point doing further checks |
1860 | return false; |
1861 | } |
1862 | |
f374fb10 |
1863 | if (!$userid) { |
0d67c514 |
1864 | if (empty($USER->groupmember)) { |
1865 | return false; |
1866 | } |
1867 | foreach ($USER->groupmember as $courseid => $mgroupid) { |
1868 | if ($mgroupid == $groupid) { |
1869 | return true; |
1870 | } |
1871 | } |
1872 | return false; |
f374fb10 |
1873 | } |
1874 | |
b0ccd3fb |
1875 | return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid); |
f374fb10 |
1876 | } |
1877 | |
0d67c514 |
1878 | /** |
c6d15803 |
1879 | * Returns the group ID of the current user in the given course |
1880 | * |
1881 | * @uses $USER |
1882 | * @param int $courseid The course being examined - relates to id field in 'course' table. |
7cf1c7bd |
1883 | * @todo Finish documenting this function |
c6d15803 |
1884 | */ |
0d67c514 |
1885 | function mygroupid($courseid) { |
1886 | global $USER; |
1887 | |
1888 | if (empty($USER->groupmember[$courseid])) { |
1889 | return 0; |
1890 | } else { |
1891 | return $USER->groupmember[$courseid]; |
1892 | } |
1893 | } |
1894 | |
f374fb10 |
1895 | /** |
c6d15803 |
1896 | * For a given course, and possibly course module, determine |
1897 | * what the current default groupmode is: |
1898 | * NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS |
1899 | * |
89dcb99d |
1900 | * @param course $course A {@link $COURSE} object |
1901 | * @param array? $cm A course module object |
c6d15803 |
1902 | * @return int A group mode (NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS) |
1903 | */ |
f374fb10 |
1904 | function groupmode($course, $cm=null) { |
1905 | |
1906 | if ($cm and !$course->groupmodeforce) { |
1907 | return $cm->groupmode; |
1908 | } |
1909 | return $course->groupmode; |
1910 | } |
1911 | |
1912 | |
1913 | /** |
c6d15803 |
1914 | * Sets the current group in the session variable |
1915 | * |
1916 | * @uses $SESSION |
1917 | * @param int $courseid The course being examined - relates to id field in 'course' table. |
1918 | * @param int $groupid The group being examined. |
1919 | * @return int Current group id which was set by this function |
7cf1c7bd |
1920 | * @todo Finish documenting this function |
c6d15803 |
1921 | */ |
f374fb10 |
1922 | function set_current_group($courseid, $groupid) { |
1923 | global $SESSION; |
1924 | |
1925 | return $SESSION->currentgroup[$courseid] = $groupid; |
1926 | } |
1927 | |
1928 | |
1929 | /** |
c6d15803 |
1930 | * Gets the current group for the current user as an id or an object |
1931 | * |
1932 | * @uses $CFG |
1933 | * @uses $SESSION |
1934 | * @param int $courseid The course being examined - relates to id field in 'course' table. |
1935 | * @param boolean $full ? |
7cf1c7bd |
1936 | * @todo Finish documenting this function |
c6d15803 |
1937 | */ |
f374fb10 |
1938 | function get_current_group($courseid, $full=false) { |
1939 | global $SESSION, $USER; |
1940 | |
ce04df6b |
1941 | if (!isset($SESSION->currentgroup[$courseid])) { |
f374fb10 |
1942 | if (empty($USER->groupmember[$courseid])) { |
8a2c9076 |
1943 | return 0; |
f374fb10 |
1944 | } else { |
1945 | $SESSION->currentgroup[$courseid] = $USER->groupmember[$courseid]; |
1946 | } |
1947 | } |
1948 | |
1949 | if ($full) { |
0da33e07 |
1950 | return get_record('groups', 'id', $SESSION->currentgroup[$courseid]); |
f374fb10 |
1951 | } else { |
1952 | return $SESSION->currentgroup[$courseid]; |
1953 | } |
1954 | } |
1955 | |
0d67c514 |
1956 | /** |
c6d15803 |
1957 | * A combination function to make it easier for modules |
1958 | * to set up groups. |
1959 | * |
1960 | * It will use a given "groupid" parameter and try to use |
1961 | * that to reset the current group for the user. |
1962 | * |
1963 | * @uses VISIBLEGROUPS |
89dcb99d |
1964 | * @param course $course A {@link $COURSE} object |
c6d15803 |
1965 | * @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS |
1966 | * @param int $groupid Will try to use this optional parameter to |
1967 | * reset the current group for the user |
89dcb99d |
1968 | * @return int|false Returns the current group id or false if error. |
7cf1c7bd |
1969 | * @todo Finish documenting this function |
c6d15803 |
1970 | */ |
eb6147a8 |
1971 | function get_and_set_current_group($course, $groupmode, $groupid=-1) { |
0d67c514 |
1972 | |
1973 | if (!$groupmode) { // Groups don't even apply |
d8ba183c |
1974 | return false; |
0d67c514 |
1975 | } |
1976 | |
1977 | $currentgroupid = get_current_group($course->id); |
1978 | |
eb6147a8 |
1979 | if ($groupid < 0) { // No change was specified |
1980 | return $currentgroupid; |
1981 | } |
1982 | |
1983 | if ($groupid) { // Try to change the current group to this groupid |
0d67c514 |
1984 | if ($group = get_record('groups', 'id', $groupid, 'courseid', $course->id)) { // Exists |
1985 | if (isteacheredit($course->id)) { // Sets current default group |
1986 | $currentgroupid = set_current_group($course->id, $group->id); |
1987 | |
1988 | } else if ($groupmode == VISIBLEGROUPS) { // All groups are visible |
1989 | $currentgroupid = $group->id; |
1990 | } |
1991 | } |
eb6147a8 |
1992 | } else { // When groupid = 0 it means show ALL groups |
1993 | if (isteacheredit($course->id)) { // Sets current default group |
1994 | $currentgroupid = set_current_group($course->id, 0); |
1995 | |
1996 | } else if ($groupmode == VISIBLEGROUPS) { // All groups are visible |
1997 | $currentgroupid = 0; |
1998 | } |
0d67c514 |
1999 | } |
2000 | |
2001 | return $currentgroupid; |
2002 | } |
2003 | |
2004 | |
c3cbfe7f |
2005 | /** |
c6d15803 |
2006 | * A big combination function to make it easier for modules |
2007 | * to set up groups. |
2008 | * |
2009 | * Terminates if the current user shouldn't be looking at this group |
2010 | * Otherwise returns the current group if there is one |
2011 | * Otherwise returns false if groups aren't relevant |
2012 | * |
2013 | * @uses SEPARATEGROUPS |
2014 | * @uses VISIBLEGROUPS |
89dcb99d |
2015 | * @param course $course A {@link $COURSE} object |
c6d15803 |
2016 | * @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS |
2017 | * @param string $urlroot ? |
7cf1c7bd |
2018 | * @todo Finish documenting this function |
c6d15803 |
2019 | */ |
c3cbfe7f |
2020 | function setup_and_print_groups($course, $groupmode, $urlroot) { |
2021 | |
eb6147a8 |
2022 | if (isset($_GET['group'])) { |
2023 | $changegroup = $_GET['group']; /// 0 or higher |
2024 | } else { |
2025 | $changegroup = -1; /// This means no group change was specified |
2026 | } |
2027 | |
2028 | $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup); |
c3cbfe7f |
2029 | |
eb6147a8 |
2030 | if ($currentgroup === false) { |
c3cbfe7f |
2031 | return false; |
2032 | } |
2033 | |
4b6d8dd5 |
2034 | if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id) and !$currentgroup) { |
2035 | print_heading(get_string('notingroup')); |
c3cbfe7f |
2036 | print_footer($course); |
2037 | exit; |
2038 | } |
2039 | |
2040 | if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) { |
b0ccd3fb |
2041 | if ($groups = get_records_menu('groups', 'courseid', $course->id, 'name ASC', 'id,name')) { |
eb6147a8 |
2042 | echo '<div align="center">'; |
c3cbfe7f |
2043 | print_group_menu($groups, $groupmode, $currentgroup, $urlroot); |
eb6147a8 |
2044 | echo '</div>'; |
c3cbfe7f |
2045 | } |
2046 | } |
2047 | |
2048 | return $currentgroup; |
2049 | } |
0d67c514 |
2050 | |
f374fb10 |
2051 | |
2052 | |
f9903ed0 |
2053 | /// CORRESPONDENCE //////////////////////////////////////////////// |
2054 | |
7cf1c7bd |
2055 | /** |
2056 | * Send an email to a specified user |
2057 | * |
7cf1c7bd |
2058 | * @uses $CFG |
2059 | * @uses $_SERVER |
c6d15803 |
2060 | * @uses SITEID |
89dcb99d |
2061 | * @param user $user A {@link $USER} object |
2062 | * @param user $from A {@link $USER} object |
7cf1c7bd |
2063 | * @param string $subject plain text subject line of the email |
2064 | * @param string $messagetext plain text version of the message |
2065 | * @param string $messagehtml complete html version of the message (optional) |
2066 | * @param string $attachment a file on the filesystem, relative to $CFG->dataroot |
2067 | * @param string $attachname the name of the file (extension indicates MIME) |
c6d15803 |
2068 | * @param boolean $usetrueaddress determines whether $from email address should |
2069 | * be sent out. Will be overruled by user profile setting for maildisplay |
2070 | * @return boolean|string Returns "true" if mail was sent OK, "emailstop" if email |
2071 | * was blocked by user and "false" if there was another sort of error. |
7cf1c7bd |
2072 | */ |
b0ccd3fb |
2073 | function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $attachment='', $attachname='', $usetrueaddress=true) { |
f9903ed0 |
2074 | |
4216daa6 |
2075 | global $CFG, $_SERVER; |
f9903ed0 |
2076 | |
0cc6fa6a |
2077 | global $course; // This is a bit of an ugly hack to be gotten rid of later |
2078 | if (!empty($course->lang)) { // Course language is defined |
2079 | $CFG->courselang = $course->lang; |
2080 | } |
2081 | |
b0ccd3fb |
2082 | include_once($CFG->libdir .'/phpmailer/class.phpmailer.php'); |
f9903ed0 |
2083 | |
cadb96f2 |
2084 | if (empty($user)) { |
2085 | return false; |
2086 | } |
2087 | |
2088 | if (!empty($user->emailstop)) { |
579dcca4 |
2089 | return 'emailstop'; |
f9903ed0 |
2090 | } |
d8ba183c |
2091 | |
f9903ed0 |
2092 | $mail = new phpmailer; |
2093 | |
b0ccd3fb |
2094 | $mail->Version = 'Moodle '. $CFG->version; // mailer version |
2095 | $mail->PluginDir = $CFG->libdir .'/phpmailer/'; // plugin directory (eg smtp plugin) |
562bbe90 |
2096 | |
98c4eae3 |
2097 | |
b0ccd3fb |
2098 | if (current_language() != 'en') { |
2099 | $mail->CharSet = get_string('thischarset'); |
98c4eae3 |
2100 | } |
2101 | |
b0ccd3fb |
2102 | if ($CFG->smtphosts == 'qmail') { |
62740736 |
2103 | $mail->IsQmail(); // use Qmail system |
2104 | |
2105 | } else if (empty($CFG->smtphosts)) { |
2106 | $mail->IsMail(); // use PHP mail() = sendmail |
2107 | |
2108 | } else { |
1e411ffc |
2109 | $mail->IsSMTP(); // use SMTP directly |
57ef3480 |
2110 | if ($CFG->debug > 7) { |
b0ccd3fb |
2111 | echo '<pre>' . "\n"; |
57ef3480 |
2112 | $mail->SMTPDebug = true; |
2113 | } |
b0ccd3fb |
2114 | $mail->Host = $CFG->smtphosts; // specify main and backup servers |
9f58537a |
2115 | |
2116 | if ($CFG->smtpuser) { // Use SMTP authentication |
2117 | $mail->SMTPAuth = true; |
2118 | $mail->Username = $CFG->smtpuser; |
2119 | $mail->Password = $CFG->smtppass; |
2120 | } |
7f86ce17 |
2121 | } |
f9903ed0 |
2122 | |
2b97bd71 |
2123 | $adminuser = get_admin(); |
2124 | |
b0ccd3fb |
2125 | $mail->Sender = $adminuser->email; |
2b97bd71 |
2126 | |
a402bdcb |
2127 | if (is_string($from)) { // So we can pass whatever we want if there is need |
2128 | $mail->From = $CFG->noreplyaddress; |
0d8a590a |
2129 | $mail->FromName = $from; |
a402bdcb |
2130 | } else if ($usetrueaddress and $from->maildisplay) { |
b0ccd3fb |
2131 | $mail->From = $from->email; |
6e506bf9 |
2132 | $mail->FromName = fullname($from); |
2133 | } else { |
b0ccd3fb |
2134 | $mail->From = $CFG->noreplyaddress; |
0d8a590a |
2135 | $mail->FromName = fullname($from); |
6e506bf9 |
2136 | } |
136dabd8 |
2137 | $mail->Subject = stripslashes($subject); |
f9903ed0 |
2138 | |
b0ccd3fb |
2139 | $mail->AddAddress($user->email, fullname($user) ); |
f9903ed0 |
2140 | |
58d24720 |
2141 | $mail->WordWrap = 79; // set word wrap |
f9903ed0 |
2142 | |
857b798b |
2143 | if (!empty($from->customheaders)) { // Add custom headers |
2144 | if (is_array($from->customheaders)) { |
2145 | foreach ($from->customheaders as $customheader) { |
2146 | $mail->AddCustomHeader($customheader); |
2147 | } |
2148 | } else { |
2149 | $mail->AddCustomHeader($from->customheaders); |
2150 | } |
b68dca19 |
2151 | } |
8f0cd6ef |
2152 | |
136dabd8 |
2153 | if ($messagehtml) { |
2154 | $mail->IsHTML(true); |
b0ccd3fb |
2155 | $mail->Encoding = 'quoted-printable'; // Encoding to use |
136dabd8 |
2156 | $mail->Body = $messagehtml; |
78681899 |
2157 | $mail->AltBody = "\n$messagetext\n"; |
136dabd8 |
2158 | } else { |
2159 | $mail->IsHTML(false); |
78681899 |
2160 | $mail->Body = "\n$messagetext\n"; |
f9903ed0 |
2161 | } |
2162 | |
136dabd8 |
2163 | if ($attachment && $attachname) { |
2164 | if (ereg( "\\.\\." ,$attachment )) { // Security check for ".." in dir path |
b0ccd3fb |
2165 | $mail->AddAddress($adminuser->email, fullname($adminuser) ); |
2166 | $mail->AddStringAttachment('Error in attachment. User attempted to attach a filename with a unsafe name.', 'error.txt', '8bit', 'text/plain'); |
136dabd8 |
2167 | } else { |
b0ccd3fb |
2168 | include_once($CFG->dirroot .'/files/mimetypes.php'); |
2169 | $mimetype = mimeinfo('type', $attachname); |
2170 | $mail->AddAttachment($CFG->dataroot .'/'. $attachment, $attachname, 'base64', $mimetype); |
136dabd8 |
2171 | } |
f9903ed0 |
2172 | } |
2173 | |
136dabd8 |
2174 | if ($mail->Send()) { |
2175 | return true; |
2176 | } else { |
b0ccd3fb |
2177 | mtrace('ERROR: '. $mail->ErrorInfo); |
2178 | add_to_log(SITEID, 'library', 'mailer', $_SERVER['REQUEST_URI'], 'ERROR: '. $mail->ErrorInfo); |
f9903ed0 |
2179 | return false; |
2180 | } |
f9903ed0 |
2181 | } |
2182 | |
7cf1c7bd |
2183 | /** |
2184 | * Resets specified user's password and send the new password to the user via email. |
2185 | * |
2186 | * @uses $CFG |
89dcb99d |
2187 | * @param user $user A {@link $USER} object |
c6d15803 |
2188 | * @return boolean|string Returns "true" if mail was sent OK, "emailstop" if email |
2189 | * was blocked by user and "false" if there was another sort of error. |
7cf1c7bd |
2190 | */ |
1d881d92 |
2191 | function reset_password_and_mail($user) { |
2192 | |
2193 | global $CFG; |
2194 | |
2195 | $site = get_site(); |
2196 | $from = get_admin(); |
2197 | |
2198 | $newpassword = generate_password(); |
2199 | |
b0ccd3fb |
2200 | if (! set_field('user', 'password', md5($newpassword), 'id', $user->id) ) { |
2201 | error('Could not set user password!'); |
1d881d92 |
2202 | } |
2203 | |
2204 | $a->firstname = $user->firstname; |
2205 | $a->sitename = $site->fullname; |
2206 | $a->username = $user->username; |
2207 | $a->newpassword = $newpassword; |
b0ccd3fb |
2208 | $a->link = $CFG->wwwroot .'/login/change_password.php'; |
2209 | $a->signoff = fullname($from, true).' ('. $from->email .')'; |
1d881d92 |
2210 | |
b0ccd3fb |
2211 | $message = get_string('newpasswordtext', '', $a); |
1d881d92 |
2212 | |
b0ccd3fb |
2213 | $subject = $site->fullname .': '. get_string('changedpassword'); |
1d881d92 |
2214 | |
2215 | return email_to_user($user, $from, $subject, $message); |
2216 | |
2217 | } |
2218 | |
7cf1c7bd |
2219 | /** |
2220 | * Send email to specified user with confirmation text and activation link. |
2221 | * |
2222 | * @uses $CFG |
89dcb99d |
2223 | * @param user $user A {@link $USER} object |
c6d15803 |
2224 | * @return boolean|string Returns "true" if mail was sent OK, "emailstop" if email |
2225 | * was blocked by user and "false" if there was another sort of error. |
7cf1c7bd |
2226 | */ |
2227 | function send_confirmation_email($user) { |
1d881d92 |
2228 | |
2229 | global $CFG; |
2230 | |
2231 | $site = get_site(); |
2232 | $from = get_admin(); |
2233 | |
2234 | $data->firstname = $user->firstname; |
2235 | $data->sitename = $site->fullname; |
b0ccd3fb |
2236 | $data->link = $CFG->wwwroot .'/login/confirm.php?p='. $user->secret .'&s='. $user->username; |
2237 | $data->admin = fullname($from) .' ('. $from->email .')'; |
1d881d92 |
2238 | |
b0ccd3fb |
2239 | $message = get_string('emailconfirmation', '', $data); |
2240 | $subject = get_string('emailconfirmationsubject', '', $site->fullname); |
1d881d92 |
2241 | |
58d24720 |
2242 | $messagehtml = text_to_html($message, false, false, true); |
2243 | |
2244 | return email_to_user($user, $from, $subject, $message, $messagehtml); |
1d881d92 |
2245 | |
2246 | } |
2247 | |
7cf1c7bd |
2248 | /** |
2249 | * send_password_change_confirmation_email. |
2250 | * |
c6d15803 |
2251 | * @uses $CFG |
89dcb99d |
2252 | * @param user $user A {@link $USER} object |
c6d15803 |
2253 | * @return boolean|string Returns "true" if mail was sent OK, "emailstop" if email |
2254 | * was blocked by user and "false" if there was another sort of error. |
7cf1c7bd |
2255 | * @todo Finish documenting this function |
2256 | */ |
eb347b6b |
2257 | function send_password_change_confirmation_email($user) { |
2258 | |
2259 | global $CFG; |
2260 | |
2261 | $site = get_site(); |
2262 | $from = get_admin(); |
2263 | |
2264 | $data->firstname = $user->firstname; |
2265 | $data->sitename = $site->fullname; |
b0ccd3fb |
2266 | $data->link = $CFG->wwwroot .'/login/forgot_password.php?p='. $user->secret .'&s='. $user->username; |
2267 | $data->admin = fullname($from).' ('. $from->email .')'; |
eb347b6b |
2268 | |
b0ccd3fb |
2269 | $message = get_string('emailpasswordconfirmation', '', $data); |
2270 | $subject = get_string('emailpasswordconfirmationsubject', '', $site->fullname); |
eb347b6b |
2271 | |
2272 | return email_to_user($user, $from, $subject, $message); |
2273 | |
2274 | } |
2275 | |
7cf1c7bd |
2276 | /** |
2277 | * Check that an email is allowed. It returns an error message if there |
2278 | * was a problem. |
2279 | * |
2280 | * @param type description |
2281 | * @todo Finish documenting this function |
2282 | */ |
85a1d4c9 |
2283 | function email_is_not_allowed($email) { |
85a1d4c9 |
2284 | |
2285 | global $CFG; |
2286 | |
2287 | if (!empty($CFG->allowemailaddresses)) { |
2288 | $allowed = explode(' ', $CFG->allowemailaddresses); |
2289 | foreach ($allowed as $allowedpattern) { |
2290 | $allowedpattern = trim($allowedpattern); |
2291 | if (!$allowedpattern) { |
2292 | continue; |
2293 | } |
2294 | if (strpos($email, $allowedpattern) !== false) { // Match! |
2295 | return false; |
2296 | } |
2297 | } |
b0ccd3fb |
2298 | return get_string('emailonlyallowed', '', $CFG->allowemailaddresses); |
85a1d4c9 |
2299 | |
2300 | } else if (!empty($CFG->denyemailaddresses)) { |
2301 | $denied = explode(' ', $CFG->denyemailaddresses); |
2302 | foreach ($denied as $deniedpattern) { |
2303 | $deniedpattern = trim($deniedpattern); |
2304 | if (!$deniedpattern) { |
2305 | continue; |
2306 | } |
2307 | if (strpos($email, $deniedpattern) !== false) { // Match! |
b0ccd3fb |
2308 | return get_string('emailnotallowed', '', $CFG->denyemailaddresses); |
85a1d4c9 |
2309 | } |
2310 | } |
2311 | } |
2312 | |
2313 | return false; |
2314 | } |
1d881d92 |
2315 | |
136dabd8 |
2316 | |
f9903ed0 |
2317 | /// FILE HANDLING ///////////////////////////////////////////// |
2318 | |
7cf1c7bd |
2319 | /** |
c6d15803 |
2320 | * Create a directory. |
7cf1c7bd |
2321 | * |
2322 | * @uses $CFG |
8c3dba73 |
2323 | * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1 |
c6d15803 |
2324 | * param boolean $shownotices If true then notification messages will be printed out on error. |
2325 | * @return string|false Returns full path to directory if successful, false if not |
7cf1c7bd |
2326 | */ |
66f9a82c |
2327 | function make_upload_directory($directory, $shownotices=true) { |
6b174680 |
2328 | |
2329 | global $CFG; |
2330 | |
2331 | $currdir = $CFG->dataroot; |
fe287429 |
2332 | |
2e6d4273 |
2333 | umask(0000); |
2334 | |
6b174680 |
2335 | if (!file_exists($currdir)) { |
2e6d4273 |
2336 | if (! mkdir($currdir, $CFG->directorypermissions)) { |
66f9a82c |
2337 | if ($shownotices) { |
b0ccd3fb |
2338 | notify('ERROR: You need to create the directory '. $currdir .' with web server write access'); |
66f9a82c |
2339 | } |
6b174680 |
2340 | return false; |
2341 | } |
2342 | } |
2343 | |
b0ccd3fb |
2344 | $dirarray = explode('/', $directory); |
6b174680 |
2345 | |
2346 | foreach ($dirarray as $dir) { |
b0ccd3fb |
2347 | $currdir = $currdir .'/'. $dir; |
6b174680 |
2348 | if (! file_exists($currdir)) { |
2e6d4273 |
2349 | if (! mkdir($currdir, $CFG->directorypermissions)) { |
66f9a82c |
2350 | if ($shownotices) { |
b0ccd3fb |
2351 | notify('ERROR: Could not find or create a directory ('. $currdir .')'); |
66f9a82c |
2352 | } |
6b174680 |
2353 | return false; |
2354 | } |
feffa4e6 |
2355 | @chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it |
6b174680 |
2356 | } |
2357 | } |
2358 | |
2359 | return $currdir; |
2360 | } |
1e03c552 |
2361 | |
7cf1c7bd |
2362 | /** |
2363 | * Makes an upload directory for a particular module. |
2364 | * |
2365 | * @uses $CFG |
c6d15803 |
2366 | * @param int $courseid The id of the course in question - maps to id field of 'course' table. |
2367 | * @return string|false Returns full path to directory if successful, false if not |
7cf1c7bd |
2368 | * @todo Finish documenting this function |
2369 | */ |
ca4f8eb8 |
2370 | function make_mod_upload_directory($courseid) { |
2371 | global $CFG; |
2372 | |
b0ccd3fb |
2373 | if (! $moddata = make_upload_directory($courseid .'/'. $CFG->moddata)) { |
ca4f8eb8 |
2374 | return false; |
2375 | } |
2376 | |
b0ccd3fb |
2377 | $strreadme = get_string('readme'); |
ca4f8eb8 |
2378 | |
b0ccd3fb |
2379 | if (file_exists($CFG->dirroot .'/lang/'. $CFG->lang .'/docs/module_files.txt')) { |
2380 | copy($CFG->dirroot .'/lang/'. $CFG->lang .'/docs/module_files.txt', $moddata .'/'. $strreadme .'.txt'); |
ca4f8eb8 |
2381 | } else { |
b0ccd3fb |
2382 | copy($CFG->dirroot .'/lang/en/docs/module_files.txt', $moddata .'/'. $strreadme .'.txt'); |
ca4f8eb8 |
2383 | } |
2384 | return $moddata; |
2385 | } |
2386 | |
7cf1c7bd |
2387 | /** |
c6d15803 |
2388 | * Returns current name of file on disk if it exists. |
7cf1c7bd |
2389 | * |
c6d15803 |
2390 | * @param string $newfile File to be verified |
2391 | * @return string Current name of file on disk if true |
7cf1c7bd |
2392 | * @todo Finish documenting this function |
2393 | */ |
44e2d2bb |
2394 | function valid_uploaded_file($newfile) { |
9c9f7d77 |
2395 | if (empty($newfile)) { |
b0ccd3fb |
2396 | return ''; |
9c9f7d77 |
2397 | } |
44e2d2bb |
2398 | if (is_uploaded_file($newfile['tmp_name']) and $newfile['size'] > 0) { |
2399 | return $newfile['tmp_name']; |
2400 | } else { |
b0ccd3fb |
2401 | return ''; |
44e2d2bb |
2402 | } |
2403 | } |
2404 | |
7cf1c7bd |
2405 | /** |
2406 | * Returns the maximum size for uploading files. |
2407 | * |
2408 | * There are seven possible upload limits: |
2409 | * 1. in Apache using LimitRequestBody (no way of checking or changing this) |
2410 | * 2. in php.ini for 'upload_max_filesize' (can not be changed inside PHP) |
2411 | * 3. in .htaccess for 'upload_max_filesize' (can not be changed inside PHP) |
2412 | * 4. in php.ini for 'post_max_size' (can not be changed inside PHP) |
2413 | * 5. by the Moodle admin in $CFG->maxbytes |
2414 | * 6. by the teacher in the current course $course->maxbytes |
2415 | * 7. by the teacher for the current module, eg $assignment->maxbytes |
2416 | * |
2417 | * These last two are passed to this function as arguments (in bytes). |
2418 | * Anything defined as 0 is ignored. |
2419 | * The smallest of all the non-zero numbers is returned. |
2420 | * |
c6d15803 |
2421 | * @param int $sizebytes ? |
2422 | * @param int $coursebytes Current course $course->maxbytes (in bytes) |
2423 | * @param int $modulebytes Current module ->maxbytes (in bytes) |
2424 | * @return int The maximum size for uploading files. |
7cf1c7bd |
2425 | * @todo Finish documenting this function |
2426 | */ |
4909e176 |
2427 | function get_max_upload_file_size($sitebytes=0, $coursebytes=0, $modulebytes=0) { |
4909e176 |
2428 | |
b0ccd3fb |
2429 | if (! $filesize = ini_get('upload_max_filesize')) { |
2430 | $filesize = '5M'; |
44e2d2bb |
2431 | } |
4909e176 |
2432 | $minimumsize = get_real_size($filesize); |
2433 | |
b0ccd3fb |
2434 | if ($postsize = ini_get('post_max_size')) { |
316ebf78 |
2435 | $postsize = get_real_size($postsize); |
2436 | if ($postsize < $minimumsize) { |
2437 | $minimumsize = $postsize; |
2438 | } |
2439 | } |
2440 | |
4909e176 |
2441 | if ($sitebytes and $sitebytes < $minimumsize) { |
2442 | $minimumsize = $sitebytes; |
2443 | } |
2444 | |
2445 | if ($coursebytes and $coursebytes < $minimumsize) { |
2446 | $minimumsize = $coursebytes; |
2447 | } |
2448 | |
2449 | if ($modulebytes and $modulebytes < $minimumsize) { |
2450 | $minimumsize = $modulebytes; |
2451 | } |
2452 | |
2453 | return $minimumsize; |
2454 | } |
2455 | |
7cf1c7bd |
2456 | /** |
2457 | * Related to the above function - this function returns an |
2458 | * array of possible sizes in an array, translated to the |
2459 | * local language. |
2460 | * |
c6d15803 |
2461 | * @uses SORT_NUMERIC |
2462 | * @param int $sizebytes ? |
2463 | * @param int $coursebytes Current course $course->maxbytes (in bytes) |
2464 | * @param int $modulebytes Current module ->maxbytes (in bytes) |
2465 | * @return int |
7cf1c7bd |
2466 | * @todo Finish documenting this function |
2467 | */ |
4909e176 |
2468 | function get_max_upload_sizes($sitebytes=0, $coursebytes=0, $modulebytes=0) { |
4909e176 |
2469 | |
2470 | if (!$maxsize = get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes)) { |
2471 | return array(); |
2472 | } |
2473 | |
2474 | $filesize[$maxsize] = display_size($maxsize); |
2475 | |
d8ba183c |
2476 | $sizelist = array(10240, 51200, 102400, 512000, 1048576, 2097152, |
4909e176 |
2477 | 5242880, 10485760, 20971520, 52428800, 104857600); |
2478 | |
2479 | foreach ($sizelist as $sizebytes) { |
2480 | if ($sizebytes < $maxsize) { |
2481 | $filesize[$sizebytes] = display_size($sizebytes); |
2482 | } |
2483 | } |
2484 | |
2485 | krsort($filesize, SORT_NUMERIC); |
2486 | |
2487 | return $filesize; |
44e2d2bb |
2488 | } |
2489 | |
7cf1c7bd |
2490 | /** |
2491 | * If there has been an error uploading a file, print the appropriate error message |
2492 | * Numerical constants used as constant definitions not added until PHP version 4.2.0 |
2493 | * |
c6d15803 |
2494 | * $filearray is a 1-dimensional sub-array of the $_FILES array |
7cf1c7bd |
2495 | * eg $filearray = $_FILES['userfile1'] |
8c3dba73 |
2496 | * If left empty then the first element of the $_FILES array will be used |
2497 | * |
c6d15803 |
2498 | * @uses $_FILES |
2499 | * @param array $filearray A 1-dimensional sub-array of the $_FILES array |
2500 | * @param boolean $returnerror ? |
7cf1c7bd |
2501 | * @return boolean |
2502 | * @todo Finish documenting this function |
2503 | */ |
ebd52396 |
2504 | function print_file_upload_error($filearray = '', $returnerror = false) { |
ebd52396 |
2505 | |
2506 | if ($filearray == '' or !isset($filearray['error'])) { |
2507 | |
2508 | if (empty($_FILES)) return false; |
2509 | |
2510 | $files = $_FILES; /// so we don't mess up the _FILES array for subsequent code |
2511 | $filearray = array_shift($files); /// use first element of array |
2512 | } |
2513 | |
2514 | switch ($filearray['error']) { |
2515 | |
2516 | case 0: // UPLOAD_ERR_OK |
2517 | if ($filearray['size'] > 0) { |
2518 | $errmessage = get_string('uploadproblem', $filearray['name']); |
2519 | } else { |
2520 | $errmessage = get_string('uploadnofilefound'); /// probably a dud file name |
2521 | } |
2522 | break; |
2523 | |
2524 | case 1: // UPLOAD_ERR_INI_SIZE |
2525 | $errmessage = get_string('uploadserverlimit'); |
2526 | break; |
2527 | |
2528 | case 2: // UPLOAD_ERR_FORM_SIZE |
2529 | $errmessage = get_string('uploadformlimit'); |
2530 | break; |
2531 | |
2532 | case 3: // UPLOAD_ERR_PARTIAL |
2533 | $errmessage = get_string('uploadpartialfile'); |
2534 | break; |
2535 | |
2536 | case 4: // UPLOAD_ERR_NO_FILE |
2537 | $errmessage = get_string('uploadnofilefound'); |
2538 | break; |
2539 | |
2540 | default: |
2541 | $errmessage = get_string('uploadproblem', $filearray['name']); |
2542 | } |
2543 | |
2544 | if ($returnerror) { |
2545 | return $errmessage; |
2546 | } else { |
2547 | notify($errmessage); |
2548 | return true; |
2549 | } |
2550 | |
2551 | } |
2552 | |
7cf1c7bd |
2553 | /** |
2554 | * Returns an array with all the filenames in |
2555 | * all subdirectories, relative to the given rootdir. |
2556 | * If excludefile is defined, then that file/directory is ignored |
2557 | * If getdirs is true, then (sub)directories are included in the output |
2558 | * If getfiles is true, then files are included in the output |
2559 | * (at least one of these must be true!) |
2560 | * |
c6d15803 |
2561 | * @param string $rootdir ? |
2562 | * @param string $excludefile If defined then the specified file/directory is ignored |
2563 | * @param boolean $descend ? |
2564 | * @param boolean $getdirs If true then (sub)directories are included in the output |
2565 | * @param boolean $getfiles If true then files are included in the output |
2566 | * @return array An array with all the filenames in |
2567 | * all subdirectories, relative to the given rootdir |
2568 | * @todo Finish documenting this function. Add examples of $excludefile usage. |
7cf1c7bd |
2569 | */ |
b0ccd3fb |
2570 | function get_directory_list($rootdir, $excludefile='', $descend=true, $getdirs=false, $getfiles=true) { |
f9903ed0 |
2571 | |
2572 | $dirs = array(); |
f9903ed0 |
2573 | |
16a5602c |
2574 | if (!$getdirs and !$getfiles) { // Nothing to show |
12407705 |
2575 | return $dirs; |
2576 | } |
2577 | |
16a5602c |
2578 | if (!is_dir($rootdir)) { // Must be a directory |
2579 | return $dirs; |
2580 | } |
2581 | |
2582 | if (!$dir = opendir($rootdir)) { // Can't open it for some reason |
d897cae4 |
2583 | return $dirs; |
2584 | } |
2585 | |
81fcd0f0 |
2586 | while (false !== ($file = readdir($dir))) { |
b35e8568 |
2587 | $firstchar = substr($file, 0, 1); |
b0ccd3fb |
2588 | if ($firstchar == '.' or $file == 'CVS' or $file == $excludefile) { |
b35e8568 |
2589 | continue; |
2590 | } |
b0ccd3fb |
2591 | $fullfile = $rootdir .'/'. $file; |
2592 | if (filetype($fullfile) == 'dir') { |
16a5602c |
2593 | if ($getdirs) { |
55fd8177 |
2594 | $dirs[] = $file; |
2595 | } |
bf5c2e84 |
2596 | if ($descend) { |
16a5602c |
2597 | $subdirs = get_directory_list($fullfile, $excludefile, $descend, $getdirs, $getfiles); |
bf5c2e84 |
2598 | foreach ($subdirs as $subdir) { |
b0ccd3fb |
2599 | $dirs[] = $file .'/'. $subdir; |
bf5c2e84 |
2600 | } |
f9903ed0 |
2601 | } |
16a5602c |
2602 | } else if ($getfiles) { |
b35e8568 |
2603 | $dirs[] = $file; |
f9903ed0 |
2604 | } |
2605 | } |
44e2d2bb |
2606 | closedir($dir); |
f9903ed0 |
2607 | |
774ab660 |
2608 | asort($dirs); |
2609 | |
f9903ed0 |
2610 | return $dirs; |
2611 | } |
2612 | |
7cf1c7bd |
2613 | /** |
2614 | * Adds up all the files in a directory and works out the size. |
2615 | * |
2616 | * @param string $rootdir ? |
2617 | * @param string $excludefile ? |
c6d15803 |
2618 | * @return array |
7cf1c7bd |
2619 | * @todo Finish documenting this function |
2620 | */ |
b0ccd3fb |
2621 | function get_directory_size($rootdir, $excludefile='') { |
16a5602c |
2622 | |
2623 | $size = 0; |
2624 | |
2625 | if (!is_dir($rootdir)) { // Must be a directory |
2626 | return $dirs; |
2627 | } |
2628 | |
b5b90f26 |
2629 | if (!$dir = @opendir($rootdir)) { // Can't open it for some reason |
16a5602c |
2630 | return $dirs; |
2631 | } |
2632 | |
2633 | while (false !== ($file = readdir($dir))) { |
2634 | $firstchar = substr($file, 0, 1); |
b0ccd3fb |
2635 | if ($firstchar == '.' or $file == 'CVS' or $file == $excludefile) { |
16a5602c |
2636 | continue; |
2637 | } |
b0ccd3fb |
2638 | $fullfile = $rootdir .'/'. $file; |
2639 | if (filetype($fullfile) == 'dir') { |
16a5602c |
2640 | $size += get_directory_size($fullfile, $excludefile); |
2641 | } else { |
2642 | $size += filesize($fullfile); |
2643 | } |
2644 | } |
2645 | closedir($dir); |
2646 | |
2647 | return $size; |
2648 | } |
2649 | |
7cf1c7bd |
2650 | /** |
2651 | * Converts numbers like 10M into bytes. |
2652 | * |
7cf1c7bd |
2653 | * @param mixed $size The size to be converted |
2654 | * @return mixed |
2655 | */ |
989bfa9d |
2656 | function get_real_size($size=0) { |
989bfa9d |
2657 | if (!$size) { |
d8ba183c |
2658 | return 0; |
989bfa9d |
2659 | } |
2660 | $scan['MB'] = 1048576; |
64efda84 |
2661 | $scan['Mb'] = 1048576; |
989bfa9d |
2662 | $scan['M'] = 1048576; |
266a416e |
2663 | $scan['m'] = 1048576; |
989bfa9d |
2664 | $scan['KB'] = 1024; |
64efda84 |
2665 | $scan['Kb'] = 1024; |
989bfa9d |
2666 | $scan['K'] = 1024; |
266a416e |
2667 | $scan['k'] = 1024; |
989bfa9d |
2668 | |
2669 | while (list($key) = each($scan)) { |
2670 | if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) { |
2671 | $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key]; |
2672 | break; |
2673 | } |
2674 | } |
2675 | return $size; |
2676 | } |
2677 | |
7cf1c7bd |
2678 | /** |
2679 | * Converts bytes into display form |
2680 | * |
c6d15803 |
2681 | * @param string $size ? |
2682 | * @return string |
89dcb99d |
2683 | * @staticvar string $gb Localized string for size in gigabytes |
2684 | * @staticvar string $mb Localized string for size in megabytes |
2685 | * @staticvar string $kb Localized string for size in kilobytes |
2686 | * @staticvar string $b Localized string for size in bytes |
c6d15803 |
2687 | * @todo Finish documenting this function. Verify return type. |
7cf1c7bd |
2688 | */ |
44e2d2bb |
2689 | function display_size($size) { |
4909e176 |
2690 | |
7cf1c7bd |
2691 | static $gb, $mb, $kb, $b; |
4909e176 |
2692 | |
2693 | if (empty($gb)) { |
2694 | $gb = get_string('sizegb'); |
2695 | $mb = get_string('sizemb'); |
2696 | $kb = get_string('sizekb'); |
2697 | $b = get_string('sizeb'); |
2698 | } |
2699 | |
44e2d2bb |
2700 | if ($size >= 1073741824) { |
4909e176 |
2701 | $size = round($size / 1073741824 * 10) / 10 . $gb; |
44e2d2bb |
2702 | } else if ($size >= 1048576) { |
4909e176 |
2703 | $size = round($size / 1048576 * 10) / 10 . $mb; |
44e2d2bb |
2704 | } else if ($size >= 1024) { |
4909e176 |
2705 | $size = round($size / 1024 * 10) / 10 . $kb; |
d8ba183c |
2706 | } else { |
b0ccd3fb |
2707 | $size = $size .' '. $b; |
44e2d2bb |
2708 | } |
2709 | return $size; |
2710 | } |
2711 | |
7cf1c7bd |
2712 | /** |
8c3dba73 |
2713 | * Cleans a given filename by removing suspicious or troublesome characters |
2714 | * Only these are allowed: |
2715 | * alphanumeric _ - . |
7cf1c7bd |
2716 | * |
c6d15803 |
2717 | * @param string $string ? |
2718 | * @return string |
7cf1c7bd |
2719 | * @todo Finish documenting this function |
2720 | */ |
6b174680 |
2721 | function clean_filename($string) { |
b0ccd3fb |
2722 | $string = eregi_replace("\.\.+", '', $string); |
8644437d |
2723 | $string = preg_replace('/[^\.a-zA-Z\d\_-]/','_', $string ); // only allowed chars |
b0ccd3fb |
2724 | $string = eregi_replace("_+", '_', $string); |
c6d15803 |
2725 | return $string; |
6b174680 |
2726 | } |
2727 | |
2728 | |
1180c6dc |
2729 | /// STRING TRANSLATION //////////////////////////////////////// |
2730 | |
8c3dba73 |
2731 | /** |
2732 | * Returns the code for the current language |
2733 | * |
2734 | * @uses $CFG |
2735 | * @param $USER |
2736 | * @param $SESSION |
2737 | * @return string |
2738 | */ |
4bfa92e7 |
2739 | function current_language() { |
3db3acfb |
2740 | global $CFG, $USER, $SESSION; |
4bfa92e7 |
2741 | |
e5415d58 |
2742 | if (!empty($CFG->courselang)) { // Course language can override all other settings for this page |
b3153e4b |
2743 | return $CFG->courselang; |
2744 | |
e5415d58 |
2745 | } else if (!empty($SESSION->lang)) { // Session language can override other settings |
3db3acfb |
2746 | return $SESSION->lang; |
2747 | |
e5415d58 |
2748 | } else if (!empty($USER->lang)) { // User language can override site language |
4bfa92e7 |
2749 | return $USER->lang; |
3db3acfb |
2750 | |
4bfa92e7 |
2751 | } else { |
2752 | return $CFG->lang; |
2753 | } |
2754 | } |
bcc83c41 |
2755 | |
8c3dba73 |
2756 | /** |
2757 | * Given a string to translate - prints it out. |
2758 | * |
c6d15803 |
2759 | * @param string $identifier ? |
2760 | * @param string $module ? |
2761 | * @param mixed $a ? |
8c3dba73 |
2762 | */ |
b0ccd3fb |
2763 | function print_string($identifier, $module='', $a=NULL) { |
9fa49e22 |
2764 | echo get_string($identifier, $module, $a); |
2765 | } |
2766 | |
8c3dba73 |
2767 | /** |
2768 | * Return the translated string specified by $identifier as |
2769 | * for $module. Uses the same format files as STphp. |
2770 | * $a is an object, string or number that can be used |
2771 | * within translation strings |
2772 | * |
2773 | * eg "hello \$a->firstname \$a->lastname" |
2774 | * or "hello \$a" |
2775 | * |
2776 | * @uses $CFG |
c6d15803 |
2777 | * @param string $identifier ? |
2778 | * @param string $module ? |
2779 | * @param mixed $a ? |
2780 | * @return string |
8c3dba73 |
2781 | * @todo Finish documenting this function |
2782 | */ |
2783 | function get_string($identifier, $module='', $a=NULL) { |
1180c6dc |
2784 | |
4bfa92e7 |
2785 | global $CFG; |
1180c6dc |
2786 | |
e11dc9b6 |
2787 | global $course; /// Not a nice hack, but quick |
ac8abb5f |
2788 | if (empty($CFG->courselang)) { |
2789 | if (!empty($course->lang)) { |
2790 | $CFG->courselang = $course->lang; |
2791 | } |
e11dc9b6 |
2792 | } |
2793 | |
4bfa92e7 |
2794 | $lang = current_language(); |
1180c6dc |
2795 | |
b0ccd3fb |
2796 | if ($module == '') { |
2797 | $module = 'moodle'; |
1180c6dc |
2798 | } |
2799 | |
b0ccd3fb |
2800 | $langpath = $CFG->dirroot .'/lang'; |
7cf1c7bd |
2801 | $langfile = $langpath .'/'. $lang .'/'. $module .'.php'; |
1180c6dc |
2802 | |
b947c69a |
2803 | // Look for the string - if found then return it |
2804 | |
2805 | if (file_exists($langfile)) { |
2806 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
2807 | eval($result); |
2808 | return $resultstring; |
1180c6dc |
2809 | } |
2810 | } |
2811 | |
cdac797c |
2812 | // If it's a module, then look within the module pack itself mod/xxxx/lang/en/module.php |
2813 | |
b0ccd3fb |
2814 | if ($module != 'moodle') { |
2815 | $modlangpath = $CFG->dirroot .'/mod/'. $module .'/lang'; |
2816 | $langfile = $modlangpath .'/'. $lang .'/'. $module .'.php'; |
cdac797c |
2817 | if (file_exists($langfile)) { |
2818 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
2819 | eval($result); |
2820 | return $resultstring; |
2821 | } |
2822 | } |
2823 | } |
1180c6dc |
2824 | |
cdac797c |
2825 | // If the preferred language was English we can abort now |
b0ccd3fb |
2826 | if ($lang == 'en') { |
2827 | return '[['. $identifier .']]'; |
b947c69a |
2828 | } |
1180c6dc |
2829 | |
b947c69a |
2830 | // Is a parent language defined? If so, try it. |
d8ba183c |
2831 | |
b0ccd3fb |
2832 | if ($result = get_string_from_file('parentlanguage', $langpath .'/'. $lang .'/moodle.php', "\$parentlang")) { |
b947c69a |
2833 | eval($result); |
2834 | if (!empty($parentlang)) { |
b0ccd3fb |
2835 | $langfile = $langpath .'/'. $parentlang .'/'. $module .'.php'; |
b947c69a |
2836 | if (file_exists($langfile)) { |
2837 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
2838 | eval($result); |
2839 | return $resultstring; |
2840 | } |
1180c6dc |
2841 | } |
2842 | } |
2843 | } |
b947c69a |
2844 | |
2845 | // Our only remaining option is to try English |
2846 | |
b0ccd3fb |
2847 | $langfile = $langpath .'/en/'. $module .'.php'; |
b947c69a |
2848 | if (!file_exists($langfile)) { |
b0ccd3fb |
2849 | return 'ERROR: No lang file ('. $langpath .'/en/'. $module .'.php)!'; |
b947c69a |
2850 | } |
2851 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
2852 | eval($result); |
2853 | return $resultstring; |
2854 | } |
2855 | |
cdac797c |
2856 | // If it's a module, then look within the module pack itself mod/xxxx/lang/en/module.php |
2857 | |
b0ccd3fb |
2858 | if ($module != 'moodle') { |
2859 | $langfile = $modlangpath .'/en/'. $module .'.php'; |
cdac797c |
2860 | if (file_exists($langfile)) { |
2861 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
2862 | eval($result); |
2863 | return $resultstring; |
2864 | } |
2865 | } |
2866 | } |
2867 | |
b0ccd3fb |
2868 | return '[['. $identifier .']]'; // Last resort |
1180c6dc |
2869 | } |
2870 | |
8c3dba73 |
2871 | /** |
c6d15803 |
2872 | * This function is only used from {@link get_string()}. |
8c3dba73 |
2873 | * |
da72d76d |
2874 | * @internal Only used from get_string, not meant to be public API |
c6d15803 |
2875 | * @param string $identifier ? |
2876 | * @param string $langfile ? |
2877 | * @param string $destination ? |
2878 | * @return string|false ? |
89dcb99d |
2879 | * @staticvar array $strings Localized strings |
da72d76d |
2880 | * @access private |
c6d15803 |
2881 | * @todo Finish documenting this function. |
8c3dba73 |
2882 | */ |
1180c6dc |
2883 | function get_string_from_file($identifier, $langfile, $destination) { |
2b32bddd |
2884 | |
2885 | static $strings; // Keep the strings cached in memory. |
2886 | |
2887 | if (empty($strings[$langfile])) { |
a32c99e2 |
2888 | $string = array(); |
2b32bddd |
2889 | include ($langfile); |
2890 | $strings[$langfile] = $string; |
2891 | } else { |
2892 | $string = &$strings[$langfile]; |
2893 | } |
1180c6dc |
2894 | |
2895 | if (!isset ($string[$identifier])) { |
2896 | return false; |
2897 | } |
2898 | |
b0ccd3fb |
2899 | return $destination .'= sprintf("'. $string[$identifier] .'");'; |
1180c6dc |
2900 | } |
f9903ed0 |
2901 | |
8c3dba73 |
2902 | /** |
2903 | * Converts an array of strings to their localized value. |
2904 | * |
2905 | * @param array $array An array of strings |
2906 | * @param string $module The language module that these strings can be found in. |
2907 | * @return string |
2908 | */ |
9d3c795c |
2909 | function get_strings($array, $module='') { |
9d3c795c |
2910 | |
2911 | $string = NULL; |
2912 | foreach ($array as $item) { |
2913 | $string->$item = get_string($item, $module); |
2914 | } |
2915 | return $string; |
2916 | } |
f9903ed0 |
2917 | |
8c3dba73 |
2918 | /** |
2919 | * Returns a list of language codes and their full names |
2920 | * |
c6d15803 |
2921 | * @uses $CFG |
2922 | * @return array An associative array with contents in the form of LanguageCode => LanguageName |
8c3dba73 |
2923 | * @todo Finish documenting this function |
2924 | */ |
1a72314d |
2925 | function get_list_of_languages() { |
1a72314d |
2926 | global $CFG; |
2927 | |
984a8bf3 |
2928 | $languages = array(); |
2929 | |
2930 | if (!empty($CFG->langlist)) { // use admin's list of languages |
2931 | $langlist = explode(',', $CFG->langlist); |
2932 | foreach ($langlist as $lang) { |
b0ccd3fb |
2933 | if (file_exists($CFG->dirroot .'/lang/'. $lang .'/moodle.php')) { |
2934 | include($CFG->dirroot .'/lang/'. $lang .'/moodle.php'); |
2935 | $languages[$lang] = $string['thislanguage'].' ('. $lang .')'; |
984a8bf3 |
2936 | unset($string); |
2937 | } |
2938 | } |
2939 | } else { |
b0ccd3fb |
2940 | if (!$langdirs = get_list_of_plugins('lang')) { |
984a8bf3 |
2941 | return false; |
2942 | } |
2943 | foreach ($langdirs as $lang) { |
b0ccd3fb |
2944 | include($CFG->dirroot .'/lang/'. $lang .'/moodle.php'); |
2945 | $languages[$lang] = $string['thislanguage'] .' ('. $lang .')'; |
984a8bf3 |
2946 | unset($string); |
2947 | } |
1a72314d |
2948 | } |
2949 | |
1a72314d |
2950 | return $languages; |
2951 | } |
2952 | |
8c3dba73 |
2953 | /** |
2954 | * Returns a list of country names in the current language |
2955 | * |
c6d15803 |
2956 | * @uses $CFG |
2957 | * @uses $USER |
2958 | * @return string? |
2959 | * @todo Finish documenting this function. |
8c3dba73 |
2960 | */ |
5833a6c8 |
2961 | function get_list_of_countries() { |
5833a6c8 |
2962 | global $CFG, $USER; |
2963 | |
2964 | $lang = current_language(); |
2965 | |
b0ccd3fb |
2966 | if (!file_exists($CFG->dirroot .'/lang/'. $lang .'/countries.php')) { |
2967 | if ($parentlang = get_string('parentlanguage')) { |
2968 | if (file_exists($CFG->dirroot .'/lang/'. $parentlang .'/countries.php')) { |
aa3eb050 |
2969 | $lang = $parentlang; |
2970 | } else { |
b0ccd3fb |
2971 | $lang = 'en'; // countries.php must exist in this pack |
aa3eb050 |
2972 | } |
2973 | } else { |
b0ccd3fb |
2974 | $lang = 'en'; // countries.php must exist in this pack |
aa3eb050 |
2975 | } |
5833a6c8 |
2976 | } |
2977 | |
b0ccd3fb |
2978 | include($CFG->dirroot .'/lang/'. $lang .'/countries.php'); |
5833a6c8 |
2979 | |
f8dbffb1 |
2980 | if (!empty($string)) { |
2981 | asort($string); |
2982 | } |
5833a6c8 |
2983 | |
2984 | return $string; |
2985 | } |
2986 | |
8c3dba73 |
2987 | /** |
2988 | * Returns a list of picture names in the current language |
2989 | * |
c6d15803 |
2990 | * @uses $CFG |
2991 | * @return string? |
2992 | * @todo Finish documenting this function. |
8c3dba73 |
2993 | */ |
2994 | function get_list_of_pixnames() { |
82196932 |
2995 | global $CFG; |
2996 | |
2997 | $lang = current_language(); |
2998 | |
b0ccd3fb |
2999 | if (!file_exists($CFG->dirroot .'/lang/'. $lang .'/pix.php')) { |
3000 | if ($parentlang = get_string('parentlanguage')) { |
3001 | if (file_exists($CFG->dirroot .'/lang/'. $parentlang .'/pix.php')) { |
82196932 |
3002 | $lang = $parentlang; |
3003 | } else { |
b0ccd3fb |
3004 | $lang = 'en'; // countries.php must exist in this pack |
82196932 |
3005 | } |
3006 | } else { |
b0ccd3fb |
3007 | $lang = 'en'; // countries.php must exist in this pack |
82196932 |
3008 | } |
3009 | } |
3010 | |
b0ccd3fb |
3011 | include_once($CFG->dirroot .'/lang/'. $lang .'/pix.php'); |
82196932 |
3012 | |
3013 | return $string; |
3014 | } |
3015 | |
8c3dba73 |
3016 | /** |
3017 | * Can include a given document file (depends on second |
3018 | * parameter) or just return info about it. |
3019 | * |
c6d15803 |
3020 | * @uses $CFG |
3021 | * @param string $file ? |
3022 | * @param boolean $include ? |
3023 | * @return ? |
8c3dba73 |
3024 | * @todo Finish documenting this function |
3025 | */ |
3026 | function document_file($file, $include=true) { |
c9d4e6da |
3027 | global $CFG; |
9bd2c874 |
3028 | |
db356340 |
3029 | $file = clean_filename($file); |
3030 | |
9bd2c874 |
3031 | if (empty($file)) { |
9bd2c874 |
3032 | return false; |
3033 | } |
3034 | |
b0ccd3fb |
3035 | $langs = array(current_language(), get_string('parentlanguage'), 'en'); |
9bd2c874 |
3036 | |
db356340 |
3037 | foreach ($langs as $lang) { |
b0ccd3fb |
3038 | $info->filepath = $CFG->dirroot .'/lang/'. $lang .'/docs/'. $file; |
3039 | $info->urlpath = $CFG->wwwroot .'/lang/'. $lang .'/docs/'. $file; |
9bd2c874 |
3040 | |
db356340 |
3041 | if (file_exists($info->filepath)) { |
3042 | if ($include) { |
3043 | include($info->filepath); |
3044 | } |
3045 | return $info; |
0c106cd3 |
3046 | } |
9bd2c874 |
3047 | } |
3048 | |
db356340 |
3049 | return false; |
9bd2c874 |
3050 | } |
3051 | |
1a72314d |
3052 | |
f9903ed0 |
3053 | /// ENCRYPTION //////////////////////////////////////////////// |
3054 | |
8c3dba73 |
3055 | /** |
3056 | * rc4encrypt |
3057 | * |
3058 | * @param string $data ? |
c6d15803 |
3059 | * @return string |
8c3dba73 |
3060 | * @todo Finish documenting this function |
3061 | */ |
f9903ed0 |
3062 | function rc4encrypt($data) { |
b0ccd3fb |
3063 | $password = 'nfgjeingjk'; |
3064 | return endecrypt($password, $data, ''); |
f9903ed0 |
3065 | } |
3066 | |
8c3dba73 |
3067 | /** |
3068 | * rc4decrypt |
3069 | * |
3070 | * @param string $data ? |
c6d15803 |
3071 | * @return string |
8c3dba73 |
3072 | * @todo Finish documenting this function |
3073 | */ |
f9903ed0 |
3074 | function rc4decrypt($data) { |
b0ccd3fb |
3075 | $password = 'nfgjeingjk'; |
3076 | return endecrypt($password, $data, 'de'); |
f9903ed0 |
3077 | } |
3078 | |
8c3dba73 |
3079 | /** |
3080 | * Based on a class by Mukul Sabharwal [mukulsabharwal @ yahoo.com] |
3081 | * |
c6d15803 |
3082 | * @param string $pwd ? |
3083 | * @param string $data ? |
3084 | * @param string $case ? |
3085 | * @return string |
8c3dba73 |
3086 | * @todo Finish documenting this function |
3087 | */ |
3088 | function endecrypt ($pwd, $data, $case) { |
f9903ed0 |
3089 | |
3090 | if ($case == 'de') { |
3091 | $data = urldecode($data); |
3092 | } |
3093 | |
b0ccd3fb |
3094 | $key[] = ''; |
3095 | $box[] = ''; |
3096 | $temp_swap = ''; |
f9903ed0 |
3097 | $pwd_length = 0; |
3098 | |
3099 | $pwd_length = strlen($pwd); |
3100 | |
3101 | for ($i = 0; $i <= 255; $i++) { |
3102 | $key[$i] = ord(substr($pwd, ($i % $pwd_length), 1)); |
3103 | $box[$i] = $i; |
3104 | } |
3105 | |
3106 | $x = 0; |
3107 | |
3108 | for ($i = 0; $i <= 255; $i++) { |
3109 | $x = ($x + $box[$i] + $key[$i]) % 256; |
3110 | $temp_swap = $box[$i]; |
3111 | $box[$i] = $box[$x]; |
3112 | $box[$x] = $temp_swap; |
3113 | } |
3114 | |
b0ccd3fb |
3115 | $temp = ''; |
3116 | $k = ''; |
f9903ed0 |
3117 | |
b0ccd3fb |
3118 | $cipherby = ''; |
3119 | $cipher = ''; |
f9903ed0 |
3120 | |
3121 | $a = 0; |
3122 | $j = 0; |
3123 | |
3124 | for ($i = 0; $i < strlen($data); $i++) { |
3125 | $a = ($a + 1) % 256; |
3126 | $j = ($j + $box[$a]) % 256; |
3127 | $temp = $box[$a]; |
3128 | $box[$a] = $box[$j]; |
3129 | $box[$j] = $temp; |
3130 | $k = $box[(($box[$a] + $box[$j]) % 256)]; |
3131 | $cipherby = ord(substr($data, $i, 1)) ^ $k; |
3132 | $cipher .= chr($cipherby); |
3133 | } |
3134 | |
3135 | if ($case == 'de') { |
3136 | $cipher = urldecode(urlencode($cipher)); |
3137 | } else { |
3138 | $cipher = urlencode($cipher); |
3139 | } |
3140 | |
3141 | return $cipher; |
3142 | } |
3143 | |
3144 | |
5fba04fb |
3145 | /// CALENDAR MANAGEMENT //////////////////////////////////////////////////////////////// |
3146 | |
3147 | |
8c3dba73 |
3148 | /** |
c6d15803 |
3149 | * Call this function to add an event to the calendar table |
8c3dba73 |
3150 | * and to call any calendar plugins |
8c3dba73 |
3151 | * |
c6d15803 |
3152 | * @uses $CFG |
3153 | * @param array $event An associative array representing an event from the calendar table. The event will be identified by the id field. The object event should include the following: |
3154 | * <ul> |
3155 | * <li><b>$event->name</b> - Name for the event |
3156 | * <li><b>$event->description</b> - Description of the event (defaults to '') |
7ecb19bb |
3157 | * <li><b>$event->format</b> - Format for the description (using formatting types defined at the top of weblib.php) |
c6d15803 |
3158 | * <li><b>$event->courseid</b> - The id of the course this event belongs to (0 = all courses) |
3159 | * <li><b>$event->groupid</b> - The id of the group this event belongs to (0 = no group) |
3160 | * <li><b>$event->userid</b> - The id of the user this event belongs to (0 = no user) |
3161 | * <li><b>$event->modulename</b> - Name of the module that creates this event |
3162 | * <li><b>$event->instance</b> - Instance of the module that owns this event |
3163 | * <li><b>$event->eventtype</b> - The type info together with the module info could |
3164 | * be used by calendar plugins to decide how to display event |
3165 | * <li><b>$event->timestart</b>- Timestamp for start of event |
3166 | * <li><b>$event->timeduration</b> - Duration (defaults to zero) |
7ecb19bb |
3167 | * <li><b>$event->>visible</b> - 0 if the event should be hidden (e.g. because the activity that created it is hidden) |
c6d15803 |
3168 | * </ul> |
3169 | * @return int The id number of the resulting record |
8c3dba73 |
3170 | * @todo Finish documenting this function |
3171 | */ |
3172 | function add_event($event) { |
5fba04fb |
3173 | |
3174 | global $CFG; |
3175 | |
3176 | $event->timemodified = time(); |
d8ba183c |
3177 | |
b0ccd3fb |
3178 | if (!$event->id = insert_record('event', $event)) { |
5fba04fb |
3179 | return false; |
3180 | } |
d8ba183c |
3181 | |
5fba04fb |
3182 | if (!empty($CFG->calendar)) { // call the add_event function of the selected calendar |
b0ccd3fb |
3183 | if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) { |
3184 | include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php'); |
5fba04fb |
3185 | $calendar_add_event = $CFG->calendar.'_add_event'; |
3186 | if (function_exists($calendar_add_event)) { |
3187 | $calendar_add_event($event); |
3188 | } |
3189 | } |
3190 | } |
d8ba183c |
3191 | |
5fba04fb |
3192 | return $event->id; |
3193 | } |
3194 | |
8c3dba73 |
3195 | /** |
c6d15803 |
3196 | * Call this function to update an event in the calendar table |
8c3dba73 |
3197 | * the event will be identified by the id field of the $event object. |
3198 | * |
c6d15803 |
3199 | * @uses $CFG |
3200 | * @param array $event An associative array representing an event from the calendar table. The event will be identified by the id field. |
3201 | * @return boolean |
8c3dba73 |
3202 | * @todo Finish documenting this function |
3203 | */ |
5fba04fb |
3204 | function update_event($event) { |
5fba04fb |
3205 | |
3206 | global $CFG; |
3207 | |
3208 | $event->timemodified = time(); |
d8ba183c |
3209 | |
5fba04fb |
3210 | if (!empty($CFG->calendar)) { // call the update_event function of the selected calendar |
b0ccd3fb |
3211 | if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) { |
3212 | include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php'); |
5fba04fb |
3213 | $calendar_update_event = $CFG->calendar.'_update_event'; |
3214 | if (function_exists($calendar_update_event)) { |
3215 | $calendar_update_event($event); |
3216 | } |
3217 | } |
3218 | } |
b0ccd3fb |
3219 | return update_record('event', $event); |
5fba04fb |
3220 | } |
3221 | |
8c3dba73 |
3222 | /** |
3223 | * Call this function to delete the event with id $id from calendar table. |
3224 | * |
c6d15803 |
3225 | * @uses $CFG |
3226 | * @param int $id The id of an event from the 'calendar' table. |
3227 | * @return array An associative array with the results from the SQL call. |
3228 | * @todo Verify return type |
8c3dba73 |
3229 | */ |
5fba04fb |
3230 | function delete_event($id) { |
5fba04fb |
3231 | |
3232 | global $CFG; |
3233 | |
3234 | if (!empty($CFG->calendar)) { // call the delete_event function of the selected calendar |
b0ccd3fb |
3235 | if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) { |
3236 | include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php'); |
5fba04fb |
3237 | $calendar_delete_event = $CFG->calendar.'_delete_event'; |
3238 | if (function_exists($calendar_delete_event)) { |
3239 | $calendar_delete_event($id); |
3240 | } |
3241 | } |
3242 | } |
b0ccd3fb |
3243 | return delete_records('event', 'id', $id); |
5fba04fb |
3244 | } |
3245 | |
8c3dba73 |
3246 | /** |
c6d15803 |
3247 | * Call this function to hide an event in the calendar table |
8c3dba73 |
3248 | * the event will be identified by the id field of the $event object. |
3249 | * |
c6d15803 |
3250 | * @uses $CFG |
3251 | * @param array $event An associative array representing an event from the calendar table. The event will be identified by the id field. |
3252 | * @return array An associative array with the results from the SQL call. |
3253 | * @todo Verify return type |
8c3dba73 |
3254 | */ |
dcd338ff |
3255 | function hide_event($event) { |
dcd338ff |
3256 | global $CFG; |
3257 | |
3258 | if (!empty($CFG->calendar)) { // call the update_event function of the selected calendar |
b0ccd3fb |
3259 | if (file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) { |
|