Commit | Line | Data |
---|---|---|
20dff4b9 MG |
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_wiki data generator. | |
19 | * | |
20 | * @package mod_wiki | |
21 | * @category test | |
22 | * @copyright 2013 Marina Glancy | |
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_wiki data generator class. | |
30 | * | |
31 | * @package mod_wiki | |
32 | * @category test | |
33 | * @copyright 2013 Marina Glancy | |
34 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
35 | */ | |
36 | class mod_wiki_generator extends testing_module_generator { | |
37 | ||
38 | /** | |
39 | * @var int keep track of how many pages have been created. | |
40 | */ | |
41 | protected $pagecount = 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->pagecount = 0; | |
50 | parent::reset(); | |
51 | } | |
52 | ||
53 | public function create_instance($record = null, array $options = null) { | |
54 | // Add default values for wiki. | |
55 | $record = (array)$record + array( | |
56 | 'wikimode' => 'collaborative', | |
57 | 'firstpagetitle' => 'Front page for wiki '.($this->instancecount+1), | |
58 | 'defaultformat' => 'html', | |
59 | 'forceformat' => 0 | |
60 | ); | |
61 | ||
62 | return parent::create_instance($record, (array)$options); | |
63 | } | |
64 | ||
65 | public function create_content($wiki, $record = array()) { | |
66 | $record = (array)$record + array( | |
67 | 'wikiid' => $wiki->id | |
68 | ); | |
69 | return $this->create_page($wiki, $record); | |
70 | } | |
71 | ||
72 | public function create_first_page($wiki, $record = array()) { | |
73 | $record = (array)$record + array( | |
74 | 'title' => $wiki->firstpagetitle, | |
75 | ); | |
76 | return $this->create_page($wiki, $record); | |
77 | } | |
78 | ||
79 | /** | |
80 | * Generates a page in wiki. | |
81 | * | |
82 | * @param stdClass wiki object returned from create_instance (if known) | |
83 | * @param stdClass|array $record data to insert as wiki entry. | |
84 | * @return stdClass | |
85 | * @throws coding_exception if neither $record->wikiid nor $wiki->id is specified | |
86 | */ | |
87 | public function create_page($wiki, $record = array()) { | |
88 | global $CFG, $USER; | |
89 | require_once($CFG->dirroot.'/mod/wiki/locallib.php'); | |
90 | $this->pagecount++; | |
91 | $record = (array)$record + array( | |
92 | 'title' => 'wiki page '.$this->pagecount, | |
93 | 'wikiid' => $wiki->id, | |
94 | 'subwikiid' => 0, | |
95 | 'group' => 0, | |
96 | 'content' => 'Wiki page content '.$this->pagecount, | |
97 | 'format' => $wiki->defaultformat | |
98 | ); | |
99 | if (empty($record['wikiid']) && empty($record['subwikiid'])) { | |
100 | throw new coding_exception('wiki page generator requires either wikiid or subwikiid'); | |
101 | } | |
102 | if (!$record['subwikiid']) { | |
103 | if (!isset($record['userid'])) { | |
104 | $record['userid'] = ($wiki->wikimode == 'individual') ? $USER->id : 0; | |
105 | } | |
106 | if ($subwiki = wiki_get_subwiki_by_group($record['wikiid'], $record['group'], $record['userid'])) { | |
107 | $record['subwikiid'] = $subwiki->id; | |
108 | } else { | |
109 | $record['subwikiid'] = wiki_add_subwiki($record['wikiid'], $record['group'], $record['userid']); | |
110 | } | |
111 | } | |
112 | ||
92c90b96 MG |
113 | $wikipage = wiki_get_page_by_title($record['subwikiid'], $record['title']); |
114 | if (!$wikipage) { | |
115 | $pageid = wiki_create_page($record['subwikiid'], $record['title'], $record['format'], $USER->id); | |
116 | $wikipage = wiki_get_page($pageid); | |
20dff4b9 | 117 | } |
20dff4b9 | 118 | $rv = wiki_save_page($wikipage, $record['content'], $USER->id); |
92c90b96 MG |
119 | |
120 | if (array_key_exists('tags', $record)) { | |
121 | $tags = is_array($record['tags']) ? $record['tags'] : preg_split('/,/', $record['tags']); | |
122 | if (empty($wiki->cmid)) { | |
123 | $cm = get_coursemodule_from_instance('wiki', $wiki->id, isset($wiki->course) ? $wiki->course : 0); | |
124 | $wiki->cmid = $cm->id; | |
125 | } | |
126 | core_tag_tag::set_item_tags('mod_wiki', 'wiki_pages', $wikipage->id, | |
127 | context_module::instance($wiki->cmid), $tags); | |
128 | } | |
20dff4b9 MG |
129 | return $rv['page']; |
130 | } | |
131 | } |