PageRenderTime 82ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/deprecatedlib.php

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