PageRenderTime 70ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/book/lib.php

https://github.com/arborrow/moodle
PHP | 456 lines | 214 code | 66 blank | 176 comment | 28 complexity | 257d0c870122807c38ff224e62d06ece MD5 | raw file
  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. * Book module core interaction API
  18. *
  19. * @package mod_book
  20. * @copyright 2004-2011 Petr Skoda {@link http://skodak.org}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die;
  24. /**
  25. * Returns list of available numbering types
  26. * @return array
  27. */
  28. function book_get_numbering_types() {
  29. global $CFG; // required for the include
  30. require_once(dirname(__FILE__).'/locallib.php');
  31. return array (
  32. BOOK_NUM_NONE => get_string('numbering0', 'mod_book'),
  33. BOOK_NUM_NUMBERS => get_string('numbering1', 'mod_book'),
  34. BOOK_NUM_BULLETS => get_string('numbering2', 'mod_book'),
  35. BOOK_NUM_INDENTED => get_string('numbering3', 'mod_book')
  36. );
  37. }
  38. /**
  39. * Returns list of available navigation link types.
  40. * @return array
  41. */
  42. function book_get_nav_types() {
  43. require_once(dirname(__FILE__).'/locallib.php');
  44. return array (
  45. BOOK_LINK_TOCONLY => get_string('navtoc', 'mod_book'),
  46. BOOK_LINK_IMAGE => get_string('navimages', 'mod_book'),
  47. BOOK_LINK_TEXT => get_string('navtext', 'mod_book'),
  48. );
  49. }
  50. /**
  51. * Returns list of available navigation link CSS classes.
  52. * @return array
  53. */
  54. function book_get_nav_classes() {
  55. return array ('navtoc', 'navimages', 'navtext');
  56. }
  57. /**
  58. * Returns all other caps used in module
  59. * @return array
  60. */
  61. function book_get_extra_capabilities() {
  62. // used for group-members-only
  63. return array('moodle/site:accessallgroups');
  64. }
  65. /**
  66. * Add book instance.
  67. *
  68. * @param stdClass $data
  69. * @param stdClass $mform
  70. * @return int new book instance id
  71. */
  72. function book_add_instance($data, $mform) {
  73. global $DB;
  74. $data->timecreated = time();
  75. $data->timemodified = $data->timecreated;
  76. if (!isset($data->customtitles)) {
  77. $data->customtitles = 0;
  78. }
  79. return $DB->insert_record('book', $data);
  80. }
  81. /**
  82. * Update book instance.
  83. *
  84. * @param stdClass $data
  85. * @param stdClass $mform
  86. * @return bool true
  87. */
  88. function book_update_instance($data, $mform) {
  89. global $DB;
  90. $data->timemodified = time();
  91. $data->id = $data->instance;
  92. if (!isset($data->customtitles)) {
  93. $data->customtitles = 0;
  94. }
  95. $DB->update_record('book', $data);
  96. $book = $DB->get_record('book', array('id'=>$data->id));
  97. $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
  98. return true;
  99. }
  100. /**
  101. * Delete book instance by activity id
  102. *
  103. * @param int $id
  104. * @return bool success
  105. */
  106. function book_delete_instance($id) {
  107. global $DB;
  108. if (!$book = $DB->get_record('book', array('id'=>$id))) {
  109. return false;
  110. }
  111. $DB->delete_records('book_chapters', array('bookid'=>$book->id));
  112. $DB->delete_records('book', array('id'=>$book->id));
  113. return true;
  114. }
  115. /**
  116. * Given a course and a time, this module should find recent activity
  117. * that has occurred in book activities and print it out.
  118. *
  119. * @param stdClass $course
  120. * @param bool $viewfullnames
  121. * @param int $timestart
  122. * @return bool true if there was output, or false is there was none
  123. */
  124. function book_print_recent_activity($course, $viewfullnames, $timestart) {
  125. return false; // True if anything was printed, otherwise false
  126. }
  127. /**
  128. * This function is used by the reset_course_userdata function in moodlelib.
  129. * @param $data the data submitted from the reset course.
  130. * @return array status array
  131. */
  132. function book_reset_userdata($data) {
  133. return array();
  134. }
  135. /**
  136. * No cron in book.
  137. *
  138. * @return bool
  139. */
  140. function book_cron () {
  141. return true;
  142. }
  143. /**
  144. * No grading in book.
  145. *
  146. * @param int $bookid
  147. * @return null
  148. */
  149. function book_grades($bookid) {
  150. return null;
  151. }
  152. /**
  153. * This function returns if a scale is being used by one book
  154. * it it has support for grading and scales. Commented code should be
  155. * modified if necessary. See book, glossary or journal modules
  156. * as reference.
  157. *
  158. * @param int $bookid
  159. * @param int $scaleid
  160. * @return boolean True if the scale is used by any journal
  161. */
  162. function book_scale_used($bookid, $scaleid) {
  163. return false;
  164. }
  165. /**
  166. * Checks if scale is being used by any instance of book
  167. *
  168. * This is used to find out if scale used anywhere
  169. *
  170. * @param int $scaleid
  171. * @return bool true if the scale is used by any book
  172. */
  173. function book_scale_used_anywhere($scaleid) {
  174. return false;
  175. }
  176. /**
  177. * Return read actions.
  178. *
  179. * Note: This is not used by new logging system. Event with
  180. * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
  181. * be considered as view action.
  182. *
  183. * @return array
  184. */
  185. function book_get_view_actions() {
  186. global $CFG; // necessary for includes
  187. $return = array('view', 'view all');
  188. $plugins = core_component::get_plugin_list('booktool');
  189. foreach ($plugins as $plugin => $dir) {
  190. if (file_exists("$dir/lib.php")) {
  191. require_once("$dir/lib.php");
  192. }
  193. $function = 'booktool_'.$plugin.'_get_view_actions';
  194. if (function_exists($function)) {
  195. if ($actions = $function()) {
  196. $return = array_merge($return, $actions);
  197. }
  198. }
  199. }
  200. return $return;
  201. }
  202. /**
  203. * Return write actions.
  204. *
  205. * Note: This is not used by new logging system. Event with
  206. * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
  207. * will be considered as post action.
  208. *
  209. * @return array
  210. */
  211. function book_get_post_actions() {
  212. global $CFG; // necessary for includes
  213. $return = array('update');
  214. $plugins = core_component::get_plugin_list('booktool');
  215. foreach ($plugins as $plugin => $dir) {
  216. if (file_exists("$dir/lib.php")) {
  217. require_once("$dir/lib.php");
  218. }
  219. $function = 'booktool_'.$plugin.'_get_post_actions';
  220. if (function_exists($function)) {
  221. if ($actions = $function()) {
  222. $return = array_merge($return, $actions);
  223. }
  224. }
  225. }
  226. return $return;
  227. }
  228. /**
  229. * Supported features
  230. *
  231. * @param string $feature FEATURE_xx constant for requested feature
  232. * @return mixed True if module supports feature, false if not, null if doesn't know
  233. */
  234. function book_supports($feature) {
  235. switch($feature) {
  236. case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
  237. case FEATURE_GROUPS: return false;
  238. case FEATURE_GROUPINGS: return false;
  239. case FEATURE_MOD_INTRO: return true;
  240. case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
  241. case FEATURE_GRADE_HAS_GRADE: return false;
  242. case FEATURE_GRADE_OUTCOMES: return false;
  243. case FEATURE_BACKUP_MOODLE2: return true;
  244. case FEATURE_SHOW_DESCRIPTION: return true;
  245. default: return null;
  246. }
  247. }
  248. /**
  249. * Adds module specific settings to the settings block
  250. *
  251. * @param settings_navigation $settingsnav The settings navigation object
  252. * @param navigation_node $booknode The node to add module settings to
  253. * @return void
  254. */
  255. function book_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $booknode) {
  256. global $USER, $PAGE;
  257. $plugins = core_component::get_plugin_list('booktool');
  258. foreach ($plugins as $plugin => $dir) {
  259. if (file_exists("$dir/lib.php")) {
  260. require_once("$dir/lib.php");
  261. }
  262. $function = 'booktool_'.$plugin.'_extend_settings_navigation';
  263. if (function_exists($function)) {
  264. $function($settingsnav, $booknode);
  265. }
  266. }
  267. $params = $PAGE->url->params();
  268. if (!empty($params['id']) and !empty($params['chapterid']) and has_capability('mod/book:edit', $PAGE->cm->context)) {
  269. if (!empty($USER->editing)) {
  270. $string = get_string("turneditingoff");
  271. $edit = '0';
  272. } else {
  273. $string = get_string("turneditingon");
  274. $edit = '1';
  275. }
  276. $url = new moodle_url('/mod/book/view.php', array('id'=>$params['id'], 'chapterid'=>$params['chapterid'], 'edit'=>$edit, 'sesskey'=>sesskey()));
  277. $booknode->add($string, $url, navigation_node::TYPE_SETTING);
  278. }
  279. }
  280. /**
  281. * Lists all browsable file areas
  282. * @param object $course
  283. * @param object $cm
  284. * @param object $context
  285. * @return array
  286. */
  287. function book_get_file_areas($course, $cm, $context) {
  288. $areas = array();
  289. $areas['chapter'] = get_string('chapters', 'mod_book');
  290. return $areas;
  291. }
  292. /**
  293. * File browsing support for book module chapter area.
  294. * @param object $browser
  295. * @param object $areas
  296. * @param object $course
  297. * @param object $cm
  298. * @param object $context
  299. * @param string $filearea
  300. * @param int $itemid
  301. * @param string $filepath
  302. * @param string $filename
  303. * @return object file_info instance or null if not found
  304. */
  305. function book_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
  306. global $CFG, $DB;
  307. // note: 'intro' area is handled in file_browser automatically
  308. if (!has_capability('mod/book:read', $context)) {
  309. return null;
  310. }
  311. if ($filearea !== 'chapter') {
  312. return null;
  313. }
  314. require_once(dirname(__FILE__).'/locallib.php');
  315. if (is_null($itemid)) {
  316. return new book_file_info($browser, $course, $cm, $context, $areas, $filearea);
  317. }
  318. $fs = get_file_storage();
  319. $filepath = is_null($filepath) ? '/' : $filepath;
  320. $filename = is_null($filename) ? '.' : $filename;
  321. if (!$storedfile = $fs->get_file($context->id, 'mod_book', $filearea, $itemid, $filepath, $filename)) {
  322. return null;
  323. }
  324. // modifications may be tricky - may cause caching problems
  325. $canwrite = has_capability('mod/book:edit', $context);
  326. $chaptername = $DB->get_field('book_chapters', 'title', array('bookid'=>$cm->instance, 'id'=>$itemid));
  327. $chaptername = format_string($chaptername, true, array('context'=>$context));
  328. $urlbase = $CFG->wwwroot.'/pluginfile.php';
  329. return new file_info_stored($browser, $context, $storedfile, $urlbase, $chaptername, true, true, $canwrite, false);
  330. }
  331. /**
  332. * Serves the book attachments. Implements needed access control ;-)
  333. *
  334. * @param stdClass $course course object
  335. * @param cm_info $cm course module object
  336. * @param context $context context object
  337. * @param string $filearea file area
  338. * @param array $args extra arguments
  339. * @param bool $forcedownload whether or not force download
  340. * @param array $options additional options affecting the file serving
  341. * @return bool false if file not found, does not return if found - just send the file
  342. */
  343. function book_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
  344. global $CFG, $DB;
  345. if ($context->contextlevel != CONTEXT_MODULE) {
  346. return false;
  347. }
  348. require_course_login($course, true, $cm);
  349. if ($filearea !== 'chapter') {
  350. return false;
  351. }
  352. if (!has_capability('mod/book:read', $context)) {
  353. return false;
  354. }
  355. $chid = (int)array_shift($args);
  356. if (!$book = $DB->get_record('book', array('id'=>$cm->instance))) {
  357. return false;
  358. }
  359. if (!$chapter = $DB->get_record('book_chapters', array('id'=>$chid, 'bookid'=>$book->id))) {
  360. return false;
  361. }
  362. if ($chapter->hidden and !has_capability('mod/book:viewhiddenchapters', $context)) {
  363. return false;
  364. }
  365. $fs = get_file_storage();
  366. $relativepath = implode('/', $args);
  367. $fullpath = "/$context->id/mod_book/chapter/$chid/$relativepath";
  368. if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
  369. return false;
  370. }
  371. // Nasty hack because we do not have file revisions in book yet.
  372. $lifetime = $CFG->filelifetime;
  373. if ($lifetime > 60*10) {
  374. $lifetime = 60*10;
  375. }
  376. // finally send the file
  377. send_stored_file($file, $lifetime, 0, $forcedownload, $options);
  378. }
  379. /**
  380. * Return a list of page types
  381. *
  382. * @param string $pagetype current page type
  383. * @param stdClass $parentcontext Block's parent context
  384. * @param stdClass $currentcontext Current context of block
  385. * @return array
  386. */
  387. function book_page_type_list($pagetype, $parentcontext, $currentcontext) {
  388. $module_pagetype = array('mod-book-*'=>get_string('page-mod-book-x', 'mod_book'));
  389. return $module_pagetype;
  390. }