PageRenderTime 23ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/book/lib.php

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