function game_cryptex_check($id, $game, $attempt, $cryptexrec, $q, $answer, $context) { global $DB; if ($attempt === false) { game_cryptex_continue($id, $game, $attempt, $cryptexrec, false); return; } $crossm = $DB->get_record_select('game_cross', "id={$attempt->id}"); $query = $DB->get_record_select('game_queries', "id={$q}"); $answer1 = trim(game_upper($query->answertext)); $answer2 = trim(game_upper($answer)); $len1 = game_strlen($answer1); $len2 = game_strlen($answer2); $equal = $len1 == $len2; if ($equal) { for ($i = 0; $i < $len1; $i++) { if (game_substr($answer1, $i, 1) != game_substr($answer2, $i, 1)) { $equal = true; break; } } } if ($equal == false) { game_update_queries($game, $attempt, $query, 0, $answer2, true); game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, true, false, false, $context); return; } game_update_queries($game, $attempt, $query, 1, $answer2); $onlyshow = false; $showsolution = false; game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, true, $onlyshow, $showsolution, $context); }
function game_cryptex_check($id, $game, $attempt, $cryptexrec, $q, $answer) { if ($attempt === false) { game_cryptex_continue($id, $game, $attempt, $cryptexrec); return; } $crossm = get_record_select('game_cross', "id={$attempt->id}"); $query = get_record_select('game_queries', "id={$q}"); $answer1 = trim(game_upper($query->answertext)); $answer2 = trim(game_upper($answer)); $textlib = textlib_get_instance(); $len1 = $textlib->strlen($answer1); $len2 = $textlib->strlen($answer2); $equal = $len1 == $len2; if ($equal) { for ($i = 0; $i < $len1; $i++) { if ($textlib->substr($answer1, $i, 1) != $textlib->substr($answer2, $i, 1)) { $equal = true; break; } } } if ($equal == false) { game_update_queries($game, $attempt, $query, 0, $answer2); game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, true); return; } game_update_queries($game, $attempt, $query, 1, $answer2); game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, true); }
function setwords($answers, $maxcols) { $textlib = textlib_get_instance(); $this->m_input_answers = array(); foreach ($answers as $word => $answer) { $this->m_input_answers[game_upper($word)] = $answer; } $this->m_words = array(); $maxlen = 0; foreach ($this->m_input_answers as $word => $answer) { $len = $textlib->strlen($word); if ($len > $maxlen) { $maxlen = $len; } } $N20 = $maxlen; $this->m_N20min = round($N20 - $N20 / 4); $this->m_N20max = round($N20 + $N20 / 4); if ($this->m_N20max > $maxcols and $maxcols > 0) { $this->m_N20max = $maxcols; } if ($this->m_N20min > $this->m_N20max) { $this->m_N20min = $this->m_N20max; } $this->m_words = array(); foreach ($this->m_input_answers as $word => $answer) { $len = $textlib->strlen($word); if ($len <= $this->m_N20max) { $this->m_words[] = game_upper($word); } } $this->randomize(); return count($this->m_words); }
function game_cross_continue($id, $game, $attempt, $cross, $g = '', $endofgame = '') { if ($endofgame) { if ($g == '') { game_updateattempts($game, $attempt, -1, true); $endofgame = false; } } if ($attempt != false and $cross != false) { return game_cross_play($id, $game, $attempt, $cross, $g, false, false, $endofgame, false, false, false, false); } if ($attempt == false) { $attempt = game_addattempt($game); } $textlib = textlib_get_instance(); $cross = new CrossDB(); $questions = array(); $infos = array(); $answers = array(); $recs = game_questions_shortanswer($game); if ($recs == false) { error('game_cross_continue: ' . get_string('cross_nowords', 'game')); } $infos = array(); foreach ($recs as $rec) { if ($game->param7 == false) { if ($textlib->strpos($rec->answertext, ' ')) { continue; //spaces not allowed } } $rec->answertext = game_upper($rec->answertext); $answers[$rec->answertext] = game_repairquestion($rec->questiontext); $infos[$rec->answertext] = array($game->sourcemodule, $rec->questionid, $rec->glossaryentryid, $rec->attachment); } $cross->setwords($answers, $game->param1); if ($cross->computedata($crossm, $crossd, $game->param2)) { $new_crossd = array(); foreach ($crossd as $rec) { $info = $infos[$rec->answertext]; if ($info != false) { $rec->sourcemodule = $info[0]; $rec->questionid = $info[1]; $rec->glossaryentryid = $info[2]; $rec->attachment = $info[3]; } $new_crossd[] = $rec; } $cross->save($game, $crossm, $new_crossd, $attempt->id); game_updateattempts($game, $attempt, 0, 0); return game_cross_play($id, $game, $attempt, $crossm, '', false, false, false, false, false, false, false); } if (count($crossd) == 0) { error('game_cross_continue: ' . get_string('cross_nowords', 'game')); } }
function game_cross_new($game, $attemptid, &$crossm) { global $DB, $USER; $cross = new CrossDB(); $questions = array(); $infos = array(); $answers = array(); $recs = game_questions_shortanswer($game); if ($recs == false) { print_error('game_cross_continue: ' . get_string('no_words', 'game')); } $infos = array(); $reps = array(); foreach ($recs as $rec) { if ($game->param7 == false) { if (game_strpos($rec->answertext, ' ')) { continue; // Spaces not allowed. } } $rec->answertext = game_upper($rec->answertext); $answers[$rec->answertext] = game_repairquestion($rec->questiontext); $infos[$rec->answertext] = array($game->sourcemodule, $rec->questionid, $rec->glossaryentryid, $rec->attachment); $a = array('gameid' => $game->id, 'userid' => $USER->id, 'questionid' => $rec->questionid, 'glossaryentryid' => $rec->glossaryentryid); if (($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions AS r')) != false) { $reps[$rec->answertext] = $rec2->r; } } $cross->setwords($answers, $game->param1, $reps); // The game->param4 is minimum words in crossword. // The game->param2 is maximum words in crossword. if ($cross->computedata($crossm, $crossd, $lettets, $game->param4, $game->param2, $game->param8)) { $newcrossd = array(); foreach ($crossd as $rec) { $info = $infos[$rec->answertext]; if ($info != false) { $rec->sourcemodule = $info[0]; $rec->questionid = $info[1]; $rec->glossaryentryid = $info[2]; $rec->attachment = $info[3]; } $newcrossd[] = $rec; } $cross->savecross($game, $crossm, $newcrossd, $attemptid); } if (count($crossd) == 0) { print_error('game_cross_continue: ' . get_string('no_words', 'game') . $game->id); } }
function game_getallletters($word, $lang = '') { for (;;) { $strings = get_string_manager()->load_component_strings('game', $lang); if (isset($strings['lettersall'])) { $letters = $strings['lettersall']; $word2 = game_upper($word, $lang); if (hangman_existall($word2, $letters)) { return $letters; } } if ($lang == '') { break; } else { $lang = ''; } } return ''; }
function game_exportjavame_exportdata_hangmanp($src, $destmobiledir, $destdir, $game, $map, $maxwidth, $maxheight) { global $CFG; $fp = fopen($destdir . "/{$destmobiledir}/{$destmobiledir}.txt", "w"); fputs($fp, "01=01\r\n"); fclose($fp); $destdirphoto = $destdir . '/' . $destmobiledir . '/01'; mkdir($destdirphoto); $fp = fopen($destdirphoto . '/photo.txt', "w"); foreach ($map as $line) { $file = $line->attachment; $pos = strrpos($file, '.'); if ($pos != false) { $file = $line->id . substr($file, $pos); $src = $CFG->dataroot . '/' . $game->course . '/moddata/' . $line->attachment; game_export_javame_smartcopyimage($src, $destdirphoto . '/' . $file, $maxwidth, $maxheight); $s = $file . '=' . game_upper($line->answer); fputs($fp, "{$s}\r\n"); } } fclose($fp); }
function game_grade_responses($question, $responses, $maxgrade, &$answertext) { if ($question->qtype == 'multichoice') { $name = "resp{$question->id}_"; $value = $responses->{$name}; $answer = $question->options->answers[$value]; $answertext = $answer->answer; return $answer->fraction * $maxgrade; } else { $name = "resp{$question->id}_"; $answertext = game_upper($responses->{$name}); foreach ($question->options->answers as $answer) { if (game_upper($answer->answer) == $answertext) { return $answer->fraction * $maxgrade; } } return 0; } }
board_height [0] = <?php echo $board->height; ?> ; pawn_width [0] = 40; pawn_height [0]= 40; <?php echo "var countofquestionsM={$countofquestionsm};\r\n"; echo 'var countofquestionsS=' . count($questionss) . ";\r\n"; foreach ($questionss as $line) { $s = $line->question . '#' . str_replace(array('"', '#'), array("'", ' '), $line->answer); if ($questionsm != '') { $questionsm .= ",\r"; } $questionsm .= '"' . base64_encode(game_upper($s)) . '"'; $s = '#' . str_replace(array('"', '#'), array("'", ' '), $line->feedback); if ($retfeedback != '') { $retfeedback .= ",\r"; } $retfeedback .= '"' . base64_encode($s) . '"'; } $rettimesasked = ''; for ($i = 0; $i < $countofquestionsm + count($questionss); $i++) { $rettimesasked .= ',0'; } $rettimesasked = substr($rettimesasked, 1); echo "var questions=new Array( {$questionsm});\r\n"; echo "var feedbacks=new Array( {$retfeedback});\r\n"; echo "var quest_times_asked=new Array( {$rettimesasked}); //How many times is used a question\r\n"; ?>
public function setwords($answers, $maxcols, $reps) { $this->mreps = array(); foreach ($reps as $word => $r) { $this->mreps[game_upper($word)] = $r; } $this->maveragereps = 0; foreach ($reps as $r) { $this->maveragereps += $r; } if (count($reps)) { $this->maveragereps /= count($reps); } $this->minputanswers = array(); foreach ($answers as $word => $answer) { $this->minputanswers[game_upper($word)] = $answer; } $this->mwords = array(); $maxlen = 0; foreach ($this->minputanswers as $word => $answer) { $len = game_strlen($word); if ($len > $maxlen) { $maxlen = $len; } } $n20 = $maxlen; if ($n20 < 15) { $n20 = 15; } $this->mn20min = round($n20 - $n20 / 4); $this->mn20max = round($n20 + $n20 / 4); if ($this->mn20max > $maxcols and $maxcols > 0) { $this->mn20max = $maxcols; } if ($this->mn20min > $this->mn20max) { $this->mn20min = $this->mn20max; } $this->mwords = array(); foreach ($this->minputanswers as $word => $answer) { $len = game_strlen($word); if ($len <= $this->mn20max) { $this->mwords[] = game_upper($word); } } $this->randomize(); return count($this->mwords); }
function game_sudoku_check_glossaryentries($id, $game, $attempt, $sudoku, $finishattempt, $course) { global $QTYPES, $DB; $responses = data_submitted(); //this function returns offsetentries, numbers, correctquestions $offsetentries = game_sudoku_compute_offsetquestions($game->sourcemodule, $attempt, $numbers, $correctquestions); $entrieslist = game_sudoku_getquestionlist($offsetentries); // Load the glossary entries if (!($entries = $DB->get_records_select('glossary_entries', "id IN ({$entrieslist})"))) { print_error(get_string('noglossaryentriesfound', 'game')); } foreach ($entries as $entry) { $answerundefined = optional_param('resp' . $entry->id, 'undefined', PARAM_TEXT); if ($answerundefined == 'undefined') { continue; } $answer = optional_param('resp' . $entry->id, '', PARAM_TEXT); if ($answer == '') { continue; } if (game_upper($entry->concept) != game_upper($answer)) { continue; } //correct answer $select = "attemptid={$attempt->id}"; $select .= " AND glossaryentryid={$entry->id} AND col>0"; $select .= " AND questiontext is null"; // check the student guesses not source glossary entry. $query = new stdClass(); if (($query->id = $DB->get_field_select('game_queries', 'id', $select)) == 0) { echo "not found {$select}<br>"; continue; } game_update_queries($game, $attempt, $query, 1, $answer); } game_sudoku_check_last($id, $game, $attempt, $sudoku, $finishattempt, $course); return true; }
function game_sudoku_check_glossaryentries($id, $game, $attempt, $sudoku, $finishattempt) { global $QTYPES, $CFG; $responses = data_submitted(); //this function returns offsetentries, numbers, correctquestions $offsetentries = game_sudoku_compute_offsetquestions($game->sourcemodule, $attempt, $numbers, $correctquestions); $entrieslist = game_sudoku_getquestionlist($offsetentries); // Load the glossary entries if (!($entries = get_records_select('glossary_entries', "id IN ({$entrieslist})"))) { error(get_string('noglossaryentriesfound', 'game')); } foreach ($entries as $entry) { if (!array_key_exists('resp' . $entry->id, $_POST)) { continue; } $answer = $_POST['resp' . $entry->id]; if ($answer == '') { continue; } if (game_upper($entry->concept) != game_upper($answer)) { continue; } //correct answer $select = "attemptid={$attempt->id} and score < 0.5"; $select .= " AND glossaryentryid={$entry->id}"; unset($query); if (($query->id = get_field_select('game_queries', 'id', $select)) == 0) { echo "not found {$select}<br>"; continue; } game_update_queries($game, $attempt, $query, 1, $answer); } game_sudoku_check_last($id, $game, $attempt, $sudoku, $finishattempt); }
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_snakes_check_questions($id, $game, $attempt, $snakes) { global $QTYPES, $CFG; $responses = data_submitted(); if ($responses->queryid != $snakes->queryid) { game_snakes_play($id, $game, $attempt, $snakes); return; } $questionlist = get_field('game_queries', 'questionid', 'id', $responses->queryid); $questions = game_sudoku_getquestions($questionlist); $actions = question_extract_responses($questions, $responses, QUESTION_EVENTSUBMIT); $correct = false; $query = ''; foreach ($questions as $question) { if (!array_key_exists($question->id, $actions)) { //no answered continue; } unset($state); unset($cmoptions); $question->maxgrade = 100; $state->responses = $actions[$question->id]->responses; $state->event = QUESTION_EVENTGRADE; $state->responses[''] = game_upper($state->responses['']); $cmoptions = array(); $QTYPES[$question->qtype]->grade_responses($question, $state, $cmoptions); unset($query); $select = "attemptid={$attempt->id} "; $select .= " AND questionid={$question->id}"; if (($query->id = get_field_select('game_queries', 'id', $select)) == 0) { die("problem game_sudoku_check_questions (select={$select})"); continue; } $grade = $state->raw_grade; if ($grade < 50) { //wrong answer game_update_queries($game, $attempt, $query, 0, ''); continue; } //correct answer $correct = true; game_update_queries($game, $attempt, $query, 1, ''); } //set the grade of the whole game game_snakes_position($id, $game, $attempt, $snakes, $correct, $query); }
function game_getallletters($word, $lang = '') { global $CFG; if ($lang != '') { $langfile = "{$CFG->dirroot}/mod/game/lang/{$lang}/game.php"; if (file_exists($langfile)) { for ($i = 1; $i <= 2; $i++) { if ($result = get_string_from_file('lettersall' . $i, $langfile, "\$letters")) { eval($result); if ($letters != '') { if (hangman_existall($word, $letters)) { return $letters; } } } } } } for ($i = 1; $i <= 2; $i++) { $letters = get_string('lettersall' . $i, 'game'); if ($letters == '') { continue; } $letters = game_upper($letters); if (hangman_existall($word, $letters)) { return $letters; } } return ''; }
function game_snakes_check_glossary($id, $game, $attempt, $snakes, $context) { global $QTYPES, $CFG, $DB; $responses = data_submitted(); if ($responses->queryid != $snakes->queryid) { game_snakes_play($id, $game, $attempt, $snakes, $context); return; } $query = $DB->get_record('game_queries', array('id' => $responses->queryid)); $glossaryentry = $DB->get_record('glossary_entries', array('id' => $query->glossaryentryid)); $name = 'resp' . $query->glossaryentryid; $useranswer = $responses->answer; if (game_upper($useranswer) != game_upper($glossaryentry->concept)) { //wrong answer $correct = false; game_update_queries($game, $attempt, $query, 0, $useranswer); //last param is grade } else { //correct answer $correct = true; game_update_queries($game, $attempt, $query, 1, $useranswer); //last param is grade } //set the grade of the whole game game_snakes_position($id, $game, $attempt, $snakes, $correct, $query, $context); }
function game_hiddenpicture_check_mainquestion($id, $game, &$attempt, &$hiddenpicture, $finishattempt, $context) { global $QTYPES, $CFG, $DB; $responses = data_submitted(); $glossaryentryid = $responses->glossaryentryid; $queryid = $responses->queryid; // Load the glossary entry if (!($entry = $DB->get_record('glossary_entries', array('id' => $glossaryentryid)))) { print_error(get_string('noglossaryentriesfound', 'game')); } $answer = $responses->answer; $correct = false; if ($answer != '') { if (game_upper($entry->concept) == game_upper($answer)) { $correct = true; } } // Load the query if (!($query = $DB->get_record('game_queries', array('id' => $queryid)))) { print_error("The query {$queryid} not found"); } game_update_queries($game, $attempt, $query, $correct, $answer); if ($correct) { $hiddenpicture->found = 1; } else { $hiddenpicture->wrong++; } if (!$DB->update_record('game_hiddenpicture', $hiddenpicture)) { print_error('game_hiddenpicture_check_mainquestion: error updating in game_hiddenpicture'); } $score = game_hidden_picture_computescore($game, $hiddenpicture); game_updateattempts($game, $attempt, $score, $correct); if ($correct == false) { game_hiddenpicture_play($id, $game, $attempt, $hiddenpicture, false, $context); return true; } //Finish the game $query = $DB->get_record_select('game_queries', "attemptid={$hiddenpicture->id} AND col=0", null, 'id,glossaryentryid,attachment,questiontext'); game_showpicture($id, $game, $attempt, $query, '', '', false); echo '<p><BR/><font size="5" color="green">' . get_string('win', 'game') . '</font><BR/><BR/></p>'; global $CFG; echo '<br/>'; echo "<a href=\"{$CFG->wwwroot}/mod/game/attempt.php?id={$id}\">"; echo get_string('nextgame', 'game') . '</a> '; if (!($cm = $DB->get_record('course_modules', array('id' => $id)))) { print_error("Course Module ID was incorrect id={$id}"); } echo "<a href=\"{$CFG->wwwroot}/course/view.php?id={$cm->course}\">" . get_string('finish', 'game') . '</a> '; return false; }
function game_grade_questions($questions) { $grades = array(); foreach ($_POST as $key => $value) { $id = game_question_get_id_from_name_prefix($key); if ($id === false) { continue; } if (array_key_exists($id, $grades)) { $grade = $grades[$id]; } else { $grade = new stdClass(); $grade->grade = 0; $grade->id = $id; } $grade->response = $value; $question = $questions[$id]; if ($question->qtype == 'multichoice') { $answer = $question->options->answers[$value]; $grade->grade += $answer->fraction; } else { if ($question->qtype == 'shortanswer') { foreach ($question->options->answers as $answerid => $answer) { if (game_upper($answer->answer == $value)) { $grade->grade = $answer->fraction; break; } } } } $grades[$grade->id] = $grade; } return $grades; }
var can_play = true; <?php $destdir = game_export_createtempdir(); $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;
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); }