コード例 #1
0
ファイル: play.php プロジェクト: OctaveBabel/moodle-itop
function game_cryptex_continue($id, $game, $attempt, $cryptexrec, $endofgame, $context)
{
    global $DB, $USER;
    if ($endofgame) {
        game_updateattempts($game, $attempt, -1, true);
        $endofgame = false;
    }
    if ($attempt != false and $cryptexrec != false) {
        $crossm = $DB->get_record('game_cross', array('id' => $attempt->id));
        return game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, false, false, false, $context);
    }
    if ($attempt === false) {
        $attempt = game_addattempt($game);
    }
    $cryptex = new CryptexDB();
    $questions = array();
    $infos = array();
    $answers = array();
    $recs = game_questions_shortanswer($game);
    if ($recs == false) {
        print_error(get_string('no_words', 'game'));
    }
    $infos = array();
    $reps = array();
    foreach ($recs as $rec) {
        if ($game->param7 == false) {
            if (game_strpos($rec->answertext, ' ')) {
                continue;
                // Spaces not allowed.
            }
        }
        $rec->answertext = game_upper($rec->answertext);
        $answers[$rec->answertext] = game_repairquestion($rec->questiontext);
        $infos[$rec->answertext] = array($game->sourcemodule, $rec->questionid, $rec->glossaryentryid);
        $a = array('gameid' => $game->id, 'userid' => $USER->id, 'questionid' => $rec->questionid, 'glossaryentryid' => $rec->glossaryentryid);
        if (($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions AS r')) != false) {
            $reps[$rec->answertext] = $rec2->r;
        }
    }
    $cryptex->setwords($answers, $game->param1, $reps);
    // The game->param4 is minimum words.
    // The game->param2 is maximum words.
    if ($cryptex->computedata($crossm, $crossd, $letters, $game->param4, $game->param2)) {
        $newcrossd = array();
        foreach ($crossd as $rec) {
            if (array_key_exists($rec->answertext, $infos)) {
                $info = $infos[$rec->answertext];
                $rec->id = 0;
                $rec->sourcemodule = $info[0];
                $rec->questionid = $info[1];
                $rec->glossaryentryid = $info[2];
            }
            game_update_queries($game, $attempt, $rec, 0, '');
            $newcrossd[] = $rec;
        }
        $cryptexrec = $cryptex->savecryptex($game, $crossm, $newcrossd, $attempt->id, $letters);
    }
    game_updateattempts($game, $attempt, 0, 0);
    return game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, false, false, false, $context);
}
コード例 #2
0
ファイル: play.php プロジェクト: OctaveBabel/moodle-itop
/**
 * This file plays the game millionaire
 * 
 * @author  bdaloukas
 * @version $Id: play.php,v 1.31 2012/07/25 11:16:05 bdaloukas Exp $
 * @package game
 **/
function game_millionaire_continue($id, $game, $attempt, $millionaire, $context)
{
    // User must select quiz or question as a source module.
    if ($game->quizid == 0 and $game->questioncategoryid == 0) {
        if ($game->sourcemodule == 'quiz') {
            print_error(get_string('millionaire_must_select_quiz', 'game'));
        } else {
            print_error(get_string('millionaire_must_select_questioncategory', 'game'));
        }
    }
    if ($attempt != false and $millionaire != false) {
        // Continue an existing game.
        return game_millionaire_play($id, $game, $attempt, $millionaire, $context);
    }
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    $newrec = new stdClass();
    $newrec->id = $attempt->id;
    $newrec->queryid = 0;
    $newrec->level = 0;
    $newrec->state = 0;
    if (!game_insert_record('game_millionaire', $newrec)) {
        print_error('error inserting in game_millionaire');
    }
    game_millionaire_play($id, $game, $attempt, $newrec, $context);
}
コード例 #3
0
function game_hiddenpicture_continue($id, $game, $attempt, $hiddenpicture)
{
    global $CFG, $USER;
    if ($attempt != false and $hiddenpicture != false) {
        //Continue a previous attempt
        return game_hiddenpicture_play($id, $game, $attempt, $hiddenpicture);
    }
    if ($attempt == false) {
        //Start a new attempt
        $attempt = game_addattempt($game);
    }
    $cols = $game->param1;
    $rows = $game->param2;
    if ($cols == 0) {
        error(get_string('hiddenpicture_nocols', 'game'));
    }
    if ($rows == 0) {
        error(get_string('hiddenpicture_norows', 'game'));
    }
    //new attempt
    $n = $game->param1 * $game->param2;
    $recs = game_questions_selectrandom($game, $n);
    $newrec = game_hiddenpicture_selectglossaryentry($game, $attempt);
    if ($recs === false) {
        error(get_string('hiddenpicture_no_questions', 'game'));
    }
    $positions = array();
    $pos = 1;
    for ($col = 0; $col < $cols; $col++) {
        for ($row = 0; $row < $rows; $row++) {
            $positions[] = $pos++;
        }
    }
    $i = 0;
    foreach ($recs as $rec) {
        if ($i >= $n) {
            break;
        }
        unset($query);
        $query->attemptid = $newrec->id;
        $query->gamekind = $game->gamekind;
        $query->gameid = $game->id;
        $query->userid = $USER->id;
        $pos = array_rand($positions);
        $query->col = $positions[$pos];
        unset($positions[$pos]);
        $query->sourcemodule = $game->sourcemodule;
        $query->questionid = $rec->questionid;
        $query->glossaryentryid = $rec->glossaryentryid;
        $query->score = 0;
        if (($query->id = insert_record("game_queries", $query)) == 0) {
            error('error inserting in game_queries');
        }
    }
    //The score is zero
    game_updateattempts($game, $attempt, 0, 0);
    game_hiddenpicture_play($id, $game, $attempt, $newrec);
}
コード例 #4
0
function game_cross_continue($id, $game, $attempt, $cross, $g = '', $endofgame = '')
{
    if ($endofgame) {
        if ($g == '') {
            game_updateattempts($game, $attempt, -1, true);
            $endofgame = false;
        }
    }
    if ($attempt != false and $cross != false) {
        return game_cross_play($id, $game, $attempt, $cross, $g, false, false, $endofgame, false, false, false, false);
    }
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    $textlib = textlib_get_instance();
    $cross = new CrossDB();
    $questions = array();
    $infos = array();
    $answers = array();
    $recs = game_questions_shortanswer($game);
    if ($recs == false) {
        error('game_cross_continue: ' . get_string('cross_nowords', 'game'));
    }
    $infos = array();
    foreach ($recs as $rec) {
        if ($game->param7 == false) {
            if ($textlib->strpos($rec->answertext, ' ')) {
                continue;
                //spaces not allowed
            }
        }
        $rec->answertext = game_upper($rec->answertext);
        $answers[$rec->answertext] = game_repairquestion($rec->questiontext);
        $infos[$rec->answertext] = array($game->sourcemodule, $rec->questionid, $rec->glossaryentryid, $rec->attachment);
    }
    $cross->setwords($answers, $game->param1);
    if ($cross->computedata($crossm, $crossd, $game->param2)) {
        $new_crossd = array();
        foreach ($crossd as $rec) {
            $info = $infos[$rec->answertext];
            if ($info != false) {
                $rec->sourcemodule = $info[0];
                $rec->questionid = $info[1];
                $rec->glossaryentryid = $info[2];
                $rec->attachment = $info[3];
            }
            $new_crossd[] = $rec;
        }
        $cross->save($game, $crossm, $new_crossd, $attempt->id);
        game_updateattempts($game, $attempt, 0, 0);
        return game_cross_play($id, $game, $attempt, $crossm, '', false, false, false, false, false, false, false);
    }
    if (count($crossd) == 0) {
        error('game_cross_continue: ' . get_string('cross_nowords', 'game'));
    }
}
コード例 #5
0
function game_cryptex_continue($id, $game, $attempt, $cryptexrec, $endofgame)
{
    if ($endofgame) {
        game_updateattempts($game, $attempt, -1, true);
        $endofgame = false;
    }
    if ($attempt != false and $cryptexrec != false) {
        $crossm = get_record_select('game_cross', "id={$attempt->id}");
        return game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm);
    }
    if ($attempt === false) {
        $attempt = game_addattempt($game);
    }
    $textlib = textlib_get_instance();
    $cryptex = new CryptexDB();
    $questions = array();
    $infos = array();
    $answers = array();
    $recs = game_questions_shortanswer($game);
    if ($recs == false) {
        error(get_string('cryptex_nowords', 'game'));
    }
    $infos = array();
    foreach ($recs as $rec) {
        if ($game->param7 == false) {
            if ($textlib->strpos($rec->answertext, ' ')) {
                continue;
                //spaces not allowed
            }
        }
        $rec->answertext = game_upper($rec->answertext);
        $answers[$rec->answertext] = game_repairquestion($rec->questiontext);
        $infos[$rec->answertext] = array($game->sourcemodule, $rec->questionid, $rec->glossaryentryid);
    }
    $cryptex->setwords($answers, $game->param1);
    if ($cryptex->computedata($crossm, $crossd, $letters, $game->param2)) {
        $new_crossd = array();
        foreach ($crossd as $rec) {
            if (array_key_exists($rec->answertext, $infos)) {
                $info = $infos[$rec->answertext];
                $rec->id = 0;
                $rec->sourcemodule = $info[0];
                $rec->questionid = $info[1];
                $rec->glossaryentryid = $info[2];
            }
            game_update_queries($game, $attempt, $rec, 0, '');
            $new_crossd[] = $rec;
        }
        $cryptexrec = $cryptex->save($game, $crossm, $new_crossd, $attempt->id, $letters);
    }
    game_updateattempts($game, $attempt, 0, 0);
    return game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm);
}
コード例 #6
0
function game_bookquiz_continue($id, $game, $attempt, $bookquiz, $chapterid = 0)
{
    if ($attempt != false and $bookquiz != false) {
        return game_bookquiz_play($id, $game, $attempt, $bookquiz, $chapterid);
    }
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    unset($bookquiz);
    $bookquiz->lastchapterid = 0;
    $bookquiz->id = $attempt->id;
    if (!game_insert_record('game_bookquiz', $bookquiz)) {
        error('game_bookquiz_continue: error inserting in game_bookquiz');
    }
    return game_bookquiz_play($id, $game, $attempt, $bookquiz, 0);
}
コード例 #7
0
ファイル: play.php プロジェクト: vinoth4891/clinique
function game_bookquiz_continue($id, $game, $attempt, $bookquiz, $chapterid, $context)
{
    if ($attempt != false and $bookquiz != false) {
        return game_bookquiz_play($id, $game, $attempt, $bookquiz, $chapterid, $context);
    }
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    $bookquiz = new stdClass();
    $bookquiz->lastchapterid = 0;
    $bookquiz->id = $attempt->id;
    $bookquiz->bookid = $game->bookid;
    if (!game_insert_record('game_bookquiz', $bookquiz)) {
        print_error('game_bookquiz_continue: error inserting in game_bookquiz');
    }
    return game_bookquiz_play($id, $game, $attempt, $bookquiz, 0, $context);
}
コード例 #8
0
function game_cross_continue($id, $game, $attempt, $cross, $g, $endofgame, $context)
{
    if ($endofgame) {
        if ($g == '') {
            game_updateattempts($game, $attempt, -1, true);
            $endofgame = false;
        }
    }
    if ($attempt != false and $cross != false) {
        return game_cross_play($id, $game, $attempt, $cross, $g, false, false, $endofgame, false, false, false, false, true, $context);
    }
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    game_cross_new($game, $attempt->id, $crossm);
    game_updateattempts($game, $attempt, 0, 0);
    return game_cross_play($id, $game, $attempt, $crossm, '', false, false, false, false, false, false, false, true, $context);
}
コード例 #9
0
function game_snakes_continue($id, $game, $attempt, $snakes)
{
    if ($attempt != false and $snakes != false) {
        return game_snakes_play($id, $game, $attempt, $snakes);
    }
    if ($attempt === false) {
        $attempt = game_addattempt($game);
    }
    $newrec->id = $attempt->id;
    $newrec->snakesdatabaseid = $game->param3;
    $newrec->position = 1;
    $newrec->queryid = 0;
    $newrec->dice = rand(1, 6);
    if (!game_insert_record('game_snakes', $newrec)) {
        error('game_snakes_continue: error inserting in game_snakes');
    }
    game_updateattempts($game, $attempt, 0, 0);
    return game_snakes_play($id, $game, $attempt, $newrec);
}
コード例 #10
0
/**
 * This files plays the game millionaire
 * 
 * @author  bdaloukas
 * @version $Id: play.php,v 1.16 2009/09/12 08:12:56 bdaloukas Exp $
 * @package game
 **/
function game_millionaire_continue($id, $game, $attempt, $millionaire)
{
    //User must select quiz or question as a source module
    if ($game->quizid == 0 and $game->questioncategoryid == 0) {
        error(get_string($game->sourcemodule == 'quiz' ? 'millionaire_must_select_quiz' : 'millionaire_must_select_questioncategory'));
    }
    if ($attempt != false and $millionaire != false) {
        //continue an existing game
        return game_millionaire_play($id, $game, $attempt, $millionaire);
    }
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    $newrec->id = $attempt->id;
    $newrec->queryid = 0;
    $newrec->level = 0;
    $newrec->state = 0;
    if (!game_insert_record('game_millionaire', $newrec)) {
        error('error inserting in game_millionaire');
    }
    game_millionaire_play($id, $game, $attempt, $newrec);
}
コード例 #11
0
function game_sudoku_continue($id, $game, $attempt, $sudoku, $endofgame, $context)
{
    global $CFG, $DB, $USER;
    if ($endofgame) {
        game_updateattempts($game, $attempt, -1, true);
        $endofgame = false;
    }
    if ($attempt != false and $sudoku != false) {
        return game_sudoku_play($id, $game, $attempt, $sudoku, false, false, $context);
    }
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    //new game
    srand((double) microtime() * 1000000);
    $recsudoku = getrandomsudoku();
    if ($recsudoku == false) {
        print_error('Empty sudoku database');
    }
    $newrec = new stdClass();
    $newrec->id = $attempt->id;
    $newrec->guess = '';
    $newrec->data = $recsudoku->data;
    $newrec->opened = $recsudoku->opened;
    $need = 81 - $recsudoku->opened;
    $closed = game_sudoku_getclosed($newrec->data);
    $n = min(count($closed), $need);
    //if the teacher set the maximum number of questions
    if ($game->param2 > 0) {
        if ($game->param2 < $n) {
            $n = $game->param2;
        }
    }
    $recs = game_questions_selectrandom($game, CONST_GAME_TRIES_REPETITION * $n);
    if ($recs === false) {
        mysql_execute("DELETE FROM {game_sudoku} WHERE id={$game->id}");
        print_error(get_string('no_questions', 'game'));
    }
    $closed = array_rand($closed, $n);
    $selected_recs = game_select_from_repetitions($game, $recs, $n);
    if (!game_insert_record('game_sudoku', $newrec)) {
        print_error('error inserting in game_sudoku');
    }
    $i = 0;
    $field = $game->sourcemodule == 'glossary' ? 'glossaryentryid' : 'questionid';
    foreach ($recs as $rec) {
        if ($game->sourcemodule == 'glossary') {
            $key = $rec->glossaryentryid;
        } else {
            $key = $rec->questionid;
        }
        if (!array_key_exists($key, $selected_recs)) {
            continue;
        }
        $query = new stdClass();
        $query->attemptid = $newrec->id;
        $query->gamekind = $game->gamekind;
        $query->gameid = $game->id;
        $query->userid = $USER->id;
        $query->col = $closed[$i++];
        $query->sourcemodule = $game->sourcemodule;
        $query->questionid = $rec->questionid;
        $query->glossaryentryid = $rec->glossaryentryid;
        $query->score = 0;
        if (($query->id = $DB->insert_record('game_queries', $query)) == 0) {
            print_error('error inserting in game_queries');
        }
        game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid);
    }
    game_updateattempts($game, $attempt, 0, 0);
    game_sudoku_play($id, $game, $attempt, $newrec, false, false, $context);
}
コード例 #12
0
function game_hiddenpicture_continue($id, $game, $attempt, $hiddenpicture, $context)
{
    global $DB, $USER;
    if ($attempt != false and $hiddenpicture != false) {
        //Continue a previous attempt
        return game_hiddenpicture_play($id, $game, $attempt, $hiddenpicture, false, $context);
    }
    if ($attempt == false) {
        //Start a new attempt
        $attempt = game_addattempt($game);
    }
    $cols = $game->param1;
    $rows = $game->param2;
    if ($cols == 0) {
        print_error(get_string('hiddenpicture_nocols', 'game'));
    }
    if ($rows == 0) {
        print_error(get_string('hiddenpicture_norows', 'game'));
    }
    //new attempt
    $n = $game->param1 * $game->param2;
    $recs = game_questions_selectrandom($game, CONST_GAME_TRIES_REPETITION * $n);
    $selected_recs = game_select_from_repetitions($game, $recs, $n);
    $newrec = game_hiddenpicture_selectglossaryentry($game, $attempt);
    if ($recs === false) {
        print_error(get_string('no_questions', 'game'));
    }
    $positions = array();
    $pos = 1;
    for ($col = 0; $col < $cols; $col++) {
        for ($row = 0; $row < $rows; $row++) {
            $positions[] = $pos++;
        }
    }
    $i = 0;
    $field = $game->sourcemodule == 'glossary' ? 'glossaryentryid' : 'questionid';
    foreach ($recs as $rec) {
        if ($game->sourcemodule == 'glossary') {
            $key = $rec->glossaryentryid;
        } else {
            $key = $rec->questionid;
        }
        if (!array_key_exists($key, $selected_recs)) {
            continue;
        }
        $query = new stdClass();
        $query->attemptid = $newrec->id;
        $query->gamekind = $game->gamekind;
        $query->gameid = $game->id;
        $query->userid = $USER->id;
        $pos = array_rand($positions);
        $query->col = $positions[$pos];
        unset($positions[$pos]);
        $query->sourcemodule = $game->sourcemodule;
        $query->questionid = $rec->questionid;
        $query->glossaryentryid = $rec->glossaryentryid;
        $query->score = 0;
        if (($query->id = $DB->insert_record('game_queries', $query)) == 0) {
            print_error('error inserting in game_queries');
        }
        game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid);
    }
    //The score is zero
    game_updateattempts($game, $attempt, 0, 0);
    game_hiddenpicture_play($id, $game, $attempt, $newrec, false, $context);
}
コード例 #13
0
function game_getattempt($game, &$detail, $autoadd = false)
{
    global $DB, $USER;
    $select = "gameid={$game->id} AND userid={$USER->id} and timefinish=0 ";
    if ($USER->id == 1) {
        $key = 'mod/game:instanceid' . $game->id;
        if (array_key_exists($key, $_SESSION)) {
            $select .= ' AND id="' . $_SESSION[$key] . '"';
        } else {
            $select .= ' AND id=-1';
        }
    }
    if ($recs = $DB->get_records_select('game_attempts', $select)) {
        foreach ($recs as $attempt) {
            if ($USER->id == 1) {
                $_SESSION[$key] = $attempt->id;
            }
            $detail = $DB->get_record('game_' . $game->gamekind, array('id' => $attempt->id));
            return $attempt;
        }
    }
    if ($autoadd) {
        game_addattempt($game);
        return game_getattempt($game, $detail, false);
    }
    return false;
}
コード例 #14
0
function game_sudoku_continue($id, $game, $attempt, $sudoku, $endofgame = '')
{
    global $CFG, $USER;
    if ($endofgame) {
        game_updateattempts($game, $attempt, -1, true);
        $endofgame = false;
    }
    if ($attempt != false and $sudoku != false) {
        return game_sudoku_play($id, $game, $attempt, $sudoku);
    }
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    //new game
    srand((double) microtime() * 1000000);
    $recsudoku = getrandomsudoku();
    if ($recsudoku == false) {
        $link = "<a href=\"{$CFG->wwwroot}/mod/game/db/importsudoku.php\">import</a>";
        echo get_string('sudoku_emptydatabase', 'game', $link);
        die;
    }
    $newrec->id = $attempt->id;
    $newrec->guess = '';
    $newrec->data = $recsudoku->data;
    $newrec->opened = $recsudoku->opened;
    $need = 81 - $recsudoku->opened;
    $recs = game_questions_selectrandom($game, $need);
    if ($recs === false) {
        error(get_string('sudoku_no_questions', 'game'));
    }
    $closed = game_sudoku_getclosed($newrec->data);
    $n = min(count($closed), count($recs));
    //if the teacher set the maximum number of questions
    if ($game->param2 > 0) {
        if ($game->param2 < $n) {
            $n = $game->param2;
        }
    }
    $closed = array_rand($closed, $n);
    if (!game_insert_record('game_sudoku', $newrec)) {
        error('error inserting in game_sudoku');
    }
    $i = 0;
    foreach ($recs as $rec) {
        if ($i >= $n) {
            break;
        }
        unset($query);
        $query->attemptid = $newrec->id;
        $query->gamekind = $game->gamekind;
        $query->gameid = $game->id;
        $query->userid = $USER->id;
        $query->col = $closed[$i++];
        $query->sourcemodule = $game->sourcemodule;
        $query->questionid = $rec->questionid;
        $query->glossaryentryid = $rec->glossaryentryid;
        $query->score = 0;
        if (($query->id = insert_record("game_queries", $query)) == 0) {
            error('error inserting in game_queries');
        }
    }
    game_updateattempts($game, $attempt, 0, 0);
    game_sudoku_play($id, $game, $attempt, $newrec);
}
コード例 #15
0
function game_hangman_continue($id, $game, $attempt, $hangman, $newletter, $action, $context)
{
    global $DB, $USER;
    if ($attempt != false and $hangman != false) {
        if ($action == 'nextword' and $hangman->finishedword != 0) {
            // Finish with one word and continue to another.
            if (!$DB->set_field('game_hangman', 'finishedword', 0, array('id' => $hangman->id))) {
                error("game_hangman_continue: Can't update game_hangman");
            }
        } else {
            return game_hangman_play($id, $game, $attempt, $hangman, false, false, $context);
        }
    }
    $updatehangman = ($attempt != false and $hangman != false);
    // New game.
    srand((double) microtime() * 1000003);
    // I try 10 times to find a new question.
    $found = false;
    $minnum = 0;
    $unchanged = 0;
    for ($i = 1; $i <= 10; $i++) {
        $rec = game_question_shortanswer($game, $game->param7, false);
        if ($rec === false) {
            continue;
        }
        $answer = game_upper($rec->answertext, $game->language);
        if ($game->language == '') {
            $game->language = game_detectlanguage($answer);
            $answer = game_upper($rec->answertext, $game->language);
        }
        $answer2 = $answer;
        if ($game->param7) {
            // Have to delete space.
            $answer2 = str_replace(' ', '', $answer2);
        }
        if ($game->param8) {
            // Have to delete -.
            $answer2 = str_replace('-', '', $answer2);
            $answer2 = str_replace('Ç', '', $answer2);
            $answer2 = str_replace('Ã', '', $answer2);
            $answer2 = str_replace('Õ', '', $answer2);
        }
        $allletters = game_getallletters($answer2, $game->language, $game->userlanguage);
        if ($allletters == '') {
            continue;
        }
        if ($game->param7) {
            $allletters .= '_';
        }
        if ($game->param8) {
            $allletters .= '-';
            $allletters .= '';
            $allletters .= '';
            $allletters .= '';
        }
        if ($game->param7 == false) {
            // I don't allow spaces.
            if (strpos($answer, " ")) {
                continue;
            }
        }
        $copy = false;
        $select2 = 'gameid=? AND userid=? AND questionid=? AND glossaryentryid=?';
        if (($rec2 = $DB->get_record_select('game_repetitions', $select2, array($game->id, $USER->id, $rec->questionid, $rec->glossaryentryid), 'id,repetitions AS r')) != false) {
            if ($rec2->r < $minnum or $minnum == 0) {
                $minnum = $rec2->r;
                $copy = true;
            }
        } else {
            $minnum = 0;
            $copy = true;
        }
        if ($copy) {
            $found = true;
            $min = new stdClass();
            $min->questionid = $rec->questionid;
            $min->glossaryentryid = $rec->glossaryentryid;
            $min->attachment = $rec->attachment;
            $min->questiontext = $rec->questiontext;
            $min->answerid = $rec->answerid;
            $min->answer = $answer;
            $min->language = $game->language;
            $min->allletters = $allletters;
            if ($minnum == 0) {
                break;
                // We found an unused word.
            }
        } else {
            $unchanged++;
        }
        if ($unchanged > 2) {
            if ($found) {
                break;
            }
        }
    }
    if ($found == false) {
        print_error(get_string('no_words', 'game'));
    }
    // Found one word for hangman.
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    if (!$DB->set_field('game_attempts', 'language', $min->language, array('id' => $attempt->id))) {
        print_error("game_hangman_continue: Can't set language");
    }
    $query = new stdClass();
    $query->attemptid = $attempt->id;
    $query->gameid = $game->id;
    $query->userid = $USER->id;
    $query->sourcemodule = $game->sourcemodule;
    $query->questionid = $min->questionid;
    $query->glossaryentryid = $min->glossaryentryid;
    $query->attachment = $min->attachment;
    $query->questiontext = addslashes($min->questiontext);
    $query->score = 0;
    $query->timelastattempt = time();
    $query->answertext = $min->answer;
    $query->answerid = $min->answerid;
    if (!($query->id = $DB->insert_record('game_queries', $query))) {
        print_error("game_hangman_continue: Can't insert to table game_queries");
    }
    $newrec = new stdClass();
    $newrec->id = $attempt->id;
    $newrec->queryid = $query->id;
    if ($updatehangman == false) {
        $newrec->maxtries = $game->param4;
        if ($newrec->maxtries == 0) {
            $newrec->maxtries = 1;
        }
        $newrec->finishedword = 0;
        $newrec->corrects = 0;
    }
    $newrec->allletters = $min->allletters;
    $letters = '';
    if ($game->param1) {
        $letters .= game_substr($min->answer, 0, 1);
    }
    if ($game->param2) {
        $letters .= game_substr($min->answer, -1, 1);
    }
    $newrec->letters = $letters;
    if ($updatehangman == false) {
        if (!game_insert_record('game_hangman', $newrec)) {
            print_error('game_hangman_continue: error inserting in game_hangman');
        }
    } else {
        if (!$DB->update_record('game_hangman', $newrec)) {
            print_error('game_hangman_continue: error updating in game_hangman');
        }
        $newrec = $DB->get_record('game_hangman', array('id' => $newrec->id));
    }
    game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid);
    game_hangman_play($id, $game, $attempt, $newrec, false, false, $context);
}
コード例 #16
0
function game_hangman_continue($id, $game, $attempt, $hangman, $newletter, $action)
{
    global $USER;
    if ($attempt != false and $hangman != false) {
        if ($action == 'nextword' and $hangman->finishedword != 0) {
            //finish with one word and continue to another
            if (!set_field('game_hangman', 'finishedword', 0, 'id', $hangman->id)) {
                error("game_hangman_continue: Can't update game_hangman");
            }
        } else {
            return game_hangman_play($id, $game, $attempt, $hangman);
        }
    }
    $updatehangman = ($attempt != false and $hangman != false);
    $textlib = textlib_get_instance();
    //new game
    srand((double) microtime() * 1000000);
    //I try 10 times to find a new question
    $found_words = 0;
    //try to find CONST_GAME_TRIES_REPETITION words
    $min_num = 0;
    //number of repetitions
    $min_id = 0;
    //id with min_num repetitions
    for ($i = 1; $i <= 10; $i++) {
        $rec = game_question_shortanswer($game, $game->param7, false);
        if ($rec === false) {
            continue;
        }
        $answer = game_upper($rec->answertext, $game->language);
        $answer2 = $answer;
        if ($game->param7) {
            //Have to delete space
            $answer2 = str_replace(' ', '', $answer2);
        }
        if ($game->param8) {
            //Have to delete -
            $answer2 = str_replace('-', '', $answer2);
        }
        if ($game->language == '') {
            $game->language = game_detectlanguage($answer2);
        }
        $allletters = game_getallletters($answer2, $game->language);
        if ($allletters == '') {
            continue;
        }
        if ($game->param7) {
            $allletters .= '_';
        }
        if ($game->param8) {
            $allletters .= '-';
        }
        if ($game->param7 == false) {
            //I don't allow spaces
            if (strpos($answer, " ")) {
                continue;
            }
        }
        $copy = false;
        $select2 = "gameid={$game->id} AND userid='{$USER->id}' AND questionid='{$rec->questionid}' AND glossaryentryid='{$rec->glossaryentryid}'";
        if (($rec2 = get_record_select('game_repetitions', $select2, 'id,repetitions r')) != false) {
            if ($rec2->r < $min_num or $min_num == 0) {
                $min_num = $rec2->r;
                $copy = true;
            }
        } else {
            $min_num = 0;
            $copy = true;
        }
        if ($copy) {
            $min_id = $rec->id;
            $min->questionid = $rec->questionid;
            $min->glossaryentryid = $rec->glossaryentryid;
            $min->attachment = $rec->attachment;
            $min->questiontext = $rec->questiontext;
            $min->answerid = $rec->answerid;
            if ($min_num == 0) {
                break;
            }
        }
        //found a correct word
        if ($found_words >= CONST_GAME_TRIES_REPETITION) {
            break;
        }
    }
    if ($min_id == 0) {
        error(get_string('hangman_nowords', 'game'));
    }
    //Found one word for hangman
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    $_GET['newletter'] = '';
    $query->attemptid = $attempt->id;
    $query->gameid = $game->id;
    $query->userid = $USER->id;
    $query->sourcemodule = $game->sourcemodule;
    $query->questionid = $min->questionid;
    $query->glossaryentryid = $min->glossaryentryid;
    $query->attachment = $min->attachment;
    $query->questiontext = addslashes($min->questiontext);
    $query->score = 0;
    $query->timelastattempt = time();
    $query->answertext = $answer;
    $query->answerid = $min->answerid;
    if (!($query->id = insert_record('game_queries', $query))) {
        print_object($query);
        error("game_hangman_continue: Can't insert to table game_queries");
    }
    $newrec->id = $attempt->id;
    $newrec->queryid = $query->id;
    if ($updatehangman == false) {
        $newrec->maxtries = $game->param4;
        if ($newrec->maxtries == 0) {
            $newrec->maxtries = 1;
        }
        $newrec->finishedword = 0;
        $newrec->corrects = 0;
    }
    $newrec->allletters = $allletters;
    $letters = '';
    if ($game->param1) {
        $letters .= $textlib->substr($answer, 0, 1);
    }
    if ($game->param2) {
        $letters .= $textlib->substr($answer, -1, 1);
    }
    $newrec->letters = $letters;
    if ($updatehangman == false) {
        if (!game_insert_record('game_hangman', $newrec)) {
            error('game_hangman_continue: error inserting in game_hangman');
        }
    } else {
        if (!update_record('game_hangman', $newrec)) {
            error('game_hangman_continue: error updating in game_hangman');
        }
        $newrec = get_record_select('game_hangman', "id={$newrec->id}");
    }
    game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid);
    game_hangman_play($id, $game, $attempt, $newrec);
}