From 71ee4471ac31a2889224a88a2b68af4c4d48cd21 Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 31 Jul 2007 14:34:11 +0000 Subject: [PATCH] MDL-10544 outcome adding from modedit --- course/modedit.php | 61 ++++++++++++++++++++++++++++++++++++++- course/moodleform_mod.php | 34 ++++++++++++++++++++++ mod/glossary/mod_form.php | 3 +- 3 files changed, 96 insertions(+), 2 deletions(-) diff --git a/course/modedit.php b/course/modedit.php index fa01f3c804..88b114ec67 100644 --- a/course/modedit.php +++ b/course/modedit.php @@ -4,6 +4,7 @@ require_once("../config.php"); require_once("lib.php"); + require_once($CFG->libdir.'/gradelib.php'); require_login(); @@ -100,6 +101,16 @@ $form->return = $return; $form->update = $update; + // add existing outcomes + if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$form->modulename, + 'iteminstance'=>$form->instance, 'courseid'=>$COURSE->id))) { + foreach ($items as $item) { + if (!empty($item->outcomeid)) { + $form->{'outcome_'.$item->outcomeid} = 1; + } + } + } + $sectionname = get_section_name($course->format); $fullmodulename = get_string("modulename", $module->name); @@ -149,7 +160,7 @@ } else { redirect("view.php?id=$course->id#section-".$cousesection); } - } else if ($fromform=$mform->get_data()){ + } else if ($fromform = $mform->get_data()) { if (empty($fromform->coursemodule)) { //add if (! $course = get_record("course", "id", $fromform->course)) { error("This course doesn't exist"); @@ -283,6 +294,54 @@ error("Data submitted is invalid."); } + // add outcomes if requested + if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) { + foreach($outcomes as $outcome) { + $elname = 'outcome_'.$outcome->id; + if (array_key_exists($elname, $fromform) and $fromform->$elname) { + // we have a request for new outcome grade item + $grade_item = new grade_item(); + $max = 999; + if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename, + 'iteminstance'=>$fromform->instance, 'courseid'=>$COURSE->id))) { + $exists = false; + foreach($items as $item) { + if ($item->outcomeid == $outcome->id) { + $exists = true; + break; + } + if (empty($item->outcomeid)) { + continue; + } + if ($item->itemnumber > $max) { + $max = $item->itemnumber; + } + } + } + if ($exists) { + continue; + } + $grade_item->courseid = $COURSE->id; + $grade_item->itemtype = 'mod'; + $grade_item->itemmodule = $fromform->modulename; + $grade_item->iteminstance = $fromform->instance; + $grade_item->itemnumber = $max + 1; + $grade_item->itemname = $fromform->name.' - '.$outcome->fullname; + $grade_item->outcomeid = $outcome->id; + $grade_item->gradetype = GRADE_TYPE_SCALE; + $grade_item->scaleid = $outcome->scaleid; + + $grade_item->insert(); + + if ($item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$grade_item->itemmodule, + 'iteminstance'=>$grade_item->iteminstance, 'itemnumber'=>0, 'courseid'=>$COURSE->id))) { + $grade_item->set_parent($item->categoryid); + $grade_item->move_after_sortorder($item->sortorder); + } + } + } + } + rebuild_course_cache($course->id); redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule"); diff --git a/course/moodleform_mod.php b/course/moodleform_mod.php index a0869614cb..f887318f81 100644 --- a/course/moodleform_mod.php +++ b/course/moodleform_mod.php @@ -35,6 +35,7 @@ class moodleform_mod extends moodleform { $this->_cm = $cm; parent::moodleform('modedit.php'); } + /** * Only available on moodleform_mod. * @@ -42,6 +43,29 @@ class moodleform_mod extends moodleform { */ function data_preprocessing(&$default_values){ } + + function definition_after_data() { + global $COURSE; + $mform =& $this->_form; + + if ($id = $mform->getElementValue('update')) { + $modulename = $mform->getElementValue('modulename'); + $instance = $mform->getElementValue('instance'); + + if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename, + 'iteminstance'=>$instance, 'courseid'=>$COURSE->id))) { + foreach ($items as $item) { + if (!empty($item->outcomeid)) { + $elname = 'outcome_'.$item->outcomeid; + if ($mform->elementExists($elname)) { + $mform->hardFreeze($elname); // prevent removing of existing outcomes + } + } + } + } + } + } + /** * Load in existing data as form defaults. Usually new entry defaults are stored directly in * form definition (new entry form); this function is used to load in data where values @@ -56,12 +80,14 @@ class moodleform_mod extends moodleform { $this->data_preprocessing($default_values); parent::set_data($default_values + $this->standard_coursemodule_elements_settings());//never slashed for moodleform_mod } + /** * Adds all the standard elements to a form to edit the settings for an activity module. * * @param bool $supportsgroups does this module support groups? */ function standard_coursemodule_elements($supportsgroups=true){ + global $COURSE; $mform =& $this->_form; $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form')); if ($supportsgroups){ @@ -70,6 +96,14 @@ class moodleform_mod extends moodleform { } $mform->addElement('modvisible', 'visible', get_string('visible')); $mform->addElement('text', 'cmidnumber', get_string('idnumber')); + + if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) { + $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades')); + foreach($outcomes as $outcome) { + $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name()); + } + + } $this->standard_hidden_coursemodule_elements(); } diff --git a/mod/glossary/mod_form.php b/mod/glossary/mod_form.php index cdef399e74..752ef26a32 100644 --- a/mod/glossary/mod_form.php +++ b/mod/glossary/mod_form.php @@ -150,7 +150,8 @@ class mod_glossary_mod_form extends moodleform_mod { $this->add_action_buttons(); } - function definition_after_data(){ + function definition_after_data() { + parent::definition_after_data(); global $COURSE; $mform =& $this->_form; $mainglossaryel =& $mform->getElement('mainglossary'); -- 2.39.5