From 6f9354beb0738c9d4ca1177e514ece8439798216 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Thu, 20 Feb 2014 10:43:08 +0800 Subject: [PATCH 1/2] MDL-44287 glossary: use course mod api to create new module when importing as new --- course/modlib.php | 10 ++++++++ mod/glossary/import.php | 55 +++++++++++------------------------------ 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/course/modlib.php b/course/modlib.php index 42df746f5e559..35285c9c1be1f 100644 --- a/course/modlib.php +++ b/course/modlib.php @@ -343,6 +343,9 @@ function set_moduleinfo_defaults($moduleinfo) { if (!isset($moduleinfo->completionview)) { $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. if (isset($moduleinfo->completionusegrade) && $moduleinfo->completionusegrade) { @@ -351,6 +354,13 @@ function set_moduleinfo_defaults($moduleinfo) { $moduleinfo->completiongradeitemnumber = null; } + if (!isset($moduleinfo->conditiongradegroup)) { + $moduleinfo->conditiongradegroup = array(); + } + if (!isset($moduleinfo->conditionfieldgroup)) { + $moduleinfo->conditionfieldgroup = array(); + } + return $moduleinfo; } diff --git a/mod/glossary/import.php b/mod/glossary/import.php index 542a1a946432d..87a9c937096ed 100644 --- a/mod/glossary/import.php +++ b/mod/glossary/import.php @@ -3,6 +3,7 @@ require_once("../../config.php"); require_once("lib.php"); require_once("$CFG->dirroot/course/lib.php"); +require_once("$CFG->dirroot/course/modlib.php"); require_once('import_form.php'); $id = required_param('id', PARAM_INT); // Course Module ID @@ -88,17 +89,16 @@ if ( $xmlglossary['NAME'][0]['#'] ) { $glossary = new stdClass(); + $glossary->modulename = 'glossary'; + $glossary->module = $cm->module; $glossary->name = ($xmlglossary['NAME'][0]['#']); - $glossary->course = $course->id; $glossary->globalglossary = ($xmlglossary['GLOBALGLOSSARY'][0]['#']); $glossary->intro = ($xmlglossary['INTRO'][0]['#']); $glossary->introformat = isset($xmlglossary['INTROFORMAT'][0]['#']) ? $xmlglossary['INTROFORMAT'][0]['#'] : FORMAT_MOODLE; $glossary->showspecial = ($xmlglossary['SHOWSPECIAL'][0]['#']); $glossary->showalphabet = ($xmlglossary['SHOWALPHABET'][0]['#']); $glossary->showall = ($xmlglossary['SHOWALL'][0]['#']); - $glossary->timecreated = time(); - $glossary->timemodified = time(); - $glossary->cmidnumber = $cm->idnumber; + $glossary->cmidnumber = null; // Setting the default values if no values were passed if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) { @@ -132,48 +132,23 @@ $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 - if ( !$glossary->id = glossary_add_instance($glossary) ) { + if ( !($glossary = add_moduleinfo($glossary, $course)) ) { echo $OUTPUT->notification("Error while trying to create the new glossary."); glossary_print_tabbed_table_end(); echo $OUTPUT->footer(); exit; } 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'); } } else { From 7d96cf2369353d1a85489845744a60b901683c00 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Thu, 20 Feb 2014 14:14:29 +0800 Subject: [PATCH 2/2] MDL-44287 glossary: do not output before redirecting --- mod/glossary/editcategories.php | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/mod/glossary/editcategories.php b/mod/glossary/editcategories.php index f569e4d1e28b5..23fe6f326aa8c 100644 --- a/mod/glossary/editcategories.php +++ b/mod/glossary/editcategories.php @@ -69,16 +69,14 @@ $strglossaries = get_string("modulenameplural", "glossary"); $strglossary = get_string("modulename", "glossary"); -$PAGE->navbar->add($strglossaries, new moodle_url('/mod/glossary/index.php', array('id'=>$course->id))); -$PAGE->navbar->add(get_string("categories","glossary")); +$PAGE->navbar->add(get_string("categories","glossary"), + new moodle_url('/mod/glossary/editcategories.php', array('id' => $cm->id,'mode' => 'cat'))); if (!empty($action)) { $navaction = get_string($action). " " . core_text::strtolower(get_string("category","glossary")); $PAGE->navbar->add($navaction); } $PAGE->set_title($glossary->name); $PAGE->set_heading($course->fullname); -echo $OUTPUT->header(); -echo $OUTPUT->heading(format_string($glossary->name), 2); // Prepare format_string/text options $fmtoptions = array( @@ -107,6 +105,8 @@ add_to_log($course->id, "glossary", "edit category", "editcategories.php?id=$cm->id", $hook,$cm->id); } else { + echo $OUTPUT->header(); + echo $OUTPUT->heading(format_string($glossary->name), 2); echo $OUTPUT->heading(format_string(get_string("edit"). " " . get_string("category","glossary")), 3); $name = $category->name; @@ -121,14 +121,12 @@ $DB->delete_records("glossary_entries_categories", array("categoryid"=>$hook)); $DB->delete_records("glossary_categories", array("id"=>$hook)); - echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow'); - echo "
" . get_string("categorydeleted","glossary") ."
"; - echo $OUTPUT->box_end(); - add_to_log($course->id, "glossary", "delete category", "editcategories.php?id=$cm->id", $hook,$cm->id); - redirect("editcategories.php?id=$cm->id"); + redirect("editcategories.php?id=$cm->id", get_string("categorydeleted", "glossary"), 2); } else { + echo $OUTPUT->header(); + echo $OUTPUT->heading(format_string($glossary->name), 2); echo $OUTPUT->heading(format_string(get_string("delete"). " " . get_string("category","glossary")), 3); echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow'); @@ -173,13 +171,7 @@ if ( $confirm ) { $dupcategory = $DB->get_records_sql("SELECT * FROM {glossary_categories} WHERE ".$DB->sql_like('name','?', false)." AND glossaryid=?", array($name, $glossary->id)); if ( $dupcategory ) { - echo "

" . get_string("add"). " " . get_string("category","glossary"). "

"; - - echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow'); - echo "
" . get_string("duplicatecategory","glossary") ."
"; - echo $OUTPUT->box_end(); - - redirect("editcategories.php?id=$cm->id&action=add&name=$name"); + redirect("editcategories.php?id=$cm->id&action=add&name=$name", get_string("duplicatecategory", "glossary"), 2); } else { $action = ""; @@ -192,6 +184,8 @@ add_to_log($course->id, "glossary", "add category", "editcategories.php?id=$cm->id", $cat->id,$cm->id); } } else { + echo $OUTPUT->header(); + echo $OUTPUT->heading(format_string($glossary->name), 2); echo "

" . get_string("add"). " " . get_string("category","glossary"). "

"; $name=""; require "editcategories.html"; @@ -203,6 +197,9 @@ die; } +echo $OUTPUT->header(); +echo $OUTPUT->heading(format_string($glossary->name), 2); + ?>