Skip to content

Commit

Permalink
MDL-29482: added validation to rubric editor and the 'work in progres…
Browse files Browse the repository at this point in the history
…s' status
  • Loading branch information
marinaglancy committed Nov 2, 2011
1 parent 37f391f commit 2ae7faf
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 62 deletions.
13 changes: 13 additions & 0 deletions grade/grading/form/lib.php
Expand Up @@ -134,6 +134,19 @@ public function is_form_available() {
return false;
}

/**
* Returns a message why this form is unavailable. Maybe overriden by plugins to give more details.
* @see is_form_available()
*
* @return string
*/
public function form_unavailable_notification() {
if ($this->is_form_available()) {
return null;
}
return get_string('gradingformunavailable', 'grades');
}

/**
* Returns URL of a page where the grading form can be defined and edited.
*
Expand Down
5 changes: 1 addition & 4 deletions grade/grading/form/rubric/edit.php
Expand Up @@ -43,16 +43,13 @@
$PAGE->set_url(new moodle_url('/grade/grading/form/rubric/edit.php', array('areaid' => $areaid)));
$PAGE->set_title(get_string('definerubric', 'gradingform_rubric'));
$PAGE->set_heading(get_string('definerubric', 'gradingform_rubric'));
$PAGE->requires->js('/grade/grading/form/rubric/js/rubriceditor.js');

//TODO freeze rubric editor if needed
$mform = new gradingform_rubric_editrubric(null, array('areaid' => $areaid, 'context' => $context, 'freezerubric' => optional_param('freeze', 0, PARAM_INT)));
$mform = new gradingform_rubric_editrubric(null, array('areaid' => $areaid, 'context' => $context));
$data = $controller->get_definition_for_editing();
$returnurl = optional_param('returnurl', $manager->get_management_url(), PARAM_LOCALURL);
$data->returnurl = $returnurl;
$mform->set_data($data);
if ($mform->is_cancelled()) {
// todo process editing cancel in a better way
redirect($returnurl);
} else if ($mform->is_submitted() && $mform->is_validated()) {
$data = $mform->get_data();
Expand Down
68 changes: 56 additions & 12 deletions grade/grading/form/rubric/edit_form.php
Expand Up @@ -36,7 +36,7 @@
class gradingform_rubric_editrubric extends moodleform {

/**
* Form elements definition
* Form element definition
*/
public function definition() {
$form = $this->_form;
Expand All @@ -56,22 +56,66 @@ public function definition() {
$form->addElement('editor', 'description_editor', get_string('description', 'gradingform_rubric'), null, $options);
$form->setType('description_editor', PARAM_RAW);

// rubric completion status
$choices = array();
$choices[gradingform_controller::DEFINITION_STATUS_WORKINPROGRESS] = get_string('statusworkinprogress', 'gradingform_rubric');
$choices[gradingform_controller::DEFINITION_STATUS_PRIVATE] = get_string('statusprivate', 'gradingform_rubric');
$choices[gradingform_controller::DEFINITION_STATUS_PUBLIC] = get_string('statuspublic', 'gradingform_rubric');
$form->addElement('select', 'status', 'Current rubric status', $choices)->freeze();

// rubric editor
$element = $form->addElement('rubriceditor', 'rubric', get_string('rubric', 'gradingform_rubric'));
$form->setType('rubric', PARAM_RAW);
$form->addRule('rubric', '', 'rubriceditorcompleted'); //TODO how to add this rule automatically?????
if (array_key_exists('freezerubric', $this->_customdata) && $this->_customdata['freezerubric']) {
$element->freeze();
}
//$element->freeze(); // TODO freeze rubric editor if needed

// submit and cancel buttons
$buttonarray = array(
$form->createElement('submit', 'submitdraft', get_string('saveandcontinue', 'core_grading')),
$form->createElement('submit', 'submitfinal', get_string('saveandmakeready', 'core_grading')),
$form->createElement('cancel')
);
$buttonarray = array();
$buttonarray[] = &$form->createElement('submit', 'saverubric', get_string('saverubric', 'gradingform_rubric'));
$buttonarray[] = &$form->createElement('submit', 'saverubricdraft', get_string('saverubricdraft', 'gradingform_rubric'));
$buttonarray[] = &$form->createElement('cancel');
$form->addGroup($buttonarray, 'buttonar', '', array(' '), false);
$form->setType('buttonar', PARAM_RAW);
$form->closeHeaderBefore('buttonar');
}

/**
* Form vlidation.
* If there are errors return array of errors ("fieldname"=>"error message"),
* otherwise true if ok.
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK (true allowed for backwards compatibility too).
*/
function validation($data, $files) {
$err = parent::validation($data, $files);
$err = array();
$form = $this->_form;
$rubricel = $form->getElement('rubric');
if ($rubricel->non_js_button_pressed($data['rubric'])) {
// if JS is disabled and button such as 'Add criterion' is pressed - prevent from submit
$err['rubricdummy'] = 1;
} else if (isset($data['saverubric']) && $data['saverubric']) {
// If user attempts to make rubric active - it needs to be validated
if ($rubricel->validate($data['rubric']) !== false) {
$err['rubricdummy'] = 1;
}
}
return $err;
}

/**
* Return submitted data if properly submitted or returns NULL if validation fails or
* if there is no submitted data.
*
* @return object submitted data; NULL if not valid or not submitted or cancelled
*/
function get_data() {
$data = parent::get_data();
if (!empty($data->saverubric)) {
$data->status = gradingform_controller::DEFINITION_STATUS_PUBLIC; // TODO ???
} else if (!empty($data->saverubricdraft)) {
$data->status = gradingform_controller::DEFINITION_STATUS_WORKINPROGRESS;
}
return $data;
}
}
14 changes: 14 additions & 0 deletions grade/grading/form/rubric/lang/en/gradingform_rubric.php
Expand Up @@ -55,3 +55,17 @@
$string['showscorestudent'] = 'Display points for each level to those being graded';
$string['enableremarks'] = 'Allow grader to add text remarks for each criteria';
$string['showremarksstudent'] = 'Show remarks to those being graded';

$string['saverubric'] = 'Save rubric and make it available';
$string['saverubricdraft'] = 'Save as draft';

$string['statusworkinprogress'] = 'Work in progress';
$string['statusprivate'] = 'Private';
$string['statuspublic'] = 'Public';

$string['err_nocriteria'] = 'Rubric must contain at least one criterion';
$string['err_mintwolevels'] = 'Each criterion must have at least two levels';
$string['err_nodescription'] = 'Criterion description can not be empty';
$string['err_nodefinition'] = 'Level definition can not be empty';
$string['err_scoreformat'] = 'Number of points for each level must be a valid non-negative number';
$string['err_totalscore'] = 'Maximum number of points possible when graded by the rubric must be more than zero';
13 changes: 12 additions & 1 deletion grade/grading/form/rubric/lib.php
Expand Up @@ -136,6 +136,13 @@ public function update_definition(stdClass $newdefinition, $status = null, $user
}
}
foreach ($levelsdata as $levelid => $level) {
if (isset($level['score'])) {
$level['score'] = (float)$level['score'];
if ($level['score']<0) {
// TODO why we can't allow negative score for rubric?
$level['score'] = 0;
}
}
if (preg_match('/^NEWID\d+$/', $levelid)) {
// insert level into DB
$data = array('criterionid' => $id, 'definitionformat' => FORMAT_MOODLE); // TODO format is not supported yet
Expand Down Expand Up @@ -213,7 +220,11 @@ protected function load_definition() {
// pick the level data
if (!empty($record->rlid)) {
foreach (array('id', 'score', 'definition', 'definitionformat') as $fieldname) {
$this->definition->rubric_criteria[$record->rcid]['levels'][$record->rlid][$fieldname] = $record->{'rl'.$fieldname};
$value = $record->{'rl'.$fieldname};
if ($fieldname == 'score') {
$value = (float)$value; // To prevent display like 1.00000
}
$this->definition->rubric_criteria[$record->rcid]['levels'][$record->rlid][$fieldname] = $value;
}
}
}
Expand Down
27 changes: 21 additions & 6 deletions grade/grading/form/rubric/renderer.php
Expand Up @@ -27,7 +27,7 @@
/**
* Grading method plugin renderer
*/
class gradingform_rubric_renderer {
class gradingform_rubric_renderer extends plugin_renderer_base {

/**
* This function returns html code for displaying criterion. Depending on $mode it may be the
Expand Down Expand Up @@ -82,9 +82,17 @@ public function criterion_template($mode, $options, $elementname = '{NAME}', $cr
}
$description = $criterion['description'];
}
$criterion_template .= html_writer::tag('td', $description, array('class' => 'description', 'id' => '{NAME}-criteria-{CRITERION-id}-description'));
$descriptionclass = 'description';
if (isset($criterion['error_description'])) {
$descriptionclass .= ' error';
}
$criterion_template .= html_writer::tag('td', $description, array('class' => $descriptionclass, 'id' => '{NAME}-criteria-{CRITERION-id}-description'));
$levels_str_table = html_writer::tag('table', html_writer::tag('tr', $levels_str, array('id' => '{NAME}-criteria-{CRITERION-id}-levels')));
$criterion_template .= html_writer::tag('td', $levels_str_table, array('class' => 'levels'));
$levelsclass = 'levels';
if (isset($criterion['error_levels'])) {
$levelsclass .= ' error';
}
$criterion_template .= html_writer::tag('td', $levels_str_table, array('class' => $levelsclass));
if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
$value = get_string('criterionaddlevel', 'gradingform_rubric');
$button = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][addlevel]',
Expand Down Expand Up @@ -174,7 +182,11 @@ public function level_template($mode, $options, $elementname = '{NAME}', $criter
$level_template .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levelid]', 'value' => $level['id']));
}
$score = html_writer::tag('span', $score, array('id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-score'));
$level_template .= html_writer::tag('div', $definition, array('class' => 'definition', 'id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-definition'));
$definitionclass = 'definition';
if (isset($level['error_definition'])) {
$definitionclass .= ' error';
}
$level_template .= html_writer::tag('div', $definition, array('class' => $definitionclass, 'id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-definition'));
$displayscore = true;
if (!$options['showscoreteacher'] && in_array($mode, array(gradingform_rubric_controller::DISPLAY_EVAL, gradingform_rubric_controller::DISPLAY_EVAL_FROZEN, gradingform_rubric_controller::DISPLAY_REVIEW))) {
$displayscore = false;
Expand All @@ -183,7 +195,11 @@ public function level_template($mode, $options, $elementname = '{NAME}', $criter
$displayscore = false;
}
if ($displayscore) {
$level_template .= html_writer::tag('div', $score. get_string('scorepostfix', 'gradingform_rubric'), array('class' => 'score'));
$scoreclass = 'score';
if (isset($level['error_score'])) {
$scoreclass .= ' error';
}
$level_template .= html_writer::tag('div', $score. get_string('scorepostfix', 'gradingform_rubric'), array('class' => $scoreclass));
}
if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
$value = get_string('leveldelete', 'gradingform_rubric');
Expand Down Expand Up @@ -333,7 +349,6 @@ public function display_rubric($criteria, $options, $mode, $elementname = null,
}
foreach ($criterion['levels'] as $levelid => $level) {
$level['id'] = $levelid;
$level['score'] = (float)$level['score']; // otherwise the display will look like 1.00000
$level['class'] = $this->get_css_class_suffix($levelcnt++, sizeof($criterion['levels']) -1);
$level['checked'] = (isset($criterionvalue['levelid']) && ((int)$criterionvalue['levelid'] === $levelid));
if ($level['checked'] && ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN || $mode == gradingform_rubric_controller::DISPLAY_REVIEW || $mode == gradingform_rubric_controller::DISPLAY_VIEW)) {
Expand Down

0 comments on commit 2ae7faf

Please sign in to comment.