PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/book/lib.php

https://bitbucket.org/ngmares/moodle
PHP | 451 lines | 213 code | 67 blank | 171 comment | 28 complexity | 5bbbd02b21ead5f8c9841ac32aab00a3 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  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. * No cron in book.
  145. *
  146. * @return bool
  147. */
  148. function book_cron () {
  149. return true;
  150. }
  151. /**
  152. * No grading in book.
  153. *
  154. * @param int $bookid
  155. * @return null
  156. */
  157. function book_grades($bookid) {
  158. return null;
  159. }
  160. /**
  161. * This function returns if a scale is being used by one book
  162. * it it has support for grading and scales. Commented code should be
  163. * modified if necessary. See book, glossary or journal modules
  164. * as reference.
  165. *
  166. * @param int $bookid
  167. * @param int $scaleid
  168. * @return boolean True if the scale is used by any journal
  169. */
  170. function book_scale_used($bookid, $scaleid) {
  171. return false;
  172. }
  173. /**
  174. * Checks if scale is being used by any instance of book
  175. *
  176. * This is used to find out if scale used anywhere
  177. *
  178. * @param int $scaleid
  179. * @return bool true if the scale is used by any book
  180. */
  181. function book_scale_used_anywhere($scaleid) {
  182. return false;
  183. }
  184. /**
  185. * Return read actions.
  186. * @return array
  187. */
  188. function book_get_view_actions() {
  189. global $CFG; // necessary for includes
  190. $return = array('view', 'view all');
  191. $plugins = get_plugin_list('booktool');
  192. foreach ($plugins as $plugin => $dir) {
  193. if (file_exists("$dir/lib.php")) {
  194. require_once("$dir/lib.php");
  195. }
  196. $function = 'booktool_'.$plugin.'_get_view_actions';
  197. if (function_exists($function)) {
  198. if ($actions = $function()) {
  199. $return = array_merge($return, $actions);
  200. }
  201. }
  202. }
  203. return $return;
  204. }
  205. /**
  206. * Return write actions.
  207. * @return array
  208. */
  209. function book_get_post_actions() {
  210. global $CFG; // necessary for includes
  211. $return = array('update');
  212. $plugins = get_plugin_list('booktool');
  213. foreach ($plugins as $plugin => $dir) {
  214. if (file_exists("$dir/lib.php")) {
  215. require_once("$dir/lib.php");
  216. }
  217. $function = 'booktool_'.$plugin.'_get_post_actions';
  218. if (function_exists($function)) {
  219. if ($actions = $function()) {
  220. $return = array_merge($return, $actions);
  221. }
  222. }
  223. }
  224. return $return;
  225. }
  226. /**
  227. * Supported features
  228. *
  229. * @param string $feature FEATURE_xx constant for requested feature
  230. * @return mixed True if module supports feature, false if not, null if doesn't know
  231. */
  232. function book_supports($feature) {
  233. switch($feature) {
  234. case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
  235. case FEATURE_GROUPS: return false;
  236. case FEATURE_GROUPINGS: return false;
  237. case FEATURE_GROUPMEMBERSONLY: return true;
  238. case FEATURE_MOD_INTRO: return true;
  239. case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
  240. case FEATURE_GRADE_HAS_GRADE: return false;
  241. case FEATURE_GRADE_OUTCOMES: return false;
  242. case FEATURE_BACKUP_MOODLE2: return true;
  243. case FEATURE_SHOW_DESCRIPTION: return true;
  244. default: return null;
  245. }
  246. }
  247. /**
  248. * Adds module specific settings to the settings block
  249. *
  250. * @param settings_navigation $settingsnav The settings navigation object
  251. * @param navigation_node $booknode The node to add module settings to
  252. * @return void
  253. */
  254. function book_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $booknode) {
  255. global $USER, $PAGE;
  256. $plugins = get_plugin_list('booktool');
  257. foreach ($plugins as $plugin => $dir) {
  258. if (file_exists("$dir/lib.php")) {
  259. require_once("$dir/lib.php");
  260. }
  261. $function = 'booktool_'.$plugin.'_extend_settings_navigation';
  262. if (function_exists($function)) {
  263. $function($settingsnav, $booknode);
  264. }
  265. }
  266. $params = $PAGE->url->params();
  267. if (!empty($params['id']) and !empty($params['chapterid']) and has_capability('mod/book:edit', $PAGE->cm->context)) {
  268. if (!empty($USER->editing)) {
  269. $string = get_string("turneditingoff");
  270. $edit = '0';
  271. } else {
  272. $string = get_string("turneditingon");
  273. $edit = '1';
  274. }
  275. $url = new moodle_url('/mod/book/view.php', array('id'=>$params['id'], 'chapterid'=>$params['chapterid'], 'edit'=>$edit, 'sesskey'=>sesskey()));
  276. $booknode->add($string, $url, navigation_node::TYPE_SETTING);
  277. }
  278. }
  279. /**
  280. * Lists all browsable file areas
  281. * @param object $course
  282. * @param object $cm
  283. * @param object $context
  284. * @return array
  285. */
  286. function book_get_file_areas($course, $cm, $context) {
  287. $areas = array();
  288. $areas['chapter'] = get_string('chapters', 'mod_book');
  289. return $areas;
  290. }
  291. /**
  292. * File browsing support for book module chapter area.
  293. * @param object $browser
  294. * @param object $areas
  295. * @param object $course
  296. * @param object $cm
  297. * @param object $context
  298. * @param string $filearea
  299. * @param int $itemid
  300. * @param string $filepath
  301. * @param string $filename
  302. * @return object file_info instance or null if not found
  303. */
  304. function book_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
  305. global $CFG, $DB;
  306. // note: 'intro' area is handled in file_browser automatically
  307. if (!has_capability('mod/book:read', $context)) {
  308. return null;
  309. }
  310. if ($filearea !== 'chapter') {
  311. return null;
  312. }
  313. require_once(dirname(__FILE__).'/locallib.php');
  314. if (is_null($itemid)) {
  315. return new book_file_info($browser, $course, $cm, $context, $areas, $filearea);
  316. }
  317. $fs = get_file_storage();
  318. $filepath = is_null($filepath) ? '/' : $filepath;
  319. $filename = is_null($filename) ? '.' : $filename;
  320. if (!$storedfile = $fs->get_file($context->id, 'mod_book', $filearea, $itemid, $filepath, $filename)) {
  321. return null;
  322. }
  323. // modifications may be tricky - may cause caching problems
  324. $canwrite = has_capability('mod/book:edit', $context);
  325. $chaptername = $DB->get_field('book_chapters', 'title', array('bookid'=>$cm->instance, 'id'=>$itemid));
  326. $chaptername = format_string($chaptername, true, array('context'=>$context));
  327. $urlbase = $CFG->wwwroot.'/pluginfile.php';
  328. return new file_info_stored($browser, $context, $storedfile, $urlbase, $chaptername, true, true, $canwrite, false);
  329. }
  330. /**
  331. * Serves the book attachments. Implements needed access control ;-)
  332. *
  333. * @param stdClass $course course object
  334. * @param cm_info $cm course module object
  335. * @param context $context context object
  336. * @param string $filearea file area
  337. * @param array $args extra arguments
  338. * @param bool $forcedownload whether or not force download
  339. * @param array $options additional options affecting the file serving
  340. * @return bool false if file not found, does not return if found - just send the file
  341. */
  342. function book_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
  343. global $DB;
  344. if ($context->contextlevel != CONTEXT_MODULE) {
  345. return false;
  346. }
  347. require_course_login($course, true, $cm);
  348. if ($filearea !== 'chapter') {
  349. return false;
  350. }
  351. if (!has_capability('mod/book:read', $context)) {
  352. return false;
  353. }
  354. $chid = (int)array_shift($args);
  355. if (!$book = $DB->get_record('book', array('id'=>$cm->instance))) {
  356. return false;
  357. }
  358. if (!$chapter = $DB->get_record('book_chapters', array('id'=>$chid, 'bookid'=>$book->id))) {
  359. return false;
  360. }
  361. if ($chapter->hidden and !has_capability('mod/book:viewhiddenchapters', $context)) {
  362. return false;
  363. }
  364. $fs = get_file_storage();
  365. $relativepath = implode('/', $args);
  366. $fullpath = "/$context->id/mod_book/chapter/$chid/$relativepath";
  367. if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
  368. return false;
  369. }
  370. // finally send the file
  371. send_stored_file($file, 360, 0, $forcedownload, $options);
  372. }
  373. /**
  374. * Return a list of page types
  375. *
  376. * @param string $pagetype current page type
  377. * @param stdClass $parentcontext Block's parent context
  378. * @param stdClass $currentcontext Current context of block
  379. * @return array
  380. */
  381. function book_page_type_list($pagetype, $parentcontext, $currentcontext) {
  382. $module_pagetype = array('mod-book-*'=>get_string('page-mod-book-x', 'mod_book'));
  383. return $module_pagetype;
  384. }