$mform = new edit_item_form(qualified_me(), array('id'=>$id));
if ($item = get_record('grade_items', 'id', $id, 'courseid', $course->id)) {
+ $item->calculation = grade_item::denormalize_formula($item->calculation, $course->id);
$mform->set_data($item);
+
} else {
$mform->set_data(array('courseid'=>$course->id, 'itemtype'=>'manual'));
}
$data->checkbox = 0; // work around the missing value if checkbox not selected
}
+ if (array_key_exists('calculation', $data)) {
+ $data->calculation = grade_item::normalize_formula($data->calculation, $course->id);
+ }
+
$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->itemtype = 'manual'; // all new items to be manual only
$grade_item->insert();
} else {
* Also if user changes the idnumber the formula does not need to be updated.
*/
- // first detect if we need to update calculation formula from [idnumber] to [#giXXX#] (after backup, etc.)
- if (!$this->calculation_normalized and preg_match_all('/\[(?!#gi)(.*?)\]/', $this->calculation, $matches)) {
- foreach ($matches[1] as $idnumber) {
- if ($grade_item = grade_item::fetch(array('courseid'=>$this->courseid, 'idnumber'=>$idnumber))) {
- $this->calculation = str_replace('['.$grade_item->idnumber.']', '[#gi'.$grade_item->id.'#]', $this->calculation);
- }
- }
- $this->update(); // update in db if needed
- $this->calculation_normalized = true;
- return !empty($this->calculation);
+ // first detect if we need to change calculation formula from [idnumber] to [#giXXX#] (after backup, etc.)
+ if (!$this->calculation_normalized and preg_match('/\[(?!#gi)(.*?)\]/', $this->calculation)) {
+ $this->set_calculation($this->calculation);
}
- return true;
+ return !empty($this->calculation);
}
/**
*/
function get_calculation() {
if ($this->is_calculated()) {
- $formula = $this->calculation;
- // denormalize formula - convert [#giXX#] to [idnumber]
- if (preg_match_all('/\[#gi([0-9]+)#\]/', $formula, $matches)) {
- foreach ($matches[1] as $id) {
- if ($grade_item = grade_item::fetch(array('id'=>$id))) {
- if (!empty($grade_item->idnumber)) {
- $formula = str_replace('[#gi'.$grade_item->id.'#]', '['.$grade_item->idnumber.']', $formula);
- }
- }
- }
- }
-
- return $formula;
+ return grade_item::denormalize_formula($this->calculation, $this->courseid);
} else {
return NULL;
* @return boolean success
*/
function set_calculation($formula) {
- $formula = trim($formula);
+ $this->calculation = grade_item::normalize_formula($formula, $this->courseid);
+ $this->calculation_normalized = true;
+ return $this->update();
+ }
+ /**
+ * Denormalizes the calculation formula to [idnumber] form
+ * @param string $formula
+ * @return string denormalized string
+ */
+ function denormalize_formula($formula, $courseid) {
if (empty($formula)) {
- $this->calculation = NULL;
-
- } else {
- if (strpos($formula, '=') !== 0) {
- $formula = '='.$formula;
- }
+ return '';
+ }
- // normalize formula - we want grade item ids [#giXXX#] instead of [idnumber]
- if ($grade_items = grade_item::fetch_all(array('courseid'=>$this->courseid))) {
- foreach ($grade_items as $grade_item) {
- $formula = str_replace('['.$grade_item->idnumber.']', '[#gi'.$grade_item->id.'#]', $formula);
+ // denormalize formula - convert [#giXX#] to [idnumber]
+ if (preg_match_all('/\[#gi([0-9]+)#\]/', $formula, $matches)) {
+ foreach ($matches[1] as $id) {
+ if ($grade_item = grade_item::fetch(array('id'=>$id, 'courseid'=>$courseid))) {
+ if (!empty($grade_item->idnumber)) {
+ $formula = str_replace('[#gi'.$grade_item->id.'#]', '['.$grade_item->idnumber.']', $formula);
+ }
}
}
+ }
+
+ return $formula;
+
+ }
+
+ /**
+ * Normalizes the calculation formula to [#giXX#] form
+ * @param string $formula
+ * @return string normalized string
+ */
+ function normalize_formula($formula, $courseid) {
+ $formula = trim($formula);
+
+ if (empty($formula)) {
+ return NULL;
- $this->calculation = $formula;
}
- $this->calculation_normalized = true;
- return $this->update();
+ // normalize formula - we want grade item ids [#giXXX#] instead of [idnumber]
+ if ($grade_items = grade_item::fetch_all(array('courseid'=>$courseid))) {
+ foreach ($grade_items as $grade_item) {
+ $formula = str_replace('['.$grade_item->idnumber.']', '[#gi'.$grade_item->id.'#]', $formula);
+ }
+ }
+
+ return $formula;
}
/**