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); }
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); }
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); }
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; }
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_snakes_computenextquestion($game, &$snakes, &$query) { global $USER; if (($recs = game_questions_selectrandom($game, 1)) == false) { return false; } foreach ($recs as $rec) { $query->attemptid = $snakes->id; $query->gameid = $game->id; $query->userid = $USER->id; $query->sourcemodule = $game->sourcemodule; $query->questionid = $rec->questionid; $query->glossaryentryid = $rec->glossaryentryid; $query->score = 0; $query->timelastattempt = time(); if (!($query->id = insert_record('game_queries', $query))) { error("Can't insert to table game_queries"); } $snakes->queryid = $query->id; $updrec->id = $snakes->id; $updrec->queryid = $query->id; $updrec->dice = rand(1, 6); if (!update_record('game_snakes', $updrec)) { error('game_questions_selectrandom: error updating in game_snakes'); } return true; } return false; }