Exemplo n.º 1
0
/**
 * 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;
}
Exemplo n.º 2
0
/**
 * 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))) {
        print_error('Could not find any user attempts');
    }
    // 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');
        }
    }
    return true;
}
/**
 * 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, $userid = null)
{
    global $USER;
    if (empty($userid)) {
        $userid = $USER->id;
    }
    // Get all the attempts made by the user
    if (!($attempts = game_get_user_attempts($game->id, $userid))) {
        notify('Could not find any user attempts');
        return false;
    }
    // Calculate the best grade
    $bestscore = game_calculate_best_score($game, $attempts);
    //$bestgrade = game_rescale_grade($bestgrade, $game);
    // Save the best grade in the database
    if ($grade = get_record('game_grades', 'gameid', $game->id, 'userid', $userid)) {
        $grade->score = $bestscore;
        $grade->timemodified = time();
        if (!update_record('game_grades', $grade)) {
            notify('Could not update best grade');
            return false;
        }
    } else {
        $grade->gameid = $game->id;
        $grade->userid = $userid;
        $grade->score = $bestscore;
        $grade->timemodified = time();
        if (!insert_record('game_grades', $grade)) {
            notify('Could not insert new best grade');
            return false;
        }
    }
    return true;
}