mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-42400 generator: module generators call add_moduleinfo instead of direct inserts
This commit is contained in:
parent
b4b7587294
commit
7fbe33fcf1
18 changed files with 194 additions and 409 deletions
|
@ -43,6 +43,8 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
|
|||
|
||||
unset_config('noemailever');
|
||||
|
||||
$CFG->enablecompletion = true;
|
||||
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
|
||||
$fordb = new stdClass();
|
||||
|
@ -69,7 +71,7 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
|
|||
$this->badgeid = $DB->insert_record('badge', $fordb, true);
|
||||
|
||||
// Create a course with activity and auto completion tracking.
|
||||
$this->course = $this->getDataGenerator()->create_course();
|
||||
$this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
|
||||
$this->user = $this->getDataGenerator()->create_user();
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
$this->assertNotEmpty($studentrole);
|
||||
|
|
|
@ -86,7 +86,8 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) {
|
|||
print_error('cannotaddcoursemodule');
|
||||
}
|
||||
|
||||
if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
|
||||
if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true) &&
|
||||
isset($moduleinfo->introeditor)) {
|
||||
$introeditor = $moduleinfo->introeditor;
|
||||
unset($moduleinfo->introeditor);
|
||||
$moduleinfo->intro = $introeditor['text'];
|
||||
|
|
|
@ -70,6 +70,12 @@ abstract class testing_module_generator extends component_generator_base {
|
|||
|
||||
/**
|
||||
* Create course module and link it to course
|
||||
*
|
||||
* Since 2.6 it is recommended to use function add_moduleinfo() to create a module.
|
||||
*
|
||||
* @deprecated since 2.6
|
||||
* @see testing_module_generator::create_instance()
|
||||
*
|
||||
* @param integer $courseid
|
||||
* @param array $options section, visible
|
||||
* @return integer $cm instance id
|
||||
|
@ -110,6 +116,12 @@ abstract class testing_module_generator extends component_generator_base {
|
|||
|
||||
/**
|
||||
* Called after *_add_instance()
|
||||
*
|
||||
* Since 2.6 it is recommended to use function add_moduleinfo() to create a module.
|
||||
*
|
||||
* @deprecated since 2.6
|
||||
* @see testing_module_generator::create_instance()
|
||||
*
|
||||
* @param int $id
|
||||
* @param int $cmid
|
||||
* @return stdClass module instance
|
||||
|
@ -130,12 +142,133 @@ abstract class testing_module_generator extends component_generator_base {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create a test module
|
||||
* @param array|stdClass $record
|
||||
* @param array $options
|
||||
* @return stdClass activity record
|
||||
* Merges together arguments $record and $options and fills default module
|
||||
* fields that are shared by all module types
|
||||
*
|
||||
* @param object|array $record fields (different from defaults) for this module
|
||||
* @param null|array $options for backward-compatibility this may include fields from course_modules
|
||||
* table. They are merged into $record
|
||||
* @throws coding_exception if $record->course is not specified
|
||||
*/
|
||||
abstract public function create_instance($record = null, array $options = null);
|
||||
protected function prepare_moduleinfo_record($record, $options) {
|
||||
global $DB;
|
||||
// Make sure we don't modify the original object.
|
||||
$moduleinfo = (object)(array)$record;
|
||||
|
||||
if (empty($moduleinfo->course)) {
|
||||
throw new coding_exception('module generator requires $record->course');
|
||||
}
|
||||
|
||||
$moduleinfo->modulename = $this->get_modulename();
|
||||
$moduleinfo->module = $DB->get_field('modules', 'id', array('name' => $moduleinfo->modulename));
|
||||
|
||||
// Allow idnumber to be set as either $options['idnumber'] or $moduleinfo->cmidnumber or $moduleinfo->idnumber.
|
||||
// The actual field name is 'idnumber' but add_moduleinfo() expects 'cmidnumber'.
|
||||
if (isset($options['idnumber'])) {
|
||||
$moduleinfo->cmidnumber = $options['idnumber'];
|
||||
} else if (!isset($moduleinfo->cmidnumber) && isset($moduleinfo->idnumber)) {
|
||||
$moduleinfo->cmidnumber = $moduleinfo->idnumber;
|
||||
}
|
||||
|
||||
// These are the fields from table 'course_modules' in 2.6 when the second
|
||||
// argument $options is being deprecated.
|
||||
// List excludes fields: instance (does not exist yet), course, module and idnumber (set above)
|
||||
$easymergefields = array('section', 'added', 'score', 'indent',
|
||||
'visible', 'visibleold', 'groupmode', 'groupingid', 'groupmembersonly',
|
||||
'completion', 'completiongradeitemnumber', 'completionview', 'completionexpected',
|
||||
'availablefrom', 'availableuntil', 'showavailability', 'showdescription');
|
||||
foreach ($easymergefields as $key) {
|
||||
if (isset($options[$key])) {
|
||||
$moduleinfo->$key = $options[$key];
|
||||
}
|
||||
}
|
||||
|
||||
// Set default values. Note that visibleold and completiongradeitemnumber are not used when creating a module.
|
||||
$defaults = array(
|
||||
'section' => 0,
|
||||
'visible' => 1,
|
||||
'cmidnumber' => '',
|
||||
'groupmode' => 0,
|
||||
'groupingid' => 0,
|
||||
'groupmembersonly' => 0,
|
||||
'showavailability' => 0,
|
||||
'availablefrom' => 0,
|
||||
'availableuntil' => 0,
|
||||
'completion' => 0,
|
||||
'completionview' => 0,
|
||||
'completionexpected' => 0,
|
||||
'conditiongradegroup' => array(),
|
||||
'conditionfieldgroup' => array(),
|
||||
'conditioncompletiongroup' => array()
|
||||
);
|
||||
foreach ($defaults as $key => $value) {
|
||||
if (!isset($moduleinfo->$key)) {
|
||||
$moduleinfo->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $moduleinfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of the module for testing purposes.
|
||||
*
|
||||
* Module type will be taken from the class name. Each module type may overwrite
|
||||
* this function to add other default values used by it.
|
||||
*
|
||||
* @param array|stdClass $record data for module being generated. Requires 'course' key
|
||||
* (an id or the full object). Also can have any fields from add module form.
|
||||
* @param null|array $options general options for course module. Since 2.6 it is
|
||||
* possible to omit this argument by merging options into $record
|
||||
* @return stdClass record from module-defined table with additional field
|
||||
* cmid (corresponding id in course_modules table)
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot.'/course/modlib.php');
|
||||
|
||||
$this->instancecount++;
|
||||
|
||||
// Merge options into record and add default values.
|
||||
$record = $this->prepare_moduleinfo_record($record, $options);
|
||||
|
||||
// Retrieve the course record.
|
||||
if (!empty($record->course->id)) {
|
||||
$course = $record->course;
|
||||
$record->course = $record->course->id;
|
||||
} else {
|
||||
$course = get_course($record->course);
|
||||
}
|
||||
|
||||
// Fill the name and intro with default values (if missing).
|
||||
if (empty($record->name)) {
|
||||
$record->name = get_string('pluginname', $this->get_modulename()).' '.$this->instancecount;
|
||||
}
|
||||
if (empty($record->introeditor) && empty($record->intro)) {
|
||||
$record->intro = 'Test '.$this->get_modulename().' ' . $this->instancecount;
|
||||
}
|
||||
if (empty($record->introeditor) && empty($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
|
||||
// Before Moodle 2.6 it was possible to create a module with completion tracking when
|
||||
// it is not setup for course and/or site-wide. Display debugging message so it is
|
||||
// easier to trace an error in unittests.
|
||||
if ($record->completion && empty($CFG->enablecompletion)) {
|
||||
debugging('Did you forget to set $CFG->enablecompletion before generating module with completion tracking?', DEBUG_DEVELOPER);
|
||||
}
|
||||
if ($record->completion && empty($course->enablecompletion)) {
|
||||
debugging('Did you forget to enable completion tracking for the course before generating module with completion tracking?', DEBUG_DEVELOPER);
|
||||
}
|
||||
|
||||
// Add the module to the course.
|
||||
$moduleinfo = add_moduleinfo($record, $course, $mform = null);
|
||||
|
||||
// Prepare object to return with additional field cmid.
|
||||
$instance = $DB->get_record($this->get_modulename(), array('id' => $moduleinfo->instance), '*', MUST_EXIST);
|
||||
$instance->cmid = $moduleinfo->coursemodule;
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -53,8 +53,11 @@ class core_completionlib_testcase extends advanced_testcase {
|
|||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Enable completion before creating modules, otherwise the completion data is not written in DB.
|
||||
$CFG->enablecompletion = true;
|
||||
|
||||
// Create a course with activities.
|
||||
$this->course = $this->getDataGenerator()->create_course();
|
||||
$this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
|
||||
$this->user = $this->getDataGenerator()->create_user();
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
$this->assertNotEmpty($studentrole);
|
||||
|
@ -718,10 +721,14 @@ class core_completionlib_testcase extends advanced_testcase {
|
|||
}
|
||||
|
||||
public function test_get_activities() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Enable completion before creating modules, otherwise the completion data is not written in DB.
|
||||
$CFG->enablecompletion = true;
|
||||
|
||||
// Create a course with mixed auto completion data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
|
||||
$completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
|
||||
$completionmanual = array('completion' => COMPLETION_TRACKING_MANUAL);
|
||||
$completionnone = array('completion' => COMPLETION_TRACKING_NONE);
|
||||
|
@ -734,7 +741,7 @@ class core_completionlib_testcase extends advanced_testcase {
|
|||
$data2 = $this->getDataGenerator()->create_module('data', array('course' => $course->id), $completionnone);
|
||||
|
||||
// Create data in another course to make sure it's not considered.
|
||||
$course2 = $this->getDataGenerator()->create_course();
|
||||
$course2 = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
|
||||
$c2forum = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id), $completionauto);
|
||||
$c2page = $this->getDataGenerator()->create_module('page', array('course' => $course2->id), $completionmanual);
|
||||
$c2data = $this->getDataGenerator()->create_module('data', array('course' => $course2->id), $completionnone);
|
||||
|
@ -755,11 +762,15 @@ class core_completionlib_testcase extends advanced_testcase {
|
|||
}
|
||||
|
||||
public function test_has_activities() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Enable completion before creating modules, otherwise the completion data is not written in DB.
|
||||
$CFG->enablecompletion = true;
|
||||
|
||||
// Create a course with mixed auto completion data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$course2 = $this->getDataGenerator()->create_course();
|
||||
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
|
||||
$course2 = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
|
||||
$completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
|
||||
$completionnone = array('completion' => COMPLETION_TRACKING_NONE);
|
||||
$c1forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id), $completionauto);
|
||||
|
@ -776,9 +787,10 @@ class core_completionlib_testcase extends advanced_testcase {
|
|||
* Test course module completion update event.
|
||||
*/
|
||||
public function test_course_module_completion_updated_event() {
|
||||
global $USER;
|
||||
global $USER, $CFG;
|
||||
|
||||
$this->setup_data();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
$completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
|
||||
|
|
|
@ -77,6 +77,8 @@ class core_modinfolib_testcase extends advanced_testcase {
|
|||
foreach ($conditionsfield as $conditionfield) {
|
||||
$ci->add_user_field_condition($conditionfield->fieldname, $conditionfield->operator, $conditionfield->value);
|
||||
}
|
||||
// Direct calls to condition_info_section methods do not reset the course cache. Do it manually.
|
||||
rebuild_course_cache($course->id, true);
|
||||
|
||||
// Create and enrol a student.
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
||||
|
@ -164,6 +166,8 @@ class core_modinfolib_testcase extends advanced_testcase {
|
|||
foreach ($conditionsfield as $conditionfield) {
|
||||
$ci->add_user_field_condition($conditionfield->fieldname, $conditionfield->operator, $conditionfield->value);
|
||||
}
|
||||
// Direct access to condition_info functions does not reset course cache, do it manually.
|
||||
rebuild_course_cache($course->id, true);
|
||||
|
||||
// Retrieve all related records from DB.
|
||||
$assigndb = $DB->get_record('assign', array('id' => $assign->id));
|
||||
|
@ -542,6 +546,9 @@ class core_modinfolib_testcase extends advanced_testcase {
|
|||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Enable conditional availability before creating modules, otherwise the condition data is not written in DB.
|
||||
$CFG->enableavailability = true;
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
// 1. Create an activity that is currently unavailable and hidden entirely (for students).
|
||||
|
|
|
@ -26,30 +26,10 @@ defined('MOODLE_INTERNAL') || die();
|
|||
*/
|
||||
class mod_assign_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* Create new assign module instance
|
||||
* @param array|stdClass $record
|
||||
* @param array $options (mostly course_module properties)
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/assign/lib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('module generator requires $record->course');
|
||||
}
|
||||
|
||||
$defaultsettings = array(
|
||||
'name' => get_string('pluginname', 'assign').' '.$i,
|
||||
'intro' => 'Test assign ' . $i,
|
||||
'introformat' => FORMAT_MOODLE,
|
||||
'alwaysshowdescription' => 1,
|
||||
'submissiondrafts' => 1,
|
||||
'requiresubmissionstatement' => 0,
|
||||
|
@ -63,7 +43,6 @@ class mod_assign_generator extends testing_module_generator {
|
|||
'requireallteammemberssubmit' => 0,
|
||||
'teamsubmissiongroupingid' => 0,
|
||||
'blindmarking' => 0,
|
||||
'cmidnumber' => '',
|
||||
'attemptreopenmethod' => 'none',
|
||||
'maxattempts' => -1,
|
||||
'markingworkflow' => 0,
|
||||
|
@ -76,9 +55,6 @@ class mod_assign_generator extends testing_module_generator {
|
|||
}
|
||||
}
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = assign_add_instance($record, null);
|
||||
rebuild_course_cache($record->course, true);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,34 +36,9 @@ defined('MOODLE_INTERNAL') || die();
|
|||
*/
|
||||
class mod_assignment_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* Create new assignment module instance
|
||||
* @param array|stdClass $record
|
||||
* @param array $options (mostly course_module properties)
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/assignment/locallib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('module generator requires $record->course');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'assignment').' '.$i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test assignment '.$i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->assignmenttype)) {
|
||||
$record->assignmenttype = 'upload';
|
||||
}
|
||||
|
@ -73,14 +48,7 @@ class mod_assignment_generator extends testing_module_generator {
|
|||
if (!isset($record->timedue)) {
|
||||
$record->timedue = 0;
|
||||
}
|
||||
if (isset($options['idnumber'])) {
|
||||
$record->cmidnumber = $options['idnumber'];
|
||||
} else {
|
||||
$record->cmidnumber = '';
|
||||
}
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = assignment_add_instance($record, null);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,34 +50,12 @@ class mod_book_generator extends testing_module_generator {
|
|||
parent::reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new book module instance
|
||||
* @param array|stdClass $record
|
||||
* @param array $options
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/book/locallib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('Module generator requires $record->course.');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'book') . ' ' . $i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test book ' . $i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->numbering)) {
|
||||
$record->numbering = BOOK_NUM_NUMBERS;
|
||||
}
|
||||
|
@ -85,9 +63,7 @@ class mod_book_generator extends testing_module_generator {
|
|||
$record->customtitles = 0;
|
||||
}
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = book_add_instance($record, null);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
|
||||
public function create_chapter($record = null, array $options = null) {
|
||||
|
|
|
@ -50,34 +50,9 @@ class mod_chat_generator extends testing_module_generator {
|
|||
parent::reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new chat module instance
|
||||
* @param array|stdClass $record
|
||||
* @param array $options
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/chat/lib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('Module generator requires $record->course.');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'chat') . ' ' . $i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test chat ' . $i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->keepdays)) {
|
||||
$record->keepdays = 0;
|
||||
}
|
||||
|
@ -94,9 +69,7 @@ class mod_chat_generator extends testing_module_generator {
|
|||
$record->timemodified = time();
|
||||
}
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = chat_add_instance($record);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -35,35 +35,9 @@ defined('MOODLE_INTERNAL') || die();
|
|||
*/
|
||||
class mod_choice_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* Create new choice module instance
|
||||
*
|
||||
* @param array|stdClass $record
|
||||
* @param array $options
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/choice/lib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('Module generator requires $record->course.');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'choice') . ' ' . $i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test choice ' . $i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->timemodified)) {
|
||||
$record->timemodified = time();
|
||||
}
|
||||
|
@ -74,8 +48,6 @@ class mod_choice_generator extends testing_module_generator {
|
|||
$record->option[] = 'Wine';
|
||||
$record->option[] = 'Spirits';
|
||||
}
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = choice_add_instance($record);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,48 +36,16 @@ defined('MOODLE_INTERNAL') || die();
|
|||
*/
|
||||
class mod_data_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* Create new data module instance
|
||||
* @param array|stdClass $record
|
||||
* @param array $options (mostly course_module properties)
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/data/locallib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('module generator requires $record->course');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'data').' '.$i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test database '.$i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->assessed)) {
|
||||
$record->assessed = 0;
|
||||
}
|
||||
if (!isset($record->scale)) {
|
||||
$record->scale = 0;
|
||||
}
|
||||
if (isset($options['idnumber'])) {
|
||||
$record->cmidnumber = $options['idnumber'];
|
||||
} else {
|
||||
$record->cmidnumber = '';
|
||||
}
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = data_add_instance($record, null);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,36 +35,11 @@ defined('MOODLE_INTERNAL') || die();
|
|||
*/
|
||||
class mod_feedback_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* Create new feedback module instance
|
||||
*
|
||||
* @param array|stdClass $record
|
||||
* @param array $options
|
||||
* @throws coding_exception
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/feedback/lib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
require_once($CFG->dirroot.'/mod/feedback/lib.php');
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('Module generator requires $record->course.');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'feedback') . ' ' . $i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test feedback ' . $i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->anonymous)) {
|
||||
$record->anonymous = FEEDBACK_ANONYMOUS_YES;
|
||||
}
|
||||
|
@ -105,9 +80,7 @@ class mod_feedback_generator extends testing_module_generator {
|
|||
// Hack to bypass draft processing of feedback_add_instance.
|
||||
$record->page_after_submit_editor['itemid'] = false;
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = feedback_add_instance($record);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -58,34 +58,11 @@ class mod_forum_generator extends testing_module_generator {
|
|||
parent::reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new forum module instance
|
||||
* @param array|stdClass $record
|
||||
* @param array $options
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/forum/locallib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
require_once($CFG->dirroot.'/mod/forum/lib.php');
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('module generator requires $record->course');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'forum').' '.$i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test forum '.$i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->type)) {
|
||||
$record->type = 'general';
|
||||
}
|
||||
|
@ -98,15 +75,8 @@ class mod_forum_generator extends testing_module_generator {
|
|||
if (!isset($record->forcesubscribe)) {
|
||||
$record->forcesubscribe = FORUM_CHOOSESUBSCRIBE;
|
||||
}
|
||||
if (isset($options['idnumber'])) {
|
||||
$record->cmidnumber = $options['idnumber'];
|
||||
} else {
|
||||
$record->cmidnumber = '';
|
||||
}
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = forum_add_instance($record, null);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -35,38 +35,5 @@ defined('MOODLE_INTERNAL') || die();
|
|||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_label_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* Create new label module instance
|
||||
* @param array|stdClass $record
|
||||
* @param array $options
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/label/lib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('module generator requires $record->course');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'label').' '.$i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test label '.$i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = label_add_instance($record, null);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
}
|
||||
// No additional fields in label module.
|
||||
}
|
||||
|
|
|
@ -37,37 +37,9 @@ defined('MOODLE_INTERNAL') || die();
|
|||
*/
|
||||
class mod_lti_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* Create new lti module instance
|
||||
*
|
||||
* @param array|stdClass $record
|
||||
* @param array $options
|
||||
* @throws coding_exception
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
|
||||
require_once("$CFG->dirroot/mod/lti/lib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
$record = (object) (array) $record;
|
||||
$options = (array) $options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('module generator requires $record->course');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'lti').' '.$i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test lti '.$i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->toolurl)) {
|
||||
$record->toolurl = 'http://www.imsglobal.org/developers/LTI/test/v1p1/tool.php';
|
||||
}
|
||||
|
@ -89,13 +61,6 @@ class mod_lti_generator extends testing_module_generator {
|
|||
if (!isset($record->instructorchoiceacceptgrades)) {
|
||||
$record->instructorchoiceacceptgrades = 1;
|
||||
}
|
||||
if (isset($options['idnumber'])) {
|
||||
$record->cmidnumber = $options['idnumber'];
|
||||
} else {
|
||||
$record->cmidnumber = '';
|
||||
}
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = lti_add_instance($record, null);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,34 +36,12 @@ defined('MOODLE_INTERNAL') || die();
|
|||
*/
|
||||
class mod_page_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* Create new page module instance
|
||||
* @param array|stdClass $record
|
||||
* @param array $options
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/page/locallib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
require_once($CFG->dirroot . '/lib/resourcelib.php');
|
||||
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('module generator requires $record->course');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'page').' '.$i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test page '.$i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->content)) {
|
||||
$record->content = 'Test page content';
|
||||
}
|
||||
|
@ -73,17 +51,10 @@ class mod_page_generator extends testing_module_generator {
|
|||
if (!isset($record->display)) {
|
||||
$record->display = RESOURCELIB_DISPLAY_AUTO;
|
||||
}
|
||||
if (isset($options['idnumber'])) {
|
||||
$record->cmidnumber = $options['idnumber'];
|
||||
} else {
|
||||
$record->cmidnumber = '';
|
||||
}
|
||||
if (!isset($record->printintro)) {
|
||||
$record->printintro = 0;
|
||||
}
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = page_add_instance($record, null);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,38 +25,15 @@ defined('MOODLE_INTERNAL') || die();
|
|||
*/
|
||||
class mod_quiz_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* Create new quiz module instance.
|
||||
* @param array|stdClass $record
|
||||
* @param array $options (mostly course_module properties)
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/quiz/locallib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
require_once($CFG->dirroot.'/mod/quiz/locallib.php');
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('module generator requires $record->course');
|
||||
}
|
||||
if (isset($options['idnumber'])) {
|
||||
$record->cmidnumber = $options['idnumber'];
|
||||
} else {
|
||||
$record->cmidnumber = '';
|
||||
}
|
||||
|
||||
$alwaysvisible = mod_quiz_display_options::DURING | mod_quiz_display_options::IMMEDIATELY_AFTER |
|
||||
mod_quiz_display_options::LATER_WHILE_OPEN | mod_quiz_display_options::AFTER_CLOSE;
|
||||
|
||||
$defaultquizsettings = array(
|
||||
'name' => get_string('pluginname', 'quiz').' '.$i,
|
||||
'intro' => 'Test quiz ' . $i,
|
||||
'introformat' => FORMAT_MOODLE,
|
||||
'timeopen' => 0,
|
||||
'timeclose' => 0,
|
||||
'preferredbehaviour' => 'deferredfeedback',
|
||||
|
@ -99,8 +76,6 @@ class mod_quiz_generator extends testing_module_generator {
|
|||
}
|
||||
}
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = quiz_add_instance($record);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,45 +38,23 @@ class mod_resource_generator extends testing_module_generator {
|
|||
* Creates new resource module instance. By default it contains a short
|
||||
* text file.
|
||||
*
|
||||
* @param array|stdClass $record Resource module record, as from form
|
||||
* @param array $options Standard options about how to create it
|
||||
* @return stdClass Activity record, with extra cmid field
|
||||
* @param array|stdClass $record data for module being generated. Requires 'course' key
|
||||
* (an id or the full object). Also can have any fields from add module form.
|
||||
* @param null|array $options general options for course module. Since 2.6 it is
|
||||
* possible to omit this argument by merging options into $record
|
||||
* @return stdClass record from module-defined table with additional field
|
||||
* cmid (corresponding id in course_modules table)
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG, $USER;
|
||||
require_once($CFG->dirroot . '/mod/resource/locallib.php');
|
||||
|
||||
// Count generated modules.
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
require_once($CFG->dirroot . '/lib/resourcelib.php');
|
||||
// Ensure the record can be modified without affecting calling code.
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
// Course is required.
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('module generator requires $record->course');
|
||||
}
|
||||
|
||||
// Fill in optional values if not specified.
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'resource') . ' ' . $i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test resource ' . $i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->display)) {
|
||||
$record->display = RESOURCELIB_DISPLAY_AUTO;
|
||||
}
|
||||
if (isset($options['idnumber'])) {
|
||||
$record->cmidnumber = $options['idnumber'];
|
||||
} else {
|
||||
$record->cmidnumber = '';
|
||||
}
|
||||
if (!isset($record->printintro)) {
|
||||
$record->printintro = 0;
|
||||
}
|
||||
|
@ -101,14 +79,12 @@ class mod_resource_generator extends testing_module_generator {
|
|||
// Add actual file there.
|
||||
$filerecord = array('component' => 'user', 'filearea' => 'draft',
|
||||
'contextid' => $usercontext->id, 'itemid' => $record->files,
|
||||
'filename' => 'resource' . $i . '.txt', 'filepath' => '/');
|
||||
'filename' => 'resource' . ($this->instancecount+1) . '.txt', 'filepath' => '/');
|
||||
$fs = get_file_storage();
|
||||
$fs->create_file_from_string($filerecord, 'Test resource ' . $i . ' file');
|
||||
$fs->create_file_from_string($filerecord, 'Test resource ' . ($this->instancecount+1) . ' file');
|
||||
}
|
||||
|
||||
// Do work to actually add the instance.
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = resource_add_instance($record, null);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue