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 * Retrieves or generates a subwiki and returns its id 81 * 82 * @param stdClass $wiki 83 * @param int $subwikiid 84 * @param int $group 85 * @param int $userid 86 * @return int 87 */ 88 public function get_subwiki($wiki, $subwikiid = null, $group = null, $userid = null) { 89 global $USER, $DB; 90 91 if ($subwikiid) { 92 $params = ['id' => $subwikiid, 'wikiid' => $wiki->id]; 93 if ($group !== null) { 94 $params['group'] = $group; 95 } 96 if ($userid !== null) { 97 $params['userid'] = $userid; 98 } 99 return $DB->get_field('wiki_subwikis', 'id', $params, MUST_EXIST); 100 } 101 102 if ($userid === null) { 103 $userid = ($wiki->wikimode == 'individual') ? $USER->id : 0; 104 } 105 if ($group === null) { 106 $group = 0; 107 } 108 if ($subwiki = wiki_get_subwiki_by_group($wiki->id, $group, $userid)) { 109 return $subwiki->id; 110 } else { 111 return wiki_add_subwiki($wiki->id, $group, $userid); 112 } 113 } 114 115 /** 116 * Generates a page in wiki. 117 * 118 * @param stdClass wiki object returned from create_instance (if known) 119 * @param stdClass|array $record data to insert as wiki entry. 120 * @return stdClass 121 * @throws coding_exception if neither $record->wikiid nor $wiki->id is specified 122 */ 123 public function create_page($wiki, $record = array()) { 124 global $CFG, $USER; 125 require_once($CFG->dirroot.'/mod/wiki/locallib.php'); 126 $this->pagecount++; 127 $record = (array)$record + array( 128 'title' => 'wiki page '.$this->pagecount, 129 'wikiid' => $wiki->id, 130 'subwikiid' => 0, 131 'group' => null, 132 'userid' => null, 133 'content' => 'Wiki page content '.$this->pagecount, 134 'format' => $wiki->defaultformat 135 ); 136 if (empty($record['wikiid']) && empty($record['subwikiid'])) { 137 throw new coding_exception('wiki page generator requires either wikiid or subwikiid'); 138 } 139 $record['subwikiid'] = $this->get_subwiki($wiki, $record['subwikiid'], $record['group'], $record['userid']); 140 141 $wikipage = wiki_get_page_by_title($record['subwikiid'], $record['title']); 142 if (!$wikipage) { 143 $pageid = wiki_create_page($record['subwikiid'], $record['title'], $record['format'], $USER->id); 144 $wikipage = wiki_get_page($pageid); 145 } 146 $rv = wiki_save_page($wikipage, $record['content'], $USER->id); 147 148 if (array_key_exists('tags', $record)) { 149 $tags = is_array($record['tags']) ? $record['tags'] : preg_split('/,/', $record['tags']); 150 if (empty($wiki->cmid)) { 151 $cm = get_coursemodule_from_instance('wiki', $wiki->id, isset($wiki->course) ? $wiki->course : 0); 152 $wiki->cmid = $cm->id; 153 } 154 core_tag_tag::set_item_tags('mod_wiki', 'wiki_pages', $wikipage->id, 155 context_module::instance($wiki->cmid), $tags); 156 } 157 return $rv['page']; 158 } 159 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body