lib.php 10.3 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
33
34
35
36
37
38
39
40
41
42
43
44

define('BOOK_NUM_NONE',     '0');
define('BOOK_NUM_NUMBERS',  '1');
define('BOOK_NUM_BULLETS',  '2');
define('BOOK_NUM_INDENTED', '3');

/**
 * Returns list of available numbering types
 * @return array
 */
function book_get_numbering_types() {
    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
45
46
47
48
49
50
51
/**
 * Add book instance.
 *
 * @param stdClass $data
 * @param stdClass $mform
 * @return int new book instance id
 */
Petr Skoda's avatar
Petr Skoda committed
52
function book_add_instance($book) {
Petr Skoda's avatar
Petr Skoda committed
53
    global $DB;
Petr Skoda's avatar
Petr Skoda committed
54
55
56

    $book->timecreated = time();
    $book->timemodified = $book->timecreated;
57
58
59
    if (!isset($book->customtitles)) {
        $book->customtitles = 0;
    }
Petr Skoda's avatar
Petr Skoda committed
60

Petr Skoda's avatar
Petr Skoda committed
61
    return $DB->insert_record('book', $book);
Petr Skoda's avatar
Petr Skoda committed
62
63
}

Petr Skoda's avatar
Petr Skoda committed
64
65
66
67
68
69
70
/**
 * Update book instance.
 *
 * @param stdClass $data
 * @param stdClass $mform
 * @return bool true
 */
Petr Skoda's avatar
Petr Skoda committed
71
function book_update_instance($book) {
Petr Skoda's avatar
Petr Skoda committed
72
    global $DB;
Petr Skoda's avatar
Petr Skoda committed
73
74
75

    $book->timemodified = time();
    $book->id = $book->instance;
76
77
78
    if (!isset($book->customtitles)) {
        $book->customtitles = 0;
    }
Petr Skoda's avatar
Petr Skoda committed
79

Petr Skoda's avatar
Petr Skoda committed
80
    $DB->update_record('book', $book);
Petr Skoda's avatar
Petr Skoda committed
81

Petr Skoda's avatar
Petr Skoda committed
82
    return true;
Petr Skoda's avatar
Petr Skoda committed
83
84
}

Petr Skoda's avatar
Petr Skoda committed
85
86
87
88
89
90
91
92
93
/**
 *
 * 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
94
function book_delete_instance($id) {
Petr Skoda's avatar
Petr Skoda committed
95
    global $DB;
Petr Skoda's avatar
Petr Skoda committed
96

Petr Skoda's avatar
Petr Skoda committed
97
    if (!$book = $DB->get_record('book', array('id'=>$id))) {
Petr Skoda's avatar
Petr Skoda committed
98
99
100
        return false;
    }

Petr Skoda's avatar
Petr Skoda committed
101
102
    $DB->delete_records('book_chapters', array('bookid'=>$book->id));
    $DB->delete_records('book', array('id'=>$book->id));
Petr Skoda's avatar
Petr Skoda committed
103

Petr Skoda's avatar
Petr Skoda committed
104
    return true;
Petr Skoda's avatar
Petr Skoda committed
105
106
}

107
function book_get_types() {
Petr Skoda's avatar
Petr Skoda committed
108
    $type = new stdClass();
109
    $type->modclass = MOD_CLASS_RESOURCE;
Petr Skoda's avatar
Petr Skoda committed
110
    $type->type     = 'book';
111
    $type->typestr  = get_string('modulename', 'mod_book');
112

Petr Skoda's avatar
Petr Skoda committed
113
    return array($type);
114
}
Petr Skoda's avatar
Petr Skoda committed
115

Petr Skoda's avatar
Petr Skoda committed
116
117
118
119
120
121
122
123
/**
 * Return use outline
 * @param object $course
 * @param object $user
 * @param object $mod
 * @param object $page
 * @return object|null
 */
Petr Skoda's avatar
Petr Skoda committed
124
function book_user_outline($course, $user, $mod, $book) {
Petr Skoda's avatar
Petr Skoda committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    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
140
141
142
}

function book_user_complete($course, $user, $mod, $book) {
Petr Skoda's avatar
Petr Skoda committed
143
144
    // Print a detailed representation of what a  user has done with
    // a given particular instance of this module, for user activity reports.
Petr Skoda's avatar
Petr Skoda committed
145
146
147
148
149

    return true;
}

function book_print_recent_activity($course, $isteacher, $timestart) {
Petr Skoda's avatar
Petr Skoda committed
150
151
152
    // 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.
Petr Skoda's avatar
Petr Skoda committed
153
154
155
156
    return false;  //  True if anything was printed, otherwise false
}

function book_cron () {
Petr Skoda's avatar
Petr Skoda committed
157
158
159
    // Function to be run periodically according to the moodle cron
    // This function searches for things that need to be done, such
    // as sending out mail, toggling flags etc ...
Petr Skoda's avatar
Petr Skoda committed
160
161
162
163
    return true;
}

function book_grades($bookid) {
Petr Skoda's avatar
Petr Skoda committed
164
165
    // Must return an array of grades for a given instance of this module,
    // indexed by user.  It also returns a maximum allowed grade.
Petr Skoda's avatar
Petr Skoda committed
166

Petr Skoda's avatar
Petr Skoda committed
167
    return null;
Petr Skoda's avatar
Petr Skoda committed
168
169
170
}

function book_get_participants($bookid) {
Petr Skoda's avatar
Petr Skoda committed
171
172
173
174
    //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
175
176
177
178

    return false;
}

179
180
181
/**
 * 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
182
 * modified if necessary. See book, glossary or journal modules
183
 * as reference.
Petr Skoda's avatar
Petr Skoda committed
184
 *
185
186
187
188
 * @param $bookid int
 * @param $scaleid int
 * @return boolean True if the scale is used by any journal
 */
Petr Skoda's avatar
Petr Skoda committed
189
function book_scale_used($bookid,$scaleid) {
190
    return false;
Petr Skoda's avatar
Petr Skoda committed
191
192
}

193
/**
Petr Skoda's avatar
Petr Skoda committed
194
 * Checks if scale is being used by any instance of book
195
196
 *
 * This is used to find out if scale used anywhere
Petr Skoda's avatar
Petr Skoda committed
197
 *
198
199
200
 * @param $scaleid int
 * @return boolean True if the scale is used by any journal
 */
Petr Skoda's avatar
Petr Skoda committed
201
function book_scale_used_anywhere($scaleid) {
202
203
    return false;
}
204
205
206
207
208
209
210
211

function book_get_view_actions() {
    return array('view', 'view all', 'print');
}

function book_get_post_actions() {
    return array('update');
}
Petr Skoda's avatar
Petr Skoda committed
212
213
214
215
216
217
218
219
220
221
222
223
224

/**
 * 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) {
        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
225
        case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
Petr Skoda's avatar
Petr Skoda committed
226
        case FEATURE_BACKUP_MOODLE2:          return true;
Petr Skoda's avatar
Petr Skoda committed
227
228
229
230

        default: return null;
    }
}
Petr Skoda's avatar
Petr Skoda committed
231
232
233
234
235

/**
 * Adds module specific settings to the settings block
 *
 * @param settings_navigation $settings The settings navigation object
Petr Skoda's avatar
Petr Skoda committed
236
 * @param navigation_node $booknode The node to add module settings to
Petr Skoda's avatar
Petr Skoda committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
 * @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();

    if (empty($params['id']) or empty($params['chapterid'])) {
        return;
    }

257
    if (has_capability('mod/book:print', $PAGE->cm->context)) {
258
        $url = new moodle_url('/mod/book/print.php', array('id'=>$params['id']));
259
        $booknode->add(get_string('printbook', 'mod_book'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('print_book', '', 'mod_book', array('class'=>'icon')));
260
261
262
263
264
265

        // buggy navlib - actions not supported!!
        //$action = new action_link($url, get_string('printbook', 'book'), new popup_action('onclick', $url));
        //$booknode->add('', $action, navigation_node::TYPE_SETTING);

        $url = new moodle_url('/mod/book/print.php', array('id'=>$params['id'], 'chapterid'=>$params['chapterid']));
266
        $booknode->add(get_string('printchapter', 'mod_book'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('print_chapter', '', 'mod_book', array('class'=>'icon')));
Petr Skoda's avatar
Petr Skoda committed
267
268
    }

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
    if (has_capability('mod/book:import', $PAGE->cm->context)) {
        //TODO
        //$doimport = ($allowimport and $edit) ? '<div>(<a href="import.php?id='.$cm->id.'">'.get_string('doimport', 'book').'</a>)</div>' : '';
    }

    if (has_capability('mod/book:exportimscp', $PAGE->cm->context)) {
        //TODO
        /// Enable the IMS CP button
        //$generateimscp = ($allowexport) ? '<a title="'.get_string('generateimscp', 'book').'" href="generateimscp.php?id='.$cm->id.'"><img class="bigicon" src="'.$OUTPUT->pix_url('generateimscp', 'mod_book').'" alt="'.get_string('generateimscp', 'book').'"></img></a>' : '';
    }

    if (has_capability('mod/book:edit', $PAGE->cm->context)) {
        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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
}

/**
 * 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
 * @return bool false if file not found, does not return if found - justsend the file
 */
function book_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
    global $CFG, $DB;

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