diff --git a/classes/event/course_module_instance_list_viewed.php b/classes/event/course_module_instance_list_viewed.php index 338c6b6..3ba2ef7 100644 --- a/classes/event/course_module_instance_list_viewed.php +++ b/classes/event/course_module_instance_list_viewed.php @@ -1,4 +1,18 @@ . // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . @@ -13,7 +27,7 @@ namespace mod_game\event; defined('MOODLE_INTERNAL') || die(); -/** +/* * The mod_game instance list viewed event class. * * @package mod_game @@ -32,8 +46,8 @@ class course_module_instance_list_viewed extends \core\event\course_module_insta $params = array( 'context' => \context_course::instance($course->id) ); - $event = \mod_game\event\course_module_instance_list_viewed::create($params); + $event = \mod_game\event\course_module_instance_list_viewed::create( $params); $event->add_record_snapshot('course', $course); return $event; - }} - + } +} diff --git a/classes/event/course_module_viewed.php b/classes/event/course_module_viewed.php index 41b27c5..94dc544 100644 --- a/classes/event/course_module_viewed.php +++ b/classes/event/course_module_viewed.php @@ -1,4 +1,18 @@ . // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . @@ -36,7 +50,6 @@ class course_module_viewed extends \core\event\course_module_viewed { 'context' => $context, 'objectid' => $game->id ); - /** @var course_module_viewed $event */ $event = self::create($data); $event->add_record_snapshot('game', $game); return $event; @@ -47,7 +60,6 @@ class course_module_viewed extends \core\event\course_module_viewed { 'context' => $context, 'objectid' => $game->id ); - /** @var course_module_viewed $event */ $event = self::create($data); $event->add_record_snapshot('game', $game); return $event; diff --git a/classes/event/game_played.php b/classes/event/game_played.php index 7945147..b201dfe 100644 --- a/classes/event/game_played.php +++ b/classes/event/game_played.php @@ -1,4 +1,18 @@ . // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . @@ -38,7 +52,6 @@ class game_played extends \core\event\base { 'context' => $context, 'objectid' => $game->id ); - /** @var chapter_viewed $event */ $event = self::create($data); $event->add_record_snapshot('game', $game); return $event; @@ -50,7 +63,8 @@ class game_played extends \core\event\base { * @return array|null */ protected function get_legacy_logdata() { - return array($this->courseid, 'game', 'view', 'view.php?id=' . $this->contextinstanceid, $this->objectid, $this->contextinstanceid); + return array($this->courseid, 'game', 'view', 'view.php?id=' . $this->contextinstanceid, + $this->objectid, $this->contextinstanceid); } /** diff --git a/classes/plugininfo/booktool.php b/classes/plugininfo/booktool.php index 5981409..687daca 100644 --- a/classes/plugininfo/booktool.php +++ b/classes/plugininfo/booktool.php @@ -1,4 +1,21 @@ . +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + /** * Subplugin info class. * diff --git a/classes/plugininfo/gametool.php b/classes/plugininfo/gametool.php index 5981409..687daca 100644 --- a/classes/plugininfo/gametool.php +++ b/classes/plugininfo/gametool.php @@ -1,4 +1,21 @@ . +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + /** * Subplugin info class. * diff --git a/cross/cross_class.php b/cross/cross_class.php index c41d595..e556ef7 100755 --- a/cross/cross_class.php +++ b/cross/cross_class.php @@ -1,4 +1,20 @@ . +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . /* Crossing Words for @@ -6,749 +22,719 @@ Codewalkers PHP Coding Contest of July 2002 (http://codewalkers.com/php-contest.php) Author Àngel Fenoy from Arenys de Mar, Barcelona. -*/ +*/ class Cross { - var $m_input_answers; //contains the words and the answers - var $m_words; //the words that will be used - - var $m_time_limit = 30; - - //computed by computenextcross - var $m_best_cross_pos; //the best puzzle - var $m_best_cross_dir; //the best puzzle - var $m_best_cross_word; //the best puzzle - var $m_best_puzzle; - - var $m_bests; //best score as a phrase - var $m_best_score; //the best score - - var $m_best_connectors; - var $m_best_filleds; - var $m_best_spaces; - var $m_best_N20; - - //computepuzzleinfo - var $m_mincol; //computed by ComputePuzzleInfo - var $m_maxcol; //computed by ComputePuzzleInfo - var $m_minrow; //computed by ComputePuzzleInfo - var $m_maxrow; //computed by ComputePuzzleInfo - var $m_cLetter; //computed by ComputePuzzleInfo - var $m_reps; //repetition of each word - var $m_average_reps;//average of repetitions - - function setwords( $answers, $maxcols, $reps) - { - $this->m_reps = array(); - foreach( $reps as $word => $r){ - $this->m_reps[ game_upper( $word)] = $r; - } - - $this->m_average_reps=0; - foreach( $reps as $r) - $this->m_average_reps += $r; - if( count( $reps)) - $this->m_average_reps /= count( $reps); - - $this->m_input_answers = array(); - foreach( $answers as $word => $answer){ - $this->m_input_answers[ game_upper( $word)] = $answer; - } - - $this->m_words = array(); - - $maxlen = 0; - foreach( $this->m_input_answers as $word => $answer) - { - $len = game_strlen( $word); - if( $len > $maxlen){ - $maxlen = $len; - } - } - - $N20 = $maxlen; - if( $N20 < 15) - $N20 = 15; - - $this->m_N20min = round( $N20 - $N20/4); - $this->m_N20max = round( $N20 + $N20/4); - if( $this->m_N20max > $maxcols and $maxcols > 0){ - $this->m_N20max = $maxcols; - } - if( $this->m_N20min > $this->m_N20max){ - $this->m_N20min = $this->m_N20max; - } - - $this->m_words = array(); - foreach( $this->m_input_answers as $word => $answer) - { - $len = game_strlen( $word); - - if( $len <= $this->m_N20max){ - $this->m_words[] = game_upper( $word); - } - } - - $this->randomize(); - - return count( $this->m_words); + public $minputanswers; // Contains the words and the answers. + public $mwords; // The words that will be used. + + public $mtimelimit = 30; + + // Computed by computenextcross. + public $mbestcrosspos; // The best puzzle. + public $mbestcrossdir; // The best puzzle. + public $mbestcrossword; // The best puzzle. + public $mbestpuzzle; + + public $mbests; // The best score as a phrase. + public $mbestscore; // The best score. + + public $mbestconnectors; + public $mbestfilleds; + public $mbestspaces; + public $mbestn20; + + // Computepuzzleinfo. + public $mmincol; // Computed by ComputePuzzleInfo. + public $mmaxcol; // Computed by ComputePuzzleInfo. + public $mminrow; // Computed by ComputePuzzleInfo. + public $mmaxrow; // Computed by ComputePuzzleInfo. + public $mcletter; // Computed by ComputePuzzleInfo. + public $mreps; // Repetition of each word. + public $maveragereps; // Average of repetitions. + + public function setwords( $answers, $maxcols, $reps) { + $this->mreps = array(); + foreach ($reps as $word => $r) { + $this->mreps[ game_upper( $word)] = $r; + } + + $this->maveragereps = 0; + foreach ($reps as $r) { + $this->maveragereps += $r; + } + if (count( $reps)) { + $this->maveragereps /= count( $reps); + } + + $this->minputanswers = array(); + foreach ($answers as $word => $answer) { + $this->minputanswers[ game_upper( $word)] = $answer; + } + + $this->mwords = array(); + + $maxlen = 0; + foreach ($this->minputanswers as $word => $answer) { + $len = game_strlen( $word); + if ($len > $maxlen) { + $maxlen = $len; + } + } + + $n20 = $maxlen; + if ($n20 < 15) { + $n20 = 15; + } + + $this->mn20min = round( $n20 - $n20 / 4); + $this->mn20max = round( $n20 + $n20 / 4); + if ( $this->mn20max > $maxcols and $maxcols > 0) { + $this->mn20max = $maxcols; + } + if ($this->mn20min > $this->mn20max) { + $this->mn20min = $this->mn20max; + } + + $this->m_words = array(); + foreach ($this->m_input_answers as $word => $answer) { + $len = game_strlen( $word); + + if ($len <= $this->mn20max) { + $this->mwords[] = game_upper( $word); + } + } + + $this->randomize(); + + return count( $this->mwords); } - - function randomize() - { - $n = count( $this->m_words); - for($j=0; $j <= $n/4; $j++) - { - $i = array_rand( $this->m_words); - - $this->swap( $this->m_words[ $i], $this->m_words[ 0]); - } - } - - function computedata( &$crossm, &$crossd, &$letters, $minwords, $maxwords) - { + + public function randomize() { + $n = count( $this->mwords); + for ($j = 0; $j <= $n / 4; $j++) { + $i = array_rand( $this->mwords); + + $this->swap( $this->mwords[ $i], $this->mwords[ 0]); + } + } + + public function computedata( &$crossm, &$crossd, &$letters, $minwords, $maxwords) { $t1 = time(); - + $ctries = 0; - $m_best_score = 0; + $mbestscore = 0; + + $mbestconnectors = $mbestfilleds = $mbestspaces = 0; + $mbestn20 = 0; - $m_best_connectors = $m_best_filleds = $m_best_spaces = 0; - $m_best_N20 = 0; - $nochange = 0; - for(;;) - { - //selects the size of the cross - $N20 = mt_rand( $this->m_N20min, $this->m_N20max); - - if( !$this->computenextcross( $N20, $t1, $ctries, $minwords, $maxwords, $nochange)) + for (;;) { + // Selects the size of the cross. + $n20 = mt_rand( $this->mn20min, $this->mn20max); + + if (!$this->computenextcross( $n20, $t1, $ctries, $minwords, $maxwords, $nochange)) { break; - + } + $ctries++; - if (time() - $t1 > $this->m_time_limit - 3){ + if (time() - $t1 > $this->mtimelimit - 3) { break; } - if( $nochange > 10) + if ($nochange > 10) { break; + } } - $this->computepuzzleinfo( $this->m_best_N20, $this->m_best_cross_pos, $this->m_best_cross_dir, $this->m_best_cross_word, false); - - //set_time_limit( 30); - - return $this->savepuzzle( $crossm, $crossd, $ctries, time()-$t1); + $this->computepuzzleinfo( $this->mbestn20, $this->mbestcrosspos, $this->mbestcrossdir, $this->mbestcrossword, false); + + return $this->savepuzzle( $crossm, $crossd, $ctries, time() - $t1); } - - function computenextcross( $N20, $t1, $ctries, $minwords, $maxwords, &$nochange) - { - $MAXW = $N20; - - $N21 = $N20 + 1; - $N22 = $N20 + 2; - $N2222 = $N22 * $N22; - - $base_puzzle = str_repeat('0', $N22) . - str_repeat('0' . str_repeat('.', $N20) . '0', $N20) . - str_repeat('0', $N22); - - $cross_pos = array(); - $cross_dir = array(); - $cross_word = array(); - - $magics = array(); - for ($n = 2; $n < $N21; $n++) - { - $a = array(); - for ($r = 2; $r < ($n + 2); $r++) - $a[] = $r; - - uasort($a, array( $this, 'cmp_magic')); - $magics[ $n] = $a; - } - - uasort($this->m_words, array( $this, 'cmp')); - - $words = ';' . implode(';', $this->m_words) . ';'; - - $puzzle = $base_puzzle; - - $row = mt_rand(3, max( 3, $N20-3)); - $col = mt_rand(3, max( 3, $N20-3)); - $pos = $N22 * $row + $col; - - $poss = array(); - $ret = $this->scan_pos($pos, 'h', true, $puzzle, $words, $magics, $poss, $cross_pos, $cross_dir, $cross_word, $N20); - - while ($s = sizeof($poss)) - { - $p = array_shift($poss); - - if ($this->scan_pos($p[0], $p[1], false, $puzzle, $words, $magics, $poss, $cross_pos, $cross_dir, $cross_word, $N20)){ - $n_words = count( $cross_word); - if( $maxwords) - { - if( $n_words >= $maxwords){ - break; - } - } - } - if (time() - $t1 > $this->m_time_limit - 3){ - return false; - } - } - - $n_words = count( $cross_word); - if( $minwords) - { - if( $n_words < $minwords) - return true; + + public function computenextcross( $n20, $t1, $ctries, $minwords, $maxwords, &$nochange) { + $maxw = $n20; + + $n21 = $n20 + 1; + $n22 = $n20 + 2; + $n2222 = $n22 * $n22; + + $basepuzzle = str_repeat('0', $n22) . + str_repeat('0' . str_repeat('.', $n20) . '0', $n20) . + str_repeat('0', $n22); + + $crosspos = array(); + $crossdir = array(); + $crossword = array(); + + $magics = array(); + for ($n = 2; $n < $n21; $n++) { + $a = array(); + for ($r = 2; $r < ($n + 2); $r++) { + $a[] = $r; + } + + uasort($a, array( $this, 'cmp_magic')); + $magics[ $n] = $a; + } + + uasort($this->mwords, array( $this, 'cmp')); + + $words = ';' . implode(';', $this->mwords) . ';'; + + $puzzle = $basepuzzle; + + $row = mt_rand(3, max( 3, $n20 - 3)); + $col = mt_rand(3, max( 3, $n20 - 3)); + $pos = $n22 * $row + $col; + + $poss = array(); + $ret = $this->scan_pos($pos, 'h', true, $puzzle, $words, $magics, $poss, $crosspos, $crossdir, $crossword, $n20); + + while ($s = count($poss)) { + $p = array_shift($poss); + + if ($this->scan_pos($p[0], $p[1], false, $puzzle, $words, $magics, $poss, $crosspos, $crossdir, $crossword, $n20)) { + $nwords = count( $crossword); + if ($maxwords) { + if ($nwords >= $maxwords) { + break; + } + } + } + if (time() - $t1 > $this->mtimelimit - 3) { + return false; + } + } + + $nwords = count( $crossword); + if ($minwords) { + if ($nwords < $minwords) { + return true; + } + } + + $score = $this->computescore( $puzzle, $n20, $n22, $n2222, $nwords, $nconnectors, $nfilleds, $cspaces, $crossword); + + if ($score > $this->mbestscore) { + $this->mbestcrosspos = $crosspos; + $this->m_best_cross_dir = $crossdir; + $this->mbestcrossword = $crossword; + $this->mbestpuzzle = $puzzle; + + $this->mbests = array('Words' => "$nwords * 5 = " . ($nwords * 5), + 'Connectors' => "$nconnectors * 3 = " . ($nconnectors * 3), + 'Filled in spaces' => $nfilleds, + "N20" => $n20 + ); + + $this->mbestscore = $score; + + $this->mbestconnectors = $nconnectors; + $this->mbestfilleds = $nfilleds; + $this->mbestspaces = $cspaces; + $this->mbestn20 = $n20; + $nochange = 0; + } else { + $nochange++; + } + + return true; } - - $score = $this->computescore( $puzzle, $N20, $N22, $N2222, $n_words, $n_connectors, $n_filleds, $cSpaces, $cross_word); - - if ($score > $this->m_best_score) - { - $this->m_best_cross_pos = $cross_pos; - $this->m_best_cross_dir = $cross_dir; - $this->m_best_cross_word = $cross_word; - $this->m_best_puzzle = $puzzle; - - $this->m_bests = array('Words' => "$n_words * 5 = " . ($n_words * 5), - 'Connectors' => "$n_connectors * 3 = " . ($n_connectors * 3), - 'Filled in spaces' => $n_filleds, - "N20" => $N20 - ); - - $this->m_best_score = $score; - - $this->m_best_connectors = $n_connectors; - $this->m_best_filleds = $n_filleds; - $this->m_best_spaces = $cSpaces; - $this->m_best_N20 = $N20; - $nochange = 0; - }else - { - $nochange++; - } - - return true; -} - function computescore( $puzzle, $N20, $N22, $N2222, $n_words, &$n_connectors, &$n_filleds, &$cSpaces, $cross_word) - { - $n_connectors = $n_filleds = 0; - $puzzle00 = str_replace('.', '0', $puzzle); - - $used=0; - for ($n = 0; $n < $N2222; $n++) - { - if ($puzzle00[$n]){ - $used ++; - - if (($puzzle00[$n - 1] or $puzzle00[$n + 1]) and ($puzzle00[$n - $N22] or $puzzle00[$n + $N22])){ - $n_connectors++; - } else{ - $n_filleds++; + public function computescore( $puzzle, $n20, $n22, $n2222, $nwords, &$nconnectors, &$nfilleds, &$cspaces, $crossword) { + $nconnectors = $nfilleds = 0; + $puzzle00 = str_replace('.', '0', $puzzle); + + $used = 0; + for ($n = 0; $n < $n2222; $n++) { + if ($puzzle00[$n]) { + $used ++; + + if (($puzzle00[$n - 1] or $puzzle00[$n + 1]) and ($puzzle00[$n - $n22] or $puzzle00[$n + $n22])) { + $nconnectors++; + } else { + $nfilleds++; } - } + } } - $cSpaces = substr_count( $puzzle, "."); - $score = ($n_words * 5) + ($n_connectors * 3) + $n_filleds; + $cspaces = substr_count( $puzzle, "."); + $score = ($nwords * 5) + ($nconnectors * 3) + $nfilleds; - $sum_rep = 0; - foreach( $cross_word as $word){ - $word = game_substr( $word, 1, -1); + $sumrep = 0; + foreach ($crossword as $word) { + $word = game_substr( $word, 1, -1); - if( array_key_exists( $word, $this->m_reps)) - $sum_rep += $this->m_reps[ $word] - $this->m_average_reps; + if (array_key_exists( $word, $this->mreps)) { + $sumrep += $this->mreps[ $word] - $this->maveragereps; + } } - - return $score-10*$sum_rep; + + return $score - 10 * $sumrep; } - - function computepuzzleinfo( $N20, $cross_pos, $cross_dir, $cross_word, $bPrint=false) - { - $bPrint=false; - $N22 = $N20 + 2; - - $this->m_mincol = $N22; - $this->m_maxcol = 0; - $this->m_minrow = $N22; - $this->m_maxrow = 0; - $this->m_cletter = 0; - - if( count( $cross_word) == 0){ - return; - } - - if( $bPrint) - echo "

PuzzleInfo N20=$N20 words=".count($cross_word)."
"; - for($i = 0; $i < count($cross_pos); $i++) - { - $pos = $cross_pos[ $i]; - $col = $pos % $N22; - $row = floor( $pos / $N22); - $dir = $cross_dir[ $i]; - - $len = game_strlen($cross_word[ $i])-3; - - if( $bPrint) - echo "col=$col row=$row dir=$dir word=".$cross_word[ $i]."
"; - - $this->m_cletter += $len; - - if( $col < $this->m_mincol) - $this->m_mincol = $col; - - if( $row < $this->m_minrow) - $this->m_minrow = $row; - - if( $dir == 'h') - $col += $len; - else - $row += $len; - - if( $col > $this->m_maxcol) - $this->m_maxcol = $col; - if( $row > $this->m_maxrow) - $this->m_maxrow = $row; - } - - if( $bPrint) - echo "mincol={$this->m_mincol} maxcol={$this->m_maxcol} minrow={$this->m_minrow} maxrow={$this->m_maxrow}
"; - - if( $this->m_mincol > $this->m_maxcol) - $this->m_mincol = $this->m_maxcol; - if( $this->m_minrow > $this->m_maxrow) - $this->m_minrow = $this->m_maxrow; - } - - - function savepuzzle( &$crossm, &$crossd, $ctries, $time) - { - $N22 = $this->m_best_N20 + 2; - - $cols = $this->m_maxcol - $this->m_mincol + 1; - $rows = $this->m_maxrow - $this->m_minrow + 1; - - //if( $cols < $rows) - // $bSwapColRow = 1; - //else - $bSwapColRow = 0; - - if( $bSwapColRow) - { - Swap( $cols, $rows); - Swap( $this->m_mincol, $this->m_minrow); - Swap( $this->m_maxcol, $this->m_maxrow); - } - - $crossm = new stdClass(); - $crossm->datebegin = time(); - $crossm->time = $time; - $crossm->cols = $cols; - $crossm->rows = $rows; - $crossm->words = count( $this->m_best_cross_pos); - $crossm->wordsall = count( $this->m_input_answers); - - $crossm->createscore = $this->m_best_score; - $crossm->createtries = $ctries; - $crossm->createtimelimit = $this->m_time_limit; - $crossm->createconnectors = $this->m_best_connectors; - $crossm->createfilleds = $this->m_best_filleds; - $crossm->createspaces = $this->m_best_spaces; - - for($i = 0; $i < count($this->m_best_cross_pos); $i++) - { - $pos = $this->m_best_cross_pos[ $i]; - - $col = $pos % $N22; - $row = floor( ($pos-$col) / $N22); - - $col += - $this->m_mincol + 1; - $row += - $this->m_minrow + 1; - - $dir = $this->m_best_cross_dir[ $i]; - $word = $this->m_best_cross_word[ $i]; - $word = substr( $word, 1, strlen( $word)-2); - - $rec = new stdClass(); - - $rec->col = $col; - $rec->row = $row; - $rec->horizontal = ($dir == "h" ? 1 : 0); - - $rec->answertext = $word; - $rec->questiontext = $this->m_input_answers[ $word]; - - if( $rec->horizontal) - $key = sprintf( 'h%10d %10d', $rec->row, $rec->col); - else - $key = sprintf( 'v%10d %10d', $rec->col, $rec->row); - - $crossd[ $key] = $rec; - } - if( count( $crossd) > 1){ - ksort( $crossd); - } - - return (count( $crossd) > 0); + public function computepuzzleinfo( $n20, $crosspos, $crossdir, $crossword, $bprint=false) { + $bprint = false; + $n22 = $n20 + 2; + + $this->mmincol = $n22; + $this->mmaxcol = 0; + $this->mminrow = $n22; + $this->mmaxrow = 0; + $this->mcletter = 0; + + if (count( $crossword) == 0) { + return; + } + + if ($bprint) { + echo "

PuzzleInfo n20=$n20 words=".count( $crossword)."
"; + } + for ($i = 0; $i < count($crosspos); $i++) { + $pos = $crosspos[ $i]; + $col = $pos % $n22; + $row = floor( $pos / $n22); + $dir = $crossdir[ $i]; + + $len = game_strlen( $crossword[ $i]) - 3; + + if ($bprint) { + echo "col=$col row=$row dir=$dir word=".$crossword[ $i]."
"; + } + + $this->mcletter += $len; + + if ($col < $this->mmincol) { + $this->mmincol = $col; + } + + if ($row < $this->mminrow) { + $this->mminrow = $row; + } + + if ($dir == 'h') { + $col += $len; + } else { + $row += $len; + } + + if ($col > $this->mmaxcol) { + $this->mmaxcol = $col; + } + if ($row > $this->mmaxrow) { + $this->mmaxrow = $row; + } + } + + if ($bprint) { + echo "mincol={$this->m_mincol} maxcol={$this->m_maxcol} minrow={$this->m_minrow} maxrow={$this->m_maxrow}
"; + } + + if ($this->mmincol > $this->mmaxcol) { + $this->mmincol = $this->mmaxcol; + } + if ($this->mminrow > $this->mmaxrow) { + $this->mminrow = $this->mmaxrow; + } } - function swap( &$a, &$b) - { - $temp = $a; - $a = $b; - $b = $temp; - } - - function displaycross($puzzle, $N20) - { - $N21 = $N20 + 1; - $N22 = $N20 + 2; - $N2222 = $N22 * $N22; - $N2221 = $N2222 - 1; - $N2200 = $N2222 - $N22; - - $ret = ""; - for ($n = 0;; $n ++) { - $c = game_substr( $puzzle, $n, 1); - - if (($m = $n % $N22) == 0 or $m == $N21 or $n < $N22 or $n > $N2200) { - $ret .= ""; - } elseif ( $c == '0') { - $ret .= ""; - } elseif ($c == '.') { - $ret .= ""; - } else { - if ((game_substr( $puzzle, $n - 1, 1) > '0' or - game_substr( $puzzle, $n + 1, 1) > '0') and - (game_substr( $puzzle, $n - $N22, 1) > '0' - or game_substr( $puzzle, $n + $N22, 1) > '0')) { - $ret .= ""; - } else { - $ret .= ""; - } - } - - if ($n == $N2221) { - return "$ret
$c$c
"; - } elseif ($m == $N21) { - $ret .= ""; - } + public function savepuzzle( &$crossm, &$crossd, $ctries, $time) { + $n22 = $this->mbestn20 + 2; + + $cols = $this->mmaxcol - $this->mmincol + 1; + $rows = $this->mmaxrow - $this->mminrow + 1; + + $bswapcolrow = false; + + if ($bswapcolrow) { + swap( $cols, $rows); + swap( $this->mmincol, $this->mminrow); + swap( $this->mmaxcol, $this->mmaxrow); + } + + $crossm = new stdClass(); + $crossm->datebegin = time(); + $crossm->time = $time; + $crossm->cols = $cols; + $crossm->rows = $rows; + $crossm->words = count( $this->mbestcrosspos); + $crossm->wordsall = count( $this->minputanswers); + + $crossm->createscore = $this->mbestscore; + $crossm->createtries = $ctries; + $crossm->createtimelimit = $this->mtimelimit; + $crossm->createconnectors = $this->mbestconnectors; + $crossm->createfilleds = $this->mbestfilleds; + $crossm->createspaces = $this->mbestspaces; + + for ($i = 0; $i < count($this->mbestcrosspos); $i++) { + $pos = $this->mbestcrosspos[ $i]; + + $col = $pos % $n22; + $row = floor( ($pos - $col) / $n22); + + $col += -$this->mmincol + 1; + $row += -$this->mminrow + 1; + + $dir = $this->mbestcrossdir[ $i]; + $word = $this->mbestcrossword[ $i]; + $word = substr( $word, 1, strlen( $word) - 2); + + $rec = new stdClass(); + + $rec->col = $col; + $rec->row = $row; + $rec->horizontal = ($dir == "h" ? 1 : 0); + + $rec->answertext = $word; + $rec->questiontext = $this->minputanswers[ $word]; + + if ($rec->horizontal) { + $key = sprintf( 'h%10d %10d', $rec->row, $rec->col); + } else { + $key = sprintf( 'v%10d %10d', $rec->col, $rec->row); + } + + $crossd[ $key] = $rec; + } + if (count( $crossd) > 1) { + ksort( $crossd); + } + + return (count( $crossd) > 0); + } + + public function swap( &$a, &$b) { + $temp = $a; + $a = $b; + $b = $temp; + } + + public function displaycross($puzzle, $n20) { + $n21 = $n20 + 1; + $n22 = $n20 + 2; + $n2222 = $n22 * $n22; + $n2221 = $n2222 - 1; + $n2200 = $n2222 - $n22; + + $ret = ""; + for ($n = 0;; $n ++) { + $c = game_substr( $puzzle, $n, 1); + + if (($m = $n % $n22) == 0 or $m == $n21 or $n < $n22 or $n > $n2200) { + $ret .= ""; + } else if ( $c == '0') { + $ret .= ""; + } else if ($c == '.') { + $ret .= ""; + } else { + if ((game_substr( $puzzle, $n - 1, 1) > '0' or + game_substr( $puzzle, $n + 1, 1) > '0') and + (game_substr( $puzzle, $n - $n22, 1) > '0' + or game_substr( $puzzle, $n + $n22, 1) > '0')) { + $ret .= ""; + } else { + $ret .= ""; + } + } + + if ($n == $n2221) { + return "$ret
$c$c
"; + } else if ($m == $n21) { + $ret .= ""; + } } return $ret.''; } + public function scan_pos($pos, $dir, $valblanc, &$puzzle, &$words, &$magics, + &$poss, &$crosspos, &$crossdir, &$crossword, $n20) { + + $maxw = $n20; + + $n22 = $n20 + 2; + $n2222 = $n22 * $n22; - function scan_pos($pos, $dir, $val_blanc, &$puzzle, &$words, &$magics, &$poss, &$cross_pos, &$cross_dir, &$cross_word, $N20) - { - $MAXW = $N20; - - $N22 = $N20 + 2; - $N2222 = $N22 * $N22; - - if ($dir == 'h'){ - $inc = 1; - if ($pos + $inc >= $N2222){ - return false; - } - $oinc = $N22; - $new_dir = 'v'; - }else - { - $inc = $N22; - if ($pos + $inc >= $N2222){ - return false; - } - $oinc = 1; - $new_dir = 'h'; - } - - $regex = game_substr( $puzzle, $pos, 1); - if ( ($regex == '0' or $regex == '.') and (! $val_blanc)){ - return false; - } - - if ((game_substr( $puzzle, $pos - $inc, 1) > '0')){ - return false; - } - - if ((game_substr( $puzzle, $pos + $inc, 1) > '0')){ - return false; - } + if ($dir == 'h') { + $inc = 1; + if ($pos + $inc >= $n2222) { + return false; + } + $oinc = $n22; + $newdir = 'v'; + } else { + $inc = $n22; + if ($pos + $inc >= $n2222) { + return false; + } + $oinc = 1; + $newdir = 'h'; + } + + $regex = game_substr( $puzzle, $pos, 1); + if ( ($regex == '0' or $regex == '.') and (!$valblanc)) { + return false; + } + + if ((game_substr( $puzzle, $pos - $inc, 1) > '0')) { + return false; + } + + if ((game_substr( $puzzle, $pos + $inc, 1) > '0')) { + return false; + } $left = $right = 0; - for ($limit_a = $pos - $inc; ($w = game_substr( $puzzle, $limit_a, 1)) !== '0'; $limit_a -= $inc) - { - if ($w == '.' and ((game_substr( $puzzle, $limit_a - $oinc, 1) > '0') or (game_substr( $puzzle, $limit_a + $oinc, 1) > '0'))){ - break; - } - - if (++$left == $MAXW){ - $left --; - break; - } - - $regex = $w . $regex; - } - - for ($limit_b = $pos + $inc; ($w = game_substr( $puzzle, $limit_b, 1)) !== '0'; $limit_b += $inc) - { - if ($w== '.' and ((game_substr( $puzzle, $limit_b - $oinc, 1) > '0') or (game_substr( $puzzle, $limit_b + $oinc, 1) > '0'))){ - break; - } - - if (++$right == $MAXW){ - $right--; - break; - } - - $regex .= $w; - } - - if (($len_regex = game_strlen($regex)) < 2){ - return false; - } - - foreach ($magics[$len_regex] as $m => $lens) - { - $ini = max(0, ($left + 1) - $lens); - $fin = $left; - - $pos_p = max($limit_a + $inc, $pos - (($lens - 1 ) * $inc)); - - for($pos_c = $ini; $pos_c <= $fin; $pos_c++, $pos_p += $inc) - { - if (game_substr( $puzzle, $pos_p - $inc, 1) > '0'){ - continue; - } - - $w = game_substr($regex, $pos_c, $lens); - - if( !$this->my_preg_match( $w, $words, $word)) - continue; - - $larr0 = $pos_p + ((game_strlen( $word) - 2) * $inc); - - if ($larr0 >= $N2222){ - continue; - } - - if (game_substr( $puzzle, $larr0, 1) > '0'){ - continue; - } - - $words = str_replace( $word, ';', $words); - - $len = game_strlen( $word) ; - for ($n = 1, $pp = $pos_p; $n < $len - 1; $n++, $pp += $inc) - { - $this->setchar( $puzzle, $pp, game_substr( $word , $n, 1)); - - if ($pp == $pos) - continue; - - $c = game_substr( $puzzle, $pp, 1); - $poss[] = array($pp, $new_dir, ord( $c)); - } - - $cross_pos[] = $pos_p; - $cross_dir[] = ($new_dir == 'h' ? 'v' : 'h'); - $cross_word[] = $word; - - $this->setchar( $puzzle, $pos_p - $inc, '0'); - $this->setchar( $puzzle, $pp, '0'); - - return true; - } + for ($limita = $pos - $inc; ($w = game_substr( $puzzle, $limita, 1)) !== '0'; $limita -= $inc) { + if ($w == '.' and ((game_substr( $puzzle, $limita - $oinc, 1) > '0') or + (game_substr( $puzzle, $limita + $oinc, 1) > '0'))) { + break; + } + + if (++$left == $maxw) { + $left --; + break; + } + + $regex = $w . $regex; + } + + for ($limitb = $pos + $inc; ($w = game_substr( $puzzle, $limitb, 1)) !== '0'; $limitb += $inc) { + if ($w == '.' and ((game_substr( $puzzle, $limitb - $oinc, 1) > '0') + or (game_substr( $puzzle, $limitb + $oinc, 1) > '0'))) { + break; + } + + if (++$right == $maxw) { + $right--; + break; + } + + $regex .= $w; + } + + if (($lenregex = game_strlen($regex)) < 2) { + return false; + } + + foreach ($magics[$lenregex] as $m => $lens) { + $ini = max(0, ($left + 1) - $lens); + $fin = $left; + + $posp = max($limita + $inc, $pos - (($lens - 1 ) * $inc)); + + for ($posc = $ini; $posc <= $fin; $posc++, $posp += $inc) { + if (game_substr( $puzzle, $posp - $inc, 1) > '0') { + continue; + } + + $w = game_substr($regex, $posc, $lens); + + if (!$this->my_preg_match( $w, $words, $word)) { + continue; + } + + $larr0 = $posp + ((game_strlen( $word) - 2) * $inc); + + if ($larr0 >= $n2222) { + continue; + } + + if (game_substr( $puzzle, $larr0, 1) > '0') { + continue; + } + + $words = str_replace( $word, ';', $words); + + $len = game_strlen( $word); + for ($n = 1, $pp = $posp; $n < $len - 1; $n++, $pp += $inc) { + $this->setchar( $puzzle, $pp, game_substr( $word , $n, 1)); + + if ($pp == $pos) { + continue; + } + + $c = game_substr( $puzzle, $pp, 1); + $poss[] = array($pp, $newdir, ord( $c)); + } + + $crosspos[] = $posp; + $crossdir[] = ($newdir == 'h' ? 'v' : 'h'); + $crossword[] = $word; + + $this->setchar( $puzzle, $posp - $inc, '0'); + $this->setchar( $puzzle, $pp, '0'); + + return true; + } } return false; } - function my_preg_match( $w, $words, &$word) - { - $a = explode( ";", $words); - $len_w = game_strlen( $w); - foreach( $a as $test) - { - if( game_strlen( $test) != $len_w) - continue; - - for( $i=0; $i <$len_w; $i++) - { - if( game_substr( $w, $i, 1) == '.') - continue; - if( game_substr( $w, $i, 1) != game_substr( $test, $i, 1) ) - break; - } - if( $i < $len_w) - continue; - $word = ';'.$test.';'; - - return true; - } - return false; - } - - - function setchar( &$s, $pos, $char) - { - $ret = ""; - - if( $pos > 0) - $ret .= game_substr( $s, 0, $pos); - - $s = $ret . $char . game_substr( $s, $pos+1, game_strlen( $s)-$pos-1); - } - - function showhtml_base( $crossm, $crossd, $showsolution, $showhtmlsolutions, $showstudentguess, $context, $game) - { - $this->m_LegendH = array(); - $this->m_LegendV = array(); - - $sRet = "CrosswordWidth = {$crossm->cols};\n"; - $sRet .= "CrosswordHeight = {$crossm->rows};\n"; - - $sRet .= "Words=".count( $crossd).";\n"; - $sWordLength = ""; - $sguess = ""; - $ssolutions = ''; - $shtmlsolutions = ''; - $sWordX = ""; - $sWordY = ""; - $sClue = ""; - $LastHorizontalWord = -1; - $i = -1; - $LegendV = array(); - $LegendH = array(); - - if( $game->glossaryid) - { + public function my_preg_match( $w, $words, &$word) { + $a = explode( ";", $words); + $lenw = game_strlen( $w); + foreach ($a as $test) { + if (game_strlen( $test) != $lenw) { + continue; + } + + for ($i = 0; $i < $lenw; $i++) { + if (game_substr( $w, $i, 1) == '.') { + continue; + } + if (game_substr( $w, $i, 1) != game_substr( $test, $i, 1) ) { + break; + } + } + if ($i < $lenw) { + continue; + } + $word = ';'.$test.';'; + + return true; + } + return false; + } + + public function setchar( &$s, $pos, $char) { + $ret = ""; + + if ($pos > 0) { + $ret .= game_substr( $s, 0, $pos); + } + + $s = $ret . $char . game_substr( $s, $pos + 1, game_strlen( $s) - $pos - 1); + } + + public function showhtml_base( $crossm, $crossd, $showsolution, $showhtmlsolutions, $showstudentguess, $context, $game) { + $this->mLegendh = array(); + $this->mLegendv = array(); + + $sret = "CrosswordWidth = {$crossm->cols};\n"; + $sret .= "CrosswordHeight = {$crossm->rows};\n"; + + $sret .= "Words=".count( $crossd).";\n"; + $swordlength = ""; + $sguess = ""; + $ssolutions = ''; + $shtmlsolutions = ''; + $swordx = ""; + $ssordy = ""; + $sclue = ""; + $lasthorizontalword = -1; + $i = -1; + $legendv = array(); + $legendh = array(); + + if ($game->glossaryid) { $cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course); $contextglossary = game_get_context_module_instance( $cmglossary->id); } - foreach ($crossd as $rec) - { - if( $rec->horizontal == false and $LastHorizontalWord == -1){ - $LastHorizontalWord = $i; - } - - $i++; - - $sWordLength .= ",".game_strlen( $rec->answertext); - if( $rec->questionid != 0) - { - $q = game_filterquestion(str_replace( '\"', '"', $rec->questiontext), $rec->questionid, $context->id, $game->course); + foreach ($crossd as $rec) { + if ($rec->horizontal == false and $lasthorizontalword == -1) { + $lasthorizontalword = $i; + } + + $i++; + + $swordlength .= ",".game_strlen( $rec->answertext); + if ($rec->questionid != 0) { + $q = game_filterquestion(str_replace( '\"', '"', $rec->questiontext), + $rec->questionid, $context->id, $game->course); $rec->questiontext = game_repairquestion( $q); - }else - { - //glossary - $q = game_filterglossary(str_replace( '\"', '"', $rec->questiontext), $rec->glossaryentryid, $contextglossary->id, $game->course); + } else { + // Glossary. + $q = game_filterglossary(str_replace( '\"', '"', $rec->questiontext), + $rec->glossaryentryid, $contextglossary->id, $game->course); $rec->questiontext = game_repairquestion( $q); } - - $sClue .= ',"'.game_tojavascriptstring( game_filtertext( $rec->questiontext, 0))."\"\r\n"; - if( $showstudentguess) - $sguess .= ',"'.$rec->studentanswer.'"'; - else - $sguess .= ",''"; - $sWordX .= ",".($rec->col-1); - $sWordY .= ",".($rec->row-1); - if( $showsolution){ - $ssolutions .= ',"'.$rec->answertext.'"'; - }else - { - $ssolutions .= ',""'; - } - - if( $showhtmlsolutions){ - $shtmlsolutions .= ',"'.base64_encode( $rec->answertext).'"'; - } - - $attachment = ''; - //if( game_issoundfile( $rec->attachment)){ - // $attachment = game_showattachment( $rec->attachment); - //} - + + $sclue .= ',"'.game_tojavascriptstring( game_filtertext( $rec->questiontext, 0))."\"\r\n"; + if ($showstudentguess) { + $sguess .= ',"'.$rec->studentanswer.'"'; + } else { + $sguess .= ",''"; + } + $swordx .= ",".($rec->col - 1); + $swordy .= ",".($rec->row - 1); + if ($showsolution) { + $ssolutions .= ',"'.$rec->answertext.'"'; + } else { + $ssolutions .= ',""'; + } + + if ($showhtmlsolutions) { + $shtmlsolutions .= ',"'.base64_encode( $rec->answertext).'"'; + } + + $attachment = ''; + $s = $rec->questiontext.$attachment; - if( $rec->horizontal){ - if( array_key_exists( $rec->row, $LegendH)){ - $LegendH[ $rec->row][] = $s; - }else - { - $LegendH[ $rec->row] = array( $s); - } - }else - { - if( array_key_exists( $rec->col, $LegendV)){ - $LegendV[ $rec->col][] = $s; - }else - { - $LegendV[ $rec->col] = array( $s); - } - } - } - - $letters = get_string( 'lettersall', 'game'); - - $this->m_LegendH = array(); - foreach( $LegendH as $key => $value) - { - if( count( $value) == 1) - $this->m_LegendH[ $key] = $value[ 0]; - else - { - for( $i=0; $i < count( $value); $i++) - { - $this->m_LegendH[ $key.game_substr( $letters, $i, 1)] = $value[ $i]; + if ($rec->horizontal) { + if (array_key_exists( $rec->row, $legendh)) { + $legendh[ $rec->row][] = $s; + } else { + $legendh[ $rec->row] = array( $s); } - } - } - - $this->m_LegendV = array(); - foreach( $LegendV as $key => $value) - { - if( count( $value) == 1) - $this->m_LegendV[ $key] = $value[ 0]; - else - { - for( $i=0; $i < count( $value); $i++) - { - $this->m_LegendV[ $key.game_substr( $letters, $i, 1)] = $value[ $i]; + } else { + if (array_key_exists( $rec->col, $legendv)) { + $legendv[ $rec->col][] = $s; + } else { + $legendv[ $rec->col] = array( $s); } - } - } - - ksort( $this->m_LegendH); - ksort( $this->m_LegendV); - - $sRet .= "WordLength = new Array( ".game_substr( $sWordLength, 1).");\n"; - $sRet .= "Clue = new Array( ".game_substr( $sClue, 1).");\n"; - $sguess = str_replace( ' ', '_', $sguess); - $sRet .= "Guess = new Array( ".game_substr( $sguess, 1).");\n"; - $sRet .= "Solutions = new Array( ".game_substr( $ssolutions, 1).");\n"; - if( $showhtmlsolutions){ - $sRet .= "HtmlSolutions = new Array( ".game_substr( $shtmlsolutions, 1).");\n"; - } - $sRet .= "WordX = new Array( ".game_substr( $sWordX, 1).");\n"; - $sRet .= "WordY = new Array( ".game_substr( $sWordY, 1).");\n"; - $sRet .= "LastHorizontalWord = $LastHorizontalWord;\n"; - - return $sRet; - } + } + } + + $letters = get_string( 'lettersall', 'game'); + $this->mlegendh = array(); + foreach ($legendh as $key => $value) { + if (count( $value) == 1) { + $this->m_LegendH[ $key] = $value[ 0]; + } else { + for ($i = 0; $i < count( $value); $i++) { + $this->mlegendh[ $key.game_substr( $letters, $i, 1)] = $value[ $i]; + } + } + } - function cmp($a, $b) { - return game_strlen($b) - game_strlen($a); - } + $this->mlegendv = array(); + foreach ($legendv as $key => $value) { + if (count( $value) == 1) { + $this->mlegendv[ $key] = $value[ 0]; + } else { + for ($i = 0; $i < count( $value); $i++) { + $this->mlegendv[ $key.game_substr( $letters, $i, 1)] = $value[ $i]; + } + } + } + ksort( $this->mlegendh); + ksort( $this->mlegendv); - function cmp_magic($a, $b) { - return (game_strlen($a) + mt_rand(0, 3)) - (game_strlen($b) - mt_rand(0, 1)); - } + $sret .= "WordLength = new Array( ".game_substr( $swordlength, 1).");\n"; + $sret .= "Clue = new Array( ".game_substr( $sclue, 1).");\n"; + $sguess = str_replace( ' ', '_', $sguess); + $sret .= "Guess = new Array( ".game_substr( $sguess, 1).");\n"; + $sret .= "Solutions = new Array( ".game_substr( $ssolutions, 1).");\n"; + if ($showhtmlsolutions) { + $sret .= "HtmlSolutions = new Array( ".game_substr( $shtmlsolutions, 1).");\n"; + } + $sret .= "WordX = new Array( ".game_substr( $swordx, 1).");\n"; + $sret .= "WordY = new Array( ".game_substr( $swordy, 1).");\n"; + $sret .= "LastHorizontalWord = $lasthorizontalword;\n"; + + return $sret; + } + + public function cmp($a, $b) { + return game_strlen($b) - game_strlen($a); + } + + public function cmp_magic($a, $b) { + return (game_strlen($a) + mt_rand(0, 3)) - (game_strlen($b) - mt_rand(0, 1)); + } } diff --git a/cross/crossdb_class.php b/cross/crossdb_class.php index 3b66f7e..4b6660a 100755 --- a/cross/crossdb_class.php +++ b/cross/crossdb_class.php @@ -1,4 +1,21 @@ -. +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + /** * This class is a cross that can load and save to a table * @@ -7,242 +24,230 @@ * @package game **/ -class CrossDB extends Cross -{ - function savecross( $game, &$crossm, $crossd, $id) - { - global $DB, $USER; - - $crossm->id = $id; - $crossm->sourcemodule = $game->sourcemodule; - - $this->delete_records( $id); - - if (!(game_insert_record( "game_cross", $crossm))){ - print_error( 'Insert page: new page game_cross not inserted'); - } - - foreach( $crossd as $rec) - { - $rec->attemptid = $id; - $rec->questiontext = addslashes( $rec->questiontext); - - $rec->gamekind = $game->gamekind; - $rec->gameid = $game->id; - $rec->userid = $USER->id; - $rec->sourcemodule = $game->sourcemodule; - - if (!$DB->insert_record( 'game_queries', $rec)){ - print_error( 'Insert page: new page game_queries not inserted'); - } +class CrossDB extends Cross { + public function savecross( $game, &$crossm, $crossd, $id) { + global $DB, $USER; + + $crossm->id = $id; + $crossm->sourcemodule = $game->sourcemodule; + + $this->delete_records( $id); + + if (!(game_insert_record( "game_cross", $crossm))) { + print_error( 'Insert page: new page game_cross not inserted'); + } + + foreach ($crossd as $rec) { + $rec->attemptid = $id; + $rec->questiontext = addslashes( $rec->questiontext); + + $rec->gamekind = $game->gamekind; + $rec->gameid = $game->id; + $rec->userid = $USER->id; + $rec->sourcemodule = $game->sourcemodule; + + if (!$DB->insert_record( 'game_queries', $rec)) { + print_error( 'Insert page: new page game_queries not inserted'); + } game_update_repetitions($game->id, $USER->id, $rec->questionid, $rec->glossaryentryid); - } + } return true; - } + } - function delete_records( $id) - { + public function delete_records( $id) { global $DB; - if( !$DB->delete_records( 'game_queries', array( 'attemptid' => $id))){ - print_error( "Can't delete from game_queries attemptid=$id"); + if (!$DB->delete_records( 'game_queries', array( 'attemptid' => $id))) { + print_error( "Can't delete from game_queries attemptid=$id"); } - if( !$DB->delete_records( 'game_cross', array( 'id' => $id))){ - print_error( "Can't delete from game_cross id=$id"); + if (!$DB->delete_records( 'game_cross', array( 'id' => $id))) { + print_error( "Can't delete from game_cross id=$id"); } } - function loadcross( $g, &$done, &$html, $game, $attempt, $crossrec, $onlyshow, $showsolution, &$finishattempt, $showhtmlsolutions, &$language, $showstudentguess, $context) - { - global $DB; - - $info = ''; - $correctLetters = 0; - $allLetters = 0; - $wrongLetters = 0; - $html = ''; - $done = false; - - $loadfromdb = ( $g == ""); - - $this->m_mincol = $this->m_minrow = 0; - $this->m_maxcol = $crossrec->cols; - $this->m_maxrow = $crossrec->rows; - - if( $g == ""){ - $g = str_repeat( ' ', $this->m_maxcol * $this->m_maxrow); - } - - $load = false; - - $puzzle = str_repeat('.', $this->m_maxrow * $this->m_maxcol); - if ($recs = $DB->get_records( 'game_queries', array( 'attemptid' => $crossrec->id))) - { - $a = array(); - foreach ($recs as $rec) - { - if( $rec->horizontal) - $key = sprintf( 'h%10d %10d', $rec->row, $rec->col); - else - $key = sprintf( 'v%10d %10d', $rec->col, $rec->row); - - $a[ $key] = $rec; - } - - ksort( $a); - $b = array(); - $correctletters = $wrongletters = $restletters = 0; - - foreach( $a as $rec){ - $this->updatecrossquestions( $rec, $g, $pos, $correctletters, $wrongletters, $restletters, $game, $attempt, $crossrec, $loadfromdb); - $b[] = $rec; - - if( ($rec->col != 0) and ($rec->row != 0)){ - $load = true; - } - if( $language == ''){ - $language = game_detectlanguage( $rec->answertext); + public function loadcross( $g, &$done, &$html, $game, $attempt, $crossrec, $onlyshow, $showsolution, + &$finishattempt, $showhtmlsolutions, &$language, $showstudentguess, $context) { + global $DB; + + $info = ''; + $correctletters = 0; + $allletters = 0; + $wrongletters = 0; + $html = ''; + $done = false; + + $loadfromdb = ( $g == ""); + + $this->mmincol = $this->mminrow = 0; + $this->mmaxcol = $crossrec->cols; + $this->mmaxrow = $crossrec->rows; + + if ($g == "") { + $g = str_repeat( ' ', $this->m_maxcol * $this->m_maxrow); + } + + $load = false; + + $puzzle = str_repeat('.', $this->m_maxrow * $this->m_maxcol); + if ($recs = $DB->get_records( 'game_queries', array( 'attemptid' => $crossrec->id))) { + $a = array(); + foreach ($recs as $rec) { + if ($rec->horizontal) { + $key = sprintf( 'h%10d %10d', $rec->row, $rec->col); + } else { + $key = sprintf( 'v%10d %10d', $rec->col, $rec->row); + } + $a[ $key] = $rec; } - } - $info = $this->game_cross_computecheck( $correctletters, $wrongletters, $restletters, $game, $attempt, $done, $onlyshow, $showsolution, $finishattempt); - $html = $this->showhtml_base( $crossrec, $b, $showsolution, $showhtmlsolutions, $showstudentguess, $context, $game); + + ksort( $a); + $b = array(); + $correctletters = $wrongletters = $restletters = 0; + + foreach ($a as $rec) { + $this->updatecrossquestions( $rec, $g, $pos, $correctletters, + $wrongletters, $restletters, $game, $attempt, $crossrec, $loadfromdb); + $b[] = $rec; + + if (($rec->col != 0) and ($rec->row != 0)) { + $load = true; + } + if ($language == '') { + $language = game_detectlanguage( $rec->answertext); + } + } + $info = $this->game_cross_computecheck( $correctletters, $wrongletters, + $restletters, $game, $attempt, $done, $onlyshow, $showsolution, $finishattempt); + $html = $this->showhtml_base( $crossrec, $b, $showsolution, $showhtmlsolutions, $showstudentguess, $context, $game); + } + + if ($load == false) { + $finishattempt = true; + } + + return $info; } - - if( $load == false) - { - $finishattempt = true; - } - - return $info; - } - -function game_cross_computecheck( $correctletters, $wrongletters, $restletters, $game, $attempt, &$done, $onlyshow, $showsolution, $finishattempt) -{ - $ret = ''; - - //if( $correctletters == 0 and $wrongletters == 0){ - // return $ret; - //} - - $and = get_string( 'and', 'game'); - - $a = array(); - if( $correctletters) - $a[] = $correctletters.' '.( $correctletters > 1 ? get_string( 'cross_corrects', 'game') :get_string( 'cross_correct', 'game')); - if( $wrongletters) - $a[] = ''.$wrongletters.' '.( $wrongletters > 1 ? get_string( 'cross_errors', 'game') : get_string( 'cross_error', 'game')).''; - - if( $correctletters > 1 or $wrongletters > 1) { - $ret = get_string( 'cross_found_many', 'game'); - }else if( count( $a)) - { - $ret = get_string( 'cross_found_one', 'game'); - }else - $ret = ''; - - $i = 0; - foreach( $a as $msg) - { - $i++; - - if( $i == 1){ - $ret .= ' '.$msg; - }else if( $i == count($a)) - { - $ret .= ' '.get_string( 'and', 'game').' '.$msg; - }else - { - $ret .= ', '.$msg; - } - } - - $done = ( $restletters == 0 ? true : false); - - if( $finishattempt == false){ - if( $onlyshow or $showsolution){ - return $ret; - } - }else{ - $done = 1; - } - - $grade = $correctletters / ($correctletters + $restletters); - $ret .= '
'.get_string( 'grade', 'game').' '.round( $grade * 100).' %'; - - game_updateattempts( $game, $attempt, $grade, $done); - - return $ret; -} - //rec is a record of cross_questions - function updatecrossquestions( &$rec, &$g, &$pos, &$correctletters, &$wrongletters, &$restletters, $game, $attempt, $crossrec, $loadfromdb) - { - global $DB, $USER; - - $word = $rec->answertext; - $len = game_strlen( $word); - - if( $loadfromdb) - $guess = $rec->studentanswer; - else - $guess = game_substr( $g, $pos, $len); - - $len_guess = game_strlen( $guess);; - $pos += $len; - - $is_empty = true; - for($i = 0; $i < $len; $i++) - { - if( $i < $len_guess) - $letterguess = game_substr( $guess, $i, 1); - else - $letterguess = " "; - - if( $letterguess != ' ') - $is_empty = false; - - $letterword= game_substr( $word, $i, 1); - if( $letterword != $letterguess) - { - if( ($letterguess != ' ' and $letterguess != '_')){ + public function game_cross_computecheck( $correctletters, $wrongletters, $restletters, $game, + $attempt, &$done, $onlyshow, $showsolution, $finishattempt) { + + $ret = ''; + + $and = get_string( 'and', 'game'); + + $a = array(); + if ($correctletters) { + $a[] = $correctletters.' '. + ( $correctletters > 1 ? get_string( 'cross_corrects', 'game') : get_string( 'cross_correct', 'game')); + } + if ($wrongletters) { + $a[] = ''.$wrongletters.' '. + ( $wrongletters > 1 ? get_string( 'cross_errors', 'game') : get_string( 'cross_error', 'game')).''; + } + + if ($correctletters > 1 or $wrongletters > 1) { + $ret = get_string( 'cross_found_many', 'game'); + } else if ( count( $a)) { + $ret = get_string( 'cross_found_one', 'game'); + } else { + $ret = ''; + } + + $i = 0; + foreach ($a as $msg) { + $i++; + + if ($i == 1) { + $ret .= ' '.$msg; + } else if ( $i == count($a)) { + $ret .= ' '.get_string( 'and', 'game').' '.$msg; + } else { + $ret .= ', '.$msg; + } + } + + $done = ( $restletters == 0 ? true : false); + + if ($finishattempt == false) { + if ($onlyshow or $showsolution) { + return $ret; + } + } else { + $done = 1; + } + + $grade = $correctletters / ($correctletters + $restletters); + $ret .= '
'.get_string( 'grade', 'game').' '.round( $grade * 100).' %'; + + game_updateattempts( $game, $attempt, $grade, $done); + + return $ret; + } + + // Rec is a record of cross_questions. + public function updatecrossquestions( &$rec, &$g, &$pos, &$correctletters, &$wrongletters, + &$restletters, $game, $attempt, $crossrec, $loadfromdb) { + + global $DB, $USER; + + $word = $rec->answertext; + $len = game_strlen( $word); + + if ($loadfromdb) { + $guess = $rec->studentanswer; + } else { + $guess = game_substr( $g, $pos, $len); + } + + $lenguess = game_strlen( $guess);; + $pos += $len; + + $isempty = true; + for ($i = 0; $i < $len; $i++) { + if ($i < $lenguess) { + $letterguess = game_substr( $guess, $i, 1); + } else { + $letterguess = " "; + } + + if ($letterguess != ' ') { + $isempty = false; + } + $letterword = game_substr( $word, $i, 1); + if ($letterword != $letterguess) { + if (($letterguess != ' ' and $letterguess != '_')) { $wrongletters++; } game_setchar( $guess, $i, '_'); $restletters++; - }else if( $letterguess == ' '){ - if( $guess == $word){ - $correctletters++; - }else - { - //$wrongletters++; - //game_setchar( $guess, $i, '_'); - } - }else - { - $correctletters++; - } - } - - if( $is_empty){ - return; - } - if( ($rec->studentanswer == $guess )){ - return; - } - - $rec->studentanswer = $guess; - - $updrec = new stdClass(); - $updrec->studentanswer = $guess; - $updrec->id = $rec->id; - if (!$DB->update_record( 'game_queries', $updrec, $rec->id)){ - print_error( 'Update game_queries: not updated'); - } - - $score = $correctletters / $len; - game_update_queries( $game, $attempt, $rec, $score, $guess); - } -} + } else if ($letterguess == ' ') { + if ($guess == $word) { + $correctletters++; + } + } else { + $correctletters++; + } + } + + if ($isempty) { + return; + } + if (($rec->studentanswer == $guess )) { + return; + } + + $rec->studentanswer = $guess; + $updrec = new stdClass(); + $updrec->studentanswer = $guess; + $updrec->id = $rec->id; + if (!$DB->update_record( 'game_queries', $updrec, $rec->id)) { + print_error( 'Update game_queries: not updated'); + } + + $score = $correctletters / $len; + game_update_queries( $game, $attempt, $rec, $score, $guess); + } +} diff --git a/cross/play.php b/cross/play.php index fc4c9a2..43bd043 100755 --- a/cross/play.php +++ b/cross/play.php @@ -1,144 +1,163 @@ -. +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +// This files plays the game "Crossword". require( "cross_class.php"); require( "crossdb_class.php"); -function game_cross_continue( $id, $game, $attempt, $cross, $g, $endofgame, $context) -{ - if( $endofgame){ - if( $g == ''){ - game_updateattempts( $game, $attempt, -1, true); - $endofgame = false; - } - } +function game_cross_continue( $id, $game, $attempt, $cross, $g, $endofgame, $context) { + if ($endofgame) { + if ($g == '') { + game_updateattempts( $game, $attempt, -1, true); + $endofgame = false; + } + } - if( $attempt != false and $cross != false){ - return game_cross_play( $id, $game, $attempt, $cross, $g, false, false, $endofgame, false, false, false, false, true, $context); - } - - if( $attempt == false){ - $attempt = game_addattempt( $game); - } - - game_cross_new( $game, $attempt->id, $crossm); - game_updateattempts( $game, $attempt, 0, 0); + if ($attempt != false and $cross != false) { + return game_cross_play( $id, $game, $attempt, $cross, $g, false, false, $endofgame, + false, false, false, false, true, $context); + } + + if ($attempt == false) { + $attempt = game_addattempt( $game); + } + + game_cross_new( $game, $attempt->id, $crossm); + game_updateattempts( $game, $attempt, 0, 0); return game_cross_play( $id, $game, $attempt, $crossm, '', false, false, false, false, false, false, false, true, $context); } -function game_cross_new( $game, $attemptid, &$crossm) -{ +function game_cross_new( $game, $attemptid, &$crossm) { global $DB, $USER; - - $cross = new CrossDB(); - $questions = array(); - $infos = array(); + $cross = new CrossDB(); - $answers = array(); - $recs = game_questions_shortanswer( $game); - if( $recs == false){ - print_error( 'game_cross_continue: '.get_string( 'no_words', 'game')); - } - $infos = array(); + $questions = array(); + $infos = array(); + + $answers = array(); + $recs = game_questions_shortanswer( $game); + if ($recs == false) { + print_error( 'game_cross_continue: '.get_string( 'no_words', 'game')); + } + $infos = array(); $reps = array(); - foreach( $recs as $rec){ - if( $game->param7 == false){ - if( game_strpos( $rec->answertext, ' ')){ - continue; //spaces not allowed - } - } - - $rec->answertext = game_upper( $rec->answertext); - $answers[ $rec->answertext] = game_repairquestion( $rec->questiontext); - $infos[ $rec->answertext] = array( $game->sourcemodule, $rec->questionid, $rec->glossaryentryid, $rec->attachment); + foreach ($recs as $rec) { + if ($game->param7 == false) { + if (game_strpos( $rec->answertext, ' ')) { + continue; // Spaces not allowed. + } + } + + $rec->answertext = game_upper( $rec->answertext); + $answers[ $rec->answertext] = game_repairquestion( $rec->questiontext); + $infos[ $rec->answertext] = array( $game->sourcemodule, $rec->questionid, $rec->glossaryentryid, $rec->attachment); - $a = array( 'gameid' => $game->id, 'userid' => $USER->id, 'questionid' => $rec->questionid, 'glossaryentryid' => $rec->glossaryentryid); - if(($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions AS r')) != false){ + $a = array( 'gameid' => $game->id, 'userid' => $USER->id, + 'questionid' => $rec->questionid, 'glossaryentryid' => $rec->glossaryentryid); + if (($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions AS r')) != false) { $reps[ $rec->answertext] = $rec2->r; } - } - - $cross->setwords( $answers, $game->param1, $reps); - - //game->param4 is minimum words in crossword - //game->param2 is maximum words in crossword - if( $cross->computedata( $crossm, $crossd, $lettets, $game->param4, $game->param2)){ - $new_crossd = array(); - foreach( $crossd as $rec) - { - $info = $infos[ $rec->answertext]; - if( $info != false){ - $rec->sourcemodule = $info[ 0]; - $rec->questionid = $info[ 1]; - $rec->glossaryentryid = $info[ 2]; - $rec->attachment = $info[ 3]; - } - $new_crossd[] = $rec; - } - $cross->savecross( $game, $crossm, $new_crossd, $attemptid); - } - - if( count( $crossd) == 0){ - print_error( 'game_cross_continue: '.get_string( 'no_words', 'game')); - } + } + + $cross->setwords( $answers, $game->param1, $reps); + + // The game->param4 is minimum words in crossword. + // The game->param2 is maximum words in crossword. + if ($cross->computedata( $crossm, $crossd, $lettets, $game->param4, $game->param2)) { + $newcrossd = array(); + foreach ($crossd as $rec) { + $info = $infos[ $rec->answertext]; + if ($info != false) { + $rec->sourcemodule = $info[ 0]; + $rec->questionid = $info[ 1]; + $rec->glossaryentryid = $info[ 2]; + $rec->attachment = $info[ 3]; + } + $newcrossd[] = $rec; + } + $cross->savecross( $game, $crossm, $newcrossd, $attemptid); + } + + if (count( $crossd) == 0) { + print_error( 'game_cross_continue: '.get_string( 'no_words', 'game')); + } } -function showlegend( $legend, $title) -{ - if( count( $legend) == 0) +function showlegend( $legend, $title) { + if (count( $legend) == 0) { return; - + } + echo "
$title
"; - foreach( $legend as $key => $line) - { + foreach ($legend as $key => $line) { $line = game_repairquestion( $line); echo game_filtertext( "$key: $line
", 0); } } -function game_cross_play( $id, $game, $attempt, $crossrec, $g, $onlyshow, $showsolution, $endofgame, $print, $checkbutton, $showhtmlsolutions, $showhtmlprintbutton,$showstudentguess, $context) -{ - global $CFG, $DB; +function game_cross_play( $id, $game, $attempt, $crossrec, $g, $onlyshow, $showsolution, + $endofgame, $print, $checkbutton, $showhtmlsolutions, $showhtmlprintbutton, $showstudentguess, $context) { + global $CFG, $DB; - $cross = new CrossDB(); + $cross = new CrossDB(); $language = $attempt->language; - $info = $cross->loadcross( $g, $done, $html, $game, $attempt, $crossrec, $onlyshow, $showsolution, $endofgame, $showhtmlsolutions, $attempt->language,$showstudentguess, $context); + $info = $cross->loadcross( $g, $done, $html, $game, $attempt, $crossrec, $onlyshow, + $showsolution, $endofgame, $showhtmlsolutions, $attempt->language, + $showstudentguess, $context); - if( $language != $attempt->language){ - if( !$DB->set_field( 'game_attempts', 'language', $attempt->language, array( 'id' => $attempt->id))){ - print_error( "game_cross_play: Can't set language"); + if ($language != $attempt->language) { + if (!$DB->set_field( 'game_attempts', 'language', $attempt->language, array( 'id' => $attempt->id))) { + print_error( "game_cross_play: Can't set language"); } } - if( $done or $endofgame){ - if (! $cm = $DB->get_record( 'course_modules', array( 'id' => $id))) { - print_error("Course Module ID was incorrect id=$id"); - } - - if( $endofgame == false){ - echo ''.get_string( 'win', 'game').'
'; - } - if( game_can_start_new_attempt( $game)) - { - echo '
'; - echo "wwwroot}/mod/game/attempt.php?id=$id&forcenew=1\">".get_string( 'nextgame', 'game').'         '; - } - }else if( $info != ''){ - echo "
$info
"; - } + if ($done or $endofgame) { + if (! $cm = $DB->get_record( 'course_modules', array( 'id' => $id))) { + print_error("Course Module ID was incorrect id=$id"); + } - if( $attempt->language != '') + if ($endofgame == false) { + echo ''.get_string( 'win', 'game').'
'; + } + if (game_can_start_new_attempt( $game)) { + echo '
'; + echo "wwwroot}/mod/game/attempt.php?id=$id&forcenew=1\">". + get_string( 'nextgame', 'game').'         '; + } + } else if ($info != '') { + echo "
$info
"; + } + + if ($attempt->language != '') { $wordrtl = game_right_to_left( $attempt->language); - else + } else { $wordrtl = right_to_left(); + } + $reverseprint = ($wordrtl != right_to_left()); - if( $reverseprint) + if ($reverseprint) { $textdir = 'dir="'.($wordrtl ? 'rtl' : 'ltr').'"'; - else + } else { $textdir = ''; + } ?>