if (!has_any_capability(array('mod/workshop:peerassess', 'mod/workshop:assessallsubmissions'), $PAGE->context)) {
print_error('nopermissions', '', $workshop->view_url());
}
+ // todo do a check that the user is allowed to assess this submission
$PAGE->set_url($workshop->assess_url($assessment->id));
$PAGE->set_title($workshop->name);
$PAGE->set_heading($course->fullname);
// load the grading strategy logic
$strategy = $workshop->grading_strategy_instance();
-// load the assessment form definition from the database
-// this must be called before get_assessment_form() where we have to know
-// the number of repeating fieldsets
-
-//todo $formdata = $strategy->load_assessment($assessment);
-
// load the form to edit the grading strategy dimensions
-$mform = $strategy->get_assessment_form($PAGE->url, $mode);
+$mform = $strategy->get_assessment_form($PAGE->url, $mode, $assessment);
if ($mform->is_cancelled()) {
- redirect($returnurl);
+ redirect($workshop->view_url());
} elseif ($data = $mform->get_data()) {
if (isset($data->backtoeditform)) {
* @return void
*/
protected function definition_inner(&$mform) {
- $fields = (array)$this->_customdata['fields'];
- $nodims = $this->_customdata['nodims']; // number of assessment dimensions
+ $fields = $this->_customdata['fields'];
+ $current = $this->_customdata['current'];
+ $nodims = $this->_customdata['nodims']; // number of assessment dimensions
$mform->addElement('hidden', 'nodims', $nodims);
$mform->addElement('header', "dimensionhdr__idx_$i", $dimtitle);
// dimension id
- $mform->addElement('hidden', 'dimensionid__idx_'.$i, $fields['dimensionid__idx_'.$i]);
+ $mform->addElement('hidden', 'dimensionid__idx_'.$i, $fields->{'dimensionid__idx_'.$i});
+
+ // grade id
+ $mform->addElement('hidden', 'gradeid__idx_'.$i); // value set by set_data() later
// dimension description
- $desc = '<div id="id_dim_'.$fields['dimensionid__idx_'.$i].'_desc" class="fitem description accumulative">'."\n";
- $desc .= format_text($fields['description__idx_'.$i], $fields['description__idx_'.$i.'format']);
+ $desc = '<div id="id_dim_'.$fields->{'dimensionid__idx_'.$i}.'_desc" class="fitem description accumulative">'."\n";
+ $desc .= format_text($fields->{'description__idx_'.$i}, $fields->{'description__idx_'.$i.'format'});
$desc .= "\n</div>";
$mform->addElement('html', $desc);
// grade for this aspect
$label = get_string('dimensiongrade', 'workshop');
- $options = make_grades_menu($fields['grade__idx_' . $i]);
+ $options = make_grades_menu($fields->{'grade__idx_' . $i});
$mform->addElement('select', 'grade__idx_' . $i, $label, $options);
// comment
//$mform->addElement('editor', 'peercomment__idx_' . $i, $label, null, array('maxfiles' => 0));
$mform->addElement('textarea', 'peercomment__idx_' . $i, $label, array('cols' => 60, 'rows' => 5));
}
-
+ $this->set_data($current);
}
-
}
* @param moodle_url $actionurl URL of form handler, defaults to auto detect the current url
* @param string $mode Mode to open the form in: preview/assessment
*/
- public function get_assessment_form(moodle_url $actionurl=null, $mode='preview') {
+ public function get_assessment_form(moodle_url $actionurl=null, $mode='preview', stdClass $assessment=null) {
global $CFG; // needed because the included files use it
global $PAGE;
+ global $DB;
require_once(dirname(__FILE__) . '/assessment_form.php');
$fields = $this->load_fields();
'pluginfile.php', $PAGE->context->id, 'workshop_dimension_description', $fields->{'dimensionid__idx_'.$i});
}
+ if ('assessment' === $mode and !empty($assessment)) {
+ // load the previously saved assessment data
+ $grades = $DB->get_records('workshop_grades', array('assessmentid' => $assessment->id), '', 'dimensionid,*');
+ $current = new stdClass();
+ for ($i = 0; $i < $this->nodimensions; $i++) {
+ $dimid = $fields->{'dimensionid__idx_'.$i};
+ if (isset($grades[$dimid])) {
+ $current->{'gradeid__idx_'.$i} = $grades[$dimid]->id;
+ $current->{'grade__idx_'.$i} = $grades[$dimid]->grade;
+ $current->{'peercomment__idx_'.$i} = $grades[$dimid]->peercomment;
+ }
+ }
+ }
+
// set up the required custom data common for all strategies
$customdata['strategy'] = $this;
$customdata['mode'] = $mode;
// set up strategy-specific custom data
$customdata['nodims'] = $this->nodimensions;
$customdata['fields'] = $fields;
+ $customdata['current'] = isset($current) ? $current : null;
$attributes = array('class' => 'assessmentform accumulative');
return new workshop_accumulative_assessment_form($actionurl, $customdata, 'post', '', $attributes);
if (!isset($data->nodims)) {
throw coding_expection('You did not send me the number of assessment dimensions to process');
}
-
- foreach ($this->_cook_assessment_form_data($assessment, $data) as $cooked) {
- $cooked->id = $DB->get_field('workshop_grades', 'id', array('assessmentid' => $cooked->assessmentid,
- 'strategy' => 'accumulative',
- 'dimensionid' => $cooked->dimensionid));
- if (false === $cooked->id) {
- // not found - new grade
- $cooked->id = $DB->insert_record('workshop_grades', $cooked);
- } else {
- // update existing grade
- $DB->update_record('workshop_grades', $cooked);
- }
- }
- // todo recalculate grades
- }
-
- /**
- * Prepares data returned by {@link workshop_accumulative_assessment_form} so they can be saved into database
- *
- * Called internally from {@link save_assessment()} only. Could be private but
- * keeping protected for unit testing purposes.
- *
- * @param object $raw Raw data returned by mform
- * @return array Array of objects to be inserted/updated in DB
- */
- protected function _cook_assessment_form_data(stdClass $assessment, stdClass $raw) {
- $raw = (array)$raw;
- $cooked = array();
- for ($i = 0; $i < $raw['nodims']; $i++) {
+ for ($i = 0; $i < $data->nodims; $i++) {
$grade = new stdClass();
+ $grade->id = $data->{'gradeid__idx_' . $i};
$grade->assessmentid = $assessment->id;
- $grade->strategy = $raw['strategyname'];
- $grade->dimensionid = $raw['dimensionid__idx_' . $i];
- $grade->grade = $raw['grade__idx_' . $i];
- $grade->peercomment = $raw['peercomment__idx_' . $i];
+ $grade->dimensionid = $data->{'dimensionid__idx_' . $i};
+ $grade->grade = $data->{'grade__idx_' . $i};
+ $grade->peercomment = $data->{'peercomment__idx_' . $i};
$grade->peercommentformat = FORMAT_HTML;
- $cooked[$i] = $grade;
+ if (empty($grade->id)) {
+ // new grade
+ $grade->id = $DB->insert_record('workshop_grades', $grade);
+ } else {
+ // updated grade
+ $DB->update_record('workshop_grades', $grade);
+ }
}
- return $cooked;
+ // todo recalculate grades immediately or by cron ?
}
}