/**
 * Update grades in central gradebook
 *
 * @param object $game null means all games
 * @param int $userid specific user only, 0 mean all
 */
function game_update_grades($game = null, $userid = 0, $nullifnone = true)
{
    global $CFG;
    if (!function_exists('grade_update')) {
        //workaround for buggy PHP versions
        if (file_exists($CFG->libdir . '/gradelib.php')) {
            require_once $CFG->libdir . '/gradelib.php';
        } else {
            return;
        }
    }
    if ($game != null) {
        if ($grades = game_get_user_grades($game, $userid)) {
            game_grade_item_update($game, $grades);
        } else {
            if ($userid and $nullifnone) {
                $grade = new object();
                $grade->userid = $userid;
                $grade->rawgrade = NULL;
                game_grade_item_update($game, $grade);
            } else {
                game_grade_item_update($game);
            }
        }
    } else {
        $sql = "SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid\n                  FROM {$CFG->prefix}game a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m\n                 WHERE m.name='game' AND m.id=cm.module AND cm.instance=a.id";
        if ($rs = get_recordset_sql($sql)) {
            while ($game = rs_fetch_next_record($rs)) {
                if ($game->grade != 0) {
                    game_update_grades($game, 0, false);
                } else {
                    game_grade_item_update($game);
                }
            }
            rs_close($rs);
        }
    }
}
Beispiel #2
0
/**
 * Delete a game attempt.
 */
function game_delete_attempt($attempt, $game)
{
    global $DB;
    if (is_numeric($attempt)) {
        if (!($attempt = $DB->get_record('game_attempts', 'id', $attempt))) {
            return;
        }
    }
    if ($attempt->gameid != $game->id) {
        debugging("Trying to delete attempt {$attempt->id} which belongs to game {$attempt->gameid} " . "but was passed gameid {$game->id}.");
        return;
    }
    $DB->delete_records('game_attempts', array('id' => $attempt->id));
    delete_attempt($attempt->id);
    // Search game_attempts for other instances by this user.
    // If none, then delete record for this game, this user from game_grades
    // else recalculate best grade
    $userid = $attempt->userid;
    if (!$DB->record_exists('game_attempts', array('userid' => $userid, 'gameid' => $game->id))) {
        $DB->delete_records('game_grades', array('userid' => $userid, 'gameid' => $game->id));
    } else {
        game_save_best_score($game);
    }
    game_update_grades($game, $userid);
}