Beispiel #1
0
/**
 * 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);
}
 function save($game, &$crossm, $crossd, $id, $letters)
 {
     global $USER;
     CrossDB::delete_cross($id);
     if (CrossDB::save($game, $crossm, $crossd, $id) == false) {
         return false;
     }
     $crossm->id = $id;
     $newrec->id = $id;
     $newrec->letters = $letters;
     if (!($cryptexid = game_insert_record("game_cryptex", $newrec))) {
         print_error('Insert page: new page game_cryptex not inserted');
     }
     return $newrec;
 }
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);
}
Beispiel #4
0
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);
}
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);
}
 function save($game, &$crossm, $crossd, $id)
 {
     global $USER;
     $crossm->id = $id;
     $crossm->sourcemodule = $game->sourcemodule;
     if (!game_insert_record("game_cross", $crossm)) {
         error("Insert page: new page game_cross not inserted");
     }
     foreach ($crossd as $rec) {
         $rec->attemptid = $id;
         $rec->questiontext = addslashes($rec->questiontext);
         $rec->gamekind = $game->gamekind;
         $rec->gameid = $game->id;
         $rec->userid = $USER->id;
         $rec->sourcemodule = $game->sourcemodule;
         if (!insert_record("game_queries", $rec)) {
             error("Insert page: new page game_queries not inserted");
         }
     }
     return true;
 }
/**
 * 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);
}
 function save($game, &$crossm, $crossd, $id)
 {
     global $DB, $USER;
     $crossm->id = $id;
     $crossm->sourcemodule = $game->sourcemodule;
     if (!game_insert_record("game_cross", $crossm)) {
         print_error('Insert page: new page game_cross not inserted');
     }
     foreach ($crossd as $rec) {
         $rec->attemptid = $id;
         $rec->questiontext = addslashes($rec->questiontext);
         $rec->gamekind = $game->gamekind;
         $rec->gameid = $game->id;
         $rec->userid = $USER->id;
         $rec->sourcemodule = $game->sourcemodule;
         if (!$DB->insert_record('game_queries', $rec)) {
             print_error('Insert page: new page game_queries not inserted');
         }
         game_update_repetitions($game->id, $USER->id, $rec->questionid, $rec->glossaryentryid);
     }
     return true;
 }
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);
}
Beispiel #10
0
function game_hiddenpicture_selectglossaryentry($game, $attempt)
{
    global $CFG, $DB, $USER;
    srand((double) microtime() * 1000000);
    if ($game->glossaryid2 == 0) {
        print_error(get_string('must_select_glossary', 'game'));
    }
    $select = "ge.glossaryid={$game->glossaryid2}";
    $table = '{glossary_entries} ge';
    if ($game->glossarycategoryid2) {
        $table .= ",{glossary_entries_categories} gec";
        $select .= " AND gec.entryid = ge.id AND gec.categoryid = {$game->glossarycategoryid2}";
    }
    if ($game->param7 == 0) {
        //Allow spaces
        $select .= " AND concept NOT LIKE '% %'";
    }
    $sql = "SELECT ge.id,attachment FROM {$table} WHERE {$select}";
    if (($recs = $DB->get_records_sql($sql)) == false) {
        $a->name = "'" . $DB->get_field('glossary', 'name', array('id' => $game->glossaryid2)) . "'";
        print_error(get_string('hiddenpicture_nomainquestion', 'game', $a));
        return false;
    }
    $ids = array();
    $keys = array();
    $fs = get_file_storage();
    $cmg = get_coursemodule_from_instance('glossary', $game->glossaryid2, $game->course);
    $context = game_get_context_module_instance($cmg->id);
    foreach ($recs as $rec) {
        $files = $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $rec->id, "timemodified", false);
        if ($files) {
            foreach ($files as $key => $file) {
                $s = strtoupper($file->get_filename());
                $s = substr($s, -4);
                if ($s == '.GIF' or $s == '.JPG' or $s == '.PNG') {
                    $ids[] = $rec->id;
                    $keys[] = $file->get_pathnamehash();
                }
            }
        }
    }
    if (count($ids) == 0) {
        $a->name = "'" . $DB->get_field('glossary', 'name', array('id' => $game->glossaryid2)) . "'";
        print_error(get_string('hiddenpicture_nomainquestion', 'game', $a));
        return false;
    }
    //Have to select randomly one glossaryentry
    $poss = array();
    for ($i = 0; $i < count($ids); $i++) {
        $poss[] = $i;
    }
    shuffle($poss);
    $min_num = 0;
    $attachement = '';
    for ($i = 0; $i < count($ids); $i++) {
        $pos = $poss[$i];
        $tempid = $ids[$pos];
        $a = array('gameid' => $game->id, 'userid' => $USER->id, 'questionid' => 0, 'glossaryentryid' => $tempid);
        if (($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions r')) != false) {
            if ($rec2->r < $min_num or $min_num == 0) {
                $min_num = $rec2->r;
                $glossaryentryid = $tempid;
                $attachement = $keys[$pos];
            }
        } else {
            $glossaryentryid = $tempid;
            $attachement = $keys[$pos];
            break;
        }
    }
    $sql = 'SELECT id, concept as answertext, definition as questiontext, id as glossaryentryid, 0 as questionid, glossaryid, attachment' . ' FROM {glossary_entries} WHERE id = ' . $glossaryentryid;
    if (($rec = $DB->get_record_sql($sql)) == false) {
        return false;
    }
    $query = new stdClass();
    $query->attemptid = $attempt->id;
    $query->gamekind = $game->gamekind;
    $query->gameid = $game->id;
    $query->userid = $USER->id;
    $query->col = 0;
    $query->sourcemodule = 'glossary';
    $query->questionid = 0;
    $query->glossaryentryid = $rec->glossaryentryid;
    $query->attachment = $attachement;
    $query->questiontext = $rec->questiontext;
    $query->answertext = $rec->answertext;
    $query->score = 0;
    if (($query->id = $DB->insert_record('game_queries', $query)) == 0) {
        print_error('Error inserting in game_queries');
    }
    $newrec = new stdClass();
    $newrec->id = $attempt->id;
    $newrec->correct = 0;
    if (!game_insert_record('game_hiddenpicture', $newrec)) {
        print_error('Error inserting in game_hiddenpicture');
    }
    game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid);
    return $newrec;
}
        unset($html);
        $html->id = $game->id;
        $html->checkbutton = 1;
        $html->printbutton = 1;
        game_insert_record('game_export_html', $html);
        $html = $DB->get_record('game_export_html', array('id' => $game->id));
    }
    $html->type = 0;
    $mform = new mod_game_exporthtml_form(null, array('id' => $id, 'html' => $html));
} else {
    $javame = $DB->get_record('game_export_javame', array('id' => $game->id));
    if ($javame == false) {
        unset($javame);
        $javame->id = $game->id;
        $javame->filename = $game->gamekind;
        game_insert_record('game_export_javame', $javame);
        $javame = $DB->get_record('game_export_javame', array('id' => $game->id));
    }
    $mform = new mod_game_exportjavame_form(null, array('id' => $id, 'javame' => $javame));
}
if ($mform->is_cancelled()) {
    ob_end_flush();
    if ($id) {
        redirect("view.php?id={$cm->id}&amp;mode=entry&amp;hook={$id}");
    } else {
        redirect("view.php?id={$cm->id}");
    }
} else {
    if ($entry = $mform->get_data()) {
        $mform->export();
    } else {
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);
}
function game_hiddenpicture_selectglossaryentry($game, $attempt)
{
    global $CFG, $USER;
    srand((double) microtime() * 1000000);
    if ($game->glossaryid2 == 0) {
        error(get_string('must_select_glossary', 'game'));
    }
    $select = "ge.glossaryid={$game->glossaryid2}";
    $table = 'glossary_entries ge';
    if ($game->glossarycategoryid2) {
        $table .= ",{$CFG->prefix}glossary_entries_categories gec";
        $select .= " AND gec.entryid = ge.id AND gec.categoryid = {$game->glossarycategoryid2}";
    }
    if ($game->param7 == 0) {
        //Allow spaces
        $select .= " AND concept NOT LIKE '% %'";
    }
    $select .= " AND attachment LIKE '%.%'";
    if (($recs = get_records_select($table, $select, '', 'ge.id,attachment')) == false) {
        $a->name = "'" . get_field_select('glossary', 'name', "id={$game->glossaryid2}") . "'";
        error(get_string('hiddenpicture_nomainquestion', 'game', $a));
        return false;
    }
    $ids = array();
    foreach ($recs as $rec) {
        $s = strtoupper($rec->attachment);
        $s = substr($s, -4);
        if ($s == '.GIF' or $s == '.JPG' or $s == '.PNG') {
            $ids[] = $rec->id;
        }
    }
    if (count($ids) == 0) {
        $a->name = "'" . get_field_select('glossary', 'name', "id={$game->glossaryid2}") . "'";
        error(get_string('hiddenpicture_nomainquestion', 'game', $a));
        return false;
    }
    $sel = mt_rand(0, count($ids) - 1);
    $id = $ids[$sel];
    $sql = 'SELECT id, concept as answertext, definition as questiontext, id as glossaryentryid, 0 as questionid, glossaryid, attachment' . " FROM {$CFG->prefix}glossary_entries WHERE id = {$id}";
    if (($rec = get_record_sql($sql)) == false) {
        return false;
    }
    $query->attemptid = $attempt->id;
    $query->gamekind = $game->gamekind;
    $query->gameid = $game->id;
    $query->userid = $USER->id;
    $query->col = 0;
    $query->sourcemodule = 'glossary';
    $query->questionid = 0;
    $query->glossaryentryid = $rec->glossaryentryid;
    $query->attachment = str_replace("\\", '/', $CFG->dataroot) . "/{$game->course}/moddata/glossary/{$game->glossaryid2}/{$query->glossaryentryid}/{$rec->attachment}";
    $query->questiontext = $rec->questiontext;
    $query->answertext = $rec->answertext;
    $query->score = 0;
    if (($query->id = insert_record("game_queries", $query)) == 0) {
        error('error inserting in game_queries');
    }
    $newrec->id = $attempt->id;
    if (!game_insert_record('game_hiddenpicture', $newrec)) {
        error('error inserting in game_hiddenpicture');
    }
    return $newrec;
}
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);
}
function game_restore_snakes($newattemptid, $info, $restore)
{
    global $CFG;
    $status = true;
    //Get the game_snakes array
    if (array_key_exists('GAME_SNAKES', $info['#'])) {
        $info = $info['#']['GAME_SNAKES'][0];
    } else {
        return $status;
    }
    //Now, build the game_snakes record structure
    $game_snakes = new stdClass();
    $game_snakes->id = $newattemptid;
    $fields = array('snakesdatabaseid', 'queryid', 'position', 'dice');
    game_restore_record($info, $game_snakes, $fields);
    //We have to recode the some
    game_recode($restore->backup_unique_code, $game_snakes, 'queryid', 'game_queries');
    game_recode($restore->backup_unique_code, $game_snakes, 'snakesdatabaseid', 'game_snakes_database');
    //The structure is equal to the db, so insert the game_snakes
    if (game_insert_record("game_snakes", $game_snakes) == false) {
        $status = false;
    }
    return $status;
}
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);
}