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'));
    }
}
Esempio n. 2
0
function game_cross_continue($id, $game, $attempt, $cross, $g, $endofgame, $context)
{
    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, true, $context);
    }
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    game_cross_new($game, $attempt->id, $crossm);
    game_updateattempts($game, $attempt, 0, 0);
    return game_cross_play($id, $game, $attempt, $crossm, '', false, false, false, false, false, false, false, true, $context);
}
 function game_cross_computecheck($correctletters, $wrongletters, $restletters, $game, $attempt, &$done, $onlyshow, $showsolution, $finishattempt)
 {
     $ret = '';
     if ($correctletters == 0 and $wrongletters == 0) {
         return $ret;
     }
     $and = get_string('and', 'game');
     $a = array();
     if ($correctletters) {
         $a[] = $correctletters . ' ' . ($correctletters > 1 ? get_string('cross_corrects', 'game') : get_string('cross_correct', 'game'));
     }
     if ($wrongletters) {
         $a[] = $wrongletters . ' ' . ($wrongletters > 1 ? get_string('cross_errors', 'game') : get_string('cross_error', 'game'));
     }
     if ($correctletters > 1 or $wrongletters > 1) {
         $ret = get_string('cross_found_many', 'game');
     } else {
         $ret = get_string('cross_found_one', 'game');
     }
     $i = 0;
     foreach ($a as $msg) {
         $i++;
         if ($i == 1) {
             $ret .= ' ' . $msg;
         } else {
             if ($i == count($a)) {
                 $ret .= ' ' . get_string('and', 'game') . ' ' . $msg;
             } else {
                 $ret .= ', ' . $msg;
             }
         }
     }
     $done = $restletters == 0 ? true : false;
     if ($finishattempt == false) {
         if ($onlyshow or $showsolution) {
             return $ret;
         }
     } else {
         $done = 1;
     }
     $grade = $correctletters / ($correctletters + $restletters);
     $ret .= '<br>' . get_string('grade', 'game') . ' ' . round($grade * 100) . ' %';
     game_updateattempts($game, $attempt, $grade, $done);
     return $ret;
 }
Esempio n. 4
0
function game_sudoku_check_last($id, $game, $attempt, $sudoku, $finishattempt, $course)
{
    global $CFG, $DB;
    $correct = $DB->get_field_select('game_queries', 'COUNT(*) AS c', "attemptid={$attempt->id} AND score > 0.9");
    $all = $DB->get_field_select('game_queries', 'COUNT(*) AS c', "attemptid={$attempt->id}");
    if ($all) {
        $grade = $correct / $all;
    } else {
        $grade = 0;
    }
    game_updateattempts($game, $attempt, $grade, $finishattempt);
}
Esempio n. 5
0
function game_bookquiz_check_questions($id, $game, $attempt, $bookquiz, $context)
{
    global $USER, $DB;
    $scoreattempt = optional_param('scoreattempt', 0, PARAM_INT);
    $responses = data_submitted();
    $questionlist = $responses->questionids;
    $questions = game_sudoku_getquestions($questionlist);
    $grades = game_grade_questions($questions);
    $scorequestion = 0;
    $scoreattempt = 0;
    $chapterid = required_param('chapterid', PARAM_INT);
    $nextchapterid = required_param('nextchapterid', PARAM_INT);
    foreach ($questions as $question) {
        if (!array_key_exists($question->id, $grades)) {
            //no answered
            continue;
        }
        $grade = $grades[$question->id];
        if ($grade->grade < 0.5) {
            continue;
        }
        //found one correct answer
        if (!$DB->get_field('game_bookquiz_chapters', 'id', array('attemptid' => $attempt->id, 'chapterid' => $chapterid))) {
            $newrec = new stdClass();
            $newrec->attemptid = $attempt->id;
            $newrec->chapterid = $chapterid;
            if (!$DB->insert_record('game_bookquiz_chapters', $newrec, false)) {
                print_object($newrec);
                print_error("Can't insert to table game_bookquiz_chapters");
            }
        }
        //Have to go to next page.
        $bookquiz->lastchapterid = $nextchapterid;
        $scorequestion = 1;
        break;
    }
    $query = new stdClass();
    $query->id = 0;
    $query->attemptid = $attempt->id;
    $query->gameid = $game->id;
    $query->userid = $USER->id;
    $query->sourcemodule = 'question';
    $query->questionid = $question->id;
    $query->glossaryentryid = 0;
    $query->questiontext = $question->questiontext;
    $query->timelastattempt = time();
    game_update_queries($game, $attempt, $query, $scorequestion, '');
    game_updateattempts($game, $attempt, $scoreattempt, 0);
    game_bookquiz_continue($id, $game, $attempt, $bookquiz, $bookquiz->lastchapterid, $context);
}
Esempio n. 6
0
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> &nbsp; &nbsp; &nbsp; &nbsp;';
    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;
}
Esempio n. 7
0
function game_updateattempts_maxgrade($game, $attempt, $grade, $finished)
{
    global $DB;
    $recgrade = $DB->get_field('game_attempts', 'score', array('id' => $attempt->id));
    if ($recgrade > $grade) {
        $grade = -1;
        //don't touch the grade
    }
    game_updateattempts($game, $attempt, $grade, $finished);
}
function game_millionaire_onquit($id, $game, $attempt, $query)
{
    global $CFG;
    game_updateattempts($game, $attempt, -1, true);
    if (!($cm = get_record("course_modules", "id", $id))) {
        error("Course Module ID was incorrect id={$id}");
    }
    echo '<br>';
    echo "<a href=\"{$CFG->wwwroot}/mod/game/attempt.php?id={$id}\">" . get_string('nextgame', 'game') . '</a> &nbsp; &nbsp; &nbsp; &nbsp; ';
    echo "<a href=\"{$CFG->wwwroot}/course/view.php?id={$cm->course}\">" . get_string('finish', 'game') . '</a> ';
}
Esempio n. 9
0
function game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, $updateattempt = false, $onlyshow = false, $showsolution = false, $context)
{
    global $DB;
    global $CFG;
    if ($game->toptext != '') {
        echo $game->toptext . '<br>';
    }
    echo '<br>';
    $cryptex = new CryptexDB();
    $language = $attempt->language;
    $questions = $cryptex->loadcryptex($crossm, $mask, $corrects, $attempt->language);
    if ($language != $attempt->language) {
        if (!$DB->set_field('game_attempts', 'language', $attempt->language, array('id' => $attempt->id))) {
            print_error("game_cross_play: Can't set language");
        }
    }
    if ($attempt->language != '') {
        $wordrtl = game_right_to_left($attempt->language);
    } else {
        $wordrtl = right_to_left();
    }
    $reverseprint = $wordrtl != right_to_left();
    if ($reverseprint) {
        $textdir = 'dir="' . ($wordrtl ? 'rtl' : 'ltr') . '"';
    } else {
        $textdir = '';
    }
    $len = textlib::strlen($mask);
    //count1 means there is a guested letter
    //count2 means there is a letter that not guessed
    $count1 = $count2 = 0;
    for ($i = 0; $i < $len; $i++) {
        $c = textlib::substr($mask, $i, 1);
        if ($c == '1') {
            $count1++;
        } else {
            if ($c == '2') {
                $count2++;
            }
        }
    }
    if ($count1 + $count2 == 0) {
        $gradeattempt = 0;
    } else {
        $gradeattempt = $count1 / ($count1 + $count2);
    }
    $finished = $count2 == 0;
    if ($finished === false && $game->param8 > 0) {
        $found = false;
        foreach ($questions as $q) {
            if ($q->tries < $game->param8) {
                $found = true;
            }
        }
        if ($found == false) {
            $finished = true;
        }
        //rich max tries
    }
    if ($updateattempt) {
        game_updateattempts($game, $attempt, $gradeattempt, $finished);
    }
    if ($onlyshow == false and $showsolution == false) {
        if ($finished) {
            game_cryptex_onfinished($id, $game, $attempt, $cryptexrec);
        }
    }
    ?>
<style type="text/css"><!--

.answerboxstyle  {
background-color:	#FFFAF0;
border-color:	#808080;
border-style:	solid;
border-width:	1px;
display:	block;
padding:	.75em;
width:	240pt;
}
--></style>
<?php 
    echo '<table border=0>';
    echo '<tr><td>';
    $cryptex->displaycryptex($crossm->cols, $crossm->rows, $cryptexrec->letters, $mask, $showsolution, $textdir);
    ?>
</td>

<td width=10%>&nbsp;</td>
<td>

<form  method="get" action="<?php 
    echo $CFG->wwwroot;
    ?>
/mod/game/attempt.php">
<div id="answerbox" class="answerboxstyle" style="display:none;">
<div id="wordclue" name="wordclue" class="cluebox"> </div>
<input id="action" name="action" type="hidden" value="cryptexcheck">
<input id="q" name="q" type="hidden" >
<input id="id" name="id" value="<?php 
    echo $id;
    ?>
" type="hidden">

<div style="margin-top:1em;"><input id="answer" name="answer" type="text" size="24"
 style="font-weight: bold; text-transform:uppercase;" autocomplete="off"></div>

<table border="0" cellspacing="0" cellpadding="0" width="100%" style="margin-top:1em;"><tr>
<td align="right">
<button id="okbutton" type="submit" class="button" style="font-weight: bold;">OK</button> &nbsp;
<button id="cancelbutton" type="button" class="button" onclick="DeselectCurrentWord();">Cancel</button>
</td></tr></table>
</form>
</td>
</tr>
</table>


<?php 
    $grade = round(100 * $gradeattempt);
    echo '<br>' . get_string('grade', 'game') . ' ' . $grade . ' %';
    echo "<br><br>";
    $i = 0;
    $else = '';
    $contextglossary = false;
    foreach ($questions as $key => $q) {
        //print_r( $q);
        $i++;
        if ($showsolution == false) {
            //When I want to show the solution a want to show the questions to.
            if (array_key_exists($q->id, $corrects)) {
                continue;
            }
        }
        $question = "{$i}. " . $q->questiontext;
        $question = game_filterquestion(str_replace('\\"', '"', $question), $q->questionid, $context->id, $game->course);
        $question2 = strip_tags($question);
        //ADDED BY DP (AUG 2009) - fixes " breaking the Answer button for this question
        if ($onlyshow == false and $showsolution == false) {
            if ($game->param8 == 0 || $game->param8 > $q->tries) {
                $question .= ' &nbsp;<input type="submit" value="' . get_string('answer') . '" onclick="OnCheck( ' . $q->id . ',\'' . $question2 . '\');" />';
            }
        }
        echo $question;
        if ($showsolution) {
            echo " &nbsp;&nbsp;&nbsp;{$q->answertext}<B></b>";
        }
        echo '<br>';
    }
    if ($game->bottomtext != '') {
        echo '<br><br>' . $game->bottomtext;
    }
    ?>
		<script>
			function OnCheck( id, question)
			{
				document.getElementById("q").value = id;
				document.getElementById("wordclue").innerHTML = question;

				// Finally, show the answer box.
				document.getElementById("answerbox").style.display = "block";
				try
				{
					document.getElementById("answer").focus();
					document.getElementById("answer").select();
				}
				catch (e)
				{
				}
			}
		</script>
	<?php 
}
Esempio n. 10
0
function game_snakes_position($id, $game, $attempt, $snakes, $correct, $query, $context)
{
    global $DB;
    $data = $DB->get_field('game_snakes_database', 'data', array('id' => $snakes->snakesdatabaseid));
    if ($correct) {
        if ($next = game_snakes_foundlander($snakes->position + $snakes->dice, $data)) {
            $snakes->position = $next;
        } else {
            $snakes->position = $snakes->position + $snakes->dice;
        }
    } else {
        if ($next = game_snakes_foundsnake($snakes->position, $data)) {
            $snakes->position = $next;
        }
    }
    $updrec = new stdClass();
    $updrec->id = $snakes->id;
    $updrec->position = $snakes->position;
    $updrec->queryid = 0;
    if (!$DB->update_record('game_snakes', $updrec)) {
        print_error("game_snakes_position: Can't update game_snakes");
    }
    $board = $DB->get_record_select('game_snakes_database', "id={$snakes->snakesdatabaseid}");
    $gradeattempt = $snakes->position / ($board->cols * $board->rows);
    $finished = $snakes->position > $board->cols * $board->rows ? 1 : 0;
    game_updateattempts($game, $attempt, $gradeattempt, $finished);
    game_snakes_computenextquestion($game, $snakes, $query);
    game_snakes_play($id, $game, $attempt, $snakes, $context);
}
function game_sudoku_check_last($id, $game, $attempt, $sudoku, $finishattempt)
{
    global $CFG;
    $correct = get_field_select('game_queries', 'COUNT(*) AS c', "attemptid={$attempt->id} AND score > 0.9");
    $all = get_field_select('game_queries', 'COUNT(*) AS c', "attemptid={$attempt->id}");
    if ($all) {
        $grade = $correct / $all;
    } else {
        $grade = 0;
    }
    game_updateattempts($game, $attempt, $grade, $finishattempt);
    redirect("{$CFG->wwwroot}/mod/game/attempt.php?id={$id}", '', 0);
}
function game_bookquiz_check_questions($id, $game, $attempt, $bookquiz)
{
    global $QTYPES, $CFG, $USER;
    $responses = data_submitted();
    $questionlist = $responses->questionids;
    $questions = game_sudoku_getquestions($questionlist);
    $actions = question_extract_responses($questions, $responses, QUESTION_EVENTSUBMIT);
    $scorequestion = 0;
    $scoreattempt = 0;
    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;
        $cmoptions = array();
        $QTYPES[$question->qtype]->grade_responses($question, $state, $cmoptions);
        if ($state->raw_grade < 50) {
            continue;
        }
        //found one correct answer
        $chapterid = $responses->chapterid;
        if (!get_field('game_bookquiz_chapters', 'id', 'attemptid', $attempt->id, 'chapterid', $chapterid)) {
            unset($newrec);
            $newrec->attemptid = $attempt->id;
            $newrec->chapterid = $chapterid;
            if (!insert_record('game_bookquiz_chapters', $newrec, false)) {
                print_object($newrec);
                error("Can't insert to table game_bookquiz_chapters");
            }
        }
        //Have to go to next page.
        $bookquiz->lastchapterid = $responses->nextchapterid;
        if (!set_field('game_bookquiz', 'lastchapterid', $bookquiz->lastchapterid, 'id', $bookquiz->id)) {
            error("Can't compute next chapter");
        }
        $scorequestion = 1;
        $scoreattempt = $_POST['scoreattempt'];
        break;
    }
    $query->id = 0;
    $query->attemptid = $attempt->id;
    $query->gameid = $game->id;
    $query->userid = $USER->id;
    $query->sourcemodule = 'question';
    $query->questionid = $question->id;
    $query->glossaryentryid = 0;
    $query->questiontext = $question->questiontext;
    $query->timelastattempt = time();
    game_update_queries($game, $attempt, $query, $scorequestion, '');
    game_updateattempts($game, $attempt, $scoreattempt, 0);
    game_bookquiz_continue($id, $game, $attempt, $bookquiz);
}
Esempio n. 13
0
function hangman_showpage(&$done, &$correct, &$wrong, $max, &$wordline, &$wordline2, &$links, $game, &$attempt, &$hangman, &$query, $onlyshow, $showsolution, $context)
{
    global $USER, $CFG, $DB;
    $id = optional_param('id', 0, PARAM_INT);
    // Course Module ID.
    $word = $query->answertext;
    $newletter = optional_param('newletter', "", PARAM_TEXT);
    if ($newletter == '_') {
        $newletter = ' ';
    }
    if ($newletter == 'A') {
        $letters = $hangman->letters;
        if ($newletter != null) {
            if (game_strpos($letters, $newletter) === false) {
                $letters .= $newletter;
            }
            if (game_strpos($letters, 'Ã') === false) {
                $letters .= 'Ã';
            }
        }
    } elseif ($newletter == 'C') {
        $letters = $hangman->letters;
        if ($newletter != null) {
            if (game_strpos($letters, $newletter) === false) {
                $letters .= $newletter;
            }
            if (game_strpos($letters, 'Ç') === false) {
                $letters .= 'Ç';
            }
        }
    } else {
        $letters = $hangman->letters;
        if ($newletter != null) {
            if (game_strpos($letters, $newletter) === false) {
                $letters .= $newletter;
            }
        }
    }
    $links = "";
    $alpha = $hangman->allletters;
    $wrong = 0;
    if ($query->questionid) {
        $questiontext = str_replace(array("\\'", '\\"'), array("'", '"'), $query->questiontext);
        $query->questiontext = game_filterquestion($questiontext, $query->questionid, $context->id, $game->course);
    } else {
        $cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course);
        $contextglossary = game_get_context_module_instance($cmglossary->id);
        $query->questiontext = game_filterglossary(str_replace('\\"', '"', $query->questiontext), $query->glossaryentryid, $contextglossary->id, $game->course);
    }
    if ($game->param5) {
        $s = trim(game_filtertext($query->questiontext, $game->course));
        if ($s != '.' and $s != '') {
            echo "<br/><b>" . $s . '</b>';
        }
        if ($query->attachment != '') {
            $file = "{$CFG->wwwroot}/file.php/{$game->course}/moddata/{$query->attachment}";
            echo "<img src=\"{$file}\" />";
        }
        echo "<br/><br/>";
    }
    $wordline = $wordline2 = "";
    $len = game_strlen($word);
    $done = 1;
    $answer = '';
    $correct = 0;
    for ($x = 0; $x < $len; $x++) {
        $char = game_substr($word, $x, 1);
        if ($showsolution) {
            $wordline2 .= $char == " " ? '&nbsp; ' : $char;
            $done = 0;
        }
        if (game_strpos($letters, $char) === false) {
            $wordline .= "_<font size=\"1\">&nbsp;</font>\r\n";
            $done = 0;
            $answer .= '_';
        } else {
            $wordline .= $char == " " ? '&nbsp; ' : $char;
            $answer .= $char;
            $correct++;
        }
    }
    $lenalpha = game_strlen($alpha);
    $fontsize = 5;
    for ($c = 0; $c < $lenalpha; $c++) {
        $char = game_substr($alpha, $c, 1);
        if (game_strpos($letters, $char) === false) {
            // User doesn't select this character.
            $params = 'id=' . $id . '&amp;newletter=' . urlencode($char);
            if ($onlyshow or $showsolution) {
                $links .= $char;
            } else {
                $links .= "<font size=\"{$fontsize}\"><a href=\"attempt.php?{$params}\">{$char}</a></font>\r\n";
            }
            continue;
        }
        if ($char == 'A') {
            if (game_strpos($word, $char) === false and game_strpos($word, 'Ã') === false) {
                $links .= "\r\n<font size=\"{$fontsize}\" color=\"red\">{$char} </font>";
                $wrong++;
            } else {
                $links .= "\r\n<B><font size=\"{$fontsize}\">{$char} </font></B> ";
            }
        } elseif ($char == 'C') {
            if (game_strpos($word, $char) === false and game_strpos($word, 'Ç') === false) {
                $links .= "\r\n<font size=\"{$fontsize}\" color=\"red\">{$char} </font>";
                $wrong++;
            } else {
                $links .= "\r\n<B><font size=\"{$fontsize}\">{$char} </font></B> ";
            }
        } elseif (game_strpos($word, $char) === false) {
            $links .= "\r\n<font size=\"{$fontsize}\" color=\"red\">{$char} </font>";
            $wrong++;
        } else {
            $links .= "\r\n<B><font size=\"{$fontsize}\">{$char} </font></B> ";
        }
    }
    $finishedword = ($done or $wrong >= $max);
    $finished = false;
    $updrec = new stdClass();
    $updrec->id = $hangman->id;
    $updrec->letters = $letters;
    if ($finishedword) {
        if ($hangman->finishedword == 0) {
            // Only one time per word increace the variable try.
            $hangman->try = $hangman->try + 1;
            if ($hangman->try > $hangman->maxtries) {
                $finished = true;
            }
            if ($done) {
                $hangman->corrects = $hangman->corrects + 1;
                $updrec->corrects = $hangman->corrects;
            }
        }
        $updrec->try = $hangman->try;
        $updrec->finishedword = 1;
    }
    $query->percent = ($correct - $wrong / $max) / game_strlen($word);
    if ($query->percent < 0) {
        $query->percent = 0;
    }
    if ($onlyshow or $showsolution) {
        return;
    }
    if (!$DB->update_record('game_hangman', $updrec)) {
        print_error("hangman_showpage: Can't update game_hangman id={$updrec->id}");
    }
    if ($done) {
        $score = 1;
    } else {
        if ($wrong >= $max) {
            $score = 0;
        } else {
            $score = -1;
        }
    }
    game_updateattempts($game, $attempt, $score, $finished);
    game_update_queries($game, $attempt, $query, $score, $answer);
}
Esempio n. 14
0
function game_bookquiz_check_questions($id, $game, $attempt, $bookquiz)
{
    global $QTYPES, $CFG, $USER, $DB;
    $responses = data_submitted();
    $questionlist = $responses->questionids;
    $questions = game_sudoku_getquestions($questionlist);
    $scorequestion = 0;
    $scoreattempt = 0;
    foreach ($questions as $question) {
        $grade = game_grade_responses($question, $responses, 100, $answertext);
        if ($grade < 50) {
            continue;
        }
        //found one correct answer
        $chapterid = $responses->chapterid;
        if (!$DB->get_field('game_bookquiz_chapters', 'id', array('attemptid' => $attempt->id, 'chapterid' => $chapterid))) {
            unset($newrec);
            $newrec->attemptid = $attempt->id;
            $newrec->chapterid = $chapterid;
            if (!$DB->insert_record('game_bookquiz_chapters', $newrec, false)) {
                print_object($newrec);
                print_error("Can't insert to table game_bookquiz_chapters");
            }
        }
        //Have to go to next page.
        $bookquiz->lastchapterid = $responses->nextchapterid;
        if (!$DB->set_field('game_bookquiz', 'lastchapterid', $bookquiz->lastchapterid, array('id' => $bookquiz->id))) {
            print_error("Can't compute next chapter");
        }
        $scorequestion = 1;
        $scoreattempt = $_POST['scoreattempt'];
        break;
    }
    $query->id = 0;
    $query->attemptid = $attempt->id;
    $query->gameid = $game->id;
    $query->userid = $USER->id;
    $query->sourcemodule = 'question';
    $query->questionid = $question->id;
    $query->glossaryentryid = 0;
    $query->questiontext = $question->questiontext;
    $query->timelastattempt = time();
    game_update_queries($game, $attempt, $query, $scorequestion, '');
    game_updateattempts($game, $attempt, $scoreattempt, 0);
    game_bookquiz_continue($id, $game, $attempt, $bookquiz);
}
Esempio n. 15
0
function hangman_showpage(&$done, &$correct, &$wrong, $max, &$word_line, &$word_line2, &$links, $game, &$attempt, &$hangman, &$query, $onlyshow, $showsolution)
{
    global $USER, $CFG, $DB;
    $id = optional_param('id', 0, PARAM_INT);
    // Course Module ID, or
    $word = $query->answertext;
    $textlib = textlib_get_instance();
    $newletter = optional_param('newletter', "", PARAM_TEXT);
    if ($newletter == '_') {
        $newletter = ' ';
    }
    $letters = $hangman->letters;
    if ($newletter != NULL) {
        if ($textlib->strpos($letters, $newletter) === false) {
            $letters .= $newletter;
        }
    }
    $links = "";
    $alpha = $hangman->allletters;
    $wrong = 0;
    if ($game->param5) {
        $s = trim(game_filtertext($query->questiontext, $game->course));
        if ($s != '.' and $s != '') {
            echo "<br/><b>" . $s . '</b>';
        }
        if ($query->attachment != '') {
            $file = "{$CFG->wwwroot}/file.php/{$game->course}/moddata/{$query->attachment}";
            echo "<img src=\"{$file}\" />";
        }
        echo "<br/><br/>";
    }
    $word_line = $word_line2 = "";
    $len = $textlib->strlen($word);
    $done = 1;
    $answer = '';
    for ($x = 0; $x < $len; $x++) {
        $char = $textlib->substr($word, $x, 1);
        if ($showsolution) {
            $word_line2 .= $char == " " ? '&nbsp; ' : $char;
            $done = 0;
        }
        if ($textlib->strpos($letters, $char) === false) {
            $word_line .= "_<font size=\"1\">&nbsp;</font>\r\n";
            $done = 0;
            $answer .= '_';
        } else {
            $word_line .= $char == " " ? '&nbsp; ' : $char;
            $answer .= $char;
        }
    }
    $correct = 0;
    $len_alpha = $textlib->strlen($alpha);
    $fontsize = 5;
    for ($c = 0; $c < $len_alpha; $c++) {
        $char = $textlib->substr($alpha, $c, 1);
        if ($textlib->strpos($letters, $char) === false) {
            //User doesn't select this character
            $params = 'id=' . $id . '&amp;newletter=' . urlencode($char);
            if ($onlyshow or $showsolution) {
                $links .= $char;
            } else {
                $links .= "<font size=\"{$fontsize}\"><a href=\"attempt.php?{$params}\">{$char}</a></font>\r\n";
            }
            continue;
        }
        if ($textlib->strpos($word, $char) === false) {
            $links .= "\r\n<font size=\"{$fontsize}\" color=\"red\">{$char} </font>";
            $wrong++;
        } else {
            $links .= "\r\n<B><font size=\"{$fontsize}\">{$char} </font></B> ";
            $correct++;
        }
    }
    $finishedword = ($done or $wrong >= 6);
    $finished = false;
    $updrec->id = $hangman->id;
    $updrec->letters = $letters;
    if ($finishedword) {
        if ($hangman->finishedword == 0) {
            //only one time per word increace the variable try
            $hangman->try = $hangman->try + 1;
            if ($hangman->try > $hangman->maxtries) {
                $finished = true;
            }
            if ($done) {
                $hangman->corrects = $hangman->corrects + 1;
                $updrec->corrects = $hangman->corrects;
            }
        }
        $updrec->try = $hangman->try;
        $updrec->finishedword = 1;
    }
    $query->percent = ($correct - $wrong / 6) / $textlib->strlen($word);
    if ($query->percent < 0) {
        $query->percent = 0;
    }
    if ($onlyshow or $showsolution) {
        return;
    }
    if (!$DB->update_record('game_hangman', $updrec)) {
        show_error("hangman_showpage: Can't update game_hangman id={$updrec->id}");
    }
    if ($done) {
        $score = 1;
    } else {
        if ($wrong >= 6) {
            $score = 0;
        } else {
            $score = -1;
        }
    }
    game_updateattempts($game, $attempt, $score, $finished);
    game_update_queries($game, $attempt, $query, $score, $answer);
}
function game_updateattempts_maxgrade($game, $attempt, $grade, $finished)
{
    $recgrade = get_field('game_attempts', 'score', 'id', $attempt->id);
    if ($recgrade > $grade) {
        $grade = -1;
        //don't touch the grade
    }
    game_updateattempts($game, $attempt, $grade, $finished);
}