Ejemplo n.º 1
0
/**
 * Create grade item for given seplment.
 *
 * @param stdClass $sepl record with extra cmidnumber
 * @param array $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
 * @return int 0 if ok, error code otherwise
 */
function sepl_grade_item_update($sepl, $grades = null)
{
    global $CFG;
    require_once $CFG->libdir . '/gradelib.php';
    if (!isset($sepl->courseid)) {
        $sepl->courseid = $sepl->course;
    }
    $params = array('itemname' => $sepl->name, 'idnumber' => $sepl->cmidnumber);
    // Check if feedback plugin for gradebook is enabled, if yes then
    // gradetype = GRADE_TYPE_TEXT else GRADE_TYPE_NONE.
    $gradefeedbackenabled = false;
    if (isset($sepl->gradefeedbackenabled)) {
        $gradefeedbackenabled = $sepl->gradefeedbackenabled;
    } else {
        if ($sepl->grade == 0) {
            // Grade feedback is needed only when grade == 0.
            require_once $CFG->dirroot . '/mod/sepl/locallib.php';
            $mod = get_coursemodule_from_instance('sepl', $sepl->id, $sepl->courseid);
            $cm = context_module::instance($mod->id);
            $seplment = new sepl($cm, null, null);
            $gradefeedbackenabled = $seplment->is_gradebook_feedback_enabled();
        }
    }
    if ($sepl->grade > 0) {
        $params['gradetype'] = GRADE_TYPE_VALUE;
        $params['grademax'] = $sepl->grade;
        $params['grademin'] = 0;
    } else {
        if ($sepl->grade < 0) {
            $params['gradetype'] = GRADE_TYPE_SCALE;
            $params['scaleid'] = -$sepl->grade;
        } else {
            if ($gradefeedbackenabled) {
                // $sepl->grade == 0 and feedback enabled.
                $params['gradetype'] = GRADE_TYPE_TEXT;
            } else {
                // $sepl->grade == 0 and no feedback enabled.
                $params['gradetype'] = GRADE_TYPE_NONE;
            }
        }
    }
    if ($grades === 'reset') {
        $params['reset'] = true;
        $grades = null;
    }
    return grade_update('mod/sepl', $sepl->courseid, 'mod', 'sepl', $sepl->id, 0, $grades, $params);
}