Skip to content
Browse files

Merge pull request #23 from marinaglancy/wip-nov07-rubric

  • Loading branch information...
2 parents fa4cbd2 + 188e840 commit 903198de7b5cd00b86db38f0d3bd644047632cff @mudrd8mz mudrd8mz committed Nov 7, 2011
View
39 grade/grading/form/lib.php
@@ -397,6 +397,9 @@ public function get_active_instances($itemid) {
*/
public function has_active_instances() {
global $DB;
+ if (empty($this->definition->id)) {
+ return false;
+ }
if ($this->hasactiveinstances === null) {
$conditions = array('formid' => $this->definition->id,
'status' => gradingform_instance::INSTANCE_STATUS_ACTIVE);
@@ -446,6 +449,25 @@ public function create_instance($raterid, $itemid = null) {
}
/**
+ * If instanceid is specified and grading instance exists and it is created by this rater for
+ * this item, this instance is returned.
+ * Otherwise new instance is created for the specified rater and itemid
+ *
+ * @param int $instanceid
+ * @param int $raterid
+ * @param int $itemid
+ * @return gradingform_instance
+ */
+ public function get_or_create_instance($instanceid, $raterid, $itemid) {
+ global $DB;
+ if ($instanceid &&
+ $instance = $DB->get_record('grading_instances', array('id' => $instanceid, 'raterid' => $raterid, 'itemid' => $itemid), '*', IGNORE_MISSING)) {
+ return $this->get_instance($instance);
+ }
+ return $this->create_instance($raterid, $itemid);
+ }
+
+ /**
* Returns the HTML code displaying the preview of the grading form
*
* Plugins are forced to override this. Ideally they should delegate
@@ -672,12 +694,25 @@ public function get_controller() {
}
/**
+ * Returns the specified element from object $this->data
+ *
+ * @param string $key
+ * @return mixed
+ */
+ public function get_data($key) {
+ if (isset($this->data->$key)) {
+ return $this->data->$key;
+ }
+ return null;
+ }
+
+ /**
* Returns instance id
*
* @return int
*/
public function get_id() {
- return $this->data->id;
+ return $this->get_data('id');
}
/**
@@ -686,7 +721,7 @@ public function get_id() {
* @return int
*/
public function get_status() {
- return $this->data->status;
+ return $this->get_data('status');
}
/**
View
134 grade/grading/form/rubric/js/rubriceditor.js
@@ -23,13 +23,17 @@ M.gradingform_rubriceditor.addhandlers = function() {
M.gradingform_rubriceditor.eventhandler = Y.on('click', M.gradingform_rubriceditor.buttonclick, '#rubric-'+name+' input[type=submit]', null);
}
+// switches all input text elements to non-edit mode
M.gradingform_rubriceditor.disablealleditors = function() {
var Y = M.gradingform_rubriceditor.Y
var name = M.gradingform_rubriceditor.name
Y.all('#rubric-'+name+' .level').each( function(node) {M.gradingform_rubriceditor.editmode(node, false)} );
Y.all('#rubric-'+name+' .description').each( function(node) {M.gradingform_rubriceditor.editmode(node, false)} );
}
+// function invoked on each click on the page. If level and/or criterion description is clicked
+// it switches this element to edit mode. If rubric button is clicked it does nothing so the 'buttonclick'
+// function is invoked
M.gradingform_rubriceditor.clickanywhere = function(e) {
var el = e.target
// if clicked on button - disablecurrenteditor, continue
@@ -54,56 +58,55 @@ M.gradingform_rubriceditor.clickanywhere = function(e) {
M.gradingform_rubriceditor.disablealleditors()
}
+// switch the criterion description or level to edit mode or switch back
M.gradingform_rubriceditor.editmode = function(el, editmode, focustb) {
var ta = el.one('textarea')
- if (!ta.get('parentNode').one('.plainvalue')) {
- ta.get('parentNode').append('<div class="plainvalue"></div>')
- }
- var tb = el.one('input[type=text]')
- if (tb && !tb.get('parentNode').one('.plainvalue')) {
- tb.get('parentNode').append('<div class="plainvalue"></div>')
+ if (!editmode && ta.getStyle('display') == 'none') return;
+ if (editmode && ta.getStyle('display') == 'block') return;
+ var pseudotablink = '<a href="#" class="pseudotablink"> </a>',
+ taplain = ta.get('parentNode').one('.plainvalue'),
+ tbplain = null,
+ tb = el.one('input[type=text]')
+ // add 'plainvalue' next to textarea for description/definition and next to input text field for score (if applicable)
+ if (!taplain) {
+ ta.get('parentNode').append('<div class="plainvalue"><span></span>'+pseudotablink+'</div>')
+ taplain = ta.get('parentNode').one('.plainvalue')
+ taplain.one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
+ if (tb) {
+ tb.get('parentNode').append('<div class="plainvalue"><span></span>'+pseudotablink+'</div>')
+ tbplain = tb.get('parentNode').one('.plainvalue')
+ tbplain.one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
+ }
}
+ if (tb && !tbplain) tbplain = tb.get('parentNode').one('.plainvalue')
if (!editmode) {
- if (ta.getStyle('display') == 'none') return;
+ // if we need to hide the input fields, copy their contents to plainvalue(s). If description/definition
+ // is empty, display the default text ('Click to edit ...') and add/remove 'empty' CSS class to element
var value = ta.get('value')
- if (value.length) ta.get('parentNode').one('.plainvalue').removeClass('empty')
+ if (value.length) taplain.removeClass('empty')
else {
value = (el.hasClass('level')) ? M.str.gradingform_rubric.levelempty : M.str.gradingform_rubric.criterionempty
- ta.get('parentNode').one('.plainvalue').addClass('empty')
- }
- ta.get('parentNode').one('.plainvalue').set('innerHTML', value)
- ta.get('parentNode').one('.plainvalue').setStyle('display', 'block')
- ta.setStyle('display', 'none')
- if (tb) {
- tb.get('parentNode').one('.plainvalue').set('innerHTML', tb.get('value'))
- tb.get('parentNode').one('.plainvalue').setStyle('display', 'inline-block')
- tb.setStyle('display', 'none')
+ taplain.addClass('empty')
}
+ taplain.one('span').set('innerHTML', value)
+ if (tb) tbplain.one('span').set('innerHTML', tb.get('value'))
} else {
- if (tb) {
- tb.get('parentNode').one('.plainvalue').setStyle('display', 'none')
- tb.setStyle('display', 'inline-block')
- }
- var width = ta.get('parentNode').getComputedStyle('width') // TODO min width
- var height = ta.get('parentNode').getComputedStyle('height') // TODO min height
- if (el.hasClass('level')) {
- height = el.getComputedStyle('height') - el.one('.score').getComputedStyle('height')
- } else if (el.hasClass('description')) {
- height = el.get('parentNode').getComputedStyle('height')
- }
- ta.get('parentNode').one('.plainvalue').setStyle('display', 'none')
- ta.setStyle('display', 'block').setStyle('width', width).setStyle('height', height)
- if (tb && focustb) tb.focus(); else ta.focus()
+ // if we need to show the input fields, set the width/height for textarea so it fills the cell
+ var width = parseFloat(ta.get('parentNode').getComputedStyle('width')),
+ height
+ if (el.hasClass('level')) height = parseFloat(el.getComputedStyle('height')) - parseFloat(el.one('.score').getComputedStyle('height'))
+ else height = parseFloat(ta.get('parentNode').getComputedStyle('height'))
+ ta.setStyle('width', Math.max(width,50)+'px').setStyle('height', Math.max(height,20)+'px')
}
- if (!ta.get('parentNode').one('.plainvalue').one('.pseudotablink')) {
- var pseudotablink = '<a href="#" class="pseudotablink"> </a>'
- ta.get('parentNode').one('.plainvalue').append(pseudotablink)
- ta.get('parentNode').one('.plainvalue').one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
- if (tb) {
- tb.get('parentNode').one('.plainvalue').append(pseudotablink)
- tb.get('parentNode').one('.plainvalue').one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
- }
+ // hide/display textarea, textbox and plaintexts
+ taplain.setStyle('display', editmode ? 'none' : 'block')
+ ta.setStyle('display', editmode ? 'block' : 'none')
+ if (tb) {
+ tbplain.setStyle('display', editmode ? 'none' : 'inline-block')
+ tb.setStyle('display', editmode ? 'inline-block' : 'none')
}
+ // focus the proper input field in edit mode
+ if (editmode) { if (tb && focustb) tb.focus(); else ta.focus() }
}
// handler for clicking on submit buttons within rubriceditor element. Adds/deletes/rearranges criteria and/or levels on client side
@@ -133,43 +136,52 @@ M.gradingform_rubriceditor.buttonclick = function(e, confirmed) {
};
if (chunks.length == 3 && action == 'addcriterion') {
// ADD NEW CRITERION
- var nlevels = 3
- var criteria = Y.all('#'+name+'-criteria .criterion')
- if (criteria.size()) nlevels = Math.max(nlevels, criteria.item(criteria.size()-1).all('.level').size())
- var levelsstr = '';
- for (var levidx=0;levidx<nlevels;levidx++) {
- levelsstr += M.gradingform_rubriceditor.templates[name]['level'].
- replace(/\{CRITERION-id\}/g, 'NEWID'+newid).replace(/\{LEVEL-id\}/g, 'NEWID'+(newlevid+levidx)).replace(/\{.+?\}/g, '')
- }
- var newcriterion = M.gradingform_rubriceditor.templates[name]['criterion'].
- replace(/\{CRITERION-id\}/g, 'NEWID'+newid).replace(/\{LEVELS\}/, levelsstr).replace(/\{.+?\}/g, '')
+ var levelsscores = [0], levidx = 1
var parentel = Y.one('#'+name+'-criteria')
if (parentel.one('>tbody')) parentel = parentel.one('>tbody')
- parentel.append(newcriterion)
- M.gradingform_rubriceditor.addhandlers();
+ if (parentel.all('.criterion').size()) {
+ var lastcriterion = parentel.all('.criterion').item(parentel.all('.criterion').size()-1).all('.level')
+ for (levidx=0;levidx<lastcriterion.size();levidx++) levelsscores[levidx] = lastcriterion.item(levidx).one('.score input[type=text]').get('value')
+ }
+ for (levidx;levidx<3;levidx++) levelsscores[levidx] = parseFloat(levelsscores[levidx-1])+1
+ var levelsstr = '';
+ for (levidx=0;levidx<levelsscores.length;levidx++) {
+ levelsstr += M.gradingform_rubriceditor.templates[name]['level'].replace(/\{LEVEL-id\}/g, 'NEWID'+(newlevid+levidx)).replace(/\{LEVEL-score\}/g, levelsscores[levidx])
+ }
+ var newcriterion = M.gradingform_rubriceditor.templates[name]['criterion'].replace(/\{LEVELS\}/, levelsstr)
+ parentel.append(newcriterion.replace(/\{CRITERION-id\}/g, 'NEWID'+newid).replace(/\{.+?\}/g, ''))
M.gradingform_rubriceditor.assignclasses('#rubric-'+name+' #'+name+'-criteria-NEWID'+newid+'-levels .level')
+ M.gradingform_rubriceditor.addhandlers();
+ M.gradingform_rubriceditor.disablealleditors()
+ M.gradingform_rubriceditor.assignclasses(elements_str)
M.gradingform_rubriceditor.editmode(Y.one('#rubric-'+name+' #'+name+'-criteria-NEWID'+newid+'-description'),true)
} else if (chunks.length == 5 && action == 'addlevel') {
// ADD NEW LEVEL
+ var newscore = 0;
+ parent = Y.one('#'+name+'-criteria-'+chunks[2]+'-levels')
+ parent.all('.level').each(function (node) { newscore = Math.max(newscore, parseFloat(node.one('.score input[type=text]').get('value'))+1) })
var newlevel = M.gradingform_rubriceditor.templates[name]['level'].
- replace(/\{CRITERION-id\}/g, chunks[2]).replace(/\{LEVEL-id\}/g, 'NEWID'+newlevid).replace(/\{.+?\}/g, '')
- Y.one('#'+name+'-criteria-'+chunks[2]+'-levels').append(newlevel)
- var levels = Y.all('#'+name+'-criteria-'+chunks[2]+'-levels .level')
- if (levels.size()) levels.set('width', Math.round(100/levels.size())+'%')
+ replace(/\{CRITERION-id\}/g, chunks[2]).replace(/\{LEVEL-id\}/g, 'NEWID'+newlevid).replace(/\{LEVEL-score\}/g, newscore).replace(/\{.+?\}/g, '')
+ parent.append(newlevel)
M.gradingform_rubriceditor.addhandlers();
- M.gradingform_rubriceditor.editmode(levels.item(levels.size()-1),true)
+ M.gradingform_rubriceditor.disablealleditors()
+ M.gradingform_rubriceditor.assignclasses(elements_str)
+ M.gradingform_rubriceditor.editmode(parent.all('.level').item(parent.all('.level').size()-1), true)
} else if (chunks.length == 4 && action == 'moveup') {
// MOVE CRITERION UP
el = Y.one('#'+name+'-criteria-'+chunks[2])
if (el.previous()) el.get('parentNode').insertBefore(el, el.previous())
+ M.gradingform_rubriceditor.assignclasses(elements_str)
} else if (chunks.length == 4 && action == 'movedown') {
// MOVE CRITERION DOWN
el = Y.one('#'+name+'-criteria-'+chunks[2])
if (el.next()) el.get('parentNode').insertBefore(el.next(), el)
+ M.gradingform_rubriceditor.assignclasses(elements_str)
} else if (chunks.length == 4 && action == 'delete') {
// DELETE CRITERION
if (confirmed) {
Y.one('#'+name+'-criteria-'+chunks[2]).remove()
+ M.gradingform_rubriceditor.assignclasses(elements_str)
} else {
dialog_options['message'] = M.str.gradingform_rubric.confirmdeletecriterion
M.util.show_confirm_dialog(e, dialog_options);
@@ -178,8 +190,7 @@ M.gradingform_rubriceditor.buttonclick = function(e, confirmed) {
// DELETE LEVEL
if (confirmed) {
Y.one('#'+name+'-criteria-'+chunks[2]+'-'+chunks[3]+'-'+chunks[4]).remove()
- levels = Y.all('#'+name+'-criteria-'+chunks[2]+'-levels .level')
- if (levels.size()) levels.set('width', Math.round(100/levels.size())+'%')
+ M.gradingform_rubriceditor.assignclasses(elements_str)
} else {
dialog_options['message'] = M.str.gradingform_rubric.confirmdeletelevel
M.util.show_confirm_dialog(e, dialog_options);
@@ -189,21 +200,22 @@ M.gradingform_rubriceditor.buttonclick = function(e, confirmed) {
return;
}
e.preventDefault();
- // properly set classes and sortorder
- M.gradingform_rubriceditor.assignclasses(elements_str)
}
+// properly set classes (first/last/odd/even), level width and/or criterion sortorder for elements Y.all(elements_str)
M.gradingform_rubriceditor.assignclasses = function (elements_str) {
var elements = M.gradingform_rubriceditor.Y.all(elements_str)
for (var i=0;i<elements.size();i++) {
elements.item(i).removeClass('first').removeClass('last').removeClass('even').removeClass('odd').
addClass(((i%2)?'odd':'even') + ((i==0)?' first':'') + ((i==elements.size()-1)?' last':''))
elements.item(i).all('input[type=hidden]').each(
- function(node) { if (node.get('name').match(/sortorder/)) node.set('value', i) }
+ function(node) {if (node.get('name').match(/sortorder/)) node.set('value', i)}
);
+ if (elements.item(i).hasClass('level')) elements.item(i).set('width', Math.round(100/elements.size())+'%')
}
}
+// returns unique id for the next added element, it should not be equal to any of Y.all(elements_str) ids
M.gradingform_rubriceditor.calculatenewid = function (elements_str) {
var newid = 1
M.gradingform_rubriceditor.Y.all(elements_str).each( function(node) {
View
1 grade/grading/form/rubric/lang/en/gradingform_rubric.php
@@ -82,5 +82,6 @@
$string['needregrademessage'] = 'Rubric definition was changed after this student had been graded. You must update the grade otherwise it will not be shown to student.';
$string['rubricnotcompleted'] = 'Please choose something for each criterion';
+$string['restoredfromdraft'] = 'Please note: You did not submit the last attempt to grade this person. The grading is restored from draft. To cancel the changes press "Cancel" button below';
$string['backtoediting'] = 'Back to editing';
View
47 grade/grading/form/rubric/lib.php
@@ -484,6 +484,36 @@ protected function delete_plugin_definition() {
}
/**
+ * If instanceid is specified and grading instance exists and it is created by this rater for
+ * this item, this instance is returned.
+ * If there exists a draft for this raterid+itemid, take this draft (this is the change from parent)
+ * Otherwise new instance is created for the specified rater and itemid
+ *
+ * @param int $instanceid
+ * @param int $raterid
+ * @param int $itemid
+ * @return gradingform_instance
+ */
+ public function get_or_create_instance($instanceid, $raterid, $itemid) {
+ global $DB;
+ if ($instanceid &&
+ $instance = $DB->get_record('grading_instances', array('id' => $instanceid, 'raterid' => $raterid, 'itemid' => $itemid), '*', IGNORE_MISSING)) {
+ return $this->get_instance($instance);
+ }
+ if ($itemid && $raterid) {
+ if ($rs = $DB->get_records('grading_instances', array('raterid' => $raterid, 'itemid' => $itemid), 'timemodified DESC', '*', 0, 1)) {
+ $record = reset($rs);
+ $currentinstance = $this->get_current_instance($raterid, $itemid);
+ if ($record->status == gradingform_rubric_instance::INSTANCE_STATUS_INCOMPLETE && $record->timemodified > $currentinstance->get_data('timemodified')) {
+ $record->isrestored = true;
+ return $this->get_instance($record);
+ }
+ }
+ }
+ return $this->create_instance($raterid, $itemid);
+ }
+
+ /**
* Returns html code to be included in student's feedback.
*
* @param moodle_page $page
@@ -725,6 +755,23 @@ public function render_grading_element($page, $gradingformelement) {
if ($currentinstance && $currentinstance->get_status() == gradingform_instance::INSTANCE_STATUS_NEEDUPDATE) {
$html .= html_writer::tag('div', get_string('needregrademessage', 'gradingform_rubric'), array('class' => 'gradingform_rubric-regrade'));
}
+ $haschanges = false;
+ if ($currentinstance) {
+ $curfilling = $currentinstance->get_rubric_filling();
+ foreach ($curfilling['criteria'] as $criterionid => $curvalues) {
+ $value['criteria'][$criterionid]['savedlevelid'] = $curvalues['levelid'];
+ $newremark = null;
+ $newlevelid = null;
+ if (isset($value['criteria'][$criterionid]['remark'])) $newremark = $value['criteria'][$criterionid]['remark'];
+ if (isset($value['criteria'][$criterionid]['levelid'])) $newlevelid = $value['criteria'][$criterionid]['levelid'];
+ if ($newlevelid != $curvalues['levelid'] || $newremark != $curvalues['remark']) {
+ $haschanges = true;
+ }
+ }
+ }
+ if ($this->get_data('isrestored') && $haschanges) {
+ $html .= html_writer::tag('div', get_string('restoredfromdraft', 'gradingform_rubric'), array('class' => 'gradingform_rubric-restored'));
+ }
$html .= $this->get_controller()->get_renderer($page)->display_rubric($criteria, $options, $mode, $gradingformelement->getName(), $value);
return $html;
}
View
3 grade/grading/form/rubric/renderer.php
@@ -355,6 +355,9 @@ public function display_rubric($criteria, $options, $mode, $elementname = null,
$level['class'] .= ' checked';
//in mode DISPLAY_EVAL the class 'checked' will be added by JS if it is enabled. If JS is not enabled, the 'checked' class will only confuse
}
+ if (isset($criterionvalue['savedlevelid']) && ((int)$criterionvalue['savedlevelid'] === $levelid)) {
+ $level['class'] .= ' currentchecked';
+ }
$level['tdwidth'] = 100/count($criterion['levels']);
$levels_str .= $this->level_template($mode, $options, $elementname, $id, $level);
}
View
24 grade/grading/form/rubric/rubriceditor.php
@@ -152,7 +152,24 @@ function prepare_data($value = null, $withvalidation = false) {
foreach ($value['criteria'] as $id => $criterion) {
if ($id == 'addcriterion') {
$id = $this->get_next_id(array_keys($value['criteria']));
- $criterion = array('description' => '');
+ $criterion = array('description' => '', 'levels' => array());
+ $i = 0;
+ // when adding new criterion copy the number of levels and their scores from the last criterion
+ if (!empty($value['criteria'][$lastid]['levels'])) {
+ foreach ($value['criteria'][$lastid]['levels'] as $lastlevel) {
+ $criterion['levels']['NEWID'+($i++)]['score'] = $lastlevel['score'];
+ }
+ } else {
+ $criterion['levels']['NEWID'+($i++)]['score'] = 0;
+ }
+ // add more levels so there are at least 3 in the new criterion. Increment by 1 the score for each next one
+ for ($i; $i<3; $i++) {
+ $criterion['levels']['NEWID'+$i]['score'] = $criterion['levels']['NEWID'+($i-1)]['score'] + 1;
+ }
+ // set other necessary fields (definition) for the levels in the new criterion
+ foreach (array_keys($criterion['levels']) as $i) {
+ $criterion['levels'][$i]['definition'] = '';
+ }
$this->nonjsbuttonpressed = true;
}
$levels = array();
@@ -165,6 +182,11 @@ function prepare_data($value = null, $withvalidation = false) {
'definition' => '',
'score' => 0,
);
+ foreach ($criterion['levels'] as $lastlevel) {
+ if ($level['score'] < $lastlevel['score'] + 1) {
+ $level['score'] = $lastlevel['score'] + 1;
+ }
+ }
$this->nonjsbuttonpressed = true;
}
if (!array_key_exists('delete', $level)) {
View
2 grade/grading/form/rubric/styles.css
@@ -82,6 +82,7 @@
.gradingform_rubric.editor .criterion.last .controls .movedown input {display:none;}
/* evaluation */
+.gradingform_rubric .criterion .levels .level.currentchecked {background:#fff0f0;}
.gradingform_rubric .criterion .levels .level.checked {background:#d0ffd0;border:1px solid #555;}
.gradingform_rubric.evaluate .criterion .levels .level:hover {background:#30ff30;}
@@ -114,4 +115,5 @@
*/
.gradingform_rubric-regrade {padding:10px;background:#FFDDDD;border:1px solid #F00;margin-bottom:10px;}
+.gradingform_rubric-restored {padding:10px;background:#FFFFDD;border:1px solid #FF0;margin-bottom:10px;}
.gradingform_rubric-error {color:red;font-weight:bold;}
View
5 mod/assignment/lib.php
@@ -615,6 +615,7 @@ function submissions($mode) {
//make user global so we can use the id
global $USER, $OUTPUT, $DB, $PAGE;
+
$mailinfo = optional_param('mailinfo', null, PARAM_BOOL);
if (optional_param('next', null, PARAM_BOOL)) {
@@ -1077,7 +1078,8 @@ function display_submission($offset=-1,$userid =-1, $display=true) {
if (!empty($submission->id)) {
$itemid = $submission->id;
}
- $mformdata->advancedgradinginstance = $controller->create_instance($USER->id, $itemid);
+ $instanceid = optional_param('advancedgradinginstanceid', 0, PARAM_INT);
+ $mformdata->advancedgradinginstance = $controller->get_or_create_instance($instanceid, $USER->id, $itemid);
} else {
$advancedgradingwarning = $controller->form_unavailable_notification();
}
@@ -2389,6 +2391,7 @@ function add_grades_section() {
$grademenu = make_grades_menu($this->_customdata->assignment->grade);
if ($gradinginstance = $this->use_advanced_grading()) {
+ $mform->addElement('hidden', 'advancedgradinginstanceid', $gradinginstance->get_id());
$gradinginstance->get_controller()->set_grade_range($grademenu);
$mform->addElement('grading', 'advancedgrading', get_string('grade').':', array('gradinginstance' => $gradinginstance));
} else {

0 comments on commit 903198d

Please sign in to comment.
Something went wrong with that request. Please try again.