From: skodak Date: Thu, 12 Jul 2007 20:12:06 +0000 (+0000) Subject: MDL-10364 initial calcualtion edit form X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=46fa681022fef85a5871bdfaad35733aec863cb3;p=moodle.git MDL-10364 initial calcualtion edit form --- diff --git a/grade/edit/edit_calculation.php b/grade/edit/edit_calculation.php index 0055e265b0..70cf97213a 100644 --- a/grade/edit/edit_calculation.php +++ b/grade/edit/edit_calculation.php @@ -1,6 +1,59 @@ 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&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 diff --git a/grade/edit/edit_calculation_form.php b/grade/edit/edit_calculation_form.php index e69de29bb2..dac6f10eae 100644 --- a/grade/edit/edit_calculation_form.php +++ b/grade/edit/edit_calculation_form.php @@ -0,0 +1,48 @@ +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; + } + } + +} +?>