weekly release 2.9dev
[moodle.git] / lib / deprecatedlib.php
CommitLineData
c861fe2f 1<?php
2
a5cb8d69 3// This file is part of Moodle - http://moodle.org/
4//
c861fe2f 5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
a5cb8d69 14//
c861fe2f 15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
c4d0753b 17
18/**
19 * deprecatedlib.php - Old functions retained only for backward compatibility
20 *
21 * Old functions retained only for backward compatibility. New code should not
22 * use any of these functions.
23 *
78bfb562 24 * @package core
c861fe2f 25 * @subpackage deprecated
78bfb562
PS
26 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
c861fe2f 28 * @deprecated
c4d0753b 29 */
30
78bfb562
PS
31defined('MOODLE_INTERNAL') || die();
32
7eaca5a8 33/**
b33da4bf 34 * Add an entry to the legacy log table.
7eaca5a8
35 *
36 * @deprecated since 2.7 use new events instead
37 *
38 * @param int $courseid The course id
39 * @param string $module The module name e.g. forum, journal, resource, course, user etc
40 * @param string $action 'view', 'update', 'add' or 'delete', possibly followed by another word to clarify.
41 * @param string $url The file and parameters used to see the results of the action
42 * @param string $info Additional description information
43 * @param int $cm The course_module->id if there is one
44 * @param int|stdClass $user If log regards $user other than $USER
45 * @return void
46 */
47function add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0) {
b33da4bf 48 debugging('add_to_log() has been deprecated, please rewrite your code to the new events API', DEBUG_DEVELOPER);
7eaca5a8
49
50 // This is a nasty hack that allows us to put all the legacy stuff into legacy storage,
51 // this way we may move all the legacy settings there too.
52 $manager = get_log_manager();
53 if (method_exists($manager, 'legacy_add_to_log')) {
54 $manager->legacy_add_to_log($courseid, $module, $action, $url, $info, $cm, $user);
55 }
56}
57
114e3209
58/**
59 * Adds a file upload to the log table so that clam can resolve the filename to the user later if necessary
60 *
61 * @deprecated since 2.7 - use new file picker instead
62 *
63 * @param string $newfilepath
64 * @param stdClass $course
65 * @param bool $nourl
66 */
67function clam_log_upload($newfilepath, $course=null, $nourl=false) {
68 debugging('clam_log_upload() is not supposed to be used any more, use new file picker instead', DEBUG_DEVELOPER);
69}
70
71/**
72 * This function logs to error_log and to the log table that an infected file has been found and what's happened to it.
73 *
74 * @deprecated since 2.7 - use new file picker instead
75 *
76 * @param string $oldfilepath
77 * @param string $newfilepath
78 * @param int $userid The user
79 */
80function clam_log_infected($oldfilepath='', $newfilepath='', $userid=0) {
81 debugging('clam_log_infected() is not supposed to be used any more, use new file picker instead', DEBUG_DEVELOPER);
82}
83
84/**
85 * Some of the modules allow moving attachments (glossary), in which case we need to hunt down an original log and change the path.
86 *
87 * @deprecated since 2.7 - use new file picker instead
88 *
89 * @param string $oldpath
90 * @param string $newpath
91 * @param boolean $update
92 */
93function clam_change_log($oldpath, $newpath, $update=true) {
94 debugging('clam_change_log() is not supposed to be used any more, use new file picker instead', DEBUG_DEVELOPER);
95}
96
97/**
98 * Replaces the given file with a string.
99 *
100 * @deprecated since 2.7 - infected files are now deleted in file picker
101 *
102 * @param string $file
103 * @return boolean
104 */
105function clam_replace_infected_file($file) {
106 debugging('clam_change_log() is not supposed to be used any more', DEBUG_DEVELOPER);
107 return false;
108}
109
6780a1d3
110/**
111 * Checks whether the password compatibility library will work with the current
112 * version of PHP. This cannot be done using PHP version numbers since the fix
113 * has been backported to earlier versions in some distributions.
114 *
115 * See https://github.com/ircmaxell/password_compat/issues/10 for more details.
116 *
117 * @deprecated since 2.7 PHP 5.4.x should be always compatible.
118 *
119 * @return bool always returns false
120 */
121function password_compat_not_supported() {
122 debugging('Do not use password_compat_not_supported() - bcrypt is now always available', DEBUG_DEVELOPER);
123 return false;
124}
125
d79d5ac2
PS
126/**
127 * Factory method that was returning moodle_session object.
128 *
129 * @deprecated since 2.6
130 * @return \core\session\manager
131 */
132function session_get_instance() {
133 // Note: the new session manager includes all methods from the original session class.
134 static $deprecatedinstance = null;
135
136 debugging('session_get_instance() is deprecated, use \core\session\manager instead', DEBUG_DEVELOPER);
137
138 if (!$deprecatedinstance) {
139 $deprecatedinstance = new \core\session\manager();
140 }
141
142 return $deprecatedinstance;
143}
144
145/**
146 * Returns true if legacy session used.
147 *
148 * @deprecated since 2.6
149 * @return bool
150 */
151function session_is_legacy() {
152 debugging('session_is_legacy() is deprecated, do not use any more', DEBUG_DEVELOPER);
153 return false;
154}
155
156/**
157 * Terminates all sessions, auth hooks are not executed.
158 * Useful in upgrade scripts.
159 *
160 * @deprecated since 2.6
161 */
162function session_kill_all() {
163 debugging('session_kill_all() is deprecated, use \core\session\manager::kill_all_sessions() instead', DEBUG_DEVELOPER);
164 \core\session\manager::kill_all_sessions();
165}
166
167/**
168 * Mark session as accessed, prevents timeouts.
169 *
170 * @deprecated since 2.6
171 * @param string $sid
172 */
173function session_touch($sid) {
174 debugging('session_touch() is deprecated, use \core\session\manager::touch_session() instead', DEBUG_DEVELOPER);
175 \core\session\manager::touch_session($sid);
176}
177
178/**
179 * Terminates one sessions, auth hooks are not executed.
180 *
181 * @deprecated since 2.6
182 * @param string $sid session id
183 */
184function session_kill($sid) {
185 debugging('session_kill() is deprecated, use \core\session\manager::kill_session() instead', DEBUG_DEVELOPER);
186 \core\session\manager::kill_session($sid);
187}
188
189/**
190 * Terminates all sessions of one user, auth hooks are not executed.
191 * NOTE: This can not work for file based sessions!
192 *
193 * @deprecated since 2.6
194 * @param int $userid user id
195 */
196function session_kill_user($userid) {
197 debugging('session_kill_user() is deprecated, use \core\session\manager::kill_user_sessions() instead', DEBUG_DEVELOPER);
198 \core\session\manager::kill_user_sessions($userid);
199}
200
d79d5ac2
PS
201/**
202 * Setup $USER object - called during login, loginas, etc.
203 *
204 * Call sync_user_enrolments() manually after log-in, or log-in-as.
205 *
206 * @deprecated since 2.6
207 * @param stdClass $user full user record object
208 * @return void
209 */
210function session_set_user($user) {
211 debugging('session_set_user() is deprecated, use \core\session\manager::set_user() instead', DEBUG_DEVELOPER);
212 \core\session\manager::set_user($user);
213}
214
215/**
216 * Is current $USER logged-in-as somebody else?
217 * @deprecated since 2.6
218 * @return bool
219 */
220function session_is_loggedinas() {
221 debugging('session_is_loggedinas() is deprecated, use \core\session\manager::is_loggedinas() instead', DEBUG_DEVELOPER);
222 return \core\session\manager::is_loggedinas();
223}
224
225/**
226 * Returns the $USER object ignoring current login-as session
227 * @deprecated since 2.6
228 * @return stdClass user object
229 */
230function session_get_realuser() {
231 debugging('session_get_realuser() is deprecated, use \core\session\manager::get_realuser() instead', DEBUG_DEVELOPER);
232 return \core\session\manager::get_realuser();
233}
234
235/**
236 * Login as another user - no security checks here.
237 * @deprecated since 2.6
238 * @param int $userid
239 * @param stdClass $context
240 * @return void
241 */
242function session_loginas($userid, $context) {
243 debugging('session_loginas() is deprecated, use \core\session\manager::loginas() instead', DEBUG_DEVELOPER);
244 \core\session\manager::loginas($userid, $context);
245}
246
6b32d6bc
PS
247/**
248 * Minify JavaScript files.
249 *
250 * @deprecated since 2.6
251 *
252 * @param array $files
253 * @return string
254 */
255function js_minify($files) {
256 debugging('js_minify() is deprecated, use core_minify::js_files() or core_minify::js() instead.');
257 return core_minify::js_files($files);
258}
259
260/**
261 * Minify CSS files.
262 *
263 * @deprecated since 2.6
264 *
265 * @param array $files
266 * @return string
267 */
268function css_minify_css($files) {
269 debugging('css_minify_css() is deprecated, use core_minify::css_files() or core_minify::css() instead.');
270 return core_minify::css_files($files);
271}
272
f0f0e1fe
PS
273/**
274 * Function to call all event handlers when triggering an event
275 *
276 * @deprecated since 2.6
277 *
278 * @param string $eventname name of the event
279 * @param mixed $eventdata event data object
280 * @return int number of failed events
281 */
282function events_trigger($eventname, $eventdata) {
9634712a 283 debugging('events_trigger() is deprecated, please use new events instead', DEBUG_DEVELOPER);
f0f0e1fe
PS
284 return events_trigger_legacy($eventname, $eventdata);
285}
286
9e19a0f0
PS
287/**
288 * List all core subsystems and their location
289 *
290 * This is a whitelist of components that are part of the core and their
291 * language strings are defined in /lang/en/<<subsystem>>.php. If a given
292 * plugin is not listed here and it does not have proper plugintype prefix,
293 * then it is considered as course activity module.
294 *
295 * The location is optionally dirroot relative path. NULL means there is no special
296 * directory for this subsystem. If the location is set, the subsystem's
297 * renderer.php is expected to be there.
298 *
299 * @deprecated since 2.6, use core_component::get_core_subsystems()
300 *
301 * @param bool $fullpaths false means relative paths from dirroot, use true for performance reasons
302 * @return array of (string)name => (string|null)location
303 */
304function get_core_subsystems($fullpaths = false) {
305 global $CFG;
306
307 // NOTE: do not add any other debugging here, keep forever.
308
309 $subsystems = core_component::get_core_subsystems();
310
311 if ($fullpaths) {
312 return $subsystems;
313 }
314
315 debugging('Short paths are deprecated when using get_core_subsystems(), please fix the code to use fullpaths instead.', DEBUG_DEVELOPER);
316
317 $dlength = strlen($CFG->dirroot);
318
319 foreach ($subsystems as $k => $v) {
320 if ($v === null) {
321 continue;
322 }
323 $subsystems[$k] = substr($v, $dlength+1);
324 }
325
326 return $subsystems;
327}
328
329/**
330 * Lists all plugin types.
331 *
332 * @deprecated since 2.6, use core_component::get_plugin_types()
333 *
334 * @param bool $fullpaths false means relative paths from dirroot
335 * @return array Array of strings - name=>location
336 */
337function get_plugin_types($fullpaths = true) {
338 global $CFG;
339
340 // NOTE: do not add any other debugging here, keep forever.
341
342 $types = core_component::get_plugin_types();
343
344 if ($fullpaths) {
345 return $types;
346 }
347
348 debugging('Short paths are deprecated when using get_plugin_types(), please fix the code to use fullpaths instead.', DEBUG_DEVELOPER);
349
350 $dlength = strlen($CFG->dirroot);
351
352 foreach ($types as $k => $v) {
353 if ($k === 'theme') {
354 $types[$k] = 'theme';
355 continue;
356 }
357 $types[$k] = substr($v, $dlength+1);
358 }
359
360 return $types;
361}
362
363/**
364 * Use when listing real plugins of one type.
365 *
366 * @deprecated since 2.6, use core_component::get_plugin_list()
367 *
368 * @param string $plugintype type of plugin
369 * @return array name=>fulllocation pairs of plugins of given type
370 */
371function get_plugin_list($plugintype) {
372
373 // NOTE: do not add any other debugging here, keep forever.
374
375 if ($plugintype === '') {
376 $plugintype = 'mod';
377 }
378
379 return core_component::get_plugin_list($plugintype);
380}
381
382/**
383 * Get a list of all the plugins of a given type that define a certain class
384 * in a certain file. The plugin component names and class names are returned.
385 *
386 * @deprecated since 2.6, use core_component::get_plugin_list_with_class()
387 *
388 * @param string $plugintype the type of plugin, e.g. 'mod' or 'report'.
389 * @param string $class the part of the name of the class after the
390 * frankenstyle prefix. e.g 'thing' if you are looking for classes with
391 * names like report_courselist_thing. If you are looking for classes with
392 * the same name as the plugin name (e.g. qtype_multichoice) then pass ''.
393 * @param string $file the name of file within the plugin that defines the class.
394 * @return array with frankenstyle plugin names as keys (e.g. 'report_courselist', 'mod_forum')
395 * and the class names as values (e.g. 'report_courselist_thing', 'qtype_multichoice').
396 */
397function get_plugin_list_with_class($plugintype, $class, $file) {
398
399 // NOTE: do not add any other debugging here, keep forever.
400
401 return core_component::get_plugin_list_with_class($plugintype, $class, $file);
402}
403
404/**
405 * Returns the exact absolute path to plugin directory.
406 *
407 * @deprecated since 2.6, use core_component::get_plugin_directory()
408 *
409 * @param string $plugintype type of plugin
410 * @param string $name name of the plugin
411 * @return string full path to plugin directory; NULL if not found
412 */
413function get_plugin_directory($plugintype, $name) {
414
415 // NOTE: do not add any other debugging here, keep forever.
416
417 if ($plugintype === '') {
418 $plugintype = 'mod';
419 }
420
421 return core_component::get_plugin_directory($plugintype, $name);
422}
423
424/**
425 * Normalize the component name using the "frankenstyle" names.
426 *
427 * @deprecated since 2.6, use core_component::normalize_component()
428 *
429 * @param string $component
430 * @return array as (string)$type => (string)$plugin
431 */
432function normalize_component($component) {
433
434 // NOTE: do not add any other debugging here, keep forever.
435
436 return core_component::normalize_component($component);
437}
438
439/**
440 * Return exact absolute path to a plugin directory.
441 *
442 * @deprecated since 2.6, use core_component::normalize_component()
443 *
444 * @param string $component name such as 'moodle', 'mod_forum'
445 * @return string full path to component directory; NULL if not found
446 */
447function get_component_directory($component) {
448
449 // NOTE: do not add any other debugging here, keep forever.
450
451 return core_component::get_component_directory($component);
452}
453
454
455// === Deprecated before 2.6.0 ===
456
689096bc
PS
457/**
458 * Hack to find out the GD version by parsing phpinfo output
459 *
460 * @return int GD version (1, 2, or 0)
461 */
462function check_gd_version() {
463 // TODO: delete function in Moodle 2.7
464 debugging('check_gd_version() is deprecated, GD extension is always available now');
465
466 $gdversion = 0;
467
468 if (function_exists('gd_info')){
469 $gd_info = gd_info();
470 if (substr_count($gd_info['GD Version'], '2.')) {
471 $gdversion = 2;
472 } else if (substr_count($gd_info['GD Version'], '1.')) {
473 $gdversion = 1;
474 }
475
476 } else {
477 ob_start();
478 phpinfo(INFO_MODULES);
479 $phpinfo = ob_get_contents();
480 ob_end_clean();
481
482 $phpinfo = explode("\n", $phpinfo);
483
484
485 foreach ($phpinfo as $text) {
486 $parts = explode('</td>', $text);
487 foreach ($parts as $key => $val) {
488 $parts[$key] = trim(strip_tags($val));
489 }
490 if ($parts[0] == 'GD Version') {
491 if (substr_count($parts[1], '2.0')) {
492 $parts[1] = '2.0';
493 }
494 $gdversion = intval($parts[1]);
495 }
496 }
497 }
498
499 return $gdversion; // 1, 2 or 0
500}
501
b28247fe
PS
502/**
503 * Not used any more, the account lockout handling is now
504 * part of authenticate_user_login().
505 * @deprecated
506 */
507function update_login_count() {
c4844bf4
PS
508 // TODO: delete function in Moodle 2.6
509 debugging('update_login_count() is deprecated, all calls need to be removed');
b28247fe
PS
510}
511
512/**
513 * Not used any more, replaced by proper account lockout.
514 * @deprecated
515 */
516function reset_login_count() {
c4844bf4
PS
517 // TODO: delete function in Moodle 2.6
518 debugging('reset_login_count() is deprecated, all calls need to be removed');
b28247fe
PS
519}
520
c6d75bff
PS
521/**
522 * Insert or update log display entry. Entry may already exist.
523 * $module, $action must be unique
524 * @deprecated
525 *
526 * @param string $module
527 * @param string $action
528 * @param string $mtable
529 * @param string $field
530 * @return void
531 *
532 */
533function update_log_display_entry($module, $action, $mtable, $field) {
534 global $DB;
535
536 debugging('The update_log_display_entry() is deprecated, please use db/log.php description file instead.');
537}
538
35716b86
PS
539/**
540 * Given some text in HTML format, this function will pass it
541 * through any filters that have been configured for this context.
542 *
05226d76 543 * @deprecated use the text formatting in a standard way instead (http://docs.moodle.org/dev/Output_functions)
35716b86 544 * this was abused mostly for embedding of attachments
05226d76 545 * @todo final deprecation of this function in MDL-40607
35716b86
PS
546 * @param string $text The text to be passed through format filters
547 * @param int $courseid The current course.
548 * @return string the filtered string.
549 */
550function filter_text($text, $courseid = NULL) {
551 global $CFG, $COURSE;
552
05226d76
DP
553 debugging('filter_text() is deprecated, use format_text(), format_string() etc instead.', DEBUG_DEVELOPER);
554
35716b86
PS
555 if (!$courseid) {
556 $courseid = $COURSE->id;
557 }
558
b0c6dc1c 559 if (!$context = context_course::instance($courseid, IGNORE_MISSING)) {
35716b86
PS
560 return $text;
561 }
562
563 return filter_manager::instance()->filter_text($text, $context);
564}
565
17c70aa0
PS
566/**
567 * This function indicates that current page requires the https
568 * when $CFG->loginhttps enabled.
569 *
570 * By using this function properly, we can ensure 100% https-ized pages
571 * at our entire discretion (login, forgot_password, change_password)
572 * @deprecated use $PAGE->https_required() instead
05226d76 573 * @todo final deprecation of this function in MDL-40607
17c70aa0
PS
574 */
575function httpsrequired() {
576 global $PAGE;
05226d76 577 debugging('httpsrequired() is deprecated use $PAGE->https_required() instead.', DEBUG_DEVELOPER);
17c70aa0
PS
578 $PAGE->https_required();
579}
580
50a8bd6c
PS
581/**
582 * Given a physical path to a file, returns the URL through which it can be reached in Moodle.
583 *
584 * @deprecated use moodle_url factory methods instead
585 *
586 * @param string $path Physical path to a file
587 * @param array $options associative array of GET variables to append to the URL
588 * @param string $type (questionfile|rssfile|httpscoursefile|coursefile)
589 * @return string URL to file
590 */
591function get_file_url($path, $options=null, $type='coursefile') {
17c70aa0 592 global $CFG;
50a8bd6c
PS
593
594 $path = str_replace('//', '/', $path);
595 $path = trim($path, '/'); // no leading and trailing slashes
596
597 // type of file
598 switch ($type) {
599 case 'questionfile':
600 $url = $CFG->wwwroot."/question/exportfile.php";
601 break;
602 case 'rssfile':
603 $url = $CFG->wwwroot."/rss/file.php";
604 break;
605 case 'httpscoursefile':
606 $url = $CFG->httpswwwroot."/file.php";
607 break;
608 case 'coursefile':
609 default:
610 $url = $CFG->wwwroot."/file.php";
611 }
612
613 if ($CFG->slasharguments) {
614 $parts = explode('/', $path);
615 foreach ($parts as $key => $part) {
616 /// anchor dash character should not be encoded
617 $subparts = explode('#', $part);
618 $subparts = array_map('rawurlencode', $subparts);
619 $parts[$key] = implode('#', $subparts);
620 }
621 $path = implode('/', $parts);
622 $ffurl = $url.'/'.$path;
623 $separator = '?';
624 } else {
625 $path = rawurlencode('/'.$path);
626 $ffurl = $url.'?file='.$path;
627 $separator = '&amp;';
628 }
629
630 if ($options) {
631 foreach ($options as $name=>$value) {
632 $ffurl = $ffurl.$separator.$name.'='.$value;
633 $separator = '&amp;';
634 }
635 }
636
637 return $ffurl;
638}
639
613bbd7c 640/**
4f0c2d00 641 * Return all course participant for a given course
613bbd7c 642 *
05226d76
DP
643 * @deprecated use get_enrolled_users($context) instead.
644 * @todo final deprecation of this function in MDL-40607
4f0c2d00
PS
645 * @param integer $courseid
646 * @return array of user
613bbd7c 647 */
4f0c2d00 648function get_course_participants($courseid) {
05226d76 649 debugging('get_course_participants() is deprecated, use get_enrolled_users() instead.', DEBUG_DEVELOPER);
b0c6dc1c 650 return get_enrolled_users(context_course::instance($courseid));
613bbd7c 651}
652
613bbd7c 653/**
4f0c2d00 654 * Return true if the user is a participant for a given course
613bbd7c 655 *
05226d76
DP
656 * @deprecated use is_enrolled($context, $userid) instead.
657 * @todo final deprecation of this function in MDL-40607
4f0c2d00
PS
658 * @param integer $userid
659 * @param integer $courseid
660 * @return boolean
613bbd7c 661 */
4f0c2d00 662function is_course_participant($userid, $courseid) {
05226d76 663 debugging('is_course_participant() is deprecated, use is_enrolled() instead.', DEBUG_DEVELOPER);
b0c6dc1c 664 return is_enrolled(context_course::instance($courseid), $userid);
613bbd7c 665}
666
667/**
668 * Searches logs to find all enrolments since a certain date
669 *
670 * used to print recent activity
671 *
613bbd7c 672 * @param int $courseid The course in question.
c861fe2f 673 * @param int $timestart The date to check forward of
613bbd7c 674 * @return object|false {@link $USER} records or false if error.
613bbd7c 675 */
676function get_recent_enrolments($courseid, $timestart) {
10df888a 677 global $DB;
364fffda 678
02ca2cad 679 debugging('get_recent_enrolments() is deprecated as it returned inaccurate results.', DEBUG_DEVELOPER);
613bbd7c 680
02ca2cad 681 $context = context_course::instance($courseid);
210751f6 682 $sql = "SELECT u.id, u.firstname, u.lastname, MAX(l.time)
10df888a 683 FROM {user} u, {role_assignments} ra, {log} l
684 WHERE l.time > ?
685 AND l.course = ?
686 AND l.module = 'course'
687 AND l.action = 'enrol'
9f43d70d 688 AND ".$DB->sql_cast_char2int('l.info')." = u.id
10df888a 689 AND u.id = ra.userid
690 AND ra.contextid ".get_related_contexts_string($context)."
210751f6
PS
691 GROUP BY u.id, u.firstname, u.lastname
692 ORDER BY MAX(l.time) ASC";
10df888a 693 $params = array($timestart, $courseid);
694 return $DB->get_records_sql($sql, $params);
613bbd7c 695}
696
ed5dd29f 697/**
05226d76
DP
698 * @deprecated use clean_param($string, PARAM_FILE) instead.
699 * @todo final deprecation of this function in MDL-40607
c861fe2f 700 *
ed5dd29f 701 * @param string $string ?
702 * @param int $allowdots ?
c861fe2f 703 * @return bool
ed5dd29f 704 */
705function detect_munged_arguments($string, $allowdots=1) {
05226d76 706 debugging('detect_munged_arguments() is deprecated, please use clean_param(,PARAM_FILE) instead.', DEBUG_DEVELOPER);
ed5dd29f 707 if (substr_count($string, '..') > $allowdots) { // Sometimes we allow dots in references
708 return true;
709 }
69593309 710 if (preg_match('/[\|\`]/', $string)) { // check for other bad characters
ed5dd29f 711 return true;
712 }
713 if (empty($string) or $string == '/') {
714 return true;
715 }
716
717 return false;
718}
719
9152fc99 720
0c6d2dd4 721/**
722 * Unzip one zip file to a destination dir
723 * Both parameters must be FULL paths
724 * If destination isn't specified, it will be the
725 * SAME directory where the zip file resides.
c861fe2f 726 *
727 * @global object
728 * @param string $zipfile The zip file to unzip
729 * @param string $destination The location to unzip to
730 * @param bool $showstatus_ignored Unused
0c6d2dd4 731 */
732function unzip_file($zipfile, $destination = '', $showstatus_ignored = true) {
733 global $CFG;
734
735 //Extract everything from zipfile
736 $path_parts = pathinfo(cleardoubleslashes($zipfile));
737 $zippath = $path_parts["dirname"]; //The path of the zip file
738 $zipfilename = $path_parts["basename"]; //The name of the zip file
739 $extension = $path_parts["extension"]; //The extension of the file
740
741 //If no file, error
742 if (empty($zipfilename)) {
743 return false;
744 }
745
746 //If no extension, error
747 if (empty($extension)) {
748 return false;
749 }
750
751 //Clear $zipfile
752 $zipfile = cleardoubleslashes($zipfile);
753
754 //Check zipfile exists
755 if (!file_exists($zipfile)) {
756 return false;
757 }
758
759 //If no destination, passed let's go with the same directory
760 if (empty($destination)) {
761 $destination = $zippath;
762 }
763
764 //Clear $destination
765 $destpath = rtrim(cleardoubleslashes($destination), "/");
766
767 //Check destination path exists
768 if (!is_dir($destpath)) {
769 return false;
770 }
771
0b0bfa93 772 $packer = get_file_packer('application/zip');
773
774 $result = $packer->extract_to_pathname($zipfile, $destpath);
0c6d2dd4 775
776 if ($result === false) {
777 return false;
778 }
779
780 foreach ($result as $status) {
781 if ($status !== true) {
782 return false;
783 }
784 }
785
786 return true;
787}
788
ed94cb66 789/**
790 * Zip an array of files/dirs to a destination zip file
791 * Both parameters must be FULL paths to the files/dirs
c861fe2f 792 *
793 * @global object
794 * @param array $originalfiles Files to zip
795 * @param string $destination The destination path
796 * @return bool Outcome
ed94cb66 797 */
798function zip_files ($originalfiles, $destination) {
799 global $CFG;
800
801 //Extract everything from destination
802 $path_parts = pathinfo(cleardoubleslashes($destination));
803 $destpath = $path_parts["dirname"]; //The path of the zip file
804 $destfilename = $path_parts["basename"]; //The name of the zip file
805 $extension = $path_parts["extension"]; //The extension of the file
806
807 //If no file, error
808 if (empty($destfilename)) {
809 return false;
810 }
811
812 //If no extension, add it
813 if (empty($extension)) {
814 $extension = 'zip';
815 $destfilename = $destfilename.'.'.$extension;
816 }
817
818 //Check destination path exists
819 if (!is_dir($destpath)) {
820 return false;
821 }
822
823 //Check destination path is writable. TODO!!
824
825 //Clean destination filename
826 $destfilename = clean_filename($destfilename);
827
828 //Now check and prepare every file
829 $files = array();
830 $origpath = NULL;
831
832 foreach ($originalfiles as $file) { //Iterate over each file
833 //Check for every file
834 $tempfile = cleardoubleslashes($file); // no doubleslashes!
835 //Calculate the base path for all files if it isn't set
836 if ($origpath === NULL) {
837 $origpath = rtrim(cleardoubleslashes(dirname($tempfile)), "/");
838 }
839 //See if the file is readable
840 if (!is_readable($tempfile)) { //Is readable
841 continue;
842 }
843 //See if the file/dir is in the same directory than the rest
844 if (rtrim(cleardoubleslashes(dirname($tempfile)), "/") != $origpath) {
845 continue;
846 }
847 //Add the file to the array
848 $files[] = $tempfile;
849 }
850
851 $zipfiles = array();
852 $start = strlen($origpath)+1;
853 foreach($files as $file) {
854 $zipfiles[substr($file, $start)] = $file;
855 }
856
0b0bfa93 857 $packer = get_file_packer('application/zip');
ed94cb66 858
3ed22f1a 859 return $packer->archive_to_pathname($zipfiles, $destpath . '/' . $destfilename);
ed94cb66 860}
861
5bf243d1 862/**
863 * Get the IDs for the user's groups in the given course.
864 *
c861fe2f 865 * @global object
5bf243d1 866 * @param int $courseid The course being examined - the 'course' table id field.
c861fe2f 867 * @return array|bool An _array_ of groupids, or false
5bf243d1 868 * (Was return $groupids[0] - consequences!)
05226d76
DP
869 * @deprecated use groups_get_all_groups() instead.
870 * @todo final deprecation of this function in MDL-40607
5bf243d1 871 */
872function mygroupid($courseid) {
873 global $USER;
05226d76
DP
874
875 debugging('mygroupid() is deprecated, please use groups_get_all_groups() instead.', DEBUG_DEVELOPER);
876
5bf243d1 877 if ($groups = groups_get_all_groups($courseid, $USER->id)) {
878 return array_keys($groups);
879 } else {
880 return false;
881 }
882}
883
5bf243d1 884
5bf243d1 885/**
886 * Returns the current group mode for a given course or activity module
364fffda 887 *
5bf243d1 888 * Could be false, SEPARATEGROUPS or VISIBLEGROUPS (<-- Martin)
c861fe2f 889 *
890 * @param object $course Course Object
891 * @param object $cm Course Manager Object
892 * @return mixed $course->groupmode
5bf243d1 893 */
894function groupmode($course, $cm=null) {
895
896 if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
897 return $cm->groupmode;
898 }
899 return $course->groupmode;
900}
901
c584346c 902/**
903 * Sets the current group in the session variable
904 * When $SESSION->currentgroup[$courseid] is set to 0 it means, show all groups.
905 * Sets currentgroup[$courseid] in the session variable appropriately.
906 * Does not do any permission checking.
c861fe2f 907 *
908 * @global object
c584346c 909 * @param int $courseid The course being examined - relates to id field in
910 * 'course' table.
911 * @param int $groupid The group being examined.
912 * @return int Current group id which was set by this function
913 */
914function set_current_group($courseid, $groupid) {
915 global $SESSION;
916 return $SESSION->currentgroup[$courseid] = $groupid;
917}
918
5bf243d1 919
5bf243d1 920/**
364fffda 921 * Gets the current group - either from the session variable or from the database.
5bf243d1 922 *
c861fe2f 923 * @global object
364fffda 924 * @param int $courseid The course being examined - relates to id field in
5bf243d1 925 * 'course' table.
364fffda 926 * @param bool $full If true, the return value is a full record object.
5bf243d1 927 * If false, just the id of the record.
c861fe2f 928 * @return int|bool
5bf243d1 929 */
930function get_current_group($courseid, $full = false) {
931 global $SESSION;
932
933 if (isset($SESSION->currentgroup[$courseid])) {
934 if ($full) {
935 return groups_get_group($SESSION->currentgroup[$courseid]);
936 } else {
937 return $SESSION->currentgroup[$courseid];
938 }
939 }
940
941 $mygroupid = mygroupid($courseid);
942 if (is_array($mygroupid)) {
943 $mygroupid = array_shift($mygroupid);
944 set_current_group($courseid, $mygroupid);
945 if ($full) {
946 return groups_get_group($mygroupid);
947 } else {
948 return $mygroupid;
949 }
950 }
951
952 if ($full) {
953 return false;
954 } else {
955 return 0;
956 }
957}
958
061e6b28 959/**
960 * Filter a user list and return only the users that can see the course module based on
961 * groups/permissions etc. It is assumed that the users are pre-filtered to those who are enrolled in the course.
962 *
963 * @category group
964 * @param stdClass|cm_info $cm The course module
965 * @param array $users An array of users, indexed by userid
966 * @return array A filtered list of users that can see the module, indexed by userid.
967 * @deprecated Since Moodle 2.8
968 */
969function groups_filter_users_by_course_module_visible($cm, $users) {
970 debugging('groups_filter_users_by_course_module_visible() is deprecated. ' .
971 'Replace with a call to \core_availability\info_module::filter_user_list(), ' .
972 'which does basically the same thing but includes other restrictions such ' .
973 'as profile restrictions.', DEBUG_DEVELOPER);
974 if (empty($users)) {
975 return $users;
976 }
977 // Since this function allows stdclass, let's play it safe and ensure we
978 // do have a cm_info.
979 if (!($cm instanceof cm_info)) {
980 $modinfo = get_fast_modinfo($cm->course);
981 $cm = $modinfo->get_cm($cm->id);
982 }
983 $info = new \core_availability\info_module($cm);
984 return $info->filter_user_list($users);
985}
986
987/**
988 * Determine if a course module is currently visible to a user
989 *
990 * Deprecated (it was never very useful as it only took into account the
991 * groupmembersonly option and no other way of hiding activities). Always
992 * returns true.
993 *
994 * @category group
995 * @param stdClass|cm_info $cm The course module
996 * @param int $userid The user to check against the group.
997 * @return bool True
998 * @deprecated Since Moodle 2.8
999 */
1000function groups_course_module_visible($cm, $userid=null) {
1001 debugging('groups_course_module_visible() is deprecated and always returns ' .
1002 'true; use $cm->uservisible to decide whether the current user can ' .
1003 'access an activity.', DEBUG_DEVELOPER);
1004 return true;
1005}
5bf243d1 1006
8ec50604 1007/**
a8ab8de9
PS
1008 * Inndicates fatal error. This function was originally printing the
1009 * error message directly, since 2.0 it is throwing exception instead.
3400bf6c 1010 * The error printing is handled in default exception handler.
a8ab8de9 1011 *
8ec50604 1012 * Old method, don't call directly in new code - use print_error instead.
1013 *
8ec50604 1014 * @param string $message The message to display to the user about the error.
1015 * @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.
a8ab8de9 1016 * @return void, always throws moodle_exception
8ec50604 1017 */
245ac557 1018function error($message, $link='') {
78946b9b 1019 throw new moodle_exception('notlocalisederrormessage', 'error', $link, $message, 'error() is a deprecated function, please call print_error() instead of error()');
251387d0 1020}
8ec50604 1021
8ec50604 1022
b7009474 1023/**
1024 * @deprecated use $PAGE->theme->name instead.
05226d76 1025 * @todo final deprecation of this function in MDL-40607
b7009474 1026 * @return string the name of the current theme.
1027 */
1028function current_theme() {
1029 global $PAGE;
05226d76
DP
1030
1031 debugging('current_theme() is deprecated, please use $PAGE->theme->name instead', DEBUG_DEVELOPER);
b7009474 1032 return $PAGE->theme->name;
1033}
1034
8954245a 1035/**
1036 * Prints some red text using echo
1037 *
1038 * @deprecated
1039 * @param string $error The text to be displayed in red
1040 */
1041function formerr($error) {
3bd6b994 1042 debugging('formerr() has been deprecated. Please change your code to use $OUTPUT->error_text($string).');
8954245a 1043 global $OUTPUT;
1044 echo $OUTPUT->error_text($error);
1045}
1046
34a2777c 1047/**
1048 * Return the markup for the destination of the 'Skip to main content' links.
1049 * Accessibility improvement for keyboard-only users.
1050 *
1051 * Used in course formats, /index.php and /course/index.php
1052 *
1053 * @deprecated use $OUTPUT->skip_link_target() in instead.
05226d76 1054 * @todo final deprecation of this function in MDL-40607
34a2777c 1055 * @return string HTML element.
1056 */
1057function skip_main_destination() {
1058 global $OUTPUT;
05226d76
DP
1059
1060 debugging('skip_main_destination() is deprecated, please use $OUTPUT->skip_link_target() instead.', DEBUG_DEVELOPER);
34a2777c 1061 return $OUTPUT->skip_link_target();
1062}
1063
34a2777c 1064/**
1065 * Print a message in a standard themed container.
1066 *
05226d76
DP
1067 * @deprecated use $OUTPUT->container() instead.
1068 * @todo final deprecation of this function in MDL-40607
34a2777c 1069 * @param string $message, the content of the container
1070 * @param boolean $clearfix clear both sides
1071 * @param string $classes, space-separated class names.
1072 * @param string $idbase
1073 * @param boolean $return, return as string or just print it
1074 * @return string|void Depending on value of $return
1075 */
1076function print_container($message, $clearfix=false, $classes='', $idbase='', $return=false) {
1077 global $OUTPUT;
05226d76
DP
1078
1079 debugging('print_container() is deprecated. Please use $OUTPUT->container() instead.', DEBUG_DEVELOPER);
34a2777c 1080 if ($clearfix) {
1081 $classes .= ' clearfix';
1082 }
1083 $output = $OUTPUT->container($message, $classes, $idbase);
1084 if ($return) {
1085 return $output;
1086 } else {
1087 echo $output;
1088 }
1089}
1090
1091/**
1092 * Starts a container using divs
1093 *
05226d76
DP
1094 * @deprecated use $OUTPUT->container_start() instead.
1095 * @todo final deprecation of this function in MDL-40607
34a2777c 1096 * @param boolean $clearfix clear both sides
1097 * @param string $classes, space-separated class names.
1098 * @param string $idbase
1099 * @param boolean $return, return as string or just print it
1100 * @return string|void Based on value of $return
1101 */
1102function print_container_start($clearfix=false, $classes='', $idbase='', $return=false) {
1103 global $OUTPUT;
05226d76
DP
1104
1105 debugging('print_container_start() is deprecated. Please use $OUTPUT->container_start() instead.', DEBUG_DEVELOPER);
1106
34a2777c 1107 if ($clearfix) {
1108 $classes .= ' clearfix';
1109 }
1110 $output = $OUTPUT->container_start($classes, $idbase);
1111 if ($return) {
1112 return $output;
1113 } else {
1114 echo $output;
1115 }
1116}
1117
1118/**
1119 * Simple function to end a container (see above)
1120 *
05226d76
DP
1121 * @deprecated use $OUTPUT->container_end() instead.
1122 * @todo final deprecation of this function in MDL-40607
34a2777c 1123 * @param boolean $return, return as string or just print it
1124 * @return string|void Based on $return
1125 */
1126function print_container_end($return=false) {
1127 global $OUTPUT;
05226d76 1128 debugging('print_container_end() is deprecated. Please use $OUTPUT->container_end() instead.', DEBUG_DEVELOPER);
34a2777c 1129 $output = $OUTPUT->container_end();
1130 if ($return) {
1131 return $output;
1132 } else {
1133 echo $output;
1134 }
1135}
1136
1137/**
1138 * Print a bold message in an optional color.
1139 *
1140 * @deprecated use $OUTPUT->notification instead.
1141 * @param string $message The message to print out
1142 * @param string $style Optional style to display message text in
1143 * @param string $align Alignment option
1144 * @param bool $return whether to return an output string or echo now
a5cb8d69 1145 * @return string|bool Depending on $result
34a2777c 1146 */
1147function notify($message, $classes = 'notifyproblem', $align = 'center', $return = false) {
1148 global $OUTPUT;
1149
1150 if ($classes == 'green') {
1151 debugging('Use of deprecated class name "green" in notify. Please change to "notifysuccess".', DEBUG_DEVELOPER);
1152 $classes = 'notifysuccess'; // Backward compatible with old color system
1153 }
1154
1155 $output = $OUTPUT->notification($message, $classes);
1156 if ($return) {
1157 return $output;
1158 } else {
1159 echo $output;
1160 }
1161}
1162
1163/**
1164 * Print a continue button that goes to a particular URL.
1165 *
05226d76
DP
1166 * @deprecated use $OUTPUT->continue_button() instead.
1167 * @todo final deprecation of this function in MDL-40607
74623e0a 1168 *
34a2777c 1169 * @param string $link The url to create a link to.
1170 * @param bool $return If set to true output is returned rather than echoed, default false
1171 * @return string|void HTML String if return=true nothing otherwise
1172 */
1173function print_continue($link, $return = false) {
1174 global $CFG, $OUTPUT;
1175
05226d76
DP
1176 debugging('print_continue() is deprecated. Please use $OUTPUT->continue_button() instead.', DEBUG_DEVELOPER);
1177
34a2777c 1178 if ($link == '') {
1179 if (!empty($_SERVER['HTTP_REFERER'])) {
1180 $link = $_SERVER['HTTP_REFERER'];
1181 $link = str_replace('&', '&amp;', $link); // make it valid XHTML
1182 } else {
1183 $link = $CFG->wwwroot .'/';
1184 }
1185 }
1186
1187 $output = $OUTPUT->continue_button($link);
1188 if ($return) {
1189 return $output;
1190 } else {
1191 echo $output;
1192 }
1193}
1194
34a2777c 1195/**
1196 * Print a standard header
1197 *
05226d76
DP
1198 * @deprecated use $PAGE methods instead.
1199 * @todo final deprecation of this function in MDL-40607
34a2777c 1200 * @param string $title Appears at the top of the window
1201 * @param string $heading Appears at the top of the page
1202 * @param string $navigation Array of $navlinks arrays (keys: name, link, type) for use as breadcrumbs links
1203 * @param string $focus Indicates form element to get cursor focus on load eg inputform.password
1204 * @param string $meta Meta tags to be added to the header
1205 * @param boolean $cache Should this page be cacheable?
1206 * @param string $button HTML code for a button (usually for module editing)
1207 * @param string $menu HTML code for a popup menu
1208 * @param boolean $usexml use XML for this page
1209 * @param string $bodytags This text will be included verbatim in the <body> tag (useful for onload() etc)
1210 * @param bool $return If true, return the visible elements of the header instead of echoing them.
1211 * @return string|void If return=true then string else void
1212 */
1213function print_header($title='', $heading='', $navigation='', $focus='',
e120c61d 1214 $meta='', $cache=true, $button='&nbsp;', $menu=null,
34a2777c 1215 $usexml=false, $bodytags='', $return=false) {
1216 global $PAGE, $OUTPUT;
1217
05226d76
DP
1218 debugging('print_header() is deprecated. Please use $PAGE methods instead.', DEBUG_DEVELOPER);
1219
34a2777c 1220 $PAGE->set_title($title);
1221 $PAGE->set_heading($heading);
3a11c09f 1222 $PAGE->set_cacheable($cache);
34a2777c 1223 if ($button == '') {
1224 $button = '&nbsp;';
1225 }
1226 $PAGE->set_button($button);
e120c61d 1227 $PAGE->set_headingmenu($menu);
34a2777c 1228
34a2777c 1229 // TODO $menu
1230
1231 if ($meta) {
1232 throw new coding_exception('The $meta parameter to print_header is no longer supported. '.
e29380f3 1233 'You should be able to do everything you want with $PAGE->requires and other such mechanisms.');
34a2777c 1234 }
1235 if ($usexml) {
1236 throw new coding_exception('The $usexml parameter to print_header is no longer supported.');
1237 }
1238 if ($bodytags) {
1239 throw new coding_exception('The $bodytags parameter to print_header is no longer supported.');
1240 }
1241
e120c61d 1242 $output = $OUTPUT->header();
34a2777c 1243
1244 if ($return) {
1245 return $output;
1246 } else {
1247 echo $output;
1248 }
1249}
1250
47a1aa45 1251/**
1252 * This version of print_header is simpler because the course name does not have to be
1253 * provided explicitly in the strings. It can be used on the site page as in courses
1254 * Eventually all print_header could be replaced by print_header_simple
1255 *
05226d76
DP
1256 * @deprecated use $PAGE methods instead.
1257 * @todo final deprecation of this function in MDL-40607
47a1aa45 1258 * @param string $title Appears at the top of the window
1259 * @param string $heading Appears at the top of the page
1260 * @param string $navigation Premade navigation string (for use as breadcrumbs links)
1261 * @param string $focus Indicates form element to get cursor focus on load eg inputform.password
1262 * @param string $meta Meta tags to be added to the header
1263 * @param boolean $cache Should this page be cacheable?
1264 * @param string $button HTML code for a button (usually for module editing)
1265 * @param string $menu HTML code for a popup menu
1266 * @param boolean $usexml use XML for this page
1267 * @param string $bodytags This text will be included verbatim in the <body> tag (useful for onload() etc)
1268 * @param bool $return If true, return the visible elements of the header instead of echoing them.
1269 * @return string|void If $return=true the return string else nothing
1270 */
1271function print_header_simple($title='', $heading='', $navigation='', $focus='', $meta='',
1272 $cache=true, $button='&nbsp;', $menu='', $usexml=false, $bodytags='', $return=false) {
1273
1274 global $COURSE, $CFG, $PAGE, $OUTPUT;
1275
05226d76
DP
1276 debugging('print_header_simple() is deprecated. Please use $PAGE methods instead.', DEBUG_DEVELOPER);
1277
47a1aa45 1278 if ($meta) {
1279 throw new coding_exception('The $meta parameter to print_header is no longer supported. '.
1280 'You should be able to do everything you want with $PAGE->requires and other such mechanisms.');
1281 }
1282 if ($usexml) {
1283 throw new coding_exception('The $usexml parameter to print_header is no longer supported.');
1284 }
1285 if ($bodytags) {
1286 throw new coding_exception('The $bodytags parameter to print_header is no longer supported.');
1287 }
1288
1289 $PAGE->set_title($title);
3a11c09f 1290 $PAGE->set_heading($heading);
47a1aa45 1291 $PAGE->set_cacheable(true);
1292 $PAGE->set_button($button);
1293
1294 $output = $OUTPUT->header();
1295
1296 if ($return) {
1297 return $output;
1298 } else {
1299 echo $output;
1300 }
1301}
1302
a5cb8d69 1303/**
1304 * Prints a nice side block with an optional header. The content can either
1305 * be a block of HTML or a list of text with optional icons.
1306 *
a5cb8d69 1307 * @static int $block_id Increments for each call to the function
1308 * @param string $heading HTML for the heading. Can include full HTML or just
1309 * plain text - plain text will automatically be enclosed in the appropriate
1310 * heading tags.
1311 * @param string $content HTML for the content
1312 * @param array $list an alternative to $content, it you want a list of things with optional icons.
1313 * @param array $icons optional icons for the things in $list.
1314 * @param string $footer Extra HTML content that gets output at the end, inside a &lt;div class="footer">
1315 * @param array $attributes an array of attribute => value pairs that are put on the
6605ff8c
SH
1316 * outer div of this block. If there is a class attribute ' block' gets appended to it. If there isn't
1317 * already a class, class='block' is used.
a5cb8d69 1318 * @param string $title Plain text title, as embedded in the $heading.
05226d76
DP
1319 * @deprecated use $OUTPUT->block() instead.
1320 * @todo final deprecation of this function in MDL-40607
a5cb8d69 1321 */
1322function print_side_block($heading='', $content='', $list=NULL, $icons=NULL, $footer='', $attributes = array(), $title='') {
1323 global $OUTPUT;
d4a03c00 1324
05226d76 1325 debugging('print_side_block() is deprecated, please use $OUTPUT->block() instead.', DEBUG_DEVELOPER);
d4a03c00 1326 // We don't use $heading, becuse it often contains HTML that we don't want.
1327 // However, sometimes $title is not set, but $heading is.
1328 if (empty($title)) {
1329 $title = strip_tags($heading);
1330 }
1331
1332 // Render list contents to HTML if required.
1333 if (empty($content) && $list) {
1334 $content = $OUTPUT->list_block_contents($icons, $list);
1335 }
1336
a5cb8d69 1337 $bc = new block_contents();
a5cb8d69 1338 $bc->content = $content;
a5cb8d69 1339 $bc->footer = $footer;
1340 $bc->title = $title;
1341
1342 if (isset($attributes['id'])) {
1343 $bc->id = $attributes['id'];
1344 unset($attributes['id']);
1345 }
a5cb8d69 1346 $bc->attributes = $attributes;
1347
3ceb6910 1348 echo $OUTPUT->block($bc, BLOCK_POS_LEFT); // POS LEFT may be wrong, but no way to get a better guess here.
a5cb8d69 1349}
1350
f8065dd2 1351/**
1352 * Prints a basic textarea field.
1353 *
1354 * @deprecated since Moodle 2.0
1355 *
1356 * When using this function, you should
1357 *
1358 * @global object
3d27180e 1359 * @param bool $unused No longer used.
f8065dd2 1360 * @param int $rows Number of rows to display (minimum of 10 when $height is non-null)
1361 * @param int $cols Number of columns to display (minimum of 65 when $width is non-null)
1362 * @param null $width (Deprecated) Width of the element; if a value is passed, the minimum value for $cols will be 65. Value is otherwise ignored.
1363 * @param null $height (Deprecated) Height of the element; if a value is passe, the minimum value for $rows will be 10. Value is otherwise ignored.
1364 * @param string $name Name to use for the textarea element.
1365 * @param string $value Initial content to display in the textarea.
1366 * @param int $obsolete deprecated
1367 * @param bool $return If false, will output string. If true, will return string value.
1368 * @param string $id CSS ID to add to the textarea element.
1369 * @return string|void depending on the value of $return
1370 */
3d27180e 1371function print_textarea($unused, $rows, $cols, $width, $height, $name, $value='', $obsolete=0, $return=false, $id='') {
f8065dd2 1372 /// $width and height are legacy fields and no longer used as pixels like they used to be.
1373 /// However, you can set them to zero to override the mincols and minrows values below.
1374
3d8a479b
MD
1375 // Disabling because there is not yet a viable $OUTPUT option for cases when mforms can't be used
1376 // debugging('print_textarea() has been deprecated. You should be using mforms and the editor element.');
f8065dd2 1377
1378 global $CFG;
1379
1380 $mincols = 65;
1381 $minrows = 10;
1382 $str = '';
1383
1384 if ($id === '') {
1385 $id = 'edit-'.$name;
1386 }
1387
3d27180e
DW
1388 if ($height && ($rows < $minrows)) {
1389 $rows = $minrows;
f8065dd2 1390 }
3d27180e
DW
1391 if ($width && ($cols < $mincols)) {
1392 $cols = $mincols;
f8065dd2 1393 }
1394
3d27180e
DW
1395 editors_head_setup();
1396 $editor = editors_get_preferred_editor(FORMAT_HTML);
1397 $editor->use_editor($id, array('legacy'=>true));
1398
0ac97084 1399 $str .= "\n".'<textarea class="form-textarea" id="'. $id .'" name="'. $name .'" rows="'. $rows .'" cols="'. $cols .'" spellcheck="true">'."\n";
3d27180e 1400 $str .= htmlspecialchars($value); // needed for editing of cleaned text!
f8065dd2 1401 $str .= '</textarea>'."\n";
1402
1403 if ($return) {
1404 return $str;
1405 }
1406 echo $str;
1407}
1408
4bcc5118
PS
1409/**
1410 * Returns a string of html with an image of a help icon linked to a help page on a number of help topics.
1411 * Should be used only with htmleditor or textarea.
1412 *
1413 * @global object
1414 * @global object
1415 * @param mixed $helptopics variable amount of params accepted. Each param may be a string or an array of arguments for
1416 * helpbutton.
1417 * @return string Link to help button
1418 */
1419function editorhelpbutton(){
1420 return '';
1421
1422 /// TODO: MDL-21215
f8065dd2 1423}
1424
4bcc5118
PS
1425/**
1426 * Print a help button.
1427 *
1428 * Prints a special help button for html editors (htmlarea in this case)
1429 *
1430 * @todo Write code into this function! detect current editor and print correct info
1431 * @global object
1432 * @return string Only returns an empty string at the moment
1433 */
1434function editorshortcutshelpbutton() {
1435 /// TODO: MDL-21215
1436
1437 global $CFG;
1438 //TODO: detect current editor and print correct info
4bcc5118
PS
1439 return '';
1440}
1441
1442
f8065dd2 1443/**
1444 * Returns an image of an up or down arrow, used for column sorting. To avoid unnecessary DB accesses, please
1445 * provide this function with the language strings for sortasc and sortdesc.
1446 *
05226d76
DP
1447 * @deprecated use $OUTPUT->arrow() instead.
1448 * @todo final deprecation of this function in MDL-40607
f8065dd2 1449 *
f8065dd2 1450 * If no sort string is associated with the direction, an arrow with no alt text will be printed/returned.
1451 *
1452 * @global object
1453 * @param string $direction 'up' or 'down'
1454 * @param string $strsort The language string used for the alt attribute of this image
1455 * @param bool $return Whether to print directly or return the html string
1456 * @return string|void depending on $return
1457 *
1458 */
1459function print_arrow($direction='up', $strsort=null, $return=false) {
f8065dd2 1460 global $OUTPUT;
1461
05226d76
DP
1462 debugging('print_arrow() is deprecated. Please use $OUTPUT->arrow() instead.', DEBUG_DEVELOPER);
1463
f8065dd2 1464 if (!in_array($direction, array('up', 'down', 'right', 'left', 'move'))) {
1465 return null;
1466 }
1467
1468 $return = null;
1469
1470 switch ($direction) {
1471 case 'up':
1472 $sortdir = 'asc';
1473 break;
1474 case 'down':
1475 $sortdir = 'desc';
1476 break;
1477 case 'move':
1478 $sortdir = 'asc';
1479 break;
1480 default:
1481 $sortdir = null;
1482 break;
1483 }
1484
1485 // Prepare language string
1486 $strsort = '';
1487 if (empty($strsort) && !empty($sortdir)) {
1488 $strsort = get_string('sort' . $sortdir, 'grades');
1489 }
1490
b5d0cafc 1491 $return = ' <img src="'.$OUTPUT->pix_url('t/' . $direction) . '" alt="'.$strsort.'" /> ';
f8065dd2 1492
1493 if ($return) {
1494 return $return;
1495 } else {
1496 echo $return;
1497 }
1498}
1499
8100c169 1500/**
1501 * Given an array of values, output the HTML for a select element with those options.
1502 *
1503 * @deprecated since Moodle 2.0
1504 *
1505 * Normally, you only need to use the first few parameters.
1506 *
1507 * @param array $options The options to offer. An array of the form
1508 * $options[{value}] = {text displayed for that option};
1509 * @param string $name the name of this form control, as in &lt;select name="..." ...
1510 * @param string $selected the option to select initially, default none.
1511 * @param string $nothing The label for the 'nothing is selected' option. Defaults to get_string('choose').
1512 * Set this to '' if you don't want a 'nothing is selected' option.
1513 * @param string $script if not '', then this is added to the &lt;select> element as an onchange handler.
1514 * @param string $nothingvalue The value corresponding to the $nothing option. Defaults to 0.
1515 * @param boolean $return if false (the default) the the output is printed directly, If true, the
1516 * generated HTML is returned as a string.
1517 * @param boolean $disabled if true, the select is generated in a disabled state. Default, false.
1518 * @param int $tabindex if give, sets the tabindex attribute on the &lt;select> element. Default none.
1519 * @param string $id value to use for the id attribute of the &lt;select> element. If none is given,
1520 * then a suitable one is constructed.
1521 * @param mixed $listbox if false, display as a dropdown menu. If true, display as a list box.
1522 * By default, the list box will have a number of rows equal to min(10, count($options)), but if
1523 * $listbox is an integer, that number is used for size instead.
1524 * @param boolean $multiple if true, enable multiple selections, else only 1 item can be selected. Used
1525 * when $listbox display is enabled
1526 * @param string $class value to use for the class attribute of the &lt;select> element. If none is given,
1527 * then a suitable one is constructed.
1528 * @return string|void If $return=true returns string, else echo's and returns void
1529 */
1530function choose_from_menu ($options, $name, $selected='', $nothing='choose', $script='',
1531 $nothingvalue='0', $return=false, $disabled=false, $tabindex=0,
1532 $id='', $listbox=false, $multiple=false, $class='') {
053203a8 1533
8100c169 1534 global $OUTPUT;
4461cc60 1535 debugging('choose_from_menu() has been deprecated. Please change your code to use html_writer::select().');
053203a8 1536
1537 if ($script) {
1538 debugging('The $script parameter has been deprecated. You must use component_actions instead', DEBUG_DEVELOPER);
1539 }
4461cc60
PS
1540 $attributes = array();
1541 $attributes['disabled'] = $disabled ? 'disabled' : null;
1542 $attributes['tabindex'] = $tabindex ? $tabindex : null;
1543 $attributes['multiple'] = $multiple ? $multiple : null;
1544 $attributes['class'] = $class ? $class : null;
1545 $attributes['id'] = $id ? $id : null;
053203a8 1546
4461cc60 1547 $output = html_writer::select($options, $name, $selected, array($nothingvalue=>$nothing), $attributes);
053203a8 1548
1549 if ($return) {
1550 return $output;
1551 } else {
1552 echo $output;
8100c169 1553 }
053203a8 1554}
1555
c68e4098 1556/**
1557 * Prints a help button about a scale
1558 *
05226d76
DP
1559 * @deprecated use $OUTPUT->help_icon_scale($courseid, $scale) instead.
1560 * @todo final deprecation of this function in MDL-40607
c68e4098 1561 *
1562 * @global object
1563 * @param id $courseid
1564 * @param object $scale
1565 * @param boolean $return If set to true returns rather than echo's
1566 * @return string|bool Depending on value of $return
1567 */
1568function print_scale_menu_helpbutton($courseid, $scale, $return=false) {
c68e4098 1569 global $OUTPUT;
1570
05226d76
DP
1571 debugging('print_scale_menu_helpbutton() is deprecated. Please use $OUTPUT->help_icon_scale($courseid, $scale) instead.', DEBUG_DEVELOPER);
1572
4bcc5118 1573 $output = $OUTPUT->help_icon_scale($courseid, $scale);
c68e4098 1574
1575 if ($return) {
1576 return $output;
1577 } else {
1578 echo $output;
1579 }
1580}
1581
49c8c8d2 1582/**
1583 * Display an standard html checkbox with an optional label
1584 *
05226d76
DP
1585 * @deprecated use html_writer::checkbox() instead.
1586 * @todo final deprecation of this function in MDL-40607
49c8c8d2 1587 *
1588 * @staticvar int $idcounter
1589 * @param string $name The name of the checkbox
1590 * @param string $value The valus that the checkbox will pass when checked
1591 * @param bool $checked The flag to tell the checkbox initial state
1592 * @param string $label The label to be showed near the checkbox
1593 * @param string $alt The info to be inserted in the alt tag
1594 * @param string $script If not '', then this is added to the checkbox element
1595 * as an onchange handler.
1596 * @param bool $return Whether this function should return a string or output
1597 * it (defaults to false)
1598 * @return string|void If $return=true returns string, else echo's and returns void
1599 */
916276fc 1600function print_checkbox($name, $value, $checked = true, $label = '', $alt = '', $script='', $return=false) {
49c8c8d2 1601 global $OUTPUT;
6a5c71b9 1602
05226d76
DP
1603 debugging('print_checkbox() is deprecated. Please use html_writer::checkbox() instead.', DEBUG_DEVELOPER);
1604
49c8c8d2 1605 if (!empty($script)) {
7de13103 1606 debugging('The use of the $script param in print_checkbox has not been migrated into html_writer::checkbox().', DEBUG_DEVELOPER);
49c8c8d2 1607 }
1608
2f0e96e4 1609 $output = html_writer::checkbox($name, $value, $checked, $label);
49c8c8d2 1610
1611 if (empty($return)) {
1612 echo $output;
1613 } else {
1614 return $output;
1615 }
1616
1617}
6a5c71b9 1618
c351150f 1619/**
1620 * Prints the 'update this xxx' button that appears on module pages.
1621 *
1622 * @deprecated since Moodle 2.0
1623 *
1624 * @param string $cmid the course_module id.
1625 * @param string $ignored not used any more. (Used to be courseid.)
1626 * @param string $string the module name - get_string('modulename', 'xxx')
1627 * @return string the HTML for the button, if this user has permission to edit it, else an empty string.
1628 */
1629function update_module_button($cmid, $ignored, $string) {
d1f06fb5 1630 global $CFG, $OUTPUT;
c351150f 1631
bc2e0484 1632 // debugging('update_module_button() has been deprecated. Please change your code to use $OUTPUT->update_module_button().');
c351150f 1633
d1f06fb5 1634 //NOTE: DO NOT call new output method because it needs the module name we do not have here!
1635
b0c6dc1c 1636 if (has_capability('moodle/course:manageactivities', context_module::instance($cmid))) {
d1f06fb5 1637 $string = get_string('updatethis', '', $string);
1638
5c2ed7e2
PS
1639 $url = new moodle_url("$CFG->wwwroot/course/mod.php", array('update' => $cmid, 'return' => true, 'sesskey' => sesskey()));
1640 return $OUTPUT->single_button($url, $string);
d1f06fb5 1641 } else {
1642 return '';
1643 }
c351150f 1644}
1645
7d2a0492 1646/**
1647 * Prints breadcrumb trail of links, called in theme/-/header.html
1648 *
1649 * This function has now been deprecated please use output's navbar method instead
1650 * as shown below
1651 *
1652 * <code php>
1653 * echo $OUTPUT->navbar();
1654 * </code>
1655 *
05226d76
DP
1656 * @deprecated use $OUTPUT->navbar() instead
1657 * @todo final deprecation of this function in MDL-40607
7d2a0492 1658 * @param mixed $navigation deprecated
1659 * @param string $separator OBSOLETE, and now deprecated
1660 * @param boolean $return False to echo the breadcrumb string (default), true to return it.
1661 * @return string|void String or null, depending on $return.
1662 */
1663function print_navigation ($navigation, $separator=0, $return=false) {
1664 global $OUTPUT,$PAGE;
1665
05226d76 1666 debugging('print_navigation() is deprecated, please update use $OUTPUT->navbar() instead.', DEBUG_DEVELOPER);
7d2a0492 1667
1668 $output = $OUTPUT->navbar();
1669
1670 if ($return) {
1671 return $output;
1672 } else {
1673 echo $output;
1674 }
1675}
1676
1677/**
1678 * This function will build the navigation string to be used by print_header
1679 * and others.
1680 *
1681 * It automatically generates the site and course level (if appropriate) links.
1682 *
1683 * If you pass in a $cm object, the method will also generate the activity (e.g. 'Forums')
1684 * and activityinstances (e.g. 'General Developer Forum') navigation levels.
1685 *
1686 * If you want to add any further navigation links after the ones this function generates,
1687 * the pass an array of extra link arrays like this:
1688 * array(
1689 * array('name' => $linktext1, 'link' => $url1, 'type' => $linktype1),
1690 * array('name' => $linktext2, 'link' => $url2, 'type' => $linktype2)
1691 * )
1692 * The normal case is to just add one further link, for example 'Editing forum' after
1693 * 'General Developer Forum', with no link.
1694 * To do that, you need to pass
1695 * array(array('name' => $linktext, 'link' => '', 'type' => 'title'))
1696 * However, becuase this is a very common case, you can use a shortcut syntax, and just
1697 * pass the string 'Editing forum', instead of an array as $extranavlinks.
1698 *
1699 * At the moment, the link types only have limited significance. Type 'activity' is
1700 * recognised in order to implement the $CFG->hideactivitytypenavlink feature. Types
1701 * that are known to appear are 'home', 'course', 'activity', 'activityinstance' and 'title'.
1702 * This really needs to be documented better. In the mean time, try to be consistent, it will
1703 * enable people to customise the navigation more in future.
1704 *
1705 * When passing a $cm object, the fields used are $cm->modname, $cm->name and $cm->course.
1706 * If you get the $cm object using the function get_coursemodule_from_instance or
1707 * get_coursemodule_from_id (as recommended) then this will be done for you automatically.
1708 * If you don't have $cm->modname or $cm->name, this fuction will attempt to find them using
1709 * the $cm->module and $cm->instance fields, but this takes extra database queries, so a
1710 * warning is printed in developer debug mode.
1711 *
05226d76
DP
1712 * @deprecated Please use $PAGE->navabar methods instead.
1713 * @todo final deprecation of this function in MDL-40607
7d2a0492 1714 * @param mixed $extranavlinks - Normally an array of arrays, keys: name, link, type. If you
1715 * only want one extra item with no link, you can pass a string instead. If you don't want
1716 * any extra links, pass an empty string.
1717 * @param mixed $cm deprecated
1718 * @return array Navigation array
1719 */
1720function build_navigation($extranavlinks, $cm = null) {
1721 global $CFG, $COURSE, $DB, $SITE, $PAGE;
1722
05226d76 1723 debugging('build_navigation() is deprecated, please use $PAGE->navbar methods instead.', DEBUG_DEVELOPER);
7d2a0492 1724 if (is_array($extranavlinks) && count($extranavlinks)>0) {
7d2a0492 1725 foreach ($extranavlinks as $nav) {
1726 if (array_key_exists('name', $nav)) {
a4397489 1727 if (array_key_exists('link', $nav) && !empty($nav['link'])) {
1728 $link = $nav['link'];
1729 } else {
1730 $link = null;
1731 }
91152a35 1732 $PAGE->navbar->add($nav['name'],$link);
7d2a0492 1733 }
1734 }
1735 }
6f5e0852 1736
7d2a0492 1737 return(array('newnav' => true, 'navlinks' => array()));
1738}
1739
1740/**
05226d76 1741 * @deprecated not relevant with global navigation in Moodle 2.x+
b9aaae16 1742 * @todo remove completely in MDL-40607
7d2a0492 1743 */
1744function navmenu($course, $cm=NULL, $targetwindow='self') {
7d2a0492 1745 // This function has been deprecated with the creation of the global nav in
1746 // moodle 2.0
05226d76 1747 debugging('navmenu() is deprecated, it is no longer relevant with global navigation.', DEBUG_DEVELOPER);
7d2a0492 1748
1749 return '';
f43cdceb 1750}
76d9df3f
SH
1751
1752/// CALENDAR MANAGEMENT ////////////////////////////////////////////////////////////////
1753
1754
1755/**
1756 * Call this function to add an event to the calendar table and to call any calendar plugins
1757 *
1758 * @param object $event An object representing an event from the calendar table.
1759 * The event will be identified by the id field. The object event should include the following:
1760 * <ul>
1761 * <li><b>$event->name</b> - Name for the event
1762 * <li><b>$event->description</b> - Description of the event (defaults to '')
1763 * <li><b>$event->format</b> - Format for the description (using formatting types defined at the top of weblib.php)
1764 * <li><b>$event->courseid</b> - The id of the course this event belongs to (0 = all courses)
1765 * <li><b>$event->groupid</b> - The id of the group this event belongs to (0 = no group)
1766 * <li><b>$event->userid</b> - The id of the user this event belongs to (0 = no user)
1767 * <li><b>$event->modulename</b> - Name of the module that creates this event
1768 * <li><b>$event->instance</b> - Instance of the module that owns this event
1769 * <li><b>$event->eventtype</b> - The type info together with the module info could
1770 * be used by calendar plugins to decide how to display event
1771 * <li><b>$event->timestart</b>- Timestamp for start of event
1772 * <li><b>$event->timeduration</b> - Duration (defaults to zero)
1773 * <li><b>$event->visible</b> - 0 if the event should be hidden (e.g. because the activity that created it is hidden)
1774 * </ul>
1775 * @return int|false The id number of the resulting record or false if failed
05226d76
DP
1776 * @deprecated please use calendar_event::create() instead.
1777 * @todo final deprecation of this function in MDL-40607
76d9df3f
SH
1778 */
1779 function add_event($event) {
1780 global $CFG;
1781 require_once($CFG->dirroot.'/calendar/lib.php');
05226d76
DP
1782
1783 debugging('add_event() is deprecated, please use calendar_event::create() instead.', DEBUG_DEVELOPER);
76d9df3f
SH
1784 $event = calendar_event::create($event);
1785 if ($event !== false) {
1786 return $event->id;
1787 }
1788 return false;
1789}
1790
1791/**
1792 * Call this function to update an event in the calendar table
1793 * the event will be identified by the id field of the $event object.
1794 *
1795 * @param object $event An object representing an event from the calendar table. The event will be identified by the id field.
1796 * @return bool Success
05226d76 1797 * @deprecated please calendar_event->update() instead.
76d9df3f
SH
1798 */
1799function update_event($event) {
1800 global $CFG;
1801 require_once($CFG->dirroot.'/calendar/lib.php');
05226d76
DP
1802
1803 debugging('update_event() is deprecated, please use calendar_event->update() instead.', DEBUG_DEVELOPER);
76d9df3f
SH
1804 $event = (object)$event;
1805 $calendarevent = calendar_event::load($event->id);
1806 return $calendarevent->update($event);
1807}
1808
1809/**
1810 * Call this function to delete the event with id $id from calendar table.
1811 *
1812 * @param int $id The id of an event from the 'event' table.
1813 * @return bool
05226d76
DP
1814 * @deprecated please use calendar_event->delete() instead.
1815 * @todo final deprecation of this function in MDL-40607
76d9df3f
SH
1816 */
1817function delete_event($id) {
1818 global $CFG;
1819 require_once($CFG->dirroot.'/calendar/lib.php');
05226d76
DP
1820
1821 debugging('delete_event() is deprecated, please use calendar_event->delete() instead.', DEBUG_DEVELOPER);
1822
76d9df3f
SH
1823 $event = calendar_event::load($id);
1824 return $event->delete();
1825}
1826
1827/**
1828 * Call this function to hide an event in the calendar table
1829 * the event will be identified by the id field of the $event object.
78946b9b 1830 *
76d9df3f
SH
1831 * @param object $event An object representing an event from the calendar table. The event will be identified by the id field.
1832 * @return true
05226d76
DP
1833 * @deprecated please use calendar_event->toggle_visibility(false) instead.
1834 * @todo final deprecation of this function in MDL-40607
76d9df3f
SH
1835 */
1836function hide_event($event) {
1837 global $CFG;
1838 require_once($CFG->dirroot.'/calendar/lib.php');
05226d76
DP
1839
1840 debugging('hide_event() is deprecated, please use calendar_event->toggle_visibility(false) instead.', DEBUG_DEVELOPER);
1841
76d9df3f
SH
1842 $event = new calendar_event($event);
1843 return $event->toggle_visibility(false);
1844}
1845
1846/**
1847 * Call this function to unhide an event in the calendar table
1848 * the event will be identified by the id field of the $event object.
1849 *
1850 * @param object $event An object representing an event from the calendar table. The event will be identified by the id field.
1851 * @return true
05226d76
DP
1852 * @deprecated please use calendar_event->toggle_visibility(true) instead.
1853 * @todo final deprecation of this function in MDL-40607
76d9df3f
SH
1854 */
1855function show_event($event) {
1856 global $CFG;
1857 require_once($CFG->dirroot.'/calendar/lib.php');
05226d76
DP
1858
1859 debugging('show_event() is deprecated, please use calendar_event->toggle_visibility(true) instead.', DEBUG_DEVELOPER);
1860
76d9df3f
SH
1861 $event = new calendar_event($event);
1862 return $event->toggle_visibility(true);
0189bf77 1863}
6f3451e5 1864
3fed29a7
PS
1865/**
1866 * Original singleton helper function, please use static methods instead,
2f1e464a 1867 * ex: core_text::convert()
3fed29a7 1868 *
2f1e464a 1869 * @deprecated since Moodle 2.2 use core_text::xxxx() instead
3fed29a7
PS
1870 * @see textlib
1871 * @return textlib instance
1872 */
1873function textlib_get_instance() {
1874
2f1e464a 1875 debugging('textlib_get_instance() is deprecated. Please use static calling core_text::functioname() instead.', DEBUG_DEVELOPER);
3fed29a7
PS
1876
1877 return new textlib();
1878}
1879
ee7084e9
MG
1880/**
1881 * Gets the generic section name for a courses section
1882 *
1883 * The global function is deprecated. Each course format can define their own generic section name
1884 *
1885 * @deprecated since 2.4
1886 * @see get_section_name()
1887 * @see format_base::get_section_name()
1888 *
1889 * @param string $format Course format ID e.g. 'weeks' $course->format
1890 * @param stdClass $section Section object from database
1891 * @return Display name that the course format prefers, e.g. "Week 2"
1892 */
1893function get_generic_section_name($format, stdClass $section) {
1894 debugging('get_generic_section_name() is deprecated. Please use appropriate functionality from class format_base', DEBUG_DEVELOPER);
1895 return get_string('sectionname', "format_$format") . ' ' . $section->section;
1896}
99e9f9a6
MG
1897
1898/**
1899 * Returns an array of sections for the requested course id
1900 *
1901 * It is usually not recommended to display the list of sections used
1902 * in course because the course format may have it's own way to do it.
1903 *
1904 * If you need to just display the name of the section please call:
1905 * get_section_name($course, $section)
1906 * {@link get_section_name()}
1907 * from 2.4 $section may also be just the field course_sections.section
1908 *
1909 * If you need the list of all sections it is more efficient to get this data by calling
b46be6ad 1910 * $modinfo = get_fast_modinfo($courseorid);
99e9f9a6
MG
1911 * $sections = $modinfo->get_section_info_all()
1912 * {@link get_fast_modinfo()}
1913 * {@link course_modinfo::get_section_info_all()}
1914 *
1915 * Information about one section (instance of section_info):
b46be6ad 1916 * get_fast_modinfo($courseorid)->get_sections_info($section)
99e9f9a6
MG
1917 * {@link course_modinfo::get_section_info()}
1918 *
1919 * @deprecated since 2.4
1920 *
1921 * @param int $courseid
1922 * @return array Array of section_info objects
1923 */
1924function get_all_sections($courseid) {
1925 global $DB;
1926 debugging('get_all_sections() is deprecated. See phpdocs for this function', DEBUG_DEVELOPER);
b46be6ad 1927 return get_fast_modinfo($courseid)->get_section_info_all();
99e9f9a6 1928}
722e6ba9
MG
1929
1930/**
1931 * Given a full mod object with section and course already defined, adds this module to that section.
1932 *
1933 * This function is deprecated, please use {@link course_add_cm_to_section()}
1934 * Note that course_add_cm_to_section() also updates field course_modules.section and
1935 * calls rebuild_course_cache()
1936 *
1937 * @deprecated since 2.4
1938 *
1939 * @param object $mod
1940 * @param int $beforemod An existing ID which we will insert the new module before
1941 * @return int The course_sections ID where the mod is inserted
1942 */
44aa854e 1943function add_mod_to_section($mod, $beforemod = null) {
722e6ba9
MG
1944 debugging('Function add_mod_to_section() is deprecated, please use course_add_cm_to_section()', DEBUG_DEVELOPER);
1945 global $DB;
b46be6ad 1946 return course_add_cm_to_section($mod->course, $mod->coursemodule, $mod->section, $beforemod);
722e6ba9 1947}
d57aa283
MG
1948
1949/**
1950 * Returns a number of useful structures for course displays
1951 *
1952 * Function get_all_mods() is deprecated in 2.4
1953 * Instead of:
1954 * <code>
b46be6ad 1955 * get_all_mods($courseid, $mods, $modnames, $modnamesplural, $modnamesused);
d57aa283
MG
1956 * </code>
1957 * please use:
1958 * <code>
b46be6ad 1959 * $mods = get_fast_modinfo($courseorid)->get_cms();
d57aa283
MG
1960 * $modnames = get_module_types_names();
1961 * $modnamesplural = get_module_types_names(true);
b46be6ad 1962 * $modnamesused = get_fast_modinfo($courseorid)->get_used_module_names();
d57aa283
MG
1963 * </code>
1964 *
1965 * @deprecated since 2.4
1966 *
1967 * @param int $courseid id of the course to get info about
1968 * @param array $mods (return) list of course modules
1969 * @param array $modnames (return) list of names of all module types installed and available
1970 * @param array $modnamesplural (return) list of names of all module types installed and available in the plural form
1971 * @param array $modnamesused (return) list of names of all module types used in the course
1972 */
1973function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
1974 debugging('Function get_all_mods() is deprecated. Use get_fast_modinfo() and get_module_types_names() instead. See phpdocs for details', DEBUG_DEVELOPER);
1975
1976 global $COURSE;
1977 $modnames = get_module_types_names();
1978 $modnamesplural= get_module_types_names(true);
b46be6ad 1979 $modinfo = get_fast_modinfo($courseid);
d57aa283
MG
1980 $mods = $modinfo->get_cms();
1981 $modnamesused = $modinfo->get_used_module_names();
1982}
4ede27b2
MG
1983
1984/**
1985 * Returns course section - creates new if does not exist yet
1986 *
1987 * This function is deprecated. To create a course section call:
b46be6ad 1988 * course_create_sections_if_missing($courseorid, $sections);
4ede27b2 1989 * to get the section call:
b46be6ad 1990 * get_fast_modinfo($courseorid)->get_section_info($sectionnum);
4ede27b2
MG
1991 *
1992 * @see course_create_sections_if_missing()
1993 * @see get_fast_modinfo()
1994 * @deprecated since 2.4
1995 *
1996 * @param int $section relative section number (field course_sections.section)
1997 * @param int $courseid
1998 * @return stdClass record from table {course_sections}
1999 */
2000function get_course_section($section, $courseid) {
2001 global $DB;
2002 debugging('Function get_course_section() is deprecated. Please use course_create_sections_if_missing() and get_fast_modinfo() instead.', DEBUG_DEVELOPER);
2003
2004 if ($cw = $DB->get_record("course_sections", array("section"=>$section, "course"=>$courseid))) {
2005 return $cw;
2006 }
2007 $cw = new stdClass();
2008 $cw->course = $courseid;
2009 $cw->section = $section;
2010 $cw->summary = "";
2011 $cw->summaryformat = FORMAT_HTML;
2012 $cw->sequence = "";
2013 $id = $DB->insert_record("course_sections", $cw);
2014 rebuild_course_cache($courseid, true);
2015 return $DB->get_record("course_sections", array("id"=>$id));
2016}
1b2581f4
MG
2017
2018/**
2019 * Return the start and end date of the week in Weekly course format
2020 *
2021 * It is not recommended to use this function outside of format_weeks plugin
2022 *
2023 * @deprecated since 2.4
2024 * @see format_weeks::get_section_dates()
2025 *
2026 * @param stdClass $section The course_section entry from the DB
2027 * @param stdClass $course The course entry from DB
2028 * @return stdClass property start for startdate, property end for enddate
2029 */
2030function format_weeks_get_section_dates($section, $course) {
2031 debugging('Function format_weeks_get_section_dates() is deprecated. It is not recommended to'.
2032 ' use it outside of format_weeks plugin', DEBUG_DEVELOPER);
2033 if (isset($course->format) && $course->format === 'weeks') {
2034 return course_get_format($course)->get_section_dates($section);
2035 }
2036 return null;
2037}
9a36be73
MG
2038
2039/**
2040 * Obtains shared data that is used in print_section when displaying a
2041 * course-module entry.
2042 *
2043 * Deprecated. Instead of:
2044 * list($content, $name) = get_print_section_cm_text($cm, $course);
2045 * use:
2046 * $content = $cm->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
2047 * $name = $cm->get_formatted_name();
2048 *
2049 * @deprecated since 2.5
2050 * @see cm_info::get_formatted_content()
2051 * @see cm_info::get_formatted_name()
2052 *
2053 * This data is also used in other areas of the code.
2054 * @param cm_info $cm Course-module data (must come from get_fast_modinfo)
2055 * @param object $course (argument not used)
2056 * @return array An array with the following values in this order:
2057 * $content (optional extra content for after link),
2058 * $instancename (text of link)
2059 */
2060function get_print_section_cm_text(cm_info $cm, $course) {
2061 debugging('Function get_print_section_cm_text() is deprecated. Please use '.
2062 'cm_info::get_formatted_content() and cm_info::get_formatted_name()',
2063 DEBUG_DEVELOPER);
2064 return array($cm->get_formatted_content(array('overflowdiv' => true, 'noclean' => true)),
2065 $cm->get_formatted_name());
2066}
2067
2068/**
2069 * Prints the menus to add activities and resources.
2070 *
2071 * Deprecated. Please use:
2072 * $courserenderer = $PAGE->get_renderer('core', 'course');
2073 * $output = $courserenderer->course_section_add_cm_control($course, $section, $sectionreturn,
2074 * array('inblock' => $vertical));
2075 * echo $output; // if $return argument in print_section_add_menus() set to false
2076 *
2077 * @deprecated since 2.5
2078 * @see core_course_renderer::course_section_add_cm_control()
2079 *
2080 * @param stdClass $course course object, must be the same as set on the page
2081 * @param int $section relative section number (field course_sections.section)
2082 * @param null|array $modnames (argument ignored) get_module_types_names() is used instead of argument
2083 * @param bool $vertical Vertical orientation
2084 * @param bool $return Return the menus or send them to output
2085 * @param int $sectionreturn The section to link back to
2086 * @return void|string depending on $return
2087 */
2088function print_section_add_menus($course, $section, $modnames = null, $vertical=false, $return=false, $sectionreturn=null) {
2089 global $PAGE;
2090 debugging('Function print_section_add_menus() is deprecated. Please use course renderer '.
2091 'function course_section_add_cm_control()', DEBUG_DEVELOPER);
2092 $output = '';
2093 $courserenderer = $PAGE->get_renderer('core', 'course');
2094 $output = $courserenderer->course_section_add_cm_control($course, $section, $sectionreturn,
2095 array('inblock' => $vertical));
2096 if ($return) {
2097 return $output;
2098 } else {
2099 echo $output;
2100 return !empty($output);
2101 }
2102}
2103
2104/**
2105 * Produces the editing buttons for a module
2106 *
2107 * Deprecated. Please use:
2108 * $courserenderer = $PAGE->get_renderer('core', 'course');
2109 * $actions = course_get_cm_edit_actions($mod, $indent, $section);
2110 * return ' ' . $courserenderer->course_section_cm_edit_actions($actions);
2111 *
2112 * @deprecated since 2.5
2113 * @see course_get_cm_edit_actions()
2114 * @see core_course_renderer->course_section_cm_edit_actions()
2115 *
2116 * @param stdClass $mod The module to produce editing buttons for
2117 * @param bool $absolute_ignored (argument ignored) - all links are absolute
2118 * @param bool $moveselect (argument ignored)
2119 * @param int $indent The current indenting
2120 * @param int $section The section to link back to
2121 * @return string XHTML for the editing buttons
2122 */
2123function make_editing_buttons(stdClass $mod, $absolute_ignored = true, $moveselect = true, $indent=-1, $section=null) {
2124 global $PAGE;
2125 debugging('Function make_editing_buttons() is deprecated, please see PHPdocs in '.
2126 'lib/deprecatedlib.php on how to replace it', DEBUG_DEVELOPER);
2127 if (!($mod instanceof cm_info)) {
2128 $modinfo = get_fast_modinfo($mod->course);
2129 $mod = $modinfo->get_cm($mod->id);
2130 }
2131 $actions = course_get_cm_edit_actions($mod, $indent, $section);
2132
2133 $courserenderer = $PAGE->get_renderer('core', 'course');
2134 // The space added before the <span> is a ugly hack but required to set the CSS property white-space: nowrap
2135 // and having it to work without attaching the preceding text along with it. Hopefully the refactoring of
2136 // the course page HTML will allow this to be removed.
2137 return ' ' . $courserenderer->course_section_cm_edit_actions($actions);
2138}
2139
2140/**
2141 * Prints a section full of activity modules
2142 *
2143 * Deprecated. Please use:
2144 * $courserenderer = $PAGE->get_renderer('core', 'course');
2145 * echo $courserenderer->course_section_cm_list($course, $section, $sectionreturn,
2146 * array('hidecompletion' => $hidecompletion));
2147 *
2148 * @deprecated since 2.5
2149 * @see core_course_renderer::course_section_cm_list()
2150 *
2151 * @param stdClass $course The course
2152 * @param stdClass|section_info $section The section object containing properties id and section
2153 * @param array $mods (argument not used)
2154 * @param array $modnamesused (argument not used)
2155 * @param bool $absolute (argument not used)
2156 * @param string $width (argument not used)
2157 * @param bool $hidecompletion Hide completion status
2158 * @param int $sectionreturn The section to return to
2159 * @return void
2160 */
2161function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%", $hidecompletion=false, $sectionreturn=null) {
2162 global $PAGE;
2163 debugging('Function print_section() is deprecated. Please use course renderer function '.
2164 'course_section_cm_list() instead.', DEBUG_DEVELOPER);
2165 $displayoptions = array('hidecompletion' => $hidecompletion);
2166 $courserenderer = $PAGE->get_renderer('core', 'course');
2167 echo $courserenderer->course_section_cm_list($course, $section, $sectionreturn, $displayoptions);
2168}
00ba185d 2169
ff233851
MG
2170/**
2171 * Displays the list of courses with user notes
2172 *
2173 * This function is not used in core. It was replaced by block course_overview
2174 *
2175 * @deprecated since 2.5
2176 *
2177 * @param array $courses
2178 * @param array $remote_courses
2179 */
2180function print_overview($courses, array $remote_courses=array()) {
2181 global $CFG, $USER, $DB, $OUTPUT;
2182 debugging('Function print_overview() is deprecated. Use block course_overview to display this information', DEBUG_DEVELOPER);
2183
2184 $htmlarray = array();
2185 if ($modules = $DB->get_records('modules')) {
2186 foreach ($modules as $mod) {
2187 if (file_exists(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php')) {
2188 include_once(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php');
2189 $fname = $mod->name.'_print_overview';
2190 if (function_exists($fname)) {
2191 $fname($courses,$htmlarray);
2192 }
2193 }
2194 }
2195 }
2196 foreach ($courses as $course) {
2197 $fullname = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
2198 echo $OUTPUT->box_start('coursebox');
2199 $attributes = array('title' => s($fullname));
2200 if (empty($course->visible)) {
2201 $attributes['class'] = 'dimmed';
2202 }
2203 echo $OUTPUT->heading(html_writer::link(
2204 new moodle_url('/course/view.php', array('id' => $course->id)), $fullname, $attributes), 3);
2205 if (array_key_exists($course->id,$htmlarray)) {
2206 foreach ($htmlarray[$course->id] as $modname => $html) {
2207 echo $html;
2208 }
2209 }
2210 echo $OUTPUT->box_end();
2211 }
2212
2213 if (!empty($remote_courses)) {
2214 echo $OUTPUT->heading(get_string('remotecourses', 'mnet'));
2215 }
2216 foreach ($remote_courses as $course) {
2217 echo $OUTPUT->box_start('coursebox');
2218 $attributes = array('title' => s($course->fullname));
2219 echo $OUTPUT->heading(html_writer::link(
2220 new moodle_url('/auth/mnet/jump.php', array('hostid' => $course->hostid, 'wantsurl' => '/course/view.php?id='.$course->remoteid)),
2221 format_string($course->shortname),
2222 $attributes) . ' (' . format_string($course->hostname) . ')', 3);
2223 echo $OUTPUT->box_end();
2224 }
2225}
a3f66bde
MG
2226
2227/**
2228 * This function trawls through the logs looking for
2229 * anything new since the user's last login
2230 *
2231 * This function was only used to print the content of block recent_activity
2232 * All functionality is moved into class {@link block_recent_activity}
2233 * and renderer {@link block_recent_activity_renderer}
2234 *
2235 * @deprecated since 2.5
2236 * @param stdClass $course
2237 */
2238function print_recent_activity($course) {
2239 // $course is an object
2240 global $CFG, $USER, $SESSION, $DB, $OUTPUT;
2241 debugging('Function print_recent_activity() is deprecated. It is not recommended to'.
2242 ' use it outside of block_recent_activity', DEBUG_DEVELOPER);
2243
2244 $context = context_course::instance($course->id);
2245
2246 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
2247
2248 $timestart = round(time() - COURSE_MAX_RECENT_PERIOD, -2); // better db caching for guests - 100 seconds
2249
2250 if (!isguestuser()) {
2251 if (!empty($USER->lastcourseaccess[$course->id])) {
2252 if ($USER->lastcourseaccess[$course->id] > $timestart) {
2253 $timestart = $USER->lastcourseaccess[$course->id];
2254 }
2255 }
2256 }
2257
2258 echo '<div class="activitydate">';
2259 echo get_string('activitysince', '', userdate($timestart));
2260 echo '</div>';
2261 echo '<div class="activityhead">';
2262
2263 echo '<a href="'.$CFG->wwwroot.'/course/recent.php?id='.$course->id.'">'.get_string('recentactivityreport').'</a>';
2264
2265 echo "</div>\n";
2266
2267 $content = false;
2268
2269/// Firstly, have there been any new enrolments?
2270
2271 $users = get_recent_enrolments($course->id, $timestart);
2272
2273 //Accessibility: new users now appear in an <OL> list.
2274 if ($users) {
2275 echo '<div class="newusers">';
2276 echo $OUTPUT->heading(get_string("newusers").':', 3);
2277 $content = true;
2278 echo "<ol class=\"list\">\n";
2279 foreach ($users as $user) {
2280 $fullname = fullname($user, $viewfullnames);
2281 echo '<li class="name"><a href="'."$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a></li>\n";
2282 }
2283 echo "</ol>\n</div>\n";
2284 }
2285
2286/// Next, have there been any modifications to the course structure?
2287
2288 $modinfo = get_fast_modinfo($course);
2289
2290 $changelist = array();
2291
2292 $logs = $DB->get_records_select('log', "time > ? AND course = ? AND
2293 module = 'course' AND
2294 (action = 'add mod' OR action = 'update mod' OR action = 'delete mod')",
2295 array($timestart, $course->id), "id ASC");
2296
2297 if ($logs) {
2298 $actions = array('add mod', 'update mod', 'delete mod');
2299 $newgones = array(); // added and later deleted items
2300 foreach ($logs as $key => $log) {
2301 if (!in_array($log->action, $actions)) {
2302 continue;
2303 }
2304 $info = explode(' ', $log->info);
2305
2306 // note: in most cases I replaced hardcoding of label with use of
2307 // $cm->has_view() but it was not possible to do this here because
2308 // we don't necessarily have the $cm for it
2309 if ($info[0] == 'label') { // Labels are ignored in recent activity
2310 continue;
2311 }
2312
2313 if (count($info) != 2) {
2314 debugging("Incorrect log entry info: id = ".$log->id, DEBUG_DEVELOPER);
2315 continue;
2316 }
2317
2318 $modname = $info[0];
2319 $instanceid = $info[1];
2320
2321 if ($log->action == 'delete mod') {
2322 // unfortunately we do not know if the mod was visible
2323 if (!array_key_exists($log->info, $newgones)) {
2324 $strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $modname));
2325 $changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted);
2326 }
2327 } else {
2328 if (!isset($modinfo->instances[$modname][$instanceid])) {
2329 if ($log->action == 'add mod') {
2330 // do not display added and later deleted activities
2331 $newgones[$log->info] = true;
2332 }
2333 continue;
2334 }
2335 $cm = $modinfo->instances[$modname][$instanceid];
2336 if (!$cm->uservisible) {
2337 continue;
2338 }
2339
2340 if ($log->action == 'add mod') {
2341 $stradded = get_string('added', 'moodle', get_string('modulename', $modname));
2342 $changelist[$log->info] = array('operation' => 'add', 'text' => "$stradded:<br /><a href=\"$CFG->wwwroot/mod/$cm->modname/view.php?id={$cm->id}\">".format_string($cm->name, true)."</a>");
2343
2344 } else if ($log->action == 'update mod' and empty($changelist[$log->info])) {
2345 $strupdated = get_string('updated', 'moodle', get_string('modulename', $modname));
2346 $changelist[$log->info] = array('operation' => 'update', 'text' => "$strupdated:<br /><a href=\"$CFG->wwwroot/mod/$cm->modname/view.php?id={$cm->id}\">".format_string($cm->name, true)."</a>");
2347 }
2348 }
2349 }
2350 }
2351
2352 if (!empty($changelist)) {
2353 echo $OUTPUT->heading(get_string("courseupdates").':', 3);
2354 $content = true;
2355 foreach ($changelist as $changeinfo => $change) {
2356 echo '<p class="activity">'.$change['text'].'</p>';
2357 }
2358 }
2359
2360/// Now display new things from each module
2361
2362 $usedmodules = array();
2363 foreach($modinfo->cms as $cm) {
2364 if (isset($usedmodules[$cm->modname])) {
2365 continue;
2366 }
2367 if (!$cm->uservisible) {
2368 continue;
2369 }
2370 $usedmodules[$cm->modname] = $cm->modname;
2371 }
2372
2373 foreach ($usedmodules as $modname) { // Each module gets it's own logs and prints them
2374 if (file_exists($CFG->dirroot.'/mod/'.$modname.'/lib.php')) {
2375 include_once($CFG->dirroot.'/mod/'.$modname.'/lib.php');
2376 $print_recent_activity = $modname.'_print_recent_activity';
2377 if (function_exists($print_recent_activity)) {
2378 // NOTE: original $isteacher (second parameter below) was replaced with $viewfullnames!
2379 $content = $print_recent_activity($course, $viewfullnames, $timestart) || $content;
2380 }
2381 } else {
2382 debugging("Missing lib.php in lib/{$modname} - please reinstall files or uninstall the module");
2383 }
2384 }
2385
2386 if (! $content) {
2387 echo '<p class="message">'.get_string('nothingnew').'</p>';
2388 }
2389}
a347aee3
MN
2390
2391/**
2392 * Delete a course module and any associated data at the course level (events)
2393 * Until 1.5 this function simply marked a deleted flag ... now it
2394 * deletes it completely.
2395 *
2396 * @deprecated since 2.5
2397 *
2398 * @param int $id the course module id
2399 * @return boolean true on success, false on failure
2400 */
2401function delete_course_module($id) {
2402 debugging('Function delete_course_module() is deprecated. Please use course_delete_module() instead.', DEBUG_DEVELOPER);
2403
2404 global $CFG, $DB;
2405
2406 require_once($CFG->libdir.'/gradelib.php');
2407 require_once($CFG->dirroot.'/blog/lib.php');
2408
2409 if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
2410 return true;
2411 }
2412 $modulename = $DB->get_field('modules', 'name', array('id'=>$cm->module));
2413 //delete events from calendar
2414 if ($events = $DB->get_records('event', array('instance'=>$cm->instance, 'modulename'=>$modulename))) {
2415 foreach($events as $event) {
2416 delete_event($event->id);
2417 }
2418 }
2419 //delete grade items, outcome items and grades attached to modules
2420 if ($grade_items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,
2421 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course))) {
2422 foreach ($grade_items as $grade_item) {
2423 $grade_item->delete('moddelete');
2424 }
2425 }
2426 // Delete completion and availability data; it is better to do this even if the
2427 // features are not turned on, in case they were turned on previously (these will be
2428 // very quick on an empty table)
2429 $DB->delete_records('course_modules_completion', array('coursemoduleid' => $cm->id));
a347aee3
MN
2430 $DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id,
2431 'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY));
2432
2433 delete_context(CONTEXT_MODULE, $cm->id);
2434 return $DB->delete_records('course_modules', array('id'=>$cm->id));
2435}
2c49fb4c
MG
2436
2437/**
2438 * Prints the turn editing on/off button on course/index.php or course/category.php.
2439 *
2440 * @deprecated since 2.5
2441 *
2442 * @param integer $categoryid The id of the category we are showing, or 0 for system context.
2443 * @return string HTML of the editing button, or empty string, if this user is not allowed
2444 * to see it.
2445 */
2446function update_category_button($categoryid = 0) {
2447 global $CFG, $PAGE, $OUTPUT;
2448 debugging('Function update_category_button() is deprecated. Pages to view '.
2449 'and edit courses are now separate and no longer depend on editing mode.',
2450 DEBUG_DEVELOPER);
2451
2452 // Check permissions.
2453 if (!can_edit_in_category($categoryid)) {
2454 return '';
2455 }
2456
2457 // Work out the appropriate action.
2458 if ($PAGE->user_is_editing()) {
2459 $label = get_string('turneditingoff');
2460 $edit = 'off';
2461 } else {
2462 $label = get_string('turneditingon');
2463 $edit = 'on';
2464 }
2465
2466 // Generate the button HTML.
2467 $options = array('categoryedit' => $edit, 'sesskey' => sesskey());
2468 if ($categoryid) {
2469 $options['id'] = $categoryid;
2470 $page = 'category.php';
2471 } else {
2472 $page = 'index.php';
2473 }
2474 return $OUTPUT->single_button(new moodle_url('/course/' . $page, $options), $label, 'get');
2475}
4e0b6025
MG
2476
2477/**
2478 * This function recursively travels the categories, building up a nice list
2479 * for display. It also makes an array that list all the parents for each
2480 * category.
2481 *
2482 * For example, if you have a tree of categories like:
2483 * Miscellaneous (id = 1)
2484 * Subcategory (id = 2)
2485 * Sub-subcategory (id = 4)
2486 * Other category (id = 3)
2487 * Then after calling this function you will have
2488 * $list = array(1 => 'Miscellaneous', 2 => 'Miscellaneous / Subcategory',
2489 * 4 => 'Miscellaneous / Subcategory / Sub-subcategory',
2490 * 3 => 'Other category');
2491 * $parents = array(2 => array(1), 4 => array(1, 2));
2492 *
2493 * If you specify $requiredcapability, then only categories where the current
2494 * user has that capability will be added to $list, although all categories
2495 * will still be added to $parents, and if you only have $requiredcapability
2496 * in a child category, not the parent, then the child catgegory will still be
2497 * included.
2498 *
2499 * If you specify the option $excluded, then that category, and all its children,
2500 * are omitted from the tree. This is useful when you are doing something like
2501 * moving categories, where you do not want to allow people to move a category
2502 * to be the child of itself.
2503 *
2504 * This function is deprecated! For list of categories use
2505 * coursecat::make_all_categories($requiredcapability, $excludeid, $separator)
2506 * For parents of one particular category use
2507 * coursecat::get($id)->get_parents()
2508 *
2509 * @deprecated since 2.5
2510 *
2511 * @param array $list For output, accumulates an array categoryid => full category path name
2512 * @param array $parents For output, accumulates an array categoryid => list of parent category ids.
2513 * @param string/array $requiredcapability if given, only categories where the current
2514 * user has this capability will be added to $list. Can also be an array of capabilities,
2515 * in which case they are all required.
2516 * @param integer $excludeid Omit this category and its children from the lists built.
2517 * @param object $category Not used
2518 * @param string $path Not used
2519 */
2520function make_categories_list(&$list, &$parents, $requiredcapability = '',
2521 $excludeid = 0, $category = NULL, $path = "") {
2522 global $CFG, $DB;
2523 require_once($CFG->libdir.'/coursecatlib.php');
2524
2525 debugging('Global function make_categories_list() is deprecated. Please use '.
2526 'coursecat::make_categories_list() and coursecat::get_parents()',
2527 DEBUG_DEVELOPER);
2528
2529 // For categories list use just this one function:
2530 if (empty($list)) {
2531 $list = array();
2532 }
2533 $list += coursecat::make_categories_list($requiredcapability, $excludeid);
2534
2535 // Building the list of all parents of all categories in the system is highly undesirable and hardly ever needed.
2536 // Usually user needs only parents for one particular category, in which case should be used:
2537 // coursecat::get($categoryid)->get_parents()
2538 if (empty($parents)) {
2539 $parents = array();
2540 }
2541 $all = $DB->get_records_sql('SELECT id, parent FROM {course_categories} ORDER BY sortorder');
2542 foreach ($all as $record) {
2543 if ($record->parent) {
2544 $parents[$record->id] = array_merge($parents[$record->parent], array($record->parent));
2545 } else {
2546 $parents[$record->id] = array();
2547 }
2548 }
2549}
deb65ced
MG
2550
2551/**
2552 * Delete category, but move contents to another category.
2553 *
2554 * This function is deprecated. Please use
2555 * coursecat::get($category->id)->delete_move($newparentid, $showfeedback);
2556 *
2557 * @see coursecat::delete_move()
2558 * @deprecated since 2.5
2559 *
2560 * @param object $category
2561 * @param int $newparentid category id
2562 * @return bool status
2563 */
2564function category_delete_move($category, $newparentid, $showfeedback=true) {
2565 global $CFG;
2566 require_once($CFG->libdir.'/coursecatlib.php');
2567
2568 debugging('Function category_delete_move() is deprecated. Please use coursecat::delete_move() instead.');
2569
2570 return coursecat::get($category->id)->delete_move($newparentid, $showfeedback);
2571}
2572
2573/**
2574 * Recursively delete category including all subcategories and courses.
2575 *
2576 * This function is deprecated. Please use
2577 * coursecat::get($category->id)->delete_full($showfeedback);
2578 *
2579 * @see coursecat::delete_full()
2580 * @deprecated since 2.5
2581 *
2582 * @param stdClass $category
2583 * @param boolean $showfeedback display some notices
2584 * @return array return deleted courses
2585 */
2586function category_delete_full($category, $showfeedback=true) {
2587 global $CFG, $DB;
2588 require_once($CFG->libdir.'/coursecatlib.php');
2589
2590 debugging('Function category_delete_full() is deprecated. Please use coursecat::delete_full() instead.');
2591
2592 return coursecat::get($category->id)->delete_full($showfeedback);
2593}
6e1d1ee0
MG
2594
2595/**
2596 * Efficiently moves a category - NOTE that this can have
2597 * a huge impact access-control-wise...
2598 *
2599 * This function is deprecated. Please use
2600 * $coursecat = coursecat::get($category->id);
2601 * if ($coursecat->can_change_parent($newparentcat->id)) {
2602 * $coursecat->change_parent($newparentcat->id);
2603 * }
2604 *
2605 * Alternatively you can use
2606 * $coursecat->update(array('parent' => $newparentcat->id));
2607 *
2608 * Function update() also updates field course_categories.timemodified
2609 *
2610 * @see coursecat::change_parent()
2611 * @see coursecat::update()
2612 * @deprecated since 2.5
2613 *
2614 * @param stdClass|coursecat $category
2615 * @param stdClass|coursecat $newparentcat
2616 */
2617function move_category($category, $newparentcat) {
2618 global $CFG;
2619 require_once($CFG->libdir.'/coursecatlib.php');
2620
2621 debugging('Function move_category() is deprecated. Please use coursecat::change_parent() instead.');
2622
2623 return coursecat::get($category->id)->change_parent($newparentcat->id);
2624}
2625
2626/**
2627 * Hide course category and child course and subcategories
2628 *
2629 * This function is deprecated. Please use
2630 * coursecat::get($category->id)->hide();
2631 *
2632 * @see coursecat::hide()
2633 * @deprecated since 2.5
2634 *
2635 * @param stdClass $category
2636 * @return void
2637 */
2638function course_category_hide($category) {
2639 global $CFG;
2640 require_once($CFG->libdir.'/coursecatlib.php');
2641
2642 debugging('Function course_category_hide() is deprecated. Please use coursecat::hide() instead.');
2643
2644 coursecat::get($category->id)->hide();
2645}
2646
2647/**
2648 * Show course category and child course and subcategories
2649 *
2650 * This function is deprecated. Please use
2651 * coursecat::get($category->id)->show();
2652 *
2653 * @see coursecat::show()
2654 * @deprecated since 2.5
2655 *
2656 * @param stdClass $category
2657 * @return void
2658 */
2659function course_category_show($category) {
2660 global $CFG;
2661 require_once($CFG->libdir.'/coursecatlib.php');
2662
2663 debugging('Function course_category_show() is deprecated. Please use coursecat::show() instead.');
2664
2665 coursecat::get($category->id)->show();
2666}
2d8a275b
MG
2667
2668/**
2669 * Return specified category, default if given does not exist
2670 *
2671 * This function is deprecated.
2672 * To get the category with the specified it please use:
2673 * coursecat::get($catid, IGNORE_MISSING);
2674 * or
2675 * coursecat::get($catid, MUST_EXIST);
2676 *
2677 * To get the first available category please use
2678 * coursecat::get_default();
2679 *
2680 * class coursecat will also make sure that at least one category exists in DB
2681 *
2682 * @deprecated since 2.5
2683 * @see coursecat::get()
2684 * @see coursecat::get_default()
2685 *
2686 * @param int $catid course category id
2687 * @return object caregory
2688 */
2689function get_course_category($catid=0) {
2690 global $DB;
2691
2692 debugging('Function get_course_category() is deprecated. Please use coursecat::get(), see phpdocs for more details');
2693
2694 $category = false;
2695
2696 if (!empty($catid)) {
2697 $category = $DB->get_record('course_categories', array('id'=>$catid));
2698 }
2699
2700 if (!$category) {
2701 // the first category is considered default for now
2702 if ($category = $DB->get_records('course_categories', null, 'sortorder', '*', 0, 1)) {
2703 $category = reset($category);
2704
2705 } else {
2706 $cat = new stdClass();
2707 $cat->name = get_string('miscellaneous');
2708 $cat->depth = 1;
2709 $cat->sortorder = MAX_COURSES_IN_CATEGORY;
2710 $cat->timemodified = time();
2711 $catid = $DB->insert_record('course_categories', $cat);
2712 // make sure category context exists
2713 context_coursecat::instance($catid);
2714 mark_context_dirty('/'.SYSCONTEXTID);
2715 fix_course_sortorder(); // Required to build course_categories.depth and .path.
2716 $category = $DB->get_record('course_categories', array('id'=>$catid));
2717 }
2718 }
2719
2720 return $category;
2721}
9bad61db
MG
2722
2723/**
2724 * Create a new course category and marks the context as dirty
2725 *
2726 * This function does not set the sortorder for the new category and
2727 * {@link fix_course_sortorder()} should be called after creating a new course
2728 * category
2729 *
2730 * Please note that this function does not verify access control.
2731 *
2732 * This function is deprecated. It is replaced with the method create() in class coursecat.
2733 * {@link coursecat::create()} also verifies the data, fixes sortorder and logs the action
2734 *
2735 * @deprecated since 2.5
2736 *
2737 * @param object $category All of the data required for an entry in the course_categories table
2738 * @return object new course category
2739 */
2740function create_course_category($category) {
2741 global $DB;
2742
2743 debugging('Function create_course_category() is deprecated. Please use coursecat::create(), see phpdocs for more details', DEBUG_DEVELOPER);
2744
2745 $category->timemodified = time();
2746 $category->id = $DB->insert_record('course_categories', $category);
2747 $category = $DB->get_record('course_categories', array('id' => $category->id));
2748
2749 // We should mark the context as dirty
2750 $category->context = context_coursecat::instance($category->id);
2751 $category->context->mark_dirty();
2752
2753 return $category;
2754}
bc81b006
MG
2755
2756/**
2757 * Returns an array of category ids of all the subcategories for a given
2758 * category.
2759 *
2760 * This function is deprecated.
2761 *
2762 * To get visible children categories of the given category use:
2763 * coursecat::get($categoryid)->get_children();
2764 * This function will return the array or coursecat objects, on each of them
2765 * you can call get_children() again
2766 *
2767 * @see coursecat::get()
2768 * @see coursecat::get_children()
2769 *
2770 * @deprecated since 2.5
2771 *
2772 * @global object
2773 * @param int $catid - The id of the category whose subcategories we want to find.
2774 * @return array of category ids.
2775 */
2776function get_all_subcategories($catid) {
2777 global $DB;
2778
2779 debugging('Function get_all_subcategories() is deprecated. Please use appropriate methods() of coursecat class. See phpdocs for more details',
2780 DEBUG_DEVELOPER);
2781
2782 $subcats = array();
2783
2784 if ($categories = $DB->get_records('course_categories', array('parent' => $catid))) {
2785 foreach ($categories as $cat) {
2786 array_push($subcats, $cat->id);
2787 $subcats = array_merge($subcats, get_all_subcategories($cat->id));
2788 }
2789 }
2790 return $subcats;
2791}
8db5dcb7
MG
2792
2793/**
2794 * Gets the child categories of a given courses category
2795 *
2796 * This function is deprecated. Please use functions in class coursecat:
2797 * - coursecat::get($parentid)->has_children()
2798 * tells if the category has children (visible or not to the current user)
2799 *
2800 * - coursecat::get($parentid)->get_children()
2801 * returns an array of coursecat objects, each of them represents a children category visible
2802 * to the current user (i.e. visible=1 or user has capability to view hidden categories)
2803 *
2804 * - coursecat::get($parentid)->get_children_count()
2805 * returns number of children categories visible to the current user
2806 *
2807 * - coursecat::count_all()
2808 * returns total count of all categories in the system (both visible and not)
2809 *
2810 * - coursecat::get_default()
2811 * returns the first category (usually to be used if count_all() == 1)
2812 *
2813 * @deprecated since 2.5
2814 *
2815 * @param int $parentid the id of a course category.
2816 * @return array all the child course categories.
2817 */
2818function get_child_categories($parentid) {
2819 global $DB;
2820 debugging('Function get_child_categories() is deprecated. Use coursecat::get_children() or see phpdocs for more details.',
2821 DEBUG_DEVELOPER);
2822
2823 $rv = array();
2824 $sql = context_helper::get_preload_record_columns_sql('ctx');
2825 $records = $DB->get_records_sql("SELECT c.*, $sql FROM {course_categories} c ".
2826 "JOIN {context} ctx on ctx.instanceid = c.id AND ctx.contextlevel = ? WHERE c.parent = ? ORDER BY c.sortorder",
2827 array(CONTEXT_COURSECAT, $parentid));
2828 foreach ($records as $category) {
2829 context_helper::preload_from_record($category);
2830 if (!$category->visible && !has_capability('moodle/category:viewhiddencategories', context_coursecat::instance($category->id))) {
2831 continue;
2832 }
2833 $rv[] = $category;
2834 }
2835 return $rv;
2836}
e1d54562
MG
2837
2838/**
2839 * Returns a sorted list of categories.
2840 *
2841 * When asking for $parent='none' it will return all the categories, regardless
2842 * of depth. Wheen asking for a specific parent, the default is to return
2843 * a "shallow" resultset. Pass false to $shallow and it will return all
2844 * the child categories as well.
2845 *
2846 * @deprecated since 2.5
2847 *
2848 * This function is deprecated. Use appropriate functions from class coursecat.
2849 * Examples:
2850 *
2851 * coursecat::get($categoryid)->get_children()
2852 * - returns all children of the specified category as instances of class
0198b4a5
MG
2853 * coursecat, which means on each of them method get_children() can be called again.
2854 * Only categories visible to the current user are returned.
e1d54562 2855 *
0198b4a5
MG
2856 * coursecat::get(0)->get_children()
2857 * - returns all top-level categories visible to the current user.
e1d54562
MG
2858 *
2859 * Sort fields can be specified, see phpdocs to {@link coursecat::get_children()}
2860 *
0198b4a5
MG
2861 * coursecat::make_categories_list()
2862 * - returns an array of all categories id/names in the system.
2863 * Also only returns categories visible to current user and can additionally be
2864 * filetered by capability, see phpdocs to {@link coursecat::make_categories_list()}
2865 *
2866 * make_categories_options()
2867 * - Returns full course categories tree to be used in html_writer::select()
2868 *
e1d54562
MG
2869 * Also see functions {@link coursecat::get_children_count()}, {@link coursecat::count_all()},
2870 * {@link coursecat::get_default()}
2871 *
2872 * The code of this deprecated function is left as it is because coursecat::get_children()
0198b4a5
MG
2873 * returns categories as instances of coursecat and not stdClass. Also there is no
2874 * substitute for retrieving the category with all it's subcategories. Plugin developers
2875 * may re-use the code/queries from this function in their plugins if really necessary.
e1d54562
MG
2876 *
2877 * @param string $parent The parent category if any
2878 * @param string $sort the sortorder
2879 * @param bool $shallow - set to false to get the children too
2880 * @return array of categories
2881 */
2882function get_categories($parent='none', $sort=NULL, $shallow=true) {
2883 global $DB;
2884
0198b4a5 2885 debugging('Function get_categories() is deprecated. Please use coursecat::get_children() or see phpdocs for other alternatives',
e1d54562
MG
2886 DEBUG_DEVELOPER);
2887
2888 if ($sort === NULL) {
2889 $sort = 'ORDER BY cc.sortorder ASC';
2890 } elseif ($sort ==='') {
2891 // leave it as empty
2892 } else {
2893 $sort = "ORDER BY $sort";
2894 }
2895
2896 list($ccselect, $ccjoin) = context_instance_preload_sql('cc.id', CONTEXT_COURSECAT, 'ctx');
2897
2898 if ($parent === 'none') {
2899 $sql = "SELECT cc.* $ccselect
2900 FROM {course_categories} cc
2901 $ccjoin
2902 $sort";
2903 $params = array();
2904
2905 } elseif ($shallow) {
2906 $sql = "SELECT cc.* $ccselect
2907 FROM {course_categories} cc
2908 $ccjoin
2909 WHERE cc.parent=?
2910 $sort";
2911 $params = array($parent);
2912
2913 } else {
2914 $sql = "SELECT cc.* $ccselect
2915 FROM {course_categories} cc
2916 $ccjoin
2917 JOIN {course_categories} ccp
2918 ON ((cc.parent = ccp.id) OR (cc.path LIKE ".$DB->sql_concat('ccp.path',"'/%'")."))
2919 WHERE ccp.id=?
2920 $sort";
2921 $params = array($parent);
2922 }
2923 $categories = array();
2924
2925 $rs = $DB->get_recordset_sql($sql, $params);
2926 foreach($rs as $cat) {
db314f34 2927 context_helper::preload_from_record($cat);
e1d54562
MG
2928 $catcontext = context_coursecat::instance($cat->id);
2929 if ($cat->visible || has_capability('moodle/category:viewhiddencategories', $catcontext)) {
2930 $categories[$cat->id] = $cat;
2931 }
2932 }
2933 $rs->close();
2934 return $categories;
2935}
a8d683ca
MG
2936
2937/**
2938* Displays a course search form
2939*
2940* This function is deprecated, please use course renderer:
2941* $renderer = $PAGE->get_renderer('core', 'course');
2942* echo $renderer->course_search_form($value, $format);
2943*
2944* @deprecated since 2.5
2945*
2946* @param string $value default value to populate the search field
2947* @param bool $return if true returns the value, if false - outputs
2948* @param string $format display format - 'plain' (default), 'short' or 'navbar'
2949* @return null|string
2950*/
2951function print_course_search($value="", $return=false, $format="plain") {
2952 global $PAGE;
2953 debugging('Function print_course_search() is deprecated, please use course renderer', DEBUG_DEVELOPER);
2954 $renderer = $PAGE->get_renderer('core', 'course');
2955 if ($return) {
2956 return $renderer->course_search_form($value, $format);
2957 } else {
2958 echo $renderer->course_search_form($value, $format);
2959 }
2960}
09ae7ee0
MG
2961
2962/**
2963 * Prints custom user information on the home page
2964 *
2965 * This function is deprecated, please use:
2966 * $renderer = $PAGE->get_renderer('core', 'course');
2967 * echo $renderer->frontpage_my_courses()
2968 *
2969 * @deprecated since 2.5
2970 */
2971function print_my_moodle() {
2972 global $PAGE;
2973 debugging('Function print_my_moodle() is deprecated, please use course renderer function frontpage_my_courses()', DEBUG_DEVELOPER);
2974
2975 $renderer = $PAGE->get_renderer('core', 'course');
2976 echo $renderer->frontpage_my_courses();
2977}
2978
2979/**
2980 * Prints information about one remote course
2981 *
2982 * This function is deprecated, it is replaced with protected function
2983 * {@link core_course_renderer::frontpage_remote_course()}
2984 * It is only used from function {@link core_course_renderer::frontpage_my_courses()}
2985 *
2986 * @deprecated since 2.5
2987 */
2988function print_remote_course($course, $width="100%") {
2989 global $CFG, $USER;
2990 debugging('Function print_remote_course() is deprecated, please use course renderer', DEBUG_DEVELOPER);
2991
2992 $linkcss = '';
2993
2994 $url = "{$CFG->wwwroot}/auth/mnet/jump.php?hostid={$course->hostid}&amp;wantsurl=/course/view.php?id={$course->remoteid}";
2995
2996 echo '<div class="coursebox remotecoursebox clearfix">';
2997 echo '<div class="info">';
2998 echo '<div class="name"><a title="'.get_string('entercourse').'"'.
2999 $linkcss.' href="'.$url.'">'
3000 . format_string($course->fullname) .'</a><br />'
3001 . format_string($course->hostname) . ' : '
3002 . format_string($course->cat_name) . ' : '
3003 . format_string($course->shortname). '</div>';
3004 echo '</div><div class="summary">';
3005 $options = new stdClass();
3006 $options->noclean = true;
3007 $options->para = false;
3008 $options->overflowdiv = true;
3009 echo format_text($course->summary, $course->summaryformat, $options);
3010 echo '</div>';
3011 echo '</div>';
3012}
3013
3014/**
3015 * Prints information about one remote host
3016 *
3017 * This function is deprecated, it is replaced with protected function
3018 * {@link core_course_renderer::frontpage_remote_host()}
3019 * It is only used from function {@link core_course_renderer::frontpage_my_courses()}
3020 *
3021 * @deprecated since 2.5
3022 */
3023function print_remote_host($host, $width="100%") {
3024 global $OUTPUT;
3025 debugging('Function print_remote_host() is deprecated, please use course renderer', DEBUG_DEVELOPER);
3026
3027 $linkcss = '';
3028
3029 echo '<div class="coursebox clearfix">';
3030 echo '<div class="info">';
3031 echo '<div class="name">';
3032 echo '<img src="'.$OUTPUT->pix_url('i/mnethost') . '" class="icon" alt="'.get_string('course').'" />';
3033 echo '<a title="'.s($host['name']).'" href="'.s($host['url']).'">'
3034 . s($host['name']).'</a> - ';
3035 echo $host['count'] . ' ' . get_string('courses');
3036 echo '</div>';
3037 echo '</div>';
3038 echo '</div>';
3039}
3040
3041/**
3042 * Recursive function to print out all the categories in a nice format
3043 * with or without courses included
3044 *
3045 * @deprecated since 2.5
3046 *
3047 * See http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5
3048 */
3049function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1, $showcourses = true, $categorycourses=NULL) {
3050 global $PAGE;
3051 debugging('Function print_whole_category_list() is deprecated, please use course renderer', DEBUG_DEVELOPER);
3052
3053 $renderer = $PAGE->get_renderer('core', 'course');
3054 if ($showcourses && $category) {
3055 echo $renderer->course_category($category);
3056 } else if ($showcourses) {
3057 echo $renderer->frontpage_combo_list();
3058 } else {
3059 echo $renderer->frontpage_categories_list();
3060 }
3061}
3062
3063/**
3064 * Prints the category information.
3065 *
3066 * @deprecated since 2.5
3067 *
3068 * This function was only used by {@link print_whole_category_list()} but now
3069 * all course category rendering is moved to core_course_renderer.
3070 *
3071 * @param stdClass $category
3072 * @param int $depth The depth of the category.
3073 * @param bool $showcourses If set to true course information will also be printed.
3074 * @param array|null $courses An array of courses belonging to the category, or null if you don't have it yet.
3075 */
3076function print_category_info($category, $depth = 0, $showcourses = false, array $courses = null) {
3077 global $PAGE;
3078 debugging('Function print_category_info() is deprecated, please use course renderer', DEBUG_DEVELOPER);
3079
3080 $renderer = $PAGE->get_renderer('core', 'course');
3081 echo $renderer->course_category($category);
3082}
3083
3084/**
3085 * This function generates a structured array of courses and categories.
3086 *
3087 * @deprecated since 2.5
3088 *
3089 * This function is not used any more in moodle core and course renderer does not have render function for it.
3090 * Combo list on the front page is displayed as:
3091 * $renderer = $PAGE->get_renderer('core', 'course');
3092 * echo $renderer->frontpage_combo_list()
3093 *
3094 * The new class {@link coursecat} stores the information about course category tree
3095 * To get children categories use:
3096 * coursecat::get($id)->get_children()
3097 * To get list of courses use:
3098 * coursecat::get($id)->get_courses()
3099 *
3100 * See http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5
3101 *
3102 * @param int $id
3103 * @param int $depth
3104 */
3105function get_course_category_tree($id = 0, $depth = 0) {
3106 global $DB, $CFG;
3107 if (!$depth) {
3108 debugging('Function get_course_category_tree() is deprecated, please use course renderer or coursecat class, see function phpdocs for more info', DEBUG_DEVELOPER);
3109 }
3110
3111 $categories = array();
3112 $categoryids = array();
3113 $sql = context_helper::get_preload_record_columns_sql('ctx');
3114 $records = $DB->get_records_sql("SELECT c.*, $sql FROM {course_categories} c ".
3115 "JOIN {context} ctx on ctx.instanceid = c.id AND ctx.contextlevel = ? WHERE c.parent = ? ORDER BY c.sortorder",
3116 array(CONTEXT_COURSECAT, $id));
3117 foreach ($records as $category) {
3118 context_helper::preload_from_record($category);
3119 if (!$category->visible && !has_capability('moodle/category:viewhiddencategories', context_coursecat::instance($category->id))) {
3120 continue;
3121 }
3122 $categories[] = $category;
3123 $categoryids[$category->id] = $category;
3124 if (empty($CFG->maxcategorydepth) || $depth <= $CFG->maxcategorydepth) {
3125 list($category->categories, $subcategories) = get_course_category_tree($category->id, $depth+1);
3126 foreach ($subcategories as $subid=>$subcat) {
3127 $categoryids[$subid] = $subcat;
3128 }
3129 $category->courses = array();
3130 }
3131 }
3132
3133 if ($depth > 0) {
3134 // This is a recursive call so return the required array
3135 return array($categories, $categoryids);
3136 }
3137
3138 if (empty($categoryids)) {
3139 // No categories available (probably all hidden).
3140 return array();
3141 }
3142
3143 // The depth is 0 this function has just been called so we can finish it off
3144
3145 list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
3146 list($catsql, $catparams) = $DB->get_in_or_equal(array_keys($categoryids));
3147 $sql = "SELECT
3148 c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.summary,c.category
3149 $ccselect
3150 FROM {course} c
3151 $ccjoin
3152 WHERE c.category $catsql ORDER BY c.sortorder ASC";
3153 if ($courses = $DB->get_records_sql($sql, $catparams)) {
3154 // loop throught them
3155 foreach ($courses as $course) {
3156 if ($course->id == SITEID) {
3157 continue;
3158 }
db314f34 3159 context_helper::preload_from_record($course);
09ae7ee0
MG
3160 if (!empty($course->visible) || has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
3161 $categoryids[$course->category]->courses[$course->id] = $course;
3162 }
3163 }
3164 }
3165 return $categories;
3166}
3167
3168/**
3169 * Print courses in category. If category is 0 then all courses are printed.
3170 *
3171 * @deprecated since 2.5
3172 *
3173 * To print a generic list of courses use:
3174 * $renderer = $PAGE->get_renderer('core', 'course');
3175 * echo $renderer->courses_list($courses);
3176 *
3177 * To print list of all courses:
3178 * $renderer = $PAGE->get_renderer('core', 'course');
3179 * echo $renderer->frontpage_available_courses();
3180 *
3181 * To print list of courses inside category:
3182 * $renderer = $PAGE->get_renderer('core', 'course');
3183 * echo $renderer->course_category($category); // this will also print subcategories
3184 *
3185 * @param int|stdClass $category category object or id.
3186 * @return bool true if courses found and printed, else false.
3187 */
3188function print_courses($category) {
3189 global $CFG, $OUTPUT, $PAGE;
3190 require_once($CFG->libdir. '/coursecatlib.php');
3191 debugging('Function print_courses() is deprecated, please use course renderer', DEBUG_DEVELOPER);
3192
3193 if (!is_object($category) && $category==0) {
3194 $courses = coursecat::get(0)->get_courses(array('recursive' => true, 'summary' => true, 'coursecontacts' => true));
3195 } else {
3196 $courses = coursecat::get($category->id)->get_courses(array('summary' => true, 'coursecontacts' => true));
3197 }
3198
3199 if ($courses) {
3200 $renderer = $PAGE->get_renderer('core', 'course');
3201 echo $renderer->courses_list($courses);
3202 } else {
3203 echo $OUTPUT->heading(get_string("nocoursesyet"));
3204 $context = context_system::instance();
3205 if (has_capability('moodle/course:create', $context)) {
3206 $options = array();
3207 if (!empty($category->id)) {
3208 $options['category'] = $category->id;
3209 } else {
3210 $options['category'] = $CFG->defaultrequestcategory;
3211 }
3212 echo html_writer::start_tag('div', array('class'=>'addcoursebutton'));
3213 echo $OUTPUT->single_button(new moodle_url('/course/edit.php', $options), get_string("addnewcourse"));
3214 echo html_writer::end_tag('div');
3215 return false;
3216 }
3217 }
3218 return true;
3219}
3220
3221/**
3222 * Print a description of a course, suitable for browsing in a list.
3223 *
3224 * @deprecated since 2.5
3225 *
3226 * Please use course renderer to display a course information box.
3227 * $renderer = $PAGE->get_renderer('core', 'course');
3228 * echo $renderer->courses_list($courses); // will print list of courses
3229 * echo $renderer->course_info_box($course); // will print one course wrapped in div.generalbox
3230 *
3231 * @param object $course the course object.
3232 * @param string $highlightterms Ignored in this deprecated function!
3233 */
3234function print_course($course, $highlightterms = '') {
3235 global $PAGE;
3236
3237 debugging('Function print_course() is deprecated, please use course renderer', DEBUG_DEVELOPER);
3238 $renderer = $PAGE->get_renderer('core', 'course');
3239 // Please note, correct would be to use $renderer->coursecat_coursebox() but this function is protected.
3240 // To print list of courses use $renderer->courses_list();
3241 echo $renderer->course_info_box($course);
3242}
3243
3244/**
3245 * Gets an array whose keys are category ids and whose values are arrays of courses in the corresponding category.
3246 *
3247 * @deprecated since 2.5
3248 *
3249 * This function is not used any more in moodle core and course renderer does not have render function for it.
3250 * Combo list on the front page is displayed as:
3251 * $renderer = $PAGE->get_renderer('core', 'course');
3252 * echo $renderer->frontpage_combo_list()
3253 *
3254 * The new class {@link coursecat} stores the information about course category tree
3255 * To get children categories use:
3256 * coursecat::get($id)->get_children()
3257 * To get list of courses use:
3258 * coursecat::get($id)->get_courses()
3259 *
3260 * See http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5
3261 *
3262 * @param int $categoryid
3263 * @return array
3264 */
3265function get_category_courses_array($categoryid = 0) {
3266 debugging('Function get_category_courses_array() is deprecated, please use methods of coursecat class', DEBUG_DEVELOPER);
3267 $tree = get_course_category_tree($categoryid);
3268 $flattened = array();
3269 foreach ($tree as $category) {
3270 get_category_courses_array_recursively($flattened, $category);
3271 }
3272 return $flattened;
3273}
3274
3275/**
3276 * Recursive function to help flatten the course category tree.
3277 *
3278 * @deprecated since 2.5
3279 *
3280 * Was intended to be called from {@link get_category_courses_array()}
3281 *
3282 * @param array &$flattened An array passed by reference in which to store courses for each category.
3283 * @param stdClass $category The category to get courses for.
3284 */
3285function get_category_courses_array_recursively(array &$flattened, $category) {
3286 debugging('Function get_category_courses_array_recursively() is deprecated, please use methods of coursecat class', DEBUG_DEVELOPER);
3287 $flattened[$category->id] = $category->courses;
3288 foreach ($category->categories as $childcategory) {
3289 get_category_courses_array_recursively($flattened, $childcategory);
3290 }
3291}
3292
7ac18cf9
AA
3293/**
3294 * Returns a URL based on the context of the current page.
3295 * This URL points to blog/index.php and includes filter parameters appropriate for the current page.
3296 *
3297 * @param stdclass $context
3298 * @deprecated since Moodle 2.5 MDL-27814 - please do not use this function any more.
3299 * @todo Remove this in 2.7
3300 * @return string
3301 */
3302function blog_get_context_url($context=null) {
3303 global $CFG;
3304
3305 debugging('Function blog_get_context_url() is deprecated, getting params from context is not reliable for blogs.', DEBUG_DEVELOPER);
3306 $viewblogentriesurl = new moodle_url('/blog/index.php');
3307
3308 if (empty($context)) {
3309 global $PAGE;
3310 $context = $PAGE->context;
3311 }
3312
3313 // Change contextlevel to SYSTEM if viewing the site course
3314 if ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID) {
3315 $context = context_system::instance();
3316 }
3317
3318 $filterparam = '';
3319 $strlevel = '';
3320
3321 switch ($context->contextlevel) {
3322 case CONTEXT_SYSTEM:
3323 case CONTEXT_BLOCK:
3324 case CONTEXT_COURSECAT:
3325 break;
3326 case CONTEXT_COURSE:
3327 $filterparam = 'courseid';
3328 $strlevel = get_string('course');
3329 break;
3330 case CONTEXT_MODULE:
3331 $filterparam = 'modid';
329846f1 3332 $strlevel = $context->get_context_name();
7ac18cf9
AA
3333 break;
3334 case CONTEXT_USER:
3335 $filterparam = 'userid';
3336 $strlevel = get_string('user');
3337 break;
3338 }
3339
3340 if (!empty($filterparam)) {
3341 $viewblogentriesurl->param($filterparam, $context->instanceid);
3342 }
3343
3344 return $viewblogentriesurl;
3345}
3346
09ae7ee0
MG
3347/**
3348 * Retrieve course records with the course managers and other related records
3349 * that we need for print_course(). This allows print_courses() to do its job
3350 * in a constant number of DB queries, regardless of the number of courses,
3351 * role assignments, etc.
3352 *
3353 * The returned array is indexed on c.id, and each course will have
3354 * - $course->managers - array containing RA objects that include a $user obj
3355 * with the minimal fields needed for fullname()
3356 *
3357 * @deprecated since 2.5
3358 *
3359 * To get list of all courses with course contacts ('managers') use
3360 * coursecat::get(0)->get_courses(array('recursive' => true, 'coursecontacts' => true));
3361 *
3362 * To get list of courses inside particular category use
3363 * coursecat::get($id)->get_courses(array('coursecontacts' => true));
3364 *
3365 * Additionally you can specify sort order, offset and maximum number of courses,
3366 * see {@link coursecat::get_courses()}
3367 *
3368 * Please note that code of this function is not changed to use coursecat class because
3369 * coursecat::get_courses() returns result in slightly different format. Also note that
3370 * get_courses_wmanagers() DOES NOT check that users are enrolled in the course and
3371 * coursecat::get_courses() does.
3372 *
3373 * @global object
3374 * @global object
3375 * @global object
3376 * @uses CONTEXT_COURSE
3377 * @uses CONTEXT_SYSTEM
3378 * @uses CONTEXT_COURSECAT
3379 * @uses SITEID
3380 * @param int|string $categoryid Either the categoryid for the courses or 'all'
3381 * @param string $sort A SQL sort field and direction
3382 * @param array $fields An array of additional fields to fetch
3383 * @return array
3384 */
3385function get_courses_wmanagers($categoryid=0, $sort="c.sortorder ASC", $fields=array()) {
3386 /*
3387 * The plan is to
3388 *
3389 * - Grab the courses JOINed w/context
3390 *
3391 * - Grab the interesting course-manager RAs
3392 * JOINed with a base user obj and add them to each course
3393 *
3394 * So as to do all the work in 2 DB queries. The RA+user JOIN
3395 * ends up being pretty expensive if it happens over _all_
3396 * courses on a large site. (Are we surprised!?)
3397 *
3398 * So this should _never_ get called with 'all' on a large site.
3399 *
3400 */
3401 global $USER, $CFG, $DB;
3402 debugging('Function get_courses_wmanagers() is deprecated, please use coursecat::get_courses()', DEBUG_DEVELOPER);
3403
3404 $params = array();
3405 $allcats = false; // bool flag
3406 if ($categoryid === 'all') {
3407 $categoryclause = '';
3408 $allcats = true;
3409 } elseif (is_numeric($categoryid)) {
3410 $categoryclause = "c.category = :catid";
3411 $params['catid'] = $categoryid;
3412 } else {
3413 debugging("Could not recognise categoryid = $categoryid");
3414 $categoryclause = '';
3415 }
3416
3417 $basefields = array('id', 'category', 'sortorder',
3418 'shortname', 'fullname', 'idnumber',
3419 'startdate', 'visible',
3420 'newsitems', 'groupmode', 'groupmodeforce');
3421
3422 if (!is_null($fields) && is_string($fields)) {
3423 if (empty($fields)) {
3424 $fields = $basefields;
3425 } else {
3426 // turn the fields from a string to an array that
3427 // get_user_courses_bycap() will like...
3428 $fields = explode(',',$fields);
3429 $fields = array_map('trim', $fields);
3430 $fields = array_unique(array_merge($basefields, $fields));
3431 }
3432 } elseif (is_array($fields)) {
3433 $fields = array_merge($basefields,$fields);
3434 }
3435 $coursefields = 'c.' .join(',c.', $fields);
3436
3437 if (empty($sort)) {
3438 $sortstatement = "";
3439 } else {
3440 $sortstatement = "ORDER BY $sort";
3441 }
3442
3443 $where = 'WHERE c.id != ' . SITEID;
3444 if ($categoryclause !== ''){
3445 $where = "$where AND $categoryclause";
3446 }
3447
3448 // pull out all courses matching the cat
3449 list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
3450 $sql = "SELECT $coursefields $ccselect
3451 FROM {course} c
3452 $ccjoin
3453 $where
3454 $sortstatement";
3455
3456 $catpaths = array();
3457 $catpath = NULL;
3458 if ($courses = $DB->get_records_sql($sql, $params)) {
3459 // loop on courses materialising
3460 // the context, and prepping data to fetch the
3461 // managers efficiently later...
3462 foreach ($courses as $k => $course) {
db314f34 3463 context_helper::preload_from_record($course);
09ae7ee0
MG
3464 $coursecontext = context_course::instance($course->id);
3465 $courses[$k] = $course;
3466 $courses[$k]->managers = array();
3467 if ($allcats === false) {
3468 // single cat, so take just the first one...
3469 if ($catpath === NULL) {
3470 $catpath = preg_replace(':/\d+$:', '', $coursecontext->path);
3471 }
3472 } else {
3473 // chop off the contextid of the course itself
3474 // like dirname() does...
3475 $catpaths[] = preg_replace(':/\d+$:', '', $coursecontext->path);
3476 }
3477 }
3478 } else {
3479 return array(); // no courses!
3480 }
3481
3482 $CFG->coursecontact = trim($CFG->coursecontact);
3483 if (empty($CFG->coursecontact)) {
3484 return $courses;
3485 }
3486
3487 $managerroles = explode(',', $CFG->coursecontact);
3488 $catctxids = '';
3489 if (count($managerroles)) {
3490 if ($allcats === true) {
3491 $catpaths = array_unique($catpaths);
3492 $ctxids = array();
3493 foreach ($catpaths as $cpath) {
3494 $ctxids = array_merge($ctxids, explode('/',substr($cpath,1)));
3495 }
3496 $ctxids = array_unique($ctxids);
3497 $catctxids = implode( ',' , $ctxids);
3498 unset($catpaths);
3499 unset($cpath);
3500 } else {
3501 // take the ctx path from the first course
3502 // as all categories will be the same...
3503 $catpath = substr($catpath,1);
3504 $catpath = preg_replace(':/\d+$:','',$catpath);
3505 $catctxids = str_replace('/',',',$catpath);
3506 }
3507 if ($categoryclause !== '') {
3508 $categoryclause = "AND $categoryclause";
3509 }
3510 /*
3511 * Note: Here we use a LEFT OUTER JOIN that can
3512 * "optionally" match to avoid passing a ton of context
3513 * ids in an IN() clause. Perhaps a subselect is faster.
3514 *
3515 * In any case, this SQL is not-so-nice over large sets of
3516 * courses with no $categoryclause.
3517 *
3518 */
3519 $sql = "SELECT ctx.path, ctx.instanceid, ctx.contextlevel,
3520 r.id AS roleid, r.name AS rolename, r.shortname AS roleshortname,
3521 rn.name AS rolecoursealias, u.id AS userid, u.firstname, u.lastname
3522 FROM {role_assignments} ra
3523 JOIN {context} ctx ON ra.contextid = ctx.id
3524 JOIN {user} u ON ra.userid = u.id
3525 JOIN {role} r ON ra.roleid = r.id
3526 LEFT JOIN {role_names} rn ON (rn.contextid = ctx.id AND rn.roleid = r.id)
3527 LEFT OUTER JOIN {course} c
3528 ON (ctx.instanceid=c.id AND ctx.contextlevel=".CONTEXT_COURSE.")
3529 WHERE ( c.id IS NOT NULL";
3530 // under certain conditions, $catctxids is NULL
3531 if($catctxids == NULL){
3532 $sql .= ") ";
3533 }else{
3534 $sql .= " OR ra.contextid IN ($catctxids) )";
3535 }
3536
3537 $sql .= "AND ra.roleid IN ({$CFG->coursecontact})
3538 $categoryclause
3539 ORDER BY r.sortorder ASC, ctx.contextlevel ASC, ra.sortorder ASC";
3540 $rs = $DB->get_recordset_sql($sql, $params);
3541
3542 // This loop is fairly stupid as it stands - might get better
3543 // results doing an initial pass clustering RAs by path.
3544 foreach($rs as $ra) {
3545 $user = new stdClass;
3546 $user->id = $ra->userid; unset($ra->userid);
3547 $user->firstname = $ra->firstname; unset($ra->firstname);
3548 $user->lastname = $ra->lastname; unset($ra->lastname);
3549 $ra->user = $user;
3550 if ($ra->contextlevel == CONTEXT_SYSTEM) {
3551 foreach ($courses as $k => $course) {
3552 $courses[$k]->managers[] = $ra;
3553 }
3554 } else if ($ra->contextlevel == CONTEXT_COURSECAT) {
3555 if ($allcats === false) {
3556 // It always applies
3557 foreach ($courses as $k => $course) {
3558 $courses[$k]->managers[] = $ra;
3559 }
3560 } else {
3561 foreach ($courses as $k => $course) {
3562 $coursecontext = context_course::instance($course->id);
3563 // Note that strpos() returns 0 as "matched at pos 0"
3564 if (strpos($coursecontext->path, $ra->path.'/') === 0) {
3565 // Only add it to subpaths
3566 $courses[$k]->managers[] = $ra;
3567 }
3568 }
3569 }
3570 } else { // course-level
3571 if (!array_key_exists($ra->instanceid, $courses)) {
3572 //this course is not in a list, probably a frontpage course
3573 continue;
3574 }
3575 $courses[$ra->instanceid]->managers[] = $ra;
3576 }
3577 }
3578 $rs->close();
3579 }
3580
3581 return $courses;
3582}
c269b9d1
MG
3583
3584/**
3585 * Converts a nested array tree into HTML ul:li [recursive]
3586 *
3587 * @deprecated since 2.5
3588 *
3589 * @param array $tree A tree array to convert
3590 * @param int $row Used in identifying the iteration level and in ul classes
3591 * @return string HTML structure
3592 */
3593function convert_tree_to_html($tree, $row=0) {
3594 debugging('Function convert_tree_to_html() is deprecated since Moodle 2.5. Consider using class tabtree and core_renderer::render_tabtree()', DEBUG_DEVELOPER);
3595
3596 $str = "\n".'<ul class="tabrow'.$row.'">'."\n";
3597
3598 $first = true;
3599 $count = count($tree);
3600
3601 foreach ($tree as $tab) {
3602 $count--; // countdown to zero
3603
3604 $liclass = '';
3605
3606 if ($first && ($count == 0)) { // Just one in the row
3607 $liclass = 'first last';
3608 $first = false;
3609 } else if ($first) {
3610 $liclass = 'first';
3611 $first = false;
3612 } else if ($count == 0) {
3613 $liclass = 'last';
3614 }
3615
3616 if ((empty($tab->subtree)) && (!empty($tab->selected))) {
3617 $liclass .= (empty($liclass)) ? 'onerow' : ' onerow';
3618 }
3619
3620 if ($tab->inactive || $tab->active || $tab->selected) {
3621 if ($tab->selected) {
3622 $liclass .= (empty($liclass)) ? 'here selected' : ' here selected';
3623 } else if ($tab->active) {
3624 $liclass .= (empty($liclass)) ? 'here active' : ' here active';
3625 }
3626 }
3627
3628 $str .= (!empty($liclass)) ? '<li class="'.$liclass.'">' : '<li>';
3629
3630 if ($tab->inactive || $tab->active || ($tab->selected && !$tab->linkedwhenselected)) {
3631 // The a tag is used for styling
3632 $str .= '<a class="nolink"><span>'.$tab->text.'</span></a>';
3633 } else {
3634 $str .= '<a href="'.$tab->link.'" title="'.$tab->title.'"><span>'.$tab->text.'</span></a>';
3635 }
3636
3637 if (!empty($tab->subtree)) {
3638 $str .= convert_tree_to_html($tab->subtree, $row+1);
3639 } else if ($tab->selected) {
3640 $str .= '<div class="tabrow'.($row+1).' empty">&nbsp;</div>'."\n";
3641 }
3642
3643 $str .= ' </li>'."\n";
3644 }
3645 $str .= '</ul>'."\n";
3646
3647 return $str;
3648}
3649
3650/**
3651 * Convert nested tabrows to a nested array
3652 *
3653 * @deprecated since 2.5
3654 *
3655 * @param array $tabrows A [nested] array of tab row objects
3656 * @param string $selected The tabrow to select (by id)
3657 * @param array $inactive An array of tabrow id's to make inactive
3658 * @param array $activated An array of tabrow id's to make active
3659 * @return array The nested array
3660 */
3661function convert_tabrows_to_tree($tabrows, $selected, $inactive, $activated) {
3662
3663 debugging('Function convert_tabrows_to_tree() is deprecated since Moodle 2.5. Consider using class tabtree', DEBUG_DEVELOPER);
c269b9d1 3664
3d7414b3 3665 // Work backwards through the rows (bottom to top) collecting the tree as we go.
c269b9d1
MG
3666 $tabrows = array_reverse($tabrows);
3667
3668 $subtree = array();
3669
3670 foreach ($tabrows as $row) {
3671 $tree = array();
3672
3673 foreach ($row as $tab) {
3674 $tab->inactive = in_array((string)$tab->id, $inactive);
3675 $tab->active = in_array((string)$tab->id, $activated);
3676 $tab->selected = (string)$tab->id == $selected;
3677
3678 if ($tab->active || $tab->selected) {
3679 if ($subtree) {
3680 $tab->subtree = $subtree;
3681 }
3682 }
3683 $tree[] = $tab;
3684 }
3685 $subtree = $tree;
3686 }
3687
3688 return $subtree;
3689}
9052fc44 3690
8ef3a222
DP
3691/**
3692 * Can handle rotated text. Whether it is safe to use the trickery in textrotate.js.
3693 *
3694 * @deprecated since 2.5 - do not use, the textrotate.js will work it out automatically
3695 * @return bool True for yes, false for no
3696 */
3697function can_use_rotated_text() {
3698 debugging('can_use_rotated_text() is deprecated since Moodle 2.5. JS feature detection is used automatically.', DEBUG_DEVELOPER);
3699 return true;
3700}
61a0299a
AA
3701
3702/**
3703 * Get the context instance as an object. This function will create the
3704 * context instance if it does not exist yet.
3705 *
3706 * @deprecated since 2.2, use context_course::instance() or other relevant class instead
3707 * @todo This will be deleted in Moodle 2.8, refer MDL-34472
3708 * @param integer $contextlevel The context level, for example CONTEXT_COURSE, or CONTEXT_MODULE.
3709 * @param integer $instance The instance id. For $level = CONTEXT_COURSE, this would be $course->id,
3710 * for $level = CONTEXT_MODULE, this would be $cm->id. And so on. Defaults to 0
3711 * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
3712 * MUST_EXIST means throw exception if no record or multiple records found
3713 * @return context The context object.
3714 */
3715function get_context_instance($contextlevel, $instance = 0, $strictness = IGNORE_MISSING) {
3716
1b80060f 3717 debugging('get_context_instance() is deprecated, please use context_xxxx::instance() instead.', DEBUG_DEVELOPER);
61a0299a
AA
3718
3719 $instances = (array)$instance;
3720 $contexts = array();
3721
3722 $classname = context_helper::get_class_for_level($contextlevel);
3723
3724 // we do not load multiple contexts any more, PAGE should be responsible for any preloading
3725 foreach ($instances as $inst) {
3726 $contexts[$inst] = $classname::instance($inst, $strictness);
3727 }
3728
3729 if (is_array($instance)) {
3730 return $contexts;
3731 } else {
3732 return $contexts[$instance];
3733 }
3734}
7b2ca655 3735
ce2b2150
RT
3736/**
3737 * Get a context instance as an object, from a given context id.
3738 *
3739 * @deprecated since Moodle 2.2 MDL-35009 - please do not use this function any more.
3740 * @todo MDL-34550 This will be deleted in Moodle 2.8
3741 * @see context::instance_by_id($id)
3742 * @param int $id context id
3743 * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
3744 * MUST_EXIST means throw exception if no record or multiple records found
3745 * @return context|bool the context object or false if not found.
3746 */
3747function get_context_instance_by_id($id, $strictness = IGNORE_MISSING) {
1b80060f 3748 debugging('get_context_instance_by_id() is deprecated, please use context::instance_by_id($id) instead.', DEBUG_DEVELOPER);
ce2b2150
RT
3749 return context::instance_by_id($id, $strictness);
3750}
85b2e46f 3751
492ba9de
AA
3752/**
3753 * Returns system context or null if can not be created yet.
3754 *
3755 * @see context_system::instance()
01546175 3756 * @deprecated since 2.2
492ba9de
AA
3757 * @param bool $cache use caching
3758 * @return context system context (null if context table not created yet)
3759 */
3760function get_system_context($cache = true) {
3761 debugging('get_system_context() is deprecated, please use context_system::instance() instead.', DEBUG_DEVELOPER);
3762 return context_system::instance(0, IGNORE_MISSING, $cache);
3763}
8e8891b7
FM
3764
3765/**
3766 * Recursive function which, given a context, find all parent context ids,
3767 * and return the array in reverse order, i.e. parent first, then grand
3768 * parent, etc.
3769 *
3770 * @see context::get_parent_context_ids()
3771 * @deprecated since 2.2, use $context->get_parent_context_ids() instead
3772 * @param context $context
3773 * @param bool $includeself optional, defaults to false
3774 * @return array
3775 */
3776function get_parent_contexts(context $context, $includeself = false) {
3777 debugging('get_parent_contexts() is deprecated, please use $context->get_parent_context_ids() instead.', DEBUG_DEVELOPER);
3778 return $context->get_parent_context_ids($includeself);
3779}
766fd0d9
AD
3780
3781/**
7f5b51c4
RT
3782 * Return the id of the parent of this context, or false if there is no parent (only happens if this
3783 * is the site context.)
3784 *
3785 * @deprecated since Moodle 2.2
3786 * @see context::get_parent_context()
3787 * @param context $context
3788 * @return integer the id of the parent context.
766fd0d9 3789 */
7f5b51c4
RT
3790function get_parent_contextid(context $context) {
3791 debugging('get_parent_contextid() is deprecated, please use $context->get_parent_context() instead.', DEBUG_DEVELOPER);
3792
3793 if ($parent = $context->get_parent_context()) {
3794 return $parent->id;
3795 } else {
3796 return false;
3797 }
766fd0d9 3798}
f9aa8016
FM
3799
3800/**
3801 * Recursive function which, given a context, find all its children contexts.
3802 *
3803 * For course category contexts it will return immediate children only categories and courses.
3804 * It will NOT recurse into courses or child categories.
3805 * If you want to do that, call it on the returned courses/categories.
3806 *
3807 * When called for a course context, it will return the modules and blocks
3808 * displayed in the course page.
3809 *
3810 * If called on a user/course/module context it _will_ populate the cache with the appropriate
3811 * contexts ;-)
3812 *
3813 * @see context::get_child_contexts()
3814 * @deprecated since 2.2
3815 * @param context $context
3816 * @return array Array of child records
3817 */
3818function get_child_contexts(context $context) {
3819 debugging('get_child_contexts() is deprecated, please use $context->get_child_contexts() instead.', DEBUG_DEVELOPER);
3820 return $context->get_child_contexts();
3821}
9fd