Permalink
Browse files

Merge pull request #13 from marinaglancy/wip-formvalidation-rubric

Initial prototype of integrating advanced grading methods into current forms

There is a new form element called 'grading', which receives grading_controller as an argument and uses it's functions to produce html and validate the value.

The biggest change in grading form is that I included call to new function validate_and_preprocess_feedback, which validates and also fills the xgrade field with value, calculated by controller

Another issue: when the teacher grades the student on offline assignment for the first time, there may be no entry in submission table (and therefore no id). So I create a submission if there is advanced grading otherwise we won't be able to save via AJAX the process of filling rubric, because normal grading will create an entry only on submit.

And as an example I created text field in rubric, that is saved as grade. There is validation that value can not be more than 100.
  • Loading branch information...
mudrd8mz committed Oct 12, 2011
2 parents 6abcb0c + 6798c63 commit a5c4c99b24034db5eab416a7ff6a4fd50b29eaff
Showing with 287 additions and 7 deletions.
  1. +24 −0 grade/grading/form/lib.php
  2. +57 −0 grade/grading/form/rubric/lib.php
  3. +13 −0 grade/grading/lib.php
  4. +1 −0 lang/en/grading.php
  5. +118 −0 lib/form/grading.php
  6. +1 −0 lib/formslib.php
  7. +73 −7 mod/assignment/lib.php
View
@@ -264,6 +264,30 @@ public function prepare_instance($raterid, $itemid) {
}
}
+ /**
+ * Saves non-js data and returns the gradebook grade
+ */
+ abstract public function save_and_get_grade($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;
+ }
+
////////////////////////////////////////////////////////////////////////////
@@ -226,6 +226,41 @@ public function get_definition_for_editing() {
return $properties;
}
+ /**
+ * Saves non-js data and returns the gradebook grade
+ */
+ public function save_and_get_grade($itemid, $formdata) {
+ // TODO: this function is a patch at the moment!
+ if (is_array($formdata) && array_key_exists('grade', $formdata)) {
+ return $formdata['grade'];
+ }
+ return -1;
+ }
+
+ /**
+ * Returns html for form element
+ */
+ public function to_html($gradingformelement) {
+ // TODO: this function is a patch at the moment!
+
+ //global $PAGE, $USER;
+ //$gradingrenderer = $this->prepare_renderer($PAGE);
+ $output = '';
+ $elementname = $gradingformelement->getName();
+ $elementvalue = $gradingformelement->getValue();
+ $submissionid = $gradingformelement->get_grading_attribute('submissionid');
+ $output .= "assessing submission $submissionid<br />";
+ $output .= html_writer::empty_tag('input', array('type' => 'text', 'name' => $elementname.'[grade]', 'size' => '20', 'value' => $elementvalue['grade']));
+ //$output .= "assessing user $userid on assignment $assignmentid<br>";
+ //TODO find $submissionid from $userid & $assignmentid (may not exist yet, actually)
+ /*$submissionid = null;
+ $gradingwidget = $this->make_grading_widget($USER->id, $submissionid);
+ if ($gradingwidget instanceof renderable) {
+ return $output. $gradingrenderer->render($gradingwidget);
+ }*/
+ return $output;
+ }
+
// TODO the following functions may be moved to parent:
/**
@@ -273,4 +308,26 @@ public function postupdate_definition_data($data) {
// 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!
+ }
+
+ /**
+ *
+ */
+ public function default_validation_error_message() {
+ return 'Validation failed';
+ }
+
+ /**
+ *
+ */
+ public function validate_grading_element($elementvalue, $itemid) {
+ if ($elementvalue['grade'] > 100 || $elementvalue['grade'] < 0) {
+ return false;
+ }
+ return true;
+ }
}
View
@@ -374,6 +374,19 @@ public function get_controller($method) {
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
@@ -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
@@ -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
@@ -2487,6 +2487,7 @@ function getValidationScript($format = null) {
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');
Oops, something went wrong.

0 comments on commit a5c4c99

Please sign in to comment.