lib.php 12.7 KB
Newer Older
1
<?php
Petr Skoda's avatar
Petr Skoda committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// This file is part of Book module for Moodle - http://moodle.org/
//
// 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
20
21
/**
 * Book module core interaction API
 *
 * @package    mod
 * @subpackage book
22
 * @copyright  2004-2011 Petr Skoda  {@link http://skodak.org}
Petr Skoda's avatar
Petr Skoda committed
23
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
Petr Skoda's avatar
Petr Skoda committed
25

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

28
29
30
31
32
/**
 * Returns list of available numbering types
 * @return array
 */
function book_get_numbering_types() {
33
34
35
    global $CFG; // required for the include
    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') );
}

Petr Skoda's avatar
Petr Skoda committed
42
43
44
45
46
47
48
49
50
/**
 * 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
51
52
53
54
55
56
57
/**
 * Add book instance.
 *
 * @param stdClass $data
 * @param stdClass $mform
 * @return int new book instance id
 */
58
function book_add_instance($data, $mform) {
Petr Skoda's avatar
Petr Skoda committed
59
    global $DB;
Petr Skoda's avatar
Petr Skoda committed
60

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

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

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

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

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

Petr Skoda's avatar
Petr Skoda committed
88
    return true;
Petr Skoda's avatar
Petr Skoda committed
89
90
}

Petr Skoda's avatar
Petr Skoda committed
91
92
93
94
95
96
97
98
99
/**
 *
 * Given an ID of an instance of this module,
 * this function will permanently delete the instance
 * and any data that depends on it.
 *
 * @param int $id
 * @return bool success
 */
Petr Skoda's avatar
Petr Skoda committed
100
function book_delete_instance($id) {
Petr Skoda's avatar
Petr Skoda committed
101
    global $DB;
Petr Skoda's avatar
Petr Skoda committed
102

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

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

Petr Skoda's avatar
Petr Skoda committed
110
    return true;
Petr Skoda's avatar
Petr Skoda committed
111
112
}

Petr Skoda's avatar
Petr Skoda committed
113
114
/**
 * Return use outline
115
116
117
118
119
 *
 * @param stdClass $course
 * @param stdClass $user
 * @param stdClass $mod
 * @param object $book
Petr Skoda's avatar
Petr Skoda committed
120
121
 * @return object|null
 */
Petr Skoda's avatar
Petr Skoda committed
122
function book_user_outline($course, $user, $mod, $book) {
Petr Skoda's avatar
Petr Skoda committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    global $DB;

    if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'book',
                                              'action'=>'view', 'info'=>$book->id), 'time ASC')) {

        $numviews = count($logs);
        $lastlog = array_pop($logs);

        $result = new stdClass();
        $result->info = get_string('numviews', '', $numviews);
        $result->time = $lastlog->time;

        return $result;
    }
    return NULL;
Petr Skoda's avatar
Petr Skoda committed
138
139
}

140
141
142
143
144
145
146
147
148
149
/**
 * Print a detailed representation of what a  user has done with
 * a given particular instance of this module, for user activity reports.
 *
 * @param $course
 * @param $user
 * @param $mod
 * @param $book
 * @return bool
 */
Petr Skoda's avatar
Petr Skoda committed
150
151
152
153
function book_user_complete($course, $user, $mod, $book) {
    return true;
}

154
155
156
157
158
159
160
161
162
/**
 * Given a course and a time, this module should find recent activity
 * that has occurred in book activities and print it out.
 * Return true if there was output, or false is there was none.
 * @param $course
 * @param $isteacher
 * @param $timestart
 * @return bool
 */
Petr Skoda's avatar
Petr Skoda committed
163
164
165
166
function book_print_recent_activity($course, $isteacher, $timestart) {
    return false;  //  True if anything was printed, otherwise false
}

167
168
169
170
171
/**
 * No cron in book.
 *
 * @return bool
 */
Petr Skoda's avatar
Petr Skoda committed
172
173
174
175
function book_cron () {
    return true;
}

176
177
178
179
180
181
/**
 * No grading in book.
 *
 * @param $bookid
 * @return null
 */
Petr Skoda's avatar
Petr Skoda committed
182
function book_grades($bookid) {
Petr Skoda's avatar
Petr Skoda committed
183
    return null;
Petr Skoda's avatar
Petr Skoda committed
184
185
186
}

function book_get_participants($bookid) {
Petr Skoda's avatar
Petr Skoda committed
187
188
189
190
    //Must return an array of user records (all data) who are participants
    //for a given instance of book. Must include every user involved
    //in the instance, independent of his role (student, teacher, admin...)
    //See other modules as example.
Petr Skoda's avatar
Petr Skoda committed
191
192
193
194

    return false;
}

195
196
197
/**
 * 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
198
 * modified if necessary. See book, glossary or journal modules
199
 * as reference.
Petr Skoda's avatar
Petr Skoda committed
200
 *
201
202
203
204
 * @param $bookid int
 * @param $scaleid int
 * @return boolean True if the scale is used by any journal
 */
Petr Skoda's avatar
Petr Skoda committed
205
function book_scale_used($bookid,$scaleid) {
206
    return false;
Petr Skoda's avatar
Petr Skoda committed
207
208
}

209
/**
Petr Skoda's avatar
Petr Skoda committed
210
 * Checks if scale is being used by any instance of book
211
212
 *
 * This is used to find out if scale used anywhere
Petr Skoda's avatar
Petr Skoda committed
213
 *
214
215
216
 * @param $scaleid int
 * @return boolean True if the scale is used by any journal
 */
Petr Skoda's avatar
Petr Skoda committed
217
function book_scale_used_anywhere($scaleid) {
218
219
    return false;
}
220

221
222
223
224
/**
 * Return read actions.
 * @return array
 */
225
function book_get_view_actions() {
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    global $CFG; // necessary for includes

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

    $plugins = get_plugin_list('booktool');
    foreach($plugins as $plugin=>$dir) {
        if (file_exists("$dir/lib.php")) {
            require_once("$dir/lib.php");
        }
        $function = 'booktool_'.$plugin.'_get_view_actions';
        if (function_exists($function)) {
            if ($actions = $function($settingsnav, $booknode)) {
                $return = array_merge($return, $actions);
            }
        }
    }

    return $return;
244
245
}

246
247
248
249
/**
 * Return write actions.
 * @return array
 */
250
function book_get_post_actions() {
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    global $CFG; // necessary for includes

    $return = array('update');

    $plugins = get_plugin_list('booktool');
    foreach($plugins as $plugin=>$dir) {
        if (file_exists("$dir/lib.php")) {
            require_once("$dir/lib.php");
        }
        $function = 'booktool_'.$plugin.'_get_post_actions';
        if (function_exists($function)) {
            if ($actions = $function($settingsnav, $booknode)) {
                $return = array_merge($return, $actions);
            }
        }
    }

    return $return;
269
}
Petr Skoda's avatar
Petr Skoda committed
270
271
272
273
274
275
276
277
278

/**
 * 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) {
279
        case FEATURE_MOD_ARCHETYPE:           return MOD_ARCHETYPE_RESOURCE;
Petr Skoda's avatar
Petr Skoda committed
280
281
282
283
        case FEATURE_GROUPS:                  return false;
        case FEATURE_GROUPINGS:               return false;
        case FEATURE_GROUPMEMBERSONLY:        return true;
        case FEATURE_MOD_INTRO:               return true;
Petr Skoda's avatar
Petr Skoda committed
284
        case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
285
286
        case FEATURE_GRADE_HAS_GRADE:         return false;
        case FEATURE_GRADE_OUTCOMES:          return false;
Petr Skoda's avatar
Petr Skoda committed
287
        case FEATURE_BACKUP_MOODLE2:          return true;
Petr Skoda's avatar
Petr Skoda committed
288
289
290
291

        default: return null;
    }
}
Petr Skoda's avatar
Petr Skoda committed
292
293
294
295

/**
 * Adds module specific settings to the settings block
 *
296
 * @param settings_navigation $settingsnav The settings navigation object
Petr Skoda's avatar
Petr Skoda committed
297
 * @param navigation_node $booknode The node to add module settings to
Petr Skoda's avatar
Petr Skoda committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
 * @return void
 */
function book_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $booknode) {
    global $USER, $PAGE, $CFG, $DB, $OUTPUT;

    if ($PAGE->cm->modname !== 'book') {
        return;
    }

    if (empty($PAGE->cm->context)) {
        $PAGE->cm->context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->instance);
    }


    $params = $PAGE->url->params();

314
    if (empty($params['id'])) {
Petr Skoda's avatar
Petr Skoda committed
315
316
317
        return;
    }

318
319
320
321
322
323
324
325
    $plugins = get_plugin_list('booktool');
    foreach($plugins as $plugin=>$dir) {
        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
326
        }
Petr Skoda's avatar
Petr Skoda committed
327
328
    }

329
    if (!empty($params['chapterid']) and has_capability('mod/book:edit', $PAGE->cm->context)) {
330
331
332
333
334
335
336
337
338
339
        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
340
341
}

Petr Skoda's avatar
Petr Skoda committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

/**
 * 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;
}

/**
 * File browsing support for book module ontent area.
 * @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;

372
    // note: 'intro' area is handled in file_browser automatically
Petr Skoda's avatar
Petr Skoda committed
373
374
375
376
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

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

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

    require_once("$CFG->dirroot/mod/book/locallib.php");

    if (is_null($itemid)) {
        return new book_file_info($browser, $course, $cm, $context, $areas, $filearea, $itemid);
    }

    $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
405
406
407
408
409
410
411
412
413
/**
 * Serves the book attachments. Implements needed access control ;-)
 *
 * @param object $course
 * @param object $cm
 * @param object $context
 * @param string $filearea
 * @param array $args
 * @param bool $forcedownload
414
 * @return bool false if file not found, does not return if found - just send the file
Petr Skoda's avatar
Petr Skoda committed
415
416
 */
function book_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
417
    global $DB;
Petr Skoda's avatar
Petr Skoda committed
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455

    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;
    }

    // finally send the file
    send_stored_file($file, 360, 0, false);
Petr Skoda's avatar
Petr Skoda committed
456
457
458
459
460
461
462
463
464
465
466
467
468
469
}

/**
 * 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;
}