@ -1,4 +1,19 @@
<?php // $Id: play.php,v 1.31 2012/07/25 11:16:05 bdaloukas Exp $
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see < http: / / www . gnu . org / licenses / > .
/**
/**
* This file plays the game millionaire
* This file plays the game millionaire
*
*
@ -7,22 +22,22 @@
* @package game
* @package game
**/
**/
function game_millionaire_continue( $id, $game, $attempt, $millionaire, $context)
function game_millionaire_continue( $id, $game, $attempt, $millionaire, $context) {
{
// User must select quiz or question as a source module.
//User must select quiz or question as a source module
if (($game->quizid == 0) and ($game->questioncategoryid == 0)) {
if( ($game->quizid == 0) and ($game->questioncategoryid == 0)){
if ($game->sourcemodule == 'quiz') {
if( $game->sourcemodule == 'quiz')
print_error( get_string( 'millionaire_must_select_quiz', 'game'));
print_error( get_string( 'millionaire_must_select_quiz', 'game'));
else
} else {
print_error( get_string( 'millionaire_must_select_questioncategory', 'game'));
print_error( get_string( 'millionaire_must_select_questioncategory', 'game'));
}
}
}
if( $attempt != false and $millionaire != false) {
if ($attempt != false and $millionaire != false) {
//continue an existing game
// Continue an existing game.
return game_millionaire_play( $id, $game, $attempt, $millionaire, $context);
return game_millionaire_play( $id, $game, $attempt, $millionaire, $context);
}
}
if( $attempt == false) {
if ($attempt == false) {
$attempt = game_addattempt( $game);
$attempt = game_addattempt( $game);
}
}
@ -32,33 +47,30 @@ function game_millionaire_continue( $id, $game, $attempt, $millionaire, $context
$newrec->level = 0;
$newrec->level = 0;
$newrec->state = 0;
$newrec->state = 0;
if( !game_insert_record( 'game_millionaire', $newrec)){
if ( !game_insert_record( 'game_millionaire', $newrec)) {
print_error( 'error inserting in game_millionaire');
print_error( 'error inserting in game_millionaire');
}
}
game_millionaire_play( $id, $game, $attempt, $newrec, $context);
game_millionaire_play( $id, $game, $attempt, $newrec, $context);
}
}
function game_millionaire_play( $id, $game, $attempt, $millionaire, $context) {
function game_millionaire_play( $id, $game, $attempt, $millionaire, $context)
{
global $DB;
global $DB;
$buttons = optional_param('buttons', 0, PARAM_INT);
$buttons = optional_param('buttons', 0, PARAM_INT);
$Help5050_ x = optional_param('Help5050_x', 0, PARAM_INT);
$help5050 x = optional_param('Help5050_x', 0, PARAM_INT);
$HelpTelephone_ x = optional_param('HelpTelephone_x', 0, PARAM_INT);
$helptelephone x = optional_param('HelpTelephone_x', 0, PARAM_INT);
$HelpPeople_ x = optional_param('HelpPeople_x', 0, PARAM_INT);
$helppeople x = optional_param('HelpPeople_x', 0, PARAM_INT);
$Quit_ x = optional_param('Quit_x', 0, PARAM_INT);
$quit x = optional_param('Quit_x', 0, PARAM_INT);
if( $millionaire->queryid) {
if ($millionaire->queryid) {
$query = $DB->get_record( 'game_queries', array( 'id' => $millionaire->queryid));
$query = $DB->get_record( 'game_queries', array( 'id' => $millionaire->queryid));
}else
} else {
{
$query = new StdClass;
$query = new StdClass;
}
}
$found = 0;
$found = 0;
for($i=1; $i < = $buttons; $i++){
for ($i = 1; $i < = $buttons; $i++) {
$name = 'btAnswer'.$i;
$name = 'btAnswer'.$i;
$answer = optional_param($name, '', PARAM_RAW);
$answer = optional_param($name, '', PARAM_RAW);
if (!empty($answer)) {
if (!empty($answer)) {
@ -67,40 +79,38 @@ function game_millionaire_play( $id, $game, $attempt, $millionaire, $context)
}
}
}
}
if( $found == 1)
if ($found == 1) {
;//nothing
$found = $found; // Nothing.
else if( !empty($Help5050_x))
} else if (!empty($help5050x)) {
game_millionaire_OnHelp5050( $game, $id, $millionaire, $game, $query, $context);
game_millionaire_OnHelp5050( $game, $id, $millionaire, $game, $query, $context);
else if( !empty($HelpTelephone_x))
} else if (!empty($helptelephonex)) {
game_millionaire_OnHelpTelephone( $game, $id, $millionaire, $query, $context);
game_millionaire_OnHelpTelephone( $game, $id, $millionaire, $query, $context);
else if( !empty($HelpPeople_x))
} else if (!empty($helppeoplex)) {
game_millionaire_OnHelpPeople( $game, $id, $millionaire, $query, $context);
game_millionaire_OnHelpPeople( $game, $id, $millionaire, $query, $context);
else if( !empty($Quit_x))
} else if (!empty($quitx)) {
game_millionaire_OnQuit( $id, $game, $attempt, $query, $context);
game_millionaire_OnQuit( $id, $game, $attempt, $query, $context);
else
} else {
game_millionaire_ShowNextQuestion( $id, $game, $attempt, $millionaire, $context);
game_millionaire_ShowNextQuestion( $id, $game, $attempt, $millionaire, $context);
}
}
}
function game_millionaire_showgrid( $game, $millionaire, $id, $query, $aanswer, $info, $context) {
function game_millionaire_showgrid( $game, $millionaire, $id, $query, $aAnswer, $info, $context)
global $CFG, $OUTPUT;
{
global $OUTPUT;
$question = str_replace( array("\'", '\"'), array("'", '"'), $query->questiontext);
$question = str_replace( array("\'", '\"'), array("'", '"'), $query->questiontext);
if( $game->param8 == '')
if ($game->param8 == '') {
$color = 408080;
$color = 408080;
else
} else {
$color = substr( '000000'.base_convert($game->param8, 10, 16), -6);
$color = substr( '000000'.base_convert($game->param8, 10, 16), -6);
}
$color1 = 'black';
$color1 = 'black';
$color2 = 'DarkOrange';
$color2 = 'DarkOrange';
$colorback= "white";
$colorback = "white";
$stylequestion = "background:$colorback;color:$color1";
$stylequestion = "background:$colorback;color:$color1";
$stylequestionselected = "background:$colorback;color:$color2";
$stylequestionselected = "background:$colorback;color:$color2";
global $CFG;
$state = $millionaire->state;
$state = $millionaire->state;
$level = $millionaire->level;
$level = $millionaire->level;
@ -109,78 +119,79 @@ function game_millionaire_showgrid( $game, $millionaire, $id, $query, $aAnswer,
echo '< form name = "Form1" method = "post" action = "attempt.php" id = "Form1" > ';
echo '< form name = "Form1" method = "post" action = "attempt.php" id = "Form1" > ';
echo "< table cellpadding = 0 cellspacing = 0 border = 0 > \r\n";
echo "< table cellpadding = 0 cellspacing = 0 border = 0 > \r\n";
echo "< tr $ background > ";
echo "< tr $ background > ";
echo '< td rowspan = '.(17+count( $aA nswer)).' > < / td > ';
echo '< td rowspan = '.(17 + count( $aa nswer)).' > < / td > ';
echo "< td colspan = 6 > < / td > ";
echo "< td colspan = 6 > < / td > ";
echo '< td rowspan = '.(17+count( $aA nswer)).' > < / td > ';
echo '< td rowspan = '.(17 + count( $aa nswer)).' > < / td > ';
echo "< / tr > \r\n";
echo "< / tr > \r\n";
echo "< tr height = 10% > ";
echo "< tr height = 10% > ";
echo "< td $ background rowspan = 3 colspan = 2 > ";
echo "< td $ background rowspan = 3 colspan = 2 > ";
$dirgif = 'millionaire/1/';
$dirgif = 'millionaire/1/';
if( $state & 1)
if ($state & 1) {
{
$gif = "5050x";
$gif = "5050x";
$disabled = "disabled=1";
$disabled = "disabled=1";
}else
} else {
{
$gif = "5050";
$gif = "5050";
$disabled = "";
$disabled = "";
}
}
echo '< input type = "image" ' . $ disabled . ' name = "Help5050" id = "Help5050" Title = "50 50" src = "'.$OUTPUT->pix_url($dirgif.$gif, 'mod_game').'" alt = "" border = "0" > ';
echo '< input type = "image" ' . $ disabled . ' name = "Help5050" id = "Help5050" Title = "50 50" src = "'.
$OUTPUT->pix_url($dirgif.$gif, 'mod_game').'" alt="" border="0"> ';
if( $state & 2)
if ($state & 2) {
{
$gif = "telephonex";
$gif = "telephonex";
$disabled = "disabled=1";
$disabled = "disabled=1";
}else
} else {
{
$gif = "telephone";
$gif = "telephone";
$disabled = "";
$disabled = "";
}
}
echo '< input type = "image" name = "HelpTelephone" ' . $ disabled . ' id = "HelpTelephone" Title = "'.get_string( 'millionaire_telephone', 'game').'" src = "'.$OUTPUT->pix_url($dirgif.$gif, 'mod_game').'" alt = "" border = "0" > ';
if( $state & 4)
echo '< input type = "image" name = "HelpTelephone" ' . $ disabled .
{
' id="HelpTelephone" Title="'.get_string( 'millionaire_telephone', 'game').
'" src="'.$OUTPUT->pix_url($dirgif.$gif, 'mod_game').'" alt="" border="0"> ';
if ($state & 4) {
$gif = "peoplex";
$gif = "peoplex";
$disabled = "disabled=1";
$disabled = "disabled=1";
}else
} else {
{
$gif = "people";
$gif = "people";
$disabled = "";
$disabled = "";
}
}
echo '< input type = "image" name = "HelpPeople" ' . $ disabled . ' id = "HelpPeople" Title = "'.get_string( 'millionaire_helppeople', 'game').'" src = "'.$OUTPUT->pix_url($dirgif.$gif, 'mod_game').'" alt = "" border = "0" > ';
echo '< input type = "image" name = "HelpPeople" ' . $ disabled . ' id = "HelpPeople" Title = "'.
get_string( 'millionaire_helppeople', 'game').'" src="'.
$OUTPUT->pix_url($dirgif.$gif, 'mod_game').'" alt="" border="0"> ';
echo '< input type = "image" name = "Quit" id = "Quit" Title = "'.get_string( 'millionaire_quit', 'game').'" src = "'.$OUTPUT->pix_url($dirgif.'x', 'mod_game').'" alt = "" border = "0" > ';
echo '< input type = "image" name = "Quit" id = "Quit" Title = "'.
get_string( 'millionaire_quit', 'game').'" src="'.
$OUTPUT->pix_url($dirgif.'x', 'mod_game').'" alt="" border="0"> ';
echo "\r\n";
echo "\r\n";
echo "< / td > \r\n";
echo "< / td > \r\n";
$styletext = "";
$styletext = "";
if( strpos( $question, 'color:') == false and strpos( $question, 'background:') == false){
if ( strpos( $question, 'color:') == false and strpos( $question, 'background:') == false) {
$styletext = "style='$stylequestion'";
$styletext = "style='$stylequestion'";
}
}
$aVal = array( 100, 200, 300, 400, 500, 1000, 1500, 2000, 4000, 5000, 10000, 20000, 40000, 80000, 150000);
$aval = array( 100, 200, 300, 400, 500, 1000, 1500, 2000, 4000, 5000, 10000, 20000, 40000, 80000, 150000);
for( $i=15; $i >= 1; $i--)
for ($i = 15; $i >= 1; $i--) {
{
$btr = false;
$bTR = false;
switch( $i)
switch ($i) {
{
case 15:
case 15:
echo "< td rowspan = ".(16+count( $aAnswer))." $ background > < / td > \r\n";
echo "< td rowspan = ".(16 + count ( $ aanswer ) ) .
$bTR = true;
"$background> < / td > \r\n";
$btr = true;
break;
break;
case 14:
case 14:
case 13:
case 13:
echo "< tr > \n";
echo "< tr > \n";
$bTR = true;
$btr = true;
break;
break;
case 12:
case 12:
//$question = game_filterquestion($question, $query->questionid, $context->id, $game->course);
$question = game_show_query( $game, $query, $question);
$question = game_show_query( $game, $query, $question);
echo "< tr > ";
echo "< tr > ";
echo "< td rowspan = 12 colspan = 2 valign = top style = \"$styletext\" > $question< / td > \r\n";
echo "< td rowspan = 12 colspan = 2 valign = top style = \"$styletext\" > $question< / td > \r\n";
$bTR = true;
$btr = true;
break;
break;
case 11:
case 11:
case 10:
case 10:
@ -194,65 +205,68 @@ function game_millionaire_showgrid( $game, $millionaire, $id, $query, $aAnswer,
case 2:
case 2:
case 1:
case 1:
echo "< tr > ";
echo "< tr > ";
$bTR = true;
$btr = true;
break;
break;
default:
default:
echo "< tr > ";
echo "< tr > ";
$bTR = true;
$btr = true;
}
}
if( $i == $level+1)
if ( $i == $level + 1) {
$style = "background:$color1;color:$color2";
$style = "background:$color1;color:$color2";
else
} else {
$style = $stylequestion;
$style = $stylequestion;
}
echo "< td style = '$style' align = right > $i< / td > ";
echo "< td style = '$style' align = right > $i< / td > ";
if( $i < $level+1)
if ( $i < $level + 1) {
echo "< td style = '$style' > * < / td > ";
echo "< td style = '$style' > * < / td > ";
else if( $i == 15 and $level < = 1)
} else if ( $i == 15 and $level < = 1) {
echo "< td style = '$style' > < / td > ";
echo "< td style = '$style' > < / td > ";
else
} else {
echo "< td style = '$style' > < / td > ";
echo "< td style = '$style' > < / td > ";
echo "< td style = '$style' align = right > ".sprintf( "%10d", $aVal[ $i-1])."< / td > \r\n";
}
if( $bTR)
echo "< td style = '$style' align = right > ".sprintf( "%10d", $aval[ $i - 1])."< / td > \r\n";
if ($btr) {
echo "< / tr > \r\n";
echo "< / tr > \r\n";
}
}
}
echo "< tr $ background > < td colspan = 10 > < / td > < / tr > \r\n";
echo "< tr $ background > < td colspan = 10 > < / td > < / tr > \r\n";
$bF irst = true;
$bf irst = true;
$letters = get_string( 'lettersall', 'game');
$letters = get_string( 'lettersall', 'game');
for( $i=1; $i < = count( $aAnswer); $i++)
for ($i = 1; $i < = count( $aanswer); $i++) {
{
$name = "btAnswer".$i;
$name = "btAnswer".$i;
$s = game_substr( $letters, $i- 1, 1);
$s = game_substr( $letters, $i - 1, 1);
$disabled = ( $state == 15 ? "disabled=1" : "");
$disabled = ( $state == 15 ? "disabled=1" : "");
$style = $stylequestion;
$style = $stylequestion;
if( (strpos( $aAnswer[ $i-1], 'color:') != false) or (strpos( $aAnswer[ $i-1], 'background:') != false)) {
if ((strpos( $aanswer[ $i - 1], 'color:') != false) or (strpos( $aanswer[ $i - 1], 'background:') != false)) {
$style = '';
$style = '';
}
}
if( $state == 15 and $i+1 == $query->correct) {
if ($state == 15 and $i + 1 == $query->correct) {
$style = $stylequestionselected;
$style = $stylequestionselected;
}
}
$button = '< input style = "'.$style.'" ' . $ disabled . ' type = "submit" name = "'.$name.'" value = "'.$s.'" id = "'.$name." 1 \ " " .
$button = '< input style = "'.$style.'" ' . $ disabled . ' type = "submit" name = "'.$name.'" value = "'.$s.'" id = "'.$name." 1 \ " " .
" onmouseover=\"this.style.backgroundColor = '$color2';$name.style.backgroundColor = '$color2';\" ".
" onmouseover=\"this.style.backgroundColor = '$color2';$name.style.backgroundColor = '$color2';\" ".
" onmouseout=\"this.style.backgroundColor = '$colorback';$name.style.backgroundColor = '$colorback';\" >";
" onmouseout=\"this.style.backgroundColor = '$colorback';$name.style.backgroundColor = '$colorback';\" >";
$text = game_filtertext($aAnswer[ $i-1], $game->course);
$text = game_filtertext($aanswer[ $i - 1], $game->course);
$answer = "< span id = $name style = \"$style\" " .
$answer = "< span id = $name style = \"$style\" " .
" onmouseover=\"this.style.backgroundColor = '$color2';{$name}1.style.backgroundColor = '$color2';\" ".
" onmouseover=\"this.style.backgroundColor = '$color2';{$name}1.style.backgroundColor = '$color2';\" ".
" onmouseout=\"this.style.backgroundColor = '$colorback';{$name}1.style.backgroundColor = '$colorback';\" >".
" onmouseout=\"this.style.backgroundColor = '$colorback';{$name}1.style.backgroundColor = '$colorback';\" >".
$text.'< / span > ';
$text.'< / span > ';
if( $aAnswer[ $i-1] != "") {
if ($aanswer[ $i - 1] != "") {
echo "< tr > \n";
echo "< tr > \n";
echo "< td style = '$stylequestion' > $button< / td > \n";
echo "< td style = '$stylequestion' > $button< / td > \n";
echo "< td $ style width = 100% > $answer< / td > ";
echo "< td $ style width = 100% > $answer< / td > ";
if( $bFirst) {
if ($bfirst) {
$bF irst = false;
$bf irst = false;
$info = game_filtertext($info, $game->course);
$info = game_filtertext($info, $game->course);
echo "< td style = \"$style\" rowspan = ".count( $aA nswer)." colspan = 3 > $info< / td > ";
echo "< td style = \"$style\" rowspan = ".count( $aa nswer)." colspan = 3 > $info< / td > ";
}
}
echo "\r\n< / tr > \r\n";
echo "\r\n< / tr > \r\n";
}
}
@ -260,69 +274,65 @@ function game_millionaire_showgrid( $game, $millionaire, $id, $query, $aAnswer,
echo "< tr > < td colspan = 10 $ background > < / td > < / tr > \r\n";
echo "< tr > < td colspan = 10 $ background > < / td > < / tr > \r\n";
echo "< input type = hidden name = state value = \"$state\" > \r\n";
echo "< input type = hidden name = state value = \"$state\" > \r\n";
echo '< input type = hidden name = id value = "'.$id.'" > ';
echo '< input type = hidden name = id value = "'.$id.'" > ';
echo "< input type = hidden name = buttons value = \"".count( $ aA nswer ) . " \ " > \r\n";
echo "< input type = hidden name = buttons value = \"".count( $ aa nswer ) . " \ " > \r\n";
echo "< / table > \r\n";
echo "< / table > \r\n";
echo "< / form > \r\n";
echo "< / form > \r\n";
}
}
function game_millionaire_ShowNextQuestion( $id, $game, $attempt, $millionaire, $context)
function game_millionaire_shownextquestion( $id, $game, $attempt, $millionaire, $context) {
{
game_millionaire_selectquestion( $aanswer, $game, $attempt, $millionaire, $query, $context);
game_millionaire_SelectQuestion( $aAnswer, $game, $attempt, $millionaire, $query, $context);
if( $game->toptext != '') {
if ($game->toptext != '') {
echo $game->toptext.'< br > < br > ';
echo $game->toptext.'< br > < br > ';
}
}
game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aA nswer, "", $context);
game_millionaire_showgrid( $game, $millionaire, $id, $query, $aa nswer, "", $context);
if( $game->bottomtext != '') {
if ($game->bottomtext != '') {
echo '< br > '.$game->bottomtext;
echo '< br > '.$game->bottomtext;
}
}
}
}
//updates tables: games_millionaire, game_attempts, game_questions
// Updates tables: games_millionaire, game_attempts, game_questions.
function game_millionaire_SelectQuestion( & $aAnswer, $game, $attempt, & $millionaire, & $query, $context)
function game_millionaire_selectquestion( & $aanswer, $game, $attempt, & $millionaire, & $query, $context) {
{
global $DB, $USER;
global $DB, $USER;
if( ($game->sourcemodule != 'quiz') and ($game->sourcemodule != 'question')){
if (($game->sourcemodule != 'quiz') and ($game->sourcemodule != 'question')) {
print_error( get_string('millionaire_sourcemodule_must_quiz_question', 'game', get_string( 'modulename', 'quiz')).' '.get_string( 'modulename', $attempt->sourcemodule));
print_error( get_string('millionaire_sourcemodule_must_quiz_question', 'game',
get_string( 'modulename', 'quiz')).' '.get_string( 'modulename', $attempt->sourcemodule));
}
}
if( $millionaire->queryid != 0) {
if ($millionaire->queryid != 0) {
game_millionaire_loadquestions( $game, $millionaire, $query, $aA nswer, $context);
game_millionaire_loadquestions( $game, $millionaire, $query, $aa nswer, $context);
return;
return;
}
}
if( $game->sourcemodule == 'quiz') {
if ($game->sourcemodule == 'quiz') {
if( $game->quizid == 0) {
if ($game->quizid == 0) {
print_error( get_string( 'must_select_quiz', 'game'));
print_error( get_string( 'must_select_quiz', 'game'));
}
}
if( game_get_moodle_version() < '02.07')
if (game_get_moodle_version() < '02.07') {
{
$select = "qtype='multichoice' AND quiz='$game->quizid' AND qmo.questionid=q.id".
$select = "qtype='multichoice' AND quiz='$game->quizid' AND qmo.questionid=q.id".
" AND qqi.question=q.id";
" AND qqi.question=q.id";
$table = "{quiz_question_instances} qqi,{question} q, {qtype_multichoice_options} qmo";
$table = "{quiz_question_instances} qqi,{question} q, {qtype_multichoice_options} qmo";
$order = '';
$order = '';
}else
} else {
{
$select = "qtype='multichoice' AND qs.quizid='$game->quizid' AND qmo.questionid=q.id".
$select = "qtype='multichoice' AND qs.quizid='$game->quizid' AND qmo.questionid=q.id".
" AND qs.questionid=q.id";
" AND qs.questionid=q.id";
$table = "{quiz_slots} qs,{question} q, {qtype_multichoice_options} qmo";
$table = "{quiz_slots} qs,{question} q, {qtype_multichoice_options} qmo";
$order = 'qs.page,qs.slot';
$order = 'qs.page,qs.slot';
}
}
}else
} else {
{
if ($game->questioncategoryid == 0) {
if( $game->questioncategoryid == 0){
print_error( get_string( 'must_select_questioncategory', 'game'));
print_error( get_string( 'must_select_questioncategory', 'game'));
}
}
//include subcategories
// Include subcategories.
$select = 'category='.$game->questioncategoryid;
$select = 'category='.$game->questioncategoryid;
if( $game->subcategories){
if ( $game->subcategories) {
$cats = question_categorylist( $game->questioncategoryid);
$cats = question_categorylist( $game->questioncategoryid);
if( count( $cats)){
if ( count( $cats)) {
$select = 'q.category in ('.implode(',', $cats).')';
$select = 'q.category in ('.implode(',', $cats).')';
}
}
}
}
@ -331,12 +341,13 @@ function game_millionaire_SelectQuestion( &$aAnswer, $game, $attempt, &$milliona
$table = '{question} q, {qtype_multichoice_options} qmo';
$table = '{question} q, {qtype_multichoice_options} qmo';
}
}
$select .= ' AND hidden=0';
$select .= ' AND hidden=0';
if( $game->shuffle or $game->quizid == 0)
if ($game->shuffle or $game->quizid == 0) {
$questionid = game_question_selectrandom( $game, $table, $select, 'q.id as id', true);
$questionid = game_question_selectrandom( $game, $table, $select, 'q.id as id', true);
else
} else {
$questionid = game_millionaire_select_serial_question( $game, $table, $select, 'q.id as id', $millionaire->level, $order);
$questionid = game_millionaire_select_serial_question( $game, $table, $select, 'q.id as id', $millionaire->level, $order);
}
if( $questionid == 0) {
if ($questionid == 0) {
print_error( get_string( 'no_questions', 'game'));
print_error( get_string( 'no_questions', 'game'));
}
}
@ -344,34 +355,34 @@ function game_millionaire_SelectQuestion( &$aAnswer, $game, $attempt, &$milliona
$recs = $DB->get_records( 'question_answers', array( 'question' => $questionid));
$recs = $DB->get_records( 'question_answers', array( 'question' => $questionid));
if( $recs === false) {
if ($recs === false) {
print_error( get_string( 'no_questions', 'game'));
print_error( get_string( 'no_questions', 'game'));
}
}
$correct = 0;
$correct = 0;
$ids = array();
$ids = array();
foreach( $recs as $rec) {
foreach ($recs as $rec) {
$aA nswer[] = game_filterquestion_answer(str_replace( '\"', '"', $rec->answer), $rec->id, $context->id, $game->course);
$aa nswer[] = game_filterquestion_answer(str_replace( '\"', '"', $rec->answer), $rec->id, $context->id, $game->course);
$ids[] = $rec->id;
$ids[] = $rec->id;
if( $rec->fraction == 1) {
if ($rec->fraction == 1) {
$correct = $rec->id;
$correct = $rec->id;
}
}
}
}
$count = count( $aA nswer);
$count = count( $aa nswer);
for($i= 1; $i < = $count; $i++){
for ($i = 1; $i < = $count; $i++) {
$sel = mt_rand(0, $count- 1);
$sel = mt_rand(0, $count - 1);
$temp = array_splice( $aA nswer, $sel, 1);
$temp = array_splice( $aa nswer, $sel, 1);
$aA nswer[ ] = $temp[ 0];
$aa nswer[ ] = $temp[ 0];
$temp = array_splice( $ids, $sel, 1);
$temp = array_splice( $ids, $sel, 1);
$ids[ ] = $temp[ 0];
$ids[ ] = $temp[ 0];
}
}
$query = new StdClass;
$query = new StdClass;
$query->attemptid = $attempt->id;
$query->attemptid = $attempt->id;
$query->gamekind = $game->gamekind;
$query->gamekind = $game->gamekind;
$query->gameid = $game->id;
$query->gameid = $game->id;
$query->userid = $USER->id;
$query->userid = $USER->id;
@ -381,8 +392,7 @@ function game_millionaire_SelectQuestion( &$aAnswer, $game, $attempt, &$milliona
$query->questiontext = addslashes( $q->questiontext);
$query->questiontext = addslashes( $q->questiontext);
$query->answertext = implode( ',', $ids);
$query->answertext = implode( ',', $ids);
$query->correct = array_search( $correct, $ids) + 1;
$query->correct = array_search( $correct, $ids) + 1;
if( !$query->id = $DB->insert_record( 'game_queries', $query)){
if (!$query->id = $DB->insert_record( 'game_queries', $query)) {
print_object( $query);
print_error( 'error inserting to game_queries');
print_error( 'error inserting to game_queries');
}
}
@ -390,7 +400,7 @@ function game_millionaire_SelectQuestion( &$aAnswer, $game, $attempt, &$milliona
$updrec->id = $millionaire->id;
$updrec->id = $millionaire->id;
$updrec->queryid = $query->id;
$updrec->queryid = $query->id;
if( !$newid = $DB->update_record( 'game_millionaire', $updrec)){
if ( !$newid = $DB->update_record( 'game_millionaire', $updrec)) {
print_error( 'error updating in game_millionaire');
print_error( 'error updating in game_millionaire');
}
}
@ -399,51 +409,53 @@ function game_millionaire_SelectQuestion( &$aAnswer, $game, $attempt, &$milliona
game_update_queries( $game, $attempt, $query, $score, '');
game_update_queries( $game, $attempt, $query, $score, '');
}
}
function game_millionaire_select_serial_question( $game, $table, $select, $id_fields="id", $level, $order)
function game_millionaire_select_serial_question( $game, $table, $select, $idfields = "id", $level, $order) {
{
global $DB, $USER;
global $DB, $USER;
$sql = "SELECT $id_ fields,$id_fields FROM ".$table." WHERE $select ";
$sql = "SELECT $idfields,$id_fields FROM ".$table." WHERE $select ";
if( $order != '')
if ( $order != '') {
$sql .= " ORDER BY $order";
$sql .= " ORDER BY $order";
}
if( ($recs = $DB->get_records_sql( $sql)) == false)
if ( ($recs = $DB->get_records_sql( $sql)) == false) {
return false;
return false;
}
$questions = array();
$questions = array();
foreach( $recs as $rec)
foreach ( $recs as $rec) {
$questions[] = $rec->id;
$questions[] = $rec->id;
}
$count = count( $questions);
$count = count( $questions);
if( $count == 0)
if ( $count == 0) {
return false;
return false;
}
$from = round($level * ($count-1) / 15);
$from = round($level * ($count - 1) / 15);
$to = round(max( $from, ($level+1) * ($count-1) / 15))-1;
$to = round(max( $from, ($level + 1) * ($count - 1) / 15)) - 1;
if( $to < $from)
if ( $to < $from) {
$to = $from;
$to = $from;
}
$pos = mt_rand( round( $from), round( $to));
$pos = mt_rand( round( $from), round( $to));
return $questions[ $pos];
return $questions[ $pos];
}
}
function game_millionaire_loadquestions( $game, $millionaire, & $query, & $aAnswer, $context)
function game_millionaire_loadquestions( $game, $millionaire, & $query, & $aanswer, $context) {
{
global $DB;
global $DB;
$query = $DB->get_record( 'game_queries', array( 'id' => $millionaire->queryid), 'id,questiontext,answertext,correct,questionid');
$query = $DB->get_record( 'game_queries', array( 'id' => $millionaire->queryid),
'id,questiontext,answertext,correct,questionid');
$aids = explode( ',', $query->answertext);
$aids = explode( ',', $query->answertext);
$aAnswer = array();
$aanswer = array();
foreach( $aids as $id)
foreach ($aids as $id) {
{
$rec = $DB->get_record( 'question_answers', array( 'id' => $id), 'id,answer');
$rec = $DB->get_record( 'question_answers', array( 'id' => $id), 'id,answer');
$aA nswer[] = game_filterquestion_answer(str_replace( '\"', '"', $rec->answer), $id, $context->id, $game->course);
$aa nswer[] = game_filterquestion_answer(str_replace( '\"', '"', $rec->answer), $id, $context->id, $game->course);
}
}
}
}
//flag 1:5050, 2:telephone 4:people
// Flag 1:5050, 2:telephone 4:people.
function game_millionaire_setstate( & $millionaire, $mask)
function game_millionaire_setstate( & $millionaire, $mask) {
{
global $DB;
global $DB;
$millionaire->state |= $mask;
$millionaire->state |= $mask;
@ -451,138 +463,124 @@ function game_millionaire_setstate( &$millionaire, $mask)
$updrec = new stdClass();
$updrec = new stdClass();
$updrec->id = $millionaire->id;
$updrec->id = $millionaire->id;
$updrec->state = $millionaire->state;
$updrec->state = $millionaire->state;
if( !$DB->update_record( 'game_millionaire', $updrec)){
if ( !$DB->update_record( 'game_millionaire', $updrec)) {
print_error( 'error updating in game_millionaire');
print_error( 'error updating in game_millionaire');
}
}
}
}
function game_millionaire_onhelp5050( $game, $id, & $millionaire, $query, $context) {
game_millionaire_loadquestions( $game, $millionaire, $query, $aanswer, $context);
function game_millionaire_onhelp5050( $game, $id, & $millionaire, $query, $context)
if (($millionaire->state & 1) != 0) {
{
game_millionaire_showgrid( $game, $millionaire, $id, $query, $aanswer, '', $context);
game_millionaire_loadquestions( $game, $millionaire, $query, $aAnswer, $context);
if( ($millionaire->state & 1) != 0)
{
game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aAnswer, '', $context);
return;
return;
}
}
game_millionaire_setstate( $millionaire, 1);
game_millionaire_setstate( $millionaire, 1);
$n = count( $aAnswer);
$n = count( $aanswer);
if( $n > 2)
if ($n > 2) {
{
for (;;) {
for(;;)
{
$wrong = mt_rand( 1, $n);
$wrong = mt_rand( 1, $n);
if( $wrong != $query->correct) {
if ($wrong != $query->correct) {
break;
break;
}
}
}
}
for( $i=1; $i < = $n; $i++)
for ($i = 1; $i < = $n; $i++) {
{
if ($i < > $wrong and $i < > $query->correct) {
if( $i < > $wrong and $i < > $query->correct){
$aanswer[ $i - 1] = "";
$aAnswer[ $i-1] = "";
}
}
}
}
}
}
game_millionaire_showgrid( $game, $millionaire, $id, $query, $aA nswer, '', $context);
game_millionaire_showgrid( $game, $millionaire, $id, $query, $aa nswer, '', $context);
}
}
function game_millionaire_OnHelpTelephone( $game, $id, & $millionaire, $query, $context)
function game_millionaire_onhelptelephone( $game, $id, & $millionaire, $query, $context) {
{
game_millionaire_loadquestions( $game, $millionaire, $query, $aanswer, $context);
game_millionaire_loadquestions( $game, $millionaire, $query, $aAnswer, $context);
if( ($millionaire->state & 2) != 0)
if (($millionaire->state & 2) != 0) {
{
game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aanswer, '', $context);
game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aAnswer, '', $context);
return;
return;
}
}
game_millionaire_setstate( $millionaire, 2);
game_millionaire_setstate( $millionaire, 2);
$n = count( $aA nswer);
$n = count( $aa nswer);
if( $n < 2 ) {
if ( $n < 2 ) {
$wrong = $query->correct;
$wrong = $query->correct;
}else
} else {
{
for (;;) {
for(;;)
{
$wrong = mt_rand( 1, $n);
$wrong = mt_rand( 1, $n);
if( $wrong != $query->correct)
if ($wrong != $query->correct) {
break;
break;
}
}
}
}
//with 80% gives the correct answer
}
if( mt_rand( 1, 10) < = 8)
// With 80% gives the correct answer.
if (mt_rand( 1, 10) < = 8) {
$response = $query->correct;
$response = $query->correct;
else
} else {
$response = $wrong;
$response = $wrong;
}
$info = get_string( 'millionaire_info_telephone','game').'< br > < b > '.$aAnswer[ $response- 1].'< / b > ';
$info = get_string( 'millionaire_info_telephone', 'game').'< br > < b > '.$aanswer[ $response - 1].'< / b > ';
game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aA nswer, $info, $context);
game_millionaire_showgrid( $game, $millionaire, $id, $query, $aa nswer, $info, $context);
}
}
function game_millionaire_OnHelpPeople( $game, $id, & $millionaire, $query, $context)
function game_millionaire_onhelppeople( $game, $id, & $millionaire, $query, $context) {
{
game_millionaire_loadquestions( $game, $millionaire, $query, $aanswer, $context);
game_millionaire_loadquestions( $game, $millionaire, $query, $aAnswer, $context);
if( ($millionaire->state & 4) != 0){
if ( ($millionaire->state & 4) != 0) {
game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aA nswer, '', $context);
game_millionaire_showgrid( $game, $millionaire, $id, $query, $aa nswer, '', $context);
return;
return;
}
}
game_millionaire_setstate( $millionaire, 4);
game_millionaire_setstate( $millionaire, 4);
$n = count( $aA nswer);
$n = count( $aa nswer);
$sum = 0;
$sum = 0;
$aPercent = array();
$apercent = array();
for( $i = 0; $i+1 < $n; $i++)
for ($i = 0; $i + 1 < $n; $i++) {
{
$percent = mt_rand( 0, 100 - $sum);
$percent = mt_rand( 0, 100-$sum);
$apercent[ $i] = $percent;
$aPercent[ $i] = $percent;
$sum += $percent;
$sum += $percent;
}
}
$aPercent[ $n-1] = 100 - $sum;
$apercent[ $n - 1] = 100 - $sum;
if( mt_rand( 1, 100) < = 80)
if (mt_rand( 1, 100) < = 80) {
{
// With percent 80% sets in the correct answer the biggest percent.
//with percent 80% sets in the correct answer the biggest percent
$maxpos = 0;
$max_pos = 0;
for ($i = 1; $i + 1 < $n; $i++) {
for( $i=1; $i+1 < $n; $i++)
if ($apercent[ $i] >= $apercent[ $maxpos]) {
{
$maxpos = $i;
if( $aPercent[ $i] >= $aPercent[ $max_pos])
$max_pos = $i;
}
}
$temp = $aPercent[ $max_ pos];
$temp = $apercent[ $maxpos];
$aPercent[ $max_pos] = $aPercent[ $query->correct- 1];
$apercent[ $maxpos] = $apercent[ $query->correct - 1];
$aPercent[ $query->correct- 1] = $temp;
$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);
$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_millionaire_OnAnswer( $id, $game, $attempt, & $millionaire, $query, $answer, $context)
function game_millionaire_onanswer( $id, $game, $attempt, & $millionaire, $query, $answer, $context) {
{
global $DB;
global $DB;
game_millionaire_loadquestions( $game, $millionaire, $query, $aAnswer, $context);
game_millionaire_loadquestions( $game, $millionaire, $query, $aanswer, $context);
if( $answer == $query->correct)
if ($answer == $query->correct) {
{
if ($millionaire->level < 15 ) {
if( $millionaire->level < 15 ) {
$millionaire->level++;
$millionaire->level++;
}
}
$finish = ($millionaire->level == 15 ? 1 : 0);
$finish = ($millionaire->level == 15 ? 1 : 0);
$scorequestion = 1;
$scorequestion = 1;
}else
} else {
{
$finish = 1;
$finish = 1;
$scorequestion = 0;
$scorequestion = 0;
}
}
@ -596,35 +594,30 @@ function game_millionaire_onhelp5050( $game, $id, &$millionaire, $query, $conte
$updrec->id = $millionaire->id;
$updrec->id = $millionaire->id;
$updrec->level = $millionaire->level;
$updrec->level = $millionaire->level;
$updrec->queryid = 0;
$updrec->queryid = 0;
if( !$DB->update_record( 'game_millionaire', $updrec)){
if ( !$DB->update_record( 'game_millionaire', $updrec)) {
print_error( 'error updating in game_millionaire');
print_error( 'error updating in game_millionaire');
}
}
if( $answer == $query->correct)
if ($answer == $query->correct) {
{
// Correct.
//correct
if ($finish) {
if( $finish){
echo get_string( 'win', 'game');
echo get_string( 'win', 'game');
game_millionaire_OnQuit( $id, $game, $attempt, $query);
game_millionaire_OnQuit( $id, $game, $attempt, $query);
}else
} else {
{
$millionaire->queryid = 0; // So the next function select a new question.
$millionaire->queryid = 0; //so the next function select a new question
game_millionaire_ShowNextQuestion( $id, $game, $attempt, $millionaire, $context);
}
}
}else
game_millionaire_ShowNextQuestion( $id, $game, $attempt, $millionaire, $context);
{
} else {
//wrong answer
// Wrong answer.
$info = get_string( 'millionaire_info_wrong_answer', 'game').
$info = get_string( 'millionaire_info_wrong_answer', 'game').
'< br > < br > < b > < center > '.$aAnswer[ $query->correct- 1].'< / b > ';
'< br > < br > < b > < center > '.$aanswer[ $query->correct - 1].'< / b > ';
$millionaire->state = 15;
$millionaire->state = 15;
game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aAnswer, $info, $context);
game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aanswer, $info, $context);
}
}
}
}
function game_millionaire_onquit( $id, $game, $attempt, $query)
function game_millionaire_onquit( $id, $game, $attempt, $query) {
{
global $CFG, $DB;
global $CFG, $DB;
game_updateattempts( $game, $attempt, -1, true);
game_updateattempts( $game, $attempt, -1, true);
@ -634,7 +627,7 @@ function game_millionaire_onhelp5050( $game, $id, &$millionaire, $query, $conte
}
}
echo '< br > ';
echo '< br > ';
echo "< a href = \"{$CFG- > wwwroot}/mod/game/attempt.php?id=$id\">".get_string( 'nextgame', 'game').'< / a > ';
echo "< a href = \"{$CFG- > wwwroot}/mod/game/attempt.php?id=$id\">".
get_string( 'nextgame', 'game').'< / a > ';
echo "< a href = \"{$CFG- > wwwroot}/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'< / a > ';
echo "< a href = \"{$CFG- > wwwroot}/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'< / a > ';
}
}