MDL-10364 initial calcualtion edit form
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user