From b5f11fca18112e77ba22559b4d49d3d693e4e3ad Mon Sep 17 00:00:00 2001 From: Vasilis Daloukas Date: Sun, 27 Dec 2015 02:10:48 +0200 Subject: [PATCH] Fix:Coding style --- lang/en/game.php | 81 +-- millionaire/play.php | 1117 ++++++++++++++++++------------------ report/default.php | 70 ++- report/overview/report.php | 334 ++++++----- snakes/createboard.php | 340 ++++++----- snakes/play.php | 881 ++++++++++++++-------------- 6 files changed, 1442 insertions(+), 1381 deletions(-) diff --git a/lang/en/game.php b/lang/en/game.php index 65c5bcb..b14511f 100755 --- a/lang/en/game.php +++ b/lang/en/game.php @@ -1,31 +1,46 @@ -. -//bookquiz/importodt.php +// Translated by Vasilis Daloukas. + +// File bookquiz/importodt.php. $string[ 'bookquiz_import_odt'] = 'Import from an OpenOffice file (odt)'; $string[ 'bookquiz_not_select_book'] = 'You have not select book'; $string[ 'bookquiz_subchapter'] = 'Create subchapters'; -//bookquiz/play.php +// File bookquiz/play.php. $string[ 'bookquiz_empty'] = 'The book is empty'; $string[ 'sudoku_submit'] = 'Grade answers'; -//bookquiz/questions.php +// File bookquiz/questions.php. $string[ 'bookquiz_categories'] = 'Categories'; $string[ 'bookquiz_chapters'] = 'Chapters'; $string[ 'bookquiz_numquestions'] = 'Questions'; -//classes +// Classes. $string[ 'eventgamecreated'] = 'Game created'; $string[ 'eventgamedeleted'] = 'Game deleted'; $string[ 'eventgamesupdated'] = 'Game updated'; $string[ 'eventgameviewed'] = 'Game viewed'; $string[ 'eventgameplayed'] = 'Game played'; -//cross/cross_class.php +// File cross/cross_class.php. $string[ 'lettersall'] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; -//cross/crossdb_class.php +// File cross/crossdb_class.php. $string[ 'and'] = 'and'; $string[ 'cross_correct'] = 'corrert character'; $string[ 'cross_corrects'] = 'correct characters'; @@ -36,7 +51,7 @@ $string[ 'cross_found_one'] = 'Found'; $string[ 'grade'] = 'Grade'; $string[ 'cross_disabletransformuppercase'] = 'Disables text-transform:uppercase in CSS'; -//cross/play.php +// File cross/play.php. $string[ 'cross_across'] = 'Across'; $string[ 'cross_checkbutton'] = 'Check crossword'; $string[ 'cross_down'] = 'Down'; @@ -53,10 +68,10 @@ $string[ 'no_words'] = 'There are no words'; $string[ 'print'] = 'Print'; $string[ 'win'] = 'Congratulations !!!'; -//cryptex/play.php +// File cryptex/play.php. $string[ 'finish'] = 'End of game'; -//db/access.php +// File db/access.php. $string[ 'game:attempt'] = 'Play game'; $string[ 'game:deleteattempts'] = 'Delete attempts'; $string[ 'game:grade'] = 'Grade games manually'; @@ -67,7 +82,7 @@ $string[ 'game:reviewmyattempts'] = 'reviewmyattempts'; $string[ 'game:view'] = 'view'; $string[ 'game:viewreports'] = 'viewreports'; -//hangman/play.php +// File hangman/play.php. $string[ 'hangman_correct_phrase'] = 'The correct phrase was: '; $string[ 'hangman_correct_word'] = 'The correct word was: '; $string[ 'hangman_gradeinstance'] = 'Grade in whole game'; @@ -77,7 +92,7 @@ $string[ 'hangman_restletters_one'] = 'You have ONLY 1 try'; $string[ 'hangman_wrongnum'] = 'Wrong: %d out of %d'; $string[ 'nextword'] = 'Next word'; -//hiddenpicture/play.php +// File hiddenpicture/play.php. $string[ 'hiddenpicture_mainsubmit'] = 'Grade main answer'; $string[ 'hiddenpicture_nocols'] = 'Have to specify the number of cols horizontaly'; $string[ 'hiddenpicture_nomainquestion'] = 'There are no glossary entries on glossary {$a->name} with an attached picture'; @@ -86,11 +101,11 @@ $string[ 'must_select_glossary'] = 'You must select a glossary'; $string[ 'no_questions'] = "There are no questions"; $string[ 'noglossaryentriesfound'] = 'No glossary entries found'; -//millionaire/play.php +// File millionaire/play.php. $string[ 'millionaire_must_select_questioncategory'] = 'You must select one question category'; $string[ 'millionaire_must_select_quiz'] = 'You must select one quiz'; -//report/overview/report.php +// File report/overview/report.php. $string[ 'allattempts'] = 'Show all tries'; $string[ 'allstudents'] = 'Show all $a'; $string[ 'attemptduration'] = 'Attempt duration'; @@ -111,21 +126,21 @@ $string[ 'timecompleted'] = 'Completed'; $string[ 'unfinished'] = 'open'; $string[ 'withselected'] = 'With selected'; -//snakes/play.php +// File snakes/play.php. $string[ 'snakes_dice'] = 'Dice, $a spots.'; $string[ 'snakes_player'] = 'Player, position: $a.'; -//sudoku/create.php +// File sudoku/create.php. $string[ 'sudoku_create_count'] = 'Number of sudokus that will be created'; $string[ 'sudoku_create_start'] = 'Start creating sudokus'; $string[ 'sudoku_creating'] = 'Creating {$a} sudoku'; -//sudoku/play.php +// File sudoku/play.php. $string[ 'sudoku_finishattemptbutton'] = 'End of game'; $string[ 'sudoku_guessnumber'] = 'Guess the correct number'; $string[ 'sudoku_noentriesfound'] = 'No words found in glossary'; -//export.php +// File export.php. $string[ 'export'] = 'Export'; $string[ 'html_hascheckbutton'] = 'Has check button:'; $string[ 'html_hasprintbutton'] = 'Has print button:'; @@ -141,11 +156,11 @@ $string[ 'javame_type'] = 'Type:'; $string[ 'javame_vendor'] = 'Vendor:'; $string[ 'javame_version'] = 'Version:'; -//exporthtml_hangman.php +// File exporthtml_hangman.php. $string[ 'hangman_loose'] = 'Game over'; $string[ 'html_hangman_new'] = 'New'; -//exporthtml_millionaire.php +// File exporthtml_millionaire.php. $string[ 'millionaire_helppeople'] = 'Help of people'; $string[ 'millionaire_info_people'] = 'People say'; $string[ 'millionaire_info_telephone'] = 'I think that the correct answer is '; @@ -156,14 +171,14 @@ $string[ 'millionaire_telephone'] = 'Help of telephone'; $string[ 'must_select_questioncategory'] = 'You must select a question category'; $string[ 'must_select_quiz'] = 'You must select a quiz'; -//exporthtml_snakes.php +// File exporthtml_snakes.php. $string[ 'html_snakes_check'] = 'Check'; $string[ 'html_snakes_correct'] = 'Correct!'; $string[ 'html_snakes_no_selection'] = 'Have to select something!'; $string[ 'html_snakes_wrong'] = "Your answer isn't correct. Stay on the same seat."; $string[ 'score'] = 'Score'; -//index.php +// File index.php. $string[ 'helpbookquiz'] = 'When the student answers correct can go to the next chapter.'; $string[ 'helpcross'] = 'This game takes words from either a Glossary or quiz short answer questions and generates a random crossword puzzle. Teacher can set the maximum number of columns/rows or words that contains. Student can press the button “Check crossword” to check if the answers are correct. Every crossword is dynamic so it is different to every student.'; $string[ 'helpcryptex'] = 'This game is like a crossword but the answers are hidden inside a random cryptex.'; @@ -178,7 +193,7 @@ $string[ 'modulenameplural'] = 'Games'; $string[ 'pluginadministration'] = 'Game administration'; $string[ 'pluginname'] = 'Game'; -//lib.php +// File lib.php. $string[ 'attempt'] = 'Attempt'; $string[ 'bookquiz_questions'] = 'Associate question categories to chapter of book'; $string[ 'export_to_html'] = 'Export to HTML'; @@ -200,7 +215,7 @@ $string[ 'results'] = 'Results'; $string[ 'showanswers'] = 'Show answers'; $string[ 'showattempts'] = 'Show attempts'; -//locallib.php +// File locallib.php. $string[ 'attemptfirst'] = 'First attempt'; $string[ 'attemptlast'] = 'Last attempt'; $string[ 'convertfrom'] = '-'; @@ -208,7 +223,7 @@ $string[ 'convertto'] = '-'; $string[ 'gradeaverage'] = 'Average grade'; $string[ 'gradehighest'] = 'Highest grade'; -//mod_form.php +// File mod_form.php. $string[ 'bookquiz_layout'] = 'Layout'; $string[ 'bookquiz_layout0'] = 'Question at the top of the book'; $string[ 'bookquiz_layout1'] = 'Question at the bottom of the book'; @@ -276,11 +291,11 @@ $string[ 'sudoku_options'] = 'Sudoku options'; $string[ 'toptext'] = 'Text at the top of page'; $string[ 'userdefined'] = 'User defined'; -//preview.php +// File preview.php. $string[ 'only_teachers'] = 'Only teacher can see this page'; $string[ 'preview'] = 'Preview'; -//review.php +// File review.php. $string[ 'attempts'] = 'Attempts'; $string[ 'completedon'] = 'Completed on'; $string[ 'outof'] = '{$a->grade} out of a maximum of {$a->maxgrade}'; @@ -290,7 +305,7 @@ $string[ 'showall'] = 'Show all'; $string[ 'startagain'] = 'Start again'; $string[ 'timetaken'] = 'Time taken'; -//settings.php +// File settings.php. $string[ 'hangmanimagesets'] = 'Number of image sets used by hangman'; $string[ 'hidebookquiz'] = 'Hide the "Book with questions" game'; $string[ 'hidecross'] = 'Hide the Crossword game'; @@ -310,22 +325,22 @@ $string[ 'confighidemillionaire'] = 'Configs if the Millionaire game is shown to $string[ 'confighidesnakes'] = 'Configs if the "Snakes and Ladders" game is shown to teachers or not'; $string[ 'confighidesudoku'] = 'Configs if the Sudoku game is shown to teachers or not'; -//showanswers.php +// File showanswers.php. $string[ 'clearrepetitions'] = 'Clear statistics'; $string[ 'computerepetitions'] = 'Compute statistics again'; $string[ 'feedbacks'] = 'Messages correct answer'; $string[ 'repetitions'] = 'Repetitions'; -//showattempts.php +// File showattempts.php. $string[ 'lastip'] = 'IP student'; $string[ 'showsolution'] = 'solution'; $string[ 'timefinish'] = 'End of game'; $string[ 'timelastattempt'] = 'Last attempt'; $string[ 'timestart'] = 'Start'; -//tabs.php +// File tabs.php. -//view.php +// File view.php. $string[ 'attemptgamenow'] = 'Attempt game now'; $string[ 'comment'] = 'Comment'; $string[ 'continueattemptgame'] = 'Continue a previous attempt of game'; diff --git a/millionaire/play.php b/millionaire/play.php index 13ef6df..394af3f 100755 --- a/millionaire/play.php +++ b/millionaire/play.php @@ -1,4 +1,19 @@ -. + /** * This file plays the game millionaire * @@ -7,634 +22,612 @@ * @package game **/ -function game_millionaire_continue( $id, $game, $attempt, $millionaire, $context) -{ - //User must select quiz or question as a source module - if( ($game->quizid == 0) and ($game->questioncategoryid == 0)){ - if( $game->sourcemodule == 'quiz') - print_error( get_string( 'millionaire_must_select_quiz', 'game')); - else +function game_millionaire_continue( $id, $game, $attempt, $millionaire, $context) { + // User must select quiz or question as a source module. + if (($game->quizid == 0) and ($game->questioncategoryid == 0)) { + if ($game->sourcemodule == 'quiz') { + print_error( get_string( 'millionaire_must_select_quiz', 'game')); + } else { print_error( get_string( 'millionaire_must_select_questioncategory', 'game')); - } - - if( $attempt != false and $millionaire != false){ - //continue an existing game - return game_millionaire_play( $id, $game, $attempt, $millionaire, $context); - } - - if( $attempt == false){ - $attempt = game_addattempt( $game); - } - - $newrec = new stdClass(); - $newrec->id = $attempt->id; - $newrec->queryid = 0; - $newrec->level = 0; - $newrec->state = 0; - - if( !game_insert_record( 'game_millionaire', $newrec)){ - print_error( 'error inserting in game_millionaire'); - } - - game_millionaire_play( $id, $game, $attempt, $newrec, $context); -} + } + } + + if ($attempt != false and $millionaire != false) { + // Continue an existing game. + return game_millionaire_play( $id, $game, $attempt, $millionaire, $context); + } + + if ($attempt == false) { + $attempt = game_addattempt( $game); + } + + $newrec = new stdClass(); + $newrec->id = $attempt->id; + $newrec->queryid = 0; + $newrec->level = 0; + $newrec->state = 0; + if (!game_insert_record( 'game_millionaire', $newrec)) { + print_error( 'error inserting in game_millionaire'); + } -function game_millionaire_play( $id, $game, $attempt, $millionaire, $context) -{ - global $DB; + game_millionaire_play( $id, $game, $attempt, $newrec, $context); +} + +function game_millionaire_play( $id, $game, $attempt, $millionaire, $context) { + global $DB; $buttons = optional_param('buttons', 0, PARAM_INT); - $Help5050_x = optional_param('Help5050_x', 0, PARAM_INT); - $HelpTelephone_x = optional_param('HelpTelephone_x', 0, PARAM_INT); - $HelpPeople_x = optional_param('HelpPeople_x', 0, PARAM_INT); - $Quit_x = optional_param('Quit_x', 0, PARAM_INT); - - if( $millionaire->queryid){ - $query = $DB->get_record( 'game_queries', array( 'id' => $millionaire->queryid)); - }else - { - $query = new StdClass; - } + $help5050x = optional_param('Help5050_x', 0, PARAM_INT); + $helptelephonex = optional_param('HelpTelephone_x', 0, PARAM_INT); + $helppeoplex = optional_param('HelpPeople_x', 0, PARAM_INT); + $quitx = optional_param('Quit_x', 0, PARAM_INT); + + if ($millionaire->queryid) { + $query = $DB->get_record( 'game_queries', array( 'id' => $millionaire->queryid)); + } else { + $query = new StdClass; + } $found = 0; - for($i=1; $i <= $buttons; $i++){ + for ($i = 1; $i <= $buttons; $i++) { $name = 'btAnswer'.$i; $answer = optional_param($name, '', PARAM_RAW); if (!empty($answer)) { game_millionaire_OnAnswer( $id, $game, $attempt, $millionaire, $query, $i, $context); $found = 1; } - } - - if( $found == 1) - ;//nothing - else if( !empty($Help5050_x)) - game_millionaire_OnHelp5050( $game, $id, $millionaire, $game, $query, $context); - else if( !empty($HelpTelephone_x)) - game_millionaire_OnHelpTelephone( $game, $id, $millionaire, $query, $context); - else if( !empty($HelpPeople_x)) - game_millionaire_OnHelpPeople( $game, $id, $millionaire, $query, $context); - else if( !empty($Quit_x)) - game_millionaire_OnQuit( $id, $game, $attempt, $query, $context); - else - game_millionaire_ShowNextQuestion( $id, $game, $attempt, $millionaire, $context); - } - - -function game_millionaire_showgrid( $game, $millionaire, $id, $query, $aAnswer, $info, $context) -{ - global $OUTPUT; + } + + if ($found == 1) { + $found = $found; // Nothing. + } else if (!empty($help5050x)) { + game_millionaire_OnHelp5050( $game, $id, $millionaire, $game, $query, $context); + } else if (!empty($helptelephonex)) { + game_millionaire_OnHelpTelephone( $game, $id, $millionaire, $query, $context); + } else if (!empty($helppeoplex)) { + game_millionaire_OnHelpPeople( $game, $id, $millionaire, $query, $context); + } else if (!empty($quitx)) { + game_millionaire_OnQuit( $id, $game, $attempt, $query, $context); + } else { + game_millionaire_ShowNextQuestion( $id, $game, $attempt, $millionaire, $context); + } +} + +function game_millionaire_showgrid( $game, $millionaire, $id, $query, $aanswer, $info, $context) { + global $CFG, $OUTPUT; $question = str_replace( array("\'", '\"'), array("'", '"'), $query->questiontext); - if( $game->param8 == '') - $color = 408080; - else - $color = substr( '000000'.base_convert($game->param8, 10, 16), -6); - - $color1 = 'black'; - $color2 = 'DarkOrange'; - $colorback="white"; - $stylequestion = "background:$colorback;color:$color1"; - $stylequestionselected = "background:$colorback;color:$color2"; - - global $CFG; - - $state = $millionaire->state; - $level = $millionaire->level; - - $background = "style='background:#$color'"; - - echo '
'; - echo "\r\n"; - echo ""; - echo ''; - echo ""; - echo ''; - echo "\r\n"; - - echo ""; - echo "\n"; echo ''; echo ''; echo ''; echo ''; - echo ''; echo '
                      
"; - - $dirgif = 'millionaire/1/'; - if( $state & 1) - { - $gif = "5050x"; - $disabled = "disabled=1"; - }else - { - $gif = "5050"; - $disabled = ""; - } - echo ' '; - - if( $state & 2) - { - $gif = "telephonex"; - $disabled = "disabled=1"; - }else - { - $gif = "telephone"; - $disabled = ""; - } - echo ' '; - - if( $state & 4) - { - $gif = "peoplex"; - $disabled = "disabled=1"; - }else - { - $gif = "people"; - $disabled = ""; - } - echo ' '; - - echo ' '; - echo "\r\n"; + if ($game->param8 == '') { + $color = 408080; + } else { + $color = substr( '000000'.base_convert($game->param8, 10, 16), -6); + } + + $color1 = 'black'; + $color2 = 'DarkOrange'; + $colorback = "white"; + $stylequestion = "background:$colorback;color:$color1"; + $stylequestionselected = "background:$colorback;color:$color2"; + + $state = $millionaire->state; + $level = $millionaire->level; + + $background = "style='background:#$color'"; + + echo ''; + echo "\r\n"; + echo ""; + echo ''; + echo ""; + echo ''; + echo "\r\n"; + + echo ""; + echo "\r\n"; $styletext = ""; - if( strpos( $question, 'color:') == false and strpos( $question, 'background:') == false){ + if (strpos( $question, 'color:') == false and strpos( $question, 'background:') == false) { $styletext = "style='$stylequestion'"; } - $aVal = array( 100, 200, 300, 400, 500, 1000, 1500, 2000, 4000, 5000, 10000, 20000, 40000, 80000, 150000); - for( $i=15; $i >= 1; $i--) - { - $bTR = false; - switch( $i) - { - case 15: - echo "\r\n"; - $bTR = true; - break; - case 14: - case 13: - echo "\n"; - $bTR = true; - break; - case 12: - //$question = game_filterquestion($question, $query->questionid, $context->id, $game->course); - $question = game_show_query( $game, $query, $question); - echo ""; - echo "\r\n"; - $bTR = true; - break; - case 11: - case 10: - case 9: - case 8: - case 7: - case 6: - case 5: - case 4: - case 3: - case 2: - case 1: - echo ""; - $bTR = true; - break; - default: - echo ""; - $bTR = true; - } - - if( $i == $level+1) - $style = "background:$color1;color:$color2"; - else - $style = $stylequestion; - echo ""; - - if( $i < $level+1) - echo ""; - else if( $i == 15 and $level <= 1) - echo ""; - else - echo ""; - echo "\r\n"; - if( $bTR) - echo "\r\n"; + $aval = array( 100, 200, 300, 400, 500, 1000, 1500, 2000, 4000, 5000, 10000, 20000, 40000, 80000, 150000); + for ($i = 15; $i >= 1; $i--) { + $btr = false; + + switch ($i) { + case 15: + echo "\r\n"; + $btr = true; + break; + case 14: + case 13: + echo "\n"; + $btr = true; + break; + case 12: + $question = game_show_query( $game, $query, $question); + echo ""; + echo "\r\n"; + $btr = true; + break; + case 11: + case 10: + case 9: + case 8: + case 7: + case 6: + case 5: + case 4: + case 3: + case 2: + case 1: + echo ""; + $btr = true; + break; + default: + echo ""; + $btr = true; + } + + if ($i == $level + 1) { + $style = "background:$color1;color:$color2"; + } else { + $style = $stylequestion; + } + + echo ""; + + if ($i < $level + 1) { + echo ""; + } else if ($i == 15 and $level <= 1) { + echo ""; + } else { + echo ""; + } + echo "\r\n"; + if ($btr) { + echo "\r\n"; + } } echo "\r\n"; - $bFirst = true; + $bfirst = true; $letters = get_string( 'lettersall', 'game'); - for( $i=1; $i <= count( $aAnswer); $i++) - { - $name = "btAnswer".$i; - $s = game_substr( $letters, $i-1, 1); - - $disabled = ( $state == 15 ? "disabled=1" : ""); - - $style = $stylequestion; - if( (strpos( $aAnswer[ $i-1], 'color:') != false) or (strpos( $aAnswer[ $i-1], 'background:') != false)){ + for ($i = 1; $i <= count( $aanswer); $i++) { + $name = "btAnswer".$i; + $s = game_substr( $letters, $i - 1, 1); + + $disabled = ( $state == 15 ? "disabled=1" : ""); + + $style = $stylequestion; + if ((strpos( $aanswer[ $i - 1], 'color:') != false) or (strpos( $aanswer[ $i - 1], 'background:') != false)) { $style = ''; } - if( $state == 15 and $i+1 == $query->correct){ - $style = $stylequestionselected; - } - - $button = '"; - $text = game_filtertext($aAnswer[ $i-1],$game->course); - $answer = "". - $text.''; - if( $aAnswer[ $i-1] != ""){ - echo "\n"; - + if ($state == 15 and $i + 1 == $query->correct) { + $style = $stylequestionselected; + } + + $button = '"; + $text = game_filtertext($aanswer[ $i - 1], $game->course); + $answer = "". + $text.''; + if ($aanswer[ $i - 1] != "") { + echo "\n"; + echo "\n"; - echo ""; - if( $bFirst){ - $bFirst = false; - $info = game_filtertext($info, $game->course); - echo ""; - } - echo "\r\n\r\n"; - } - } - echo "\r\n"; - echo "\r\n"; - echo ''; - echo "\r\n"; + echo ""; + if ($bfirst) { + $bfirst = false; + $info = game_filtertext($info, $game->course); + echo ""; + } + echo "\r\n\r\n"; + } + } + echo "\r\n"; + echo "\r\n"; + echo ''; + echo "\r\n"; echo "
                      
"; + + $dirgif = 'millionaire/1/'; + if ($state & 1) { + $gif = "5050x"; + $disabled = "disabled=1"; + } else { + $gif = "5050"; + $disabled = ""; + } + echo ' '; + + if ($state & 2) { + $gif = "telephonex"; + $disabled = "disabled=1"; + } else { + $gif = "telephone"; + $disabled = ""; + } + + echo ' '; + + if ($state & 4) { + $gif = "peoplex"; + $disabled = "disabled=1"; + } else { + $gif = "people"; + $disabled = ""; + } + echo ' '; + + echo ' '; + echo "\r\n"; echo "      
$question
$i  *        ".sprintf( "%10d", $aVal[ $i-1])."
      
$question
$i  *        ".sprintf( "%10d", $aval[ $i - 1])."
     
$button   $answer$info
     
  $answer$info
     
\r\n"; echo "\r\n"; } -function game_millionaire_ShowNextQuestion( $id, $game, $attempt, $millionaire, $context) -{ - game_millionaire_SelectQuestion( $aAnswer, $game, $attempt, $millionaire, $query, $context); - - if( $game->toptext != ''){ - echo $game->toptext.'

'; - } - - game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aAnswer, "", $context); - - if( $game->bottomtext != ''){ - echo '
'.$game->bottomtext; - } +function game_millionaire_shownextquestion( $id, $game, $attempt, $millionaire, $context) { + game_millionaire_selectquestion( $aanswer, $game, $attempt, $millionaire, $query, $context); + + if ($game->toptext != '') { + echo $game->toptext.'

'; + } + + game_millionaire_showgrid( $game, $millionaire, $id, $query, $aanswer, "", $context); + + if ($game->bottomtext != '') { + echo '
'.$game->bottomtext; + } } -//updates tables: games_millionaire, game_attempts, game_questions -function game_millionaire_SelectQuestion( &$aAnswer, $game, $attempt, &$millionaire, &$query, $context) -{ - global $DB, $USER; - - 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)); - } - - if( $millionaire->queryid != 0){ - game_millionaire_loadquestions( $game, $millionaire, $query, $aAnswer, $context); - return; - } - - if( $game->sourcemodule == 'quiz'){ - if( $game->quizid == 0){ - print_error( get_string( 'must_select_quiz', 'game')); - } - if( game_get_moodle_version() < '02.07') - { - $select = "qtype='multichoice' AND quiz='$game->quizid' AND qmo.questionid=q.id". - " AND qqi.question=q.id"; - $table = "{quiz_question_instances} qqi,{question} q, {qtype_multichoice_options} qmo"; +// Updates tables: games_millionaire, game_attempts, game_questions. +function game_millionaire_selectquestion( &$aanswer, $game, $attempt, &$millionaire, &$query, $context) { + global $DB, $USER; + + 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)); + } + + if ($millionaire->queryid != 0) { + game_millionaire_loadquestions( $game, $millionaire, $query, $aanswer, $context); + return; + } + + if ($game->sourcemodule == 'quiz') { + if ($game->quizid == 0) { + print_error( get_string( 'must_select_quiz', 'game')); + } + if (game_get_moodle_version() < '02.07') { + $select = "qtype='multichoice' AND quiz='$game->quizid' AND qmo.questionid=q.id". + " AND qqi.question=q.id"; + $table = "{quiz_question_instances} qqi,{question} q, {qtype_multichoice_options} qmo"; $order = ''; - }else - { - $select = "qtype='multichoice' AND qs.quizid='$game->quizid' AND qmo.questionid=q.id". - " AND qs.questionid=q.id"; - $table = "{quiz_slots} qs,{question} q, {qtype_multichoice_options} qmo"; - $order = 'qs.page,qs.slot'; + } else { + $select = "qtype='multichoice' AND qs.quizid='$game->quizid' AND qmo.questionid=q.id". + " AND qs.questionid=q.id"; + $table = "{quiz_slots} qs,{question} q, {qtype_multichoice_options} qmo"; + $order = 'qs.page,qs.slot'; + } + } else { + if ($game->questioncategoryid == 0) { + print_error( get_string( 'must_select_questioncategory', 'game')); } - }else - { - if( $game->questioncategoryid == 0){ - print_error( get_string( 'must_select_questioncategory', 'game')); - } - - //include subcategories - $select = 'category='.$game->questioncategoryid; - if( $game->subcategories){ + + // Include subcategories. + $select = 'category='.$game->questioncategoryid; + if ($game->subcategories) { $cats = question_categorylist( $game->questioncategoryid); - if( count( $cats)){ + if (count( $cats)) { $select = 'q.category in ('.implode(',', $cats).')'; - } - } - $select .= " AND qtype='multichoice' AND qmo.single=1 AND qmo.questionid=q.id"; - - $table = '{question} q, {qtype_multichoice_options} qmo'; - } - $select .= ' AND hidden=0'; - if( $game->shuffle or $game->quizid == 0) - $questionid = game_question_selectrandom( $game, $table, $select, 'q.id as id', true); - else - $questionid = game_millionaire_select_serial_question( $game, $table, $select, 'q.id as id', $millionaire->level, $order); - - if( $questionid == 0){ - print_error( get_string( 'no_questions', 'game')); - } - - $q = $DB->get_record( 'question', array( 'id' => $questionid), 'id,questiontext'); - - $recs = $DB->get_records( 'question_answers', array( 'question' => $questionid)); - - if( $recs === false){ - print_error( get_string( 'no_questions', 'game')); - } - - $correct = 0; - $ids = array(); - foreach( $recs as $rec){ - $aAnswer[] = game_filterquestion_answer(str_replace( '\"', '"', $rec->answer), $rec->id, $context->id, $game->course); - - $ids[] = $rec->id; - if( $rec->fraction == 1){ - $correct = $rec->id; - } - } - - $count = count( $aAnswer); - for($i=1; $i <= $count; $i++){ - $sel = mt_rand(0, $count-1); - - $temp = array_splice( $aAnswer, $sel, 1); - $aAnswer[ ] = $temp[ 0]; - - $temp = array_splice( $ids, $sel, 1); - $ids[ ] = $temp[ 0]; - } - - $query = new StdClass; - $query->attemptid =$attempt->id; - $query->gamekind = $game->gamekind; - $query->gameid = $game->id; - $query->userid = $USER->id; - $query->sourcemodule = $game->sourcemodule; + } + } + $select .= " AND qtype='multichoice' AND qmo.single=1 AND qmo.questionid=q.id"; + + $table = '{question} q, {qtype_multichoice_options} qmo'; + } + $select .= ' AND hidden=0'; + if ($game->shuffle or $game->quizid == 0) { + $questionid = game_question_selectrandom( $game, $table, $select, 'q.id as id', true); + } else { + $questionid = game_millionaire_select_serial_question( $game, $table, $select, 'q.id as id', $millionaire->level, $order); + } + + if ($questionid == 0) { + print_error( get_string( 'no_questions', 'game')); + } + + $q = $DB->get_record( 'question', array( 'id' => $questionid), 'id,questiontext'); + + $recs = $DB->get_records( 'question_answers', array( 'question' => $questionid)); + + if ($recs === false) { + print_error( get_string( 'no_questions', 'game')); + } + + $correct = 0; + $ids = array(); + foreach ($recs as $rec) { + $aanswer[] = game_filterquestion_answer(str_replace( '\"', '"', $rec->answer), $rec->id, $context->id, $game->course); + + $ids[] = $rec->id; + if ($rec->fraction == 1) { + $correct = $rec->id; + } + } + + $count = count( $aanswer); + for ($i = 1; $i <= $count; $i++) { + $sel = mt_rand(0, $count - 1); + + $temp = array_splice( $aanswer, $sel, 1); + $aanswer[ ] = $temp[ 0]; + + $temp = array_splice( $ids, $sel, 1); + $ids[ ] = $temp[ 0]; + } + + $query = new StdClass; + $query->attemptid = $attempt->id; + $query->gamekind = $game->gamekind; + $query->gameid = $game->id; + $query->userid = $USER->id; + $query->sourcemodule = $game->sourcemodule; $query->glossaryentryid = 0; - $query->questionid = $questionid; - $query->questiontext = addslashes( $q->questiontext); - $query->answertext = implode( ',', $ids); - $query->correct = array_search( $correct, $ids) + 1; - if( !$query->id = $DB->insert_record( 'game_queries', $query)){ - print_object( $query); - print_error( 'error inserting to game_queries'); - } - - $updrec = new StdClass; - $updrec->id = $millionaire->id; - $updrec->queryid = $query->id; - - if( !$newid = $DB->update_record( 'game_millionaire', $updrec)){ - print_error( 'error updating in game_millionaire'); - } - - $score = $millionaire->level / 15; - game_updateattempts( $game, $attempt, $score, 0); - game_update_queries( $game, $attempt, $query, $score, ''); + $query->questionid = $questionid; + $query->questiontext = addslashes( $q->questiontext); + $query->answertext = implode( ',', $ids); + $query->correct = array_search( $correct, $ids) + 1; + if (!$query->id = $DB->insert_record( 'game_queries', $query)) { + print_error( 'error inserting to game_queries'); + } + + $updrec = new StdClass; + $updrec->id = $millionaire->id; + $updrec->queryid = $query->id; + + if (!$newid = $DB->update_record( 'game_millionaire', $updrec)) { + print_error( 'error updating in game_millionaire'); + } + + $score = $millionaire->level / 15; + game_updateattempts( $game, $attempt, $score, 0); + 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; - - $sql = "SELECT $id_fields,$id_fields FROM ".$table." WHERE $select "; - if( $order != '') + + $sql = "SELECT $idfields,$id_fields FROM ".$table." WHERE $select "; + if ($order != '') { $sql .= " ORDER BY $order"; - - if( ($recs = $DB->get_records_sql( $sql)) == false) + } + + if (($recs = $DB->get_records_sql( $sql)) == false) { return false; + } $questions = array(); - foreach( $recs as $rec) - $questions[] = $rec->id; - + foreach ($recs as $rec) { + $questions[] = $rec->id; + } + $count = count( $questions); - if( $count == 0) + if ($count == 0) { return false; - - $from = round($level * ($count-1) / 15); - $to = round(max( $from, ($level+1) * ($count-1) / 15))-1; - if( $to < $from) + } + + $from = round($level * ($count - 1) / 15); + $to = round(max( $from, ($level + 1) * ($count - 1) / 15)) - 1; + if ($to < $from) { $to = $from; + } $pos = mt_rand( round( $from), round( $to)); return $questions[ $pos]; } -function game_millionaire_loadquestions( $game, $millionaire, &$query, &$aAnswer, $context) -{ +function game_millionaire_loadquestions( $game, $millionaire, &$query, &$aanswer, $context) { 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); + $aanswer = array(); + foreach ($aids as $id) { + $rec = $DB->get_record( 'question_answers', array( 'id' => $id), 'id,answer'); - $aids = explode( ',', $query->answertext); - $aAnswer = array(); - foreach( $aids as $id) - { - $rec = $DB->get_record( 'question_answers', array( 'id' => $id), 'id,answer'); - - $aAnswer[] = game_filterquestion_answer(str_replace( '\"', '"', $rec->answer), $id, $context->id, $game->course); - } + $aanswer[] = game_filterquestion_answer(str_replace( '\"', '"', $rec->answer), $id, $context->id, $game->course); + } } -//flag 1:5050, 2:telephone 4:people -function game_millionaire_setstate( &$millionaire, $mask) -{ +// Flag 1:5050, 2:telephone 4:people. +function game_millionaire_setstate( &$millionaire, $mask) { global $DB; - $millionaire->state |= $mask; - + $millionaire->state |= $mask; + $updrec = new stdClass(); - $updrec->id = $millionaire->id; - $updrec->state = $millionaire->state; - if( !$DB->update_record( 'game_millionaire', $updrec)){ - print_error( 'error updating in game_millionaire'); - } + $updrec->id = $millionaire->id; + $updrec->state = $millionaire->state; + if (!$DB->update_record( 'game_millionaire', $updrec)) { + 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) -{ - game_millionaire_loadquestions( $game, $millionaire, $query, $aAnswer, $context); - - if( ($millionaire->state & 1) != 0) - { - game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aAnswer, '', $context); - return; - } - - game_millionaire_setstate( $millionaire, 1); - - $n = count( $aAnswer); - if( $n > 2) - { - for(;;) - { - $wrong = mt_rand( 1, $n); - if( $wrong != $query->correct){ - break; - } - } - for( $i=1; $i <= $n; $i++) - { - if( $i <> $wrong and $i <> $query->correct){ - $aAnswer[ $i-1] = ""; - } - } - } - - game_millionaire_showgrid( $game, $millionaire, $id, $query, $aAnswer, '', $context); + if (($millionaire->state & 1) != 0) { + game_millionaire_showgrid( $game, $millionaire, $id, $query, $aanswer, '', $context); + return; + } + + game_millionaire_setstate( $millionaire, 1); + + $n = count( $aanswer); + if ($n > 2) { + for (;;) { + $wrong = mt_rand( 1, $n); + if ($wrong != $query->correct) { + break; + } + } + for ($i = 1; $i <= $n; $i++) { + if ($i <> $wrong and $i <> $query->correct) { + $aanswer[ $i - 1] = ""; + } + } + } + + game_millionaire_showgrid( $game, $millionaire, $id, $query, $aanswer, '', $context); } - function game_millionaire_OnHelpTelephone( $game, $id, &$millionaire, $query, $context) - { - game_millionaire_loadquestions( $game, $millionaire, $query, $aAnswer, $context); - - if( ($millionaire->state & 2) != 0) - { - game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aAnswer, '', $context); - return; - } - - game_millionaire_setstate( $millionaire, 2); - - $n = count( $aAnswer); - if( $n < 2){ - $wrong = $query->correct; - }else - { - for(;;) - { - $wrong = mt_rand( 1, $n); - if( $wrong != $query->correct) - break; - } - } - //with 80% gives the correct answer - if( mt_rand( 1, 10) <= 8) - $response = $query->correct; - else - $response = $wrong; - - $info = get_string( 'millionaire_info_telephone','game').'
'.$aAnswer[ $response-1].''; - - game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aAnswer, $info, $context); - } - - 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; +function game_millionaire_onhelptelephone( $game, $id, &$millionaire, $query, $context) { + game_millionaire_loadquestions( $game, $millionaire, $query, $aanswer, $context); + + if (($millionaire->state & 2) != 0) { + game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aanswer, '', $context); + return; + } + + game_millionaire_setstate( $millionaire, 2); + + $n = count( $aanswer); + if ($n < 2) { + $wrong = $query->correct; + } else { + for (;;) { + $wrong = mt_rand( 1, $n); + if ($wrong != $query->correct) { + break; + } } - $aPercent[ $n-1] = 100 - $sum; - if( mt_rand( 1, 100) <= 80) - { - //with percent 80% sets in the correct answer the biggest percent - $max_pos = 0; - for( $i=1; $i+1 < $n; $i++) - { - if( $aPercent[ $i] >= $aPercent[ $max_pos]) - $max_pos = $i; - } - $temp = $aPercent[ $max_pos]; - $aPercent[ $max_pos] = $aPercent[ $query->correct-1]; - $aPercent[ $query->correct-1] = $temp; + } + + // With 80% gives the correct answer. + if (mt_rand( 1, 10) <= 8) { + $response = $query->correct; + } else { + $response = $wrong; + } + + $info = get_string( 'millionaire_info_telephone', 'game').'
'.$aanswer[ $response - 1].''; + + game_millionaire_showgrid( $game, $millionaire, $id, $query, $aanswer, $info, $context); +} + +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 = '
'.get_string( 'millionaire_info_people', 'game').':
'; + for ($i = 0; $i < $n; $i++) { + $info .= "
". 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) { + global $DB; + + game_millionaire_loadquestions( $game, $millionaire, $query, $aanswer, $context); + if ($answer == $query->correct) { + if ($millionaire->level < 15) { + $millionaire->level++; } - - $info = '
'.get_string( 'millionaire_info_people', 'game').':
'; - for( $i=0; $i < $n; $i++){ - $info .= "
". 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) - { - global $DB; - - game_millionaire_loadquestions( $game, $millionaire, $query, $aAnswer, $context); - if( $answer == $query->correct) - { - if( $millionaire->level < 15){ - $millionaire->level++; - } - $finish = ($millionaire->level == 15 ? 1 : 0); - $scorequestion = 1; - }else - { - $finish = 1; - $scorequestion = 0; - } - - $score = $millionaire->level / 15; - - game_update_queries( $game, $attempt, $query, $scorequestion, $answer); - game_updateattempts( $game, $attempt, $score, $finish); - - $updrec = new stdClass(); - $updrec->id = $millionaire->id; - $updrec->level = $millionaire->level; - $updrec->queryid = 0; - if( !$DB->update_record( 'game_millionaire', $updrec)){ - print_error( 'error updating in game_millionaire'); - } - - if( $answer == $query->correct) - { - //correct - if( $finish){ - echo get_string( 'win', 'game'); - game_millionaire_OnQuit( $id, $game, $attempt, $query); - }else - { - $millionaire->queryid = 0; //so the next function select a new question - - game_millionaire_ShowNextQuestion( $id, $game, $attempt, $millionaire, $context); - } - }else - { - //wrong answer - $info = get_string( 'millionaire_info_wrong_answer', 'game'). - '

'.$aAnswer[ $query->correct-1].''; - - $millionaire->state = 15; - game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aAnswer, $info, $context); - } - } - - function game_millionaire_onquit( $id, $game, $attempt, $query) - { - global $CFG, $DB; - - game_updateattempts( $game, $attempt, -1, true); - - if (! $cm = $DB->get_record( 'course_modules', array( 'id' => $id))) { - print_error( "Course Module ID was incorrect id=$id"); - } - - echo '
'; - echo "wwwroot}/mod/game/attempt.php?id=$id\">".get_string( 'nextgame', 'game').'         '; - echo "wwwroot}/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').' '; - } + $finish = ($millionaire->level == 15 ? 1 : 0); + $scorequestion = 1; + } else { + $finish = 1; + $scorequestion = 0; + } + + $score = $millionaire->level / 15; + + game_update_queries( $game, $attempt, $query, $scorequestion, $answer); + game_updateattempts( $game, $attempt, $score, $finish); + $updrec = new stdClass(); + $updrec->id = $millionaire->id; + $updrec->level = $millionaire->level; + $updrec->queryid = 0; + if (!$DB->update_record( 'game_millionaire', $updrec)) { + print_error( 'error updating in game_millionaire'); + } + + if ($answer == $query->correct) { + // Correct. + if ($finish) { + echo get_string( 'win', 'game'); + game_millionaire_OnQuit( $id, $game, $attempt, $query); + } else { + $millionaire->queryid = 0; // So the next function select a new question. + } + game_millionaire_ShowNextQuestion( $id, $game, $attempt, $millionaire, $context); + } else { + // Wrong answer. + $info = get_string( 'millionaire_info_wrong_answer', 'game'). + '

'.$aanswer[ $query->correct - 1].''; + + $millionaire->state = 15; + game_millionaire_ShowGrid( $game, $millionaire, $id, $query, $aanswer, $info, $context); + } +} + +function game_millionaire_onquit( $id, $game, $attempt, $query) { + global $CFG, $DB; + + game_updateattempts( $game, $attempt, -1, true); + + if (! $cm = $DB->get_record( 'course_modules', array( 'id' => $id))) { + print_error( "Course Module ID was incorrect id=$id"); + } + + echo '
'; + echo "wwwroot}/mod/game/attempt.php?id=$id\">". + get_string( 'nextgame', 'game').'         '; + echo "wwwroot}/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').' '; +} diff --git a/report/default.php b/report/default.php index 65cf3f7..a40d026 100755 --- a/report/default.php +++ b/report/default.php @@ -1,49 +1,63 @@ -id" or q=$quiz->id", and "mode=reportname". -//////////////////////////////////////////////////////////////////// - -// Included by ../report.php +. + +/* + * Default class for report plugins + * + * Doesn't do anything on it's own -- it needs to be extended. + * This class displays quiz reports. Because it is called from + * within /mod/game/report.php you can assume that the page header + * and footer are taken care of. + * + * This file can refer to itself as report.php to pass variables + * to itself - all these will also be globally available. You must + * pass "id=$cm->id" or q=$quiz->id", and "mode=reportname". + */ + +// Included by ../report.php. class game_default_report { - function display($cm, $course, $game) { /// This function just displays the report + public function display($cm, $course, $game) { + // This function just displays the report. return true; } - function print_header_and_tabs($cm, $course, $game, $reportmode="overview", $meta=""){ + public function print_header_and_tabs($cm, $course, $game, $reportmode = "overview", $meta = "") { global $CFG; - /// Define some strings + + // Define some strings. $strgames = get_string("modulenameplural", "game"); $strgame = get_string("modulename", "game"); - /// Print the page header - if( function_exists( 'build_navigation')){ + // Print the page header. + if (function_exists( 'build_navigation')) { $navigation = build_navigation('', $cm); echo $OUTPUT->heading( $course->shortname, $course->shortname, $navigation); - }else{ + } else { echo $OUTPUT->heading(format_string($game->name), "", "id\">$strgames -> ".format_string($game->name), '', $meta, true, update_module_button($cm->id, $course->id, $strgame), navmenu($course, $cm)); } - - /// Print the tabs + + // Print the tabs. $currenttab = 'reports'; $mode = $reportmode; - - include('tabs.php'); + + require('tabs.php'); } } - -?> diff --git a/report/overview/report.php b/report/overview/report.php index 684d7b3..4a4723b 100755 --- a/report/overview/report.php +++ b/report/overview/report.php @@ -1,4 +1,19 @@ . + /** * This script lists student attempts * @@ -15,38 +30,38 @@ class game_report extends game_default_report { /** * Display the report. */ - function display($game, $cm, $course) { + public function display($game, $cm, $course) { global $CFG, $SESSION, $db, $QTYPES; - // Define some strings - $strreallydel = addslashes(get_string('deleteattemptcheck','game')); + // Define some strings. + $strreallydel = addslashes(get_string('deleteattemptcheck', 'game')); $strtimeformat = get_string('strftimedatetime'); $strreviewquestion = get_string('reviewresponse', 'quiz'); - // Only print headers if not asked to download data - if (!$download = optional_param('download', NULL)) { - $this->print_header_and_tabs($cm, $course, $game, $reportmode="overview"); + // Only print headers if not asked to download data. + if (!$download = optional_param('download', null)) { + $this->print_header_and_tabs($cm, $course, $game, $reportmode = "overview"); } - // Deal with actions + // Deal with actions. $action = optional_param('action', '', PARAM_ACTION); - switch($action) { - case 'delete': // Some attempts need to be deleted + switch ($action) { + case 'delete': // Some attempts need to be deleted. $attemptids = optional_param('attemptid', array(), PARAM_INT); - foreach($attemptids as $attemptid) { + foreach ($attemptids as $attemptid) { if ($attemptid && $todelete = get_record('game_attempts', 'id', $attemptid)) { delete_records('game_attempts', 'id', $attemptid); delete_records('game_queries', 'attemptid', $attemptid); // Search game_attempts for other instances by this user. - // If none, then delete record for this game, this user from game_grades - // else recalculate best grade + // If none, then delete record for this game, this user from game_grades. + // else recalculate best grade. $userid = $todelete->userid; if (!record_exists('game_attempts', 'userid', $userid, 'gameid', $game->id)) { - delete_records('game_grades', 'userid', $userid,'gameid', $game->id); + delete_records('game_grades', 'userid', $userid, 'gameid', $game->id); } else { game_save_best_score( $game, $userid); } @@ -55,12 +70,14 @@ class game_report extends game_default_report { break; } - // Print information on the number of existing attempts - if (!$download) { //do not print notices when downloading + // Print information on the number of existing attempts. + if (!$download) { + // Do not print notices when downloading. if ($attemptnum = count_records('game_attempts', 'gameid', $game->id)) { $a = new stdClass; $a->attemptnum = $attemptnum; - $a->studentnum = count_records_select('game_attempts', "gameid = '$game->id' AND preview = '0'", 'COUNT(DISTINCT userid)'); + $a->studentnum = count_records_select('game_attempts', + "gameid = '$game->id' AND preview = '0'", 'COUNT(DISTINCT userid)'); $a->studentstring = $course->students; notify( get_string('numattempts', 'game', $a)); @@ -68,8 +85,8 @@ class game_report extends game_default_report { } $context = get_context_instance(CONTEXT_MODULE, $cm->id); - /// find out current groups mode - if ($groupmode = groupmode($course, $cm)) { // Groups are being used + // Find out current groups mode. + if ($groupmode = groupmode($course, $cm)) { // Groups are being used. if (!$download) { $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id&mode=overview"); } else { @@ -79,7 +96,7 @@ class game_report extends game_default_report { $currentgroup = get_and_set_current_group($course, $groupmode); } - // Set table options + // Set table options. $noattempts = optional_param('noattempts', 0, PARAM_INT); $detailedmarks = optional_param('detailedmarks', 0, PARAM_INT); $pagesize = optional_param('pagesize', 10, PARAM_INT); @@ -88,24 +105,25 @@ class game_report extends game_default_report { $pagesize = 10; } - // Now check if asked download of data + // Now check if asked download of data. if ($download) { - $filename = clean_filename("$course->shortname ".format_string($game->name,true)); + $filename = clean_filename("$course->shortname ".format_string($game->name, true)); $sort = ''; } - // Define table columns + // Define table columns. $tablecolumns = array('checkbox', 'picture', 'fullname', 'timestart', 'timefinish', 'duration'); - $tableheaders = array(NULL, '', get_string('fullname'), get_string('startedon', 'game'), get_string('timecompleted','game'), get_string('attemptduration', 'game')); + $tableheaders = array(null, '', get_string('fullname'), get_string('startedon', 'game'), + get_string('timecompleted', 'game'), get_string('attemptduration', 'game')); if ($game->grade) { $tablecolumns[] = 'grade'; $tableheaders[] = get_string('grade', 'game').'/'.$game->grade; } - if($detailedmarks) { - // we want to display marks for all questions - // Start by getting all questions + if ($detailedmarks) { + // We want to display marks for all questions. + // Start by getting all questions. $questionlist = game_questions_in_game( $game->questions); $questionids = explode(',', $questionlist); $sql = "SELECT q.*, i.score AS maxgrade, i.id AS instance". @@ -114,18 +132,18 @@ class game_report extends game_default_report { " WHERE i.gameid = '$game->id' AND q.id = i.questionid". " AND q.id IN ($questionlist)"; if (!$questions = get_records_sql($sql)) { - print_error('No questions found'); + print_error('No questions found'); } $number = 1; - foreach($questionids as $key => $id) { + foreach ($questionids as $key => $id) { if ($questions[$id]->length) { - // Only print questions of non-zero length + // Only print questions of non-zero length. $tablecolumns[] = '$'.$id; $tableheaders[] = '#'.$number; $questions[$id]->number = $number; $number += $questions[$id]->length; } else { - // get rid of zero length questions + // Get rid of zero length questions. unset($questions[$id]); unset($questionids[$key]); } @@ -138,7 +156,7 @@ class game_report extends game_default_report { } if (!$download) { - // Set up the table + // Set up the table. $table = new flexible_table('mod-game-report-overview-report'); @@ -160,20 +178,20 @@ class game_report extends game_default_report { $table->set_attribute('id', 'attempts'); $table->set_attribute('class', 'generaltable generalbox'); - // Start working -- this is necessary as soon as the niceties are over + // Start working -- this is necessary as soon as the niceties are over. $table->setup(); - } else if ($download =='ODS') { + } else if ($download == 'ODS') { require_once("$CFG->libdir/odslib.class.php"); $filename .= ".ods"; - // Creating a workbook + // Creating a workbook. $workbook = new MoodleODSWorkbook("-"); - // Sending HTTP headers + // Sending HTTP headers. $workbook->send($filename); - // Creating the first worksheet - $sheettitle = get_string('reportoverview','game'); + // Creating the first worksheet. + $sheettitle = get_string('reportoverview', 'game'); $myxls =& $workbook->add_worksheet($sheettitle); - // format types + // Format types. $format =& $workbook->add_format(); $format->set_bold(0); $formatbc =& $workbook->add_format(); @@ -193,18 +211,18 @@ class game_report extends game_default_report { $formatg->set_bold(1); $formatg->set_color('green'); $formatg->set_align('center'); - // Here starts workshhet headers - $headers = array(get_string('fullname'), - get_string('startedon', 'game'), - get_string('timecompleted', 'game'), + // Here starts workshhet headers. + $headers = array(get_string('fullname'), + get_string('startedon', 'game'), + get_string('timecompleted', 'game'), get_string('attemptduration', 'game') ); if ($game->grade) { $headers[] = get_string('grade', 'game').'/'.$game->grade; } - if($detailedmarks) { + if ($detailedmarks) { foreach ($questionids as $id) { $headers[] = '#'.$questions[$id]->number; } @@ -214,22 +232,22 @@ class game_report extends game_default_report { } $colnum = 0; foreach ($headers as $item) { - $myxls->write(0,$colnum,$item,$formatbc); + $myxls->write(0, $colnum, $item, $formatbc); $colnum++; } - $rownum=1; - } else if ($download =='Excel') { + $rownum = 1; + } else if ($download == 'Excel') { require_once("$CFG->libdir/excellib.class.php"); $filename .= ".xls"; - // Creating a workbook + // Creating a workbook. $workbook = new MoodleExcelWorkbook("-"); - // Sending HTTP headers + // Sending HTTP headers. $workbook->send($filename); - // Creating the first worksheet - $sheettitle = get_string('reportoverview','game'); + // Creating the first worksheet. + $sheettitle = get_string('reportoverview', 'game'); $myxls =& $workbook->add_worksheet($sheettitle); - // format types + // Format types. $format =& $workbook->add_format(); $format->set_bold(0); $formatbc =& $workbook->add_format(); @@ -249,14 +267,15 @@ class game_report extends game_default_report { $formatg->set_bold(1); $formatg->set_color('green'); $formatg->set_align('center'); - // Here starts workshhet headers - $headers = array(get_string('fullname'), get_string('startedon', 'game'), get_string('timecompleted', 'game'), get_string('attemptduration', 'game')); + // Here starts workshhet headers. + $headers = array(get_string('fullname'), get_string('startedon', 'game'), + get_string('timecompleted', 'game'), get_string('attemptduration', 'game')); if ($game->grade) { $headers[] = get_string('grade', 'game').'/'.$game->grade; } - if($detailedmarks) { + if ($detailedmarks) { foreach ($questionids as $id) { $headers[] = '#'.$questions[$id]->number; } @@ -266,11 +285,11 @@ class game_report extends game_default_report { } $colnum = 0; foreach ($headers as $item) { - $myxls->write(0,$colnum,$item,$formatbc); + $myxls->write(0, $colnum, $item, $formatbc); $colnum++; } - $rownum=1; - } else if ($download=='CSV') { + $rownum = 1; + } else if ($download == 'CSV') { $filename .= ".txt"; header("Content-Type: application/download\n"); @@ -279,12 +298,13 @@ class game_report extends game_default_report { header("Cache-Control: must-revalidate,post-check=0,pre-check=0"); header("Pragma: public"); - $headers = get_string('fullname')."\t".get_string('startedon', 'game')."\t".get_string('timecompleted', 'game')."\t".get_string('attemptduration', 'game'); + $headers = get_string('fullname')."\t".get_string('startedon', 'game'). + "\t".get_string('timecompleted', 'game')."\t".get_string('attemptduration', 'game'); if ($game->grade) { $headers .= "\t".get_string('grade', 'game')."/".$game->grade; } - if($detailedmarks) { + if ($detailedmarks) { foreach ($questionids as $id) { $headers .= "\t#".$questions[$id]->number; } @@ -297,48 +317,54 @@ class game_report extends game_default_report { $contextlists = get_related_contexts_string( get_context_instance( CONTEXT_COURSE, $course->id)); - // Construct the SQL + // Construct the SQL. $select = 'SELECT qa.id,'.sql_concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).' AS uniqueid, '. 'qa.id as attemptuniqueid, qa.id AS attempt, u.id AS userid, u.firstname, u.lastname, u.picture, '. -// 'qa.uniqueid as attemptuniqueid, qa.id AS attempt, u.id AS userid, u.firstname, u.lastname, u.picture, '. 'qa.score, qa.timefinish, qa.timestart, qa.timefinish - qa.timestart AS duration '; - if ($course->id != SITEID) { // this is too complicated, so just do it for each of the four cases. + if ($course->id != SITEID) { + // This is too complicated, so just do it for each of the four cases. if (!empty($currentgroup) && empty($noattempts)) { - // we want a particular group and we only want to see students WITH attempts. + // We want a particular group and we only want to see students WITH attempts. // So join on groups_members and do an inner join on attempts. $from = 'FROM {user} u JOIN {role_assignments} ra ON ra.userid = u.id '. groups_members_join_sql(). 'JOIN {game_attempts} qa ON u.id = qa.userid AND qa.gameid = '.$game->id; - $where = ' WHERE ra.contextid ' . $contextlists . ' AND '. groups_members_where_sql($currentgroup) .' AND qa.preview = 0'; + $where = ' WHERE ra.contextid ' . $contextlists . + ' AND '. groups_members_where_sql($currentgroup) .' AND qa.preview = 0'; } else if (!empty($currentgroup) && !empty($noattempts)) { - // We want a particular group and we want to do something funky with attempts + // We want a particular group and we want to do something funky with attempts. // So join on groups_members and left join on attempts... $from = 'FROM {user} u JOIN {role_assignments} ra ON ra.userid = u.id '. groups_members_join_sql(). 'LEFT JOIN {game_attempts} qa ON u.id = qa.userid AND qa.gameid = '.$game->id; $where = ' WHERE ra.contextid ' .$contextlists . ' AND '.groups_members_where_sql($currentgroup); if ($noattempts == 1) { - // noattempts = 1 means only no attempts, so make the left join ask for only records where the right is null (no attempts) - $where .= ' AND qa.userid IS NULL'; // show ONLY no attempts; + // Noattempts = 1 means only no attempts, so make the left join ask. + // For only records where the right is null (no attempts). + $where .= ' AND qa.userid IS NULL'; // Show ONLY no attempts. } else { // We are including attempts, so exclude previews. $where .= ' AND qa.preview = 0'; } } else if (empty($currentgroup)) { - // We don't care about group, and we to do something funky with attempts - // So do a left join on attempts - $from = 'FROM {user} u JOIN {role_assignments} ra ON ra.userid = u.id LEFT JOIN {game_attempts} qa ON u.id = qa.userid AND qa.gameid = '.$game->id; + // We don't care about group, and we to do something funky with attempts. + // So do a left join on attempts. + $from = 'FROM {user} u JOIN {role_assignments} ra ON ra.userid = u.id '. + ' LEFT JOIN {game_attempts} qa ON u.id = qa.userid AND qa.gameid = '.$game->id; $where = " WHERE ra.contextid $contextlists"; if (empty($noattempts)) { - $where .= ' AND qa.userid IS NOT NULL AND qa.preview = 0'; // show ONLY students with attempts; + // Show ONLY students with attempts. + $where .= ' AND qa.userid IS NOT NULL AND qa.preview = 0'; } else if ($noattempts == 1) { - // noattempts = 1 means only no attempts, so make the left join ask for only records where the right is null (no attempts) - $where .= ' AND qa.userid IS NULL'; // show ONLY students without attempts; + // The noattempts = 1 means only no attempts,. + // So make the left join ask for only records where the right is null (no attempts). + // Show ONLY students without attempts. + $where .= ' AND qa.userid IS NULL'; } else if ($noattempts == 3) { - // we want all attempts + // We want all attempts. $from = 'FROM {user} u JOIN {game_attempts} qa ON u.id = qa.userid '; $where = ' WHERE qa.gameid = '.$game->id.' AND qa.preview = 0'; - } // noattempts = 2 means we want all students, with or without attempts + } // The noattempts = 2 means we want all students, with or without attempts. } $countsql = 'SELECT COUNT(DISTINCT('.sql_concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).')) '.$from.$where; } else { @@ -349,36 +375,35 @@ class game_report extends game_default_report { } } if (!$download) { - // Add extra limits due to initials bar - if($table->get_sql_where()) { + // Add extra limits due to initials bar. + if ($table->get_sql_where()) { $where .= ' AND '.$table->get_sql_where(); } - // Count the records NOW, before funky question grade sorting messes up $from + // Count the records NOW, before funky question grade sorting messes up $from. if (!empty($countsql)) { $totalinitials = count_records_sql($countsql); if ($table->get_sql_where()) { $countsql .= ' AND '.$table->get_sql_where(); } $total = count_records_sql($countsql); - } - // Add extra limits due to sorting by question grade - if($sort = $table->get_sql_sort()) { + // Add extra limits due to sorting by question grade. + if ($sort = $table->get_sql_sort()) { $sortparts = explode(',', $sort); $newsort = array(); $questionsort = false; - foreach($sortparts as $sortpart) { + foreach ($sortparts as $sortpart) { $sortpart = trim($sortpart); - if(substr($sortpart, 0, 1) == '$') { - if(!$questionsort) { + if (substr($sortpart, 0, 1) == '$') { + if (!$questionsort) { $qid = intval(substr($sortpart, 1)); $select .= ', grade '; $from .= ' LEFT JOIN {question_sessions} qns ON qns.attemptid = qa.id '. 'LEFT JOIN {question_states} qs ON qs.id = qns.newgraded '; $where .= ' AND ('.sql_isnull('qns.questionid').' OR qns.questionid = '.$qid.')'; - $newsort[] = 'grade '.(strpos($sortpart, 'ASC')? 'ASC' : 'DESC'); + $newsort[] = 'grade '.(strpos($sortpart, 'ASC') ? 'ASC' : 'DESC'); $questionsort = true; } } else { @@ -386,14 +411,13 @@ class game_report extends game_default_report { } } - // Reconstruct the sort string + // Reconstruct the sort string. $sort = ' ORDER BY '.implode(', ', $newsort); } - // Fix some wired sorting + // Fix some wired sorting. if (empty($sort)) { - //$sort = ' ORDER BY uniqueid'; - $sort = ' ORDER BY qa.id'; + $sort = ' ORDER BY qa.id'; } $table->pagesize($pagesize, $total); @@ -402,82 +426,98 @@ class game_report extends game_default_report { // If there is feedback, include it in the query. if ($hasfeedback) { $select .= ', qf.feedbacktext '; - $from .= " JOIN {game_feedback} AS qf ON " . + $from .= " JOIN {game_feedback} qf ON " . "qf.gameid = $game->id AND qf.mingrade <= qa.score * $game->grade AND qa.score * $game->grade < qf.maxgrade"; } - // Fetch the attempts - if (!empty($from)) { // if we're in the site course and displaying no attempts, it makes no sense to do the query. + // Fetch the attempts. + if (!empty($from)) { + // If we're in the site course and displaying no attempts, it makes no sense to do the query. if (!$download) { $attempts = get_records_sql($select.$from.$where.$sort, - $table->get_page_start(), $table->get_page_size()); + $table->get_page_start(), $table->get_page_size()); } else { $attempts = get_records_sql($select.$from.$where.$sort); } } else { $attempts = array(); } - - - // Build table rows + // Build table rows. if (!$download) { - $table->initialbars($totalinitials>20); + $table->initialbars($totalinitials > 20); } - if(!empty($attempts) || !empty($noattempts)) { + if (!empty($attempts) || !empty($noattempts)) { if ($attempts) { foreach ($attempts as $attempt) { - $picture = print_user_picture($attempt->userid, $course->id, $attempt->picture, false, true); - - // uncomment the commented lines below if you are choosing to show unenrolled users and - // have uncommented the corresponding lines earlier in this script - //if (in_array($attempt->userid, $unenrolledusers)) { - // $userlink = ''.fullname($attempt).''; - //} - //else { - $userlink = ''.fullname($attempt).''; - //} + /* Uncomment the commented lines below if you are choosing to show unenrolled users and + * have uncommented the corresponding lines earlier in this script + * if (in_array($attempt->userid, $unenrolledusers)) { + * $userlink = ''.fullname($attempt).''; + *} + *else { + * $userlink = ''.fullname($attempt).''; + *} + */ if (!$download) { $row = array( '', $picture, $userlink, - empty($attempt->attempt) ? '-' : ''.userdate($attempt->timestart, $strtimeformat).'', - empty($attempt->timefinish) ? '-' : ''.userdate($attempt->timefinish, $strtimeformat).'', - empty($attempt->attempt) ? '-' : (empty($attempt->timefinish) ? get_string('unfinished', 'game') : format_time($attempt->duration)) + empty($attempt->attempt) ? '-' : ''. + userdate($attempt->timestart, $strtimeformat).'', + empty($attempt->timefinish) ? '-' : ''. + userdate($attempt->timefinish, $strtimeformat).'', + empty($attempt->attempt) ? '-' : ( + empty($attempt->timefinish) ? get_string('unfinished', 'game') : format_time( + $attempt->duration)) ); } else { $row = array(fullname($attempt), empty($attempt->attempt) ? '-' : userdate($attempt->timestart, $strtimeformat), empty($attempt->timefinish) ? '-' : userdate($attempt->timefinish, $strtimeformat), - empty($attempt->attempt) ? '-' : (empty($attempt->timefinish) ? get_string('unfinished', 'game') : format_time($attempt->duration)) + empty($attempt->attempt) ? '-' : ( + empty($attempt->timefinish) ? get_string( + 'unfinished', 'game') : format_time($attempt->duration)) ); } if ($game->grade) { if (!$download) { - $row[] = $attempt->score === NULL ? '-' : ''.round($attempt->score * $game->grade,$game->decimalpoints).''; + $row[] = $attempt->score === null ? '-' : ''. + round($attempt->score * $game->grade, $game->decimalpoints).''; } else { - $row[] = $attempt->score === NULL ? '-' : round($attempt->score * $game->grade,$game->decimalpoints); + $row[] = $attempt->score === null ? '-' : round($attempt->score * $game->grade, $game->decimalpoints); } } - if($detailedmarks) { - if(empty($attempt->attempt)) { - foreach($questionids as $questionid) { + if ($detailedmarks) { + if (empty($attempt->attempt)) { + foreach ($questionids as $questionid) { $row[] = '-'; } } else { - foreach($questionids as $questionid) { - if ($gradedstateid = get_field('question_sessions', 'newgraded', 'attemptid', $attempt->attemptuniqueid, 'questionid', $questionid)) { - $grade = round(get_field('question_states', 'grade', 'id', $gradedstateid), $game->decimalpoints); + foreach ($questionids as $questionid) { + if ($gradedstateid = get_field('question_sessions', 'newgraded', + 'attemptid', $attempt->attemptuniqueid, 'questionid', $questionid)) { + $grade = round(get_field('question_states', 'grade', 'id', + $gradedstateid), $game->decimalpoints); } else { $grade = '--'; } if (!$download) { - $row[] = link_to_popup_window ('/mod/game/reviewquestion.php?state='.$gradedstateid.'&number='.$questions[$questionid]->number, 'reviewquestion', $grade, 450, 650, $strreviewquestion, 'none', true); + $row[] = link_to_popup_window ( + '/mod/game/reviewquestion.php?state='. + $gradedstateid.'&number='. + $questions[$questionid]->number, 'reviewquestion', $grade, + 450, 650, $strreviewquestion, 'none', true); } else { - $row[] = $grade; + $row[] = $grade; } } } @@ -493,43 +533,50 @@ class game_report extends game_default_report { $table->add_data($row); } else if ($download == 'Excel' or $download == 'ODS') { $colnum = 0; - foreach($row as $item){ - $myxls->write($rownum,$colnum,$item,$format); + foreach ($row as $item) { + $myxls->write($rownum, $colnum, $item, $format); $colnum++; } $rownum++; - } else if ($download=='CSV') { + } else if ($download == 'CSV') { $text = implode("\t", $row); echo $text." \n"; } } } if (!$download) { - // Start form + // Start form. echo '
'; - echo '
'; + echo ''; echo '
'; echo ''; echo ''; - // Print table + // Print table. $table->print_html(); - // Print "Select all" etc. + // Print "Select all" etc.. if (!empty($attempts)) { echo ''; echo '
'; - echo ''.get_string('selectall', 'game').' / '; - echo ''.get_string('selectnone', 'game').' '; + echo ''. + get_string('selectall', 'game').' / '; + echo ''. + get_string('selectnone', 'game').' '; echo '  '; $options = array('delete' => get_string('delete')); - echo choose_from_menu($options, 'action', '', get_string('withselected', 'game'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true); + echo choose_from_menu($options, 'action', '', get_string('withselected', 'game'), + 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true); echo ''; - echo ''; + echo ''; echo '
'; } - // Close form + // Close form. echo '
'; echo '
'; @@ -545,7 +592,7 @@ class game_report extends game_default_report { $options['detailedmarks'] = $detailedmarks; echo '
'; $options["download"] = "ODS"; - print_single_button("report.php", $options, get_string("downloadods", 'game')); ///check bdaloukas + print_single_button("report.php", $options, get_string("downloadods", 'game')); echo "'; $options["download"] = "Excel"; @@ -572,7 +619,7 @@ class game_report extends game_default_report { $table->print_html(); } } - // Print display options + // Print display options. echo '
'; echo '
'; echo '
'; @@ -589,16 +636,19 @@ class game_report extends game_default_report { echo '
'; - $options = array(0 => get_string('attemptsonly','game', $course->students)); + $options = array(0 => get_string('attemptsonly', 'game', $course->students)); if ($course->id != SITEID) { $options[1] = get_string('noattemptsonly', 'game', $course->students); - $options[2] = get_string('allstudents','game', $course->students); - $options[3] = get_string('allattempts','game'); + $options[2] = get_string('allstudents', 'game', $course->students); + $options[3] = get_string('allattempts', 'game'); } - choose_from_menu($options,'noattempts',$noattempts,''); + choose_from_menu($options, 'noattempts', $noattempts, ''); echo '
'; + echo ' '; echo '
'; echo ''; @@ -611,5 +661,3 @@ class game_report extends game_default_report { return true; } } - -?> diff --git a/snakes/createboard.php b/snakes/createboard.php index 7cabd7c..a0ac7e7 100755 --- a/snakes/createboard.php +++ b/snakes/createboard.php @@ -1,26 +1,30 @@ -. + +// This file creates a board for "Snakes and Ladders". +function game_createsnakesboard($imageasstring, $colsx, $colsy, $ofstop, $ofsbottom, + $ofsright, $ofsleft, $board, $setwidth, $setheight) { global $CFG; $dir = $CFG->dirroot.'/mod/game/snakes/1'; $im = imagecreatefromstring($imageasstring); - //check if need resize - if( $setwidth >0 or $setheight > 0) - { + // Check if need resize. + if ( $setwidth > 0 or $setheight > 0) { $source = $im; $width = imagesx($source); $height = imagesy($source); @@ -39,36 +43,31 @@ function game_createsnakesboard($imageasstring, $colsx, $colsy, $ofstop, $ofsbot $cy = imagesy($im) - $ofstop - $ofsbottom; $color = 0xFF0000; - for( $i=0; $i <= $colsx; $i++) - { - imageline( $im, $ofsleft+$i * $cx / $colsx, $ofstop, $ofsleft+$i * $cx / $colsx, $cy+$ofstop, $color); + for ($i = 0; $i <= $colsx; $i++) { + imageline( $im, $ofsleft + $i * $cx / $colsx, $ofstop, $ofsleft + $i * $cx / $colsx, $cy + $ofstop, $color); } - for( $i=0; $i <= $colsy; $i++) - { - imageline( $im, $ofsleft, $ofstop+$i * $cy / $colsy, $cx+$ofsleft, $ofstop+$i * $cy / $colsy, $color); + for ($i = 0; $i <= $colsy; $i++) { + imageline( $im, $ofsleft, $ofstop + $i * $cy / $colsy, $cx + $ofsleft, $ofstop + $i * $cy / $colsy, $color); } - $filenamenumbers=$dir.'/numbers.png'; - $img_numbers = imageCreateFrompng( $filenamenumbers); - $size_numbers = getimagesize ($filenamenumbers); - - for( $iy=0; $iy < $colsy; $iy++) - { - if( $iy % 2 == 0){ - $inc=false; - $num = ($colsy-$iy)*$colsy; - }else - { - $inc=true; - $num = ($colsy-$iy)*$colsy-($colsy-1); - } - $ypos = $iy * $cy / $colsy+$ofstop; - for( $ix=0; $ix < $colsx; $ix++) - { + $filenamenumbers = $dir.'/numbers.png'; + $imgnumbers = imagecreatefrompng( $filenamenumbers); + $sizenumbers = getimagesize ($filenamenumbers); + + for ($iy = 0; $iy < $colsy; $iy++) { + if ($iy % 2 == 0) { + $inc = false; + $num = ($colsy - $iy) * $colsy; + } else { + $inc = true; + $num = ($colsy - $iy) * $colsy - ($colsy - 1); + } + $ypos = $iy * $cy / $colsy + $ofstop; + for ($ix = 0; $ix < $colsx; $ix++) { $xpos = $ix * $cx / $colsx + $ofsleft; - shownumber( $im, $img_numbers, $num, $xpos, $ypos, $cx/4, $cy/4, $size_numbers); - $num = ($inc ? $num+1 : $num-1); + shownumber( $im, $imgnumbers, $num, $xpos, $ypos, $cx / 4, $cy / 4, $sizenumbers); + $num = ($inc ? $num + 1 : $num - 1); } } @@ -77,36 +76,33 @@ function game_createsnakesboard($imageasstring, $colsx, $colsy, $ofstop, $ofsbot return $im; } -function computexy( $pos, &$x, &$y, $colsx, $colsy) -{ +function computexy( $pos, &$x, &$y, $colsx, $colsy) { $x = ($pos - 1) % $colsx; - $y = ($colsy-1) - floor( ($pos - 1) / $colsy); - if($y % 2 == 0) - $x = ($colsx-1) - $x; + $y = ($colsy - 1) - floor( ($pos - 1) / $colsy); + if ($y % 2 == 0) { + $x = ($colsx - 1) - $x; + } } -function makeboard( $im, $dir, $cx, $cy, $board, $colsx, $colsy, $ofsleft, $ofstop) -{ +function makeboard( $im, $dir, $cx, $cy, $board, $colsx, $colsy, $ofsleft, $ofstop) { $a = explode( ',', $board); - foreach( $a as $s) - { - if( substr( $s,0,1) == 'L') + foreach ($a as $s) { + if (substr( $s, 0, 1) == 'L') { makeboardL( $im, $dir, $cx, $cy, substr( $s, 1), $colsx, $colsy, $ofsleft, $ofstop); - else + } else { makeboardS( $im, $dir, $cx, $cy, substr( $s, 1), $colsx, $colsy, $ofsleft, $ofstop); + } } } -function makeboardL( $im, $dir, $cx, $cy, $s, $colsx, $colsy, $ofsleft, $ofstop) -{ +function makeboardl( $im, $dir, $cx, $cy, $s, $colsx, $colsy, $ofsleft, $ofstop) { $pos = strpos( $s, '-'); $from = substr( $s, 0, $pos); - $to = substr( $s, $pos+1); + $to = substr( $s, $pos + 1); computexy( $from, $startx, $starty, $colsx, $colsy); computexy( $to, $x2, $y2, $colsx, $colsy); - if( ($x2 < $startx) and ($y2 < $starty)) - { + if (($x2 < $startx) and ($y2 < $starty)) { $temp = $x2; $x2 = $startx; $startx = $temp; $temp = $y2; $y2 = $starty; $starty = $temp; } @@ -115,201 +111,193 @@ function makeboardL( $im, $dir, $cx, $cy, $s, $colsx, $colsy, $ofsleft, $ofstop) $letter = ( $movex * $movey < 0 ? 'b' : 'a'); - $_startx = $startx; $_movex=$movex; $_starty = $starty; $_movey=$movey; - - if( $movex < 0) - { - $startx += $movex; - $movex = -$movex; - } - if( $movey < 0) - { - $starty += $movey; - $movey = -$movey; - } + $oldstartx = $startx; $oldmovex = $movex; $oldstarty = $starty; $oldmovey = $movey; + + if ($movex < 0) { + $startx += $movex; + $movex = -$movex; + } + if ($movey < 0) { + $starty += $movey; + $movey = -$movey; + } $stamp = 0; - if( $letter == 'b'){ + if ($letter == 'b') { $file = $dir.'/l'.$letter.$movey.$movex.'.png'; - if( file_exists( $file)){ + if (file_exists( $file)) { $stamp = game_imagecreatefrompng( $file); - }else - { + } else { $file = $dir.'/la'.$movey.$movex.'.png'; $source = game_imagecreatefrompng( $file); - if( $source != 0) + if ( $source != 0) { $stamp = imagerotate($source, 90, 0); + } } - }else - { + } else { $file = $dir.'/la'.$movex.$movey.'.png'; $stamp = game_imagecreatefrompng( $file); } - - $dst_x = $startx*$cx/$colsx; - $dst_y = $starty*$cy/$colsy; - $dst_w = ($movex+1) * $cx / $colsx; - $dst_h = ($movey+1) * $cy / $colsy; - - if( $stamp == 0) - { - game_printladder( $im, $file, $dst_x+$ofsleft, $dst_y+$ofstop, $dst_w, $dst_h, $cx/$colsx, $cy/$colsy); - }else - { - imagecopyresampled( $im, $stamp, $ofsleft+$dst_x, $ofstop+$dst_y, 0, 0, $dst_w, $dst_h, 100*$movex+100, 100*$movey+100); + + $dstx = $startx * $cx / $colsx; + $dsty = $starty * $cy / $colsy; + $dstw = ($movex + 1) * $cx / $colsx; + $dsth = ($movey + 1) * $cy / $colsy; + + if ($stamp == 0) { + game_printladder( $im, $file, $dstx + $ofsleft, $dsty + $ofstop, $dstw, $dsth, $cx / $colsx, $cy / $colsy); + } else { + imagecopyresampled( $im, $stamp, $ofsleft + $dstx, $ofstop + $dsty, 0, 0, $dstw, $dsth, + 100 * $movex + 100, 100 * $movey + 100); } } -function makeboardS( $im, $dir, $cx, $cy, $s, $colsx, $colsy, $ofsleft, $ofstop) -{ +function makeboards( $im, $dir, $cx, $cy, $s, $colsx, $colsy, $ofsleft, $ofstop) { $pos = strpos( $s, '-'); $from = substr( $s, 0, $pos); - $to = substr( $s, $pos+1); + $to = substr( $s, $pos + 1); computexy( $from, $startx, $starty, $colsx, $colsy); computexy( $to, $x2, $y2, $colsx, $colsy); - $swap=0; - if( ($x2 < $startx) and ($y2 < $starty)) - { + $swap = 0; + if (($x2 < $startx) and ($y2 < $starty)) { $temp = $x2; $x2 = $startx; $startx = $temp; $temp = $y2; $y2 = $starty; $starty = $temp; - $swap=1; + $swap = 1; } $movex = $x2 - $startx; $movey = $y2 - $starty; - //a*d - //*** - //b*c + /* a*d + * + * b*c + */ $stamp = $rotate = 0; - if( $movex >= 0 and $movey < 0){ + if ($movex >= 0 and $movey < 0) { $letter = 'b'; $file = $dir.'/sa'.$movey.$movex.'.png'; $source = game_imagecreatefrompng( $file); - if( $source != 0) - { + if ($source != 0) { $stamp = imagerotate($source, 270, 0); $starty += $movey; $movey = -$movey; - }else + } else { $rotate = 270; - }else if( $movex < 0 and $movey < 0){ + } + } else if ($movex < 0 and $movey < 0) { $letter = 'c'; $file = $dir.'/sa'.$movey.$movex.'.png'; $source = game_imagecreatefrompng( $file); - if( $source != 0) - { + if ($source != 0) { $stamp = imagerotate($source, 180, 0); $startx += $movex; $movex = -$movex; $starty += $movey; $movey = -$movey; - }else + } else { $rotate = 180; - }else if( ($movex < 0) and ($movey >= 0)){ + } + } else if (($movex < 0) and ($movey >= 0)) { $letter = 'd'; $file = $dir.'/sa'.$movey.$movex.'.png'; $source = game_imagecreatefrompng( $file); - if( $source != 0) - { + if ($source != 0) { $stamp = imagerotate($source, 270, 0); $startx += $movex; $movex = -$movex; - }else - $rotate=270; - }else - { + } else { + $rotate = 270; + } + } else { $file = $dir.'/sa'.$movex.$movey.'.png'; $stamp = game_imagecreatefrompng( $file); } - if( ($swap != 0) and ($stamp == 0)) - { - $temp = $x2; $x2 = $startx; $startx = $temp; - $temp = $y2; $y2 = $starty; $starty = $temp; - $movex = $x2 - $startx; - $movey = $y2 - $starty; - } + if (($swap != 0) and ($stamp == 0)) { + $temp = $x2; $x2 = $startx; $startx = $temp; + $temp = $y2; $y2 = $starty; $starty = $temp; + $movex = $x2 - $startx; + $movey = $y2 - $starty; + } - $dst_x = $startx*$cx/$colsx; - $dst_y = $starty*$cy/$colsy; - $dst_w = ($movex+1) * $cx / $colsx; - $dst_h = ($movey+1) * $cy / $colsy; + $dstx = $startx * $cx / $colsx; + $dsty = $starty * $cy / $colsy; + $dstw = ($movex + 1) * $cx / $colsx; + $dsth = ($movey + 1) * $cy / $colsy; - if( $stamp == 0) - { - game_printsnake( $im, $file, $dst_x+$ofsleft, $dst_y+$ofstop, $dst_w, $dst_h, $cx/$colsx, $cy/$colsy); - }else - imagecopyresampled( $im, $stamp, $dst_x+$ofsleft, $dst_y+$ofstop, 0, 0, $dst_w, $dst_h, 100*$movex+100, 100*$movey+100); + if ($stamp == 0) { + game_printsnake( $im, $file, $dstx + $ofsleft, $dsty + $ofstop, $dstw, $dsth, $cx / $colsx, $cy / $colsy); + } else { + imagecopyresampled( $im, $stamp, $dstx + $ofsleft, $dsty + $ofstop, 0, 0, $dstw, $dsth, + 100 * $movex + 100, 100 * $movey + 100); + } } -function game_imagecreatefrompng( $file){ - if( file_exists( $file)) +function game_imagecreatefrompng( $file) { + if (file_exists( $file)) { return imagecreatefrompng( $file); + } return 0; } -function shownumber( $img_handle, $img_numbers, $number, $x1 , $y1, $width, $height, $size_numbers){ - if( $number < 10){ - $width_number = $size_numbers[ 0] / 10; - $dstX = $x1 + $width / 10; - $dstY = $y1 + $height / 10; - $srcX = $number * $size_numbers[ 0] / 10; - $srcW = $size_numbers[ 0]/10; - $srcH = $size_numbers[ 1]; - $dstW = $width / 10; - $dstH = $dstW * $srcH / $srcW; - imagecopyresampled( $img_handle, $img_numbers, $dstX, $dstY, $srcX, 0, $dstW, $dstH, $srcW, $srcH); - }else - { +function shownumber( $imghandle, $imgnumbers, $number, $x1 , $y1, $width, $height, $sizenumbers) { + if ($number < 10) { + $widthnumber = $sizenumbers[ 0] / 10; + $dstx = $x1 + $width / 10; + $dsty = $y1 + $height / 10; + $srcx = $number * $sizenumbers[ 0] / 10; + $srcw = $sizenumbers[ 0] / 10; + $srch = $sizenumbers[ 1]; + $dstw = $width / 10; + $dsth = $dstw * $srch / $srcw; + imagecopyresampled( $imghandle, $imgnumbers, $dstx, $dsty, $srcx, 0, $dstw, $dsth, $srcw, $srch); + } else { $number1 = floor( $number / 10); $number2 = $number % 10; - shownumber( $img_handle, $img_numbers, $number1, $x1-$width/20, $y1, $width, $height, $size_numbers); - shownumber( $img_handle, $img_numbers, $number2, $x1+$width/20, $y1, $width, $height, $size_numbers); + shownumber( $imghandle, $imgnumbers, $number1, $x1 - $width / 20, $y1, $width, $height, $sizenumbers); + shownumber( $imghandle, $imgnumbers, $number2, $x1 + $width / 20, $y1, $width, $height, $sizenumbers); } } -function returnRotatedPoint($x,$y,$cx,$cy,$a) - { - // radius using distance formula - $r = sqrt(pow(($x-$cx),2)+pow(($y-$cy),2)); - // initial angle in relation to center - $iA = rad2deg(atan2(($y-$cy),($x-$cx))); +function returnrotatedpoint($x, $y, $cx, $cy, $a) { + // Radius using distance formula. + $r = sqrt(pow(($x - $cx), 2) + pow(($y - $cy), 2)); - $nx = $r * cos(deg2rad($a + $iA)); - $ny = $r * sin(deg2rad($a + $iA)); + // Initial angle in relation to center. + $ia = rad2deg(atan2(($y - $cy), ($x - $cx))); - return array("x"=>$cx+$nx,"y"=>$cy+$ny); - } + $nx = $r * cos(deg2rad($a + $ia)); + $ny = $r * sin(deg2rad($a + $ia)); -function game_printladder( $im, $file, $x, $y, $width, $height, $cellx, $celly) -{ + return array("x" => $cx + $nx, "y" => $cy + $ny); +} + +function game_printladder( $im, $file, $x, $y, $width, $height, $cellx, $celly) { $color = imagecolorallocate($im, 0, 0, 255); - $x2 = $x+$width-$cellx/2; - $y2 = $y+$height-$celly/2; - $x1 = $x+$cellx/2; - $y1 = $y+$celly/2; + $x2 = $x + $width - $cellx / 2; + $y2 = $y + $height - $celly / 2; + $x1 = $x + $cellx / 2; + $y1 = $y + $celly / 2; imageline( $im, $x1, $y1, $x2, $y2, $color); - $r = sqrt(pow(($x2-$x1),2)+pow(($y2-$y1),2)); + $r = sqrt(pow(($x2 - $x1), 2) + pow(($y2 - $y1), 2)); $mul = 100 / $r; - $x1 = $x2 - ($x2-$x1) * $mul; - $y1 = $y2 - ($y2-$y1) * $mul; + $x1 = $x2 - ($x2 - $x1) * $mul; + $y1 = $y2 - ($y2 - $y1) * $mul; $a = returnRotatedPoint( $x1, $y1, $x2, $y2, 20); imageline( $im, $x2, $y2, $a[ 'x'], $a[ 'y'], $color); $a = returnRotatedPoint( $x1, $y1, $x2, $y2, -20); imageline( $im, $x2, $y2, $a[ 'x'], $a[ 'y'], $color); } -function game_printsnake( $im, $file, $x, $y, $width, $height, $cellx, $celly) -{ +function game_printsnake( $im, $file, $x, $y, $width, $height, $cellx, $celly) { $color = imagecolorallocate($im, 0, 255, 0); - $x2 = $x+$width-$cellx/2; - $y2 = $y+$height-$celly/2; - $x1 = $x+$cellx/2; - $y1 = $y+$celly/2; + $x2 = $x + $width - $cellx / 2; + $y2 = $y + $height - $celly / 2; + $x1 = $x + $cellx / 2; + $y1 = $y + $celly / 2; imageline( $im, $x1, $y1, $x2, $y2, $color); - - $r = sqrt(pow(($x2-$x1),2)+pow(($y2-$y1),2)); + + $r = sqrt(pow(($x2 - $x1), 2) + pow(($y2 - $y1), 2)); $mul = 100 / $r; - $x2 = $x1 + ($x2-$x1) * $mul; - $y2 = $y1 + ($y2-$y1) * $mul; + $x2 = $x1 + ($x2 - $x1) * $mul; + $y2 = $y1 + ($y2 - $y1) * $mul; $a = returnRotatedPoint( $x1, $y1, $x2, $y2, 80); imageline( $im, $x1, $y1, $a[ 'x'], $a[ 'y'], $color); $a = returnRotatedPoint( $x1, $y1, $x2, $y2, -80); diff --git a/snakes/play.php b/snakes/play.php index 38d3953..e1f9879 100755 --- a/snakes/play.php +++ b/snakes/play.php @@ -1,439 +1,442 @@ -id = $attempt->id; - $newrec->snakesdatabaseid = $game->param3; - if( $newrec->snakesdatabaseid == 0) - $newrec->snakesdatabaseid = 1; - $newrec->position = 1; - $newrec->queryid = 0; - $newrec->dice = rand( 1, 6); - if( !game_insert_record( 'game_snakes', $newrec)){ - print_error( 'game_snakes_continue: error inserting in game_snakes'); - } - - game_updateattempts( $game, $attempt, 0, 0); - - return game_snakes_play( $id, $game, $attempt, $newrec, $context); -} - -function game_snakes_play( $id, $game, $attempt, $snakes, $context) -{ - global $CFG, $DB, $OUTPUT; - - $board = game_snakes_get_board( $game); - $showboard = false; - - if( $snakes->position > $board->cols * $board->rows && $snakes->queryid <> 0){ - $finish = true; - - if (! $cm = $DB->get_record('course_modules', array( 'id' => $id))) { - print_error("Course Module ID was incorrect id=$id"); - } - - echo ''.get_string( 'win', 'game').'
'; - echo '
'; - echo "wwwroot/mod/game/attempt.php?id=$id\">".get_string( 'nextgame', 'game').'         '; - echo "wwwroot/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').' '; - - $gradeattempt = 1; - $finish = 1; - game_updateattempts( $game, $attempt, $gradeattempt, $finish); - }else - { - $finish = false; - if( $snakes->queryid == 0){ - game_snakes_computenextquestion( $game, $snakes, $query); - }else - { - $query = $DB->get_record( 'game_queries', array( 'id' => $snakes->queryid)); - } - if( $game->toptext != ''){ - echo $game->toptext.'
'; - } - $showboard = true; - } - - if( $showboard and $game->param8 == 0) - game_snakes_showquestion( $id, $game, $snakes, $query, $context); - -?> - - - - - - -
- -
- -
- - -
-bottomtext != ''){ - echo '
'.$game->bottomtext; - } - - if( $showboard and $game->param8 != 0) - game_snakes_showquestion( $id, $game, $snakes, $query, $context); -} - -function game_snakes_showdice( $snakes, $board) -{ - $pos = game_snakes_computeplayerposition( $snakes, $board); -?> -
-<?php print_string('snakes_player', 'game', ($snakes->position +1)); /*Accessibility. */ ?> -
- -
- <?php print_string('snakes_dice', 'game', $snakes->dice) ?> -
-position - 1) % $board->cols; - $y = floor( ($snakes->position-1) / $board->cols); - - $cellwidth = ($board->width - $board->headerx - $board->footerx) / $board->cols; - $cellheight = ($board->height - $board->headery - $board->footery) / $board->rows; - - $pos = new stdClass(); - $pos->width = 22; - $pos->height = 22; - - $pos->ofsx = 0; - $pos->ofsy = $pos->height; - - switch( $board->direction){ - case 1: - if( ($y % 2) == 1){ - $x = $board->cols - $x - 1; - } - $pos->x = $board->headerx + $x * $cellwidth + ($cellwidth - $pos->width)/2+ $pos->ofsx; - $pos->y = $board->footery + $y * $cellheight + ($cellheight - $pos->height)/2 + $pos->ofsy; - $pos->x = round( $pos->x); - $pos->y = round( -$pos->y); - break; - } - - return $pos; -} - -function game_snakes_computenextquestion( $game, &$snakes, &$query) -{ - global $DB, $USER; - - //Retrieves CONST_GAME_TRIES_REPETITION words and select the one which is used fewer times - if( ($recs = game_questions_selectrandom( $game, 1, CONST_GAME_TRIES_REPETITION)) == false){ - return false; - } - - $glossaryid = 0; - $questionid = 0; - $min_num = 0; - $query = new stdClass(); - foreach( $recs as $rec){ - $a = array( 'gameid' => $game->id, 'userid' => $USER->id, 'questionid' => $rec->questionid, 'glossaryentryid' => $rec->glossaryentryid); - if(($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions AS r')) != false){ - if( ($rec2->r < $min_num) or ($min_num == 0)){ - $min_num = $rec2->r; - $query->glossaryentryid = $rec->glossaryentryid; - $query->questionid = $rec->questionid; - } - } - else{ - $query->glossaryentryid = $rec->glossaryentryid; - $query->questionid = $rec->questionid; - break; - } - } - - if( ($query->glossaryentryid == 0) AND ($query->questionid == 0)) - return false; - - $query->attemptid = $snakes->id; - $query->gameid = $game->id; - $query->userid = $USER->id; - $query->sourcemodule = $game->sourcemodule; - $query->score = 0; - $query->timelastattempt = time(); - if( !($query->id = $DB->insert_record( 'game_queries', $query))){ - print_error( "Can't insert to table game_queries"); - } - - $snakes->queryid = $query->id; - - $updrec = new stdClass(); - $updrec->id = $snakes->id; - $updrec->queryid = $query->id; - $updrec->dice = $snakes->dice = rand( 1, 6); - - if( !$DB->update_record( 'game_snakes', $updrec)){ - print_error( 'game_questions_selectrandom: error updating in game_snakes'); - } - - game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid); - - return true; -} - -function game_snakes_showquestion( $id, $game, $snakes, $query, $context) -{ - if( $query->sourcemodule == 'glossary'){ - game_snakes_showquestion_glossary( $id, $snakes, $query, $game); - }else - { - game_snakes_showquestion_question( $game, $id, $snakes, $query, $context); - } -} - -function game_snakes_showquestion_question( $game, $id, $snakes, $query, $context) -{ - global $CFG; - - $questionlist = $query->questionid; - $questions = game_sudoku_getquestions( $questionlist); - - /// Start the form - echo "wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n"; - echo "
\n"; - - // Add a hidden field with the quiz id - echo '\n"; - echo ''; - echo '\n"; - - /// Print all the questions - foreach( $questions as $question) - game_print_question( $game, $question, $context); - // Add a hidden field with questionids - echo '\n"; - - echo "\n"; -} - -function game_snakes_showquestion_glossary( $id, $snakes, $query, $game) -{ - global $CFG, $DB; - - $entry = $DB->get_record( 'glossary_entries', array('id' => $query->glossaryentryid)); - - /// Start the form - echo "
wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n"; - echo "
\n"; - - // Add a hidden field with the queryid - echo '\n"; - echo ''; - echo '\n"; - - /// Print all the questions - - // Add a hidden field with glossaryentryid - echo '\n"; - - $cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course); - $contextglossary = game_get_context_module_instance( $cmglossary->id); - $s = game_filterglossary(str_replace( '\"', '"', $entry->definition), $query->glossaryentryid, $contextglossary->id, $game->course); - echo $s.'
'; - - echo get_string( 'answer').': '; - echo "
"; - - echo "
\n"; -} - - -function game_snakes_check_questions( $id, $game, $attempt, $snakes, $context) -{ - global $QTYPES, $CFG, $DB; - - $responses = data_submitted(); - - if( $responses->queryid != $snakes->queryid){ - game_snakes_play( $id, $game, $attempt, $snakes, $context); - return; - } - - $questionlist = $DB->get_field( 'game_queries', 'questionid', array( 'id' => $responses->queryid)); - - $questions = game_sudoku_getquestions( $questionlist); - $correct = false; - $query = ''; - foreach($questions as $question) { - $query = new stdClass(); - $query->id = $snakes->queryid; - - $grade = game_grade_responses( $question, $responses, 100, $answertext); - if( $grade < 99){ - //wrong answer - game_update_queries( $game, $attempt, $query, 0, $answertext); - continue; - } - - //correct answer - $correct = true; - - game_update_queries( $game, $attempt, $query, 1, ''); - } - - //set the grade of the whole game - game_snakes_position( $id, $game, $attempt, $snakes, $correct, $query, $context); -} - - -function game_snakes_check_glossary( $id, $game, $attempt, $snakes, $context) -{ - global $QTYPES, $CFG, $DB; - - $responses = data_submitted(); - - if( $responses->queryid != $snakes->queryid){ - game_snakes_play( $id, $game, $attempt, $snakes, $context); - return; - } - - $query = $DB->get_record( 'game_queries', array( 'id' => $responses->queryid)); - - $glossaryentry = $DB->get_record( 'glossary_entries', array( 'id' => $query->glossaryentryid)); - - $name = 'resp'.$query->glossaryentryid; - $useranswer = $responses->answer; - - if( game_upper( $useranswer) != game_upper( $glossaryentry->concept)){ - //wrong answer - $correct = false; - game_update_queries( $game, $attempt, $query, 0, $useranswer);//last param is grade - }else - { - //correct answer - $correct = true; - - game_update_queries( $game, $attempt, $query, 1, $useranswer);//last param is grade - } - - //set the grade of the whole game - game_snakes_position( $id, $game, $attempt, $snakes, $correct, $query, $context); -} - - -function game_snakes_position( $id, $game, $attempt, $snakes, $correct, $query, $context) -{ - global $DB; - - $data = $DB->get_field( 'game_snakes_database', 'data', array( 'id' => $snakes->snakesdatabaseid)); - - if( $correct){ - if( ($next=game_snakes_foundlander( $snakes->position + $snakes->dice, $data))){ - $snakes->position = $next; - }else - { - $snakes->position = $snakes->position + $snakes->dice; - } - }else - { - if( ($next=game_snakes_foundsnake( $snakes->position, $data))){ - $snakes->position = $next; - } - } - - $updrec = new stdClass(); - $updrec->id = $snakes->id; - $updrec->position = $snakes->position; - $updrec->queryid = 0; - - if( !$DB->update_record( 'game_snakes', $updrec)){ - print_error( "game_snakes_position: Can't update game_snakes"); - } - - $board = $DB->get_record_select( 'game_snakes_database', "id=$snakes->snakesdatabaseid"); - $gradeattempt = $snakes->position / ($board->cols * $board->rows); - $finished = ( $snakes->position > $board->cols * $board->rows ? 1 : 0); - - game_updateattempts( $game, $attempt, $gradeattempt, $finished); - - game_snakes_computenextquestion( $game, $snakes, $query); - - game_snakes_play( $id, $game, $attempt, $snakes, $context); -} - -//in lander go forward -function game_snakes_foundlander( $position, $data) -{ - preg_match( "/L$position-([0-9]*)/", $data, $matches); - - if( count( $matches)){ - return $matches[ 1]; - } - - return 0; -} - -//in snake go backward -function game_snakes_foundsnake( $position, $data) -{ - preg_match( "/S([0-9]*)-$position,/", $data.',', $matches); - - if( count( $matches)){ - return $matches[ 1]; - } - - return 0; -} - -function game_snakes_remove_attemptdata ($questionusageid, $questionid) { - global $DB; - - $sql = "SELECT qas.id - FROM mdl_question_attempts qa - LEFT JOIN mdl_question_attempt_steps qas - ON qa.id=qas.questionattemptid - WHERE questionusageid = $questionusageid - AND questionid = $questionid - AND state != 'todo'"; - - if ($stepdata = $DB->get_records_sql($sql)) { - foreach($stepdata as $step) { - if ($step->id > 0) { - $DB->delete_records('question_attempt_step_data', array('attemptstepid' => $step->id)); - $DB->get_records_sql("update {question_attempt_steps} set state='todo' where id = {$step->id}"); - } - } - } -} +. + +// This files plays the game "Snakes and Ladders". + +function game_snakes_continue( $id, $game, $attempt, $snakes, $context) { + if ($attempt != false and $snakes != false) { + return game_snakes_play( $id, $game, $attempt, $snakes, $context); + } + + if ($attempt === false) { + $attempt = game_addattempt( $game); + } + + $newrec = new stdClass(); + $newrec->id = $attempt->id; + $newrec->snakesdatabaseid = $game->param3; + if ($newrec->snakesdatabaseid == 0) { + $newrec->snakesdatabaseid = 1; + } + $newrec->position = 1; + $newrec->queryid = 0; + $newrec->dice = rand( 1, 6); + if (!game_insert_record( 'game_snakes', $newrec)) { + print_error( 'game_snakes_continue: error inserting in game_snakes'); + } + + game_updateattempts( $game, $attempt, 0, 0); + + return game_snakes_play( $id, $game, $attempt, $newrec, $context); +} + +function game_snakes_play( $id, $game, $attempt, $snakes, $context) { + global $CFG, $DB, $OUTPUT; + + $board = game_snakes_get_board( $game); + $showboard = false; + + if ($snakes->position > $board->cols * $board->rows && $snakes->queryid <> 0) { + $finish = true; + + if (! $cm = $DB->get_record('course_modules', array( 'id' => $id))) { + print_error("Course Module ID was incorrect id=$id"); + } + + echo ''.get_string( 'win', 'game').'
'; + echo '
'; + echo "wwwroot/mod/game/attempt.php?id=$id\">". + get_string( 'nextgame', 'game').'         '; + echo "wwwroot/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').' '; + + $gradeattempt = 1; + $finish = 1; + game_updateattempts( $game, $attempt, $gradeattempt, $finish); + } else { + $finish = false; + if ($snakes->queryid == 0) { + game_snakes_computenextquestion( $game, $snakes, $query); + } else { + $query = $DB->get_record( 'game_queries', array( 'id' => $snakes->queryid)); + } + if ($game->toptext != '') { + echo $game->toptext.'
'; + } + $showboard = true; + } + + if ($showboard and $game->param8 == 0) { + game_snakes_showquestion( $id, $game, $snakes, $query, $context); + } +?> + + + + + + +
+ +
+ +
+ + +
+bottomtext != '') { + echo '
'.$game->bottomtext; + } + + if ($showboard and $game->param8 != 0) { + game_snakes_showquestion( $id, $game, $snakes, $query, $context); + } +} + +function game_snakes_showdice( $snakes, $board) { + $pos = game_snakes_computeplayerposition( $snakes, $board); +?> +
+ +
+ +
+ <?php print_string('snakes_dice', 'game', $snakes->dice) ?> +
+position - 1) % $board->cols; + $y = floor( ($snakes->position - 1) / $board->cols); + + $cellwidth = ($board->width - $board->headerx - $board->footerx) / $board->cols; + $cellheight = ($board->height - $board->headery - $board->footery) / $board->rows; + + $pos = new stdClass(); + $pos->width = 22; + $pos->height = 22; + + $pos->ofsx = 0; + $pos->ofsy = $pos->height; + + switch( $board->direction) { + case 1: + if (($y % 2) == 1) { + $x = $board->cols - $x - 1; + } + $pos->x = $board->headerx + $x * $cellwidth + ($cellwidth - $pos->width) / 2 + $pos->ofsx; + $pos->y = $board->footery + $y * $cellheight + ($cellheight - $pos->height) / 2 + $pos->ofsy; + $pos->x = round( $pos->x); + $pos->y = round( -$pos->y); + break; + } + + return $pos; +} + +function game_snakes_computenextquestion( $game, &$snakes, &$query) { + global $DB, $USER; + + // Retrieves CONST_GAME_TRIES_REPETITION words and select the one which is used fewer times. + if (($recs = game_questions_selectrandom( $game, 1, CONST_GAME_TRIES_REPETITION)) == false) { + return false; + } + + $glossaryid = 0; + $questionid = 0; + $minnum = 0; + $query = new stdClass(); + foreach ($recs as $rec) { + $a = array( 'gameid' => $game->id, 'userid' => $USER->id, + 'questionid' => $rec->questionid, 'glossaryentryid' => $rec->glossaryentryid); + if (($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions AS r')) != false) { + if (($rec2->r < $minnum) or ($minnum == 0)) { + $minnum = $rec2->r; + $query->glossaryentryid = $rec->glossaryentryid; + $query->questionid = $rec->questionid; + } + } else { + $query->glossaryentryid = $rec->glossaryentryid; + $query->questionid = $rec->questionid; + break; + } + } + + if (($query->glossaryentryid == 0) and ($query->questionid == 0)) { + return false; + } + $query->attemptid = $snakes->id; + $query->gameid = $game->id; + $query->userid = $USER->id; + $query->sourcemodule = $game->sourcemodule; + $query->score = 0; + $query->timelastattempt = time(); + if (!($query->id = $DB->insert_record( 'game_queries', $query))) { + print_error( "Can't insert to table game_queries"); + } + + $snakes->queryid = $query->id; + + $updrec = new stdClass(); + $updrec->id = $snakes->id; + $updrec->queryid = $query->id; + $updrec->dice = $snakes->dice = rand( 1, 6); + + if (!$DB->update_record( 'game_snakes', $updrec)) { + print_error( 'game_questions_selectrandom: error updating in game_snakes'); + } + + game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid); + + return true; +} + +function game_snakes_showquestion( $id, $game, $snakes, $query, $context) { + if ($query->sourcemodule == 'glossary') { + game_snakes_showquestion_glossary( $id, $snakes, $query, $game); + } else { + game_snakes_showquestion_question( $game, $id, $snakes, $query, $context); + } +} + +function game_snakes_showquestion_question( $game, $id, $snakes, $query, $context) { + global $CFG; + + $questionlist = $query->questionid; + $questions = game_sudoku_getquestions( $questionlist); + + // Start the form. + echo "
wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n"; + echo "
\n"; + + // Add a hidden field with the quiz id. + echo '\n"; + echo ''; + echo '\n"; + + // Print all the questions. + foreach ($questions as $question) { + game_print_question( $game, $question, $context); + } + // Add a hidden field with questionids. + echo '\n"; + + echo "
\n"; +} + +function game_snakes_showquestion_glossary( $id, $snakes, $query, $game) { + global $CFG, $DB; + + $entry = $DB->get_record( 'glossary_entries', array('id' => $query->glossaryentryid)); + + // Start the form. + echo "
wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n"; + echo "
\n"; + + // Add a hidden field with the queryid. + echo '\n"; + echo ''; + echo '\n"; + + // Print all the questions. + + // Add a hidden field with glossaryentryid. + echo '\n"; + + $cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course); + $contextglossary = game_get_context_module_instance( $cmglossary->id); + $s = game_filterglossary(str_replace( '\"', '"', $entry->definition), $query->glossaryentryid, + $contextglossary->id, $game->course); + echo $s.'
'; + + echo get_string( 'answer').': '; + echo "
"; + + echo "
\n"; +} + +function game_snakes_check_questions( $id, $game, $attempt, $snakes, $context) { + global $QTYPES, $CFG, $DB; + + $responses = data_submitted(); + + if ($responses->queryid != $snakes->queryid) { + game_snakes_play( $id, $game, $attempt, $snakes, $context); + return; + } + + $questionlist = $DB->get_field( 'game_queries', 'questionid', array( 'id' => $responses->queryid)); + + $questions = game_sudoku_getquestions( $questionlist); + $correct = false; + $query = ''; + foreach ($questions as $question) { + $query = new stdClass(); + $query->id = $snakes->queryid; + + $grade = game_grade_responses( $question, $responses, 100, $answertext); + if ($grade < 99) { + // Wrong answer. + game_update_queries( $game, $attempt, $query, 0, $answertext); + continue; + } + + // Correct answer. + $correct = true; + + game_update_queries( $game, $attempt, $query, 1, ''); + } + + // Set the grade of the whole game. + game_snakes_position( $id, $game, $attempt, $snakes, $correct, $query, $context); +} + +function game_snakes_check_glossary( $id, $game, $attempt, $snakes, $context) { + global $QTYPES, $CFG, $DB; + + $responses = data_submitted(); + + if ($responses->queryid != $snakes->queryid) { + game_snakes_play( $id, $game, $attempt, $snakes, $context); + return; + } + + $query = $DB->get_record( 'game_queries', array( 'id' => $responses->queryid)); + + $glossaryentry = $DB->get_record( 'glossary_entries', array( 'id' => $query->glossaryentryid)); + + $name = 'resp'.$query->glossaryentryid; + $useranswer = $responses->answer; + + if (game_upper( $useranswer) != game_upper( $glossaryentry->concept)) { + // Wrong answer. + $correct = false; + game_update_queries( $game, $attempt, $query, 0, $useranswer); // Last param is grade. + } else { + // Correct answer. + $correct = true; + + game_update_queries( $game, $attempt, $query, 1, $useranswer); // Last param is grade. + } + + // Set the grade of the whole game. + game_snakes_position( $id, $game, $attempt, $snakes, $correct, $query, $context); +} + +function game_snakes_position( $id, $game, $attempt, $snakes, $correct, $query, $context) { + global $DB; + + $data = $DB->get_field( 'game_snakes_database', 'data', array( 'id' => $snakes->snakesdatabaseid)); + + if ($correct) { + if (($next = game_snakes_foundlander( $snakes->position + $snakes->dice, $data))) { + $snakes->position = $next; + } else { + $snakes->position = $snakes->position + $snakes->dice; + } + } else { + if (($next = game_snakes_foundsnake( $snakes->position, $data))) { + $snakes->position = $next; + } + } + + $updrec = new stdClass(); + $updrec->id = $snakes->id; + $updrec->position = $snakes->position; + $updrec->queryid = 0; + + if (!$DB->update_record( 'game_snakes', $updrec)) { + print_error( "game_snakes_position: Can't update game_snakes"); + } + + $board = $DB->get_record_select( 'game_snakes_database', "id=$snakes->snakesdatabaseid"); + $gradeattempt = $snakes->position / ($board->cols * $board->rows); + $finished = ( $snakes->position > $board->cols * $board->rows ? 1 : 0); + + game_updateattempts( $game, $attempt, $gradeattempt, $finished); + + game_snakes_computenextquestion( $game, $snakes, $query); + + game_snakes_play( $id, $game, $attempt, $snakes, $context); +} + +// In lander go forward. +function game_snakes_foundlander( $position, $data) { + preg_match( "/L$position-([0-9]*)/", $data, $matches); + + if (count( $matches)) { + return $matches[ 1]; + } + + return 0; +} + +// In snake go backward. +function game_snakes_foundsnake( $position, $data) { + preg_match( "/S([0-9]*)-$position,/", $data.',', $matches); + + if (count( $matches)) { + return $matches[ 1]; + } + + return 0; +} + +function game_snakes_remove_attemptdata ($questionusageid, $questionid) { + global $DB; + + $sql = "SELECT qas.id + FROM mdl_question_attempts qa + LEFT JOIN mdl_question_attempt_steps qas + ON qa.id=qas.questionattemptid + WHERE questionusageid = $questionusageid + AND questionid = $questionid + AND state != 'todo'"; + + if ($stepdata = $DB->get_records_sql($sql)) { + foreach ($stepdata as $step) { + if ($step->id > 0) { + $DB->delete_records('question_attempt_step_data', array('attemptstepid' => $step->id)); + $DB->get_records_sql("update {question_attempt_steps} set state='todo' where id = {$step->id}"); + } + } + } +}