2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * mod_wiki data generator.
22 * @copyright 2013 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 * mod_wiki data generator class.
33 * @copyright 2013 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class mod_wiki_generator extends testing_module_generator {
39 * @var int keep track of how many pages have been created.
41 protected $pagecount = 0;
44 * To be called from data reset code only,
45 * do not use in tests.
48 public function reset() {
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',
62 return parent::create_instance($record, (array)$options);
65 public function create_content($wiki, $record = array()) {
66 $record = (array)$record + array(
69 return $this->create_page($wiki, $record);
72 public function create_first_page($wiki, $record = array()) {
73 $record = (array)$record + array(
74 'title' => $wiki->firstpagetitle,
76 return $this->create_page($wiki, $record);
80 * Generates a page in wiki.
82 * @param stdClass wiki object returned from create_instance (if known)
83 * @param stdClass|array $record data to insert as wiki entry.
85 * @throws coding_exception if neither $record->wikiid nor $wiki->id is specified
87 public function create_page($wiki, $record = array()) {
89 require_once($CFG->dirroot.'/mod/wiki/locallib.php');
91 $record = (array)$record + array(
92 'title' => 'wiki page '.$this->pagecount,
93 'wikiid' => $wiki->id,
96 'content' => 'Wiki page content '.$this->pagecount,
97 'format' => $wiki->defaultformat
99 if (empty($record['wikiid']) && empty($record['subwikiid'])) {
100 throw new coding_exception('wiki page generator requires either wikiid or subwikiid');
102 if (!$record['subwikiid']) {
103 if (!isset($record['userid'])) {
104 $record['userid'] = ($wiki->wikimode == 'individual') ? $USER->id : 0;
106 if ($subwiki = wiki_get_subwiki_by_group($record['wikiid'], $record['group'], $record['userid'])) {
107 $record['subwikiid'] = $subwiki->id;
109 $record['subwikiid'] = wiki_add_subwiki($record['wikiid'], $record['group'], $record['userid']);
113 $wikipage = wiki_get_page_by_title($record['subwikiid'], $record['title']);
115 $pageid = wiki_create_page($record['subwikiid'], $record['title'], $record['format'], $USER->id);
116 $wikipage = wiki_get_page($pageid);
118 $rv = wiki_save_page($wikipage, $record['content'], $USER->id);
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;
126 core_tag_tag::set_item_tags('mod_wiki', 'wiki_pages', $wikipage->id,
127 context_module::instance($wiki->cmid), $tags);