require_once($CFG->dirroot . '/mod/quiz/locallib.php');
require_once($CFG->dirroot . '/course/modlib.php');
// Retrieve the course record.
$course = get_course($course->id);
$timelimit = 3600; // 60 minute
// set quiz opens as per your need
$record = array(
'course' => $course->id,
'timeopen' => $course->startdate,
'timeclose' => $course->enddate,
'preferredbehaviour' => 'deferredfeedback',
'attempts' => 1,
'attemptonlast' => 0,
'grademethod' => QUIZ_GRADEHIGHEST,
'decimalpoints' => 2,
'questiondecimalpoints' => -1,
'attemptduring' => 1,
'correctnessduring' => 0,
'marksduring' => 0,
'specificfeedbackduring' => 0,
'generalfeedbackduring' => 0,
'rightanswerduring' => 0,
'overallfeedbackduring' => 0,
'attemptimmediately' => 0,
'correctnessimmediately' => 0,
'marksimmediately' => 0,
'specificfeedbackimmediately' => 0,
'generalfeedbackimmediately' => 0,
'rightanswerimmediately' => 0,
'overallfeedbackimmediately' => 0,
'attemptopen' => 0,
'correctnessopen' =>0,
'marksopen' => 0,
'specificfeedbackopen' => 0,
'generalfeedbackopen' => 0,
'rightansweropen' => 0,
'overallfeedbackopen' => 0,
'attemptclosed' => 0,
'correctnessclosed' => 0,
'marksclosed' => 0,
'specificfeedbackclosed' => 0,
'generalfeedbackclosed' => 0,
'rightanswerclosed' => 0,
'overallfeedbackclosed' => 0,
'questionsperpage' => 10,
'shuffleanswers' => 1,
'sumgrades' => 0,
'grade' => 100,
'timecreated' => time(),
'timemodified' => time(),
'timelimit' => $timelimit,
'overduehandling' => 'autosubmit',
'graceperiod' => 0,
'quizpassword' => '',
'subnet' => '',
'browsersecurity' => '',
'delay1' => 0,
'delay2' => 0,
'showuserpicture' => 0,
'showblocks' => 0,
'navmethod' => QUIZ_NAVMETHOD_FREE,
);
$record = (object) record;
// Merge options into record and add default values.
// put up default values for module
$record = prepare_moduleinfo_record($record);
$record->name = $course->fullname;
$record->intro = $course->summary;
$record->introformat = FORMAT_HTML;
$record->introeditor = array(
'text' => $course->summary,
'format'=>FORMAT_HTML
);
if (isset($record->tags) && !is_array($record->tags)) {
$record->tags = preg_split('/\s*,\s*/', trim($record->tags), -1, PREG_SPLIT_NO_EMPTY);
}
$record->completion = 0;
// Add the module to the course.
$moduleinfo = add_moduleinfo($record, $course, $mform = null);
// function to setup up default options of a module and override them above
function prepare_moduleinfo_record($record) {
global $DB;
// Make sure we don't modify the original object.
$moduleinfo = (object) (array) $record;
$moduleinfo->modulename = 'quiz';
$moduleinfo->module = $DB->get_field('modules', 'id', array('name' => 'quiz'));
$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',
'completion', 'completiongradeitemnumber', 'completionview', 'completionexpected',
'availability', '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,
'visibleoncoursepage' => 1,
'cmidnumber' => '',
'groupmode' => 0,
'groupingid' => 0,
'availability' => null,
'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;
}