Browse Source

Fix:Coding style

MOODLE_20_STABLE
Vasilis Daloukas 9 years ago
parent
commit
b5f11fca18
  1. 81
      lang/en/game.php
  2. 1117
      millionaire/play.php
  3. 70
      report/default.php
  4. 334
      report/overview/report.php
  5. 340
      snakes/createboard.php
  6. 881
      snakes/play.php

81
lang/en/game.php

@ -1,31 +1,46 @@
<?php // $Id: game.php,v 1.22 2012/07/26 05:38:59 bdaloukas Exp $
// translated by Vasilis Daloukas
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
//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 <b>ONLY 1</b> 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 <b>{$a}</b> 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'] = '<BIG><B>Game over</B></BIG>';
$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';

1117
millionaire/play.php

File diff suppressed because it is too large

70
report/default.php

@ -1,49 +1,63 @@
<?php // $Id: default.php,v 1.2 2012/07/25 11:16:07 bdaloukas Exp $
////////////////////////////////////////////////////////////////////
/// 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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/*
* 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), "",
"<a href=\"index.php?id=$course->id\">$strgames</a>
-> ".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');
}
}
?>

334
report/overview/report.php

@ -1,4 +1,19 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This 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&amp;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 = '<a class="dimmed" href="'.$CFG->wwwroot.'/user/view.php?id='.$attempt->userid.'&amp;course='.$course->id.'">'.fullname($attempt).'</a>';
//}
//else {
$userlink = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$attempt->userid.'&amp;course='.$course->id.'">'.fullname($attempt).'</a>';
//}
/* 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 = '<a class="dimmed" href="'.$CFG->wwwroot.
* '/user/view.php?id='.$attempt->userid.'&amp;course='.$course->id.'">'.fullname($attempt).'</a>';
*}
*else {
* $userlink = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.
* $attempt->userid.'&amp;course='.$course->id.'">'.fullname($attempt).'</a>';
*}
*/
if (!$download) {
$row = array(
'<input type="checkbox" name="attemptid[]" value="'.$attempt->attempt.'" />',
$picture,
$userlink,
empty($attempt->attempt) ? '-' : '<a href="review.php?q='.$game->id.'&amp;attempt='.$attempt->attempt.'">'.userdate($attempt->timestart, $strtimeformat).'</a>',
empty($attempt->timefinish) ? '-' : '<a href="review.php?q='.$game->id.'&amp;attempt='.$attempt->attempt.'">'.userdate($attempt->timefinish, $strtimeformat).'</a>',
empty($attempt->attempt) ? '-' : (empty($attempt->timefinish) ? get_string('unfinished', 'game') : format_time($attempt->duration))
empty($attempt->attempt) ? '-' : '<a href="review.php?q='.
$game->id.'&amp;attempt='.$attempt->attempt.'">'.
userdate($attempt->timestart, $strtimeformat).'</a>',
empty($attempt->timefinish) ? '-' : '<a href="review.php?q='.
$game->id.'&amp;attempt='.$attempt->attempt.'">'.
userdate($attempt->timefinish, $strtimeformat).'</a>',
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 ? '-' : '<a href="review.php?q='.$game->id.'&amp;attempt='.$attempt->attempt.'">'.round($attempt->score * $game->grade,$game->decimalpoints).'</a>';
$row[] = $attempt->score === null ? '-' : '<a href="review.php?q='.
$game->id.'&amp;attempt='.$attempt->attempt.'">'.
round($attempt->score * $game->grade, $game->decimalpoints).'</a>';
} 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.'&amp;number='.$questions[$questionid]->number, 'reviewquestion', $grade, 450, 650, $strreviewquestion, 'none', true);
$row[] = link_to_popup_window (
'/mod/game/reviewquestion.php?state='.
$gradedstateid.'&amp;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 '<div id="tablecontainer">';
echo '<form id="attemptsform" method="post" action="report.php" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? confirm(\''.$strreallydel.'\') : true);">';
echo '<form id="attemptsform" method="post" action="report.php" '.
'onsubmit="var menu = document.getElementById(\'menuaction\'); '.
'return (menu.options[menu.selectedIndex].value == \'delete\' ? confirm(\''.$strreallydel.'\') : true);">';
echo '<div>';
echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
echo '<input type="hidden" name="mode" value="overview" />';
// Print table
// Print table.
$table->print_html();
// Print "Select all" etc.
// Print "Select all" etc..
if (!empty($attempts)) {
echo '<table id="commands">';
echo '<tr><td>';
echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'game').'</a> / ';
echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'game').'</a> ';
echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.
get_string('selectall', 'game').'</a> / ';
echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.
get_string('selectnone', 'game').'</a> ';
echo '&nbsp;&nbsp;';
$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 '<noscript id="noscriptmenuaction" style="display: inline;"><div>';
echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
echo '<script type="text/javascript">'."\n<!--\n".
'document.getElementById("noscriptmenuaction").style.display = "none";'
."\n-->\n".'</script>';
echo '</td></tr></table>';
}
// Close form
// Close form.
echo '</div>';
echo '</form></div>';
@ -545,7 +592,7 @@ class game_report extends game_default_report {
$options['detailedmarks'] = $detailedmarks;
echo '<td>';
$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 "</td>\n";
echo '<td>';
$options["download"] = "Excel";
@ -572,7 +619,7 @@ class game_report extends game_default_report {
$table->print_html();
}
}
// Print display options
// Print display options.
echo '<div class="controls">';
echo '<form id="options" action="report.php" method="get">';
echo '<div>';
@ -589,16 +636,19 @@ class game_report extends game_default_report {
echo '</tr>';
echo '<tr align="left">';
echo '<td colspan="2">';
$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 '</td></tr>';
echo '<tr align="left">';
echo '<td colspan="2"><input type="checkbox" id="checkdetailedmarks" name="detailedmarks" '.($detailedmarks?'checked="checked" ':'').'value="1" /> <label for="checkdetailedmarks">'.get_string('showdetailedmarks', 'game').'</label> ';
echo '<td colspan="2"><input type="checkbox" id="checkdetailedmarks" name="detailedmarks" '.
($detailedmarks ? 'checked="checked" ' : '').
'value="1" /> <label for="checkdetailedmarks">'.
get_string('showdetailedmarks', 'game').'</label> ';
echo '</td></tr>';
echo '<tr><td colspan="2" align="center">';
echo '<input type="submit" value="'.get_string('go').'" />';
@ -611,5 +661,3 @@ class game_report extends game_default_report {
return true;
}
}
?>

340
snakes/createboard.php

@ -1,26 +1,30 @@
<?php // $Id: createboard.php,v 1.5 2012/07/25 11:16:07 bdaloukas Exp $
// This file creates a board for "Snakes and Ladders"
/*
require( "../../../config.php");
$im=game_createsnakesboard( $_GET[ 'file'], $_GET[ 'colsx'], $_GET[ 'colsy'], $_GET[ 'ofstop'], $_GET[ 'ofsbottom'], $_GET[ 'ofsright'], $_GET[ 'ofsleft'], $_GET[ 'aboard']);
header('Content-type: image/jpg');
imagejpeg($im);
imagedestroy($im);
*/
function game_createsnakesboard($imageasstring, $colsx, $colsy, $ofstop, $ofsbottom, $ofsright, $ofsleft, $board, $setwidth, $setheight)
{
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
// This file 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);

881
snakes/play.php

@ -1,439 +1,442 @@
<?php // $Id: play.php,v 1.26 2012/07/26 05:39:00 bdaloukas Exp $
// 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 '<B>'.get_string( 'win', 'game').'</B><BR>';
echo '<br>';
echo "<a href=\"$CFG->wwwroot/mod/game/attempt.php?id=$id\">".get_string( 'nextgame', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp; ';
echo "<a href=\"$CFG->wwwroot/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'</a> ';
$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.'<br>';
}
$showboard = true;
}
if( $showboard and $game->param8 == 0)
game_snakes_showquestion( $id, $game, $snakes, $query, $context);
?>
<script language="javascript" event="onload" for="window">
<!--
var retVal = new Array();
var elements = document.getElementsByTagName("*");
for(var i = 0;i < elements.length;i++){
if( elements[ i].type == 'text'){
elements[ i].focus();
break;
}
}
-->
</script>
<table>
<tr>
<td>
<div id="board" STYLE="position:relative; left:0px;top:0px; width:<?php p($board->width); ?>px; height:<?php p($board->height); ?>px;">
<img src="<?php echo $board->imagesrc; ?>"></img>
</div>
<?php
if( $finish == false){
game_snakes_showdice( $snakes, $board);
}
?>
</td>
</tr>
</table>
<?php
if( $game->bottomtext != ''){
echo '<br>'.$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);
?>
<div ID="player1" STYLE="position:relative; left:<?php p( $pos->x);?>px; top:<?php p( $pos->y);?>px;" >
<img src="snakes/1/player1.png" alt="<?php print_string('snakes_player', 'game', ($snakes->position +1)); /*Accessibility. */ ?>" width="<?php echo $pos->width; ?>" height="<?php echo $pos->height; ?>"/>
</div>
<div ID="dice" STYLE="position:relative; left:<?php p( $board->width + round($board->width/3));?>px;top:<?php p( -2*round($board->height/3));?>px; ">
<img src="snakes/1/dice<?php p($snakes->dice);?>.png" alt="<?php print_string('snakes_dice', 'game', $snakes->dice) ?>" />
</div>
<?php
}
function game_snakes_computeplayerposition( $snakes, $board)
{
$x = ($snakes->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 "<form id=\"responseform\" method=\"post\" action=\"{$CFG->wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n";
echo "<center><input type=\"submit\" name=\"finishattempt\" value=\"".get_string('sudoku_submit', 'game')."\"></center>\n";
// Add a hidden field with the quiz id
echo '<input type="hidden" name="id" value="' . s($id) . "\" />\n";
echo '<input type="hidden" name="action" value="snakescheck" />';
echo '<input type="hidden" name="queryid" value="' . $query->id . "\" />\n";
/// Print all the questions
foreach( $questions as $question)
game_print_question( $game, $question, $context);
// Add a hidden field with questionids
echo '<input type="hidden" name="questionids" value="'.$questionlist."\" />\n";
echo "</form>\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 "<form id=\"responseform\" method=\"post\" action=\"{$CFG->wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n";
echo "<center><input type=\"submit\" name=\"finishattempt\" value=\"".get_string('sudoku_submit', 'game')."\"></center>\n";
// Add a hidden field with the queryid
echo '<input type="hidden" name="id" value="' . s($id) . "\" />\n";
echo '<input type="hidden" name="action" value="snakescheckg" />';
echo '<input type="hidden" name="queryid" value="' . $query->id . "\" />\n";
/// Print all the questions
// Add a hidden field with glossaryentryid
echo '<input type="hidden" name="glossaryentryid" value="'.$query->glossaryentryid."\" />\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.'<br>';
echo get_string( 'answer').': ';
echo "<input type=\"text\" name=\"answer\" size=30 /><br>";
echo "</form>\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}");
}
}
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
// This 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 '<B>'.get_string( 'win', 'game').'</B><BR>';
echo '<br>';
echo "<a href=\"$CFG->wwwroot/mod/game/attempt.php?id=$id\">".
get_string( 'nextgame', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp; ';
echo "<a href=\"$CFG->wwwroot/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'</a> ';
$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.'<br>';
}
$showboard = true;
}
if ($showboard and $game->param8 == 0) {
game_snakes_showquestion( $id, $game, $snakes, $query, $context);
}
?>
<script language="javascript" event="onload" for="window">
<!--
var retVal = new Array();
var elements = document.getElementsByTagName("*");
for(var i = 0;i < elements.length;i++){
if( elements[ i].type == 'text'){
elements[ i].focus();
break;
}
}
-->
</script>
<table>
<tr>
<td>
<div id="board" STYLE="position:relative; left:0px;top:0px;
width:<?php p($board->width); ?>px; height:<?php p($board->height); ?>px;">
<img src="<?php echo $board->imagesrc; ?>"></img>
</div>
<?php
if ($finish == false) {
game_snakes_showdice( $snakes, $board);
}
?>
</td>
</tr>
</table>
<?php
if ($game->bottomtext != '') {
echo '<br>'.$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);
?>
<div ID="player1" STYLE="position:relative; left:<?php p( $pos->x);?>px; top:<?php p( $pos->y);?>px;" >
<img src="snakes/1/player1.png"
alt="<?php print_string('snakes_player', 'game', ($snakes->position + 1)); /*Accessibility. */ ?>"
width="<?php echo $pos->width; ?>"
height="<?php echo $pos->height; ?>"/>
</div>
<div ID="dice" STYLE="position:relative;
left:<?php p( $board->width + round($board->width / 3)); ?>px;
top:<?php p( -2 * round($board->height / 3));?>px; ">
<img src="snakes/1/dice<?php p($snakes->dice);?>.png" alt="<?php print_string('snakes_dice', 'game', $snakes->dice) ?>" />
</div>
<?php
}
function game_snakes_computeplayerposition( $snakes, $board) {
$x = ($snakes->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 "<form id=\"responseform\" method=\"post\" ".
"action=\"{$CFG->wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n";
echo "<center><input type=\"submit\" name=\"finishattempt\" value=\"".get_string('sudoku_submit', 'game')."\"></center>\n";
// Add a hidden field with the quiz id.
echo '<input type="hidden" name="id" value="' . s($id) . "\" />\n";
echo '<input type="hidden" name="action" value="snakescheck" />';
echo '<input type="hidden" name="queryid" value="' . $query->id . "\" />\n";
// Print all the questions.
foreach ($questions as $question) {
game_print_question( $game, $question, $context);
}
// Add a hidden field with questionids.
echo '<input type="hidden" name="questionids" value="'.$questionlist."\" />\n";
echo "</form>\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 "<form id=\"responseform\" method=\"post\" ".
"action=\"{$CFG->wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n";
echo "<center><input type=\"submit\" name=\"finishattempt\" value=\"".get_string('sudoku_submit', 'game')."\"></center>\n";
// Add a hidden field with the queryid.
echo '<input type="hidden" name="id" value="' . s($id) . "\" />\n";
echo '<input type="hidden" name="action" value="snakescheckg" />';
echo '<input type="hidden" name="queryid" value="' . $query->id . "\" />\n";
// Print all the questions.
// Add a hidden field with glossaryentryid.
echo '<input type="hidden" name="glossaryentryid" value="'.$query->glossaryentryid."\" />\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.'<br>';
echo get_string( 'answer').': ';
echo "<input type=\"text\" name=\"answer\" size=30 /><br>";
echo "</form>\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}");
}
}
}
}

Loading…
Cancel
Save