MDL-10288 some more improvements for the item edit form

This commit is contained in:
skodak 2007-07-02 18:26:11 +00:00
parent 7934f019af
commit 8d27270849
4 changed files with 131 additions and 54 deletions

View 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();
}
}

View File

@ -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();
}
}
?>

View 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 {

View 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'));
$mform->addElement('text', 'grademax', get_string('grademax', 'grades'));
$mform->addElement('text', 'grademin', get_string('grademin', 'grades'));
$mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
$mform->addElement('text', 'multfactor', get_string('multfactor', 'grades'));
$mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades'));
$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);
// 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'));
$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;
}
}
}
?>