MDL-14679 towards adodb separation
[moodle.git] / lib / deprecatedlib.php
CommitLineData
c4d0753b 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
73f7ad71 10// Copyright (C) 1999 onwards Martin Dougiamas, Moodle http://moodle.com//
c4d0753b 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///////////////////////////////////////////////////////////////////////////
25
26/**
27 * deprecatedlib.php - Old functions retained only for backward compatibility
28 *
29 * Old functions retained only for backward compatibility. New code should not
30 * use any of these functions.
31 *
32 * @author Martin Dougiamas
33 * @version $Id$
34 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
35 * @package moodlecore
36 */
37
c4d0753b 38/**
39 * Determines if a user is a teacher (or better)
40 *
c4d0753b 41 * @uses $CFG
42 * @param int $courseid The id of the course that is being viewed, if any
43 * @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.
44 * @param bool $obsolete_includeadmin Not used any more
45 * @return bool
46 */
47
48function isteacher($courseid=0, $userid=0, $obsolete_includeadmin=true) {
49/// Is the user able to access this course as a teacher?
c2da0757 50 global $CFG;
c4d0753b 51
52 if (empty($CFG->rolesactive)) { // Teachers are locked out during an upgrade to 1.7
53 return false;
54 }
55
56 if ($courseid) {
57 $context = get_context_instance(CONTEXT_COURSE, $courseid);
58 } else {
12d06877 59 $context = get_context_instance(CONTEXT_SYSTEM);
c4d0753b 60 }
61
c2da0757 62 return (has_capability('moodle/legacy:teacher', $context, $userid, false)
63 or has_capability('moodle/legacy:editingteacher', $context, $userid, false)
64 or has_capability('moodle/legacy:admin', $context, $userid, false));
c4d0753b 65}
66
67/**
68 * Determines if a user is a teacher in any course, or an admin
69 *
70 * @uses $USER
71 * @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.
c2da0757 72 * @param bool $includeadmin Include anyone wo is an admin as well
c4d0753b 73 * @return bool
74 */
c2da0757 75function isteacherinanycourse($userid=0, $includeadmin=true) {
79eaec48 76 global $USER, $CFG, $DB;
c4d0753b 77
78 if (empty($CFG->rolesactive)) { // Teachers are locked out during an upgrade to 1.7
79 return false;
80 }
81
82 if (!$userid) {
83 if (empty($USER->id)) {
84 return false;
85 }
86 $userid = $USER->id;
87 }
88
79eaec48 89 if (!$DB->record_exists('role_assignments', array('userid'=>$userid))) { // Has no roles anywhere
c4d0753b 90 return false;
91 }
92
93/// If this user is assigned as an editing teacher anywhere then return true
94 if ($roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW)) {
95 foreach ($roles as $role) {
79eaec48 96 if ($DB->record_exists('role_assignments', array('roleid'=>$role->id, 'userid'=>$userid))) {
c4d0753b 97 return true;
98 }
99 }
100 }
101
102/// If this user is assigned as a non-editing teacher anywhere then return true
103 if ($roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) {
104 foreach ($roles as $role) {
79eaec48 105 if ($DB->record_exists('role_assignments', array('roleid'=>$role->id, 'userid'=>$userid))) {
c4d0753b 106 return true;
107 }
108 }
109 }
110
c2da0757 111/// Include admins if required
112 if ($includeadmin) {
12d06877 113 $context = get_context_instance(CONTEXT_SYSTEM);
c2da0757 114 if (has_capability('moodle/legacy:admin', $context, $userid, false)) {
115 return true;
116 }
117 }
c4d0753b 118
119 return false;
120}
121
c4d0753b 122
123/**
124 * Determines if the specified user is logged in as guest.
125 *
c4d0753b 126 * @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
127 * @return bool
128 */
129function isguest($userid=0) {
c2da0757 130 global $CFG;
c4d0753b 131
132 if (empty($CFG->rolesactive)) {
133 return false;
134 }
135
364fffda 136 $context = get_context_instance(CONTEXT_SYSTEM);
c4d0753b 137
c2da0757 138 return has_capability('moodle/legacy:guest', $context, $userid, false);
c4d0753b 139}
140
613bbd7c 141
142/**
143 * Get the guest user information from the database
144 *
145 * @return object(user) An associative array with the details of the guest user account.
146 * @todo Is object(user) a correct return type? Or is array the proper return type with a note that the contents include all details for a user.
147 */
148function get_guest() {
149 return get_complete_user_data('username', 'guest');
150}
151
613bbd7c 152/**
153 * Returns $user object of the main teacher for a course
154 *
155 * @uses $CFG
156 * @param int $courseid The course in question.
157 * @return user|false A {@link $USER} record of the main teacher for the specified course or false if error.
158 * @todo Finish documenting this function
159 */
160function get_teacher($courseid) {
161
162 global $CFG;
163
888fb649 164 $context = get_context_instance(CONTEXT_COURSE, $courseid);
165
1113f800 166 // Pass $view=true to filter hidden caps if the user cannot see them
167 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
168 '', '', '', '', false, true)) {
b1469317 169 $users = sort_by_roleassignment_authority($users, $context);
1113f800 170 return array_shift($users);
613bbd7c 171 }
888fb649 172
173 return false;
613bbd7c 174}
175
176/**
177 * Searches logs to find all enrolments since a certain date
178 *
179 * used to print recent activity
180 *
181 * @uses $CFG
182 * @param int $courseid The course in question.
183 * @return object|false {@link $USER} records or false if error.
184 * @todo Finish documenting this function
185 */
186function get_recent_enrolments($courseid, $timestart) {
10df888a 187 global $DB;
364fffda 188
71dea306 189 $context = get_context_instance(CONTEXT_COURSE, $courseid);
613bbd7c 190
10df888a 191 $sql = "SELECT DISTINCT u.id, u.firstname, u.lastname, l.time
192 FROM {user} u, {role_assignments} ra, {log} l
193 WHERE l.time > ?
194 AND l.course = ?
195 AND l.module = 'course'
196 AND l.action = 'enrol'
9f43d70d 197 AND ".$DB->sql_cast_char2int('l.info')." = u.id
10df888a 198 AND u.id = ra.userid
199 AND ra.contextid ".get_related_contexts_string($context)."
200 ORDER BY l.time ASC";
201 $params = array($timestart, $courseid);
202 return $DB->get_records_sql($sql, $params);
613bbd7c 203}
204
2123b644 205########### FROM weblib.php ##########################################################################
206
207
208/**
209 * Print a message in a standard themed box.
364fffda 210 * This old function used to implement boxes using tables. Now it uses a DIV, but the old
2123b644 211 * parameters remain. If possible, $align, $width and $color should not be defined at all.
212 * Preferably just use print_box() in weblib.php
213 *
214 * @param string $align, alignment of the box, not the text (default center, left, right).
215 * @param string $width, width of the box, including units %, for example '100%'.
216 * @param string $color, background colour of the box, for example '#eee'.
217 * @param int $padding, padding in pixels, specified without units.
218 * @param string $class, space-separated class names.
219 * @param string $id, space-separated id names.
220 * @param boolean $return, return as string or just print it
221 */
222function print_simple_box($message, $align='', $width='', $color='', $padding=5, $class='generalbox', $id='', $return=false) {
223 $output = '';
224 $output .= print_simple_box_start($align, $width, $color, $padding, $class, $id, true);
294ce987 225 $output .= $message;
2123b644 226 $output .= print_simple_box_end(true);
227
228 if ($return) {
229 return $output;
230 } else {
231 echo $output;
232 }
233}
234
235
236
237/**
364fffda 238 * This old function used to implement boxes using tables. Now it uses a DIV, but the old
2123b644 239 * parameters remain. If possible, $align, $width and $color should not be defined at all.
240 * Even better, please use print_box_start() in weblib.php
241 *
242 * @param string $align, alignment of the box, not the text (default center, left, right). DEPRECATED
243 * @param string $width, width of the box, including % units, for example '100%'. DEPRECATED
244 * @param string $color, background colour of the box, for example '#eee'. DEPRECATED
245 * @param int $padding, padding in pixels, specified without units. OBSOLETE
246 * @param string $class, space-separated class names.
247 * @param string $id, space-separated id names.
248 * @param boolean $return, return as string or just print it
249 */
250function print_simple_box_start($align='', $width='', $color='', $padding=5, $class='generalbox', $id='', $return=false) {
251
252 $output = '';
253
8f36e33e 254 $divclasses = 'box '.$class.' '.$class.'content';
2123b644 255 $divstyles = '';
256
257 if ($align) {
258 $divclasses .= ' boxalign'.$align; // Implement alignment using a class
259 }
260 if ($width) { // Hopefully we can eliminate these in calls to this function (inline styles are bad)
8f36e33e 261 if (substr($width, -1, 1) == '%') { // Width is a % value
262 $width = (int) substr($width, 0, -1); // Extract just the number
263 if ($width < 40) {
264 $divclasses .= ' boxwidthnarrow'; // Approx 30% depending on theme
265 } else if ($width > 60) {
266 $divclasses .= ' boxwidthwide'; // Approx 80% depending on theme
267 } else {
268 $divclasses .= ' boxwidthnormal'; // Approx 50% depending on theme
269 }
270 } else {
271 $divstyles .= ' width:'.$width.';'; // Last resort
272 }
2123b644 273 }
274 if ($color) { // Hopefully we can eliminate these in calls to this function (inline styles are bad)
275 $divstyles .= ' background:'.$color.';';
276 }
277 if ($divstyles) {
278 $divstyles = ' style="'.$divstyles.'"';
279 }
280
281 if ($id) {
282 $id = ' id="'.$id.'"';
283 }
284
285 $output .= '<div'.$id.$divstyles.' class="'.$divclasses.'">';
286
287 if ($return) {
288 return $output;
289 } else {
290 echo $output;
291 }
292}
293
294
295/**
296 * Print the end portion of a standard themed box.
297 * Preferably just use print_box_end() in weblib.php
298 */
299function print_simple_box_end($return=false) {
300 $output = '</div>';
301 if ($return) {
302 return $output;
303 } else {
304 echo $output;
305 }
306}
307
ed5dd29f 308/**
309 * deprecated - use clean_param($string, PARAM_FILE); instead
310 * Check for bad characters ?
311 *
312 * @param string $string ?
313 * @param int $allowdots ?
314 * @todo Finish documenting this function - more detail needed in description as well as details on arguments
315 */
316function detect_munged_arguments($string, $allowdots=1) {
317 if (substr_count($string, '..') > $allowdots) { // Sometimes we allow dots in references
318 return true;
319 }
320 if (ereg('[\|\`]', $string)) { // check for other bad characters
321 return true;
322 }
323 if (empty($string) or $string == '/') {
324 return true;
325 }
326
327 return false;
328}
329
9152fc99 330
ed5dd29f 331/////////////////////////////////////////////////////////////
332/// Old functions not used anymore - candidates for removal
333/////////////////////////////////////////////////////////////
334
ed5dd29f 335
1d684195 336/** various deprecated groups function **/
337
338
5bf243d1 339/**
340 * Get the IDs for the user's groups in the given course.
341 *
342 * @uses $USER
343 * @param int $courseid The course being examined - the 'course' table id field.
344 * @return array An _array_ of groupids.
345 * (Was return $groupids[0] - consequences!)
346 */
347function mygroupid($courseid) {
348 global $USER;
349 if ($groups = groups_get_all_groups($courseid, $USER->id)) {
350 return array_keys($groups);
351 } else {
352 return false;
353 }
354}
355
5bf243d1 356
5bf243d1 357/**
358 * Returns the current group mode for a given course or activity module
364fffda 359 *
5bf243d1 360 * Could be false, SEPARATEGROUPS or VISIBLEGROUPS (<-- Martin)
361 */
362function groupmode($course, $cm=null) {
363
364 if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
365 return $cm->groupmode;
366 }
367 return $course->groupmode;
368}
369
c584346c 370/**
371 * Sets the current group in the session variable
372 * When $SESSION->currentgroup[$courseid] is set to 0 it means, show all groups.
373 * Sets currentgroup[$courseid] in the session variable appropriately.
374 * Does not do any permission checking.
375 * @uses $SESSION
376 * @param int $courseid The course being examined - relates to id field in
377 * 'course' table.
378 * @param int $groupid The group being examined.
379 * @return int Current group id which was set by this function
380 */
381function set_current_group($courseid, $groupid) {
382 global $SESSION;
383 return $SESSION->currentgroup[$courseid] = $groupid;
384}
385
5bf243d1 386
5bf243d1 387/**
364fffda 388 * Gets the current group - either from the session variable or from the database.
5bf243d1 389 *
390 * @uses $USER
391 * @uses $SESSION
364fffda 392 * @param int $courseid The course being examined - relates to id field in
5bf243d1 393 * 'course' table.
364fffda 394 * @param bool $full If true, the return value is a full record object.
5bf243d1 395 * If false, just the id of the record.
396 */
397function get_current_group($courseid, $full = false) {
398 global $SESSION;
399
400 if (isset($SESSION->currentgroup[$courseid])) {
401 if ($full) {
402 return groups_get_group($SESSION->currentgroup[$courseid]);
403 } else {
404 return $SESSION->currentgroup[$courseid];
405 }
406 }
407
408 $mygroupid = mygroupid($courseid);
409 if (is_array($mygroupid)) {
410 $mygroupid = array_shift($mygroupid);
411 set_current_group($courseid, $mygroupid);
412 if ($full) {
413 return groups_get_group($mygroupid);
414 } else {
415 return $mygroupid;
416 }
417 }
418
419 if ($full) {
420 return false;
421 } else {
422 return 0;
423 }
424}
425
426
5bf243d1 427
5bf243d1 428
8ec50604 429/**
430 * Print an error page displaying an error message.
431 * Old method, don't call directly in new code - use print_error instead.
432 *
433 *
434 * @uses $SESSION
435 * @uses $CFG
436 * @param string $message The message to display to the user about the error.
437 * @param string $link The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page.
438 */
439function error ($message, $link='') {
440
441 global $CFG, $SESSION, $THEME;
e3105d45 442 debugging('error() is a deprecated function, please call print_error() instead of error()', DEBUG_DEVELOPER);
8ec50604 443 $message = clean_text($message); // In case nasties are in here
444
73f7ad71 445 /**
446 * TODO VERY DIRTY HACK USED FOR UNIT TESTING UNTIL PROPER EXCEPTION HANDLING IS IMPLEMENTED
447 */
448 if (defined('UNITTEST')) {
449 // Errors in unit test become exceptions, so you can unit test
450 // code that might call error().
451 throw new Exception('error() call: '. $message.($link!=='' ? ' ['.$link.']' : ''));
452 }
453
8ec50604 454 if (defined('FULLME') && FULLME == 'cron') {
455 // Errors in cron should be mtrace'd.
456 mtrace($message);
457 die;
458 }
459
460 if (! defined('HEADER_PRINTED')) {
461 //header not yet printed
462 @header('HTTP/1.0 404 Not Found');
463 print_header(get_string('error'));
464 } else {
465 print_container_end_all(false, $THEME->open_header_containers);
466 }
467
468 echo '<br />';
469 print_simple_box($message, '', '', '', '', 'errorbox');
470
471 debugging('Stack trace:', DEBUG_DEVELOPER);
472
473 // in case we are logging upgrade in admin/index.php stop it
474 if (function_exists('upgrade_log_finish')) {
475 upgrade_log_finish();
476 }
477
478 if (empty($link) and !defined('ADMIN_EXT_HEADER_PRINTED')) {
479 if ( !empty($SESSION->fromurl) ) {
480 $link = $SESSION->fromurl;
481 unset($SESSION->fromurl);
482 } else {
483 $link = $CFG->wwwroot .'/';
484 }
485 }
486
487 if (!empty($link)) {
488 print_continue($link);
489 }
490
491 print_footer();
492
493 for ($i=0;$i<512;$i++) { // Padding to help IE work with 404
494 echo ' ';
495 }
496
497 die;
498}
294ce987 499
500/// removed functions
501function addslashes_object( $dataobject ) {
502 error('addslashes() not available anymore');
503}
504
505function addslashes_recursive($var) {
506 error('addslashes_recursive() not available anymore');
507}
508
509
510
c4d0753b 511?>