1 |
efrain |
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 |
/**
|
|
|
18 |
* mod_book data generator.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_book
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright 2013 Frédéric Massart
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* mod_book data generator class.
|
|
|
30 |
*
|
|
|
31 |
* @package mod_book
|
|
|
32 |
* @category test
|
|
|
33 |
* @copyright 2013 Frédéric Massart
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class mod_book_generator extends testing_module_generator {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* @var int keep track of how many chapters have been created.
|
|
|
40 |
*/
|
|
|
41 |
protected $chaptercount = 0;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* To be called from data reset code only,
|
|
|
45 |
* do not use in tests.
|
|
|
46 |
* @return void
|
|
|
47 |
*/
|
|
|
48 |
public function reset() {
|
|
|
49 |
$this->chaptercount = 0;
|
|
|
50 |
parent::reset();
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public function create_instance($record = null, array $options = null) {
|
|
|
54 |
global $CFG;
|
|
|
55 |
require_once("$CFG->dirroot/mod/book/locallib.php");
|
|
|
56 |
|
|
|
57 |
$record = (object)(array)$record;
|
|
|
58 |
|
|
|
59 |
if (!isset($record->numbering)) {
|
|
|
60 |
$record->numbering = BOOK_NUM_NUMBERS;
|
|
|
61 |
}
|
|
|
62 |
if (!isset($record->customtitles)) {
|
|
|
63 |
$record->customtitles = 0;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
return parent::create_instance($record, (array)$options);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public function create_chapter($record = null, array $options = null) {
|
|
|
70 |
global $DB;
|
|
|
71 |
|
|
|
72 |
$record = (object) (array) $record;
|
|
|
73 |
$options = (array) $options;
|
|
|
74 |
$this->chaptercount++;
|
|
|
75 |
|
|
|
76 |
if (empty($record->bookid)) {
|
|
|
77 |
throw new coding_exception('Chapter generator requires $record->bookid');
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
if (empty($record->title)) {
|
|
|
81 |
$record->title = "Chapter {$this->chaptercount}";
|
|
|
82 |
}
|
|
|
83 |
if (empty($record->pagenum)) {
|
|
|
84 |
$record->pagenum = 1;
|
|
|
85 |
}
|
|
|
86 |
if (!isset($record->subchapter)) {
|
|
|
87 |
$record->subchapter = 0;
|
|
|
88 |
}
|
|
|
89 |
if (!isset($record->hidden)) {
|
|
|
90 |
$record->hidden = 0;
|
|
|
91 |
}
|
|
|
92 |
if (!isset($record->importsrc)) {
|
|
|
93 |
$record->importsrc = '';
|
|
|
94 |
}
|
|
|
95 |
if (!isset($record->content)) {
|
|
|
96 |
$record->content = "Chapter {$this->chaptercount} content";
|
|
|
97 |
}
|
|
|
98 |
if (!isset($record->contentformat)) {
|
|
|
99 |
$record->contentformat = FORMAT_MOODLE;
|
|
|
100 |
}
|
|
|
101 |
if (!isset($record->timecreated)) {
|
|
|
102 |
$record->timecreated = time();
|
|
|
103 |
}
|
|
|
104 |
if (!isset($record->timemodified)) {
|
|
|
105 |
$record->timemodified = time();
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
// Make room for new page.
|
|
|
109 |
$sql = "UPDATE {book_chapters}
|
|
|
110 |
SET pagenum = pagenum + 1
|
|
|
111 |
WHERE bookid = ? AND pagenum >= ?";
|
|
|
112 |
$DB->execute($sql, array($record->bookid, $record->pagenum));
|
|
|
113 |
$record->id = $DB->insert_record('book_chapters', $record);
|
|
|
114 |
|
|
|
115 |
$sql = "UPDATE {book}
|
|
|
116 |
SET revision = revision + 1
|
|
|
117 |
WHERE id = ?";
|
|
|
118 |
$DB->execute($sql, array($record->bookid));
|
|
|
119 |
|
|
|
120 |
if (property_exists($record, 'tags')) {
|
|
|
121 |
$cm = get_coursemodule_from_instance('book', $record->bookid);
|
|
|
122 |
$tags = is_array($record->tags) ? $record->tags : preg_split('/,/', $record->tags);
|
|
|
123 |
|
|
|
124 |
core_tag_tag::set_item_tags('mod_book', 'book_chapters', $record->id,
|
|
|
125 |
context_module::instance($cm->id), $tags);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
return $record;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public function create_content($instance, $record = array()) {
|
|
|
132 |
$record = (array)$record + array(
|
|
|
133 |
'bookid' => $instance->id
|
|
|
134 |
);
|
|
|
135 |
return $this->create_chapter($record);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
}
|