]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10544 outcome adding from modedit
authorskodak <skodak>
Tue, 31 Jul 2007 14:34:11 +0000 (14:34 +0000)
committerskodak <skodak>
Tue, 31 Jul 2007 14:34:11 +0000 (14:34 +0000)
course/modedit.php
course/moodleform_mod.php
mod/glossary/mod_form.php

index fa01f3c804022350df7e498ae4c751ef86efe725..88b114ec671ce36a3fb7c8a687338567b344a303 100644 (file)
@@ -4,6 +4,7 @@
 
     require_once("../config.php");
     require_once("lib.php");
+    require_once($CFG->libdir.'/gradelib.php');
 
     require_login();
 
         $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);
 
         } 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");
             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");
index a0869614cb2320e0e94411416a9ecfd77fd6ef0e..f887318f815e42cc69fda5d6ef6be398aa43152e 100644 (file)
@@ -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();
     }
 
index cdef399e74a0b02a8d85a18b4239e9cc4cfaaebe..752ef26a32e5538c38d51cc1458bcd75542d7c4b 100644 (file)
@@ -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');