|
@ -1,53 +1,64 @@ |
|
|
<?php // $Id: play.php,v 1.27 2012/07/25 11:16:05 bdaloukas Exp $
|
|
|
<?php |
|
|
|
|
|
// This file is part of Moodle - http://moodle.org/ |
|
|
// This file plays the game hangman |
|
|
// |
|
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify |
|
|
function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $action, $context) |
|
|
// 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 hangman. |
|
|
|
|
|
|
|
|
|
|
|
function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $action, $context) { |
|
|
global $DB, $USER; |
|
|
global $DB, $USER; |
|
|
|
|
|
|
|
|
if ($attempt != false and $hangman != false) { |
|
|
if ($attempt != false and $hangman != false) { |
|
|
if (($action == 'nextword') and ($hangman->finishedword != 0)) { |
|
|
if (($action == 'nextword') and ($hangman->finishedword != 0)) { |
|
|
//finish with one word and continue to another |
|
|
// Finish with one word and continue to another. |
|
|
if (!$DB->set_field( 'game_hangman', 'finishedword', 0, array( 'id' => $hangman->id))) { |
|
|
if (!$DB->set_field( 'game_hangman', 'finishedword', 0, array( 'id' => $hangman->id))) { |
|
|
error( "game_hangman_continue: Can't update game_hangman"); |
|
|
error( "game_hangman_continue: Can't update game_hangman"); |
|
|
} |
|
|
} |
|
|
}else |
|
|
} else { |
|
|
{ |
|
|
|
|
|
return game_hangman_play( $id, $game, $attempt, $hangman, false, false, $context); |
|
|
return game_hangman_play( $id, $game, $attempt, $hangman, false, false, $context); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$updatehangman = (($attempt != false) and ($hangman != false)); |
|
|
$updatehangman = (($attempt != false) and ($hangman != false)); |
|
|
|
|
|
|
|
|
//new game |
|
|
// New game. |
|
|
srand ((double)microtime() * 1000003); |
|
|
srand ((double)microtime() * 1000003); |
|
|
|
|
|
|
|
|
//I try 10 times to find a new question |
|
|
// I try 10 times to find a new question. |
|
|
$found = false; |
|
|
$found = false; |
|
|
$min_num = 0; |
|
|
$minnum = 0; |
|
|
$unchanged = 0; |
|
|
$unchanged = 0; |
|
|
for($i=1; $i <= 10; $i++) |
|
|
for ($i = 1; $i <= 10; $i++) { |
|
|
{ |
|
|
|
|
|
$rec = game_question_shortanswer( $game, $game->param7, false); |
|
|
$rec = game_question_shortanswer( $game, $game->param7, false); |
|
|
if ($rec === false) { |
|
|
if ($rec === false) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$answer = game_upper( $rec->answertext, $game->language); |
|
|
$answer = game_upper( $rec->answertext, $game->language); |
|
|
if( $game->language == '') |
|
|
if ($game->language == '') { |
|
|
{ |
|
|
|
|
|
$game->language = game_detectlanguage( $answer); |
|
|
$game->language = game_detectlanguage( $answer); |
|
|
$answer = game_upper( $rec->answertext, $game->language); |
|
|
$answer = game_upper( $rec->answertext, $game->language); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$answer2 = $answer; |
|
|
$answer2 = $answer; |
|
|
if ($game->param7) { |
|
|
if ($game->param7) { |
|
|
//Have to delete space |
|
|
// Have to delete space. |
|
|
$answer2 = str_replace( ' ', '', $answer2); |
|
|
$answer2 = str_replace( ' ', '', $answer2); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ($game->param8) { |
|
|
if ($game->param8) { |
|
|
//Have to delete - |
|
|
// Have to delete -. |
|
|
$answer2 = str_replace( '-', '', $answer2); |
|
|
$answer2 = str_replace( '-', '', $answer2); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -60,12 +71,13 @@ function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $act |
|
|
if ($game->param7) { |
|
|
if ($game->param7) { |
|
|
$allletters .= '_'; |
|
|
$allletters .= '_'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ($game->param8) { |
|
|
if ($game->param8) { |
|
|
$allletters .= '-'; |
|
|
$allletters .= '-'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ($game->param7 == false) { |
|
|
if ($game->param7 == false) { |
|
|
//I don't allow spaces |
|
|
// I don't allow spaces. |
|
|
if (strpos( $answer, " ")) { |
|
|
if (strpos( $answer, " ")) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
@ -73,14 +85,14 @@ function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $act |
|
|
|
|
|
|
|
|
$copy = false; |
|
|
$copy = false; |
|
|
$select2 = 'gameid=? AND userid=? AND questionid=? AND glossaryentryid=?'; |
|
|
$select2 = 'gameid=? AND userid=? AND questionid=? AND glossaryentryid=?'; |
|
|
if( ($rec2 = $DB->get_record_select( 'game_repetitions', $select2, array( $game->id, $USER->id, $rec->questionid, $rec->glossaryentryid), 'id,repetitions AS r')) != false){ |
|
|
if (($rec2 = $DB->get_record_select( 'game_repetitions', $select2, |
|
|
if( ($rec2->r < $min_num) or ($min_num == 0)){ |
|
|
array( $game->id, $USER->id, $rec->questionid, $rec->glossaryentryid), 'id,repetitions AS r')) != false) { |
|
|
$min_num = $rec2->r; |
|
|
if (($rec2->r < $minnum) or ($minnum == 0)) { |
|
|
|
|
|
$minnum = $rec2->r; |
|
|
$copy = true; |
|
|
$copy = true; |
|
|
} |
|
|
} |
|
|
}else |
|
|
} else { |
|
|
{ |
|
|
$minnum = 0; |
|
|
$min_num = 0; |
|
|
|
|
|
$copy = true; |
|
|
$copy = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -96,26 +108,29 @@ function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $act |
|
|
$min->answer = $answer; |
|
|
$min->answer = $answer; |
|
|
$min->language = $game->language; |
|
|
$min->language = $game->language; |
|
|
$min->allletters = $allletters; |
|
|
$min->allletters = $allletters; |
|
|
if( $min_num == 0) |
|
|
if ($minnum == 0) { |
|
|
break; //We found an unused word |
|
|
break; // We found an unused word. |
|
|
}else |
|
|
} |
|
|
|
|
|
} else { |
|
|
$unchanged++; |
|
|
$unchanged++; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if( $unchanged > 2) |
|
|
if ($unchanged > 2) { |
|
|
{ |
|
|
if ($found) { |
|
|
if( $found) |
|
|
|
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if ($found == false) { |
|
|
if ($found == false) { |
|
|
print_error( get_string( 'no_words', 'game')); |
|
|
print_error( get_string( 'no_words', 'game')); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Found one word for hangman |
|
|
// Found one word for hangman. |
|
|
if ($attempt == false) { |
|
|
if ($attempt == false) { |
|
|
$attempt = game_addattempt( $game); |
|
|
$attempt = game_addattempt( $game); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!$DB->set_field( 'game_attempts', 'language', $min->language, array( 'id' => $attempt->id))) { |
|
|
if (!$DB->set_field( 'game_attempts', 'language', $min->language, array( 'id' => $attempt->id))) { |
|
|
print_error( "game_hangman_continue: Can't set language"); |
|
|
print_error( "game_hangman_continue: Can't set language"); |
|
|
} |
|
|
} |
|
@ -134,7 +149,6 @@ function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $act |
|
|
$query->answertext = $min->answer; |
|
|
$query->answertext = $min->answer; |
|
|
$query->answerid = $min->answerid; |
|
|
$query->answerid = $min->answerid; |
|
|
if (!($query->id = $DB->insert_record( 'game_queries', $query))) { |
|
|
if (!($query->id = $DB->insert_record( 'game_queries', $query))) { |
|
|
print_object( $query); |
|
|
|
|
|
print_error( "game_hangman_continue: Can't insert to table game_queries"); |
|
|
print_error( "game_hangman_continue: Can't insert to table game_queries"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -156,6 +170,7 @@ function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $act |
|
|
if ($game->param1) { |
|
|
if ($game->param1) { |
|
|
$letters .= game_substr( $min->answer, 0, 1); |
|
|
$letters .= game_substr( $min->answer, 0, 1); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ($game->param2) { |
|
|
if ($game->param2) { |
|
|
$letters .= game_substr( $min->answer, -1, 1); |
|
|
$letters .= game_substr( $min->answer, -1, 1); |
|
|
} |
|
|
} |
|
@ -165,8 +180,7 @@ function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $act |
|
|
if (!game_insert_record( 'game_hangman', $newrec)) { |
|
|
if (!game_insert_record( 'game_hangman', $newrec)) { |
|
|
print_error( 'game_hangman_continue: error inserting in game_hangman'); |
|
|
print_error( 'game_hangman_continue: error inserting in game_hangman'); |
|
|
} |
|
|
} |
|
|
}else |
|
|
} else { |
|
|
{ |
|
|
|
|
|
if (!$DB->update_record( 'game_hangman', $newrec)) { |
|
|
if (!$DB->update_record( 'game_hangman', $newrec)) { |
|
|
print_error( 'game_hangman_continue: error updating in game_hangman'); |
|
|
print_error( 'game_hangman_continue: error updating in game_hangman'); |
|
|
} |
|
|
} |
|
@ -178,8 +192,7 @@ function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $act |
|
|
game_hangman_play( $id, $game, $attempt, $newrec, false, false, $context); |
|
|
game_hangman_play( $id, $game, $attempt, $newrec, false, false, $context); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function game_hangman_onfinishgame( $game, $attempt, $hangman) |
|
|
function game_hangman_onfinishgame( $game, $attempt, $hangman) { |
|
|
{ |
|
|
|
|
|
global $DB; |
|
|
global $DB; |
|
|
|
|
|
|
|
|
$score = $hangman->corrects / $hangman->maxtries; |
|
|
$score = $hangman->corrects / $hangman->maxtries; |
|
@ -191,29 +204,30 @@ function game_hangman_onfinishgame( $game, $attempt, $hangman) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function game_hangman_play( $id, $game, $attempt, $hangman, $onlyshow, $showsolution, $context) |
|
|
function game_hangman_play( $id, $game, $attempt, $hangman, $onlyshow, $showsolution, $context) { |
|
|
{ |
|
|
|
|
|
global $CFG, $DB, $OUTPUT; |
|
|
global $CFG, $DB, $OUTPUT; |
|
|
|
|
|
|
|
|
$query = $DB->get_record( 'game_queries', array( 'id' => $hangman->queryid)); |
|
|
$query = $DB->get_record( 'game_queries', array( 'id' => $hangman->queryid)); |
|
|
|
|
|
|
|
|
if( $attempt->language != '') |
|
|
if ($attempt->language != '') { |
|
|
$wordrtl = game_right_to_left( $attempt->language); |
|
|
$wordrtl = game_right_to_left( $attempt->language); |
|
|
else |
|
|
} else { |
|
|
$wordrtl = right_to_left(); |
|
|
$wordrtl = right_to_left(); |
|
|
|
|
|
} |
|
|
$reverseprint = ($wordrtl != right_to_left()); |
|
|
$reverseprint = ($wordrtl != right_to_left()); |
|
|
|
|
|
|
|
|
if ($game->toptext != '') { |
|
|
if ($game->toptext != '') { |
|
|
echo $game->toptext.'<br>'; |
|
|
echo $game->toptext.'<br>'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$max=$game->param10; // maximum number of wrong |
|
|
$max = $game->param10; // Maximum number of wrongs. |
|
|
if( $max <= 0) |
|
|
if ($max <= 0) { |
|
|
$max = 6; |
|
|
$max = 6; |
|
|
hangman_showpage( $done, $correct, $wrong, $max, $word_line, $word_line2, $links, $game, $attempt, $hangman, $query, $onlyshow, $showsolution, $context); |
|
|
} |
|
|
|
|
|
hangman_showpage( $done, $correct, $wrong, $max, $wordline, $wordline2, $links, $game, |
|
|
|
|
|
$attempt, $hangman, $query, $onlyshow, $showsolution, $context); |
|
|
|
|
|
|
|
|
if (!$done) |
|
|
if (!$done) { |
|
|
{ |
|
|
|
|
|
if ($wrong > $max) { |
|
|
if ($wrong > $max) { |
|
|
$wrong = $max; |
|
|
$wrong = $max; |
|
|
} |
|
|
} |
|
@ -225,24 +239,22 @@ function game_hangman_play( $id, $game, $attempt, $hangman, $onlyshow, $showsolu |
|
|
echo ' ALIGN="MIDDLE" BORDER="0" HEIGHT="100" alt="'.$message.'"/>'; |
|
|
echo ' ALIGN="MIDDLE" BORDER="0" HEIGHT="100" alt="'.$message.'"/>'; |
|
|
|
|
|
|
|
|
if ($wrong >= $max) { |
|
|
if ($wrong >= $max) { |
|
|
|
|
|
// This word is incorrect. If reach the max number of word I have to finish else continue with next word. |
|
|
//This word is incorrect. If reach the max number of word I have to finish else continue with next word |
|
|
hangman_onincorrect( $id, $wordline, $query->answertext, $game, $attempt, $hangman, $onlyshow, $showsolution); |
|
|
hangman_onincorrect( $id, $word_line, $query->answertext, $game, $attempt, $hangman, $onlyshow, $showsolution); |
|
|
} else { |
|
|
}else |
|
|
|
|
|
{ |
|
|
|
|
|
$i = $max - $wrong; |
|
|
$i = $max - $wrong; |
|
|
if( $i > 1) |
|
|
if ($i > 1) { |
|
|
echo ' '.get_string( 'hangman_restletters_many', 'game', $i); |
|
|
echo ' '.get_string( 'hangman_restletters_many', 'game', $i); |
|
|
else |
|
|
} else { |
|
|
echo ' '.get_string( 'hangman_restletters_one', 'game'); |
|
|
echo ' '.get_string( 'hangman_restletters_one', 'game'); |
|
|
|
|
|
} |
|
|
if ($reverseprint) { |
|
|
if ($reverseprint) { |
|
|
echo '<SPAN dir="'.($wordrtl ? 'rtl' : 'ltr').'">'; |
|
|
echo '<SPAN dir="'.($wordrtl ? 'rtl' : 'ltr').'">'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
echo "<br/><font size=\"5\">\n$word_line</font>\r\n"; |
|
|
echo "<br/><font size=\"5\">\n$wordline</font>\r\n"; |
|
|
if( $word_line2 != ''){ |
|
|
if ($wordline2 != '') { |
|
|
echo "<br/><font size=\"5\">\n$word_line2</font>\r\n"; |
|
|
echo "<br/><font size=\"5\">\n$wordline2</font>\r\n"; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ($reverseprint) { |
|
|
if ($reverseprint) { |
|
@ -253,15 +265,15 @@ function game_hangman_play( $id, $game, $attempt, $hangman, $onlyshow, $showsolu |
|
|
echo "<br/><br/><BR/>".get_string( 'hangman_letters', 'game').$links."\r\n"; |
|
|
echo "<br/><br/><BR/>".get_string( 'hangman_letters', 'game').$links."\r\n"; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}else |
|
|
} else { |
|
|
{ |
|
|
// This word is correct. If reach the max number of word I have to finish else continue with next word. |
|
|
//This word is correct. If reach the max number of word I have to finish else continue with next word |
|
|
hangman_oncorrect( $id, $wordline, $game, $attempt, $hangman, $query); |
|
|
hangman_oncorrect( $id, $word_line, $game, $attempt, $hangman, $query); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
echo "<br/><br/>".get_string( 'grade', 'game').' : '.round( $query->percent * 100).' %'; |
|
|
echo "<br/><br/>".get_string( 'grade', 'game').' : '.round( $query->percent * 100).' %'; |
|
|
if ($hangman->maxtries > 1) { |
|
|
if ($hangman->maxtries > 1) { |
|
|
echo '<br/><br/>'.get_string( 'hangman_gradeinstance', 'game').' : '.round( $hangman->corrects / $hangman->maxtries * 100).' %'; |
|
|
echo '<br/><br/>'.get_string( 'hangman_gradeinstance', 'game').' : '. |
|
|
|
|
|
round( $hangman->corrects / $hangman->maxtries * 100).' %'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ($game->bottomtext != '') { |
|
|
if ($game->bottomtext != '') { |
|
@ -269,11 +281,11 @@ function game_hangman_play( $id, $game, $attempt, $hangman, $onlyshow, $showsolu |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function hangman_showpage(&$done, &$correct, &$wrong, $max, &$word_line, &$word_line2, &$links, $game, &$attempt, &$hangman, &$query, $onlyshow, $showsolution, $context) |
|
|
function hangman_showpage(&$done, &$correct, &$wrong, $max, &$wordline, &$wordline2, &$links, |
|
|
{ |
|
|
$game, &$attempt, &$hangman, &$query, $onlyshow, $showsolution, $context) { |
|
|
global $USER, $CFG, $DB; |
|
|
global $USER, $CFG, $DB; |
|
|
|
|
|
|
|
|
$id = optional_param('id', 0, PARAM_INT); // Course Module ID, or |
|
|
$id = optional_param('id', 0, PARAM_INT); // Course Module ID. |
|
|
|
|
|
|
|
|
$word = $query->answertext; |
|
|
$word = $query->answertext; |
|
|
|
|
|
|
|
@ -283,8 +295,7 @@ function hangman_showpage(&$done, &$correct, &$wrong, $max, &$word_line, &$word_ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$letters = $hangman->letters; |
|
|
$letters = $hangman->letters; |
|
|
if( $newletter != NULL) |
|
|
if ($newletter != null) { |
|
|
{ |
|
|
|
|
|
if (game_strpos( $letters, $newletter) === false) { |
|
|
if (game_strpos( $letters, $newletter) === false) { |
|
|
$letters .= $newletter; |
|
|
$letters .= $newletter; |
|
|
} |
|
|
} |
|
@ -295,15 +306,14 @@ function hangman_showpage(&$done, &$correct, &$wrong, $max, &$word_line, &$word_ |
|
|
$alpha = $hangman->allletters; |
|
|
$alpha = $hangman->allletters; |
|
|
$wrong = 0; |
|
|
$wrong = 0; |
|
|
|
|
|
|
|
|
if( $query->questionid) |
|
|
if ($query->questionid) { |
|
|
{ |
|
|
|
|
|
$questiontext = str_replace( array("\'", '\"'), array("'", '"'), $query->questiontext); |
|
|
$questiontext = str_replace( array("\'", '\"'), array("'", '"'), $query->questiontext); |
|
|
$query->questiontext = game_filterquestion($questiontext, $query->questionid, $context->id, $game->course); |
|
|
$query->questiontext = game_filterquestion($questiontext, $query->questionid, $context->id, $game->course); |
|
|
}else |
|
|
} else { |
|
|
{ |
|
|
|
|
|
$cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course); |
|
|
$cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course); |
|
|
$contextglossary = game_get_context_module_instance( $cmglossary->id); |
|
|
$contextglossary = game_get_context_module_instance( $cmglossary->id); |
|
|
$query->questiontext = game_filterglossary(str_replace( '\"', '"', $query->questiontext), $query->glossaryentryid, $contextglossary->id, $game->course); |
|
|
$query->questiontext = game_filterglossary(str_replace( '\"', '"', |
|
|
|
|
|
$query->questiontext), $query->glossaryentryid, $contextglossary->id, $game->course); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ($game->param5) { |
|
|
if ($game->param5) { |
|
@ -318,60 +328,53 @@ function hangman_showpage(&$done, &$correct, &$wrong, $max, &$word_line, &$word_ |
|
|
echo "<br/><br/>"; |
|
|
echo "<br/><br/>"; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$word_line = $word_line2 = ""; |
|
|
$wordline = $wordline2 = ""; |
|
|
|
|
|
|
|
|
$len = game_strlen( $word); |
|
|
$len = game_strlen( $word); |
|
|
|
|
|
|
|
|
$done = 1; |
|
|
$done = 1; |
|
|
$answer = ''; |
|
|
$answer = ''; |
|
|
$correct = 0; |
|
|
$correct = 0; |
|
|
for ($x=0; $x < $len; $x++) |
|
|
for ($x = 0; $x < $len; $x++) { |
|
|
{ |
|
|
|
|
|
$char = game_substr( $word, $x, 1); |
|
|
$char = game_substr( $word, $x, 1); |
|
|
|
|
|
|
|
|
if ($showsolution) { |
|
|
if ($showsolution) { |
|
|
$word_line2 .= ( $char == " " ? ' ' : $char); |
|
|
$wordline2 .= ( $char == " " ? ' ' : $char); |
|
|
$done = 0; |
|
|
$done = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (game_strpos($letters, $char) === false) { |
|
|
if (game_strpos($letters, $char) === false) { |
|
|
$word_line.="_<font size=\"1\"> </font>\r\n"; |
|
|
$wordline .= "_<font size=\"1\"> </font>\r\n"; |
|
|
$done = 0; |
|
|
$done = 0; |
|
|
$answer .= '_'; |
|
|
$answer .= '_'; |
|
|
}else |
|
|
} else { |
|
|
{ |
|
|
$wordline .= ( $char == " " ? ' ' : $char); |
|
|
$word_line .= ( $char == " " ? ' ' : $char); |
|
|
|
|
|
$answer .= $char; |
|
|
$answer .= $char; |
|
|
$correct++; |
|
|
$correct++; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$len_alpha = game_strlen($alpha); |
|
|
$lenalpha = game_strlen( $alpha); |
|
|
$fontsize = 5; |
|
|
$fontsize = 5; |
|
|
|
|
|
|
|
|
for ($c=0; $c < $len_alpha; $c++) |
|
|
for ($c = 0; $c < $lenalpha; $c++) { |
|
|
{ |
|
|
|
|
|
$char = game_substr( $alpha, $c, 1); |
|
|
$char = game_substr( $alpha, $c, 1); |
|
|
|
|
|
|
|
|
if ( game_strpos($letters, $char) === false) |
|
|
if (game_strpos($letters, $char) === false) { |
|
|
{ |
|
|
// User doesn't select this character. |
|
|
//User doesn't select this character |
|
|
|
|
|
$params = 'id='.$id.'&newletter='.urlencode( $char); |
|
|
$params = 'id='.$id.'&newletter='.urlencode( $char); |
|
|
if ($onlyshow or $showsolution) { |
|
|
if ($onlyshow or $showsolution) { |
|
|
$links .= $char; |
|
|
$links .= $char; |
|
|
}else |
|
|
} else { |
|
|
{ |
|
|
|
|
|
$links .= "<font size=\"$fontsize\"><a href=\"attempt.php?$params\">$char</a></font>\r\n"; |
|
|
$links .= "<font size=\"$fontsize\"><a href=\"attempt.php?$params\">$char</a></font>\r\n"; |
|
|
} |
|
|
} |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ( game_strpos($word, $char) === false) |
|
|
if (game_strpos($word, $char) === false) { |
|
|
{ |
|
|
|
|
|
$links .= "\r\n<font size=\"$fontsize\" color=\"red\">$char </font>"; |
|
|
$links .= "\r\n<font size=\"$fontsize\" color=\"red\">$char </font>"; |
|
|
$wrong++; |
|
|
$wrong++; |
|
|
}else |
|
|
} else { |
|
|
{ |
|
|
|
|
|
$links .= "\r\n<B><font size=\"$fontsize\">$char </font></B> "; |
|
|
$links .= "\r\n<B><font size=\"$fontsize\">$char </font></B> "; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -383,7 +386,7 @@ function hangman_showpage(&$done, &$correct, &$wrong, $max, &$word_line, &$word_ |
|
|
$updrec->letters = $letters; |
|
|
$updrec->letters = $letters; |
|
|
if ($finishedword) { |
|
|
if ($finishedword) { |
|
|
if ($hangman->finishedword == 0) { |
|
|
if ($hangman->finishedword == 0) { |
|
|
//only one time per word increace the variable try |
|
|
// Only one time per word increace the variable try. |
|
|
$hangman->try = $hangman->try + 1; |
|
|
$hangman->try = $hangman->try + 1; |
|
|
if ($hangman->try > $hangman->maxtries) { |
|
|
if ($hangman->try > $hangman->maxtries) { |
|
|
$finished = true; |
|
|
$finished = true; |
|
@ -396,7 +399,6 @@ function hangman_showpage(&$done, &$correct, &$wrong, $max, &$word_line, &$word_ |
|
|
|
|
|
|
|
|
$updrec->try = $hangman->try; |
|
|
$updrec->try = $hangman->try; |
|
|
$updrec->finishedword = 1; |
|
|
$updrec->finishedword = 1; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$query->percent = ($correct - $wrong / $max) / game_strlen( $word); |
|
|
$query->percent = ($correct - $wrong / $max) / game_strlen( $word); |
|
@ -416,8 +418,7 @@ function hangman_showpage(&$done, &$correct, &$wrong, $max, &$word_line, &$word_ |
|
|
$score = 1; |
|
|
$score = 1; |
|
|
} else if ($wrong >= $max) { |
|
|
} else if ($wrong >= $max) { |
|
|
$score = 0; |
|
|
$score = 0; |
|
|
}else |
|
|
} else { |
|
|
{ |
|
|
|
|
|
$score = -1; |
|
|
$score = -1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -425,14 +426,14 @@ function hangman_showpage(&$done, &$correct, &$wrong, $max, &$word_line, &$word_ |
|
|
game_update_queries( $game, $attempt, $query, $score, $answer); |
|
|
game_update_queries( $game, $attempt, $query, $score, $answer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//This word is correct. If reach the max number of words I have to finish else continue with next word |
|
|
// This word is correct. |
|
|
function hangman_oncorrect( $id, $word_line, $game, $attempt, $hangman, $query) |
|
|
// If reach the max number of words I have to finish else continue with next word. |
|
|
{ |
|
|
function hangman_oncorrect( $id, $wordline, $game, $attempt, $hangman, $query) { |
|
|
global $DB; |
|
|
global $DB; |
|
|
|
|
|
|
|
|
echo "<BR/><BR/><font size=\"5\">\n$word_line</font>\r\n"; |
|
|
echo "<br/><br/><font size=\"5\">\n$wordline</font>\r\n"; |
|
|
|
|
|
|
|
|
echo '<p><BR/><font size="5" color="green">'.get_string( 'win', 'game').'</font><BR/><BR/></p>'; |
|
|
echo '<p><br/><font size="5" color="green">'.get_string( 'win', 'game').'</font><BR/><BR/></p>'; |
|
|
if ($query->answerid) { |
|
|
if ($query->answerid) { |
|
|
$feedback = $DB->get_field( 'question_answers', 'feedback', array( 'id' => $query->answerid)); |
|
|
$feedback = $DB->get_field( 'question_answers', 'feedback', array( 'id' => $query->answerid)); |
|
|
if ($feedback != '') { |
|
|
if ($feedback != '') { |
|
@ -443,44 +444,43 @@ function hangman_oncorrect( $id, $word_line, $game, $attempt, $hangman, $query) |
|
|
game_hangman_show_nextword( $id, $game, $attempt, $hangman); |
|
|
game_hangman_show_nextword( $id, $game, $attempt, $hangman); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function hangman_onincorrect( $id, $word_line, $word, $game, $attempt, $hangman, $onlyshow, $showsolution) |
|
|
function hangman_onincorrect( $id, $wordline, $word, $game, $attempt, $hangman, $onlyshow, $showsolution) { |
|
|
{ |
|
|
echo "\r\n<br/><br/><font size=\"5\">\n$wordline</font>\r\n"; |
|
|
echo "\r\n<BR/><BR/><font size=\"5\">\n$word_line</font>\r\n"; |
|
|
|
|
|
|
|
|
|
|
|
if( $onlyshow or $showsolution) |
|
|
if ( $onlyshow or $showsolution) { |
|
|
return; |
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
echo '<p><BR/><font size="5" color="red">'.get_string( 'hangman_loose', 'game').'</font><BR/><BR/></p>'; |
|
|
echo '<p><BR/><font size="5" color="red">'.get_string( 'hangman_loose', 'game').'</font><BR/><BR/></p>'; |
|
|
|
|
|
|
|
|
if ($game->param6) { |
|
|
if ($game->param6) { |
|
|
//show the correct answer |
|
|
// Show the correct answer. |
|
|
if( game_strpos($word, ' ') != false) |
|
|
if (game_strpos($word, ' ') != false) { |
|
|
echo '<br/>'.get_string( 'hangman_correct_phrase', 'game'); |
|
|
echo '<br/>'.get_string( 'hangman_correct_phrase', 'game'); |
|
|
else |
|
|
} else { |
|
|
echo '<br/>'.get_string( 'hangman_correct_word', 'game'); |
|
|
echo '<br/>'.get_string( 'hangman_correct_word', 'game'); |
|
|
|
|
|
} |
|
|
echo '<B>'.$word."</B><BR/><BR/>\r\n"; |
|
|
echo '<B>'.$word."</B><BR/><BR/>\r\n"; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
game_hangman_show_nextword( $id, $game, $attempt, $hangman, true); |
|
|
game_hangman_show_nextword( $id, $game, $attempt, $hangman, true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function game_hangman_show_nextword( $id, $game, $attempt, $hangman) |
|
|
function game_hangman_show_nextword( $id, $game, $attempt, $hangman) { |
|
|
{ |
|
|
|
|
|
global $CFG, $DB; |
|
|
global $CFG, $DB; |
|
|
|
|
|
|
|
|
echo '<br/>'; |
|
|
echo '<br/>'; |
|
|
if (($hangman->try < $hangman->maxtries) or ($hangman->maxtries == 0)) { |
|
|
if (($hangman->try < $hangman->maxtries) or ($hangman->maxtries == 0)) { |
|
|
//continue to next word |
|
|
// Continue to next word. |
|
|
|
|
|
|
|
|
$params = "id=$id&action2=nextword\">".get_string( 'nextword', 'game').'</a> '; |
|
|
$params = "id=$id&action2=nextword\">".get_string( 'nextword', 'game').'</a> '; |
|
|
echo "<a href=\"{$CFG->wwwroot}/mod/game/attempt.php?$params"; |
|
|
echo "<a href=\"{$CFG->wwwroot}/mod/game/attempt.php?$params"; |
|
|
}else |
|
|
} else { |
|
|
{ |
|
|
|
|
|
game_hangman_onfinishgame( $game, $attempt, $hangman); |
|
|
game_hangman_onfinishgame( $game, $attempt, $hangman); |
|
|
|
|
|
|
|
|
if( game_can_start_new_attempt( $game)) |
|
|
if (game_can_start_new_attempt( $game)) { |
|
|
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> '; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (! $cm = $DB->get_record('course_modules', array( 'id' => $id))) { |
|
|
if (! $cm = $DB->get_record('course_modules', array( 'id' => $id))) { |
|
|