Exemplo n.º 1
0
function game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, $updateattempt = false, $onlyshow = false, $showsolution = false, $context)
{
    global $DB;
    $textlib = textlib_get_instance();
    global $CFG;
    if ($game->toptext != '') {
        echo $game->toptext . '<br>';
    }
    echo '<br>';
    $cryptex = new CryptexDB();
    $language = $attempt->language;
    $questions = $cryptex->load($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->display($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) {
        $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;
            }
        }
        $questiontext = $q->questiontext;
        if ($q->questionid != 0) {
            $questiontext = game_filterquestion($questiontext, $q->questionid, $context->id, $game->course);
        } else {
            if ($contextglossary === false) {
                $cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course);
                $contextglossary = get_context_instance(CONTEXT_MODULE, $cmglossary->id);
            }
            $questiontext = game_filterglossary($questiontext, $q->glossaryentryid, $contextglossary->id, $game->course);
        }
        $question = "{$i}. " . $questiontext;
        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 . ');" />';
                $questiontext = str_replace(array("\n", "\r"), array(' ', ' '), $questiontext);
                if ($else != '') {
                    $else .= "\r\n            else ";
                }
                $else .= "if (id == {$q->id})\r\n                question=\"{$i}. {$questiontext}\";";
            }
        }
        $question = str_replace('\\"', '"', $question);
        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)
        {
            var question;
			    
            <?php 
    echo $else;
    ?>
			    
			    
            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 
}
function game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, $updateattempt = false, $onlyshow = false, $showsolution = false)
{
    $textlib = textlib_get_instance();
    global $CFG;
    if ($game->toptext != '') {
        echo $game->toptext . '<br>';
    }
    echo '<br>';
    $cryptex = new CryptexDB();
    $questions = $cryptex->load($crossm, $mask, $corrects);
    $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 ($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->display($crossm->cols, $crossm->rows, $cryptexrec->letters, $mask, $showsolution);
    ?>
</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;
    foreach ($questions as $key => $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 = game_filtertext($q->questiontext, 0);
        echo "{$i}. " . $question;
        $question = strip_tags($question);
        //ADDED BY DP (AUG 2009) - fixes " breaking the Answer button for this question
        $question = str_replace("'", "\\'", $question);
        if ($showsolution) {
            echo " &nbsp;&nbsp;&nbsp;{$q->answertext}<B></b>";
        } else {
            if ($onlyshow == false) {
                echo '<input type="submit" value="' . get_string('answer') . '" onclick="OnCheck( ' . $q->id . ',\'' . $question . '\');" />';
            }
        }
        echo "<br>\r\n";
    }
    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 
}