/// perform extra validation before submission
function validation($data){
-
+
global $CFG;
-
+
$errors= array();
-
+
// we can not allow 2 scales with the same exact scale as this creates
// problems for backup/restore
$courseid = empty($data['courseid'])?0:$data['courseid'];
if (count_records('scale', 'courseid', $courseid, 'scale', $data['scale'])) {
- $errors['scale'] = get_string('duplicatescale', 'grades');
- }
+ $errors['scale'] = get_string('duplicatescale', 'grades');
+ }
$options = explode(',', $data['scale']);
if (count($options) < 2) {
if ($mform->is_cancelled()) {
redirect($returnurl);
-} else if (!$mform->is_submitted()) {
- $calculation = grade_item::denormalize_formula($grade_item->calculation, $grade_item->courseid);
- $mform->set_data(array('courseid'=>$grade_item->courseid, 'calculation'=>$calculation, 'id'=>$grade_item->id, 'itemname'=>$grade_item->itemname));
+}
+
+$calculation = grade_item::denormalize_formula($grade_item->calculation, $grade_item->courseid);
+$mform->set_data(array('courseid'=>$grade_item->courseid, 'calculation'=>$calculation, 'id'=>$grade_item->id, 'itemname'=>$grade_item->itemname));
-} else if ($data = $mform->get_data()) {
+if ($data = $mform->get_data()) {
$grade_item->set_calculation($data->calculation);
redirect($returnurl);
}
require_once $CFG->libdir.'/formslib.php';
class edit_calculation_form extends moodleform {
+ var $available;
+ var $noidnumbers;
+ var $showing;
+
function definition() {
global $COURSE;
$mform =& $this->_form;
+ $this->available = grade_item::fetch_all(array('courseid'=>$COURSE->id));
+ $this->noidnumbers = array();
+ foreach ($this->available as $item) {
+ if (empty($item->idnumber)) {
+ $this->noidnumbers[$item->id] = $item;
+ unset($this->available[$item->id]);
+ }
+ }
+
/// visible elements
$mform->addElement('header', 'general', get_string('gradeitem', 'grades'));
$mform->addElement('static', 'itemname', get_string('itemname', 'grades'));
- $mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
+ $mform->addElement('textarea', 'calculation', get_string('calculation', 'grades'), 'cols="60" rows="5"');
+
+/// idnumbers
+ $mform->addElement('header', 'availableheader', get_string('availableidnumbers', 'grades'));
+ foreach ($this->available as $item) {
+ $mform->addElement('static', 'idnumber_'.$item->id, $item->get_name());
+ $mform->setDefault('idnumber_'.$item->id, '[['.$item->idnumber.']]');
+ }
+
+
+/// set idnumbers
+ if ($this->noidnumbers) {
+ $mform->addElement('header', 'addidnumbersheader', get_string('addidnumbers', 'grades'));
+ foreach ($this->noidnumbers as $item) {
+ $mform->addElement('text', 'idnumber_'.$item->id, $item->get_name(), 'size="30"');
+ }
+ }
+
/// hidden params
$mform->addElement('hidden', 'id', 0);
$mform->addElement('hidden', 'courseid', 0);
$mform->setType('courseid', PARAM_INT);
+ $mform->addElement('hidden', 'showingadding', 0);
+
+
/// add return tracking info
$gpr = $this->_customdata['gpr'];
$gpr->add_mform_elements($mform);
//-------------------------------------------------------------------------------
// buttons
+ if ($this->noidnumbers) {
+ $mform->addElement('submit', 'addidnumbers', get_string('addidnumbers', 'grades'));
+ $mform->registerNoSubmitButton('addidnumbers');
+ }
$this->add_action_buttons();
}
+ function definition_after_data() {
+ global $CFG, $COURSE;
+
+ $mform =& $this->_form;
+
+ if ($this->noidnumbers) {
+ if (optional_param('addidnumbers', 0, PARAM_RAW)) {
+ $el =& $mform->getElement('showingadding');
+ $el->setValue(1);
+ }
+
+ $this->showing = $mform->getElementValue('showingadding');
+ if (!$this->showing) {
+ foreach ($this->noidnumbers as $item) {
+ $mform->removeElement('idnumber_'.$item->id);
+ }
+ $mform->removeElement('addidnumbersheader');
+ } else {
+ $mform->removeElement('addidnumbers');
+ }
+ }
+ }
+
/// perform extra validation before submission
function validation($data){
- $errors= array();
+ $errors = array();
+
+ //first validate and store the new idnubmers
+ if ($this->noidnumbers and $this->showing) {
+ foreach ($this->noidnumbers as $item) {
+ if (!empty($data['idnumber_'.$item->id])) {
+ if(!$item->add_idnumber(stripslashes($data['idnumber_'.$item->id]))) {
+ $errors['idnumber_'.$item->id] = get_string('error');
+ }
+ }
+ }
+ }
if ($data['calculation'] != '') {
$grade_item = grade_item::fetch(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
- $result = $grade_item->validate_formula($data['calculation']);
+ $result = $grade_item->validate_formula(stripslashes($data['calculation']));
if ($result !== true) {
$errors['calculation'] = $result;
}
$icon = '<img src="'.$CFG->pixpath.'/t/edit.gif" class="icon" alt="'.get_string('manualgrade', 'grades').'"/>'; // TODO: localize
} else {
$icon = '<img src="'.$CFG->pixpath.'/i/outcomes.gif" class="icon" alt="'.get_string('outcome', 'grades').'"/>';
-
+
}
}
break;
// following items are set up from modules and should not be overrided by user
$mform->hardFreeze('itemname,idnumber,gradetype,grademax,grademin,scaleid');
$mform->removeElement('calculation');
-
+
} else if ($grade_item->is_manual_item()) {
// manual grade item does not use these - uses only final grades
$mform->removeElement('plusfactor');
$mform->removeElement('multfactor');
-
}
}
//remove the aggregation coef element if not needed
$string['enableoutcomes'] = 'Enable outcomes';
$string['encoding'] = 'Encoding';
$string['errorgradevaluenonnumeric'] = 'Received non-numeric for low or high grade for';
+$string['errorcalculationnoequal'] = 'Formula must start with equal sign (=1+2)';
$string['errorcalculationunknown'] = 'Invalid formula';
$string['errornocategorizedid'] = 'Could not get an uncategorized id!';
$string['errornocourse'] = 'Could not get course information';
$this->sortorder = 1;
}
- // If not set, generate an idnumber from itemmodule and iteminstance
- if (empty($this->idnumber)) {
- if (!empty($this->itemmodule) && !empty($this->iteminstance)) {
- $this->idnumber = "$this->itemmodule.$this->iteminstance";
- } else { // No itemmodule or iteminstance, generate a random idnumber
- $this->idnumber = rand(0,9999999999); // TODO replace rand() with proper random generator
- }
- }
-
// add proper item numbers to manual items
if ($this->itemtype == 'manual') {
if (empty($this->itemnumber)) {
$this->itemnumber = 0;
}
- while (grade_item::fetch(array('courseid'=>$this->courseid, 'itemtype'=>'manual', 'itemnumber'=>$this->itemnumber))) {
- $this->itemnumber++;
- }
}
if (parent::insert($source)) {
}
}
+ /**
+ * Set idnumber of grade item, updates also course_modules table
+ * @param string $idnumber
+ * @return boolean success
+ */
+ function add_idnumber($idnumber) {
+ //TODO: add uniqueness checking
+ if (!empty($this->idnumber)) {
+ return false;
+ }
+
+ if ($this->itemtype == 'mod' and !$this->is_outcome_item()) {
+ if (!$cm = get_coursemodule_from_instance($this->itemmodule, $this->iteminstance, $this->courseid)) {
+ return false;
+ }
+ if (!empty($cm->idnumber)) {
+ return false;
+ }
+ $ncm = new object();
+ $ncm->id = $cm->id;
+ $ncm->idnumber = $idnumber();
+ if(update_record('course_modules', $ncm)) {
+ $this->idnumber = $idnumber;
+ return $this->update();
+ }
+ return false;
+
+ } else {
+ $this->idnumber = $idnumber;
+ return $this->update();
+ }
+ }
+
/**
* Returns the locked state of this grade_item (if the grade_item is locked OR no specific
* $userid is given) or the locked state of a specific grade within this item if a specific
$o->locked = $item->is_locked();
}
$o->grade = intval($o->grade); // 0 means no grade, int for scales
-
+
$result[$item->itemnumber] = $o;
}
}
}
$grade = $data[$item->itemnumber] < 1 ? null : $data[$item->itemnumber];
$item->update_final_grade($userid, $grade, $source);
- }
+ }
}
}