Manual Course Completion and Zero'ing out empty grades

Manual Course Completion and Zero'ing out empty grades

by Joshua Johnston -
Number of replies: 4

I need to allow a student to click a button that says they are done with the course and it should give them a zero grade for any gradable item not attempted. Is there a way to do this in Moodle using built in functionality? I thought the course completion block would do it but it doesn't give people zeros.

I am using Moodle 2.4+

Average of ratings: -
In reply to Joshua Johnston

Re: Manual Course Completion and Zero'ing out empty grades

by Joshua Johnston -

I wrote some quick and dirty code to do this by inserting the needed records in to grade_grades but that doesn't give a course total grade. what else needs to be done?

In reply to Joshua Johnston

Re: Manual Course Completion and Zero'ing out empty grades

by Bob Puffer -

This could have easily gone to the gradebook forum.  In addition to inserting grade_grades records you need to set 'needsupdate' for the course grade_item record to '1'

In reply to Bob Puffer

Re: Manual Course Completion and Zero'ing out empty grades

by Joshua Johnston -

Maybe so. Since it was completion and grading I wasn't sure which topic would be more relevant so I put it in General in hopes that someone more versed in moodle than myself would have some insight.

In reply to Joshua Johnston

Re: Manual Course Completion and Zero'ing out empty grades

by Joshua Johnston -

Since no one seems to know how moodle works well enough to answer my question, here is my solution:


$emptygrades = array();
$ungradedsql = "SELECT * "
. "FROM {grade_items} "
. "WHERE courseid = :courseid "
. "AND id NOT IN ("
. "SELECT itemid FROM {grade_grades} "
. "WHERE userid = :userid)";
$results = $DB->get_records_sql($ungradedsql, array('courseid' => $course->id, 'userid' => $USER->id));
$fieldmap = array(
'itemid' => 'id',
'rawgrademax' => 'grademax',
'rawgrademin' => 'grademin',
'rawscaleid' => 'scaleid',
'hidden' => 'hidden',
'locked' => 'locked',
'locktime' => 'locktime',
);
foreach ($results as $grade_item) {
$grade = array(
'userid' => $USER->id,
'rawgrade' => 0,
'usermodified' => null,
'finalgrade' => 0,
'exported' => 0,
'overridden' => 0,
'excluded' => 0,
'feedback' => 'No attempts before course was closed',
'feedbackformat' => FORMAT_MOODLE,
'information' => null,
'informationformat' => 0,
'timecreated' => time(),
'timemodified' => time()
);
foreach ($fieldmap as $grade_field => $item_field) {
$grade[$grade_field] = $grade_item->$item_field;
}
if (!array_key_exists($grade_item->itemmodule, $emptygrades)) {
$emptygrades[$grade_item->itemmodule] = array();
}
$emptygrades[$grade_item->itemmodule][$grade_item->iteminstance] = $grade;
}
$modinfo = get_fast_modinfo($course->id);
foreach($modinfo->cms as $module) {
if (array_key_exists($module->modname, $emptygrades)
&& array_key_exists($module->instance, $emptygrades[$module->modname])) {
$grades = array($USER->id => $emptygrades[$module->modname][$module->instance]);
grade_update('mod/' . $module->modname, $course->id, 'mod', $module->modname, $module->instance, 0, $grades);
}
}