Skip to content

Commit

Permalink
MDL-32933: Add outcomes to mod_assign and upgrade tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed May 14, 2012
1 parent 71d7bc3 commit c45261d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 45 deletions.
30 changes: 28 additions & 2 deletions mod/assign/gradingtable.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ function __construct(assign $assignment, $perpage, $filter, $rowoffset=0) {
$columns[] = 'finalgrade';
$headers[] = get_string('finalgrade', 'grades');

// load the grading info for all users
$this->gradinginfo = grade_get_grades($this->assignment->get_course()->id, 'mod', 'assign', $this->assignment->get_instance()->id, $users);

if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
$columns[] = 'outcomes';
$headers[] = get_string('outcomes', 'grades');
}


// set the columns
Expand All @@ -179,6 +186,8 @@ function __construct(assign $assignment, $perpage, $filter, $rowoffset=0) {
$this->no_sorting('finalgrade');
$this->no_sorting('edit');
$this->no_sorting('select');
$this->no_sorting('outcomes');

foreach ($this->assignment->get_submission_plugins() as $plugin) {
if ($plugin->is_visible() && $plugin->is_enabled()) {
$this->no_sorting('assignsubmission_' . $plugin->get_type());
Expand All @@ -190,8 +199,6 @@ function __construct(assign $assignment, $perpage, $filter, $rowoffset=0) {
}
}

// load the grading info for all users
$this->gradinginfo = grade_get_grades($this->assignment->get_course()->id, 'mod', 'assign', $this->assignment->get_instance()->id, $users);
}

/**
Expand Down Expand Up @@ -228,6 +235,25 @@ function display_grade($grade) {
return $o;
}

/**
* Format a list of outcomes
*
* @param stdClass $row
* @return string
*/
function col_outcomes(stdClass $row) {
$outcomes = '';
foreach($this->gradinginfo->outcomes as $index=>$outcome) {
$options = make_grades_menu(-$outcome->scaleid);

$options[0] = get_string('nooutcome', 'grades');
$outcomes .= $this->output->container($outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade], 'outcome');
}

return $outcomes;
}


/**
* Format a user picture for display (and update rownum as a sideeffect)
*
Expand Down
64 changes: 62 additions & 2 deletions mod/assign/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public function get_admin_config() {
if ($this->adminconfig) {
return $this->adminconfig;
}
$this->adminconfig = get_config('mod_assign');
$this->adminconfig = get_config('assign');
return $this->adminconfig;
}

Expand Down Expand Up @@ -2487,8 +2487,30 @@ public function add_grade_form_elements(MoodleQuickForm $mform, stdClass $data,
}
}
}
$mform->addElement('static', 'progress', '', get_string('gradingstudentprogress', 'assign', array('index'=>$rownum+1, 'count'=>count($useridlist))));

$gradinginfo = grade_get_grades($this->get_course()->id,
'mod',
'assign',
$this->get_instance()->id,
$userid);
if (!empty($CFG->enableoutcomes)) {
foreach($gradinginfo->outcomes as $index=>$outcome) {
$options = make_grades_menu(-$outcome->scaleid);
if ($outcome->grades[$userid]->locked) {
$options[0] = get_string('nooutcome', 'grades');
$mform->addElement('static', 'outcome_'.$index.'['.$userid.']', $outcome->name.':',
$options[$outcome->grades[$userid]->grade]);
} else {
$options[''] = get_string('nooutcome', 'grades');
$attributes = array('id' => 'menuoutcome_'.$index );
$mform->addElement('select', 'outcome_'.$index.'['.$userid.']', $outcome->name.':', $options, $attributes );
$mform->setType('outcome_'.$index.'['.$userid.']', PARAM_INT);
$mform->setDefault('outcome_'.$index.'['.$userid.']', $outcome->grades[$userid]->grade );
}
}
}

$mform->addElement('static', 'progress', '', get_string('gradingstudentprogress', 'assign', array('index'=>$rownum+1, 'count'=>count($useridlist))));

// plugins
$this->add_plugin_grade_elements($grade, $mform, $data);
Expand Down Expand Up @@ -2701,6 +2723,43 @@ private function process_unlock($userid = 0) {
$this->add_to_log('unlock submission', get_string('unlocksubmissionforstudent', 'assign', array('id'=>$user->id, 'fullname'=>fullname($user))));
}

/**
* save outcomes submitted from grading form
*
* @param int $userid
* @param stdClass $formdata
*/
private function process_outcomes($userid, $formdata) {
global $CFG, $USER;

if (empty($CFG->enableoutcomes)) {
return;
}

require_once($CFG->libdir.'/gradelib.php');

$data = array();
$gradinginfo = grade_get_grades($this->get_course()->id,
'mod',
'assign',
$this->get_instance()->id,
$userid);

if (!empty($gradinginfo->outcomes)) {
foreach($gradinginfo->outcomes as $index=>$oldoutcome) {
$name = 'outcome_'.$index;
if (isset($formdata->{$name}[$userid]) and $oldoutcome->grades[$userid]->grade != $formdata->{$name}[$userid]) {
$data[$index] = $formdata->{$name}[$userid];
}
}
}
if (count($data) > 0) {
grade_update_outcomes('mod/assign', $this->course->id, 'mod', 'assign', $this->get_instance()->id, $userid, $data);
}

}


/**
* save grade
*
Expand Down Expand Up @@ -2763,6 +2822,7 @@ private function process_save_grade(&$mform) {
}
}
}
$this->process_outcomes($userid, $formdata);
$this->update_grade($grade);

$user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
Expand Down
51 changes: 10 additions & 41 deletions mod/assign/upgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function upgrade_assignment($oldassignmentid, & $log) {
$gradingdefinitions = null;
$gradeidmap = array();
$completiondone = false;
$gradesdone = false;

// from this point we want to rollback on failure
$rollback = false;
Expand Down Expand Up @@ -213,14 +214,22 @@ public function upgrade_assignment($oldassignmentid, & $log) {
$newassignment->update_gradebook(false,$newcoursemodule->id);

// copy the grades from the old assignment to the new one
$this->copy_grades_for_upgrade($oldassignment, $newassignment);
$DB->set_field('grade_items', 'itemmodule', 'assign', array('iteminstance'=>$oldassignment->id));
$DB->set_field('grade_items', 'iteminstance', $newassignment->get_instance()->id, array('iteminstance'=>$oldassignment->id));
$gradesdone = true;

} catch (Exception $exception) {
$rollback = true;
$log .= get_string('conversionexception', 'mod_assign', $exception->getMessage());
}

if ($rollback) {
// roll back the grades changes
if ($gradesdone) {
// copy the grades from the old assignment to the new one
$DB->set_field('grade_items', 'itemmodule', 'assignment', array('iteminstance'=>$newassignment->get_instance()->id));
$DB->set_field('grade_items', 'iteminstance', $oldassignment->id, array('iteminstance'=>$newassignment->get_instance()->id));
}
// roll back the completion changes
if ($completiondone) {
$DB->set_field('course_modules_completion', 'coursemoduleid', $oldcoursemodule->id, array('coursemoduleid'=>$newcoursemodule->id));
Expand Down Expand Up @@ -363,44 +372,4 @@ private function delete_course_module($cm) {
return true;
}

/**
* This function copies the grades from the old assignment module to this one.
*
* @param stdClass $oldassignment old assignment data record
* @param assign $newassignment the new assign class
* @return bool true or false
*/
public function copy_grades_for_upgrade($oldassignment, $newassignment) {
global $CFG;

require_once($CFG->libdir.'/gradelib.php');

// get the old and new grade items
$oldgradeitems = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>'assignment', 'iteminstance'=>$oldassignment->id));
if (!$oldgradeitems) {
return false;
}
$oldgradeitem = array_pop($oldgradeitems);
if (!$oldgradeitem) {
return false;
}
$newgradeitems = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>'assign', 'iteminstance'=>$newassignment->get_instance()->id));
if (!$newgradeitems) {
return false;
}
$newgradeitem = array_pop($newgradeitems);
if (!$newgradeitem) {
return false;
}

$gradegrades = grade_grade::fetch_all(array('itemid'=>$oldgradeitem->id));
if ($gradegrades) {
foreach ($gradegrades as $gradeid=>$grade) {
$grade->itemid = $newgradeitem->id;
grade_update('mod/assign', $newassignment->get_course()->id, 'mod', 'assign', $newassignment->get_instance()->id, 0, $grade, NULL);
}
}
return true;
}

}

0 comments on commit c45261d

Please sign in to comment.