MDL-46576 core_grades: MDL-47006 Refactor weightings code for readibility

This commit is contained in:
John Okely 2014-09-04 15:08:13 +08:00 committed by Adrian Greeve
parent ded8ea8fd1
commit b6a1e366a7
7 changed files with 62 additions and 55 deletions

View File

@ -75,12 +75,15 @@ if ($id) {
$category->grade_item_gradepass = format_float($category->grade_item_gradepass, $decimalpoints);
$category->grade_item_multfactor = format_float($category->grade_item_multfactor, 4);
$category->grade_item_plusfactor = format_float($category->grade_item_plusfactor, 4);
$category->grade_item_aggregationcoef2 = format_float($category->grade_item_aggregationcoef2, 4);
if (!$parent_category) {
// keep as is
} else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM or $parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
$category->grade_item_aggregationcoef = $category->grade_item_aggregationcoef == 0 ? 0 : 1;
if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
$category->grade_item_weight = format_float($category->grade_item_aggregationcoef2 * 100, 4);
}
unset($category->grade_item_aggregationcoef2);
} else {
$category->grade_item_aggregationcoef = format_float($category->grade_item_aggregationcoef, 4);
}
@ -105,6 +108,10 @@ if ($mform->is_cancelled()) {
redirect($returnurl);
} else if ($data = $mform->get_data(false)) {
if (isset($data->grade_item_weight)) {
$data->grade_item_aggregationcoef2 = $data->grade_item_weight / 100.0;
unset($data->grade_item_weight);
}
// If no fullname is entered for a course category, put ? in the DB
if (!isset($data->fullname) || $data->fullname == '') {
$data->fullname = '?';
@ -161,7 +168,7 @@ if ($mform->is_cancelled()) {
unset($itemdata->locked);
unset($itemdata->locktime);
$convert = array('grademax', 'grademin', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef', 'aggregationcoef2');
$convert = array('grademax', 'grademin', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef', 'weight');
foreach ($convert as $param) {
if (property_exists($itemdata, $param)) {
$itemdata->$param = unformat_float($itemdata->$param);

View File

@ -164,10 +164,10 @@ class edit_category_form extends moodleform {
$mform->addElement('checkbox', 'grade_item_weightoverride', get_string('adjustedweight', 'grades'));
$mform->addHelpButton('grade_item_weightoverride', 'weightoverride', 'grades');
$mform->addElement('text', 'grade_item_aggregationcoef2', get_string('weight', 'grades'));
$mform->addHelpButton('grade_item_aggregationcoef2', 'weight', 'grades');
$mform->setType('grade_item_aggregationcoef2', PARAM_RAW);
$mform->disabledIf('grade_item_aggregationcoef2', 'grade_item_weightoverride');
$mform->addElement('text', 'grade_item_weight', get_string('weight', 'grades'));
$mform->addHelpButton('grade_item_weight', 'weight', 'grades');
$mform->setType('grade_item_weight', PARAM_RAW);
$mform->disabledIf('grade_item_weight', 'grade_item_weightoverride');
$mform->addElement('text', 'grade_item_gradepass', get_string('gradepass', 'grades'));
$mform->setType('grade_item_gradepass', PARAM_RAW);
@ -444,8 +444,8 @@ class edit_category_form extends moodleform {
if ($mform->elementExists('grade_item_weightoverride')) {
$mform->removeElement('grade_item_weightoverride');
}
if ($mform->elementExists('grade_item_aggregationcoef2')) {
$mform->removeElement('grade_item_aggregationcoef2');
if ($mform->elementExists('grade_item_weight')) {
$mform->removeElement('grade_item_weight');
}
} else {
if ($grade_item->is_category_item()) {
@ -465,8 +465,9 @@ class edit_category_form extends moodleform {
$coefstring = $grade_item->get_coefstring();
if ($coefstring == 'aggregationcoefextrasum') {
if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
// advcheckbox is not compatible with disabledIf!
$coefstring = 'aggregationcoefextrasum';
$element =& $mform->createElement('checkbox', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
} else {
$element =& $mform->createElement('text', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
@ -480,8 +481,8 @@ class edit_category_form extends moodleform {
if ($mform->elementExists('grade_item_weightoverride')) {
$mform->removeElement('grade_item_weightoverride');
}
if ($mform->elementExists('grade_item_aggregationcoef2')) {
$mform->removeElement('grade_item_aggregationcoef2');
if ($mform->elementExists('grade_item_weight')) {
$mform->removeElement('grade_item_weight');
}
}
}

View File

@ -259,17 +259,13 @@ if ($data = data_submitted() and confirm_sesskey()) {
$recreatetree = true;
// Grade item text inputs
} elseif (preg_match('/^(grademax|aggregationcoef|aggregationcoef2|multfactor|plusfactor)_([0-9]+)$/', $key, $matches)) {
} elseif (preg_match('/^(grademax|aggregationcoef|weight|multfactor|plusfactor)_([0-9]+)$/', $key, $matches)) {
$param = $matches[1];
$aid = $matches[2];
$value = unformat_float($value);
$value = clean_param($value, PARAM_FLOAT);
if (preg_match('/^(aggregationcoef2)_([0-9]+)$/', $key, $matches)) {
$value = $value / 100.0;
}
$grade_item = grade_item::fetch(array('id' => $aid, 'courseid' => $courseid));
if ($param === 'grademax' and $value < $grade_item->grademin) {
@ -277,10 +273,14 @@ if ($data = data_submitted() and confirm_sesskey()) {
$value = $grade_item->grademin;
}
if ($param === 'aggregationcoef2' && round($grade_item->aggregationcoef2, 4) != round($value, 4)) {
$grade_item->weightoverride = 1;
// Convert weight to aggregation coef2.
if ($param === 'weight') {
$value = $value / 100.0;
if (round($grade_item->aggregationcoef2, 4) != round($value, 4)) {
$grade_item->weightoverride = 1;
}
$param = 'aggregationcoef2';
}
$grade_item->$param = $value;
$grade_item->update();

View File

@ -97,6 +97,11 @@ if ($parent_category->aggregation == GRADE_AGGREGATE_SUM or $parent_category->ag
} else {
$item->aggregationcoef = format_float($item->aggregationcoef, 4);
}
if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
$item->weight = format_float($item->aggregationcoef2 * 100.0);
} else if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
$item->weight = format_float($item->aggregationcoef);
}
$item->cancontrolvisibility = $grade_item->can_control_visibility();
$mform = new edit_item_form(null, array('current'=>$item, 'gpr'=>$gpr));
@ -105,6 +110,10 @@ if ($mform->is_cancelled()) {
redirect($returnurl);
} else if ($data = $mform->get_data(false)) {
if (isset($data->weight)) {
$data->aggregationcoef2 = $data->weight / 100.0;
unset($data->weight);
}
// If unset, give the aggregationcoef a default based on parent aggregation method
if (!isset($data->aggregationcoef) || $data->aggregationcoef == '') {
if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
@ -132,7 +141,7 @@ if ($mform->is_cancelled()) {
unset($data->locked);
unset($data->locktime);
$convert = array('grademax', 'grademin', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef', 'aggregationcoef2');
$convert = array('grademax', 'grademin', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef', 'weight');
foreach ($convert as $param) {
if (property_exists($data, $param)) {
$data->$param = unformat_float($data->$param);
@ -140,6 +149,7 @@ if ($mform->is_cancelled()) {
}
$grade_item = new grade_item(array('id'=>$id, 'courseid'=>$courseid));
$previouslyoverridden = $grade_item->weightoverride;
grade_item::set_properties($grade_item, $data);
$grade_item->outcomeid = null;
@ -148,15 +158,11 @@ if ($mform->is_cancelled()) {
$grade_item->decimals = null;
}
// Change weightoverride flag.
if (!isset($data->weightoverride)) {
$data->weightoverride = 0; // Checkbox unticked.
}
// If we are using natural weight and the weight has been un-overriden, force parent category to recalculate weights.
if ($grade_item->weightoverride != $data->weightoverride && $parent_category->aggregation == GRADE_AGGREGATE_SUM) {
if (isset($data->weightoverride) && $previouslyoverridden != $data->weightoverride &&
$parent_category->aggregation == GRADE_AGGREGATE_SUM) {
$parent_category->force_regrading();
}
$grade_item->weightoverride = $data->weightoverride;
if (empty($grade_item->id)) {
$grade_item->itemtype = 'manual'; // all new items to be manual only

View File

@ -97,13 +97,13 @@ class edit_item_form extends moodleform {
$mform->setType('grademin', PARAM_RAW);
}
$mform->addElement('checkbox', 'weightoverride', get_string('adjustedweight', 'grades'));
$mform->addElement('advcheckbox', 'weightoverride', get_string('adjustedweight', 'grades'));
$mform->addHelpButton('weightoverride', 'weightoverride', 'grades');
$mform->addElement('text', 'aggregationcoef2', get_string('weight', 'grades'));
$mform->addHelpButton('aggregationcoef2', 'weight', 'grades');
$mform->setType('aggregationcoef2', PARAM_RAW);
$mform->disabledIf('aggregationcoef2', 'weightoverride');
$mform->addElement('text', 'weight', get_string('weight', 'grades'));
$mform->addHelpButton('weight', 'weight', 'grades');
$mform->setType('weight', PARAM_RAW);
$mform->disabledIf('weight', 'weightoverride');
$mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
$mform->addHelpButton('gradepass', 'gradepass', 'grades');
@ -286,21 +286,22 @@ class edit_item_form extends moodleform {
$coefstring = $grade_item->get_coefstring();
if ($coefstring !== '') {
if ($coefstring == 'aggregationcoefextrasum') {
if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
// advcheckbox is not compatible with disabledIf!
$coefstring = 'aggregationcoefextrasum';
$element =& $mform->createElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
} else {
$element =& $mform->createElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
$element =& $mform->createElement('text', 'weight', get_string($coefstring, 'grades'));
}
if ($mform->elementExists('parentcategory')) {
$mform->insertElementBefore($element, 'parentcategory');
} else {
$mform->insertElementBefore($element, 'id');
}
$mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
$mform->addHelpButton('weight', $coefstring, 'grades');
}
$mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $parent_category->id);
$mform->disabledIf('weight', 'parentcategory', 'eq', $parent_category->id);
}
// Remove fields used by natural weighting if the parent category is not using natural weighting.
@ -308,8 +309,8 @@ class edit_item_form extends moodleform {
if ($mform->elementExists('weightoverride')) {
$mform->removeElement('weightoverride');
}
if ($mform->elementExists('aggregationcoef2')) {
$mform->removeElement('aggregationcoef2');
if ($mform->elementExists('weight')) {
$mform->removeElement('weight');
}
}

View File

@ -46,8 +46,6 @@ class grade_edit_tree {
public $uses_weight = false;
public $uses_weight2 = false;
public $table;
public $categories = array();
@ -75,12 +73,8 @@ class grade_edit_tree {
$this->columns = array(grade_edit_tree_column::factory('name', array('deepest_level' => $this->deepest_level)),
grade_edit_tree_column::factory('aggregation', array('flag' => true)));
if ($this->uses_weight2) {
$this->columns[] = grade_edit_tree_column::factory('weight', array('adv' => 'aggregationcoef2'));
}
if ($this->uses_weight) {
$this->columns[] = grade_edit_tree_column::factory('weight', array('adv' => 'aggregationcoef'));
$this->columns[] = grade_edit_tree_column::factory('weight', array('adv' => 'weight'));
}
if ($this->uses_extra_credit) {
$this->columns[] = grade_edit_tree_column::factory('extracredit', array('adv' => 'aggregationcoef'));
@ -386,23 +380,23 @@ class grade_edit_tree {
if ((($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoef') && $type == 'weight') ||
($aggcoef == 'aggregationcoefextraweight' && $type == 'extra')) {
return '<label class="accesshide" for="aggregationcoef_'.$item->id.'">'.
return '<label class="accesshide" for="weight_'.$item->id.'">'.
get_string('extracreditvalue', 'grades', $item->itemname).'</label>'.
'<input type="text" size="6" id="aggregationcoef_'.$item->id.'" name="aggregationcoef_'.$item->id.'"
'<input type="text" size="6" id="weight_'.$item->id.'" name="aggregationcoef_'.$item->id.'"
value="'.grade_edit_tree::format_number($item->aggregationcoef).'" />';
} elseif ($aggcoef == 'aggregationcoefextrasum' && $type == 'extra') {
} elseif (($aggcoef == 'aggregationcoefextrasum' || $aggcoef == 'aggregationcoefextraweightsum') && $type == 'extra') {
$checked = ($item->aggregationcoef > 0) ? 'checked="checked"' : '';
return '<input type="hidden" name="extracredit_'.$item->id.'" value="0" />
<label class="accesshide" for="extracredit_'.$item->id.'">'.
get_string('extracreditvalue', 'grades', $item->itemname).'</label>
<input type="checkbox" id="extracredit_'.$item->id.'" name="extracredit_'.$item->id.'" value="1" '."$checked />\n";
} else if ($aggcoef == 'aggregationcoefextrasum' && $type == 'weight') {
} else if ($aggcoef == 'aggregationcoefextraweightsum' && $type == 'weight') {
$label = '';
if ($item->weightoverride && $parent_category->aggregation == GRADE_AGGREGATE_SUM) {
$label = get_string('adjusted', 'grades');
}
$name = 'aggregationcoef2_' . $item->id;
$name = 'weight_' . $item->id;
$hiddenlabel = html_writer::tag(
'label',
get_string('weight', 'grades', $item->itemname),
@ -521,13 +515,11 @@ class grade_edit_tree {
$level++;
$coefstring = $element['object']->get_coefstring();
if ($element['type'] == 'category') {
if ($coefstring == 'aggregationcoefweight') {
if ($coefstring == 'aggregationcoefweight' || $coefstring == 'aggregationcoefextraweightsum') {
$this->uses_weight = true;
} elseif ($coefstring == 'aggregationcoefextraweight' || $coefstring == 'aggregationcoefextrasum') {
$this->uses_extra_credit = true;
}
if ($coefstring == 'aggregationcoefextrasum') { // TODO: coefstring2?
$this->uses_weight2 = true;
if ($coefstring == 'aggregationcoefextraweight' || $coefstring == 'aggregationcoefextraweightsum' || $coefstring == 'aggregationcoefextrasum') {
$this->uses_extra_credit = true;
}
foreach($element['children'] as $child_el) {

View File

@ -1467,7 +1467,7 @@ class grade_category extends grade_object {
$this->coefstring = 'aggregationcoefextraweight';
} else if ($this->aggregation == GRADE_AGGREGATE_SUM) {
$this->coefstring = 'aggregationcoefextrasum';
$this->coefstring = 'aggregationcoefextraweightsum';
} else {
$this->coefstring = 'aggregationcoef';