Skip to content

Commit

Permalink
CTP-3678 coursework_update_grades (#18)
Browse files Browse the repository at this point in the history
* CTP-3678 coursework_update_grades

* CTP-3678 coursework_get_user_grades PHPDoc
  • Loading branch information
watson8 authored Sep 23, 2024
1 parent 6b19515 commit e0dd5a3
Showing 1 changed file with 69 additions and 5 deletions.
74 changes: 69 additions & 5 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function mod_coursework_core_calendar_event_action_shows_item_count(calendar_eve

/**
* Create grade item for given coursework
* @param \mod_coursework\models\coursework $coursework object with extra cmid number
* @param \mod_coursework\models\coursework|stdClass $coursework object with extra cmid number
* @param null|array $grades array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
Expand All @@ -367,10 +367,22 @@ function coursework_grade_item_update($coursework, $grades = null) {

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

$paramtype = gettype($coursework);
if ($paramtype != 'object') {
throw new invalid_parameter_exception("Invalid type '$paramtype' for coursework");
}

if (get_class($coursework) != 'mod_coursework\models\coursework') {
// On activity rename, core will pass in stdClass object here, not a coursework.
$coursework = \mod_coursework\models\coursework::find($coursework);
}

$courseid = $coursework->get_course_id();

$params = ['itemname' => $coursework->name,
'idnumber' => $coursework->get_coursemodule_idnumber()];
$params = [
'itemname' => $coursework->name,
'idnumber' => $coursework->get_coursemodule_idnumber(),
];

if ($coursework->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
Expand All @@ -390,8 +402,60 @@ function coursework_grade_item_update($coursework, $grades = null) {
$grades = null;
}

return grade_update('mod/coursework', $courseid, 'mod', 'coursework', $coursework->id, 0,
$grades, $params);
return grade_update(
'mod/coursework', $courseid, 'mod', 'coursework',
$coursework->id, 0, $grades, $params
);
}

/**
* Update coursework grades in the gradebook.
* This will be called to rename the grade item when {@link core_courseformat\local\cmactions::rename()} is used.
* Needed by {@link grade_update_mod_grades()} (Force full update of module grades in central gradebook).
*
* @param stdClass $moduleinstance Instance object with extra cmidnumber and modname property.
* @param int $userid Update grade of specific user only, 0 means all participants.
* @param bool $nullifnone If true and the user has no grade then a grade item with rawgrade == null
*/
function coursework_update_grades(stdClass $moduleinstance, int $userid = 0, $nullifnone = true) {
// Code adapted from mod_assign.
global $CFG;
require_once($CFG->libdir.'/gradelib.php');

if ($moduleinstance->grade == 0) {
coursework_grade_item_update($moduleinstance);

} else if ($grades = coursework_get_user_grades($moduleinstance, $userid)) {
foreach ($grades as $k => $v) {
if ($v->rawgrade == -1) {
$grades[$k]->rawgrade = null;
}
}
coursework_grade_item_update($moduleinstance, $grades);

} else {
coursework_grade_item_update($moduleinstance);
}
}

/**
* Return gradebook grade for given user or all users.
*
* @param object $moduleinstance
* @param int $userid user ID.
* @return array array of grades
*/
function coursework_get_user_grades(object $moduleinstance, int $userid): array {

// If no user ID supplied, this returns information about grade_item only.
$grades = grade_get_grades(
$moduleinstance->course,
'mod',
'coursework',
$moduleinstance->id,
$userid != 0 ? $userid : null
);
return reset($grades->items)->grades ?? [];
}

/**
Expand Down

0 comments on commit e0dd5a3

Please sign in to comment.