lib.php 12.1 KB
Newer Older
1
<?php
2
// This file is part of Moodle - http://moodle.org/
Petr Skoda's avatar
Petr Skoda committed
3
4
5
6
7
8
9
10
11
12
13
14
15
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Petr Skoda's avatar
Petr Skoda committed
16

Petr Skoda's avatar
Petr Skoda committed
17
18
19
/**
 * Book module core interaction API
 *
20
21
 * @package    mod_book
 * @copyright  2004-2011 Petr Skoda {@link http://skodak.org}
Petr Skoda's avatar
Petr Skoda committed
22
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
Petr Skoda's avatar
Petr Skoda committed
24

Petr Skoda's avatar
Petr Skoda committed
25
defined('MOODLE_INTERNAL') || die;
Petr Skoda's avatar
Petr Skoda committed
26

27
28
29
30
31
/**
 * Returns list of available numbering types
 * @return array
 */
function book_get_numbering_types() {
32
    global $CFG; // required for the include
33

34
35
    require_once(dirname(__FILE__).'/locallib.php');

36
37
38
39
40
41
    return array (
        BOOK_NUM_NONE       => get_string('numbering0', 'mod_book'),
        BOOK_NUM_NUMBERS    => get_string('numbering1', 'mod_book'),
        BOOK_NUM_BULLETS    => get_string('numbering2', 'mod_book'),
        BOOK_NUM_INDENTED   => get_string('numbering3', 'mod_book')
    );
42
43
}

Petr Skoda's avatar
Petr Skoda committed
44
45
46
47
48
49
50
51
52
/**
 * Returns all other caps used in module
 * @return array
 */
function book_get_extra_capabilities() {
    // used for group-members-only
    return array('moodle/site:accessallgroups');
}

Petr Skoda's avatar
Petr Skoda committed
53
54
55
56
57
58
59
/**
 * Add book instance.
 *
 * @param stdClass $data
 * @param stdClass $mform
 * @return int new book instance id
 */
60
function book_add_instance($data, $mform) {
Petr Skoda's avatar
Petr Skoda committed
61
    global $DB;
Petr Skoda's avatar
Petr Skoda committed
62

63
64
65
66
    $data->timecreated = time();
    $data->timemodified = $data->timecreated;
    if (!isset($data->customtitles)) {
        $data->customtitles = 0;
67
    }
Petr Skoda's avatar
Petr Skoda committed
68

69
    return $DB->insert_record('book', $data);
Petr Skoda's avatar
Petr Skoda committed
70
71
}

Petr Skoda's avatar
Petr Skoda committed
72
73
74
75
76
77
78
/**
 * Update book instance.
 *
 * @param stdClass $data
 * @param stdClass $mform
 * @return bool true
 */
79
function book_update_instance($data, $mform) {
Petr Skoda's avatar
Petr Skoda committed
80
    global $DB;
Petr Skoda's avatar
Petr Skoda committed
81

82
83
84
85
    $data->timemodified = time();
    $data->id = $data->instance;
    if (!isset($data->customtitles)) {
        $data->customtitles = 0;
86
    }
Petr Skoda's avatar
Petr Skoda committed
87

88
    $DB->update_record('book', $data);
Petr Skoda's avatar
Petr Skoda committed
89

90
91
92
    $book = $DB->get_record('book', array('id'=>$data->id));
    $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));

Petr Skoda's avatar
Petr Skoda committed
93
    return true;
Petr Skoda's avatar
Petr Skoda committed
94
95
}

Petr Skoda's avatar
Petr Skoda committed
96
/**
97
 * Delete book instance by activity id
Petr Skoda's avatar
Petr Skoda committed
98
99
100
101
 *
 * @param int $id
 * @return bool success
 */
Petr Skoda's avatar
Petr Skoda committed
102
function book_delete_instance($id) {
Petr Skoda's avatar
Petr Skoda committed
103
    global $DB;
Petr Skoda's avatar
Petr Skoda committed
104

Petr Skoda's avatar
Petr Skoda committed
105
    if (!$book = $DB->get_record('book', array('id'=>$id))) {
Petr Skoda's avatar
Petr Skoda committed
106
107
108
        return false;
    }

Petr Skoda's avatar
Petr Skoda committed
109
110
    $DB->delete_records('book_chapters', array('bookid'=>$book->id));
    $DB->delete_records('book', array('id'=>$book->id));
Petr Skoda's avatar
Petr Skoda committed
111

Petr Skoda's avatar
Petr Skoda committed
112
    return true;
Petr Skoda's avatar
Petr Skoda committed
113
114
}

115
116
117
/**
 * Given a course and a time, this module should find recent activity
 * that has occurred in book activities and print it out.
118
119
120
121
122
 *
 * @param stdClass $course
 * @param bool $viewfullnames
 * @param int $timestart
 * @return bool true if there was output, or false is there was none
123
 */
124
function book_print_recent_activity($course, $viewfullnames, $timestart) {
Petr Skoda's avatar
Petr Skoda committed
125
126
127
    return false;  //  True if anything was printed, otherwise false
}

128
129
130
131
132
133
134
135
136
/**
 * This function is used by the reset_course_userdata function in moodlelib.
 * @param $data the data submitted from the reset course.
 * @return array status array
 */
function book_reset_userdata($data) {
    return array();
}

137
138
139
140
141
/**
 * No cron in book.
 *
 * @return bool
 */
Petr Skoda's avatar
Petr Skoda committed
142
143
144
145
function book_cron () {
    return true;
}

146
147
148
/**
 * No grading in book.
 *
149
 * @param int $bookid
150
151
 * @return null
 */
Petr Skoda's avatar
Petr Skoda committed
152
function book_grades($bookid) {
Petr Skoda's avatar
Petr Skoda committed
153
    return null;
Petr Skoda's avatar
Petr Skoda committed
154
155
}

156
157
158
/**
 * This function returns if a scale is being used by one book
 * it it has support for grading and scales. Commented code should be
Petr Skoda's avatar
Petr Skoda committed
159
 * modified if necessary. See book, glossary or journal modules
160
 * as reference.
Petr Skoda's avatar
Petr Skoda committed
161
 *
162
163
 * @param int $bookid
 * @param int $scaleid
164
165
 * @return boolean True if the scale is used by any journal
 */
166
function book_scale_used($bookid, $scaleid) {
167
    return false;
Petr Skoda's avatar
Petr Skoda committed
168
169
}

170
/**
Petr Skoda's avatar
Petr Skoda committed
171
 * Checks if scale is being used by any instance of book
172
173
 *
 * This is used to find out if scale used anywhere
Petr Skoda's avatar
Petr Skoda committed
174
 *
175
176
 * @param int $scaleid
 * @return bool true if the scale is used by any book
177
 */
Petr Skoda's avatar
Petr Skoda committed
178
function book_scale_used_anywhere($scaleid) {
179
180
    return false;
}
181

182
183
/**
 * Return read actions.
184
185
186
187
188
 *
 * Note: This is not used by new logging system. Event with
 *       crud = 'r' and edulevel = LEVEL_PARTICIPATING will
 *       be considered as view action.
 *
189
190
 * @return array
 */
191
function book_get_view_actions() {
192
    global $CFG; // necessary for includes
193

194
    $return = array('view', 'view all');
195

196
    $plugins = core_component::get_plugin_list('booktool');
197
    foreach ($plugins as $plugin => $dir) {
198
199
200
201
202
        if (file_exists("$dir/lib.php")) {
            require_once("$dir/lib.php");
        }
        $function = 'booktool_'.$plugin.'_get_view_actions';
        if (function_exists($function)) {
203
            if ($actions = $function()) {
204
205
206
207
208
209
                $return = array_merge($return, $actions);
            }
        }
    }

    return $return;
210
211
}

212
213
/**
 * Return write actions.
214
215
216
217
218
 *
 * Note: This is not used by new logging system. Event with
 *       crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
 *       will be considered as post action.
 *
219
220
 * @return array
 */
221
function book_get_post_actions() {
222
    global $CFG; // necessary for includes
223

224
    $return = array('update');
225

226
    $plugins = core_component::get_plugin_list('booktool');
227
    foreach ($plugins as $plugin => $dir) {
228
229
230
231
232
        if (file_exists("$dir/lib.php")) {
            require_once("$dir/lib.php");
        }
        $function = 'booktool_'.$plugin.'_get_post_actions';
        if (function_exists($function)) {
Petr Skoda's avatar
Petr Skoda committed
233
            if ($actions = $function()) {
234
235
236
237
238
239
                $return = array_merge($return, $actions);
            }
        }
    }

    return $return;
240
}
Petr Skoda's avatar
Petr Skoda committed
241
242
243
244
245
246
247
248
249

/**
 * Supported features
 *
 * @param string $feature FEATURE_xx constant for requested feature
 * @return mixed True if module supports feature, false if not, null if doesn't know
 */
function book_supports($feature) {
    switch($feature) {
250
        case FEATURE_MOD_ARCHETYPE:           return MOD_ARCHETYPE_RESOURCE;
Petr Skoda's avatar
Petr Skoda committed
251
252
253
        case FEATURE_GROUPS:                  return false;
        case FEATURE_GROUPINGS:               return false;
        case FEATURE_MOD_INTRO:               return true;
Petr Skoda's avatar
Petr Skoda committed
254
        case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
255
256
        case FEATURE_GRADE_HAS_GRADE:         return false;
        case FEATURE_GRADE_OUTCOMES:          return false;
Petr Skoda's avatar
Petr Skoda committed
257
        case FEATURE_BACKUP_MOODLE2:          return true;
258
        case FEATURE_SHOW_DESCRIPTION:        return true;
Petr Skoda's avatar
Petr Skoda committed
259
260
261
262

        default: return null;
    }
}
Petr Skoda's avatar
Petr Skoda committed
263
264
265
266

/**
 * Adds module specific settings to the settings block
 *
267
 * @param settings_navigation $settingsnav The settings navigation object
Petr Skoda's avatar
Petr Skoda committed
268
 * @param navigation_node $booknode The node to add module settings to
Petr Skoda's avatar
Petr Skoda committed
269
270
271
 * @return void
 */
function book_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $booknode) {
272
    global $USER, $PAGE;
Petr Skoda's avatar
Petr Skoda committed
273

274
    $plugins = core_component::get_plugin_list('booktool');
275
    foreach ($plugins as $plugin => $dir) {
276
277
278
279
280
281
        if (file_exists("$dir/lib.php")) {
            require_once("$dir/lib.php");
        }
        $function = 'booktool_'.$plugin.'_extend_settings_navigation';
        if (function_exists($function)) {
            $function($settingsnav, $booknode);
Petr Skoda's avatar
Petr Skoda committed
282
        }
Petr Skoda's avatar
Petr Skoda committed
283
284
    }

Petr Skoda's avatar
Petr Skoda committed
285
286
287
    $params = $PAGE->url->params();

    if (!empty($params['id']) and !empty($params['chapterid']) and has_capability('mod/book:edit', $PAGE->cm->context)) {
288
289
290
291
292
293
294
295
296
297
        if (!empty($USER->editing)) {
            $string = get_string("turneditingoff");
            $edit = '0';
        } else {
            $string = get_string("turneditingon");
            $edit = '1';
        }
        $url = new moodle_url('/mod/book/view.php', array('id'=>$params['id'], 'chapterid'=>$params['chapterid'], 'edit'=>$edit, 'sesskey'=>sesskey()));
        $booknode->add($string, $url, navigation_node::TYPE_SETTING);
    }
Petr Skoda's avatar
Petr Skoda committed
298
299
}

Petr Skoda's avatar
Petr Skoda committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

/**
 * Lists all browsable file areas
 * @param object $course
 * @param object $cm
 * @param object $context
 * @return array
 */
function book_get_file_areas($course, $cm, $context) {
    $areas = array();
    $areas['chapter'] = get_string('chapters', 'mod_book');
    return $areas;
}

/**
315
 * File browsing support for book module chapter area.
Petr Skoda's avatar
Petr Skoda committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
 * @param object $browser
 * @param object $areas
 * @param object $course
 * @param object $cm
 * @param object $context
 * @param string $filearea
 * @param int $itemid
 * @param string $filepath
 * @param string $filename
 * @return object file_info instance or null if not found
 */
function book_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
    global $CFG, $DB;

330
    // note: 'intro' area is handled in file_browser automatically
Petr Skoda's avatar
Petr Skoda committed
331
332
333
334
335
336
337
338
339

    if (!has_capability('mod/book:read', $context)) {
        return null;
    }

    if ($filearea !== 'chapter') {
        return null;
    }

340
    require_once(dirname(__FILE__).'/locallib.php');
Petr Skoda's avatar
Petr Skoda committed
341
342

    if (is_null($itemid)) {
343
        return new book_file_info($browser, $course, $cm, $context, $areas, $filearea);
Petr Skoda's avatar
Petr Skoda committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
    }

    $fs = get_file_storage();
    $filepath = is_null($filepath) ? '/' : $filepath;
    $filename = is_null($filename) ? '.' : $filename;
    if (!$storedfile = $fs->get_file($context->id, 'mod_book', $filearea, $itemid, $filepath, $filename)) {
        return null;
    }

    // modifications may be tricky - may cause caching problems
    $canwrite = has_capability('mod/book:edit', $context);

    $chaptername = $DB->get_field('book_chapters', 'title', array('bookid'=>$cm->instance, 'id'=>$itemid));
    $chaptername = format_string($chaptername, true, array('context'=>$context));

    $urlbase = $CFG->wwwroot.'/pluginfile.php';
    return new file_info_stored($browser, $context, $storedfile, $urlbase, $chaptername, true, true, $canwrite, false);
}

Petr Skoda's avatar
Petr Skoda committed
363
364
365
/**
 * Serves the book attachments. Implements needed access control ;-)
 *
366
367
368
369
370
371
372
 * @param stdClass $course course object
 * @param cm_info $cm course module object
 * @param context $context context object
 * @param string $filearea file area
 * @param array $args extra arguments
 * @param bool $forcedownload whether or not force download
 * @param array $options additional options affecting the file serving
373
 * @return bool false if file not found, does not return if found - just send the file
Petr Skoda's avatar
Petr Skoda committed
374
 */
375
function book_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
376
    global $CFG, $DB;
Petr Skoda's avatar
Petr Skoda committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412

    if ($context->contextlevel != CONTEXT_MODULE) {
        return false;
    }

    require_course_login($course, true, $cm);

    if ($filearea !== 'chapter') {
        return false;
    }

    if (!has_capability('mod/book:read', $context)) {
        return false;
    }

    $chid = (int)array_shift($args);

    if (!$book = $DB->get_record('book', array('id'=>$cm->instance))) {
        return false;
    }

    if (!$chapter = $DB->get_record('book_chapters', array('id'=>$chid, 'bookid'=>$book->id))) {
        return false;
    }

    if ($chapter->hidden and !has_capability('mod/book:viewhiddenchapters', $context)) {
        return false;
    }

    $fs = get_file_storage();
    $relativepath = implode('/', $args);
    $fullpath = "/$context->id/mod_book/chapter/$chid/$relativepath";
    if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
        return false;
    }

413
414
415
416
417
418
    // Nasty hack because we do not have file revisions in book yet.
    $lifetime = $CFG->filelifetime;
    if ($lifetime > 60*10) {
        $lifetime = 60*10;
    }

Petr Skoda's avatar
Petr Skoda committed
419
    // finally send the file
420
    send_stored_file($file, $lifetime, 0, $forcedownload, $options);
Petr Skoda's avatar
Petr Skoda committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
}

/**
 * Return a list of page types
 *
 * @param string $pagetype current page type
 * @param stdClass $parentcontext Block's parent context
 * @param stdClass $currentcontext Current context of block
 * @return array
 */
function book_page_type_list($pagetype, $parentcontext, $currentcontext) {
    $module_pagetype = array('mod-book-*'=>get_string('page-mod-book-x', 'mod_book'));
    return $module_pagetype;
}