Browse Source

Fix:Coding style

MOODLE_20_STABLE
Vasilis Daloukas 9 years ago
parent
commit
f28cb9eb36
  1. 397
      cryptex/cryptexdb_class.php
  2. 454
      cryptex/play.php
  3. 24
      db/access.php
  4. 15
      db/createsnakes.php
  5. 34
      db/importsnakes.php
  6. 5392
      db/importsudoku.php
  7. 263
      db/upgrade.php
  8. 712
      hangman/play.php
  9. 114
      hiddenpicture/picture.php
  10. 456
      hiddenpicture/play.php
  11. 12
      lib.php

397
cryptex/cryptexdb_class.php

@ -1,229 +1,220 @@
<?PHP <?php
// This file is part of Moodle - http://moodle.org/
class CryptexDB extends CrossDB //
{ // Moodle is free software: you can redistribute it and/or modify
function savecryptex( $game, &$crossm, $crossd, $id, $letters) // it under the terms of the GNU General Public License as published by
{ // the Free Software Foundation, either version 3 of the License, or
global $USER; // (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/>.
class CryptexDB extends CrossDB {
public function savecryptex( $game, &$crossm, $crossd, $id, $letters) {
global $USER;
CrossDB::delete_records( $id); CrossDB::delete_records( $id);
if( (CrossDB::savecross( $game, $crossm, $crossd, $id)) == false){ if ((CrossDB::savecross( $game, $crossm, $crossd, $id)) == false) {
return false; return false;
} }
$crossm->id = $id; $crossm->id = $id;
$newrec = new stdClass(); $newrec = new stdClass();
$newrec->id = $id; $newrec->id = $id;
$newrec->letters = $letters; $newrec->letters = $letters;
if (!($cryptexid = game_insert_record( "game_cryptex", $newrec))){ if (!($cryptexid = game_insert_record( "game_cryptex", $newrec))) {
print_error( 'Insert page: new page game_cryptex not inserted'); print_error( 'Insert page: new page game_cryptex not inserted');
} }
return $newrec; return $newrec;
}
function computeletters( $crossm, $crossd)
{
$letters = '';
$cols = $crossm->cols + 1;
$letters = str_repeat('.', $crossm->cols).'#';
$letters = str_repeat($letters, $crossm->rows) ;
$freqs1 = array();
$count1 = $count2 = 0;
foreach( $crossd as $rec)
{
$pos = $rec->col - 1 + ($rec->row-1) * $cols;
$s = $rec->answertext;
$len = game_strlen( $s);
$a = array();
for( $i=0; $i < $len; $i++){
$a[] = game_substr( $s, $i, 1);
}
for( $i=0; $i < $len; $i++){
$this->setchar( $letters, $pos, $a[ $i]);
$pos += ( $rec->horizontal ? 1 : $cols);
$freqs1[ ++$count1] = $a[ $i];
if( $i+1 < $len){
$freqs2[ ++$count2] = $a[ $i].$a[ $i+1];
}
}
}
$len = game_strlen( $letters);
$spaces = 0;
for( $i=0; $i < $len; $i++){
if( game_substr( $letters, $i, 1) == '.'){
$spaces++;
}
}
$step = 1;
while( $spaces)
{
if( $step == 1){
$step = 2;
$i = array_rand( $freqs1);
$this->insertchar( $letters, $crossm->cols, $crossm->rows, $freqs1[ $i], $spaces);
}else
{
$step=1;
$i = array_rand( $freqs2);
$this->insertchars( $letters, $crossm->cols, $crossm->rows, $freqs2[ $i], $spaces);
}
}
$ret_letters = "";
for( $row=0; $row < $crossm->rows; $row++){
$ret_letters .= game_substr( $letters, $cols * $row, ($cols-1));
}
return $ret_letters;
}
function displaycryptex( $cols, $rows, $letters, $mask, $showsolution, $textdir)
{
echo "<table border=1 $textdir>";
for( $row=0; $row < $rows; $row++)
{
echo "<tr>";
for( $col=0; $col < $cols; $col++){
$pos = $cols * $row+$col;
$c = game_substr( $letters, $pos, 1);
$m = game_substr( $mask, $pos, 1);
if( $showsolution and $m > '0'){
echo "<td align=center><b><FONT color=red>".$c."</font></td>";
}else if( $m == '1'){
echo "<td align=center><b><FONT color=red>".$c."</font></td>";
}else
{
echo "<td align=center>".$c."</td>";
}
}
echo "</tr>\r\n";
}
echo "</table>";
} }
function insertchar( &$letters, $cols, $rows, $char, &$spaces) public function computeletters( $crossm, $crossd) {
{ $letters = '';
$len = game_strlen( $letters); $cols = $crossm->cols + 1;
for( $i=0; $i < $len; $i++){ $letters = str_repeat('.', $crossm->cols).'#';
if( game_substr( $letters, $i, 1) == '.'){ $letters = str_repeat($letters, $crossm->rows);
$this->setchar( $letters, $i, $char);
$spaces--; $freqs1 = array();
return; $count1 = $count2 = 0;
} foreach ($crossd as $rec) {
} $pos = $rec->col - 1 + ($rec->row - 1) * $cols;
} $s = $rec->answertext;
$len = game_strlen( $s);
function insertchars( &$letters, $cols, $rows, $char, &$spaces)
{ $a = array();
$len = game_strlen( $letters); for ($i = 0; $i < $len; $i++) {
for( $i=0; $i < $len; $i++){ $a[] = game_substr( $s, $i, 1);
if( game_substr( $letters, $i, 1) == '.' and game_substr( $letters, $i+1, 1) == '.' ){ }
$this->setchar( $letters, $i, game_substr( $char, 0, 1));
$this->setchar( $letters, $i+1, game_substr( $char, 1, 1)); for ($i = 0; $i < $len; $i++) {
$spaces-=2; $this->setchar( $letters, $pos, $a[ $i]);
return true; $pos += ( $rec->horizontal ? 1 : $cols);
}
if( game_substr( $letters, $i, 1) == '.' and game_substr( $letters, $i+$cols+1, 1) == '.' ){ $freqs1[ ++$count1] = $a[ $i];
$this->setchar( $letters, $i, game_substr( $char, 0, 1)); if ($i + 1 < $len) {
$this->setchar( $letters, $i + $cols+1, game_substr( $char, 1, 1)); $freqs2[ ++$count2] = $a[ $i].$a[ $i + 1];
$spaces-=2; }
return true; }
} }
}
$len = game_strlen( $letters);
return false; $spaces = 0;
} for ($i = 0; $i < $len; $i++) {
if (game_substr( $letters, $i, 1) == '.') {
function gethash( $word) $spaces++;
{ }
$x = 37; }
$len = count( game_strlen( $word));
$step = 1;
for($i=0; $i < $len; $i++){ while ($spaces) {
$x = $x xor ord( game_substr( $word, $i, 1)); if ($step == 1) {
} $step = 2;
$i = array_rand( $freqs1);
return $x; $this->insertchar( $letters, $crossm->cols, $crossm->rows, $freqs1[ $i], $spaces);
} } else {
$step = 1;
function loadcryptex( $crossm, &$mask, &$corrects, &$language) $i = array_rand( $freqs2);
{ $this->insertchars( $letters, $crossm->cols, $crossm->rows, $freqs2[ $i], $spaces);
global $DB; }
}
$retletters = "";
for ($row = 0; $row < $crossm->rows; $row++) {
$retletters .= game_substr( $letters, $cols * $row, ($cols - 1));
}
return $retletters;
}
$questions = array(); public function displaycryptex( $cols, $rows, $letters, $mask, $showsolution, $textdir) {
$corrects = array(); echo "<table border=1 $textdir>";
for ($row = 0; $row < $rows; $row++) {
echo "<tr>";
for ($col = 0; $col < $cols; $col++) {
$pos = $cols * $row + $col;
$c = game_substr( $letters, $pos, 1);
$m = game_substr( $mask, $pos, 1);
if ($showsolution and $m > '0') {
echo "<td align=center><b><FONT color=red>".$c."</font></td>";
} else if ( $m == '1') {
echo "<td align=center><b><FONT color=red>".$c."</font></td>";
} else {
echo "<td align=center>".$c."</td>";
}
}
echo "</tr>\r\n";
}
echo "</table>";
}
$mask = str_repeat( '0', $crossm->cols * $crossm->rows); public function insertchar( &$letters, $cols, $rows, $char, &$spaces) {
$len = game_strlen( $letters);
for ($i = 0; $i < $len; $i++) {
if (game_substr( $letters, $i, 1) == '.') {
$this->setchar( $letters, $i, $char);
$spaces--;
return;
}
}
}
if ($recs = $DB->get_records( 'game_queries', array( 'attemptid' => $crossm->id))) public function insertchars( &$letters, $cols, $rows, $char, &$spaces) {
{ $len = game_strlen( $letters);
foreach ($recs as $rec) for ($i = 0; $i < $len; $i++) {
{ if (game_substr( $letters, $i, 1) == '.' and game_substr( $letters, $i + 1, 1) == '.' ) {
if( $rec->questiontext == ''){ $this->setchar( $letters, $i, game_substr( $char, 0, 1));
$rec->questiontext = ' '; $this->setchar( $letters, $i + 1, game_substr( $char, 1, 1));
} $spaces -= 2;
$key = $this->gethash( $rec->questiontext).'-'.$rec->answertext.'-'.$rec->id; return true;
$questions[ $key] = $rec; }
if (game_substr( $letters, $i, 1) == '.' and game_substr( $letters, $i + $cols + 1, 1) == '.' ) {
$this->setchar( $letters, $i, game_substr( $char, 0, 1));
$this->setchar( $letters, $i + $cols + 1, game_substr( $char, 1, 1));
$spaces -= 2;
return true;
}
}
return false;
}
$word = $rec->answertext; public function gethash( $word) {
$pos = $crossm->cols * ($rec->row-1)+($rec->col-1); $x = 37;
$len = game_strlen( $word); $len = count( game_strlen( $word));
$found = ($rec->answertext == $rec->studentanswer);
for( $i=0; $i < $len; $i++) for ($i = 0; $i < $len; $i++) {
{ $x = $x xor ord( game_substr( $word, $i, 1));
$c = ( $found ? '1' : '2'); }
if( game_substr( $mask, $pos, 1) != '1'){ return $x;
game_setchar( $mask, $pos, $c); }
}
$pos += ($rec->horizontal ? 1 : $crossm->cols); public function loadcryptex( $crossm, &$mask, &$corrects, &$language) {
} global $DB;
if( $found){ $questions = array();
$corrects[ $rec->id] = 1; $corrects = array();
}
if( $language == ''){ $mask = str_repeat( '0', $crossm->cols * $crossm->rows);
$language = game_detectlanguage( $rec->answertext);
if ($recs = $DB->get_records( 'game_queries', array( 'attemptid' => $crossm->id))) {
foreach ($recs as $rec) {
if ($rec->questiontext == '') {
$rec->questiontext = ' ';
} }
} $key = $this->gethash( $rec->questiontext).'-'.$rec->answertext.'-'.$rec->id;
ksort( $questions); $questions[ $key] = $rec;
}
return $questions; $word = $rec->answertext;
} $pos = $crossm->cols * ($rec->row - 1) + ($rec->col - 1);
$len = game_strlen( $word);
$found = ($rec->answertext == $rec->studentanswer);
for ($i = 0; $i < $len; $i++) {
$c = ( $found ? '1' : '2');
function setwords( $answers, $maxcols, $reps) if (game_substr( $mask, $pos, 1) != '1') {
{ game_setchar( $mask, $pos, $c);
return Cross::setwords( $answers, $maxcols, $reps); }
}
function computedata( &$crossm, &$crossd, &$letters, $minwords, $maxwords) $pos += ($rec->horizontal ? 1 : $crossm->cols);
{ }
if( !cross::computedata( $crossm, $crossd, $letters, $minwords, $maxwords)){
return false;
}
$letters = $this->computeletters( $crossm, $crossd); if ($found) {
$corrects[ $rec->id] = 1;
}
return true; if ($language == '') {
} $language = game_detectlanguage( $rec->answertext);
} }
}
ksort( $questions);
}
return $questions;
}
public function setwords( $answers, $maxcols, $reps) {
return Cross::setwords( $answers, $maxcols, $reps);
}
public function computedata( &$crossm, &$crossd, &$letters, $minwords, $maxwords) {
if (!cross::computedata( $crossm, $crossd, $letters, $minwords, $maxwords)) {
return false;
}
$letters = $this->computeletters( $crossm, $crossd);
return true;
}
}

454
cryptex/play.php

@ -1,4 +1,19 @@
<?php // $Id: play.php,v 1.20 2012/08/03 05:52:50 bdaloukas Exp $ <?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** /**
* This page plays the cryptex game * This page plays the cryptex game
* *
@ -9,210 +24,199 @@
require_once( "cryptexdb_class.php"); require_once( "cryptexdb_class.php");
function game_cryptex_continue( $id, $game, $attempt, $cryptexrec, $endofgame, $context) function game_cryptex_continue( $id, $game, $attempt, $cryptexrec, $endofgame, $context) {
{
global $DB, $USER; global $DB, $USER;
if( $endofgame){ if ($endofgame) {
game_updateattempts( $game, $attempt, -1, true); game_updateattempts( $game, $attempt, -1, true);
$endofgame = false; $endofgame = false;
} }
if( $attempt != false and $cryptexrec != false){ if $attempt != false and $cryptexrec != false) {
$crossm = $DB->get_record( 'game_cross', array( 'id' => $attempt->id)); $crossm = $DB->get_record( 'game_cross', array( 'id' => $attempt->id));
return game_cryptex_play( $id, $game, $attempt, $cryptexrec, $crossm, false, false, false, $context); return game_cryptex_play( $id, $game, $attempt, $cryptexrec, $crossm, false, false, false, $context);
} }
if( $attempt === false){ if ($attempt === false) {
$attempt = game_addattempt( $game); $attempt = game_addattempt( $game);
} }
$cryptex = new CryptexDB(); $cryptex = new CryptexDB();
$questions = array(); $questions = array();
$infos = array(); $infos = array();
$answers = array(); $answers = array();
$recs = game_questions_shortanswer( $game); $recs = game_questions_shortanswer( $game);
if( $recs == false){ if ($recs == false) {
print_error( get_string( 'no_words', 'game')); print_error( get_string( 'no_words', 'game'));
} }
$infos = array(); $infos = array();
$reps = array(); $reps = array();
foreach( $recs as $rec){ foreach ($recs as $rec) {
if( $game->param7 == false){ if ($game->param7 == false) {
if( game_strpos( $rec->answertext, ' ')){ if (game_strpos( $rec->answertext, ' ')) {
continue; //spaces not allowed continue; // Spaces not allowed.
} }
} }
$rec->answertext = game_upper( $rec->answertext); $rec->answertext = game_upper( $rec->answertext);
$answers[ $rec->answertext] = game_repairquestion( $rec->questiontext); $answers[ $rec->answertext] = game_repairquestion( $rec->questiontext);
$infos[ $rec->answertext] = array( $game->sourcemodule, $rec->questionid, $rec->glossaryentryid); $infos[ $rec->answertext] = array( $game->sourcemodule, $rec->questionid, $rec->glossaryentryid);
$a = array( 'gameid' => $game->id, 'userid' => $USER->id, 'questionid' => $rec->questionid, 'glossaryentryid' => $rec->glossaryentryid); $a = array( 'gameid' => $game->id, 'userid' => $USER->id,
if(($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions AS r')) != false){ 'questionid' => $rec->questionid, 'glossaryentryid' => $rec->glossaryentryid);
if (($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions AS r')) != false) {
$reps[ $rec->answertext] = $rec2->r; $reps[ $rec->answertext] = $rec2->r;
} }
} }
$cryptex->setwords( $answers, $game->param1, $reps);
//game->param4 is minimum words
//game->param2 is maximum words
if( $cryptex->computedata( $crossm, $crossd, $letters, $game->param4, $game->param2)){
$new_crossd = array();
foreach( $crossd as $rec)
{
if( array_key_exists( $rec->answertext, $infos)){
$info = $infos[ $rec->answertext];
$rec->id = 0;
$rec->sourcemodule = $info[ 0];
$rec->questionid = $info[ 1];
$rec->glossaryentryid = $info[ 2];
}
game_update_queries( $game, $attempt, $rec, 0, '');
$new_crossd[] = $rec;
}
$cryptexrec = $cryptex->savecryptex( $game, $crossm, $new_crossd, $attempt->id, $letters);
}
game_updateattempts( $game, $attempt, 0, 0); $cryptex->setwords( $answers, $game->param1, $reps);
// The game->param4 is minimum words.
// The game->param2 is maximum words.
if ($cryptex->computedata( $crossm, $crossd, $letters, $game->param4, $game->param2)) {
$newcrossd = array();
foreach ($crossd as $rec) {
if (array_key_exists( $rec->answertext, $infos)) {
$info = $infos[ $rec->answertext];
$rec->id = 0;
$rec->sourcemodule = $info[ 0];
$rec->questionid = $info[ 1];
$rec->glossaryentryid = $info[ 2];
}
game_update_queries( $game, $attempt, $rec, 0, '');
$newcrossd[] = $rec;
}
$cryptexrec = $cryptex->savecryptex( $game, $crossm, $newcrossd, $attempt->id, $letters);
}
return game_cryptex_play( $id, $game, $attempt, $cryptexrec, $crossm, false, false, false, $context); game_updateattempts( $game, $attempt, 0, 0);
return game_cryptex_play( $id, $game, $attempt, $cryptexrec, $crossm, false, false, false, $context);
} }
//q means game_queries.id // The q means game_queries.id.
function game_cryptex_check( $id, $game, $attempt, $cryptexrec, $q, $answer, $finishattempt, $context) function game_cryptex_check( $id, $game, $attempt, $cryptexrec, $q, $answer, $finishattempt, $context) {
{
global $DB; global $DB;
if ($finishattempt) {
if( $finishattempt)
{
game_updateattempts( $game, $attempt, -1, true); game_updateattempts( $game, $attempt, -1, true);
game_cryptex_continue( $id, $game, false, false, true, $context); game_cryptex_continue( $id, $game, false, false, true, $context);
return; return;
} }
if( $attempt === false) if ($attempt === false) {
{ game_cryptex_continue( $id, $game, $attempt, $cryptexrec, false, $context);
game_cryptex_continue( $id, $game, $attempt, $cryptexrec, false, $context); return;
return; }
}
$crossm = $DB->get_record_select( 'game_cross', "id=$attempt->id");
$query = $DB->get_record_select( 'game_queries', "id=$q");
$answer1 = trim( game_upper( $query->answertext));
$answer2 = trim( game_upper( $answer));
$len1 = game_strlen( $answer1);
$len2 = game_strlen( $answer2);
$equal = ( $len1 == $len2);
if( $equal){
for( $i=0; $i < $len1; $i++)
{
if( game_substr( $answer1, $i, 1) != game_substr( $answer2, $i, 1))
{
$equal = true;
break;
}
}
}
if( $equal == false)
{
game_update_queries( $game, $attempt, $query, 0, $answer2, true);
game_cryptex_play( $id, $game, $attempt, $cryptexrec, $crossm, true, false, false, $context);
return;
}
game_update_queries( $game, $attempt, $query, 1, $answer2);
$onlyshow=false;
$showsolution=false;
game_cryptex_play( $id, $game, $attempt, $cryptexrec, $crossm, true, $onlyshow, $showsolution, $context);
}
function game_cryptex_play( $id, $game, $attempt, $cryptexrec, $crossm, $updateattempt=false, $onlyshow=false, $showsolution=false, $context, $print=false, $showhtmlprintbutton=true) $crossm = $DB->get_record_select( 'game_cross', "id=$attempt->id");
{ $query = $DB->get_record_select( 'game_queries', "id=$q");
global $DB;
$answer1 = trim( game_upper( $query->answertext));
$answer2 = trim( game_upper( $answer));
$len1 = game_strlen( $answer1);
$len2 = game_strlen( $answer2);
$equal = ( $len1 == $len2);
if ($equal) {
for ($i = 0; $i < $len1; $i++) {
if (game_substr( $answer1, $i, 1) != game_substr( $answer2, $i, 1)) {
$equal = true;
break;
}
}
}
if ($equal == false) {
game_update_queries( $game, $attempt, $query, 0, $answer2, true);
game_cryptex_play( $id, $game, $attempt, $cryptexrec, $crossm, true, false, false, $context);
return;
}
game_update_queries( $game, $attempt, $query, 1, $answer2);
global $CFG; $onlyshow = false;
$showsolution = false;
game_cryptex_play( $id, $game, $attempt, $cryptexrec, $crossm, true, $onlyshow, $showsolution, $context);
}
function game_cryptex_play( $id, $game, $attempt, $cryptexrec, $crossm,
$updateattempt = false, $onlyshow = false, $showsolution = false, $context,
$print = false, $showhtmlprintbutton = true) {
global $CFG, $DB;
if( $game->toptext != ''){ if ($game->toptext != '') {
echo $game->toptext.'<br>'; echo $game->toptext.'<br>';
} }
echo '<br>'; echo '<br>';
$cryptex = new CryptexDB(); $cryptex = new CryptexDB();
$language = $attempt->language; $language = $attempt->language;
$questions = $cryptex->loadcryptex( $crossm, $mask, $corrects, $attempt->language); $questions = $cryptex->loadcryptex( $crossm, $mask, $corrects, $attempt->language);
if( $language != $attempt->language){ if ($language != $attempt->language) {
if( !$DB->set_field( 'game_attempts', 'language', $attempt->language, array( 'id' => $attempt->id))){ if (!$DB->set_field( 'game_attempts', 'language', $attempt->language, array( 'id' => $attempt->id))) {
print_error( "game_cross_play: Can't set language"); print_error( "game_cross_play: Can't set language");
} }
} }
if( $attempt->language != '') if ( $attempt->language != '') {
$wordrtl = game_right_to_left( $attempt->language); $wordrtl = game_right_to_left( $attempt->language);
else } else {
$wordrtl = right_to_left(); $wordrtl = right_to_left();
}
$reverseprint = ($wordrtl != right_to_left()); $reverseprint = ($wordrtl != right_to_left());
if( $reverseprint) if ($reverseprint) {
$textdir = 'dir="'.($wordrtl ? 'rtl' : 'ltr').'"'; $textdir = 'dir="'.($wordrtl ? 'rtl' : 'ltr').'"';
else } else {
$textdir = ''; $textdir = '';
}
$len = game_strlen( $mask);
// The count1 means there is a guested letter.
// The count2 means there is a letter that not guessed.
$count1 = $count2 = 0;
for ($i = 0; $i < $len; $i++) {
$c = game_substr( $mask, $i, 1);
if ($c == '1') {
$count1++;
} else if ($c == '2') {
$count2++;
}
}
if ($count1 + $count2 == 0) {
$gradeattempt = 0;
} else {
$gradeattempt = $count1 / ($count1 + $count2);
}
$finished = ($count2 == 0);
if (($finished === false) && ($game->param8 > 0)) {
$found = false;
foreach ($questions as $q) {
if ($q->tries < $game->param8) {
$found = true;
}
}
if ($found == false) {
$finished = true; // Rich max tries.
}
}
$len = game_strlen( $mask); if ($updateattempt) {
game_updateattempts( $game, $attempt, $gradeattempt, $finished);
//count1 means there is a guested letter }
//count2 means there is a letter that not guessed
$count1 = $count2 = 0;
for($i=0; $i < $len; $i++)
{
$c = game_substr( $mask, $i, 1);
if( $c == '1'){
$count1++;
}else if( $c == '2')
{
$count2++;
}
}
if( $count1 + $count2 == 0){
$gradeattempt = 0;
}else
{
$gradeattempt = $count1 / ($count1 + $count2);
}
$finished = ($count2 == 0);
if( ($finished === false) && ($game->param8 > 0))
{
$found = false;
foreach ( $questions as $q)
{
if ( $q->tries < $game->param8)
$found = true;
}
if( $found == false)
$finished = true; //rich max tries
}
if( $updateattempt){
game_updateattempts( $game, $attempt, $gradeattempt, $finished);
}
if( ($onlyshow == false) and ($showsolution == false)){
if( $finished){
game_cryptex_onfinished( $id, $game, $attempt, $cryptexrec);
}
}
if (($onlyshow == false) and ($showsolution == false)) {
if ($finished) {
game_cryptex_onfinished( $id, $game, $attempt, $cryptexrec);
}
}
?> ?>
<style type="text/css"><!-- <style type="text/css"><!--
@ -228,14 +232,14 @@ width: 240pt;
--></style> --></style>
<?php <?php
$grade = round( 100 * $gradeattempt); $grade = round( 100 * $gradeattempt);
echo get_string( 'grade', 'game').' '.$grade.' %'; echo get_string( 'grade', 'game').' '.$grade.' %';
echo '<br>'; echo '<br>';
echo '<table border=0>'; echo '<table border=0>';
echo '<tr><td>'; echo '<tr><td>';
$cryptex->displaycryptex( $crossm->cols, $crossm->rows, $cryptexrec->letters, $mask, $showsolution, $textdir); $cryptex->displaycryptex( $crossm->cols, $crossm->rows, $cryptexrec->letters, $mask, $showsolution, $textdir);
?> ?>
</td> </td>
@ -264,14 +268,14 @@ width: 240pt;
<?php <?php
if( $showhtmlprintbutton){ if ($showhtmlprintbutton) {
echo '<br><button id="finishattemptbutton" type="button" onclick="OnEndGame();" >'.get_string( 'finish', 'game'); echo '<br><button id="finishattemptbutton" type="button" onclick="OnEndGame();" >'.get_string( 'finish', 'game');
echo '</button>'; echo '</button>';
echo '<button id="printbutton" type="button" onclick="OnPrint();" >'.get_string( 'print', 'game'); echo '<button id="printbutton" type="button" onclick="OnPrint();" >'.get_string( 'print', 'game');
echo '</button><br>'; echo '</button><br>';
} }
if( $showhtmlprintbutton){ if ($showhtmlprintbutton) {
?> ?>
<script> <script>
function PrintHtmlClick() function PrintHtmlClick()
@ -285,57 +289,61 @@ if( $showhtmlprintbutton){
function OnPrint() function OnPrint()
{ {
<?php <?php
global $CFG; global $CFG;
$params = "id=$id&gameid=$game->id"; $params = "id=$id&gameid=$game->id";
echo "window.open( \"{$CFG->wwwroot}/mod/game/print.php?$params\");"; echo "window.open( \"{$CFG->wwwroot}/mod/game/print.php?$params\");";
?> ?>
} }
function OnEndGame() function OnEndGame()
{ {
<?php <?php
global $CFG; global $CFG;
$params = 'id='.$id.'&action=cryptexcheck&g=&finishattempt=1'; $params = 'id='.$id.'&action=cryptexcheck&g=&finishattempt=1';
echo "window.location = \"{$CFG->wwwroot}/mod/game/attempt.php?$params\";\r\n"; echo "window.location = \"{$CFG->wwwroot}/mod/game/attempt.php?$params\";\r\n";
?> ?>
} }
</script> </script>
<?php <?php
} }
$i = 0; $i = 0;
$else = ''; $else = '';
$contextglossary = false; $contextglossary = false;
foreach( $questions as $key => $q){ foreach ($questions as $key => $q) {
$i++; $i++;
if( $showsolution == false){ if ($showsolution == false) {
//When I want to show the solution a want to show the questions to. // When I want to show the solution a want to show the questions to.
if( array_key_exists( $q->id, $corrects)){ if (array_key_exists( $q->id, $corrects)) {
continue; continue;
} }
} }
$question = game_show_query( $game, $q, "$i. ".$q->questiontext, $context); $question = game_show_query( $game, $q, "$i. ".$q->questiontext, $context);
$question2 = strip_tags($question); //ADDED BY DP (AUG 2009) - fixes " breaking the Answer button for this question $question2 = strip_tags($question); // ADDED BY DP (AUG 2009) - fixes " breaking the Answer button for this question.
if( ($onlyshow == false) and ($showsolution == false)){ if (($onlyshow == false) and ($showsolution == false)) {
if( ($game->param8 == 0) || ($game->param8 > $q->tries)) if (($game->param8 == 0) || ($game->param8 > $q->tries)) {
$question .= ' &nbsp;<input type="submit" value="'.get_string( 'answer').'" onclick="OnCheck( '.$q->id.',\''.$question2.'\');" />'; $question .= ' &nbsp;<input type="submit" value="'.
} get_string( 'answer').'" onclick="OnCheck( '.$q->id.',\''.$question2.'\');" />';
}
}
echo $question; echo $question;
if( $showsolution){ if ($showsolution) {
echo " &nbsp;&nbsp;&nbsp;$q->answertext<B></b>"; echo " &nbsp;&nbsp;&nbsp;$q->answertext<B></b>";
} }
echo '<br>'; echo '<br>';
} }
if( $game->bottomtext != ''){ if ($game->bottomtext != '') {
echo '<br><br>'.$game->bottomtext; echo '<br><br>'.$game->bottomtext;
} }
?> ?>
<script> <script>
function OnCheck( id, question) function OnCheck( id, question)
{ {
@ -354,26 +362,26 @@ if( $showhtmlprintbutton){
} }
} }
</script> </script>
<?php <?php
if( $print) if ($print) {
echo '<body onload="window.print()">'; echo '<body onload="window.print()">';
else } else {
echo '<body>'; echo '<body>';
}
} }
function game_cryptex_onfinished( $id, $game, $attempt, $cryptexrec) {
global $CFG, $DB;
function game_cryptex_onfinished( $id, $game, $attempt, $cryptexrec) if (! $cm = $DB->get_record( 'course_modules', array( 'id' => $id))) {
{ print_error( "Course Module ID was incorrect id=$id");
global $CFG, $DB; }
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 '<B>'.get_string( 'win', 'game').'</B><br>';
echo '<br>'; echo '<br>';
echo "<a href=\"{$CFG->wwwroot}/mod/game/attempt.php?id=$id&forcenew=1\">".get_string( 'nextgame', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp; '; echo "<a href=\"{$CFG->wwwroot}/mod/game/attempt.php?id=$id&forcenew=1\">".
echo "<a href=\"{$CFG->wwwroot}/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'</a> '; get_string( 'nextgame', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp; ';
echo "<br><br>\r\n"; echo "<a href=\"{$CFG->wwwroot}/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'</a> ';
echo "<br><br>\r\n";
} }

24
db/access.php

@ -1,4 +1,19 @@
<?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/>.
/** /**
* Capability definitions for the game module. * Capability definitions for the game module.
* *
@ -6,8 +21,7 @@
*/ */
$capabilities = array( $capabilities = array(
// Ability to see that the game exists, and the basic information // Ability to see that the game exists, and the basic information about it, for example the start date and time limit.
// about it, for example the start date and time limit.
'mod/game:view' => array( 'mod/game:view' => array(
'captype' => 'read', 'captype' => 'read',
'contextlevel' => CONTEXT_MODULE, 'contextlevel' => CONTEXT_MODULE,
@ -31,8 +45,7 @@ $capabilities = array(
) )
), ),
// Ability for a 'Student' to review their previous attempts. Review by // Ability for a 'Student' to review their previous attempts. Review by 'Teachers' is controlled by mod/game:viewreports.
// 'Teachers' is controlled by mod/game:viewreports.
'mod/game:reviewmyattempts' => array( 'mod/game:reviewmyattempts' => array(
'captype' => 'read', 'captype' => 'read',
'contextlevel' => CONTEXT_MODULE, 'contextlevel' => CONTEXT_MODULE,
@ -53,7 +66,7 @@ $capabilities = array(
) )
), ),
// Edit the game overrides // Edit the game overrides.
'mod/game:manageoverrides' => array( 'mod/game:manageoverrides' => array(
'captype' => 'write', 'captype' => 'write',
'contextlevel' => CONTEXT_MODULE, 'contextlevel' => CONTEXT_MODULE,
@ -98,4 +111,3 @@ $capabilities = array(
'clonepermissionsfrom' => 'moodle/course:manageactivities' 'clonepermissionsfrom' => 'moodle/course:manageactivities'
) )
); );

15
db/createsnakes.php

@ -1,8 +1,21 @@
<?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/>.
require( "../../../config.php"); require( "../../../config.php");
execute_sql("truncate TABLE {game_snakes_database}"); execute_sql("truncate TABLE {game_snakes_database}");
require( "importsnakes.php"); require( "importsnakes.php");

34
db/importsnakes.php

@ -1,12 +1,25 @@
<?php // $Id: importsnakes.php,v 1.5 2012/07/25 11:16:05 bdaloukas Exp $ <?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
game_importsnakes(); game_importsnakes();
function game_importsnakes() function game_importsnakes() {
{
global $DB; global $DB;
if( $DB->count_records( 'game_snakes_database') != 0){ if ($DB->count_records( 'game_snakes_database') != 0) {
return; return;
} }
@ -39,15 +52,12 @@ function game_importsnakes()
$newrec->height = 436; $newrec->height = 436;
$newrec->data = 'L2-25,S4-23,L8-18,S16-20,L19-29,S27-33'; $newrec->data = 'L2-25,S4-23,L8-18,S16-20,L19-29,S27-33';
game_importsnakes_do( $newrec); game_importsnakes_do( $newrec);
} }
function game_importsnakes_do( $newrec) function game_importsnakes_do( $newrec) {
{
global $DB; global $DB;
if( !$DB->insert_record( 'game_snakes_database', $newrec)){ if (!$DB->insert_record( 'game_snakes_database', $newrec)) {
print_object( $newrec); print_error( "Can't insert to table game_snakes_database");
print_error( "Can't insert to table game_snakes_database"); }
}
} }

5392
db/importsudoku.php

File diff suppressed because it is too large

263
db/upgrade.php

@ -1,23 +1,37 @@
<?php //$Id: upgrade.php,v 1.30 2011/08/27 05:37:44 bdaloukas Exp $ <?php
// This file is part of Moodle - http://moodle.org/
// This file keeps track of upgrades to the game module
//
// Sometimes, changes between versions involve
// alterations to database structures and other
// major things that may break installations.
// //
// The upgrade function in this file will attempt // Moodle is free software: you can redistribute it and/or modify
// to perform all the necessary actions to upgrade // it under the terms of the GNU General Public License as published by
// your older installation to the current version. // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// //
// If there's something it cannot do itself, it // Moodle is distributed in the hope that it will be useful,
// will tell you what you need to do. // 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.
// //
// The commands in here will all be database-neutral, // You should have received a copy of the GNU General Public License
// using the methods of database_manager class // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
//
// Please do not forget to use upgrade_set_timeout() /* This file keeps track of upgrades to the game module
// before any action that may take longer time to finish. * Sometimes, changes between versions involve
* alterations to database structures and other
* major things that may break installations.
*
* The upgrade function in this file will attempt
* to perform all the necessary actions to upgrade
* your older installation to the current version.
*
* If there's something it cannot do itself, it
* will tell you what you need to do.
*
* The commands in here will all be database-neutral,
* using the methods of database_manager class
*
* Please do not forget to use upgrade_set_timeout()
* before any action that may take longer time to finish.
*/
function xmldb_game_upgrade($oldversion) { function xmldb_game_upgrade($oldversion) {
@ -27,7 +41,8 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007082802) { if ($oldversion < 2007082802) {
$table = new xmldb_table('game'); $table = new xmldb_table('game');
$field = new xmldb_field('questioncategoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'glossarycategoryid'); $field = new xmldb_field('questioncategoryid', XMLDB_TYPE_INTEGER, '10',
XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'glossarycategoryid');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -43,19 +58,22 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007082803) { if ($oldversion < 2007082803) {
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
$field = new xmldb_field('glossaryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'quizid'); $field = new xmldb_field('glossaryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
null, null, '0', 'quizid');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
$field = new xmldb_field('glossarycategoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'glossaryid'); $field = new xmldb_field('glossarycategoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
null, null, '0', 'glossaryid');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
$field = new xmldb_field('questioncategoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'glossarycategoryid'); $field = new xmldb_field('questioncategoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
null, null, '0', 'glossarycategoryid');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -65,7 +83,8 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007082804) { if ($oldversion < 2007082804) {
$table = new xmldb_table('game_millionaire'); $table = new xmldb_table('game_millionaire');
$field = new xmldb_field('questioncategoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'quizid'); $field = new xmldb_field('questioncategoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
null, null, '0', 'quizid');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -75,7 +94,8 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007082805) { if ($oldversion < 2007082805) {
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
$field = new xmldb_field('try', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'answer'); $field = new xmldb_field('try', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
null, null, '0', 'answer');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -87,7 +107,7 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2007082805, 'game'); upgrade_mod_savepoint(true, 2007082805, 'game');
} }
if ($oldversion < 2007082807) { if ($oldversion < 2007082807) {
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
@ -98,24 +118,25 @@ function xmldb_game_upgrade($oldversion) {
} }
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
$field = new xmldb_field('corrects', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'finishedword'); $field = new xmldb_field('corrects', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
null, null, '0', 'finishedword');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
upgrade_mod_savepoint(true, 2007082807, 'game'); upgrade_mod_savepoint(true, 2007082807, 'game');
} }
if ($oldversion < 2007082808) { if ($oldversion < 2007082808) {
$table = new xmldb_table('game'); $table = new xmldb_table('game');
$field = new xmldb_field('param7', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'param6'); $field = new xmldb_field('param7', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
null, null, '0', 'param6');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
upgrade_mod_savepoint(true, 2007082808, 'game'); upgrade_mod_savepoint(true, 2007082808, 'game');
} }
if ($oldversion < 2007082809) { if ($oldversion < 2007082809) {
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
@ -129,7 +150,8 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007082901) { if ($oldversion < 2007082901) {
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
$field = new xmldb_field('glossaryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'quizid'); $field = new xmldb_field('glossaryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
null, null, '0', 'quizid');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -155,13 +177,14 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2007091001, 'game'); upgrade_mod_savepoint(true, 2007091001, 'game');
} }
if ($oldversion < 2007091701) { if ($oldversion < 2007091701) {
$table = new xmldb_table( 'game_bookquiz_chapters'); $table = new xmldb_table( 'game_bookquiz_chapters');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE); $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE);
$table->add_field('gameinstanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); $table->add_field('gameinstanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
XMLDB_NOTNULL, null, '0');
$table->add_field('chapterid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); $table->add_field('chapterid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_key('PRIMARY', XMLDB_KEY_PRIMARY, array('id')); $table->add_key('PRIMARY', XMLDB_KEY_PRIMARY, array('id'));
@ -171,7 +194,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->create_table($table); $dbman->create_table($table);
} }
upgrade_mod_savepoint(true, 2007091701, 'game'); upgrade_mod_savepoint(true, 2007091701, 'game');
} }
if ($oldversion < 2007092207) { if ($oldversion < 2007092207) {
$table = new xmldb_table( 'game_snakes_database'); $table = new xmldb_table( 'game_snakes_database');
@ -194,7 +217,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->create_table($table); $dbman->create_table($table);
} }
upgrade_mod_savepoint(true, 2007092207, 'game'); upgrade_mod_savepoint(true, 2007092207, 'game');
} }
if ($oldversion < 2007092208) { if ($oldversion < 2007092208) {
$table = new xmldb_table( 'game_snakes'); $table = new xmldb_table( 'game_snakes');
@ -209,7 +232,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->create_table($table); $dbman->create_table($table);
} }
upgrade_mod_savepoint(true, 2007092208, 'game'); upgrade_mod_savepoint(true, 2007092208, 'game');
} }
if ($oldversion < 2007092301) { if ($oldversion < 2007092301) {
$table = new xmldb_table('game_snakes_database'); $table = new xmldb_table('game_snakes_database');
@ -223,7 +246,8 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007092302) { if ($oldversion < 2007092302) {
$table = new xmldb_table('game_snakes_database'); $table = new xmldb_table('game_snakes_database');
$field = new xmldb_field('height', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); $field = new xmldb_field('height', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
null, null, '0');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -305,13 +329,13 @@ function xmldb_game_upgrade($oldversion) {
$table = new xmldb_table('game_bookquiz_questions'); $table = new xmldb_table('game_bookquiz_questions');
$field = new xmldb_field('bookid'); $field = new xmldb_field('bookid');
if ($dbman->field_exists($table, $field)) { if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field); $dbman->drop_field($table, $field);
} }
upgrade_mod_savepoint(true, 2007110801, 'game'); upgrade_mod_savepoint(true, 2007110801, 'game');
} }
if ($oldversion < 2007110802) { if ($oldversion < 2007110802) {
$table = new xmldb_table( 'game_grades'); $table = new xmldb_table( 'game_grades');
@ -329,13 +353,13 @@ function xmldb_game_upgrade($oldversion) {
$dbman->create_table($table); $dbman->create_table($table);
} }
upgrade_mod_savepoint(true, 2007110802, 'game'); upgrade_mod_savepoint(true, 2007110802, 'game');
} }
if ($oldversion < 2007110811) { if ($oldversion < 2007110811) {
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
$field = new xmldb_field('sourcemodule'); $field = new xmldb_field('sourcemodule');
if ($dbman->field_exists($table, $field)) { if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field); $dbman->drop_field($table, $field);
} }
upgrade_mod_savepoint(true, 2007110811, 'game'); upgrade_mod_savepoint(true, 2007110811, 'game');
@ -345,7 +369,7 @@ function xmldb_game_upgrade($oldversion) {
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
$field = new xmldb_field('questionsid'); $field = new xmldb_field('questionsid');
if ($dbman->field_exists($table, $field)) { if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field); $dbman->drop_field($table, $field);
} }
upgrade_mod_savepoint(true, 2007110812, 'game'); upgrade_mod_savepoint(true, 2007110812, 'game');
@ -355,7 +379,7 @@ function xmldb_game_upgrade($oldversion) {
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
$field = new xmldb_field('quizid'); $field = new xmldb_field('quizid');
if ($dbman->field_exists($table, $field)) { if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field); $dbman->drop_field($table, $field);
} }
upgrade_mod_savepoint(true, 2007110813, 'game'); upgrade_mod_savepoint(true, 2007110813, 'game');
@ -365,7 +389,7 @@ function xmldb_game_upgrade($oldversion) {
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
$field = new xmldb_field('glossaryid'); $field = new xmldb_field('glossaryid');
if ($dbman->field_exists($table, $field)) { if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field); $dbman->drop_field($table, $field);
} }
upgrade_mod_savepoint(true, 2007110814, 'game'); upgrade_mod_savepoint(true, 2007110814, 'game');
@ -532,7 +556,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007110834) { if ($oldversion < 2007110834) {
$table = new xmldb_table( 'game_bookquiz'); $table = new xmldb_table( 'game_bookquiz');
$field = new xmldb_field( 'attemptid', XMLDB_TYPE_FLOAT, null, null, null, null, '0'); $field = new xmldb_field( 'attemptid', XMLDB_TYPE_FLOAT, null, null, null, null, '0');
$dbman->rename_field($table, $field, 'score'); $dbman->rename_field($table, $field, 'score');
@ -551,7 +575,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007110836) { if ($oldversion < 2007110836) {
$table = new xmldb_table( 'game_cross'); $table = new xmldb_table( 'game_cross');
$field = new xmldb_field( 'timelimit', XMLDB_TYPE_FLOAT, null, null, null, null, '0'); $field = new xmldb_field( 'timelimit', XMLDB_TYPE_FLOAT, null, null, null, null, '0');
$dbman->rename_field($table, $field, 'createtimelimit'); $dbman->rename_field($table, $field, 'createtimelimit');
upgrade_mod_savepoint(true, 2007110836, 'game'); upgrade_mod_savepoint(true, 2007110836, 'game');
@ -580,7 +604,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007110839) { if ($oldversion < 2007110839) {
$table = new xmldb_table('game_cross'); $table = new xmldb_table('game_cross');
$field = new xmldb_field('createspaces', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); $field = new xmldb_field('createspaces', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -594,7 +618,6 @@ function xmldb_game_upgrade($oldversion) {
$dbman->drop_table($table); $dbman->drop_table($table);
} }
upgrade_mod_savepoint(true, 2007110840, 'game'); upgrade_mod_savepoint(true, 2007110840, 'game');
} }
if ($oldversion < 2007110841) { if ($oldversion < 2007110841) {
@ -663,7 +686,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007110859) { if ($oldversion < 2007110859) {
$table = new xmldb_table( 'game_attempts'); $table = new xmldb_table( 'game_attempts');
$field = new xmldb_field( 'timestarted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); $field = new xmldb_field( 'timestarted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
$dbman->rename_field($table, $field, 'timestart'); $dbman->rename_field($table, $field, 'timestart');
upgrade_mod_savepoint(true, 2007110859, 'game'); upgrade_mod_savepoint(true, 2007110859, 'game');
@ -671,7 +694,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007110860) { if ($oldversion < 2007110860) {
$table = new xmldb_table( 'game_attempts'); $table = new xmldb_table( 'game_attempts');
$field = new xmldb_field( 'timefinished', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); $field = new xmldb_field( 'timefinished', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
$dbman->rename_field( $table, $field, 'timefinish'); $dbman->rename_field( $table, $field, 'timefinish');
upgrade_mod_savepoint(true, 2007110860, 'game'); upgrade_mod_savepoint(true, 2007110860, 'game');
@ -689,7 +712,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007110862) { if ($oldversion < 2007110862) {
$table = new xmldb_table( 'game_attempts'); $table = new xmldb_table( 'game_attempts');
$field = new xmldb_field( 'tries', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); $field = new xmldb_field( 'tries', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
$dbman->rename_field( $table, $field, 'attempts'); $dbman->rename_field( $table, $field, 'attempts');
upgrade_mod_savepoint(true, 2007110862, 'game'); upgrade_mod_savepoint(true, 2007110862, 'game');
@ -697,8 +720,8 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007110863) { if ($oldversion < 2007110863) {
$table = new xmldb_table( 'game_attempts'); $table = new xmldb_table( 'game_attempts');
$field = new xmldb_field( 'preview', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0', 'lastremotehost'); $field = new xmldb_field( 'preview', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0', 'lastremotehost');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -707,7 +730,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007110864) { if ($oldversion < 2007110864) {
$table = new xmldb_table( 'game_attempts'); $table = new xmldb_table( 'game_attempts');
$field = new xmldb_field( 'attempt', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'preview'); $field = new xmldb_field( 'attempt', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'preview');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -717,7 +740,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007110865) { if ($oldversion < 2007110865) {
$table = new xmldb_table( 'game_attempts'); $table = new xmldb_table( 'game_attempts');
$field = new xmldb_field( 'score', XMLDB_TYPE_FLOAT, null, XMLDB_UNSIGNED, null, null, '0', 'attempt'); $field = new xmldb_field( 'score', XMLDB_TYPE_FLOAT, null, XMLDB_UNSIGNED, null, null, '0', 'attempt');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -725,7 +748,6 @@ function xmldb_game_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2007110865, 'game'); upgrade_mod_savepoint(true, 2007110865, 'game');
} }
if ($oldversion < 2007110866) { if ($oldversion < 2007110866) {
$table = new xmldb_table( 'game_course_input'); $table = new xmldb_table( 'game_course_input');
@ -741,7 +763,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->create_table($table); $dbman->create_table($table);
} }
upgrade_mod_savepoint(true, 2007110866, 'game'); upgrade_mod_savepoint(true, 2007110866, 'game');
} }
if ($oldversion < 2007111302) { if ($oldversion < 2007111302) {
$table = new xmldb_table('game'); $table = new xmldb_table('game');
@ -824,14 +846,14 @@ function xmldb_game_upgrade($oldversion) {
} }
if ($oldversion < 2007111310) { if ($oldversion < 2007111310) {
$DB->execute('UPDATE {game} SET grade=0 WHERE grade IS NULL', true); $DB->execute('UPDATE {game} SET grade=0 WHERE grade IS NULL', true);
upgrade_mod_savepoint(true, 2007111310, 'game'); upgrade_mod_savepoint(true, 2007111310, 'game');
} }
if ($oldversion < 2007111842) { if ($oldversion < 2007111842) {
$table = new xmldb_table( 'game_queries'); $table = new xmldb_table( 'game_queries');
$field = new xmldb_field( 'gameinstanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); $field = new xmldb_field( 'gameinstanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
$dbman->rename_field( $table, $field, 'attemptid'); $dbman->rename_field( $table, $field, 'attemptid');
@ -860,7 +882,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007111844) { if ($oldversion < 2007111844) {
$table = new xmldb_table('game_queries'); $table = new xmldb_table('game_queries');
$field = new xmldb_field('questiontext', XMLDB_TYPE_TEXT, null, null, null, null, '','glossaryentryid'); $field = new xmldb_field('questiontext', XMLDB_TYPE_TEXT, null, null, null, null, '', 'glossaryentryid');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -870,7 +892,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007111845) { if ($oldversion < 2007111845) {
$table = new xmldb_table('game_queries'); $table = new xmldb_table('game_queries');
$field = new xmldb_field('score', XMLDB_TYPE_FLOAT, null, null, null, null, '0','questiontext'); $field = new xmldb_field('score', XMLDB_TYPE_FLOAT, null, null, null, null, '0', 'questiontext');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -880,7 +902,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007111846) { if ($oldversion < 2007111846) {
$table = new xmldb_table('game_queries'); $table = new xmldb_table('game_queries');
$field = new xmldb_field('studentanswer', XMLDB_TYPE_TEXT, null, null, null, null, '','glossaryentryid'); $field = new xmldb_field('studentanswer', XMLDB_TYPE_TEXT, null, null, null, null, '', 'glossaryentryid');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -890,7 +912,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007111847) { if ($oldversion < 2007111847) {
$table = new xmldb_table( 'game_queries'); $table = new xmldb_table( 'game_queries');
$field = new xmldb_field( 'col', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, '0'); $field = new xmldb_field( 'col', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, '0');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -900,7 +922,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007111848) { if ($oldversion < 2007111848) {
$table = new xmldb_table( 'game_queries'); $table = new xmldb_table( 'game_queries');
$field = new xmldb_field( 'row', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); $field = new xmldb_field( 'row', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -910,7 +932,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007111849) { if ($oldversion < 2007111849) {
$table = new xmldb_table( 'game_queries'); $table = new xmldb_table( 'game_queries');
$field = new xmldb_field( 'horizontal', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0'); $field = new xmldb_field( 'horizontal', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -930,7 +952,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007111851) { if ($oldversion < 2007111851) {
$table = new xmldb_table( 'game_queries'); $table = new xmldb_table( 'game_queries');
$field = new xmldb_field( 'correct', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); $field = new xmldb_field( 'correct', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -939,10 +961,10 @@ function xmldb_game_upgrade($oldversion) {
} }
if ($oldversion < 2007111853) { if ($oldversion < 2007111853) {
execute_sql('UPDATE {game} SET grademethod=1 WHERE grademethod=0 OR grademethod IS NULL', true); execute_sql('UPDATE {game} SET grademethod=1 WHERE grademethod=0 OR grademethod IS NULL', true);
upgrade_mod_savepoint(true, 2007111853, 'game'); upgrade_mod_savepoint(true, 2007111853, 'game');
} }
if ($oldversion < 2007111854) { if ($oldversion < 2007111854) {
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
@ -957,7 +979,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007111855) { if ($oldversion < 2007111855) {
$table = new xmldb_table('game_snakes'); $table = new xmldb_table('game_snakes');
$field = new xmldb_field('queryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'snakesdatabaseid'); $field = new xmldb_field('queryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'snakesdatabaseid');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -966,8 +988,8 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2007111856) { if ($oldversion < 2007111856) {
$table = new xmldb_table( 'game_bookquiz_chapters'); $table = new xmldb_table( 'game_bookquiz_chapters');
$field = new xmldb_field( 'attemptid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'id'); $field = new xmldb_field( 'attemptid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'id');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -1000,12 +1022,12 @@ function xmldb_game_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2007120106, 'game'); upgrade_mod_savepoint(true, 2007120106, 'game');
} }
//2008 // Here starts year 2008.
if ($oldversion < 2008011301) { if ($oldversion < 2008011301) {
$table = new xmldb_table('game'); $table = new xmldb_table('game');
$field = new xmldb_field('glossaryid2', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); $field = new xmldb_field('glossaryid2', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -1015,7 +1037,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2008011302) { if ($oldversion < 2008011302) {
$table = new xmldb_table('game'); $table = new xmldb_table('game');
$field = new xmldb_field('glossarycategoryid2', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); $field = new xmldb_field('glossarycategoryid2', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -1025,7 +1047,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2008011308) { if ($oldversion < 2008011308) {
$table = new xmldb_table('game_queries'); $table = new xmldb_table('game_queries');
$field = new xmldb_field('attachment', XMLDB_TYPE_CHAR, '200', null, null, null, ''); $field = new xmldb_field('attachment', XMLDB_TYPE_CHAR, '200', null, null, null, '');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
@ -1046,7 +1068,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->create_table($table); $dbman->create_table($table);
} }
upgrade_mod_savepoint(true, 2008011504, 'game'); upgrade_mod_savepoint(true, 2008011504, 'game');
} }
if ($oldversion < 2008012701) { if ($oldversion < 2008012701) {
$table = new xmldb_table('game'); $table = new xmldb_table('game');
@ -1056,7 +1078,7 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2008012701, 'game'); upgrade_mod_savepoint(true, 2008012701, 'game');
} }
if ($oldversion < 2008071101) { if ($oldversion < 2008071101) {
$table = new xmldb_table('game'); $table = new xmldb_table('game');
@ -1088,13 +1110,13 @@ function xmldb_game_upgrade($oldversion) {
$dbman->create_table($table); $dbman->create_table($table);
} }
upgrade_mod_savepoint(true, 2008072204, 'game'); upgrade_mod_savepoint(true, 2008072204, 'game');
} }
if ($oldversion < 2008072501) { if ($oldversion < 2008072501) {
$table = new xmldb_table('game_hangman'); $table = new xmldb_table('game_hangman');
$field = new xmldb_field('quizid'); $field = new xmldb_field('quizid');
if ($dbman->field_exists($table, $field)) { if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field); $dbman->drop_field($table, $field);
} }
upgrade_mod_savepoint(true, 2008072501, 'game'); upgrade_mod_savepoint(true, 2008072501, 'game');
@ -1138,7 +1160,7 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2008090101, 'game'); upgrade_mod_savepoint(true, 2008090101, 'game');
} }
if ($oldversion < 2008101103) { if ($oldversion < 2008101103) {
$table = new xmldb_table('game_millionaire'); $table = new xmldb_table('game_millionaire');
@ -1146,7 +1168,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->change_field_precision($table, $field); $dbman->change_field_precision($table, $field);
upgrade_mod_savepoint(true, 2008101103, 'game'); upgrade_mod_savepoint(true, 2008101103, 'game');
} }
if ($oldversion < 2008101104) { if ($oldversion < 2008101104) {
$table = new xmldb_table('game_millionaire'); $table = new xmldb_table('game_millionaire');
@ -1154,7 +1176,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->change_field_precision($table, $field); $dbman->change_field_precision($table, $field);
upgrade_mod_savepoint(true, 2008101104, 'game'); upgrade_mod_savepoint(true, 2008101104, 'game');
} }
if ($oldversion < 2008101106) { if ($oldversion < 2008101106) {
$table = new xmldb_table('game_sudoku'); $table = new xmldb_table('game_sudoku');
@ -1162,7 +1184,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->cchange_field_precision($table, $field); $dbman->cchange_field_precision($table, $field);
upgrade_mod_savepoint(true, 2008101106, 'game'); upgrade_mod_savepoint(true, 2008101106, 'game');
} }
if ($oldversion < 2008101107) { if ($oldversion < 2008101107) {
$table = new xmldb_table('game_hiddenpicture'); $table = new xmldb_table('game_hiddenpicture');
@ -1170,7 +1192,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->change_field_precision($table, $field); $dbman->change_field_precision($table, $field);
upgrade_mod_savepoint(true, 2008101107, 'game'); upgrade_mod_savepoint(true, 2008101107, 'game');
} }
if ($oldversion < 2008101108) { if ($oldversion < 2008101108) {
$table = new xmldb_table('game_hiddenpicture'); $table = new xmldb_table('game_hiddenpicture');
@ -1178,7 +1200,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->change_field_precision($table, $field); $dbman->change_field_precision($table, $field);
upgrade_mod_savepoint(true, 2008101108, 'game'); upgrade_mod_savepoint(true, 2008101108, 'game');
} }
if ($oldversion < 2008101109) { if ($oldversion < 2008101109) {
$table = new xmldb_table('game_hiddenpicture'); $table = new xmldb_table('game_hiddenpicture');
@ -1186,7 +1208,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->change_field_precision($table, $field); $dbman->change_field_precision($table, $field);
upgrade_mod_savepoint(true, 2008101109, 'game'); upgrade_mod_savepoint(true, 2008101109, 'game');
} }
if ($oldversion < 2008102701) { if ($oldversion < 2008102701) {
$table = new xmldb_table('game_queries'); $table = new xmldb_table('game_queries');
@ -1196,7 +1218,7 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2008102701, 'game'); upgrade_mod_savepoint(true, 2008102701, 'game');
} }
if ($oldversion < 2008110701) { if ($oldversion < 2008110701) {
$table = new xmldb_table( 'game_export_html'); $table = new xmldb_table( 'game_export_html');
@ -1215,11 +1237,11 @@ function xmldb_game_upgrade($oldversion) {
$dbman->create_table($table); $dbman->create_table($table);
} }
upgrade_mod_savepoint(true, 2008110701, 'game'); upgrade_mod_savepoint(true, 2008110701, 'game');
} }
if ($oldversion < 2008111701) { if ($oldversion < 2008111701) {
$table = new xmldb_table( 'game_snakes_database'); $table = new xmldb_table( 'game_snakes_database');
$field = new xmldb_field( 'file', XMLDB_TYPE_CHAR, 100, null, null, null, ''); $field = new xmldb_field( 'file', XMLDB_TYPE_CHAR, 100, null, null, null, '');
$dbman->rename_field( $table, $field, 'fileboard'); $dbman->rename_field( $table, $field, 'fileboard');
upgrade_mod_savepoint(true, 2008111701, 'game'); upgrade_mod_savepoint(true, 2008111701, 'game');
@ -1235,7 +1257,7 @@ function xmldb_game_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2008111801, 'game'); upgrade_mod_savepoint(true, 2008111801, 'game');
} }
//2009 // Year 2009 starts here.
if ($oldversion < 2009010502) { if ($oldversion < 2009010502) {
$table = new xmldb_table('game_export_javame'); $table = new xmldb_table('game_export_javame');
@ -1245,7 +1267,7 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2009010502, 'game'); upgrade_mod_savepoint(true, 2009010502, 'game');
} }
if ($oldversion < 2009031801) { if ($oldversion < 2009031801) {
$table = new xmldb_table('game_repetitions'); $table = new xmldb_table('game_repetitions');
@ -1264,7 +1286,7 @@ function xmldb_game_upgrade($oldversion) {
$dbman->create_table($table); $dbman->create_table($table);
} }
upgrade_mod_savepoint(true, 2009031801, 'game'); upgrade_mod_savepoint(true, 2009031801, 'game');
} }
if ($oldversion < 2009071403) { if ($oldversion < 2009071403) {
$table = new xmldb_table('game'); $table = new xmldb_table('game');
@ -1274,7 +1296,7 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2009071403, 'game'); upgrade_mod_savepoint(true, 2009071403, 'game');
} }
if ($oldversion < 2009072801) { if ($oldversion < 2009072801) {
$table = new xmldb_table('game_export_html'); $table = new xmldb_table('game_export_html');
@ -1284,7 +1306,7 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2009072801, 'game'); upgrade_mod_savepoint(true, 2009072801, 'game');
} }
if ($oldversion < 2009072901) { if ($oldversion < 2009072901) {
$table = new xmldb_table('game_export_html'); $table = new xmldb_table('game_export_html');
@ -1294,7 +1316,7 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2009072901, 'game'); upgrade_mod_savepoint(true, 2009072901, 'game');
} }
if ($oldversion < 2009073101) { if ($oldversion < 2009073101) {
$table = new xmldb_table('game_export_html'); $table = new xmldb_table('game_export_html');
@ -1304,7 +1326,7 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2009073101, 'game'); upgrade_mod_savepoint(true, 2009073101, 'game');
} }
if ($oldversion < 2009073102) { if ($oldversion < 2009073102) {
$table = new xmldb_table('game_export_javame'); $table = new xmldb_table('game_export_javame');
@ -1314,7 +1336,7 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2009073102, 'game'); upgrade_mod_savepoint(true, 2009073102, 'game');
} }
if ($oldversion < 2009083102) { if ($oldversion < 2009083102) {
$table = new xmldb_table('game'); $table = new xmldb_table('game');
@ -1347,7 +1369,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2010071607) { if ($oldversion < 2010071607) {
$table = new xmldb_table( 'game_export_html'); $table = new xmldb_table( 'game_export_html');
$field = new xmldb_field( 'gameid', XMLDB_TYPE_INTEGER, 10, null, null, null, null, null, '0'); $field = new xmldb_field( 'gameid', XMLDB_TYPE_INTEGER, 10, null, null, null, null, null, '0');
$dbman->rename_field($table, $field, 'id'); $dbman->rename_field($table, $field, 'id');
@ -1367,7 +1389,7 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2010071610) { if ($oldversion < 2010071610) {
$table = new xmldb_table( 'game_export_javame'); $table = new xmldb_table( 'game_export_javame');
$field = new xmldb_field( 'gameid', XMLDB_TYPE_INTEGER, 10, null, null, null, null, null, '0'); $field = new xmldb_field( 'gameid', XMLDB_TYPE_INTEGER, 10, null, null, null, null, null, '0');
$dbman->rename_field($table, $field, 'id'); $dbman->rename_field($table, $field, 'id');
@ -1376,31 +1398,31 @@ function xmldb_game_upgrade($oldversion) {
if ($oldversion < 2010072605) { if ($oldversion < 2010072605) {
// Define field language to be added to game_attempts // Define field language to be added to game_attempts.
$table = new xmldb_table('game_attempts'); $table = new xmldb_table('game_attempts');
$field = new xmldb_field('language', XMLDB_TYPE_CHAR, '10', null, null, null, null, 'attempts'); $field = new xmldb_field('language', XMLDB_TYPE_CHAR, '10', null, null, null, null, 'attempts');
// Conditionally launch add field language // Conditionally launch add field language.
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
// game savepoint reached // Game savepoint reached.
upgrade_mod_savepoint(true, 2010072605, 'game'); upgrade_mod_savepoint(true, 2010072605, 'game');
} }
if ($oldversion < 2010090301) { if ($oldversion < 2010090301) {
// Define field param9 to be added to game // Define field param9 to be added to game.
$table = new xmldb_table('game'); $table = new xmldb_table('game');
$field = new xmldb_field('param9', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'param8'); $field = new xmldb_field('param9', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'param8');
// Conditionally launch add field param9 // Conditionally launch add field param9.
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
// game savepoint reached // Game savepoint reached.
upgrade_mod_savepoint(true, 2010090301, 'game'); upgrade_mod_savepoint(true, 2010090301, 'game');
} }
@ -1421,7 +1443,7 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2011072704, 'game'); upgrade_mod_savepoint(true, 2011072704, 'game');
} }
if ($oldversion < 2011072705) { if ($oldversion < 2011072705) {
$table = new xmldb_table('game_export_html'); $table = new xmldb_table('game_export_html');
@ -1431,70 +1453,69 @@ function xmldb_game_upgrade($oldversion) {
} }
upgrade_mod_savepoint(true, 2011072705, 'game'); upgrade_mod_savepoint(true, 2011072705, 'game');
} }
if ($oldversion < 2011072902) { if ($oldversion < 2011072902) {
// Define field param10 to be added to game // Define field param10 to be added to game.
$table = new xmldb_table('game'); $table = new xmldb_table('game');
$field = new xmldb_field('param10', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'param9'); $field = new xmldb_field('param10', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'param9');
// Conditionally launch add field param10 // Conditionally launch add field param10.
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
// game savepoint reached // Game savepoint reached.
upgrade_mod_savepoint(true, 2011072902, 'game'); upgrade_mod_savepoint(true, 2011072902, 'game');
} }
if ($oldversion < 2011082603) { if ($oldversion < 2011082603) {
// Define field timeopen to be added to game // Define field timeopen to be added to game.
$table = new xmldb_table('game'); $table = new xmldb_table('game');
$field = new xmldb_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'sourcemodule'); $field = new xmldb_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'sourcemodule');
// Conditionally launch add field timeopen // Conditionally launch add field timeopen.
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
// game savepoint reached // Game savepoint reached.
upgrade_mod_savepoint(true, 2011082603, 'game'); upgrade_mod_savepoint(true, 2011082603, 'game');
} }
if ($oldversion < 2011082604) { if ($oldversion < 2011082604) {
// Define field timeclose to be added to game // Define field timeclose to be added to game.
$table = new xmldb_table('game'); $table = new xmldb_table('game');
$field = new xmldb_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'timeopen'); $field = new xmldb_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'timeopen');
// Conditionally launch add field timeclose // Conditionally launch add field timeclose.
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
// game savepoint reached // Game savepoint reached.
upgrade_mod_savepoint(true, 2011082604, 'game'); upgrade_mod_savepoint(true, 2011082604, 'game');
} }
if ($oldversion < 2013072601) { if ($oldversion < 2013072601) {
// Define field timeclose to be added to game // Define field timeclose to be added to game.
$table = new xmldb_table('game'); $table = new xmldb_table('game');
$field = new xmldb_field('maxattempts', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0', 'subcategories'); $field = new xmldb_field('maxattempts', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0', 'subcategories');
// Conditionally launch add field maxattempts // Conditionally launch add field maxattempts.
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field); $dbman->add_field($table, $field);
} }
// game savepoint reached // Game savepoint reached.
upgrade_mod_savepoint(true, 2013072601, 'game'); upgrade_mod_savepoint(true, 2013072601, 'game');
} }
if( $oldversion < 2015122105) if ($oldversion < 2015122105) {
{
$table = new xmldb_table('game'); $table = new xmldb_table('game');
$field = new xmldb_field('userlanguage', XMLDB_TYPE_CHAR, '100'); $field = new xmldb_field('userlanguage', XMLDB_TYPE_CHAR, '100');
if (!$dbman->field_exists($table, $field)) { if (!$dbman->field_exists($table, $field)) {

712
hangman/play.php

@ -1,90 +1,102 @@
<?php // $Id: play.php,v 1.27 2012/07/25 11:16:05 bdaloukas Exp $ <?php
// This file is part of Moodle - http://moodle.org/
// This file plays the game hangman //
// Moodle is free software: you can redistribute it and/or modify
function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $action, $context) // it under the terms of the GNU General Public License as published by
{ // the Free Software Foundation, either version 3 of the License, or
global $DB, $USER; // (at your option) any later version.
//
if( $attempt != false and $hangman != false){ // Moodle is distributed in the hope that it will be useful,
if( ($action == 'nextword') and ($hangman->finishedword != 0)){ // but WITHOUT ANY WARRANTY; without even the implied warranty of
//finish with one word and continue to another // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
if( !$DB->set_field( 'game_hangman', 'finishedword', 0, array( 'id' => $hangman->id))){ // GNU General Public License for more details.
error( "game_hangman_continue: Can't update game_hangman"); //
} // You should have received a copy of the GNU General Public License
}else // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
{
return game_hangman_play( $id, $game, $attempt, $hangman, false, false, $context); // This file plays the game hangman.
}
} function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $action, $context) {
global $DB, $USER;
if ($attempt != false and $hangman != false) {
if (($action == 'nextword') and ($hangman->finishedword != 0)) {
// Finish with one word and continue to another.
if (!$DB->set_field( 'game_hangman', 'finishedword', 0, array( 'id' => $hangman->id))) {
error( "game_hangman_continue: Can't update game_hangman");
}
} else {
return game_hangman_play( $id, $game, $attempt, $hangman, false, false, $context);
}
}
$updatehangman = (($attempt != false) and ($hangman != false)); $updatehangman = (($attempt != false) and ($hangman != false));
//new game // New game.
srand ((double)microtime()*1000003); srand ((double)microtime() * 1000003);
//I try 10 times to find a new question // I try 10 times to find a new question.
$found = false; $found = false;
$min_num = 0; $minnum = 0;
$unchanged = 0; $unchanged = 0;
for($i=1; $i <= 10; $i++) for ($i = 1; $i <= 10; $i++) {
{ $rec = game_question_shortanswer( $game, $game->param7, false);
$rec = game_question_shortanswer( $game, $game->param7, false); if ($rec === false) {
if( $rec === false){ continue;
continue; }
}
$answer = game_upper( $rec->answertext, $game->language); $answer = game_upper( $rec->answertext, $game->language);
if( $game->language == '') if ($game->language == '') {
{
$game->language = game_detectlanguage( $answer); $game->language = game_detectlanguage( $answer);
$answer = game_upper( $rec->answertext, $game->language); $answer = game_upper( $rec->answertext, $game->language);
} }
$answer2 = $answer; $answer2 = $answer;
if( $game->param7){ if ($game->param7) {
//Have to delete space // Have to delete space.
$answer2 = str_replace( ' ', '', $answer2); $answer2 = str_replace( ' ', '', $answer2);
} }
if( $game->param8){
//Have to delete - if ($game->param8) {
// Have to delete -.
$answer2 = str_replace( '-', '', $answer2); $answer2 = str_replace( '-', '', $answer2);
} }
$allletters = game_getallletters( $answer2, $game->language, $game->userlanguage); $allletters = game_getallletters( $answer2, $game->language, $game->userlanguage);
if( $allletters == ''){ if ($allletters == '') {
continue; continue;
} }
if( $game->param7){ if ($game->param7) {
$allletters .= '_'; $allletters .= '_';
} }
if( $game->param8){
if ($game->param8) {
$allletters .= '-'; $allletters .= '-';
} }
if( $game->param7 == false){ if ($game->param7 == false) {
//I don't allow spaces // I don't allow spaces.
if( strpos( $answer, " ")){ if (strpos( $answer, " ")) {
continue; continue;
} }
} }
$copy = false; $copy = false;
$select2 = 'gameid=? AND userid=? AND questionid=? AND glossaryentryid=?'; $select2 = 'gameid=? AND userid=? AND questionid=? AND glossaryentryid=?';
if( ($rec2 = $DB->get_record_select( 'game_repetitions', $select2, array( $game->id, $USER->id, $rec->questionid, $rec->glossaryentryid), 'id,repetitions AS r')) != false){ if (($rec2 = $DB->get_record_select( 'game_repetitions', $select2,
if( ($rec2->r < $min_num) or ($min_num == 0)){ array( $game->id, $USER->id, $rec->questionid, $rec->glossaryentryid), 'id,repetitions AS r')) != false) {
$min_num = $rec2->r; if (($rec2->r < $minnum) or ($minnum == 0)) {
$minnum = $rec2->r;
$copy = true; $copy = true;
} }
}else } else {
{ $minnum = 0;
$min_num = 0;
$copy = true; $copy = true;
} }
if( $copy){ if ($copy) {
$found = true; $found = true;
$min = new stdClass(); $min = new stdClass();
@ -96,396 +108,384 @@ function game_hangman_continue( $id, $game, $attempt, $hangman, $newletter, $act
$min->answer = $answer; $min->answer = $answer;
$min->language = $game->language; $min->language = $game->language;
$min->allletters = $allletters; $min->allletters = $allletters;
if( $min_num == 0) if ($minnum == 0) {
break; //We found an unused word break; // We found an unused word.
}else }
} else {
$unchanged++; $unchanged++;
}
if( $unchanged > 2) if ($unchanged > 2) {
{ if ($found) {
if( $found)
break; break;
}
} }
} }
if( $found == false){ if ($found == false) {
print_error( get_string( 'no_words', 'game')); print_error( get_string( 'no_words', 'game'));
} }
//Found one word for hangman // Found one word for hangman.
if( $attempt == false){ if ($attempt == false) {
$attempt = game_addattempt( $game); $attempt = game_addattempt( $game);
} }
if( !$DB->set_field( 'game_attempts', 'language', $min->language, array( 'id' => $attempt->id))){
print_error( "game_hangman_continue: Can't set language"); if (!$DB->set_field( 'game_attempts', 'language', $min->language, array( 'id' => $attempt->id))) {
print_error( "game_hangman_continue: Can't set language");
} }
$query = new stdClass(); $query = new stdClass();
$query->attemptid = $attempt->id; $query->attemptid = $attempt->id;
$query->gameid = $game->id; $query->gameid = $game->id;
$query->userid = $USER->id; $query->userid = $USER->id;
$query->sourcemodule = $game->sourcemodule; $query->sourcemodule = $game->sourcemodule;
$query->questionid = $min->questionid; $query->questionid = $min->questionid;
$query->glossaryentryid = $min->glossaryentryid; $query->glossaryentryid = $min->glossaryentryid;
$query->attachment = $min->attachment; $query->attachment = $min->attachment;
$query->questiontext = addslashes( $min->questiontext); $query->questiontext = addslashes( $min->questiontext);
$query->score = 0; $query->score = 0;
$query->timelastattempt = time(); $query->timelastattempt = time();
$query->answertext = $min->answer; $query->answertext = $min->answer;
$query->answerid = $min->answerid; $query->answerid = $min->answerid;
if( !($query->id = $DB->insert_record( 'game_queries', $query))){ if (!($query->id = $DB->insert_record( 'game_queries', $query))) {
print_object( $query); print_error( "game_hangman_continue: Can't insert to table game_queries");
print_error( "game_hangman_continue: Can't insert to table game_queries"); }
}
$newrec = new stdClass();
$newrec = new stdClass(); $newrec->id = $attempt->id;
$newrec->id = $attempt->id; $newrec->queryid = $query->id;
$newrec->queryid = $query->id; if ($updatehangman == false) {
if( $updatehangman == false){ $newrec->maxtries = $game->param4;
$newrec->maxtries = $game->param4; if ($newrec->maxtries == 0) {
if( $newrec->maxtries == 0){ $newrec->maxtries = 1;
$newrec->maxtries = 1; }
} $newrec->finishedword = 0;
$newrec->finishedword = 0; $newrec->corrects = 0;
$newrec->corrects = 0; }
}
$newrec->allletters = $min->allletters;
$newrec->allletters = $min->allletters;
$letters = '';
$letters = ''; if ($game->param1) {
if( $game->param1){ $letters .= game_substr( $min->answer, 0, 1);
$letters .= game_substr( $min->answer, 0, 1); }
}
if( $game->param2){ if ($game->param2) {
$letters .= game_substr( $min->answer, -1, 1); $letters .= game_substr( $min->answer, -1, 1);
} }
$newrec->letters = $letters; $newrec->letters = $letters;
if( $updatehangman == false){ if ($updatehangman == false) {
if( !game_insert_record( 'game_hangman', $newrec)){ if (!game_insert_record( 'game_hangman', $newrec)) {
print_error( 'game_hangman_continue: error inserting in game_hangman'); print_error( 'game_hangman_continue: error inserting in game_hangman');
} }
}else } else {
{ if (!$DB->update_record( 'game_hangman', $newrec)) {
if( !$DB->update_record( 'game_hangman', $newrec)){ print_error( 'game_hangman_continue: error updating in game_hangman');
print_error( 'game_hangman_continue: error updating in game_hangman'); }
} $newrec = $DB->get_record( 'game_hangman', array( 'id' => $newrec->id));
$newrec = $DB->get_record( 'game_hangman', array( 'id' => $newrec->id)); }
}
game_update_repetitions( $game->id, $USER->id, $query->questionid, $query->glossaryentryid);
game_update_repetitions( $game->id, $USER->id, $query->questionid, $query->glossaryentryid);
game_hangman_play( $id, $game, $attempt, $newrec, false, false, $context); game_hangman_play( $id, $game, $attempt, $newrec, false, false, $context);
} }
function game_hangman_onfinishgame( $game, $attempt, $hangman) function game_hangman_onfinishgame( $game, $attempt, $hangman) {
{
global $DB; global $DB;
$score = $hangman->corrects / $hangman->maxtries; $score = $hangman->corrects / $hangman->maxtries;
game_updateattempts( $game, $attempt, $score, true); game_updateattempts( $game, $attempt, $score, true);
if( !$DB->set_field( 'game_hangman', 'finishedword', 0, array( 'id' => $hangman->id))){ if (!$DB->set_field( 'game_hangman', 'finishedword', 0, array( 'id' => $hangman->id))) {
print_error( "game_hangman_onfinishgame: Can't update game_hangman"); print_error( "game_hangman_onfinishgame: Can't update game_hangman");
} }
} }
function game_hangman_play( $id, $game, $attempt, $hangman, $onlyshow, $showsolution, $context) function game_hangman_play( $id, $game, $attempt, $hangman, $onlyshow, $showsolution, $context) {
{ global $CFG, $DB, $OUTPUT;
global $CFG, $DB, $OUTPUT;
$query = $DB->get_record( 'game_queries', array( 'id' => $hangman->queryid)); $query = $DB->get_record( 'game_queries', array( 'id' => $hangman->queryid));
if( $attempt->language != '') if ($attempt->language != '') {
$wordrtl = game_right_to_left( $attempt->language); $wordrtl = game_right_to_left( $attempt->language);
else } else {
$wordrtl = right_to_left(); $wordrtl = right_to_left();
}
$reverseprint = ($wordrtl != right_to_left()); $reverseprint = ($wordrtl != right_to_left());
if( $game->toptext != ''){ if ($game->toptext != '') {
echo $game->toptext.'<br>'; echo $game->toptext.'<br>';
} }
$max=$game->param10; // maximum number of wrong $max = $game->param10; // Maximum number of wrongs.
if( $max <= 0) if ($max <= 0) {
$max = 6; $max = 6;
hangman_showpage( $done, $correct, $wrong, $max, $word_line, $word_line2, $links, $game, $attempt, $hangman, $query, $onlyshow, $showsolution, $context); }
hangman_showpage( $done, $correct, $wrong, $max, $wordline, $wordline2, $links, $game,
$attempt, $hangman, $query, $onlyshow, $showsolution, $context);
if (!$done) if (!$done) {
{ if ($wrong > $max) {
if ($wrong > $max){
$wrong = $max; $wrong = $max;
} }
if( $game->param3 == 0){ if ($game->param3 == 0) {
$game->param3 = 1; $game->param3 = 1;
} }
echo "\r\n<br/><img src=\"".$OUTPUT->pix_url('hangman/'.$game->param3.'/hangman_'.$wrong, 'mod_game')."\""; echo "\r\n<br/><img src=\"".$OUTPUT->pix_url('hangman/'.$game->param3.'/hangman_'.$wrong, 'mod_game')."\"";
$message = sprintf( get_string( 'hangman_wrongnum', 'game'), $wrong, $max); $message = sprintf( get_string( 'hangman_wrongnum', 'game'), $wrong, $max);
echo ' ALIGN="MIDDLE" BORDER="0" HEIGHT="100" alt="'.$message.'"/>'; echo ' ALIGN="MIDDLE" BORDER="0" HEIGHT="100" alt="'.$message.'"/>';
if ($wrong >= $max){ if ($wrong >= $max) {
// This word is incorrect. If reach the max number of word I have to finish else continue with next word.
//This word is incorrect. If reach the max number of word I have to finish else continue with next word hangman_onincorrect( $id, $wordline, $query->answertext, $game, $attempt, $hangman, $onlyshow, $showsolution);
hangman_onincorrect( $id, $word_line, $query->answertext, $game, $attempt, $hangman, $onlyshow, $showsolution); } else {
}else $i = $max - $wrong;
{ if ($i > 1) {
$i = $max-$wrong; echo ' '.get_string( 'hangman_restletters_many', 'game', $i);
if( $i > 1) } else {
echo ' '.get_string( 'hangman_restletters_many', 'game', $i); echo ' '.get_string( 'hangman_restletters_one', 'game');
else }
echo ' '.get_string( 'hangman_restletters_one', 'game'); if ($reverseprint) {
if( $reverseprint){
echo '<SPAN dir="'.($wordrtl ? 'rtl' : 'ltr').'">'; echo '<SPAN dir="'.($wordrtl ? 'rtl' : 'ltr').'">';
} }
echo "<br/><font size=\"5\">\n$word_line</font>\r\n"; echo "<br/><font size=\"5\">\n$wordline</font>\r\n";
if( $word_line2 != ''){ if ($wordline2 != '') {
echo "<br/><font size=\"5\">\n$word_line2</font>\r\n"; echo "<br/><font size=\"5\">\n$wordline2</font>\r\n";
} }
if( $reverseprint){ if ($reverseprint) {
echo "</SPAN>"; echo "</SPAN>";
} }
if( $hangman->finishedword == false){ if ($hangman->finishedword == false) {
echo "<br/><br/><BR/>".get_string( 'hangman_letters', 'game').$links."\r\n"; echo "<br/><br/><BR/>".get_string( 'hangman_letters', 'game').$links."\r\n";
} }
} }
}else } else {
{ // This word is correct. If reach the max number of word I have to finish else continue with next word.
//This word is correct. If reach the max number of word I have to finish else continue with next word hangman_oncorrect( $id, $wordline, $game, $attempt, $hangman, $query);
hangman_oncorrect( $id, $word_line, $game, $attempt, $hangman, $query); }
}
echo "<br/><br/>".get_string( 'grade', 'game').' : '.round( $query->percent * 100).' %';
echo "<br/><br/>".get_string( 'grade', 'game').' : '.round( $query->percent * 100).' %'; if ($hangman->maxtries > 1) {
if( $hangman->maxtries > 1){ echo '<br/><br/>'.get_string( 'hangman_gradeinstance', 'game').' : '.
echo '<br/><br/>'.get_string( 'hangman_gradeinstance', 'game').' : '.round( $hangman->corrects / $hangman->maxtries * 100).' %'; round( $hangman->corrects / $hangman->maxtries * 100).' %';
} }
if( $game->bottomtext != ''){ if ($game->bottomtext != '') {
echo '<br><br>'.$game->bottomtext; echo '<br><br>'.$game->bottomtext;
} }
} }
function hangman_showpage(&$done, &$correct, &$wrong, $max, &$word_line, &$word_line2, &$links, $game, &$attempt, &$hangman, &$query, $onlyshow, $showsolution, $context) function hangman_showpage(&$done, &$correct, &$wrong, $max, &$wordline, &$wordline2, &$links,
{ $game, &$attempt, &$hangman, &$query, $onlyshow, $showsolution, $context) {
global $USER, $CFG, $DB; global $USER, $CFG, $DB;
$id = optional_param('id', 0, PARAM_INT); // Course Module ID, or $id = optional_param('id', 0, PARAM_INT); // Course Module ID.
$word = $query->answertext; $word = $query->answertext;
$newletter = optional_param('newletter', "", PARAM_TEXT); $newletter = optional_param('newletter', "", PARAM_TEXT);
if( $newletter == '_'){ if ( $newletter == '_') {
$newletter = ' '; $newletter = ' ';
} }
$letters = $hangman->letters; $letters = $hangman->letters;
if( $newletter != NULL) if ($newletter != null) {
{ if (game_strpos( $letters, $newletter) === false) {
if( game_strpos( $letters,$newletter) === false){ $letters .= $newletter;
$letters .= $newletter; }
}
} }
$links=""; $links = "";
$alpha = $hangman->allletters; $alpha = $hangman->allletters;
$wrong = 0; $wrong = 0;
if( $query->questionid) if ($query->questionid) {
{ $questiontext = str_replace( array("\'", '\"'), array("'", '"'), $query->questiontext);
$questiontext = str_replace( array("\'", '\"'), array("'", '"'), $query->questiontext);
$query->questiontext = game_filterquestion($questiontext, $query->questionid, $context->id, $game->course); $query->questiontext = game_filterquestion($questiontext, $query->questionid, $context->id, $game->course);
}else } else {
{
$cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course); $cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course);
$contextglossary = game_get_context_module_instance( $cmglossary->id); $contextglossary = game_get_context_module_instance( $cmglossary->id);
$query->questiontext = game_filterglossary(str_replace( '\"', '"', $query->questiontext), $query->glossaryentryid, $contextglossary->id, $game->course); $query->questiontext = game_filterglossary(str_replace( '\"', '"',
$query->questiontext), $query->glossaryentryid, $contextglossary->id, $game->course);
} }
if( $game->param5){ if ($game->param5) {
$s = trim( game_filtertext( $query->questiontext, $game->course)); $s = trim( game_filtertext( $query->questiontext, $game->course));
if( $s != '.' and $s <> ''){ if ($s != '.' and $s <> '') {
echo "<br/><b>".$s.'</b>'; echo "<br/><b>".$s.'</b>';
} }
if( $query->attachment != ''){ if ($query->attachment != '') {
$file = "{$CFG->wwwroot}/file.php/$game->course/moddata/$query->attachment"; $file = "{$CFG->wwwroot}/file.php/$game->course/moddata/$query->attachment";
echo "<img src=\"$file\" />"; echo "<img src=\"$file\" />";
} }
echo "<br/><br/>"; echo "<br/><br/>";
} }
$word_line = $word_line2 = ""; $wordline = $wordline2 = "";
$len = game_strlen( $word); $len = game_strlen( $word);
$done = 1; $done = 1;
$answer = ''; $answer = '';
$correct = 0; $correct = 0;
for ($x=0; $x < $len; $x++) for ($x = 0; $x < $len; $x++) {
{ $char = game_substr( $word, $x, 1);
$char = game_substr( $word, $x, 1);
if ($showsolution) {
if( $showsolution){ $wordline2 .= ( $char == " " ? '&nbsp; ' : $char);
$word_line2 .= ( $char == " " ? '&nbsp; ' : $char); $done = 0;
$done = 0; }
}
if (game_strpos($letters, $char) === false) {
if ( game_strpos($letters, $char) === false){ $wordline .= "_<font size=\"1\">&nbsp;</font>\r\n";
$word_line.="_<font size=\"1\">&nbsp;</font>\r\n"; $done = 0;
$done = 0; $answer .= '_';
$answer .= '_'; } else {
}else $wordline .= ( $char == " " ? '&nbsp; ' : $char);
{ $answer .= $char;
$word_line .= ( $char == " " ? '&nbsp; ' : $char); $correct++;
$answer .= $char; }
$correct++; }
}
} $lenalpha = game_strlen( $alpha);
$fontsize = 5;
$len_alpha = game_strlen($alpha);
$fontsize = 5; for ($c = 0; $c < $lenalpha; $c++) {
$char = game_substr( $alpha, $c, 1);
for ($c=0; $c < $len_alpha; $c++)
{ if (game_strpos($letters, $char) === false) {
$char = game_substr( $alpha, $c, 1); // User doesn't select this character.
$params = 'id='.$id.'&amp;newletter='.urlencode( $char);
if ( game_strpos($letters, $char) === false) if ($onlyshow or $showsolution) {
{ $links .= $char;
//User doesn't select this character } else {
$params = 'id='.$id.'&amp;newletter='.urlencode( $char); $links .= "<font size=\"$fontsize\"><a href=\"attempt.php?$params\">$char</a></font>\r\n";
if( $onlyshow or $showsolution){ }
$links .= $char; continue;
}else }
{
$links .= "<font size=\"$fontsize\"><a href=\"attempt.php?$params\">$char</a></font>\r\n"; if (game_strpos($word, $char) === false) {
} $links .= "\r\n<font size=\"$fontsize\" color=\"red\">$char </font>";
continue; $wrong++;
} } else {
$links .= "\r\n<B><font size=\"$fontsize\">$char </font></B> ";
if ( game_strpos($word, $char) === false) }
{ }
$links .= "\r\n<font size=\"$fontsize\" color=\"red\">$char </font>"; $finishedword = ($done or $wrong >= $max);
$wrong++; $finished = false;
}else
{
$links .= "\r\n<B><font size=\"$fontsize\">$char </font></B> ";
}
}
$finishedword = ($done or $wrong >= $max);
$finished = false;
$updrec = new stdClass(); $updrec = new stdClass();
$updrec->id = $hangman->id; $updrec->id = $hangman->id;
$updrec->letters = $letters; $updrec->letters = $letters;
if( $finishedword){ if ($finishedword) {
if( $hangman->finishedword == 0){ if ($hangman->finishedword == 0) {
//only one time per word increace the variable try // Only one time per word increace the variable try.
$hangman->try = $hangman->try + 1; $hangman->try = $hangman->try + 1;
if( $hangman->try > $hangman->maxtries){ if ($hangman->try > $hangman->maxtries) {
$finished = true; $finished = true;
} }
if( $done){ if ($done) {
$hangman->corrects = $hangman->corrects + 1; $hangman->corrects = $hangman->corrects + 1;
$updrec->corrects = $hangman->corrects; $updrec->corrects = $hangman->corrects;
} }
} }
$updrec->try = $hangman->try; $updrec->try = $hangman->try;
$updrec->finishedword = 1; $updrec->finishedword = 1;
}
}
$query->percent = ($correct - $wrong / $max) / game_strlen( $word);
$query->percent = ($correct -$wrong/$max) / game_strlen( $word); if ($query->percent < 0) {
if( $query->percent < 0){ $query->percent = 0;
$query->percent = 0; }
}
if ($onlyshow or $showsolution) {
if( $onlyshow or $showsolution){ return;
return; }
}
if (!$DB->update_record( 'game_hangman', $updrec)) {
if( !$DB->update_record( 'game_hangman', $updrec)){ print_error( "hangman_showpage: Can't update game_hangman id=$updrec->id");
print_error( "hangman_showpage: Can't update game_hangman id=$updrec->id"); }
}
if ($done) {
if( $done){ $score = 1;
$score = 1; } else if ($wrong >= $max) {
}else if( $wrong >= $max){ $score = 0;
$score = 0; } else {
}else $score = -1;
{ }
$score = -1;
} game_updateattempts( $game, $attempt, $score, $finished);
game_update_queries( $game, $attempt, $query, $score, $answer);
game_updateattempts( $game, $attempt, $score, $finished);
game_update_queries( $game, $attempt, $query, $score, $answer);
} }
//This word is correct. If reach the max number of words I have to finish else continue with next word // This word is correct.
function hangman_oncorrect( $id, $word_line, $game, $attempt, $hangman, $query) // If reach the max number of words I have to finish else continue with next word.
{ function hangman_oncorrect( $id, $wordline, $game, $attempt, $hangman, $query) {
global $DB; global $DB;
echo "<BR/><BR/><font size=\"5\">\n$word_line</font>\r\n"; echo "<br/><br/><font size=\"5\">\n$wordline</font>\r\n";
echo '<p><BR/><font size="5" color="green">'.get_string( 'win', 'game').'</font><BR/><BR/></p>'; echo '<p><br/><font size="5" color="green">'.get_string( 'win', 'game').'</font><BR/><BR/></p>';
if( $query->answerid){ if ($query->answerid) {
$feedback = $DB->get_field( 'question_answers', 'feedback', array( 'id' => $query->answerid)); $feedback = $DB->get_field( 'question_answers', 'feedback', array( 'id' => $query->answerid));
if( $feedback != ''){ if ($feedback != '') {
echo "$feedback<br>"; echo "$feedback<br>";
} }
} }
game_hangman_show_nextword( $id, $game, $attempt, $hangman); game_hangman_show_nextword( $id, $game, $attempt, $hangman);
} }
function hangman_onincorrect( $id, $word_line, $word, $game, $attempt, $hangman, $onlyshow, $showsolution) function hangman_onincorrect( $id, $wordline, $word, $game, $attempt, $hangman, $onlyshow, $showsolution) {
{ echo "\r\n<br/><br/><font size=\"5\">\n$wordline</font>\r\n";
echo "\r\n<BR/><BR/><font size=\"5\">\n$word_line</font>\r\n";
if( $onlyshow or $showsolution)
return;
echo '<p><BR/><font size="5" color="red">'.get_string( 'hangman_loose', 'game').'</font><BR/><BR/></p>'; if ( $onlyshow or $showsolution) {
return;
}
if( $game->param6){ echo '<p><BR/><font size="5" color="red">'.get_string( 'hangman_loose', 'game').'</font><BR/><BR/></p>';
//show the correct answer
if( game_strpos($word, ' ') != false)
echo '<br/>'.get_string( 'hangman_correct_phrase', 'game');
else
echo '<br/>'.get_string( 'hangman_correct_word', 'game');
echo '<B>'.$word."</B><BR/><BR/>\r\n"; if ($game->param6) {
} // Show the correct answer.
if (game_strpos($word, ' ') != false) {
echo '<br/>'.get_string( 'hangman_correct_phrase', 'game');
} else {
echo '<br/>'.get_string( 'hangman_correct_word', 'game');
}
echo '<B>'.$word."</B><BR/><BR/>\r\n";
}
game_hangman_show_nextword( $id, $game, $attempt, $hangman, true); game_hangman_show_nextword( $id, $game, $attempt, $hangman, true);
} }
function game_hangman_show_nextword( $id, $game, $attempt, $hangman) function game_hangman_show_nextword( $id, $game, $attempt, $hangman) {
{ global $CFG, $DB;
global $CFG, $DB;
echo '<br/>';
if( ($hangman->try < $hangman->maxtries) or ($hangman->maxtries == 0)){
//continue to next word
$params = "id=$id&action2=nextword\">".get_string( 'nextword', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp;'; echo '<br/>';
if (($hangman->try < $hangman->maxtries) or ($hangman->maxtries == 0)) {
// Continue to next word.
$params = "id=$id&action2=nextword\">".get_string( 'nextword', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp;';
echo "<a href=\"{$CFG->wwwroot}/mod/game/attempt.php?$params"; echo "<a href=\"{$CFG->wwwroot}/mod/game/attempt.php?$params";
}else } else {
{ game_hangman_onfinishgame( $game, $attempt, $hangman);
game_hangman_onfinishgame( $game, $attempt, $hangman);
if( game_can_start_new_attempt( $game)) if (game_can_start_new_attempt( $game)) {
echo "<a href=\"{$CFG->wwwroot}/mod/game/attempt.php?id=$id\">".get_string( 'nextgame', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp; '; echo "<a href=\"{$CFG->wwwroot}/mod/game/attempt.php?id=$id\">".
} get_string( 'nextgame', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp; ';
}
}
if (! $cm = $DB->get_record('course_modules', array( 'id' => $id))) { if (! $cm = $DB->get_record('course_modules', array( 'id' => $id))) {
print_error( "Course Module ID was incorrect id=$id"); print_error( "Course Module ID was incorrect id=$id");
} }
echo "<a href=\"{$CFG->wwwroot}/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'</a> '; echo "<a href=\"{$CFG->wwwroot}/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'</a> ';
} }

114
hiddenpicture/picture.php

@ -1,51 +1,64 @@
<?php // $Id: picture.php,v 1.3 2010/07/26 00:13:32 bdaloukas Exp $ <?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require( '../../../config.php'); require( '../../../config.php');
$id = required_param('id', PARAM_INT); // Course Module ID $id = required_param('id', PARAM_INT); // Course Module ID.
$attemptid = required_param('id2', PARAM_INT); // Course Module ID $attemptid = required_param('id2', PARAM_INT); // Course Module ID.
$foundcells = required_param('f', PARAM_SEQUENCE); //CSV $foundcells = required_param('f', PARAM_SEQUENCE); // CSV.
$cells = required_param('cells', PARAM_SEQUENCE); //CSV $cells = required_param('cells', PARAM_SEQUENCE); // CSV.
$filehash = required_param('p', PARAM_PATH); $filehash = required_param('p', PARAM_PATH);
$cols = required_param('cols', PARAM_INT); $cols = required_param('cols', PARAM_INT);
$rows = required_param('rows', PARAM_INT); $rows = required_param('rows', PARAM_INT);
$filenamenumbers = required_param('n', PARAM_PATH); //Path to numbers picture $filenamenumbers = required_param('n', PARAM_PATH); // Path to numbers picture.
create_image( $id, $attemptid, $foundcells, $cells, $filehash, $cols, $rows, $filenamenumbers); create_image( $id, $attemptid, $foundcells, $cells, $filehash, $cols, $rows, $filenamenumbers);
function create_image( $id, $attemptid, $foundcells, $cells, $filehash, $cols, $rows, $filenamenumbers) function create_image( $id, $attemptid, $foundcells, $cells, $filehash, $cols, $rows, $filenamenumbers) {
{
global $CFG; global $CFG;
$a = explode( ',', $foundcells); $a = explode( ',', $foundcells);
$found = array(); $found = array();
foreach( $a as $s){ foreach ($a as $s) {
$found[ $s] = 1; $found[ $s] = 1;
} }
$a = explode( ',', $cells); $a = explode( ',', $cells);
$cells = array(); $cells = array();
foreach( $a as $s){ foreach ($a as $s) {
$cells[ $s] = 1; $cells[ $s] = 1;
} }
$file = get_file_storage()->get_file_by_hash( $filehash); $file = get_file_storage()->get_file_by_hash( $filehash);
$image = $file->get_imageinfo(); $image = $file->get_imageinfo();
if( $image === false){ if ($image === false) {
die("Aknown filehash $filehash"); die("Aknown filehash $filehash");
return false; return false;
} }
$img_handle = imagecreatefromstring($file->get_content()); $imghandle = imagecreatefromstring($file->get_content());
$mime = $image[ 'mimetype']; $mime = $image[ 'mimetype'];
$img_numbers = imageCreateFromPNG( $filenamenumbers); $imgnumbers = imagecreatefrompng( $filenamenumbers);
$size_numbers = getimagesize ($filenamenumbers); $sizenumbers = getimagesize ($filenamenumbers);
Header ("Content-type: $mime"); header("Content-type: $mime");
$color = ImageColorAllocate ($img_handle, 100, 100, 100); $color = imagecolorallocate( $imghandle, 100, 100, 100);
$width = $image[ 'width']; $width = $image[ 'width'];
$height = $image[ 'height']; $height = $image[ 'height'];
@ -53,55 +66,54 @@ function create_image( $id, $attemptid, $foundcells, $cells, $filehash, $cols, $
$font = 1; $font = 1;
for($y = 0; $y < $rows; $y++){ for ($y = 0; $y < $rows; $y++) {
for( $x=0; $x < $cols; $x++){ for ($x = 0; $x < $cols; $x++) {
$pos++; $pos++;
if( !array_key_exists( $pos, $found)){ if (!array_key_exists( $pos, $found)) {
$x1 = $x * $width / $cols; $x1 = $x * $width / $cols;
$y1 = $y * $height / $rows; $y1 = $y * $height / $rows;
imagefilledrectangle( $img_handle, $x1, $y1, $x1 + $width / $cols, $y1 + $height / $rows, $color); imagefilledrectangle( $imghandle, $x1, $y1, $x1 + $width / $cols, $y1 + $height / $rows, $color);
if( array_key_exists( $pos, $cells)){ if (array_key_exists( $pos, $cells)) {
shownumber( $img_handle, $img_numbers, $pos, $x1 , $y1, $width / $cols, $height / $rows, $size_numbers); shownumber( $imghandle, $imgnumbers, $pos, $x1 , $y1, $width / $cols, $height / $rows, $sizenumbers);
} }
} }
} }
} }
switch( $mime){ switch ($mime) {
case 'image/png': case 'image/png':
ImagePng ($img_handle); imagepng ($imghandle);
break; break;
case 'image/jpeg': case 'image/jpeg':
ImageJpeg ($img_handle); imagejpeg ($imghandle);
break; break;
case 'image/gif': case 'image/gif':
ImageGif ($img_handle); imagegif ($imghandle);
break; break;
default: default:
die('Aknown mime type $mime'); die('Aknown mime type $mime');
return false; return false;
} }
ImageDestroy ($img_handle); imagedestroy ($imghandle);
} }
function shownumber( $img_handle, $img_numbers, $number, $x1 , $y1, $width, $height, $size_numbers){ function shownumber( $imghandle, $imgnumbers, $number, $x1 , $y1, $width, $height, $sizenumbers) {
if( $number < 10){ if ($number < 10) {
$width_number = $size_numbers[ 0] / 10; $widthnumber = $sizenumbers[ 0] / 10;
$dstX = $x1 + $width / 3; $dstx = $x1 + $width / 3;
$dstY = $y1 + $height / 3; $dsty = $y1 + $height / 3;
$srcX = $number * $size_numbers[ 0] / 10; $srcx = $number * $sizenumbers[ 0] / 10;
$srcW = $size_numbers[ 0]/10; $srcw = $sizenumbers[ 0] / 10;
$srcH = $size_numbers[ 1]; $srch = $sizenumbers[ 1];
$dstW = $width / 10; $dstw = $width / 10;
$dstH = $dstW * $srcH / $srcW; $dsth = $dstw * $srch / $srcw;
imagecopyresized( $img_handle, $img_numbers, $dstX, $dstY, $srcX, 0, $dstW, $dstH, $srcW, $srcH); imagecopyresized( $imghandle, $imgnumbers, $dstx, $dsty, $srcx, 0, $dstw, $dsth, $srcw, $srchh);
}else } else {
{
$number1 = floor( $number / 10); $number1 = floor( $number / 10);
$number2 = $number % 10; $number2 = $number % 10;
shownumber( $img_handle, $img_numbers, $number1, $x1-$width/20, $y1, $width, $height, $size_numbers); shownumber( $imghandle, $imgnumbers, $number1, $x1 - $width / 20, $y1, $width, $height, $sizenumbers);
shownumber( $img_handle, $img_numbers, $number2, $x1+$width/20, $y1, $width, $height, $size_numbers); shownumber( $imghandle, $imgnumbers, $number2, $x1 + $width / 20, $y1, $width, $height, $sizenumbers);
} }
} }

456
hiddenpicture/play.php

@ -1,109 +1,123 @@
<?php // $Id: play.php,v 1.19 2012/08/15 09:26:55 bdaloukas Exp $ <?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
// This file plays the game Hidden Picture.
function game_hiddenpicture_continue( $id, $game, $attempt, $hiddenpicture, $context) {
global $DB, $USER;
// This file plays the game Hidden Picture if ($attempt != false and $hiddenpicture != false) {
// Continue a previous attempt.
return game_hiddenpicture_play( $id, $game, $attempt, $hiddenpicture, false, $context);
}
function game_hiddenpicture_continue( $id, $game, $attempt, $hiddenpicture, $context) if ($attempt == false) {
{ // Start a new attempt.
global $DB, $USER; $attempt = game_addattempt( $game);
}
if( $attempt != false and $hiddenpicture != false){ $cols = $game->param1;
//Continue a previous attempt $rows = $game->param2;
return game_hiddenpicture_play( $id, $game, $attempt, $hiddenpicture, false, $context); if ($cols == 0) {
} print_error( get_string( 'hiddenpicture_nocols', 'game'));
}
if( $attempt == false){ if ($rows == 0) {
//Start a new attempt print_error( get_string( 'hiddenpicture_norows', 'game'));
$attempt = game_addattempt( $game); }
}
// New attempt.
$cols = $game->param1;
$rows = $game->param2;
if( $cols == 0){
print_error( get_string( 'hiddenpicture_nocols', 'game'));
}
if( $rows == 0){
print_error( get_string( 'hiddenpicture_norows', 'game'));
}
//new attempt
$n = $game->param1 * $game->param2; $n = $game->param1 * $game->param2;
$recs = game_questions_selectrandom( $game, CONST_GAME_TRIES_REPETITION*$n); $recs = game_questions_selectrandom( $game, CONST_GAME_TRIES_REPETITION * $n);
$selected_recs = game_select_from_repetitions( $game, $recs, $n); $selectedrecs = game_select_from_repetitions( $game, $recs, $n);
$newrec = game_hiddenpicture_selectglossaryentry( $game, $attempt); $newrec = game_hiddenpicture_selectglossaryentry( $game, $attempt);
if( $recs === false){ if ($recs === false) {
print_error( get_string( 'no_questions', 'game')); print_error( get_string( 'no_questions', 'game'));
} }
$positions = array(); $positions = array();
$pos=1; $pos = 1;
for($col=0; $col < $cols; $col++){ for ($col = 0; $col < $cols; $col++) {
for( $row=0; $row < $rows; $row++){ for ($row = 0; $row < $rows; $row++) {
$positions[] = $pos++; $positions[] = $pos++;
} }
} }
$i = 0; $i = 0;
$field = ($game->sourcemodule == 'glossary' ? 'glossaryentryid' : 'questionid'); $field = ($game->sourcemodule == 'glossary' ? 'glossaryentryid' : 'questionid');
foreach( $recs as $rec) foreach ($recs as $rec) {
{ if ($game->sourcemodule == 'glossary') {
if( $game->sourcemodule == 'glossary') $key = $rec->glossaryentryid;
$key = $rec->glossaryentryid; } else {
else $key = $rec->questionid;
$key = $rec->questionid; }
if( !array_key_exists( $key, $selected_recs)) if (!array_key_exists( $key, $selectedrecs)) {
continue; continue;
}
$query = new stdClass(); $query = new stdClass();
$query->attemptid = $newrec->id; $query->attemptid = $newrec->id;
$query->gamekind = $game->gamekind; $query->gamekind = $game->gamekind;
$query->gameid = $game->id; $query->gameid = $game->id;
$query->userid = $USER->id; $query->userid = $USER->id;
$pos = array_rand( $positions); $pos = array_rand( $positions);
$query->col = $positions[ $pos]; $query->col = $positions[ $pos];
unset( $positions[ $pos]); unset( $positions[ $pos]);
$query->sourcemodule = $game->sourcemodule; $query->sourcemodule = $game->sourcemodule;
$query->questionid = $rec->questionid; $query->questionid = $rec->questionid;
$query->glossaryentryid = $rec->glossaryentryid; $query->glossaryentryid = $rec->glossaryentryid;
$query->score = 0; $query->score = 0;
if( ($query->id = $DB->insert_record( 'game_queries', $query)) == 0){ if (($query->id = $DB->insert_record( 'game_queries', $query)) == 0) {
print_error( 'error inserting in game_queries'); print_error( 'error inserting in game_queries');
} }
game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid); game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid);
} }
//The score is zero // The score is zero.
game_updateattempts( $game, $attempt, 0, 0); game_updateattempts( $game, $attempt, 0, 0);
game_hiddenpicture_play( $id, $game, $attempt, $newrec, false, $context); game_hiddenpicture_play( $id, $game, $attempt, $newrec, false, $context);
} }
//Create the game_hiddenpicture record // Create the game_hiddenpicture record.
function game_hiddenpicture_selectglossaryentry( $game, $attempt){ function game_hiddenpicture_selectglossaryentry( $game, $attempt) {
global $CFG, $DB, $USER; global $CFG, $DB, $USER;
srand( (double)microtime()*1000000); srand( (double)microtime() * 1000000);
if( $game->glossaryid2 == 0){ if ($game->glossaryid2 == 0) {
print_error( get_string( 'must_select_glossary', 'game')); print_error( get_string( 'must_select_glossary', 'game'));
} }
$select = "ge.glossaryid={$game->glossaryid2}"; $select = "ge.glossaryid={$game->glossaryid2}";
$table = '{glossary_entries} ge'; $table = '{glossary_entries} ge';
if( $game->glossarycategoryid2){ if ($game->glossarycategoryid2) {
$table .= ",{glossary_entries_categories} gec"; $table .= ",{glossary_entries_categories} gec";
$select .= " AND gec.entryid = ge.id AND gec.categoryid = {$game->glossarycategoryid2}"; $select .= " AND gec.entryid = ge.id AND gec.categoryid = {$game->glossarycategoryid2}";
} }
if( $game->param7 == 0){ if ($game->param7 == 0) {
//Allow spaces // Allow spaces.
$select .= " AND concept NOT LIKE '% %'"; $select .= " AND concept NOT LIKE '% %'";
} }
$sql = "SELECT ge.id,attachment FROM $table WHERE $select"; $sql = "SELECT ge.id,attachment FROM $table WHERE $select";
if( ($recs=$DB->get_records_sql( $sql)) == false){ if (($recs = $DB->get_records_sql( $sql)) == false) {
$a->name = "'".$DB->get_field('glossary', 'name', array( 'id' => $game->glossaryid2))."'"; $a->name = "'".$DB->get_field('glossary', 'name', array( 'id' => $game->glossaryid2))."'";
print_error( get_string( 'hiddenpicture_nomainquestion', 'game', $a)); print_error( get_string( 'hiddenpicture_nomainquestion', 'game', $a));
return false; return false;
} }
@ -112,58 +126,56 @@ function game_hiddenpicture_selectglossaryentry( $game, $attempt){
$fs = get_file_storage(); $fs = get_file_storage();
$cmg = get_coursemodule_from_instance('glossary', $game->glossaryid2, $game->course); $cmg = get_coursemodule_from_instance('glossary', $game->glossaryid2, $game->course);
$context = game_get_context_module_instance( $cmg->id); $context = game_get_context_module_instance( $cmg->id);
foreach( $recs as $rec){ foreach ($recs as $rec) {
$files = $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $rec->id, "timemodified", false); $files = $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $rec->id, "timemodified", false);
if( $files) if ($files) {
{ foreach ($files as $key => $file) {
foreach( $files as $key => $file)
{
$s = strtoupper( $file->get_filename()); $s = strtoupper( $file->get_filename());
$s = substr( $s, -4); $s = substr( $s, -4);
if( $s == '.GIF' or $s == '.JPG' or $s == '.PNG'){ if ($s == '.GIF' or $s == '.JPG' or $s == '.PNG') {
$ids[] = $rec->id; $ids[] = $rec->id;
$keys[] = $file->get_pathnamehash(); $keys[] = $file->get_pathnamehash();
} }
} }
} }
} }
if( count( $ids) == 0){ if (count( $ids) == 0) {
$a->name = "'".$DB->get_field( 'glossary', 'name', array( 'id' => $game->glossaryid2))."'"; $a->name = "'".$DB->get_field( 'glossary', 'name', array( 'id' => $game->glossaryid2))."'";
print_error( get_string( 'hiddenpicture_nomainquestion', 'game', $a)); print_error( get_string( 'hiddenpicture_nomainquestion', 'game', $a));
return false; return false;
} }
//Have to select randomly one glossaryentry // Have to select randomly one glossaryentry.
$poss = array(); $poss = array();
for($i=0;$i<count($ids);$i++){ for ($i = 0; $i < count($ids); $i++) {
$poss[] = $i; $poss[] = $i;
} }
shuffle( $poss); shuffle( $poss);
$min_num = 0; $minnum = 0;
$attachement = ''; $attachement = '';
for($i=0;$i<count($ids);$i++){ for ($i = 0; $i < count($ids); $i++) {
$pos = $poss[ $i]; $pos = $poss[ $i];
$tempid = $ids[ $pos]; $tempid = $ids[ $pos];
$a = array( 'gameid' => $game->id, 'userid' => $USER->id, 'questionid' => 0, 'glossaryentryid' => $tempid); $a = array( 'gameid' => $game->id, 'userid' => $USER->id, 'questionid' => 0, 'glossaryentryid' => $tempid);
if(($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions r')) != false){ if (($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions r')) != false) {
if( ($rec2->r < $min_num) or ($min_num == 0)){ if (($rec2->r < $minnum) or ($minnum == 0)) {
$min_num = $rec2->r; $minnum = $rec2->r;
$glossaryentryid = $tempid; $glossaryentryid = $tempid;
$attachement = $keys[ $pos]; $attachement = $keys[ $pos];
} }
} } else {
else{
$glossaryentryid = $tempid; $glossaryentryid = $tempid;
$attachement = $keys[ $pos]; $attachement = $keys[ $pos];
break; break;
} }
} }
$sql = 'SELECT id, concept as answertext, definition as questiontext, id as glossaryentryid, 0 as questionid, glossaryid, attachment'. $sql = 'SELECT id, concept as answertext, definition as questiontext,'.
' FROM {glossary_entries} WHERE id = '.$glossaryentryid; ' id as glossaryentryid, 0 as questionid, glossaryid, attachment'.
if( ($rec = $DB->get_record_sql( $sql)) == false) ' FROM {glossary_entries} WHERE id = '.$glossaryentryid;
if (($rec = $DB->get_record_sql( $sql)) == false) {
return false; return false;
}
$query = new stdClass(); $query = new stdClass();
$query->attemptid = $attempt->id; $query->attemptid = $attempt->id;
$query->gamekind = $game->gamekind; $query->gamekind = $game->gamekind;
@ -174,91 +186,94 @@ function game_hiddenpicture_selectglossaryentry( $game, $attempt){
$query->sourcemodule = 'glossary'; $query->sourcemodule = 'glossary';
$query->questionid = 0; $query->questionid = 0;
$query->glossaryentryid = $rec->glossaryentryid; $query->glossaryentryid = $rec->glossaryentryid;
$query->attachment = $attachement; $query->attachment = $attachement;
$query->questiontext = $rec->questiontext; $query->questiontext = $rec->questiontext;
$query->answertext = $rec->answertext; $query->answertext = $rec->answertext;
$query->score = 0; $query->score = 0;
if( ($query->id = $DB->insert_record( 'game_queries', $query)) == 0){ if (($query->id = $DB->insert_record( 'game_queries', $query)) == 0) {
print_error( 'Error inserting in game_queries'); print_error( 'Error inserting in game_queries');
} }
$newrec = new stdClass(); $newrec = new stdClass();
$newrec->id = $attempt->id; $newrec->id = $attempt->id;
$newrec->correct = 0; $newrec->correct = 0;
if( !game_insert_record( 'game_hiddenpicture', $newrec)){ if (!game_insert_record( 'game_hiddenpicture', $newrec)) {
print_error( 'Error inserting in game_hiddenpicture'); print_error( 'Error inserting in game_hiddenpicture');
} }
game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid); game_update_repetitions($game->id, $USER->id, $query->questionid, $query->glossaryentryid);
return $newrec; return $newrec;
} }
function game_hiddenpicture_play( $id, $game, $attempt, $hiddenpicture, $showsolution, $context) function game_hiddenpicture_play( $id, $game, $attempt, $hiddenpicture, $showsolution, $context) {
{ if ($game->toptext != '') {
if( $game->toptext != ''){ echo $game->toptext.'<br>';
echo $game->toptext.'<br>'; }
}
//Show picture // Show picture.
$offsetquestions = game_sudoku_compute_offsetquestions( $game->sourcemodule, $attempt, $numbers, $correctquestions); $offsetquestions = game_sudoku_compute_offsetquestions( $game->sourcemodule, $attempt, $numbers, $correctquestions);
unset( $offsetquestions[ 0]); unset( $offsetquestions[ 0]);
game_hiddenpicture_showhiddenpicture( $id, $game, $attempt, $hiddenpicture, $showsolution, $offsetquestions, $correctquestions, $id, $attempt, $showsolution); game_hiddenpicture_showhiddenpicture( $id, $game, $attempt, $hiddenpicture, $showsolution,
$offsetquestions, $correctquestions, $id, $attempt, $showsolution);
//Show questions // Show questions.
$onlyshow = false; $onlyshow = false;
$showsolution = false; $showsolution = false;
switch( $game->sourcemodule) switch ($game->sourcemodule) {
{ case 'quiz':
case 'quiz': case 'question':
case 'question': game_sudoku_showquestions_quiz( $id, $game, $attempt, $hiddenpicture, $offsetquestions,
game_sudoku_showquestions_quiz( $id, $game, $attempt, $hiddenpicture, $offsetquestions, $numbers, $correctquestions, $onlyshow, $showsolution, $context); $numbers, $correctquestions, $onlyshow, $showsolution, $context);
break; break;
case 'glossary': case 'glossary':
game_sudoku_showquestions_glossary( $id, $game, $attempt, $hiddenpicture, $offsetquestions, $numbers, $correctquestions, $onlyshow, $showsolution); game_sudoku_showquestions_glossary( $id, $game, $attempt, $hiddenpicture,
break; $offsetquestions, $numbers, $correctquestions, $onlyshow, $showsolution);
} break;
}
if( $game->bottomtext != ''){
echo '<br><br>'.$game->bottomtext; if ($game->bottomtext != '') {
} echo '<br><br>'.$game->bottomtext;
}
} }
function game_hidden_picture_computescore( $game, $hiddenpicture){ function game_hidden_picture_computescore( $game, $hiddenpicture) {
$correct = $hiddenpicture->correct; $correct = $hiddenpicture->correct;
if( $hiddenpicture->found){ if ($hiddenpicture->found) {
$correct++; $correct++;
} }
$remaining = $game->param1 * $game->param2 - $hiddenpicture->correct; $remaining = $game->param1 * $game->param2 - $hiddenpicture->correct;
$div2 = $correct + $hiddenpicture->wrong + $remaining; $div2 = $correct + $hiddenpicture->wrong + $remaining;
if( $hiddenpicture->found){ if ($hiddenpicture->found) {
$percent = ($correct + $remaining) / $div2; $percent = ($correct + $remaining) / $div2;
}else{ } else {
$percent = $correct / $div2; $percent = $correct / $div2;
} }
return $percent; return $percent;
} }
function game_hiddenpicture_showhiddenpicture( $id, $game, $attempt, $hiddenpicture, $showsolution, $offsetquestions, $correctquestions){ function game_hiddenpicture_showhiddenpicture( $id, $game, $attempt, $hiddenpicture, $showsolution,
global $DB; $offsetquestions, $correctquestions) {
global $DB;
$foundcells=''; $foundcells = '';
foreach( $correctquestions as $key => $val){ foreach ($correctquestions as $key => $val) {
$foundcells .= ','.$key; $foundcells .= ','.$key;
} }
$cells=''; $cells = '';
foreach( $offsetquestions as $key => $val){ foreach ($offsetquestions as $key => $val) {
if( $key != 0){ if ($key != 0) {
$cells .= ','.$key; $cells .= ','.$key;
} }
} }
$query = $DB->get_record_select( 'game_queries', "attemptid=$hiddenpicture->id AND col=0", null, 'id,glossaryentryid,attachment,questiontext'); $query = $DB->get_record_select( 'game_queries', "attemptid=$hiddenpicture->id AND col=0",
null, 'id,glossaryentryid,attachment,questiontext');
//Grade // Grade.
echo "<br/>".get_string( 'grade', 'game').' : '.round( $attempt->score * 100).' %'; echo "<br/>".get_string( 'grade', 'game').' : '.round( $attempt->score * 100).' %';
game_hiddenpicture_showquestion_glossary( $game, $id, $query); game_hiddenpicture_showquestion_glossary( $game, $id, $query);
@ -267,97 +282,97 @@ function game_hiddenpicture_showhiddenpicture( $id, $game, $attempt, $hiddenpict
game_showpicture( $id, $game, $attempt, $query, $cells, $foundcells, true); game_showpicture( $id, $game, $attempt, $query, $cells, $foundcells, true);
} }
function game_hiddenpicture_showquestion_glossary( $game, $id, $query) function game_hiddenpicture_showquestion_glossary( $game, $id, $query) {
{ global $CFG, $DB;
global $CFG, $DB;
$entry = $DB->get_record( 'glossary_entries', array( 'id' => $query->glossaryentryid)); $entry = $DB->get_record( 'glossary_entries', array( 'id' => $query->glossaryentryid));
/// Start the form // Start the form.
echo '<br>'; echo '<br>';
echo "<form id=\"responseform\" method=\"post\" action=\"{$CFG->wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n"; echo "<form id=\"responseform\" method=\"post\" ".
echo "<center><input type=\"submit\" name=\"finishattempt\" value=\"".get_string('hiddenpicture_mainsubmit', 'game')."\"></center>\n"; "action=\"{$CFG->wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n";
echo "<center><input type=\"submit\" name=\"finishattempt\" ".
"value=\"".get_string('hiddenpicture_mainsubmit', 'game')."\"></center>\n";
// Add a hidden field with the queryid // Add a hidden field with the queryid.
echo '<input type="hidden" name="id" value="' . s($id) . "\" />\n"; echo '<input type="hidden" name="id" value="' . s($id) . "\" />\n";
echo '<input type="hidden" name="action" value="hiddenpicturecheckg" />'; echo '<input type="hidden" name="action" value="hiddenpicturecheckg" />';
echo '<input type="hidden" name="queryid" value="' . $query->id . "\" />\n"; echo '<input type="hidden" name="queryid" value="' . $query->id . "\" />\n";
// Add a hidden field with glossaryentryid // Add a hidden field with glossaryentryid.
echo '<input type="hidden" name="glossaryentryid" value="'.$query->glossaryentryid."\" />\n"; echo '<input type="hidden" name="glossaryentryid" value="'.$query->glossaryentryid."\" />\n";
$temp = $game->glossaryid; $temp = $game->glossaryid;
$game->glossaryid = $game->glossaryid2; $game->glossaryid = $game->glossaryid2;
echo game_show_query( $game, $query, $entry->definition); echo game_show_query( $game, $query, $entry->definition);
$game->glossaryid = $temp; $game->glossaryid = $temp;
echo get_string( 'answer').': '; echo get_string( 'answer').': ';
echo "<input type=\"text\" name=\"answer\" size=30 /><br>"; echo "<input type=\"text\" name=\"answer\" size=30 /><br>";
echo "</form><br>\n"; echo "</form><br>\n";
} }
function game_hiddenpicture_check_questions( $id, $game, &$attempt, &$hiddenpicture, $finishattempt) function game_hiddenpicture_check_questions( $id, $game, &$attempt, &$hiddenpicture, $finishattempt) {
{
global $QTYPES, $DB; global $QTYPES, $DB;
$responses = data_submitted(); $responses = data_submitted();
$offsetquestions = game_sudoku_compute_offsetquestions( $game->sourcemodule, $attempt, $numbers, $correctquestions); $offsetquestions = game_sudoku_compute_offsetquestions( $game->sourcemodule, $attempt, $numbers, $correctquestions);
$questionlist = game_sudoku_getquestionlist( $offsetquestions); $questionlist = game_sudoku_getquestionlist( $offsetquestions);
$questions = game_sudoku_getquestions( $questionlist); $questions = game_sudoku_getquestions( $questionlist);
$actions = question_extract_responses($questions, $responses, QUESTION_EVENTSUBMIT); $actions = question_extract_responses($questions, $responses, QUESTION_EVENTSUBMIT);
$correct = $wrong = 0; $correct = $wrong = 0;
foreach($questions as $question) { foreach ($questions as $question) {
if( !array_key_exists( $question->id, $actions)){ if (!array_key_exists( $question->id, $actions)) {
//no answered // No answered.
continue; continue;
} }
unset( $state); unset( $state);
unset( $cmoptions); unset( $cmoptions);
$question->maxgrade = 100; $question->maxgrade = 100;
$state->responses = $actions[ $question->id]->responses; $state->responses = $actions[ $question->id]->responses;
$state->event = QUESTION_EVENTGRADE; $state->event = QUESTION_EVENTGRADE;
$cmoptions = array(); $cmoptions = array();
$QTYPES[$question->qtype]->grade_responses( $question, $state, $cmoptions); $QTYPES[$question->qtype]->grade_responses( $question, $state, $cmoptions);
unset( $query); unset( $query);
$select = "attemptid=$attempt->id"; $select = "attemptid=$attempt->id";
$select .= " AND questionid=$question->id"; $select .= " AND questionid=$question->id";
if( ($query->id = $DB->get_field_select( 'game_queries', 'id', $select)) == 0){ if (($query->id = $DB->get_field_select( 'game_queries', 'id', $select)) == 0) {
print_error("problem game_hiddenpicture_check_questions (select=$select)"); print_error("problem game_hiddenpicture_check_questions (select=$select)");
} }
$answertext = $state->responses[ '']; $answertext = $state->responses[ ''];
if( $answertext != ''){ if ($answertext != '') {
$grade = $state->raw_grade; $grade = $state->raw_grade;
if( $grade < 50){ if ($grade < 50) {
//wrong answer // Wrong answer.
game_update_queries( $game, $attempt, $query, $grade/100, $answertext); game_update_queries( $game, $attempt, $query, $grade / 100, $answertext);
$wrong++; $wrong++;
}else{ } else {
//correct answer // Correct answer.
game_update_queries( $game, $attempt, $query, 1, $answertext); game_update_queries( $game, $attempt, $query, 1, $answertext);
$correct++; $correct++;
} }
} }
} }
$hiddenpicture->correct += $correct; $hiddenpicture->correct += $correct;
$hiddenpicture->wrong += $wrong; $hiddenpicture->wrong += $wrong;
if( !$DB->update_record( 'game_hiddenpicture', $hiddenpicture)){ if (!$DB->update_record( 'game_hiddenpicture', $hiddenpicture)) {
print_error( 'game_hiddenpicture_check_questions: error updating in game_hiddenpicture'); print_error( 'game_hiddenpicture_check_questions: error updating in game_hiddenpicture');
} }
$attempt->score = game_hidden_picture_computescore( $game, $hiddenpicture); $attempt->score = game_hidden_picture_computescore( $game, $hiddenpicture);
if( !$DB->update_record( 'game_attempts', $attempt)){ if (!$DB->update_record( 'game_attempts', $attempt)) {
print_error( 'game_hiddenpicture_check_questions: error updating in game_attempt'); print_error( 'game_hiddenpicture_check_questions: error updating in game_attempt');
} }
@ -366,80 +381,79 @@ function game_hiddenpicture_check_questions( $id, $game, &$attempt, &$hiddenpict
return true; return true;
} }
function game_hiddenpicture_check_mainquestion( $id, $game, &$attempt, &$hiddenpicture, $finishattempt, $context) function game_hiddenpicture_check_mainquestion( $id, $game, &$attempt, &$hiddenpicture, $finishattempt, $context) {
{
global $QTYPES, $CFG, $DB; global $QTYPES, $CFG, $DB;
$responses = data_submitted(); $responses = data_submitted();
$glossaryentryid = $responses->glossaryentryid; $glossaryentryid = $responses->glossaryentryid;
$queryid = $responses->queryid; $queryid = $responses->queryid;
// Load the glossary entry // Load the glossary entry.
if (!($entry = $DB->get_record( 'glossary_entries', array( 'id' => $glossaryentryid)))) { if (!($entry = $DB->get_record( 'glossary_entries', array( 'id' => $glossaryentryid)))) {
print_error( get_string( 'noglossaryentriesfound', 'game')); print_error( get_string( 'noglossaryentriesfound', 'game'));
} }
$answer = $responses->answer; $answer = $responses->answer;
$correct = false; $correct = false;
if( $answer != ''){ if ($answer != '') {
if( game_upper( $entry->concept) == game_upper( $answer)){ if (game_upper( $entry->concept) == game_upper( $answer)) {
$correct = true; $correct = true;
} }
} }
// Load the query // Load the query.
if (!($query = $DB->get_record( 'game_queries', array( 'id' => $queryid)))) { if (!($query = $DB->get_record( 'game_queries', array( 'id' => $queryid)))) {
print_error( "The query $queryid not found"); print_error( "The query $queryid not found");
} }
game_update_queries( $game, $attempt, $query, $correct, $answer); game_update_queries( $game, $attempt, $query, $correct, $answer);
if( $correct){ if ($correct) {
$hiddenpicture->found = 1; $hiddenpicture->found = 1;
}else{ } else {
$hiddenpicture->wrong++; $hiddenpicture->wrong++;
} }
if( !$DB->update_record( 'game_hiddenpicture', $hiddenpicture)){ if (!$DB->update_record( 'game_hiddenpicture', $hiddenpicture)) {
print_error( 'game_hiddenpicture_check_mainquestion: error updating in game_hiddenpicture'); print_error( 'game_hiddenpicture_check_mainquestion: error updating in game_hiddenpicture');
} }
$score = game_hidden_picture_computescore( $game, $hiddenpicture); $score = game_hidden_picture_computescore( $game, $hiddenpicture);
game_updateattempts( $game, $attempt, $score, $correct); game_updateattempts( $game, $attempt, $score, $correct);
if( $correct == false){ if ($correct == false) {
game_hiddenpicture_play( $id, $game, $attempt, $hiddenpicture, false, $context); game_hiddenpicture_play( $id, $game, $attempt, $hiddenpicture, false, $context);
return true; return true;
} }
//Finish the game // Finish the game.
$query = $DB->get_record_select( 'game_queries', "attemptid=$hiddenpicture->id AND col=0", null, 'id,glossaryentryid,attachment,questiontext'); $query = $DB->get_record_select( 'game_queries', "attemptid=$hiddenpicture->id AND col=0",
null, 'id,glossaryentryid,attachment,questiontext');
game_showpicture( $id, $game, $attempt, $query, '', '', false); game_showpicture( $id, $game, $attempt, $query, '', '', false);
echo '<p><BR/><font size="5" color="green">'.get_string( 'win', 'game').'</font><BR/><BR/></p>'; echo '<p><br/><font size="5" color="green">'.get_string( 'win', 'game').'</font><BR/><BR/></p>';
global $CFG; global $CFG;
echo '<br/>'; echo '<br/>';
echo "<a href=\"$CFG->wwwroot/mod/game/attempt.php?id=$id\">"; echo "<a href=\"$CFG->wwwroot/mod/game/attempt.php?id=$id\">";
echo get_string( 'nextgame', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp;'; echo get_string( 'nextgame', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp;';
if (! $cm = $DB->get_record( 'course_modules', array( 'id' => $id))) { if (! $cm = $DB->get_record( 'course_modules', array( 'id' => $id))) {
print_error( "Course Module ID was incorrect id=$id"); print_error( "Course Module ID was incorrect id=$id");
} }
echo "<a href=\"{$CFG->wwwroot}/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'</a> '; echo "<a href=\"{$CFG->wwwroot}/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'</a> ';
return false; return false;
} }
function game_showpicture( $id, $game, $attempt, $query, $cells, $foundcells, $usemap) function game_showpicture( $id, $game, $attempt, $query, $cells, $foundcells, $usemap) {
{
global $CFG; global $CFG;
$filenamenumbers = str_replace( "\\", '/', $CFG->dirroot)."/mod/game/hiddenpicture/numbers.png"; $filenamenumbers = str_replace( "\\", '/', $CFG->dirroot)."/mod/game/hiddenpicture/numbers.png";
if( $usemap){ if ($usemap) {
$cols = $game->param1; $cols = $game->param1;
$rows = $game->param2; $rows = $game->param2;
}else{ } else {
$cols = $rows = 0; $cols = $rows = 0;
} }
$params = "id=$id&id2=$attempt->id&f=$foundcells&cols=$cols&rows=$rows&cells=$cells&p={$query->attachment}&n=$filenamenumbers"; $params = "id=$id&id2=$attempt->id&f=$foundcells&cols=$cols&rows=$rows&cells=$cells&p={$query->attachment}&n=$filenamenumbers";
@ -448,29 +462,28 @@ function game_showpicture( $id, $game, $attempt, $query, $cells, $foundcells, $u
$fs = get_file_storage(); $fs = get_file_storage();
$file = get_file_storage()->get_file_by_hash( $query->attachment); $file = get_file_storage()->get_file_by_hash( $query->attachment);
$image = $file->get_imageinfo(); $image = $file->get_imageinfo();
if( $game->param4 > 10){ if ($game->param4 > 10) {
$width = $game->param4; $width = $game->param4;
$height = $image[ 'height'] * $width / $image[ 'width']; $height = $image[ 'height'] * $width / $image[ 'width'];
}else if( $game->param5 > 10){ } else if ( $game->param5 > 10) {
$height = $game->param5; $height = $game->param5;
$width = $image[ 'width'] * $height / $image[ 'height']; $width = $image[ 'width'] * $height / $image[ 'height'];
}else } else {
{
$width = $image[ 'width']; $width = $image[ 'width'];
$height = $image[ 'height']; $height = $image[ 'height'];
} }
echo "<IMG SRC=\"$imagesrc\" width=$width "; echo "<IMG SRC=\"$imagesrc\" width=$width ";
if( $usemap){ if ($usemap) {
echo " USEMAP=\"#mapname\" "; echo " USEMAP=\"#mapname\" ";
} }
echo " BORDER=\"1\">\r\n"; echo " BORDER=\"1\">\r\n";
if( $usemap){ if ($usemap) {
echo "<MAP NAME=\"mapname\">\r\n"; echo "<MAP NAME=\"mapname\">\r\n";
$pos=0; $pos = 0;
for($row=0; $row < $rows; $row++){ for ($row = 0; $row < $rows; $row++) {
for( $col=0; $col < $cols; $col++){ for ($col = 0; $col < $cols; $col++) {
$pos++; $pos++;
$x1 = $col * $width / $cols; $x1 = $col * $width / $cols;
$y1 = $row * $height / $rows; $y1 = $row * $height / $rows;
@ -483,4 +496,3 @@ function game_showpicture( $id, $game, $attempt, $query, $cells, $foundcells, $u
echo "</MAP>"; echo "</MAP>";
} }
} }

12
lib.php

@ -868,7 +868,7 @@ function game_get_types() {
$type->typestr = '--'.get_string( 'modulenameplural', 'game'); $type->typestr = '--'.get_string( 'modulenameplural', 'game');
$types[] = $type; $types[] = $type;
$hide = ( isset( $config->hidehangman)) ? ($config->hidehangman != 0) : false); $hide = ( isset( $config->hidehangman) ? ($config->hidehangman != 0) : false);
if ($hide == false) { if ($hide == false) {
$type = new object(); $type = new object();
@ -906,7 +906,7 @@ function game_get_types() {
$types[] = $type; $types[] = $type;
} }
$hide = (isset( $config->hidemillionaire)) ? ($config->hidemillionaire != 0) : false); $hide = (isset( $config->hidemillionaire) ? ($config->hidemillionaire != 0) : false);
if ($hide == false) { if ($hide == false) {
$type = new object(); $type = new object();
$type->modclass = MOD_CLASS_ACTIVITY; $type->modclass = MOD_CLASS_ACTIVITY;
@ -915,7 +915,7 @@ function game_get_types() {
$types[] = $type; $types[] = $type;
} }
$hide = (isset( $config->hidesudoku)) ? ($config->hidesudoku != 0) : false); $hide = (isset( $config->hidesudoku) ? ($config->hidesudoku != 0) : false);
if ($hide == false) { if ($hide == false) {
$type = new object(); $type = new object();
$type->modclass = MOD_CLASS_ACTIVITY; $type->modclass = MOD_CLASS_ACTIVITY;
@ -924,7 +924,7 @@ function game_get_types() {
$types[] = $type; $types[] = $type;
} }
$hide = (isset( $config->hidesnakes)) ? ($config->hidesnakes != 0) : false); $hide = (isset( $config->hidesnakes) ? ($config->hidesnakes != 0) : false);
if ($hide == false) { if ($hide == false) {
$type = new object(); $type = new object();
$type->modclass = MOD_CLASS_ACTIVITY; $type->modclass = MOD_CLASS_ACTIVITY;
@ -933,7 +933,7 @@ function game_get_types() {
$types[] = $type; $types[] = $type;
} }
$hide = (isset( $config->hidehiddenpicture)) ? ($config->hidehiddenpicture != 0) : false); $hide = (isset( $config->hidehiddenpicture) ? ($config->hidehiddenpicture != 0) : false);
if ($hide == false) { if ($hide == false) {
$type = new object(); $type = new object();
$type->modclass = MOD_CLASS_ACTIVITY; $type->modclass = MOD_CLASS_ACTIVITY;
@ -942,7 +942,7 @@ function game_get_types() {
$types[] = $type; $types[] = $type;
} }
$hide = (isset( $config->hidebookquiz)) ? ($config->hidebookquiz != 0) : false); $hide = (isset( $config->hidebookquiz) ? ($config->hidebookquiz != 0) : false);
if ($hide == false) { if ($hide == false) {
if ($DB->get_record( 'modules', array( 'name' => 'book'), 'id,id')) { if ($DB->get_record( 'modules', array( 'name' => 'book'), 'id,id')) {
$type = new object(); $type = new object();

Loading…
Cancel
Save