public function showhtml_base($crossm, $crossd, $showsolution, $showhtmlsolutions, $showstudentguess, $context, $game)
 {
     $this->mLegendh = array();
     $this->mLegendv = array();
     $sret = "CrosswordWidth  = {$crossm->cols};\n";
     $sret .= "CrosswordHeight = {$crossm->rows};\n";
     $sret .= "Words=" . count($crossd) . ";\n";
     $swordlength = "";
     $sguess = "";
     $ssolutions = '';
     $shtmlsolutions = '';
     $swordx = "";
     $swordy = "";
     $sclue = "";
     $lasthorizontalword = -1;
     $i = -1;
     $legendv = array();
     $legendh = array();
     if ($game->glossaryid) {
         $cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course);
         $contextglossary = game_get_context_module_instance($cmglossary->id);
     }
     foreach ($crossd as $rec) {
         if ($rec->horizontal == false and $lasthorizontalword == -1) {
             $lasthorizontalword = $i;
         }
         $i++;
         $swordlength .= "," . game_strlen($rec->answertext);
         if ($rec->questionid != 0) {
             $q = game_filterquestion(str_replace('\\"', '"', $rec->questiontext), $rec->questionid, $context->id, $game->course);
             $rec->questiontext = game_repairquestion($q);
         } else {
             // Glossary.
             $q = game_filterglossary(str_replace('\\"', '"', $rec->questiontext), $rec->glossaryentryid, $contextglossary->id, $game->course);
             $rec->questiontext = game_repairquestion($q);
         }
         $sclue .= ',"' . game_tojavascriptstring(game_filtertext($rec->questiontext, 0)) . "\"\r\n";
         if ($showstudentguess) {
             $sguess .= ',"' . $rec->studentanswer . '"';
         } else {
             $sguess .= ",''";
         }
         $swordx .= "," . ($rec->col - 1);
         $swordy .= "," . ($rec->row - 1);
         if ($showsolution) {
             $ssolutions .= ',"' . $rec->answertext . '"';
         } else {
             $ssolutions .= ',""';
         }
         if ($showhtmlsolutions) {
             $shtmlsolutions .= ',"' . base64_encode($rec->answertext) . '"';
         }
         $attachment = '';
         $s = $rec->questiontext . $attachment;
         if ($rec->horizontal) {
             if (array_key_exists($rec->row, $legendh)) {
                 $legendh[$rec->row][] = $s;
             } else {
                 $legendh[$rec->row] = array($s);
             }
         } else {
             if (array_key_exists($rec->col, $legendv)) {
                 $legendv[$rec->col][] = $s;
             } else {
                 $legendv[$rec->col] = array($s);
             }
         }
     }
     $letters = get_string('lettersall', 'game');
     $this->mlegendh = array();
     foreach ($legendh as $key => $value) {
         if (count($value) == 1) {
             $this->mlegendh[$key] = $value[0];
         } else {
             for ($i = 0; $i < count($value); $i++) {
                 $this->mlegendh[$key . game_substr($letters, $i, 1)] = $value[$i];
             }
         }
     }
     $this->mlegendv = array();
     foreach ($legendv as $key => $value) {
         if (count($value) == 1) {
             $this->mlegendv[$key] = $value[0];
         } else {
             for ($i = 0; $i < count($value); $i++) {
                 $this->mlegendv[$key . game_substr($letters, $i, 1)] = $value[$i];
             }
         }
     }
     ksort($this->mlegendh);
     ksort($this->mlegendv);
     $sret .= "WordLength = new Array( " . game_substr($swordlength, 1) . ");\n";
     $sret .= "Clue = new Array( " . game_substr($sclue, 1) . ");\n";
     $sguess = str_replace(' ', '_', $sguess);
     $sret .= "Guess = new Array( " . game_substr($sguess, 1) . ");\n";
     $sret .= "Solutions = new Array( " . game_substr($ssolutions, 1) . ");\n";
     if ($showhtmlsolutions) {
         $sret .= "HtmlSolutions = new Array( " . game_substr($shtmlsolutions, 1) . ");\n";
     }
     $sret .= "WordX = new Array( " . game_substr($swordx, 1) . ");\n";
     $sret .= "WordY = new Array( " . game_substr($swordy, 1) . ");\n";
     $sret .= "LastHorizontalWord = {$lasthorizontalword};\n";
     return $sret;
 }
function game_sudoku_check_number($id, $game, $attempt, $sudoku, $pos, $num, $context)
{
    global $DB;
    $correct = game_substr($sudoku->data, $pos - 1, 1);
    if ($correct != $num) {
        game_sudoku_play($id, $game, $attempt, $sudoku, false, false, $context);
        return;
    }
    $leng = game_strlen($sudoku->guess);
    $lend = game_strlen($sudoku->data);
    if ($leng < $lend) {
        $sudoku->guess .= str_repeat(' ', $lend - $leng);
    }
    game_setchar($sudoku->guess, $pos - 1, $correct);
    if (!$DB->set_field_select('game_sudoku', 'guess', $sudoku->guess, "id={$sudoku->id}")) {
        print_error('game_sudoku_check_number: Cannot update table game_sudoku');
    }
    game_sudoku_play($id, $game, $attempt, $sudoku, false, false, $context);
}
function game_setchar(&$s, $pos, $char)
{
    $ret = "";
    if ($pos > 0) {
        $ret .= game_substr($s, 0, $pos);
    }
    $s = $ret . $char . game_substr($s, $pos + 1);
}
Beispiel #4
0
function game_millionaire_onhelppeople($game, $id, &$millionaire, $query, $context)
{
    game_millionaire_loadquestions($game, $millionaire, $query, $aanswer, $context);
    if (($millionaire->state & 4) != 0) {
        game_millionaire_showgrid($game, $millionaire, $id, $query, $aanswer, '', $context);
        return;
    }
    game_millionaire_setstate($millionaire, 4);
    $n = count($aanswer);
    $sum = 0;
    $apercent = array();
    for ($i = 0; $i + 1 < $n; $i++) {
        $percent = mt_rand(0, 100 - $sum);
        $apercent[$i] = $percent;
        $sum += $percent;
    }
    $apercent[$n - 1] = 100 - $sum;
    if (mt_rand(1, 100) <= 80) {
        // With percent 80% sets in the correct answer the biggest percent.
        $maxpos = 0;
        for ($i = 1; $i + 1 < $n; $i++) {
            if ($apercent[$i] >= $apercent[$maxpos]) {
                $maxpos = $i;
            }
            $temp = $apercent[$maxpos];
            $apercent[$maxpos] = $apercent[$query->correct - 1];
            $apercent[$query->correct - 1] = $temp;
        }
    }
    $info = '<br>' . get_string('millionaire_info_people', 'game') . ':<br>';
    for ($i = 0; $i < $n; $i++) {
        $info .= "<br>" . game_substr(get_string('lettersall', 'game'), $i, 1) . " : " . $apercent[$i] . ' %';
    }
    game_millionaire_showgrid($game, $millionaire, $id, $query, $aanswer, game_substr($info, 4), $context);
}
function game_cross_unpackpuzzle($g)
{
    $ret = "";
    $len = game_strlen($g);
    while ($len) {
        for ($i = 0; $i < $len; $i++) {
            $c = game_substr($g, $i, 1);
            if ($c >= '1' and $c <= '9') {
                if ($i > 0) {
                    //found escape character
                    if (game_substr($g, $i - 1, 1) == '/') {
                        $g = game_substr($g, 0, $i - 1) . game_substr($g, $i);
                        $i--;
                        $len--;
                        continue;
                    }
                }
                break;
            }
        }
        if ($i < $len) {
            //found the start of a number
            for ($j = $i + 1; $j < $len; $j++) {
                $c = game_substr($g, $j, 1);
                if ($c < '0' or $c > '9') {
                    break;
                }
            }
            $count = game_substr($g, $i, $j - $i);
            $ret .= game_substr($g, 0, $i) . str_repeat('_', $count);
            $g = game_substr($g, $j);
            $len = game_strlen($g);
        } else {
            $ret .= $g;
            break;
        }
    }
    return $ret;
}
 public function loadcryptex($crossm, &$mask, &$corrects, &$language)
 {
     global $DB;
     $questions = array();
     $corrects = array();
     $mask = str_repeat('0', $crossm->cols * $crossm->rows);
     if ($recs = $DB->get_records('game_queries', array('attemptid' => $crossm->id))) {
         foreach ($recs as $rec) {
             if ($rec->questiontext == '') {
                 $rec->questiontext = ' ';
             }
             $key = $this->gethash($rec->questiontext) . '-' . $rec->answertext . '-' . $rec->id;
             $questions[$key] = $rec;
             $word = $rec->answertext;
             $pos = $crossm->cols * ($rec->row - 1) + ($rec->col - 1);
             $len = game_strlen($word);
             $found = $rec->answertext == $rec->studentanswer;
             for ($i = 0; $i < $len; $i++) {
                 $c = $found ? '1' : '2';
                 if (game_substr($mask, $pos, 1) != '1') {
                     game_setchar($mask, $pos, $c);
                 }
                 $pos += $rec->horizontal ? 1 : $crossm->cols;
             }
             if ($found) {
                 $corrects[$rec->id] = 1;
             }
             if ($language == '') {
                 $language = game_detectlanguage($rec->answertext);
             }
         }
         ksort($questions);
     }
     return $questions;
 }
 function updatecrossquestions(&$rec, &$g, &$pos, &$correctletters, &$wrongletters, &$restletters, $game, $attempt, $crossrec, $loadfromdb)
 {
     global $DB, $USER;
     $word = $rec->answertext;
     $len = game_strlen($word);
     if ($loadfromdb) {
         $guess = $rec->studentanswer;
     } else {
         $guess = game_substr($g, $pos, $len);
     }
     $len_guess = game_strlen($guess);
     $pos += $len;
     $is_empty = true;
     for ($i = 0; $i < $len; $i++) {
         if ($i < $len_guess) {
             $letterguess = game_substr($guess, $i, 1);
         } else {
             $letterguess = " ";
         }
         if ($letterguess != ' ') {
             $is_empty = false;
         }
         $letterword = game_substr($word, $i, 1);
         if ($letterword != $letterguess) {
             if ($letterguess != ' ' and $letterguess != '_') {
                 $wrongletters++;
             }
             game_setchar($guess, $i, '_');
             $restletters++;
         } else {
             if ($letterguess == ' ') {
                 if ($guess == $word) {
                     $correctletters++;
                 } else {
                     //$wrongletters++;
                     //game_setchar( $guess, $i, '_');
                 }
             } else {
                 $correctletters++;
             }
         }
     }
     if ($is_empty) {
         return;
     }
     if ($rec->studentanswer == $guess) {
         return;
     }
     $rec->studentanswer = $guess;
     $updrec = new stdClass();
     $updrec->studentanswer = $guess;
     $updrec->id = $rec->id;
     if (!$DB->update_record('game_queries', $updrec, $rec->id)) {
         print_error('Update game_queries: not updated');
     }
     $score = $correctletters / $len;
     game_update_queries($game, $attempt, $rec, $score, $guess);
 }
function game_millionaire_html_print($game, $questions, $maxquestions)
{
    $color1 = 'black';
    $color2 = 'DarkOrange';
    $colorback = "white";
    $stylequestion = "background:{$colorback};color:{$color1}";
    $stylequestionselected = "background:{$colorback};color:{$color2}";
    ?>

<body onload="Reset();">

<script type="text/javascript">

    //Millionaire for Moodle by Vasilis Daloukas    
    <?php 
    echo 'var questions = new Array(' . $questions . ");\r";
    ?>
    var current_question = 0;
    var level = 0;
    var posCorrect = 0;
    var infoCorrect = "";
    var flag5050 = 0;
    var flagTelephone = 0;
    var flagPeople = 0;
    var countQuestions = 0;
    var maxQuestions = <?php 
    echo $maxquestions;
    ?>
;
    
	function Highlite( ans)
	{	    
		document.getElementById( "btAnswer" + ans).style.backgroundColor = '<?php 
    echo $color2;
    ?>
';
	}

	function Restore( ans)
	{
		document.getElementById( "btAnswer" + ans).style.backgroundColor = '<?php 
    echo $colorback;
    ?>
';
	}
	
	function OnSelectAnswer( ans)
	{
	    if( posCorrect == ans)
	    {
	        if( level+1 > 15)
	        {
	            alert( "<?php 
    echo get_string('win', 'game');
    ?>
");
	            Reset();
	        }else
	        {
	            UpdateLevel( level+1);
	            SelectNextQuestion();
	        }
	    }else
	    {
	        OnGameOver( ans);
	    }
	}
	
    function OnGameOver( ans)
    {
        document.getElementById( "info").innerHTML = "<?php 
    echo get_string('millionaire_info_wrong_answer', 'game');
    ?>
 "+document.getElementById( "lblAnswer" + posCorrect).innerHTML;
        Highlite( posCorrect);
        Restore( ans);
        document.getElementById( "lblAnswer" + posCorrect).style.backgroundColor = '<?php 
    echo $color2;
    ?>
';
        
        alert( "<?php 
    echo strip_tags(get_string('hangman_loose', 'game'));
    ?>
");
       
        Restore( posCorrect); 
        document.getElementById( "lblAnswer" + posCorrect).style.backgroundColor = '<?php 
    echo $colorback;
    ?>
';
        
        Reset();
    }
    
	function UpdateLevel( newlevel)
	{
	    if( level > 0)
	    {
	        document.getElementById( "levela" + level).bgColor = "<?php 
    echo $colorback;
    ?>
";
    	    document.getElementById( "levelb" + level).bgColor = "<?php 
    echo $colorback;
    ?>
";
	        document.getElementById( "levelc" + level).bgColor = "<?php 
    echo $colorback;
    ?>
";
	        document.getElementById( "levela" + level).style.color = "<?php 
    echo $color1;
    ?>
";	    
	        document.getElementById( "levelb" + level).style.color = "<?php 
    echo $color1;
    ?>
";
	        document.getElementById( "levelc" + level).style.color = "<?php 
    echo $color1;
    ?>
";
	    }
	    
	    level = newlevel;

	    document.getElementById( "levela" + level).bgColor = "<?php 
    echo $color2;
    ?>
";
	    document.getElementById( "levelb" + level).bgColor = "<?php 
    echo $color2;
    ?>
";
	    document.getElementById( "levelc" + level).bgColor = "<?php 
    echo $color2;
    ?>
";
	    document.getElementById( "levela" + level).style.color = "<?php 
    echo $colorback;
    ?>
";	    
	    document.getElementById( "levelb" + level).style.color = "<?php 
    echo $colorback;
    ?>
";
	    document.getElementById( "levelc" + level).style.color = "<?php 
    echo $colorback;
    ?>
";
    }
	
	function OnHelp5050( ans)
	{
	    if( flag5050)
	        return;
	        
        document.getElementById( "Help5050").src = "5050x.png";
        flag5050 = 1;
        
        for(pos = posCorrect;pos == posCorrect;pos = 1+Math.floor(Math.random()*countQuestions));

        for( i=1; i <= countQuestions; i++)
        {   
            if( (i != pos) && (i != posCorrect))
            {         
                document.getElementById( "lblAnswer" + i).style.visibility = 'hidden';
        	    document.getElementById( "btAnswer" + i).style.visibility = 'hidden';
        	}
        }
	}
	
	function OnHelpTelephone( ans)
	{
	    if( flagTelephone)
	        return;
	    flagTelephone = 1;
        document.getElementById( "HelpTelephone").src = "telephonex.png";
	    
		if( countQuestions < 2){
			wrong = posCorrect;
		}else
		{
			for(;;)
			{
				wrong = 1+Math.floor(Math.random()*countQuestions);
				if( wrong != posCorrect)
					break;
			}
		}
		//with 80% gives the correct answer
		if( Math.random() <= 0.8)
			pos = posCorrect;
		else
			pos = wrong;
			
        info = "<?php 
    echo get_string('millionaire_info_telephone', 'game') . '<br><b>';
    ?>
 ";
        info += document.getElementById( "lblAnswer" + pos).innerHTML;
        document.getElementById( "info").innerHTML = info;
	}

	function OnHelpPeople( ans)
	{
        if( flagPeople)
	        return;
	    flagPeople = 1;
        document.getElementById( "HelpPeople").src = "peoplex.png";
        
        sum = 0;
        var aPercent = new Array();
        for( i = 0; i < countQuestions-1; i++)
        {
			percent = Math.floor(Math.random()*(100-sum));
			aPercent[ i] = percent;
			sum += percent;
        }
        aPercent[ countQuestions - 1] = 100 - sum;
        if( Math.random() <= 0.8)
        {
            //with percent 80% sets in the correct answer the biggest percent
            max_pos = 0;
            for( i=1; i < countQuestions; i++)
            {
                if( aPercent[ i] >= aPercent[ max_pos])
                    max_pos = i;
            }
            temp = aPercent[ max_pos];
            aPercent[ max_pos] = aPercent[ posCorrect-1];
            aPercent[ posCorrect-1] = temp;
        }
        
        var letters = "<?php 
    echo get_string('lettersall', 'game');
    ?>
";
        info = "<?php 
    echo '<br>' . get_string('millionaire_info_people', 'game') . ':<br>';
    ?>
";
        for( i=0; i < countQuestions; i++){
            info += "<br>" + letters.charAt( i) + " : " + aPercent[ i] + " %";
		}
	    
	    document.getElementById( "info").innerHTML = info;
	}

	function OnQuit( ans)
	{
	    Reset();
	}
	
	function Reset()
	{
	    for(i=1; i <= 15; i++)
	    {
	        document.getElementById( "levela" + i).bgColor = "<?php 
    echo $colorback;
    ?>
";
    	    document.getElementById( "levelb" + i).bgColor = "<?php 
    echo $colorback;
    ?>
";	    
	        document.getElementById( "levelc" + i).bgColor = "<?php 
    echo $colorback;
    ?>
";
	        document.getElementById( "levela" + i).style.color = "<?php 
    echo $color1;
    ?>
";
	        document.getElementById( "levelb" + i).style.color = "<?php 
    echo $color1;
    ?>
";
	        document.getElementById( "levelc" + i).style.color = "<?php 
    echo $color1;
    ?>
";
	    }
	    
        flag5050 = 0;
        flagTelephone = 0;
        flagPeople = 0;
        
        document.getElementById( "Help5050").src = "5050.png";
        document.getElementById( "HelpPeople").src = "people.png";
        document.getElementById( "HelpTelephone").src = "telephone.png";

	    document.getElementById( "info").innerHTML = "";
	    UpdateLevel( 1);
	    SelectNextQuestion();
	}

    function RandomizeAnswers( elements)
    {
        posCorrect = 1;
        countQuestions = elements.length-1;

        for( i=1; i <= countQuestions; i++)
        {
            pos = 1+Math.floor(Math.random()*countQuestions);
            if( posCorrect == i)
                posCorrect = pos;
            else if( posCorrect == pos)
                posCorrect = i;
                
            var temp = elements[ i];
            elements[ i] = elements[ pos];
            elements[ pos] = temp;
        }
    }
    
	function SelectNextQuestion()
	{   
	    current_question = Math.floor(Math.random()*questions.length);
	    question = Base64.decode( questions[ current_question]);
	    
	    var elements = new Array();
        elements = question.split('#');
        
        RandomizeAnswers( elements);

	    document.getElementById( "question").innerHTML = elements[ 0];
	    for( i=1; i < elements.length; i++)
	    {
    	    document.getElementById( "lblAnswer" + i).innerHTML = elements[ i];
    	    document.getElementById( "lblAnswer" + i).style.visibility = 'visible';
    	    document.getElementById( "btAnswer" + i).style.visibility = 'visible';
	    }
	    for( i=elements.length; i<= maxQuestions; i++)
	    {
    	    document.getElementById( "lblAnswer" + i).style.visibility = 'hidden';
    	    document.getElementById( "btAnswer" + i).style.visibility = 'hidden';
    	}
    	
    	document.getElementById( "info").innerHTML = "";
    }
    
/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	}, 
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
		
</script>



<table cellpadding=0 cellspacing=0 border=0>
<tr style='background:#408080'>
<td rowspan=<?php 
    echo 17 + $maxquestions;
    ?>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td colspan=6>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td rowspan=<?php 
    echo 17 + $maxquestions;
    ?>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>

<tr height=10%>
<td style='background:#408080' rowspan=3 colspan=2>
<input type="image"  name="Help5050" id="Help5050" Title="50 50" src="5050.png" alt="" border="0" onmousedown=OnHelp5050();>&nbsp;
<input type="image" name="HelpTelephone"  id="HelpTelephone" Title="<?php 
    echo get_string('millionaire_telephone', 'game');
    ?>
" src="telephone.png" alt="" border="0" onmousedown="OnHelpTelephone();">&nbsp;
<input type="image" name="HelpPeople"  id="HelpPeople" Title="<?php 
    echo get_string('millionaire_helppeople', 'game');
    ?>
" src="people.png" alt="" border="0" onmousedown="OnHelpPeople();">&nbsp;
<input type="image" name="Quit" id="Quit" Title="<?php 
    echo get_string('millionaire_quit', 'game');
    ?>
" src="x.png" alt="" border="0" onmousedown=OnQuit();>&nbsp;
</td>
<td rowspan=<?php 
    echo 16 + $maxquestions;
    ?>
 style='background:#408080'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td id="levela15" align=right>15</td>
<td id="levelb15">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td id="levelc15" align=right>    150000</td>
</tr>

<tr><td id="levela14" align=right>14</td>
<td id="levelb14"></td><td id="levelc14" align=right>       800000</td>
</tr>

<tr><td id="levela13" align=right>13</td>
<td id="levelb13"></td><td id="levelc13" align=right>       400000</td>
</tr>

<tr><td rowspan=12 colspan=2 valign=top style='background:<?php 
    echo $colorback;
    ?>
;color:<?php 
    echo $color1;
    ?>
'><div id="question">aa</div></td>
<td id="levela12" align=r0ight>12</div></td>
<td id="levelb12"></td><td id="levelc12" align=right>       200000</td>
</tr>

<tr><td id="levela11" align=right>11</td>
<td id="levelb11"></td><td id="levelc11" align=right>       10000</td>
</tr>

<tr><td id="levela10" align=right>10</td>
<td id="levelb10"></td><td id="levelc10" align=right>       5000</td>
</tr>

<tr><td id="levela9" align=right>9</td>
<td id="levelb9"></td><td id="levelc9" align=right>       4000</td>
</tr>

<tr><td id="levela8" align=right>8</td>
<td id="levelb8"></td><td id="levelc8" align=right>       2000</td>
</tr>

<tr><td id="levela7" align=right>7</td>
<td id="levelb7"></td><td id="levelc7" align=right>       1500</td>
</tr>

<tr><td id="levela6" align=right>6</td>
<td id="levelb6"></td><td id="levelc6" align=right>       1000</td>
</tr>

<tr><td id="levela5" align=right>5</td>
<td id="levelb5"></td><td id="levelc5" align=right>       500</td>
</tr>

<tr><td id="levela4" align=right>4</td>
<td id="levelb4"></td><td id="levelc4" align=right>       400</td>
</tr>

<tr><td id="levela3" align=right>3</td>
<td id="levelb3"></td><td id="levelc3" align=right>       300</td>
</tr>

<tr><td id="levela2" align=right>2</td>
<td id="levelb2"></td><td id="levelc2" align=right>       200</td>
</tr>

<tr><td id="levela1" align=right>1</td>
<td id="levelb1"></td><td id="levelc1" align=right>       100</td>
</tr>

<tr style='background:#408080'><td colspan=10>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>

<?php 
    $letters = get_string('lettersall', 'game');
    for ($i = 1; $i <= $maxquestions; $i++) {
        $s = game_substr($letters, $i - 1, 1);
        echo "<tr>\n";
        echo "<td style='background:{$colorback};color:{$color1}'>";
        echo "<input style=\"background:{$colorback};color:{$color1};\" type=\"submit\" name=\"btAnswer{$i}\" value=\"{$s}\" id=\"btAnswer{$i}\"";
        echo " onmouseover=\"Highlite( {$i});\" onmouseout=\"Restore( {$i});\"  onmousedown=\"OnSelectAnswer( {$i});\">";
        echo "</td>\n";
        echo "<td style=\"background:{$colorback};color:{$color1};\" width=100%> &nbsp; <span id=lblAnswer{$i} style=\"background:{$colorback};color:{$color1}\" onmouseover=\"Highlite({$i});\r \n\" onmouseout=\"Restore( {$i});\" onmousedown=\"OnSelectAnswer( {$i});\"></span></td>\n";
        if ($i == 1) {
            echo "<td style='background:#408080' rowspan=" . $maxquestions . " colspan=3><div id=\"info\"></div></td>\n";
        }
        echo "</tr>\n";
    }
    ?>

<tr><td colspan=10 style='background:#408080'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>

</table>


</body>
</html>
<?php 
}
    echo "var images = new Array({$images});\r";
}
?>

var to_guess = "";
var display_word = "";
var used_letters = "";
var wrong_guesses = 0;
var used_letters_all = "";
var all_letters = new Array(<?php 
$len = game_strlen($allletters);
for ($i = 0; $i < $len; $i++) {
    if ($i > 0) {
        echo ',';
    }
    echo '"' . game_substr($allletters, $i, 1) . '"';
}
?>
);

function selectLetter(l)
{
    if (can_play == false)
    {
    }

    if (used_letters.indexOf(l) != -1)
    {
        return;
    }
	
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);
}
Beispiel #11
0
function game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, $updateattempt = false, $onlyshow = false, $showsolution = false, $context, $print = false, $showhtmlprintbutton = true)
{
    global $CFG, $DB;
    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 = game_strlen($mask);
    // The count1 means there is a guested letter.
    // The count2 means there is a letter that not guessed.
    $count1 = $count2 = 0;
    for ($i = 0; $i < $len; $i++) {
        $c = game_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 
    $grade = round(100 * $gradeattempt);
    echo get_string('grade', 'game') . ' ' . $grade . ' %';
    echo '<br>';
    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 
    if ($showhtmlprintbutton) {
        echo '<br><button id="finishattemptbutton" type="button" onclick="OnEndGame();" >' . get_string('finish', 'game');
        echo '</button>';
        echo '<button id="printbutton" type="button" onclick="OnPrint();" >' . get_string('print', 'game');
        echo '</button><br>';
    }
    if ($showhtmlprintbutton) {
        ?>
<script>
    function PrintHtmlClick()
    {
    	document.getElementById("printbutton").style.display = "none";
    	
        window.print();     

    	document.getElementById("printbutton").style.display = "block";	
    }

    function OnPrint()
    {
<?php 
        global $CFG;
        $params = "id={$id}&gameid={$game->id}";
        echo "window.open( \"{$CFG->wwwroot}/mod/game/print.php?{$params}\");";
        ?>
    }

    function OnEndGame()
    {
<?php 
        global $CFG;
        $params = 'id=' . $id . '&action=cryptexcheck&g=&finishattempt=1';
        echo "window.location = \"{$CFG->wwwroot}/mod/game/attempt.php?{$params}\";\r\n";
        ?>
    }
</script>
<?php 
    }
    $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;
            }
        }
        $question = game_show_query($game, $q, "{$i}. " . $q->questiontext, $context);
        $question2 = strip_tags($question);
        // ADDED BY DP (AUG 2009) - fixes " breaking the Answer button for this question.
        echo "<script>var msg{$q->id}=" . json_encode($question2) . ';</script>';
        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 . ",msg{$q->id});\" />";
            }
        }
        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 
    if ($print) {
        echo '<body onload="window.print()">';
    } else {
        echo '<body>';
    }
}