|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->dirroot.'/grade/grading/form/lib.php'); 00030 00034 class gradingform_rubric_controller extends gradingform_controller { 00035 // Modes of displaying the rubric (used in gradingform_rubric_renderer) 00037 const DISPLAY_EDIT_FULL = 1; 00039 const DISPLAY_EDIT_FROZEN = 2; 00041 const DISPLAY_PREVIEW = 3; 00043 const DISPLAY_PREVIEW_GRADED= 8; 00045 const DISPLAY_EVAL = 4; 00047 const DISPLAY_EVAL_FROZEN = 5; 00049 const DISPLAY_REVIEW = 6; 00051 const DISPLAY_VIEW = 7; 00052 00063 public function extend_settings_navigation(settings_navigation $settingsnav, navigation_node $node=null) { 00064 $node->add(get_string('definerubric', 'gradingform_rubric'), 00065 $this->get_editor_url(), settings_navigation::TYPE_CUSTOM, 00066 null, null, new pix_icon('icon', '', 'gradingform_rubric')); 00067 } 00068 00078 public function extend_navigation(global_navigation $navigation, navigation_node $node=null) { 00079 if (has_capability('moodle/grade:managegradingforms', $this->get_context())) { 00080 // no need for preview if user can manage forms, he will have link to manage.php in settings instead 00081 return; 00082 } 00083 if ($this->is_form_defined() && ($options = $this->get_options()) && !empty($options['alwaysshowdefinition'])) { 00084 $node->add(get_string('gradingof', 'gradingform_rubric', get_grading_manager($this->get_areaid())->get_area_title()), 00085 new moodle_url('/grade/grading/form/'.$this->get_method_name().'/preview.php', array('areaid' => $this->get_areaid())), 00086 settings_navigation::TYPE_CUSTOM); 00087 } 00088 } 00089 00097 public function update_definition(stdClass $newdefinition, $usermodified = null) { 00098 $this->update_or_check_rubric($newdefinition, $usermodified, true); 00099 if (isset($newdefinition->rubric['regrade']) && $newdefinition->rubric['regrade']) { 00100 $this->mark_for_regrade(); 00101 } 00102 } 00103 00119 public function update_or_check_rubric(stdClass $newdefinition, $usermodified = null, $doupdate = false) { 00120 global $DB; 00121 00122 // firstly update the common definition data in the {grading_definition} table 00123 if ($this->definition === false) { 00124 if (!$doupdate) { 00125 // if we create the new definition there is no such thing as re-grading anyway 00126 return 5; 00127 } 00128 // if definition does not exist yet, create a blank one 00129 // (we need id to save files embedded in description) 00130 parent::update_definition(new stdClass(), $usermodified); 00131 parent::load_definition(); 00132 } 00133 if (!isset($newdefinition->rubric['options'])) { 00134 $newdefinition->rubric['options'] = self::get_default_options(); 00135 } 00136 $newdefinition->options = json_encode($newdefinition->rubric['options']); 00137 $editoroptions = self::description_form_field_options($this->get_context()); 00138 $newdefinition = file_postupdate_standard_editor($newdefinition, 'description', $editoroptions, $this->get_context(), 00139 'grading', 'description', $this->definition->id); 00140 00141 // reload the definition from the database 00142 $currentdefinition = $this->get_definition(true); 00143 00144 // update rubric data 00145 $haschanges = array(); 00146 if (empty($newdefinition->rubric['criteria'])) { 00147 $newcriteria = array(); 00148 } else { 00149 $newcriteria = $newdefinition->rubric['criteria']; // new ones to be saved 00150 } 00151 $currentcriteria = $currentdefinition->rubric_criteria; 00152 $criteriafields = array('sortorder', 'description', 'descriptionformat'); 00153 $levelfields = array('score', 'definition', 'definitionformat'); 00154 foreach ($newcriteria as $id => $criterion) { 00155 // get list of submitted levels 00156 $levelsdata = array(); 00157 if (array_key_exists('levels', $criterion)) { 00158 $levelsdata = $criterion['levels']; 00159 } 00160 $criterionmaxscore = null; 00161 if (preg_match('/^NEWID\d+$/', $id)) { 00162 // insert criterion into DB 00163 $data = array('definitionid' => $this->definition->id, 'descriptionformat' => FORMAT_MOODLE); // TODO format is not supported yet 00164 foreach ($criteriafields as $key) { 00165 if (array_key_exists($key, $criterion)) { 00166 $data[$key] = $criterion[$key]; 00167 } 00168 } 00169 if ($doupdate) { 00170 $id = $DB->insert_record('gradingform_rubric_criteria', $data); 00171 } 00172 $haschanges[5] = true; 00173 } else { 00174 // update criterion in DB 00175 $data = array(); 00176 foreach ($criteriafields as $key) { 00177 if (array_key_exists($key, $criterion) && $criterion[$key] != $currentcriteria[$id][$key]) { 00178 $data[$key] = $criterion[$key]; 00179 } 00180 } 00181 if (!empty($data)) { 00182 // update only if something is changed 00183 $data['id'] = $id; 00184 if ($doupdate) { 00185 $DB->update_record('gradingform_rubric_criteria', $data); 00186 } 00187 $haschanges[1] = true; 00188 } 00189 // remove deleted levels from DB and calculate the maximum score for this criteria 00190 foreach ($currentcriteria[$id]['levels'] as $levelid => $currentlevel) { 00191 if ($criterionmaxscore === null || $criterionmaxscore < $currentlevel['score']) { 00192 $criterionmaxscore = $currentlevel['score']; 00193 } 00194 if (!array_key_exists($levelid, $levelsdata)) { 00195 if ($doupdate) { 00196 $DB->delete_records('gradingform_rubric_levels', array('id' => $levelid)); 00197 } 00198 $haschanges[4] = true; 00199 } 00200 } 00201 } 00202 foreach ($levelsdata as $levelid => $level) { 00203 if (isset($level['score'])) { 00204 $level['score'] = (float)$level['score']; 00205 if ($level['score']<0) { 00206 // TODO why we can't allow negative score for rubric? 00207 $level['score'] = 0; 00208 } 00209 } 00210 if (preg_match('/^NEWID\d+$/', $levelid)) { 00211 // insert level into DB 00212 $data = array('criterionid' => $id, 'definitionformat' => FORMAT_MOODLE); // TODO format is not supported yet 00213 foreach ($levelfields as $key) { 00214 if (array_key_exists($key, $level)) { 00215 $data[$key] = $level[$key]; 00216 } 00217 } 00218 if ($doupdate) { 00219 $levelid = $DB->insert_record('gradingform_rubric_levels', $data); 00220 } 00221 if ($criterionmaxscore !== null && $criterionmaxscore >= $level['score']) { 00222 // new level is added but the maximum score for this criteria did not change, re-grading may not be necessary 00223 $haschanges[2] = true; 00224 } else { 00225 $haschanges[3] = true; 00226 } 00227 } else { 00228 // update level in DB 00229 $data = array(); 00230 foreach ($levelfields as $key) { 00231 if (array_key_exists($key, $level) && $level[$key] != $currentcriteria[$id]['levels'][$levelid][$key]) { 00232 $data[$key] = $level[$key]; 00233 } 00234 } 00235 if (!empty($data)) { 00236 // update only if something is changed 00237 $data['id'] = $levelid; 00238 if ($doupdate) { 00239 $DB->update_record('gradingform_rubric_levels', $data); 00240 } 00241 if (isset($data['score'])) { 00242 $haschanges[3] = true; 00243 } 00244 $haschanges[1] = true; 00245 } 00246 } 00247 } 00248 } 00249 // remove deleted criteria from DB 00250 foreach (array_keys($currentcriteria) as $id) { 00251 if (!array_key_exists($id, $newcriteria)) { 00252 if ($doupdate) { 00253 $DB->delete_records('gradingform_rubric_criteria', array('id' => $id)); 00254 $DB->delete_records('gradingform_rubric_levels', array('criterionid' => $id)); 00255 } 00256 $haschanges[3] = true; 00257 } 00258 } 00259 foreach (array('status', 'description', 'descriptionformat', 'name', 'options') as $key) { 00260 if (isset($newdefinition->$key) && $newdefinition->$key != $this->definition->$key) { 00261 $haschanges[1] = true; 00262 } 00263 } 00264 if ($usermodified && $usermodified != $this->definition->usermodified) { 00265 $haschanges[1] = true; 00266 } 00267 if (!count($haschanges)) { 00268 return 0; 00269 } 00270 if ($doupdate) { 00271 parent::update_definition($newdefinition, $usermodified); 00272 $this->load_definition(); 00273 } 00274 // return the maximum level of changes 00275 $changelevels = array_keys($haschanges); 00276 sort($changelevels); 00277 return array_pop($changelevels); 00278 } 00279 00283 public function mark_for_regrade() { 00284 global $DB; 00285 if ($this->has_active_instances()) { 00286 $conditions = array('definitionid' => $this->definition->id, 00287 'status' => gradingform_instance::INSTANCE_STATUS_ACTIVE); 00288 $DB->set_field('grading_instances', 'status', gradingform_instance::INSTANCE_STATUS_NEEDUPDATE, $conditions); 00289 } 00290 } 00291 00297 protected function load_definition() { 00298 global $DB; 00299 $sql = "SELECT gd.*, 00300 rc.id AS rcid, rc.sortorder AS rcsortorder, rc.description AS rcdescription, rc.descriptionformat AS rcdescriptionformat, 00301 rl.id AS rlid, rl.score AS rlscore, rl.definition AS rldefinition, rl.definitionformat AS rldefinitionformat 00302 FROM {grading_definitions} gd 00303 LEFT JOIN {gradingform_rubric_criteria} rc ON (rc.definitionid = gd.id) 00304 LEFT JOIN {gradingform_rubric_levels} rl ON (rl.criterionid = rc.id) 00305 WHERE gd.areaid = :areaid AND gd.method = :method 00306 ORDER BY rc.sortorder,rl.score"; 00307 $params = array('areaid' => $this->areaid, 'method' => $this->get_method_name()); 00308 00309 $rs = $DB->get_recordset_sql($sql, $params); 00310 $this->definition = false; 00311 foreach ($rs as $record) { 00312 // pick the common definition data 00313 if ($this->definition === false) { 00314 $this->definition = new stdClass(); 00315 foreach (array('id', 'name', 'description', 'descriptionformat', 'status', 'copiedfromid', 00316 'timecreated', 'usercreated', 'timemodified', 'usermodified', 'timecopied', 'options') as $fieldname) { 00317 $this->definition->$fieldname = $record->$fieldname; 00318 } 00319 $this->definition->rubric_criteria = array(); 00320 } 00321 // pick the criterion data 00322 if (!empty($record->rcid) and empty($this->definition->rubric_criteria[$record->rcid])) { 00323 foreach (array('id', 'sortorder', 'description', 'descriptionformat') as $fieldname) { 00324 $this->definition->rubric_criteria[$record->rcid][$fieldname] = $record->{'rc'.$fieldname}; 00325 } 00326 $this->definition->rubric_criteria[$record->rcid]['levels'] = array(); 00327 } 00328 // pick the level data 00329 if (!empty($record->rlid)) { 00330 foreach (array('id', 'score', 'definition', 'definitionformat') as $fieldname) { 00331 $value = $record->{'rl'.$fieldname}; 00332 if ($fieldname == 'score') { 00333 $value = (float)$value; // To prevent display like 1.00000 00334 } 00335 $this->definition->rubric_criteria[$record->rcid]['levels'][$record->rlid][$fieldname] = $value; 00336 } 00337 } 00338 } 00339 $rs->close(); 00340 $options = $this->get_options(); 00341 if (!$options['sortlevelsasc']) { 00342 foreach (array_keys($this->definition->rubric_criteria) as $rcid) { 00343 $this->definition->rubric_criteria[$rcid]['levels'] = array_reverse($this->definition->rubric_criteria[$rcid]['levels'], true); 00344 } 00345 } 00346 } 00347 00353 public static function get_default_options() { 00354 $options = array( 00355 'sortlevelsasc' => 1, 00356 'alwaysshowdefinition' => 1, 00357 'showdescriptionteacher' => 1, 00358 'showdescriptionstudent' => 1, 00359 'showscoreteacher' => 1, 00360 'showscorestudent' => 1, 00361 'enableremarks' => 1, 00362 'showremarksstudent' => 1 00363 ); 00364 return $options; 00365 } 00366 00372 public function get_options() { 00373 $options = self::get_default_options(); 00374 if (!empty($this->definition->options)) { 00375 $thisoptions = json_decode($this->definition->options); 00376 foreach ($thisoptions as $option => $value) { 00377 $options[$option] = $value; 00378 } 00379 } 00380 return $options; 00381 } 00382 00389 public function get_definition_for_editing($addemptycriterion = false) { 00390 00391 $definition = $this->get_definition(); 00392 $properties = new stdClass(); 00393 $properties->areaid = $this->areaid; 00394 if ($definition) { 00395 foreach (array('id', 'name', 'description', 'descriptionformat', 'status') as $key) { 00396 $properties->$key = $definition->$key; 00397 } 00398 $options = self::description_form_field_options($this->get_context()); 00399 $properties = file_prepare_standard_editor($properties, 'description', $options, $this->get_context(), 00400 'grading', 'description', $definition->id); 00401 } 00402 $properties->rubric = array('criteria' => array(), 'options' => $this->get_options()); 00403 if (!empty($definition->rubric_criteria)) { 00404 $properties->rubric['criteria'] = $definition->rubric_criteria; 00405 } else if (!$definition && $addemptycriterion) { 00406 $properties->rubric['criteria'] = array('addcriterion' => 1); 00407 } 00408 00409 return $properties; 00410 } 00411 00419 public function get_definition_copy(gradingform_controller $target) { 00420 00421 $new = parent::get_definition_copy($target); 00422 $old = $this->get_definition_for_editing(); 00423 $new->description_editor = $old->description_editor; 00424 $new->rubric = array('criteria' => array(), 'options' => $old->rubric['options']); 00425 $newcritid = 1; 00426 $newlevid = 1; 00427 foreach ($old->rubric['criteria'] as $oldcritid => $oldcrit) { 00428 unset($oldcrit['id']); 00429 if (isset($oldcrit['levels'])) { 00430 foreach ($oldcrit['levels'] as $oldlevid => $oldlev) { 00431 unset($oldlev['id']); 00432 $oldcrit['levels']['NEWID'.$newlevid] = $oldlev; 00433 unset($oldcrit['levels'][$oldlevid]); 00434 $newlevid++; 00435 } 00436 } else { 00437 $oldcrit['levels'] = array(); 00438 } 00439 $new->rubric['criteria']['NEWID'.$newcritid] = $oldcrit; 00440 $newcritid++; 00441 } 00442 00443 return $new; 00444 } 00445 00452 public static function description_form_field_options($context) { 00453 global $CFG; 00454 return array( 00455 'maxfiles' => -1, 00456 'maxbytes' => get_max_upload_file_size($CFG->maxbytes), 00457 'context' => $context, 00458 ); 00459 } 00460 00466 public function get_formatted_description() { 00467 if (!isset($this->definition->description)) { 00468 return ''; 00469 } 00470 $context = $this->get_context(); 00471 00472 $options = self::description_form_field_options($this->get_context()); 00473 $description = file_rewrite_pluginfile_urls($this->definition->description, 'pluginfile.php', $context->id, 00474 'grading', 'description', $this->definition->id, $options); 00475 00476 $formatoptions = array( 00477 'noclean' => false, 00478 'trusted' => false, 00479 'filter' => true, 00480 'context' => $context 00481 ); 00482 return format_text($description, $this->definition->descriptionformat, $formatoptions); 00483 } 00484 00491 public function get_renderer(moodle_page $page) { 00492 return $page->get_renderer('gradingform_'. $this->get_method_name()); 00493 } 00494 00501 public function render_preview(moodle_page $page) { 00502 00503 if (!$this->is_form_defined()) { 00504 throw new coding_exception('It is the caller\'s responsibility to make sure that the form is actually defined'); 00505 } 00506 00507 $output = $this->get_renderer($page); 00508 $criteria = $this->definition->rubric_criteria; 00509 $options = $this->get_options(); 00510 $rubric = ''; 00511 if (has_capability('moodle/grade:managegradingforms', $page->context)) { 00512 $rubric .= $output->display_rubric_mapping_explained($this->get_min_max_score()); 00513 $rubric .= $output->display_rubric($criteria, $options, self::DISPLAY_PREVIEW, 'rubric'); 00514 } else { 00515 $rubric .= $output->display_rubric($criteria, $options, self::DISPLAY_PREVIEW_GRADED, 'rubric'); 00516 } 00517 00518 return $rubric; 00519 } 00520 00524 protected function delete_plugin_definition() { 00525 global $DB; 00526 00527 // get the list of instances 00528 $instances = array_keys($DB->get_records('grading_instances', array('definitionid' => $this->definition->id), '', 'id')); 00529 // delete all fillings 00530 $DB->delete_records_list('gradingform_rubric_fillings', 'instanceid', $instances); 00531 // delete instances 00532 $DB->delete_records_list('grading_instances', 'id', $instances); 00533 // get the list of criteria records 00534 $criteria = array_keys($DB->get_records('gradingform_rubric_criteria', array('definitionid' => $this->definition->id), '', 'id')); 00535 // delete levels 00536 $DB->delete_records_list('gradingform_rubric_levels', 'criterionid', $criteria); 00537 // delete critera 00538 $DB->delete_records_list('gradingform_rubric_criteria', 'id', $criteria); 00539 } 00540 00552 public function get_or_create_instance($instanceid, $raterid, $itemid) { 00553 global $DB; 00554 if ($instanceid && 00555 $instance = $DB->get_record('grading_instances', array('id' => $instanceid, 'raterid' => $raterid, 'itemid' => $itemid), '*', IGNORE_MISSING)) { 00556 return $this->get_instance($instance); 00557 } 00558 if ($itemid && $raterid) { 00559 if ($rs = $DB->get_records('grading_instances', array('raterid' => $raterid, 'itemid' => $itemid), 'timemodified DESC', '*', 0, 1)) { 00560 $record = reset($rs); 00561 $currentinstance = $this->get_current_instance($raterid, $itemid); 00562 if ($record->status == gradingform_rubric_instance::INSTANCE_STATUS_INCOMPLETE && 00563 (!$currentinstance || $record->timemodified > $currentinstance->get_data('timemodified'))) { 00564 $record->isrestored = true; 00565 return $this->get_instance($record); 00566 } 00567 } 00568 } 00569 return $this->create_instance($raterid, $itemid); 00570 } 00571 00582 public function render_grade($page, $itemid, $gradinginfo, $defaultcontent, $cangrade) { 00583 return $this->get_renderer($page)->display_instances($this->get_active_instances($itemid), $defaultcontent, $cangrade); 00584 } 00585 00587 00594 public static function sql_search_from_tables($gdid) { 00595 return " LEFT JOIN {gradingform_rubric_criteria} rc ON (rc.definitionid = $gdid) 00596 LEFT JOIN {gradingform_rubric_levels} rl ON (rl.criterionid = rc.id)"; 00597 } 00598 00609 public static function sql_search_where($token) { 00610 global $DB; 00611 00612 $subsql = array(); 00613 $params = array(); 00614 00615 // search in rubric criteria description 00616 $subsql[] = $DB->sql_like('rc.description', '?', false, false); 00617 $params[] = '%'.$DB->sql_like_escape($token).'%'; 00618 00619 // search in rubric levels definition 00620 $subsql[] = $DB->sql_like('rl.definition', '?', false, false); 00621 $params[] = '%'.$DB->sql_like_escape($token).'%'; 00622 00623 return array($subsql, $params); 00624 } 00625 00631 public function get_min_max_score() { 00632 if (!$this->is_form_available()) { 00633 return null; 00634 } 00635 $returnvalue = array('minscore' => 0, 'maxscore' => 0); 00636 foreach ($this->get_definition()->rubric_criteria as $id => $criterion) { 00637 $scores = array(); 00638 foreach ($criterion['levels'] as $level) { 00639 $scores[] = $level['score']; 00640 } 00641 sort($scores); 00642 $returnvalue['minscore'] += $scores[0]; 00643 $returnvalue['maxscore'] += $scores[sizeof($scores)-1]; 00644 } 00645 return $returnvalue; 00646 } 00647 } 00648 00655 class gradingform_rubric_instance extends gradingform_instance { 00656 00657 protected $rubric; 00658 00662 public function cancel() { 00663 global $DB; 00664 parent::cancel(); 00665 $DB->delete_records('gradingform_rubric_fillings', array('instanceid' => $this->get_id())); 00666 } 00667 00676 public function copy($raterid, $itemid) { 00677 global $DB; 00678 $instanceid = parent::copy($raterid, $itemid); 00679 $currentgrade = $this->get_rubric_filling(); 00680 foreach ($currentgrade['criteria'] as $criterionid => $record) { 00681 $params = array('instanceid' => $instanceid, 'criterionid' => $criterionid, 00682 'levelid' => $record['levelid'], 'remark' => $record['remark'], 'remarkformat' => $record['remarkformat']); 00683 $DB->insert_record('gradingform_rubric_fillings', $params); 00684 } 00685 return $instanceid; 00686 } 00687 00694 public function validate_grading_element($elementvalue) { 00695 $criteria = $this->get_controller()->get_definition()->rubric_criteria; 00696 if (!isset($elementvalue['criteria']) || !is_array($elementvalue['criteria']) || sizeof($elementvalue['criteria']) < sizeof($criteria)) { 00697 return false; 00698 } 00699 foreach ($criteria as $id => $criterion) { 00700 if (!isset($elementvalue['criteria'][$id]['levelid']) 00701 || !array_key_exists($elementvalue['criteria'][$id]['levelid'], $criterion['levels'])) { 00702 return false; 00703 } 00704 } 00705 return true; 00706 } 00707 00714 public function get_rubric_filling($force = false) { 00715 global $DB; 00716 if ($this->rubric === null || $force) { 00717 $records = $DB->get_records('gradingform_rubric_fillings', array('instanceid' => $this->get_id())); 00718 $this->rubric = array('criteria' => array()); 00719 foreach ($records as $record) { 00720 $this->rubric['criteria'][$record->criterionid] = (array)$record; 00721 } 00722 } 00723 return $this->rubric; 00724 } 00725 00733 public function update($data) { 00734 global $DB; 00735 $currentgrade = $this->get_rubric_filling(); 00736 parent::update($data); 00737 foreach ($data['criteria'] as $criterionid => $record) { 00738 if (!array_key_exists($criterionid, $currentgrade['criteria'])) { 00739 $newrecord = array('instanceid' => $this->get_id(), 'criterionid' => $criterionid, 00740 'levelid' => $record['levelid'], 'remarkformat' => FORMAT_MOODLE); 00741 if (isset($record['remark'])) { 00742 $newrecord['remark'] = $record['remark']; 00743 } 00744 $DB->insert_record('gradingform_rubric_fillings', $newrecord); 00745 } else { 00746 $newrecord = array('id' => $currentgrade['criteria'][$criterionid]['id']); 00747 foreach (array('levelid', 'remark'/*, 'remarkformat' TODO */) as $key) { 00748 if (isset($record[$key]) && $currentgrade['criteria'][$criterionid][$key] != $record[$key]) { 00749 $newrecord[$key] = $record[$key]; 00750 } 00751 } 00752 if (count($newrecord) > 1) { 00753 $DB->update_record('gradingform_rubric_fillings', $newrecord); 00754 } 00755 } 00756 } 00757 foreach ($currentgrade['criteria'] as $criterionid => $record) { 00758 if (!array_key_exists($criterionid, $data['criteria'])) { 00759 $DB->delete_records('gradingform_rubric_fillings', array('id' => $record['id'])); 00760 } 00761 } 00762 $this->get_rubric_filling(true); 00763 } 00764 00770 public function get_grade() { 00771 global $DB, $USER; 00772 $grade = $this->get_rubric_filling(); 00773 00774 if (!($scores = $this->get_controller()->get_min_max_score()) || $scores['maxscore'] <= $scores['minscore']) { 00775 return -1; 00776 } 00777 00778 $graderange = array_keys($this->get_controller()->get_grade_range()); 00779 if (empty($graderange)) { 00780 return -1; 00781 } 00782 sort($graderange); 00783 $mingrade = $graderange[0]; 00784 $maxgrade = $graderange[sizeof($graderange) - 1]; 00785 00786 $curscore = 0; 00787 foreach ($grade['criteria'] as $id => $record) { 00788 $curscore += $this->get_controller()->get_definition()->rubric_criteria[$id]['levels'][$record['levelid']]['score']; 00789 } 00790 return round(($curscore-$scores['minscore'])/($scores['maxscore']-$scores['minscore'])*($maxgrade-$mingrade), 0) + $mingrade; 00791 } 00792 00800 public function render_grading_element($page, $gradingformelement) { 00801 global $USER; 00802 if (!$gradingformelement->_flagFrozen) { 00803 $module = array('name'=>'gradingform_rubric', 'fullpath'=>'/grade/grading/form/rubric/js/rubric.js'); 00804 $page->requires->js_init_call('M.gradingform_rubric.init', array(array('name' => $gradingformelement->getName())), true, $module); 00805 $mode = gradingform_rubric_controller::DISPLAY_EVAL; 00806 } else { 00807 if ($gradingformelement->_persistantFreeze) { 00808 $mode = gradingform_rubric_controller::DISPLAY_EVAL_FROZEN; 00809 } else { 00810 $mode = gradingform_rubric_controller::DISPLAY_REVIEW; 00811 } 00812 } 00813 $criteria = $this->get_controller()->get_definition()->rubric_criteria; 00814 $options = $this->get_controller()->get_options(); 00815 $value = $gradingformelement->getValue(); 00816 $html = ''; 00817 if ($value === null) { 00818 $value = $this->get_rubric_filling(); 00819 } else if (!$this->validate_grading_element($value)) { 00820 $html .= html_writer::tag('div', get_string('rubricnotcompleted', 'gradingform_rubric'), array('class' => 'gradingform_rubric-error')); 00821 } 00822 $currentinstance = $this->get_current_instance(); 00823 if ($currentinstance && $currentinstance->get_status() == gradingform_instance::INSTANCE_STATUS_NEEDUPDATE) { 00824 $html .= html_writer::tag('div', get_string('needregrademessage', 'gradingform_rubric'), array('class' => 'gradingform_rubric-regrade')); 00825 } 00826 $haschanges = false; 00827 if ($currentinstance) { 00828 $curfilling = $currentinstance->get_rubric_filling(); 00829 foreach ($curfilling['criteria'] as $criterionid => $curvalues) { 00830 $value['criteria'][$criterionid]['savedlevelid'] = $curvalues['levelid']; 00831 $newremark = null; 00832 $newlevelid = null; 00833 if (isset($value['criteria'][$criterionid]['remark'])) $newremark = $value['criteria'][$criterionid]['remark']; 00834 if (isset($value['criteria'][$criterionid]['levelid'])) $newlevelid = $value['criteria'][$criterionid]['levelid']; 00835 if ($newlevelid != $curvalues['levelid'] || $newremark != $curvalues['remark']) { 00836 $haschanges = true; 00837 } 00838 } 00839 } 00840 if ($this->get_data('isrestored') && $haschanges) { 00841 $html .= html_writer::tag('div', get_string('restoredfromdraft', 'gradingform_rubric'), array('class' => 'gradingform_rubric-restored')); 00842 } 00843 if (!empty($options['showdescriptionteacher'])) { 00844 $html .= html_writer::tag('div', $this->get_controller()->get_formatted_description(), array('class' => 'gradingform_rubric-description')); 00845 } 00846 $html .= $this->get_controller()->get_renderer($page)->display_rubric($criteria, $options, $mode, $gradingformelement->getName(), $value); 00847 return $html; 00848 } 00849 }