Example #1
0
function game_updateattempts($game, $attempt, $score, $finished)
{
    global $DB, $USER;
    if ($attempt != false) {
        $updrec->id = $attempt->id;
        $updrec->timelastattempt = time();
        $updrec->lastip = getremoteaddr();
        if (isset($_SERVER['REMOTE_HOST'])) {
            $updrec->lastremotehost = $_SERVER['REMOTE_HOST'];
        } else {
            $updrec->lastremotehost = gethostbyaddr($updrec->lastip);
        }
        if ($score >= 0) {
            $updrec->score = $score;
        }
        if ($finished) {
            $updrec->timefinish = $updrec->timelastattempt;
        }
        $updrec->attempts = $attempt->attempts + 1;
        if (!$DB->update_record('game_attempts', $updrec)) {
            show_error("game_updateattempts: Can't update game_attempts id={$updrec->id}");
        }
        // update grade item and send all grades to gradebook
        game_grade_item_update($game);
        game_update_grades($game);
    }
    //Update table game_grades
    if ($finished) {
        game_save_best_score($game);
    }
}
/**
 * Removes all grades from gradebook
 * @param int $courseid
 * @param string optional type
 */
function game_reset_gradebook($courseid, $type = '')
{
    global $CFG;
    $sql = "SELECT q.*, cm.idnumber as cmidnumber, q.course as courseid\n              FROM {$CFG->prefix}game q, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m\n             WHERE m.name='game' AND m.id=cm.module AND cm.instance=q.id AND q.course={$courseid}";
    if ($games = get_records_sql($sql)) {
        foreach ($games as $game) {
            game_grade_item_update($game, 'reset');
        }
    }
}
/**
 * Save the overall grade for a user at a game in the game_grades table
 *
 * @param object $quiz The game for which the best grade is to be calculated and then saved.
 * @param integer $userid The userid to calculate the grade for. Defaults to the current user.
 * @return boolean Indicates success or failure.
 */
function game_save_best_score($game)
{
    global $DB, $USER;
    // Get all the attempts made by the user
    if (!($attempts = game_get_user_attempts($game->id, $USER->id, 'all'))) {
        print_error('Could not find any user attempts gameid=' . $game->id . ' userid=' . $USER->id);
    }
    // Calculate the best grade
    $bestscore = game_calculate_best_score($game, $attempts);
    // Save the best grade in the database
    if ($grade = $DB->get_record('game_grades', array('gameid' => $game->id, 'userid' => $USER->id))) {
        $grade->score = $bestscore;
        $grade->timemodified = time();
        if (!$DB->update_record('game_grades', $grade)) {
            print_error('Could not update best grade');
        }
    } else {
        $grade->gameid = $game->id;
        $grade->userid = $USER->id;
        $grade->score = $bestscore;
        $grade->timemodified = time();
        if (!$DB->insert_record('game_grades', $grade)) {
            print_error('Could not insert new best grade');
        }
    }
    // update gradebook
    $grades = new stdClass();
    $grades->userid = $USER->id;
    $grades->rawgrade = game_score_to_grade($bestscore, $game);
    $grades->datesubmitted = time();
    game_grade_item_update($game, $grades);
    return true;
}