Merge branch 'rubric' of github.com:mudrd8mz/moodle into rubric

This commit is contained in:
David Mudrak 2011-10-13 22:43:28 +02:00
commit 7ece4a0798
9 changed files with 438 additions and 7 deletions

View File

@ -256,6 +256,7 @@ abstract class gradingform_controller {
$instance->raterid = $raterid;
$instance->itemid = $itemid;
$instance->timemodified = time();
$instance->feedbackformat = FORMAT_MOODLE;
$instance->id = $DB->insert_record('grading_instances', $instance);
return $instance;
@ -264,6 +265,30 @@ abstract class gradingform_controller {
}
}
/**
* Saves non-js data and returns the gradebook grade
*/
abstract public function save_and_get_grade($raterid, $itemid, $formdata);
/**
* Returns html for form element
*/
abstract public function to_html($gradingformelement);
/**
*
*/
public function default_validation_error_message() {
return '';
}
/**
*
*/
public function validate_grading_element($elementvalue, $itemid) {
return true;
}
////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,25 @@
M.gradingform_rubric = {};
/**
* This function is called for each rubric on page.
*/
M.gradingform_rubric.init = function(Y, options) {
Y.on('click', M.gradingform_rubric.levelclick, '#rubric-'+options.name+' .level', null, Y, options.name);
Y.all('#rubric-'+options.name+' .radio').setStyle('display', 'none')
};
M.gradingform_rubric.levelclick = function(e, Y, name) {
var el = e.target
while (el && !el.hasClass('level')) el = el.get('parentNode')
if (!el) return
e.preventDefault();
el.siblings().removeClass('checked');
chb = el.one('input[type=radio]')
if (!chb.get('checked')) {
chb.set('checked', true)
el.addClass('checked')
} else {
el.removeClass('checked');
el.get('parentNode').all('input[type=radio]').set('checked', false)
}
}

View File

@ -226,6 +226,155 @@ class gradingform_rubric_controller extends gradingform_controller {
return $properties;
}
public function get_grading($raterid, $itemid) {
global $DB;
$sql = "SELECT f.id, f.criterionid, f.levelid, f.remark, f.remarkformat
FROM {grading_instances} i, {gradingform_rubric_fillings} f
WHERE i.formid = :formid ".
"AND i.raterid = :raterid ".
"AND i.itemid = :itemid
AND i.id = f.forminstanceid";
$params = array('formid' => $this->definition->id, 'itemid' => $itemid, 'raterid' => $raterid);
$rs = $DB->get_recordset_sql($sql, $params);
$grading = array();
foreach ($rs as $record) {
if ($record->levelid) {
$grading[$record->criterionid] = $record->levelid;
}
// TODO: remarks
}
return $grading;
}
/**
* Converts the rubric data to the gradebook score 0-100
*/
protected function calculate_grade($grade, $itemid) {
if (!$this->validate_grading_element($grade, $itemid)) {
return -1;
}
$minscore = 0;
$maxscore = 0;
foreach ($this->definition->rubric_criteria as $id => $criterion) {
$keys = array_keys($criterion['levels']);
// TODO array_reverse($keys) if levels are sorted DESC
$minscore += $criterion['levels'][$keys[0]]['score'];
$maxscore += $criterion['levels'][$keys[sizeof($keys)-1]]['score'];
}
if ($maxscore == 0) {
return -1;
}
$curscore = 0;
foreach ($grade as $id => $levelid) {
$curscore += $this->definition->rubric_criteria[$id]['levels'][$levelid]['score'];
}
return $curscore/$maxscore*100; // TODO mapping
}
/**
* Saves non-js data and returns the gradebook grade
*/
public function save_and_get_grade($raterid, $itemid, $formdata) {
global $DB, $USER;
$instance = $this->prepare_instance($raterid, $itemid);
$currentgrade = $this->get_grading($raterid, $itemid);
if (!is_array($formdata)) {
return $this->calculate_grade($currentgrade, $itemid);
}
foreach ($formdata as $criterionid => $levelid) {
$params = array('forminstanceid' => $instance->id, 'criterionid' => $criterionid);
if (!array_key_exists($criterionid, $currentgrade)) {
$DB->insert_record('gradingform_rubric_fillings', $params + array('levelid' => $levelid));
} else if ($currentgrade[$criterionid] != $levelid) {
$DB->set_field('gradingform_rubric_fillings', 'levelid', $levelid, $params);
}
}
foreach ($currentgrade as $criterionid => $levelid) {
if (!array_key_exists($criterionid, $formdata)) {
$params = array('forminstanceid' => $instance->id, 'criterionid' => $criterionid);
$DB->delete_records('gradingform_rubric_fillings', $params);
}
}
// TODO: remarks
return $this->calculate_grade($formdata, $itemid);
}
/**
* Returns html for form element
*/
public function to_html($gradingformelement) {
global $PAGE, $USER;
//TODO move to renderer
//$gradingrenderer = $this->prepare_renderer($PAGE);
$html = '';
$elementname = $gradingformelement->getName();
$elementvalue = $gradingformelement->getValue();
$submissionid = $gradingformelement->get_grading_attribute('submissionid');
$raterid = $USER->id; // TODO - this is very strange!
$html .= "assessing submission $submissionid<br />";
//$html .= html_writer::empty_tag('input', array('type' => 'text', 'name' => $elementname.'[grade]', 'size' => '20', 'value' => $elementvalue['grade']));
if (!$gradingformelement->_flagFrozen) {
$module = array('name'=>'gradingform_rubric', 'fullpath'=>'/grade/grading/form/rubric/js/rubric.js');
$PAGE->requires->js_init_call('M.gradingform_rubric.init', array(array('name' => $gradingformelement->getName(), 'criteriontemplate' =>'', 'leveltemplate' => '')), true, $module);
}
$criteria = $this->definition->rubric_criteria;
$html .= html_writer::start_tag('div', array('id' => 'rubric-'.$gradingformelement->getName(), 'class' => 'form_rubric evaluate'));
$criteria_cnt = 0;
$value = $gradingformelement->getValue();
if ($value === null) {
$value = $this->get_grading($raterid, $submissionid); // TODO maybe implement in form->set_data() ?
}
foreach ($criteria as $criterionid => $criterion) {
$html .= html_writer::start_tag('div', array('class' => 'criterion'.$this->get_css_class_suffix($criteria_cnt++, count($criteria)-1)));
$html .= html_writer::tag('div', $criterion['description'], array('class' => 'description')); // TODO descriptionformat
$html .= html_writer::start_tag('div', array('class' => 'levels'));
$level_cnt = 0;
foreach ($criterion['levels'] as $levelid => $level) {
$checked = (is_array($value) && array_key_exists($criterionid, $value) && ((int)$value[$criterionid] === $levelid));
$classsuffix = $this->get_css_class_suffix($level_cnt++, count($criterion['levels'])-1);
if ($checked) {
$classsuffix .= ' checked';
}
$html .= html_writer::start_tag('div', array('id' => $gradingformelement->getName().'-'.$criterionid.'-levels-'.$levelid, 'class' => 'level'.$classsuffix));
$input = html_writer::empty_tag('input', array('type' => 'radio', 'name' => $gradingformelement->getName().'['.$criterionid.']', 'value' => $levelid) +
($checked ? array('checked' => 'checked') : array())); // TODO rewrite
$html .= html_writer::tag('div', $input, array('class' => 'radio'));
$html .= html_writer::tag('div', $level['definition'], array('class' => 'definition')); // TODO definitionformat
$html .= html_writer::tag('div', (float)$level['score'].' pts', array('class' => 'score')); //TODO span, get_string
$html .= html_writer::end_tag('div'); // .level
}
$html .= html_writer::end_tag('div'); // .levels
$html .= html_writer::end_tag('div'); // .criterion
}
$html .= html_writer::end_tag('div'); // .rubric
return $html;
}
private function get_css_class_suffix($cnt, $maxcnt) {
$class = '';
if ($cnt == 0) {
$class .= ' first';
}
if ($cnt == $maxcnt) {
$class .= ' last';
}
if ($cnt%2) {
$class .= ' odd';
} else {
$class .= ' even';
}
return $class;
}
// TODO the following functions may be moved to parent:
/**
@ -273,4 +422,35 @@ class gradingform_rubric_controller extends gradingform_controller {
// TODO change filearea for embedded files in grading_definition.description
return $data;
}
public function is_form_available($foruserid = null) {
return true;
// TODO this is temporary for testing!
}
/**
* Returns the error message displayed in case of validation failed
*
* @see validate_grading_element
*/
public function default_validation_error_message() {
return 'The rubric is incomplete'; //TODO string
}
/**
* Validates that rubric is fully completed and contains valid grade on each criterion
*/
public function validate_grading_element($elementvalue, $itemid) {
// TODO: if there is nothing selected in rubric, we don't enter this function at all :(
$criteria = $this->definition->rubric_criteria;
if (!is_array($elementvalue) || sizeof($elementvalue) < sizeof($criteria)) {
return false;
}
foreach ($criteria as $id => $criterion) {
if (!array_key_exists($id, $elementvalue) || !array_key_exists($elementvalue[$id], $criterion['levels'])) {
return false;
}
}
return true;
}
}

View File

@ -374,6 +374,19 @@ class grading_manager {
return new $classname($this->context, $this->component, $this->area, $this->areacache->id);
}
/**
* Returns the controller for the active method if it is available
*/
public function get_active_controller() {
if ($gradingmethod = $this->get_active_method()) {
$controller = $this->get_controller($gradingmethod);
if ($controller->is_form_available()) {
return $controller;
}
}
return null;
}
////////////////////////////////////////////////////////////////////////////
/**

View File

@ -26,6 +26,7 @@
defined('MOODLE_INTERNAL') || die();
$string['exc_gradingformelement'] = 'Unable to instantiate grading form element';
$string['formnotavailable'] = 'Advanced grading method was selected to use but the grading form is not available yet. You may need to define it first via a link in the Settings block.';
$string['gradinginarea'] = 'Grading ({$a})';
$string['gradingmethod'] = 'Grading method';

View File

@ -6836,6 +6836,7 @@ FROM
}
upgrade_main_savepoint(true, 2011100700.01);
}
if ($oldversion < 2011100700.02) {
// Define field idnumber to be added to course_categories

118
lib/form/grading.php Normal file
View File

@ -0,0 +1,118 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Element-container for advanced grading custom input
*
* @copyright 2011 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
global $CFG;
require_once("HTML/QuickForm/element.php");
require_once($CFG->dirroot.'/grade/grading/form/lib.php');
if (class_exists('HTML_QuickForm')) {
HTML_QuickForm::registerRule('gradingvalidated', 'callback', '_validate', 'MoodleQuickForm_grading');
}
/**
* HTML class for a grading element
*
* @author Marina Glancy
* @access public
*/
class MoodleQuickForm_grading extends HTML_QuickForm_input{
/**
* html for help button, if empty then no help
*
* @var string
*/
var $_helpbutton='';
private $gradingattributes;
function MoodleQuickForm_grading($elementName=null, $elementLabel=null, $attributes=null) {
parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
$this->gradingattributes = $attributes;
}
function toHtml(){
return $this->get_controller()->to_html($this);
}
function get_grading_attribute($name) {
return $this->gradingattributes[$name];
}
function get_controller() {
return $this->get_grading_attribute('controller');
}
/**
* set html for help button
*
* @access public
* @param array $help array of arguments to make a help button
* @param string $function function name to call to get html
*/
function setHelpButton($helpbuttonargs, $function='helpbutton'){
debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
}
/**
* get html for help button
*
* @access public
* @return string html for help button
*/
function getHelpButton(){
return $this->_helpbutton;
}
/**
* @return string
*/
function getElementTemplateType(){
return 'default';
}
/**
* Adds necessary rules to the element
*/
function onQuickFormEvent($event, $arg, &$caller) {
if ($event == 'createElement') {
$attributes = $arg[2];
if (!is_array($attributes) || !array_key_exists('controller', $attributes) || !($attributes['controller'] instanceof gradingform_controller)) {
throw new moodle_exception('exc_gradingformelement', 'grading');
}
}
$name = $this->getName();
if ($name && $caller->elementExists($name)) {
$caller->addRule($name, $this->get_controller()->default_validation_error_message(), 'gradingvalidated', $this->gradingattributes);
}
return parent::onQuickFormEvent($event, $arg, $caller);
}
/**
* Function registered as rule for this element and is called when this element is being validated
*/
static function _validate($elementValue, $attributes = null) {
return $attributes['controller']->validate_grading_element($elementValue, $attributes['submissionid']);
}
}

View File

@ -2487,6 +2487,7 @@ MoodleQuickForm::registerElementType('file', "$CFG->libdir/form/file.php", 'Mood
MoodleQuickForm::registerElementType('filemanager', "$CFG->libdir/form/filemanager.php", 'MoodleQuickForm_filemanager');
MoodleQuickForm::registerElementType('filepicker', "$CFG->libdir/form/filepicker.php", 'MoodleQuickForm_filepicker');
MoodleQuickForm::registerElementType('format', "$CFG->libdir/form/format.php", 'MoodleQuickForm_format');
MoodleQuickForm::registerElementType('grading', "$CFG->libdir/form/grading.php", 'MoodleQuickForm_grading');
MoodleQuickForm::registerElementType('group', "$CFG->libdir/form/group.php", 'MoodleQuickForm_group');
MoodleQuickForm::registerElementType('header', "$CFG->libdir/form/header.php", 'MoodleQuickForm_header');
MoodleQuickForm::registerElementType('hidden', "$CFG->libdir/form/hidden.php", 'MoodleQuickForm_hidden');

View File

@ -630,7 +630,10 @@ class assignment_base {
switch ($mode) {
case 'grade': // We are in a main window grading
if ($submission = $this->process_feedback()) {
if (!$this->validate_and_preprocess_feedback()) {
// validation failed
$this->display_submission();
} else if ($submission = $this->process_feedback()) {
$this->display_submissions(get_string('changessaved'));
} else {
$this->display_submissions();
@ -744,7 +747,11 @@ class assignment_base {
case 'saveandnext':
///We are in pop up. save the current one and go to the next one.
//first we save the current changes
if ($submission = $this->process_feedback()) {
if (!$this->validate_and_preprocess_feedback()) {
// validation failed
$this->display_submission();
break;
} else if ($submission = $this->process_feedback()) {
//print_heading(get_string('changessaved'));
//$extra_javascript = $this->update_main_listing($submission);
}
@ -1039,7 +1046,7 @@ class assignment_base {
} elseif ($assignment->assignmenttype == 'uploadsingle') {
$mformdata->fileui_options = array('subdirs'=>0, 'maxbytes'=>$CFG->userquota, 'maxfiles'=>1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
}
$gradingman = get_grading_manager($this->context, 'mod_assignment', 'submission');
/*$gradingman = get_grading_manager($this->context, 'mod_assignment', 'submission');
if ($gradingmethod = $gradingman->get_active_method()) {
$controller = $gradingman->get_controller($gradingmethod);
if ($controller->is_form_available()) {
@ -1067,6 +1074,13 @@ class assignment_base {
} else {
notice(get_string('formnotavailable', 'core_grading'), new moodle_url('/course/view.php', array('id' => $assignment->course)));
}
}*/
if ($controller = get_grading_manager($this->context, 'mod_assignment', 'submission')->get_active_controller()) {
if (!isset($submission->id)) {
// TODO this is a patch if submission id does not exist yet
$mformdata->submission = $this->get_submission($user->id, true);
}
$mformdata->advancedgradingcontroller = $controller;
}
$submitform = new mod_assignment_grading_form( null, $mformdata );
@ -1074,7 +1088,9 @@ class assignment_base {
if (!$display) {
$ret_data = new stdClass();
$ret_data->mform = $submitform;
$ret_data->fileui_options = $mformdata->fileui_options;
if (isset($mformdata->fileui_options)) {
$ret_data->fileui_options = $mformdata->fileui_options;
}
return $ret_data;
}
@ -1574,6 +1590,34 @@ class assignment_base {
echo $OUTPUT->footer();
}
/**
* Validates the submitted form and returns false if validation did not pass.
* If validation passes, preprocess advanced grading (if applicable) and returns true.
*/
function validate_and_preprocess_feedback() {
global $USER;
if (!$feedback = data_submitted()) {
return true; // No incoming data, nothing to validate
}
$userid = required_param('userid', PARAM_INT);
$offset = required_param('offset', PARAM_INT);
$submissiondata = $this->display_submission($offset, $userid, false);
$mform = $submissiondata->mform;
if ($mform->is_submitted()) {
if (!$mform->is_validated()) {
return false;
}
// preprocess advanced grading here
if ($controller = $mform->use_advanced_grading()) {
$data = $mform->get_data();
// TODO find better way to find submission id
$submission = $this->get_submission($userid);
$_POST['xgrade'] = $controller->save_and_get_grade($USER->id /* TODO */, $submission->id, $data->advancedgrading);
}
}
return true;
}
/**
* Process teacher feedback submission
*
@ -2256,6 +2300,10 @@ class mod_assignment_grading_form extends moodleform {
global $OUTPUT;
$mform =& $this->_form;
if (isset($this->_customdata->advancedgradingcontroller)) {
$this->use_advanced_grading($this->_customdata->advancedgradingcontroller);
}
$formattr = $mform->getAttributes();
$formattr['id'] = 'submitform';
$mform->setAttributes($formattr);
@ -2301,6 +2349,19 @@ class mod_assignment_grading_form extends moodleform {
}
private $_advancegradingcontroller;
/**
* Gets or sets the controller for advanced grading
*
* @param <type> $controller
*/
public function use_advanced_grading($controller = false) {
if ($controller !== false) {
$this->_advancegradingcontroller = $controller;
}
return $this->_advancegradingcontroller;
}
function add_grades_section() {
global $CFG;
$mform =& $this->_form;
@ -2311,9 +2372,10 @@ class mod_assignment_grading_form extends moodleform {
$mform->addElement('header', 'Grades', get_string('grades', 'grades'));
if (!empty($this->_customdata->advancedgradingenabled)) {
$mform->addElement('static', 'advancedgradingwidget', get_string('grade').':', $this->_customdata->advancedgradingwidget);
if ($controller = $this->use_advanced_grading()) {
// TODO what if submission id does not exist yet!
$mform->addElement('grading', 'advancedgrading', get_string('grade').':',
array('controller' => $controller, 'submissionid' => $this->_customdata->submission->id));
} else {
// use simple direct grading
$grademenu = make_grades_menu($this->_customdata->assignment->grade);
@ -2472,6 +2534,11 @@ class mod_assignment_grading_form extends moodleform {
}
$data = file_postupdate_standard_editor($data, 'submissioncomment', $editoroptions, $this->_customdata->context, $editoroptions['component'], $editoroptions['filearea'], $itemid);
}
if ($this->use_advanced_grading() && !isset($data->advancedgrading)) {
$data->advancedgrading = null;
}
return $data;
}
}