. /** * mod_wiki data generator. * * @package mod_wiki * @category test * @copyright 2013 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); /** * mod_wiki data generator class. * * @package mod_wiki * @category test * @copyright 2013 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class mod_wiki_generator extends testing_module_generator { /** * @var int keep track of how many pages have been created. */ protected $pagecount = 0; /** * To be called from data reset code only, * do not use in tests. * @return void */ public function reset() { $this->pagecount = 0; parent::reset(); } public function create_instance($record = null, array $options = null) { // Add default values for wiki. $record = (array)$record + array( 'wikimode' => 'collaborative', 'firstpagetitle' => 'Front page for wiki '.($this->instancecount+1), 'defaultformat' => 'html', 'forceformat' => 0 ); return parent::create_instance($record, (array)$options); } public function create_content($wiki, $record = array()) { $record = (array)$record + array( 'wikiid' => $wiki->id ); return $this->create_page($wiki, $record); } public function create_first_page($wiki, $record = array()) { $record = (array)$record + array( 'title' => $wiki->firstpagetitle, ); return $this->create_page($wiki, $record); } /** * Retrieves or generates a subwiki and returns its id * * @param stdClass $wiki * @param int $subwikiid * @param int $group * @param int $userid * @return int */ public function get_subwiki($wiki, $subwikiid = null, $group = null, $userid = null) { global $USER, $DB; if ($subwikiid) { $params = ['id' => $subwikiid, 'wikiid' => $wiki->id]; if ($group !== null) { $params['group'] = $group; } if ($userid !== null) { $params['userid'] = $userid; } return $DB->get_field('wiki_subwikis', 'id', $params, MUST_EXIST); } if ($userid === null) { $userid = ($wiki->wikimode == 'individual') ? $USER->id : 0; } if ($group === null) { $group = 0; } if ($subwiki = wiki_get_subwiki_by_group($wiki->id, $group, $userid)) { return $subwiki->id; } else { return wiki_add_subwiki($wiki->id, $group, $userid); } } /** * Generates a page in wiki. * * @param stdClass wiki object returned from create_instance (if known) * @param stdClass|array $record data to insert as wiki entry. * @return stdClass * @throws coding_exception if neither $record->wikiid nor $wiki->id is specified */ public function create_page($wiki, $record = array()) { global $CFG, $USER; require_once($CFG->dirroot.'/mod/wiki/locallib.php'); $this->pagecount++; $record = (array)$record + array( 'title' => 'wiki page '.$this->pagecount, 'wikiid' => $wiki->id, 'subwikiid' => 0, 'group' => null, 'userid' => null, 'content' => 'Wiki page content '.$this->pagecount, 'format' => $wiki->defaultformat ); if (empty($record['wikiid']) && empty($record['subwikiid'])) { throw new coding_exception('wiki page generator requires either wikiid or subwikiid'); } $record['subwikiid'] = $this->get_subwiki($wiki, $record['subwikiid'], $record['group'], $record['userid']); $wikipage = wiki_get_page_by_title($record['subwikiid'], $record['title']); if (!$wikipage) { $pageid = wiki_create_page($record['subwikiid'], $record['title'], $record['format'], $USER->id); $wikipage = wiki_get_page($pageid); } $rv = wiki_save_page($wikipage, $record['content'], $USER->id); if (array_key_exists('tags', $record)) { $tags = is_array($record['tags']) ? $record['tags'] : preg_split('/,/', $record['tags']); if (empty($wiki->cmid)) { $cm = get_coursemodule_from_instance('wiki', $wiki->id, isset($wiki->course) ? $wiki->course : 0); $wiki->cmid = $cm->id; } core_tag_tag::set_item_tags('mod_wiki', 'wiki_pages', $wikipage->id, context_module::instance($wiki->cmid), $tags); } return $rv['page']; } }