MDL-15094 added some overlook conversions - thanks to Eloy's script ;-)
[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
8ec50604 427/**
428 * Print an error page displaying an error message.
429 * Old method, don't call directly in new code - use print_error instead.
430 *
8ec50604 431 * @param string $message The message to display to the user about the error.
432 * @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.
251387d0 433 * @return terminates script, does not return!
8ec50604 434 */
245ac557 435function error($message, $link='') {
251387d0 436 global $UNITTEST;
8ec50604 437
251387d0 438 // If unittest running, throw exception instead
a230012c 439 if (!empty($UNITTEST->running)) {
73f7ad71 440 // Errors in unit test become exceptions, so you can unit test
441 // code that might call error().
251387d0 442 throw new moodle_exception('notlocalisederrormessage', 'error', $link, $message);
8ec50604 443 }
444
251387d0 445 _print_normal_error('notlocalisederrormessage', 'error', $message, $link, debug_backtrace(), true); // show debug warning
446}
8ec50604 447
8ec50604 448
8ec50604 449
251387d0 450//////////////////////////
451/// removed functions ////
452//////////////////////////
294ce987 453
245ac557 454function addslashes_object($dataobject) {
294ce987 455 error('addslashes() not available anymore');
456}
457
458function addslashes_recursive($var) {
459 error('addslashes_recursive() not available anymore');
460}
461
245ac557 462function execute_sql($command, $feedback=true) {
463 error('execute_sql() not available anymore');
464}
465
466function record_exists_select($table, $select='') {
467 error('record_exists_select() not available anymore');
468}
469
470function record_exists_sql($sql) {
471 error('record_exists_sql() not available anymore');
472}
473
474function count_records_select($table, $select='', $countitem='COUNT(*)') {
475 error('count_records_select() not available anymore');
476}
477
478function count_records_sql($sql) {
479 error('count_records_sql() not available anymore');
480}
481
482function get_record_sql($sql, $expectmultiple=false, $nolimit=false) {
483 error('get_record_sql() not available anymore');
484}
485
486function get_record_select($table, $select='', $fields='*') {
487 error('get_record_select() not available anymore');
488}
489
490function get_recordset($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='') {
491 error('get_recordset() not available anymore');
492}
493
494function get_recordset_sql($sql, $limitfrom=null, $limitnum=null) {
495 error('get_recordset_sql() not available anymore');
496}
497
498function rs_fetch_record(&$rs) {
499 error('rs_fetch_record() not available anymore');
500}
501
502function rs_next_record(&$rs) {
503 error('rs_next_record() not available anymore');
504}
505
506function rs_fetch_next_record(&$rs) {
507 error('rs_fetch_next_record() not available anymore');
508}
509
510function rs_EOF($rs) {
511 error('rs_EOF() not available anymore');
512}
513
514function rs_close(&$rs) {
515 error('rs_close() not available anymore');
516}
517
518function get_records_select($table, $select='', $sort='', $fields='*', $limitfrom='', $limitnum='') {
519 error('get_records_select() not available anymore');
520}
521
522function get_field_select($table, $return, $select) {
523 error('get_field_select() not available anymore');
524}
525
526function get_field_sql($sql) {
527 error('get_field_sql() not available anymore');
528}
294ce987 529
245ac557 530function delete_records_select($table, $select='') {
531 delete_records_select('get_field_sql() not available anymore');
532}
533
534
535function configure_dbconnection() {
536 error('configure_dbconnection() removed');
537}
538
539function sql_max($field) {
540 error('sql_max() removed - use normal sql MAX() instead');
541}
542
543function sql_as() {
544 error('sql_as() removed - do not use AS for tables at all');
545}
546
547function sql_paging_limit($page, $recordsperpage) {
548 error('Function sql_paging_limit() is deprecated. Replace it with the correct use of limitfrom, limitnum parameters');
549}
550
551function db_uppercase() {
552 error('upper() removed - use normal sql UPPER()');
553}
554
555function db_lowercase() {
556 error('upper() removed - use normal sql LOWER()');
557}
558
559function modify_database($sqlfile='', $sqlstring='') {
560 error('modify_database() removed - use new XMLDB functions');
561}
562
563function where_clause($field1='', $value1='', $field2='', $value2='', $field3='', $value3='') {
564 error('where_clause() removed - use new functions with $conditions parameter');
565}
566
567function execute_sql_arr($sqlarr, $continue=true, $feedback=true) {
568 error('execute_sql_arr() removed');
569}
570
571function get_records_list($table, $field='', $values='', $sort='', $fields='*', $limitfrom='', $limitnum='') {
572 error('get_records_list() removed');
573}
574
575function get_recordset_list($table, $field='', $values='', $sort='', $fields='*', $limitfrom='', $limitnum='') {
576 error('get_recordset_list() removed');
577}
578
579function get_records_menu($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='') {
580 error('get_records_menu() removed');
581}
582
583function get_records_select_menu($table, $select='', $sort='', $fields='*', $limitfrom='', $limitnum='') {
584 error('get_records_select_menu() removed');
585}
586
587function get_records_sql_menu($sql, $limitfrom='', $limitnum='') {
588 error('get_records_sql_menu() removed');
589}
590
591function column_type($table, $column) {
592 error('column_type() removed');
593}
594
595function recordset_to_menu($rs) {
596 error('recordset_to_menu() removed');
597}
598
599function records_to_menu($records, $field1, $field2) {
600 error('records_to_menu() removed');
601}
602
603function set_field_select($table, $newfield, $newvalue, $select, $localcall = false) {
604 error('set_field_select() removed');
605}
606
607function get_fieldset_select($table, $return, $select) {
608 error('get_fieldset_select() removed');
609}
610
611function get_fieldset_sql($sql) {
612 error('get_fieldset_sql() removed');
613}
614
615function sql_ilike() {
616 error('sql_ilike() not available anymore');
617}
618
619function sql_fullname($first='firstname', $last='lastname') {
620 error('sql_fullname() not available anymore');
621}
622
623function sql_concat() {
624 error('sql_concat() not available anymore');
625}
626
627function sql_empty() {
628 error('sql_empty() not available anymore');
629}
630
631function sql_substr() {
632 error('sql_substr() not available anymore');
633}
634
635function sql_bitand($int1, $int2) {
636 error('sql_bitand() not available anymore');
637}
638
639function sql_bitnot($int1) {
640 error('sql_bitnot() not available anymore');
641}
642
643function sql_bitor($int1, $int2) {
644 error('sql_bitor() not available anymore');
645}
646
647function sql_bitxor($int1, $int2) {
648 error('sql_bitxor() not available anymore');
649}
650
651function sql_cast_char2int($fieldname, $text=false) {
652 error('sql_cast_char2int() not available anymore');
653}
654
655function sql_compare_text($fieldname, $numchars=32) {
656 error('sql_compare_text() not available anymore');
657}
658
659function sql_order_by_text($fieldname, $numchars=32) {
660 error('sql_order_by_text() not available anymore');
661}
662
663function sql_concat_join($separator="' '", $elements=array()) {
664 error('sql_concat_join() not available anymore');
665}
666
667function sql_isempty($tablename, $fieldname, $nullablefield, $textfield) {
668 error('sql_isempty() not available anymore');
669}
670
671function sql_isnotempty($tablename, $fieldname, $nullablefield, $textfield) {
672 error('sql_isnotempty() not available anymore');
673}
674
675function begin_sql() {
676 error('begin_sql() not available anymore');
677}
678
679function commit_sql() {
680 error('commit_sql() not available anymore');
681}
682
683function rollback_sql() {
684 error('rollback_sql() not available anymore');
685}
686
687function insert_record($table, $dataobject, $returnid=true, $primarykey='id') {
688 error('insert_record() not available anymore');
689}
690
691function update_record($table, $dataobject) {
692 error('update_record() not available anymore');
693}
694
695function get_records($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='') {
696 error('get_records() not available anymore');
697}
698
699function get_record($table, $field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields='*') {
700 error('get_record() not available anymore');
701}
702
703function set_field($table, $newfield, $newvalue, $field1, $value1, $field2='', $value2='', $field3='', $value3='') {
704 error('set_field() not available anymore');
705}
706
707function count_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') {
708 error('count_records() not available anymore');
709}
710
711function record_exists($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') {
712 error('record_exists() not available anymore');
713}
714
715function delete_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') {
716 error('delete_records() not available anymore');
717}
718
719function get_field($table, $return, $field1, $value1, $field2='', $value2='', $field3='', $value3='') {
720 error('get_field() not available anymore');
721}
294ce987 722
e6b4f00e 723function table_column($table, $oldfield, $field, $type='integer', $size='10',
724 $signed='unsigned', $default='0', $null='not null', $after='') {
725 error('table_column() was removed, please use new ddl functions');
726}
245ac557 727