function load($g, &$done, &$html, $game, $attempt, $crossrec, $onlyshow, $showsolution, &$finishattempt, $showhtmlsolutions, &$language, $showstudentguess, $context)
 {
     global $DB;
     $info = '';
     $correctLetters = 0;
     $allLetters = 0;
     $wrongLetters = 0;
     $html = '';
     $done = false;
     if ($g == "") {
         $game_questions = false;
     }
     $this->m_mincol = $this->m_minrow = 0;
     $this->m_maxcol = $crossrec->cols;
     $this->m_maxrow = $crossrec->rows;
     if ($g == "") {
         $g = str_repeat(' ', $this->m_maxcol * $this->m_maxrow);
     }
     $load = false;
     $puzzle = str_repeat('.', $this->m_maxrow * $this->m_maxcol);
     if ($recs = $DB->get_records('game_queries', array('attemptid' => $crossrec->id))) {
         $a = array();
         foreach ($recs as $rec) {
             if ($rec->horizontal) {
                 $key = sprintf('h%10d %10d', $rec->row, $rec->col);
             } else {
                 $key = sprintf('v%10d %10d', $rec->col, $rec->row);
             }
             $a[$key] = $rec;
         }
         ksort($a);
         $b = array();
         $correctletters = $wrongletters = $restletters = 0;
         foreach ($a as $rec) {
             $this->updatecrossquestions($rec, $g, $pos, $correctletters, $wrongletters, $restletters, $game, $attempt, $crossrec);
             $b[] = $rec;
             if ($rec->col != 0 and $rec->row != 0) {
                 $load = true;
             }
             if ($language == '') {
                 $language = game_detectlanguage($rec->answertext);
             }
         }
         $info = $this->game_cross_computecheck($correctletters, $wrongletters, $restletters, $game, $attempt, $done, $onlyshow, $showsolution, $finishattempt);
         $html = $this->showhtml_base($crossrec, $b, $showsolution, $showhtmlsolutions, $showstudentguess, $context, $game);
     }
     if ($load == false) {
         $finishattempt = true;
     }
     return $info;
 }
$export_attachment = $html->type == 'hangmanp';
$map = game_exmportjavame_getanswers($game, $export_attachment);
if ($map == false) {
    error('No Questions');
}
$questions = '';
$words = '';
$lang = '';
$allletters = '';
$images = '';
foreach ($map as $line) {
    $answer = game_upper($line->answer);
    if ($lang == '') {
        $lang = $game->language;
        if ($lang == '') {
            $lang = game_detectlanguage($answer);
        }
        if ($lang == '') {
            $lang = current_language();
        }
        $allletters = game_getallletters($answer, $lang);
    }
    if (game_getallletters($answer, $lang) != $allletters) {
        continue;
    }
    if ($html->type == 'hangmanp') {
        $file = $line->attachment;
        $pos = strrpos($file, '.');
        if ($pos == false) {
            continue;
        }
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 load($crossm, &$mask, &$corrects, &$language)
 {
     global $DB;
     $textlib = textlib_get_instance();
     $questions = array();
     $corrects = array();
     $mask = str_repeat('0', $crossm->cols * $crossm->rows);
     if ($recs = $DB->get_records('game_queries', array('attemptid' => $crossm->id))) {
         foreach ($recs as $rec) {
             if ($rec->questiontext == '') {
                 $rec->questiontext = ' ';
             }
             $key = $this->gethash($rec->questiontext) . '-' . $rec->answertext . '-' . $rec->id;
             $questions[$key] = $rec;
             $word = $rec->answertext;
             $pos = $crossm->cols * ($rec->row - 1) + ($rec->col - 1);
             $len = $textlib->strlen($word);
             $found = $rec->answertext == $rec->studentanswer;
             for ($i = 0; $i < $len; $i++) {
                 $c = $found ? '1' : '2';
                 if ($textlib->substr($mask, $pos, 1) != '1') {
                     game_setchar($mask, $pos, $c);
                 }
                 $pos += $rec->horizontal ? 1 : $crossm->cols;
             }
             if ($found) {
                 $corrects[$rec->id] = 1;
             }
             if ($language == '') {
                 $language = game_detectlanguage($rec->answertext);
             }
         }
         ksort($questions);
     }
     return $questions;
 }
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);
}