$_GET['id'] = $update;
require_once "header.php";
if (!isteacherinanycourse($USER->id)) {
    error(get_string('only_teachers', 'game'));
}
$gamekind = $_GET['gamekind'];
$id = $update;
$attemptid = (int) $_GET['attemptid'];
$attempt = get_record_select('game_attempts', "id={$attemptid}");
$game = get_record_select('game', "id={$attempt->gameid}");
$detail = get_record_select('game_' . $gamekind, "id={$attemptid}");
if (array_key_exists('solution', $_GET)) {
    $solution = $_GET['solution'];
} else {
    $solution = 0;
}
switch ($gamekind) {
    case 'cross':
        game_cross_play($update, $game, $attempt, $detail, '', true, $solution, false, false, false, false, true);
        break;
    case 'sudoku':
        game_sudoku_play($update, $game, $attempt, $detail, true, $solution);
        break;
    case 'hangman':
        game_hangman_play($update, $game, $attempt, $detail, true, $solution);
        break;
    case 'cryptex':
        $crossm = get_record_select('game_cross', "id={$attemptid}");
        game_cryptex_play($update, $game, $attempt, $detail, $crossm, false, true, $solution);
        break;
}
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);
}
Exemple #3
0
$attemptid = required_param('attemptid', PARAM_INT);
$attempt = $DB->get_record('game_attempts', array('id' => $attemptid));
$game = $DB->get_record('game', array('id' => $attempt->gameid));
$detail = $DB->get_record('game_' . $gamekind, array('id' => $attemptid));
$solution = $action == 'solution';
$PAGE->navbar->add(get_string('preview', 'game'));
switch ($gamekind) {
    case 'cross':
        $g = '';
        $onlyshow = true;
        $endofgame = false;
        $print = false;
        $checkbutton = false;
        $showhtmlsolutions = false;
        $showhtmlprintbutton = true;
        $showstudentguess = false;
        game_cross_play($update, $game, $attempt, $detail, $g, $onlyshow, $solution, $endofgame, $print, $checkbutton, $showhtmlsolutions, $showhtmlprintbutton, $showstudentguess, $context);
        break;
    case 'sudoku':
        game_sudoku_play($update, $game, $attempt, $detail, true, $solution, $context);
        break;
    case 'hangman':
        $preview = $action == 'preview';
        game_hangman_play($update, $game, $attempt, $detail, $preview, $solution, $context);
        break;
    case 'cryptex':
        $crossm = $DB->get_record('game_cross', array('id' => $attemptid));
        game_cryptex_play($update, $game, $attempt, $detail, $crossm, false, true, $solution, $context);
        break;
}
echo $OUTPUT->footer();
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);
}