MDL-44287 glossary: use course mod api to create new module when importing as new

This commit is contained in:
Marina Glancy 2014-02-20 10:43:08 +08:00
parent 76e4de31cc
commit 6f9354beb0
2 changed files with 25 additions and 40 deletions

View file

@ -343,6 +343,9 @@ function set_moduleinfo_defaults($moduleinfo) {
if (!isset($moduleinfo->completionview)) { if (!isset($moduleinfo->completionview)) {
$moduleinfo->completionview = COMPLETION_VIEW_NOT_REQUIRED; $moduleinfo->completionview = COMPLETION_VIEW_NOT_REQUIRED;
} }
if (!isset($moduleinfo->completionexpected)) {
$moduleinfo->completionexpected = 0;
}
// Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not. // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
if (isset($moduleinfo->completionusegrade) && $moduleinfo->completionusegrade) { if (isset($moduleinfo->completionusegrade) && $moduleinfo->completionusegrade) {
@ -351,6 +354,13 @@ function set_moduleinfo_defaults($moduleinfo) {
$moduleinfo->completiongradeitemnumber = null; $moduleinfo->completiongradeitemnumber = null;
} }
if (!isset($moduleinfo->conditiongradegroup)) {
$moduleinfo->conditiongradegroup = array();
}
if (!isset($moduleinfo->conditionfieldgroup)) {
$moduleinfo->conditionfieldgroup = array();
}
return $moduleinfo; return $moduleinfo;
} }

View file

@ -3,6 +3,7 @@
require_once("../../config.php"); require_once("../../config.php");
require_once("lib.php"); require_once("lib.php");
require_once("$CFG->dirroot/course/lib.php"); require_once("$CFG->dirroot/course/lib.php");
require_once("$CFG->dirroot/course/modlib.php");
require_once('import_form.php'); require_once('import_form.php');
$id = required_param('id', PARAM_INT); // Course Module ID $id = required_param('id', PARAM_INT); // Course Module ID
@ -88,17 +89,16 @@ if ($xml = glossary_read_imported_file($result)) {
if ( $xmlglossary['NAME'][0]['#'] ) { if ( $xmlglossary['NAME'][0]['#'] ) {
$glossary = new stdClass(); $glossary = new stdClass();
$glossary->modulename = 'glossary';
$glossary->module = $cm->module;
$glossary->name = ($xmlglossary['NAME'][0]['#']); $glossary->name = ($xmlglossary['NAME'][0]['#']);
$glossary->course = $course->id;
$glossary->globalglossary = ($xmlglossary['GLOBALGLOSSARY'][0]['#']); $glossary->globalglossary = ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
$glossary->intro = ($xmlglossary['INTRO'][0]['#']); $glossary->intro = ($xmlglossary['INTRO'][0]['#']);
$glossary->introformat = isset($xmlglossary['INTROFORMAT'][0]['#']) ? $xmlglossary['INTROFORMAT'][0]['#'] : FORMAT_MOODLE; $glossary->introformat = isset($xmlglossary['INTROFORMAT'][0]['#']) ? $xmlglossary['INTROFORMAT'][0]['#'] : FORMAT_MOODLE;
$glossary->showspecial = ($xmlglossary['SHOWSPECIAL'][0]['#']); $glossary->showspecial = ($xmlglossary['SHOWSPECIAL'][0]['#']);
$glossary->showalphabet = ($xmlglossary['SHOWALPHABET'][0]['#']); $glossary->showalphabet = ($xmlglossary['SHOWALPHABET'][0]['#']);
$glossary->showall = ($xmlglossary['SHOWALL'][0]['#']); $glossary->showall = ($xmlglossary['SHOWALL'][0]['#']);
$glossary->timecreated = time(); $glossary->cmidnumber = null;
$glossary->timemodified = time();
$glossary->cmidnumber = $cm->idnumber;
// Setting the default values if no values were passed // Setting the default values if no values were passed
if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) { if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
@ -132,48 +132,23 @@ if ($xml = glossary_read_imported_file($result)) {
$glossary->defaultapproval = $CFG->glossary_defaultapproval; $glossary->defaultapproval = $CFG->glossary_defaultapproval;
} }
// These fields were not included in export, assume zero.
$glossary->assessed = 0;
$glossary->availablefrom = 0;
$glossary->availableuntil = 0;
$glossary->showavailability = 0;
// New glossary is to be inserted in section 0, it is always visible.
$glossary->section = 0;
$glossary->visible = 1;
// Include new glossary and return the new ID // Include new glossary and return the new ID
if ( !$glossary->id = glossary_add_instance($glossary) ) { if ( !($glossary = add_moduleinfo($glossary, $course)) ) {
echo $OUTPUT->notification("Error while trying to create the new glossary."); echo $OUTPUT->notification("Error while trying to create the new glossary.");
glossary_print_tabbed_table_end(); glossary_print_tabbed_table_end();
echo $OUTPUT->footer(); echo $OUTPUT->footer();
exit; exit;
} else { } else {
//The instance has been created, so lets do course_modules
//and course_sections
$mod->groupmode = $course->groupmode; /// Default groupmode the same as course
$mod->instance = $glossary->id;
// course_modules and course_sections each contain a reference
// to each other, so we have to update one of them twice.
if (! $currmodule = $DB->get_record("modules", array("name"=>'glossary'))) {
print_error('modulenotexist', 'debug', '', 'Glossary');
}
$mod->module = $currmodule->id;
$mod->course = $course->id;
$mod->modulename = 'glossary';
$mod->section = 0;
if (! $mod->coursemodule = add_course_module($mod) ) {
print_error('cannotaddcoursemodule');
}
$sectionid = course_add_cm_to_section($course, $mod->coursemodule, 0);
//We get the section's visible field status
$visible = $DB->get_field("course_sections", "visible", array("id"=>$sectionid));
$DB->set_field("course_modules", "visible", $visible, array("id"=>$mod->coursemodule));
add_to_log($course->id, "course", "add mod",
"../mod/$mod->modulename/view.php?id=$mod->coursemodule",
"$mod->modulename $mod->instance");
add_to_log($course->id, $mod->modulename, "add",
"view.php?id=$mod->coursemodule",
"$mod->instance", $mod->coursemodule);
rebuild_course_cache($course->id);
echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal'); echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
} }
} else { } else {