MDL-30545 messaging prefs: show all relevant message providers.
[moodle.git] / mod / quiz / tests / generator / lib.php
CommitLineData
2cf5ab10
TH
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
17defined('MOODLE_INTERNAL') || die();
18
19/**
20 * Quiz module test data generator class
21 *
22 * @package mod_quiz
23 * @copyright 2012 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26class mod_quiz_generator extends phpunit_module_generator {
27
28 /**
29 * Create new quiz module instance.
30 * @param array|stdClass $record
31 * @param array $options (mostly course_module properties)
32 * @return stdClass activity record with extra cmid field
33 */
34 public function create_instance($record = null, array $options = null) {
35 global $CFG;
36 require_once("$CFG->dirroot/mod/quiz/locallib.php");
37
38 $this->instancecount++;
39 $i = $this->instancecount;
40
41 $record = (object)(array)$record;
42 $options = (array)$options;
43
44 if (empty($record->course)) {
45 throw new coding_exception('module generator requires $record->course');
46 }
47 if (isset($options['idnumber'])) {
48 $record->cmidnumber = $options['idnumber'];
49 } else {
50 $record->cmidnumber = '';
51 }
52
53 $alwaysvisible = mod_quiz_display_options::DURING | mod_quiz_display_options::IMMEDIATELY_AFTER |
54 mod_quiz_display_options::LATER_WHILE_OPEN | mod_quiz_display_options::AFTER_CLOSE;
55
56 $defaultquizsettings = array(
57 'name' => get_string('pluginname', 'data').' '.$i,
58 'intro' => 'Test quiz ' . $i,
59 'introformat' => FORMAT_MOODLE,
60 'timeopen' => 0,
61 'timeclose' => 0,
62 'preferredbehaviour' => 'deferredfeedback',
63 'attempts' => 0,
64 'attemptonlast' => 0,
65 'grademethod' => QUIZ_GRADEHIGHEST,
66 'decimalpoints' => 2,
67 'questiondecimalpoints' => -1,
68 'reviewattempt' => $alwaysvisible,
69 'reviewcorrectness' => $alwaysvisible,
70 'reviewmarks' => $alwaysvisible,
71 'reviewspecificfeedback' => $alwaysvisible,
72 'reviewgeneralfeedback' => $alwaysvisible,
73 'reviewrightanswer' => $alwaysvisible,
74 'reviewoverallfeedback' => $alwaysvisible,
75 'questionsperpage' => 1,
76 'shufflequestions' => 0,
77 'shuffleanswers' => 1,
78 'questions' => '',
79 'sumgrades' => 0,
80 'grade' => 0,
81 'timecreated' => time(),
82 'timemodified' => time(),
83 'timelimit' => 0,
84 'overduehandling' => 'autoabandon',
85 'graceperiod' => 86400,
86 'quizpassword' => '',
87 'subnet' => '',
88 'browsersecurity' => '',
89 'delay1' => 0,
90 'delay2' => 0,
91 'showuserpicture' => 0,
92 'showblocks' => 0,
93 'navmethod' => QUIZ_NAVMETHOD_FREE,
94 );
95
96 foreach ($defaultquizsettings as $name => $value) {
97 if (!isset($record->{$name})) {
98 $record->{$name} = $value;
99 }
100 }
101
102 $record->coursemodule = $this->precreate_course_module($record->course, $options);
103 $id = quiz_add_instance($record);
104 return $this->post_add_instance($id, $record->coursemodule);
105 }
106}