/** * Check if there are changes in the guide and it is needed to ask user whether to * mark the current grades for re-grading. User may confirm re-grading and continue, * return to editing or cancel the changes * * @param gradingform_guide_controller $controller */ public function need_confirm_regrading($controller) { $data = $this->get_data(); if (isset($data->guide['regrade'])) { // We have already displayed the confirmation on the previous step. return false; } if (!isset($data->saveguide) || !$data->saveguide) { // We only need confirmation when button 'Save guide' is pressed. return false; } if (!$controller->has_active_instances()) { // Nothing to re-grade, confirmation not needed. return false; } $changelevel = $controller->update_or_check_guide($data); if ($changelevel == 0) { // No changes in the guide, no confirmation needed. return false; } // Freeze form elements and pass the values in hidden fields. // TODO description_editor does not freeze the normal way! $form = $this->_form; foreach (array('guide', 'name') as $fieldname) { $el =& $form->getElement($fieldname); $el->freeze(); $el->setPersistantFreeze(true); if ($fieldname == 'guide') { $el->add_regrade_confirmation($changelevel); } } // Replace button text 'saveguide' and unfreeze 'Back to edit' button. $this->findbutton('saveguide')->setValue(get_string('continue')); $el =& $this->findbutton('editguide'); $el->setValue(get_string('backtoediting', 'gradingform_guide')); $el->unfreeze(); return true; }
/** * Prepares the data passed in $_POST: * - processes the pressed buttons 'addlevel', 'addcriterion', 'moveup', 'movedown', 'delete' (when JavaScript is disabled) * sets $this->nonjsbuttonpressed to true/false if such button was pressed * - if options not passed (i.e. we create a new guide) fills the options array with the default values * - if options are passed completes the options array with unchecked checkboxes * - if $withvalidation is set, adds 'error_xxx' attributes to elements that contain errors and creates an error string * and stores it in $this->validationerrors * * @param array $value * @param boolean $withvalidation whether to enable data validation * @return array */ protected function prepare_data($value = null, $withvalidation = false) { if (null === $value) { $value = $this->getValue(); } if ($this->nonjsbuttonpressed === null) { $this->nonjsbuttonpressed = false; } $errors = array(); $return = array('criteria' => array(), 'options' => gradingform_guide_controller::get_default_options(), 'comments' => array()); if (!isset($value['criteria'])) { $value['criteria'] = array(); $errors['err_nocriteria'] = 1; } // If options are present in $value, replace default values with submitted values. if (!empty($value['options'])) { foreach (array_keys($return['options']) as $option) { // Special treatment for checkboxes. if (!empty($value['options'][$option])) { $return['options'][$option] = $value['options'][$option]; } else { $return['options'][$option] = null; } } } if (is_array($value)) { // For other array keys of $value no special treatmeant neeeded, copy them to return value as is. foreach (array_keys($value) as $key) { if ($key != 'options' && $key != 'criteria' && $key != 'comments') { $return[$key] = $value[$key]; } } } // Iterate through criteria. $lastaction = null; $lastid = null; foreach ($value['criteria'] as $id => $criterion) { if ($id == 'addcriterion') { $id = $this->get_next_id(array_keys($value['criteria'])); $criterion = array('description' => ''); $this->nonjsbuttonpressed = true; } if ($withvalidation && !array_key_exists('delete', $criterion)) { if (!strlen(trim($criterion['shortname']))) { $errors['err_noshortname'] = 1; $criterion['error_description'] = true; } if (strlen(trim($criterion['shortname'])) > 255) { $errors['err_shortnametoolong'] = 1; $criterion['error_description'] = true; } if (!strlen(trim($criterion['maxscore']))) { $errors['err_nomaxscore'] = 1; $criterion['error_description'] = true; } else { if (!is_numeric($criterion['maxscore']) || $criterion['maxscore'] < 0) { $errors['err_maxscorenotnumeric'] = 1; $criterion['error_description'] = true; } } } if (array_key_exists('moveup', $criterion) || $lastaction == 'movedown') { unset($criterion['moveup']); if ($lastid !== null) { $lastcriterion = $return['criteria'][$lastid]; unset($return['criteria'][$lastid]); $return['criteria'][$id] = $criterion; $return['criteria'][$lastid] = $lastcriterion; } else { $return['criteria'][$id] = $criterion; } $lastaction = null; $lastid = $id; $this->nonjsbuttonpressed = true; } else { if (array_key_exists('delete', $criterion)) { $this->nonjsbuttonpressed = true; } else { if (array_key_exists('movedown', $criterion)) { unset($criterion['movedown']); $lastaction = 'movedown'; $this->nonjsbuttonpressed = true; } $return['criteria'][$id] = $criterion; $lastid = $id; } } } // Add sort order field to criteria. $csortorder = 1; foreach (array_keys($return['criteria']) as $id) { $return['criteria'][$id]['sortorder'] = $csortorder++; } // Iterate through comments. $lastaction = null; $lastid = null; if (!empty($value['comments'])) { foreach ($value['comments'] as $id => $comment) { if ($id == 'addcomment') { $id = $this->get_next_id(array_keys($value['comments'])); $comment = array('description' => ''); $this->nonjsbuttonpressed = true; } if (array_key_exists('moveup', $comment) || $lastaction == 'movedown') { unset($comment['moveup']); if ($lastid !== null) { $lastcomment = $return['comments'][$lastid]; unset($return['comments'][$lastid]); $return['comments'][$id] = $comment; $return['comments'][$lastid] = $lastcomment; } else { $return['comments'][$id] = $comment; } $lastaction = null; $lastid = $id; $this->nonjsbuttonpressed = true; } else { if (array_key_exists('delete', $comment)) { $this->nonjsbuttonpressed = true; } else { if (array_key_exists('movedown', $comment)) { unset($comment['movedown']); $lastaction = 'movedown'; $this->nonjsbuttonpressed = true; } $return['comments'][$id] = $comment; $lastid = $id; } } } // Add sort order field to comments. $csortorder = 1; foreach (array_keys($return['comments']) as $id) { $return['comments'][$id]['sortorder'] = $csortorder++; } } // Create validation error string (if needed). if ($withvalidation) { if (count($errors)) { $rv = array(); foreach ($errors as $error => $v) { $rv[] = get_string($error, 'gradingform_guide'); } $this->validationerrors = join('<br/ >', $rv); } else { $this->validationerrors = false; } $this->wasvalidated = true; } return $return; }