]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10288 some more improvements for the item edit form
authorskodak <skodak>
Mon, 2 Jul 2007 18:26:11 +0000 (18:26 +0000)
committerskodak <skodak>
Mon, 2 Jul 2007 18:26:11 +0000 (18:26 +0000)
grade/report/grader/edit_category.php
grade/report/grader/edit_category_form.php [new file with mode: 0644]
grade/report/grader/edit_item.php
grade/report/grader/edit_item_form.php

index c4372a2e5378056d2cc336511ce9e3117d884ff1..c00af909c5686132c9f12dcd95084a118b6447db 100644 (file)
@@ -2,7 +2,7 @@
 
 require_once '../../../config.php';
 require_once $CFG->libdir.'/gradelib.php';
-require_once $CFG->libdir.'/formslib.php';
+require_once 'edit_category_form.php';
 
 $courseid = required_param('courseid', PARAM_INT);
 $id       = optional_param('id', 0, PARAM_INT);
@@ -62,26 +62,3 @@ $mform->display();
 
 print_footer($course);
 die;
-
-
-class edit_category_form extends moodleform {
-    function definition() {
-        $mform =& $this->_form;
-
-        // visible elements
-        $mform->addElement('text', 'fullname', get_string('categoryname', 'grades'));
-
-        //TODO: add other elements
-
-        // hidden params
-        $mform->addElement('hidden', 'id', 0);
-        $mform->setType('id', PARAM_INT);
-
-        $mform->addElement('hidden', 'courseid', 0);
-        $mform->setType('courseid', PARAM_INT);
-
-//-------------------------------------------------------------------------------
-        // buttons
-        $this->add_action_buttons();
-    }
-}
diff --git a/grade/report/grader/edit_category_form.php b/grade/report/grader/edit_category_form.php
new file mode 100644 (file)
index 0000000..74e03f8
--- /dev/null
@@ -0,0 +1,27 @@
+<?php  //$Id$
+
+require_once $CFG->libdir.'/formslib.php';
+
+class edit_category_form extends moodleform {
+    function definition() {
+        $mform =& $this->_form;
+
+        // visible elements
+        $mform->addElement('text', 'fullname', get_string('categoryname', 'grades'));
+
+        //TODO: add other elements
+
+        // hidden params
+        $mform->addElement('hidden', 'id', 0);
+        $mform->setType('id', PARAM_INT);
+
+        $mform->addElement('hidden', 'courseid', 0);
+        $mform->setType('courseid', PARAM_INT);
+
+//-------------------------------------------------------------------------------
+        // buttons
+        $this->add_action_buttons();
+    }
+}
+
+?>
\ No newline at end of file
index 1da18656a6ca0b6ed665d6a3fc3ad985a4d55eeb..d46c299826dc7bd3104fdb134f3166fcf734bd54 100644 (file)
@@ -1,8 +1,7 @@
 <?php  //$Id$
 require_once '../../../config.php';
 require_once $CFG->libdir.'/gradelib.php';
-require_once $CFG->libdir.'/formslib.php';
-require_once ('edit_item_form.php');
+require_once 'edit_item_form.php';
 
 $courseid = required_param('courseid', PARAM_INT);
 $id       = optional_param('id', 0, PARAM_INT);
@@ -30,10 +29,15 @@ if ($mform->is_cancelled()) {
     redirect($returnurl);
 
 } else if ($data = $mform->get_data()) {
+    if (empty($data->checkbox)) {
+        $data->checkbox = 0; // work around the missing value if checkbox not selected
+    }
+
     $grade_item = new grade_item(array('id'=>$id, 'courseid'=>$course->id));
     grade_item::set_properties($grade_item, $data);
 
     if (empty($grade_item->id)) {
+        $grade_item->itemtype = 'manual'; // for all new items to be manual only
         $grade_item->insert();
 
     } else {
index d93376291745a0af3b3646a2bbbdd0ebb2e2ec25..0a30edb3b4d0cf09bddedf05ab440837d8d4bdc1 100644 (file)
@@ -1,42 +1,75 @@
-<?php
+<?php  //$Id$
+
+require_once $CFG->libdir.'/formslib.php';
+
 class edit_item_form extends moodleform {
     function definition() {
+        global $COURSE;
+
         $mform =& $this->_form;
-    
-        if ($id = $this->_customdata['id']) { // grade item id, if known
-            $item = get_record('grade_items', 'id', $id);
-        } else {
-            $item = NULL;
-        }
-              
-        $mform->addElement('header', 'general', get_string('gradeitem', 'form'));
-        // visible elements
+
+/// visible elements
+        $mform->addElement('header', 'general', get_string('gradeitem', 'grades'));
+
         $mform->addElement('text', 'itemname', get_string('itemname', 'grades'));
         $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades'));
         $mform->addElement('text', 'idnumber', get_string('idnumber'));
+
+        // allow setting of outcomes on module items too
+        $options = array(0=>get_string('usenooutcome', 'grades'));
+        if ($outcomes = grade_outcome::fetch_all(array('courseid'=>$COURSE->id))) {
+            foreach ($outcomes as $outcome) {
+                $options[$scale->id] = $outcome->get_name();
+            }
+        }
+        $mform->addElement('select', 'outcomeid', get_string('outcome', 'grades'), $options);
+
+        $options = array(GRADE_TYPE_NONE=>get_string('typenone', 'grades'),
+                          GRADE_TYPE_VALUE=>get_string('typevalue', 'grades'),
+                          GRADE_TYPE_SCALE=>get_string('typescale', 'grades'),
+                          GRADE_TYPE_TEXT=>get_string('typetyxt', 'grades'));
+        $mform->addElement('select', 'gradetype', get_string('gradetype', 'grades'), $options);
+        $mform->setDefault('gradetype', GRADE_TYPE_VALUE);
+
+        $mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
+        $mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
+        $mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
+
+        $options = array(0=>get_string('usenoscale', 'grades'));
+        if ($scales = get_records('scale')) {
+            foreach ($scales as $scale) {
+                $options[$scale->id] = format_string($scale->name);
+            }
+        }
+        $mform->addElement('select', 'scaleid', get_string('scale'), $options);
+        $mform->disabledIf('scaleid', 'gradetype', 'noteq', GRADE_TYPE_SCALE);
+
         $mform->addElement('text', 'grademax', get_string('grademax', 'grades'));
+        $mform->disabledIf('grademax', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
+        $mform->setDefault('grademin', 100);
+
         $mform->addElement('text', 'grademin', get_string('grademin', 'grades'));
+        $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
+        $mform->setDefault('grademin', 0);
+
         $mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
+        $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE);
+        $mform->setDefault('gradepass', 0);
+
         $mform->addElement('text', 'multfactor', get_string('multfactor', 'grades'));
+        $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
+        $mform->setDefault('multfactor', 1);
+
         $mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades'));
+        $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
+        $mform->setDefault('plusfactor', 0);
+
         $mform->addElement('checkbox', 'locked', get_string('locked', 'grades'));
-        
-        // new grade item, or existing manual grade item(?)
-        if (!$id || (!empty($item->scaleid) && $item->type == 'manual')) {
-            if ($scales = get_records('scale')) {
-                $soptions = array(0=>get_string('usenoscale', 'grades'));
-                foreach ($scales as $scale) {
-                    $soptions[$scale->id] = $scale->name;
-                }
-                $mform->addElement('select', 'scaleid', get_string('scale'), $soptions);
-            }
-        }
 
         $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
+        $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE);
 
-        // TOOD: outcomeid/calculations (only for new/manual/category?)
-
-        // hidden params
+/// hidden params
         $mform->addElement('hidden', 'id', 0);
         $mform->setType('id', PARAM_INT);
 
@@ -50,5 +83,41 @@ class edit_item_form extends moodleform {
         // buttons
         $this->add_action_buttons();
     }
+
+
+/// tweak the form - depending on existing data
+    function definition_after_data() {
+        global $CFG;
+
+        $mform =& $this->_form;
+
+        if ($id = $mform->getElementValue('id')) {
+            $grade_item = grade_item::fetch(array('id'=>$id));
+            if (!in_array($grade_item->itemtype, array('manual', 'course', 'category'))) {
+                // following items are set up from modules and should not be overrided by user
+                $mform->hardFreeze('itemname,idnumber,calculation,gradetype,grademax,grademin,scaleid');
+            }
+        }
+    }
+
+
+/// perform extra validation before submission
+    function validation($data){
+        $errors= array();
+
+        if ($data['calculation'] != '') {
+            if (strpos($data['calculation'], '=') !== 0) {
+                $errors['calculation'] = get_string('calculationerror', 'grades');
+                //TODO: add better formula validation
+            }
+        }
+
+        if (0 == count($errors)){
+            return true;
+        } else {
+            return $errors;
+        }
+    }
+
 }
 ?>
\ No newline at end of file