]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10364 initial calcualtion edit form
authorskodak <skodak>
Thu, 12 Jul 2007 20:12:06 +0000 (20:12 +0000)
committerskodak <skodak>
Thu, 12 Jul 2007 20:12:06 +0000 (20:12 +0000)
grade/edit/edit_calculation.php
grade/edit/edit_calculation_form.php

index 0055e265b08302061be2f71662a6b4525e1fc058..70cf97213a63bb8dcb8c31bcfe48e4f4895c9b53 100644 (file)
@@ -1,6 +1,59 @@
 <?php  //$Id$
+require_once '../../config.php';
+require_once $CFG->libdir.'/gradelib.php';
+require_once 'edit_calculation_form.php';
 
-echo "not implemented yet";
+$courseid = required_param('courseid', PARAM_INT);
+$id       = required_param('id', PARAM_INT);
 
+if (!$course = get_record('course', 'id', $courseid)) {
+    print_error('nocourseid');
+}
 
-?>
\ No newline at end of file
+require_login($course);
+
+$context = get_context_instance(CONTEXT_COURSE, $course->id);
+//require_capability() here!!
+
+// default return url
+//TODO: add proper return support
+$returnurl = $CFG->wwwroot.'/grade/report.php?report=grader&amp;id='.$course->id;
+
+if (!$grade_item = grade_item::fetch(array('id'=>$id, 'courseid'=>$course->id))) {
+    error('Incorect item id');
+}
+
+// module items and items without grade can not have calculation
+if ($grade_item->is_normal_item() or ($grade_item->gradetype != GRADE_TYPE_VALUE and $grade_item->gradetype != GRADE_TYPE_SCALE)) {
+    redirect($returnurl, get_string('erornocalculationallowed', 'grades')); //TODO: localize
+}
+
+$mform = new edit_calculation_form();
+
+if ($mform->is_cancelled()) {
+    redirect($returnurl);
+
+} else if (!$mform->is_submitted()) {
+    $calculation = grade_item::denormalize_formula($grade_item->calculation, $grade_item->courseid);
+    $mform->set_data(array('courseid'=>$grade_item->courseid, 'calculation'=>$calculation, 'id'=>$grade_item->id, 'itemname'=>$grade_item->itemname));
+
+} else if ($data = $mform->get_data()) {
+    $grade_item->set_calculation($data->calculation);
+    redirect($returnurl);
+}
+
+$strgrades          = get_string('grades');
+$strgraderreport    = get_string('graderreport', 'grades');
+$strcalculationedit = get_string('editcalculation', 'grades');
+
+$nav = array(array('name'=>$strgrades,'link'=>$CFG->wwwroot.'/grade/index.php?id='.$courseid, 'type'=>'misc'),
+             array('name'=>$strcalculationedit, 'link'=>'', 'type'=>'misc'));
+
+$navigation = build_navigation($nav);
+
+
+print_header_simple($strgrades . ': ' . $strgraderreport, ': ' . $strcalculationedit, $navigation, '', '', true, '', navmenu($course));
+
+$mform->display();
+
+print_footer($course);
\ No newline at end of file
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..dac6f10eaeb69a0edd5cc036838721bd18eb2159 100644 (file)
@@ -0,0 +1,48 @@
+<?php  //$Id$
+
+require_once $CFG->libdir.'/formslib.php';
+
+class edit_calculation_form extends moodleform {
+    function definition() {
+        global $COURSE;
+
+        $mform =& $this->_form;
+
+/// visible elements
+        $mform->addElement('header', 'general', get_string('gradeitem', 'grades'));
+
+        $mform->addElement('static', 'itemname', get_string('itemname', 'grades'));
+        $mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
+
+/// 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();
+    }
+
+/// perform extra validation before submission
+    function validation($data){
+        $errors= array();
+
+        if ($data['calculation'] != '') {
+            $grade_item = grade_item::fetch(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
+            $result = $grade_item->validate_formula($data['calculation']);
+            if ($result !== true) {
+                $errors['calculation'] = $result;
+            }
+        }
+        if (0 == count($errors)){
+            return true;
+        } else {
+            return $errors;
+        }
+    }
+
+}
+?>