From 0715308cd14a81aa0fa18617239839149b4fa22d Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Mon, 4 Jan 2010 17:51:10 +0000 Subject: [PATCH] Accumulative assessment saves (and loads already saved) evaluation --- mod/workshop/assessment.php | 11 +--- mod/workshop/editform.php | 3 + .../grading/accumulative/assessment_form.php | 19 +++--- .../grading/accumulative/strategy.php | 66 +++++++++---------- 4 files changed, 47 insertions(+), 52 deletions(-) diff --git a/mod/workshop/assessment.php b/mod/workshop/assessment.php index 77ac63a2d7..e400882f8b 100644 --- a/mod/workshop/assessment.php +++ b/mod/workshop/assessment.php @@ -63,6 +63,7 @@ if ('preview' == $mode) { 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); @@ -71,17 +72,11 @@ if ('preview' == $mode) { // 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)) { diff --git a/mod/workshop/editform.php b/mod/workshop/editform.php index 989020f8a4..8c51e06f03 100644 --- a/mod/workshop/editform.php +++ b/mod/workshop/editform.php @@ -37,6 +37,9 @@ require_capability('mod/workshop:editdimensions', $PAGE->context); $workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST); $workshop = new workshop($workshop, $cm, $course); +// todo: check if there already is some assessment done and do not allowed the change of the form +// once somebody already used it to assess + $PAGE->set_url($workshop->editform_url()); $PAGE->set_title($workshop->name); $PAGE->set_heading($course->fullname); diff --git a/mod/workshop/grading/accumulative/assessment_form.php b/mod/workshop/grading/accumulative/assessment_form.php index a1d35025d8..f2163d25a5 100644 --- a/mod/workshop/grading/accumulative/assessment_form.php +++ b/mod/workshop/grading/accumulative/assessment_form.php @@ -42,8 +42,9 @@ class workshop_accumulative_assessment_form extends workshop_assessment_form { * @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); @@ -53,17 +54,20 @@ class workshop_accumulative_assessment_form extends workshop_assessment_form { $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 = '
'."\n"; - $desc .= format_text($fields['description__idx_'.$i], $fields['description__idx_'.$i.'format']); + $desc = '
'."\n"; + $desc .= format_text($fields->{'description__idx_'.$i}, $fields->{'description__idx_'.$i.'format'}); $desc .= "\n
"; $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 @@ -71,7 +75,6 @@ class workshop_accumulative_assessment_form extends workshop_assessment_form { //$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); } - } diff --git a/mod/workshop/grading/accumulative/strategy.php b/mod/workshop/grading/accumulative/strategy.php index bbb4b32653..310f239aef 100644 --- a/mod/workshop/grading/accumulative/strategy.php +++ b/mod/workshop/grading/accumulative/strategy.php @@ -250,9 +250,10 @@ class workshop_accumulative_strategy extends workshop_base_strategy implements w * @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(); @@ -266,6 +267,20 @@ class workshop_accumulative_strategy extends workshop_base_strategy implements w '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; @@ -273,6 +288,7 @@ class workshop_accumulative_strategy extends workshop_base_strategy implements w // 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); @@ -297,45 +313,23 @@ class workshop_accumulative_strategy extends workshop_base_strategy implements w 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 ? } } -- 2.39.5