Exemplo n.º 1
0
 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;
 }
Exemplo n.º 2
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);
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
0
function game_question_selectrandom($game, $table, $select, $id_fields = 'id', $use_repetitions = true)
{
    global $DB, $USER;
    $count = $DB->get_field_sql("SELECT COUNT(*) FROM {$table} WHERE {$select}");
    $min_num = 0;
    $min_id = 0;
    for ($i = 1; $i <= CONST_GAME_TRIES_REPETITION; $i++) {
        $sel = mt_rand(0, $count - 1);
        $sql = "SELECT {$id_fields},{$id_fields} FROM " . $table . " WHERE {$select}";
        if (($recs = $DB->get_records_sql($sql, null, $sel, 1)) == false) {
            return false;
        }
        $id = 0;
        foreach ($recs as $rec) {
            $id = $rec->id;
        }
        if ($min_id == 0) {
            $min_id = $id;
        }
        if ($use_repetitions == false) {
            return $id;
        }
        if ($count == 1) {
            break;
        }
        $questionid = $glossaryentryid = 0;
        if ($game->sourcemodule == 'glossary') {
            $glossaryentryid = $id;
        } else {
            $questionid = $id;
        }
        $a = array('gameid' => $game->id, 'userid' => $USER->id, 'questionid' => $questionid, 'glossaryentryid' => $glossaryentryid);
        if (($rec = $DB->get_record('game_repetitions', $a, 'id,repetitions r')) != false) {
            if ($rec->r < $min_num or $min_num == 0) {
                $min_num = $rec->r;
                $min_id = $id;
            }
        } else {
            $min_id = $questionid;
            break;
        }
    }
    if ($game->sourcemodule == 'glossary') {
        game_update_repetitions($game->id, $USER->id, 0, $min_id);
    } else {
        game_update_repetitions($game->id, $USER->id, $min_id, 0);
    }
    return $min_id;
}
Exemplo n.º 5
0
function game_snakes_computenextquestion($game, &$snakes, &$query)
{
    global $DB, $USER;
    //Retrieves CONST_GAME_TRIES_REPETITION words and select the one which is used fewer times
    if (($recs = game_questions_selectrandom($game, 1, CONST_GAME_TRIES_REPETITION)) == false) {
        return false;
    }
    $glossaryid = 0;
    $questionid = 0;
    $min_num = 0;
    $query = new stdClass();
    foreach ($recs as $rec) {
        $a = array('gameid' => $game->id, 'userid' => $USER->id, 'questionid' => $rec->questionid, 'glossaryentryid' => $rec->glossaryentryid);
        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;
                $query->glossaryentryid = $rec->glossaryentryid;
                $query->questionid = $rec->questionid;
            }
        } else {
            $query->glossaryentryid = $rec->glossaryentryid;
            $query->questionid = $rec->questionid;
            break;
        }
    }
    if ($query->glossaryentryid == 0 and $query->questionid == 0) {
        return false;
    }
    $query->attemptid = $snakes->id;
    $query->gameid = $game->id;
    $query->userid = $USER->id;
    $query->sourcemodule = $game->sourcemodule;
    $query->score = 0;
    $query->timelastattempt = time();
    if (!($query->id = $DB->insert_record('game_queries', $query))) {
        print_error("Can't insert to table game_queries");
    }
    $snakes->queryid = $query->id;
    $updrec = new stdClass();
    $updrec->id = $snakes->id;
    $updrec->queryid = $query->id;
    $updrec->dice = $snakes->dice = rand(1, 6);
    if (!$DB->update_record('game_snakes', $updrec)) {
        print_error('game_questions_selectrandom: error updating in game_snakes');
    }
    game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid);
    return true;
}
Exemplo n.º 6
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);
}
function game_question_selectrandom($game, $table, $select, $id_fields = "id", $use_repetitions = true)
{
    global $CFG, $USER;
    $sql = "SELECT COUNT(*) AS c FROM {$CFG->prefix}{$table} WHERE {$select}";
    if (($rec = get_record_sql($sql)) == false) {
        return false;
    }
    $count = $rec->c;
    $min_num = 0;
    $min_id = 0;
    for ($i = 1; $i <= CONST_GAME_TRIES_REPETITION; $i++) {
        $sel = mt_rand(0, $count - 1);
        $sql = "SELECT {$id_fields},{$id_fields} FROM {$CFG->prefix}{$table} WHERE {$select}";
        if (($recs = get_records_sql($sql, $sel, 1)) == false) {
            return false;
        }
        $id = 0;
        foreach ($recs as $rec) {
            $id = $rec->id;
        }
        if ($min_id == 0) {
            $min_id = $id;
        }
        if ($use_repetitions == false) {
            return $id;
        }
        if ($count == 1) {
            break;
        }
        $questionid = $glossaryentryid = 0;
        if ($game->sourcemodule == 'glossary') {
            $glossaryentryid = $id;
        } else {
            $questionid = $id;
        }
        $select2 = "gameid={$game->id} AND userid='{$USER->id}' AND questionid='{$questionid}' AND glossaryentryid='{$glossaryentryid}'";
        if (($rec = get_record_select('game_repetitions', $select2, 'id,repetitions r')) != false) {
            if ($rec->r < $min_num or $min_num == 0) {
                $min_num = $rec->r;
                $min_id = $id;
            }
        } else {
            $min_id = $questionid;
            break;
        }
    }
    if ($game->sourcemodule == 'glossary') {
        game_update_repetitions($game->id, $USER->id, 0, $min_id);
    } else {
        game_update_repetitions($game->id, $USER->id, $min_id, 0);
    }
    return $min_id;
}
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);
}