Browse Source

Fix:Coding style

MOODLE_20_STABLE
Vasilis Daloukas 9 years ago
parent
commit
dda27f2ccf
  1. 1543
      sudoku/class.Sudoku.php
  2. 106
      sudoku/create.php
  3. 30
      sudoku/export.php
  4. 258
      sudoku/play.php
  5. 421
      sudoku/sdd/class.SDD.php
  6. 64
      sudoku/sdd/class.logfile.php

1543
sudoku/class.Sudoku.php

File diff suppressed because it is too large

106
sudoku/create.php

@ -1,21 +1,33 @@
<?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");
include_once("class.Sudoku.php"); require_once("class.Sudoku.php");
require( '../header.php'); require( '../header.php');
$action = optional_param('action', PARAM_ALPHA); // action $action = optional_param('action', PARAM_ALPHA); // The action.
if ($action == 'create') { if ($action == 'create') {
AppendSudokuB(); AppendSudokuB();
}else } else {
{
showform(); showform();
} }
function showform() function showform() {
{ $id = required_param('id', PARAM_NUMBER); // The action.
$id = required_param('id', PARAM_NUMBER); // action
?> ?>
<form name="form" method="post" action="create.php"> <form name="form" method="post" action="create.php">
@ -36,25 +48,21 @@ function showform()
</form> </form>
<?php <?php
} }
function AppendSudokuB() function appendsudokub() {
{
global $DB; global $DB;
$level1 = required_param('level1', PARAM_NUMBER); // action $level1 = required_param('level1', PARAM_NUMBER);
$level2 = required_param('level2', PARAM_NUMBER); // action $level2 = required_param('level2', PARAM_NUMBER);
$count = required_param('count', PARAM_NUMBER); // action $count = required_param('count', PARAM_NUMBER);
$level = $level1; $level = $level1;
for( $i=1; $i <= $count; $i++) for ($i = 1; $i <= $count; $i++) {
{ create( $si, $sp, $level);
//set_time_limit( 30);
Create( $si, $sp, $level);
$newrec->data = PackSudoku( $si, $sp); $newrec->data = packsudoku( $si, $sp);
if (strlen( $newrec->data) != 81) { if (strlen( $newrec->data) != 81) {
return 0; return 0;
} }
@ -72,23 +80,21 @@ function AppendSudokuB()
} }
} }
function PackSudoku( $si, $sp) function packsudoku( $si, $sp) {
{ $data = '';
$data = "";
for ($i = 1; $i <= 9; $i++) for ($i = 1; $i <= 9; $i++) {
{ for ($j = 1; $j <= 9; $j++) {
for ($j = 1; $j <= 9; $j++) $c = &$sp->thesquares[$i];
{ $c = &$c->getcell($j);
$c = &$sp->theSquares[$i]; $solution = $c->asstring( false);
$c = &$c->getCell($j) ;
$solution = $c->asString( false);
$c = &$si->theSquares[$i] ; $c = &$si->thesquares[$i];
$c = &$c->getCell($j); $c = &$c->getCell($j);
$theSolvedState = $c->solvedState() ; $thesolvedstate = $c->solvedstate();
if( $theSolvedState == 1) { //hint if ($thesolvedstate == 1) {
// Hint.
$solution = substr( 'ABCDEFGHI', $c->asString( false) - 1, 1); $solution = substr( 'ABCDEFGHI', $c->asString( false) - 1, 1);
} }
@ -99,15 +105,11 @@ function PackSudoku( $si, $sp)
return $data; return $data;
} }
function create( &$si, &$sp, $level=1) {
function create( &$si, &$sp, $level=1) for ($i = 1; $i <= 40; $i++) {
{ $sp = new sudoku();
for( $i=1; $i <= 40; $i++) $theinitialposition = $sp->generatepuzzle( 10, 50, $level);
{ if (count( $theinitialposition)) {
//set_time_limit( 30);
$sp = new Sudoku() ;
$theInitialPosition = $sp->generatePuzzle( 10, 50, $level) ;
if( count( $theInitialPosition)){
break; break;
} }
} }
@ -115,30 +117,28 @@ function create( &$si, &$sp, $level=1)
return false; return false;
} }
$si = new Sudoku() ; $si = new sudoku();
$si->initializePuzzleFromArray($theInitialPosition); $si->initializepuzzlefromarray( $theinitialposition);
return true; return true;
} }
function GetOpened( $si) function getopened( $si) {
{
$count = 0; $count = 0;
for ($i = 1; $i <= 9; $i++) for ($i = 1; $i <= 9; $i++) {
{ for ($j = 1; $j <= 9; $j++) {
for ($j = 1; $j <= 9; $j++) $c = &$si->thesquares[$i];
{ $c = &$c->getcell($j);
$c = &$si->theSquares[$i] ; $thesolvedstate = $c->solvedstate();
$c = &$c->getCell($j) ;
$theSolvedState = $c->solvedState() ;
if( $theSolvedState == 1) //hint if ($thesolvedstate == 1) {
// Hint.
$count++; $count++;
} }
} }
}
return $count; return $count;
} }

30
sudoku/export.php

@ -1,33 +1,45 @@
<?php // $Id: export.php,v 1.3 2012/07/25 11:16:07 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");
export(); export();
function export() function export() {
{
global $CFG; global $CFG;
$file = "import.php"; $file = "import.php";
$h = fopen($file, 'w') or die("can't open file"); $h = fopen($file, 'w') or die("can't open file");
fwrite( $h, "<?php\r\n"); fwrite( $h, "<?php\r\n");
fwrite( $h, "require( \"../../../config.php\");\r\n\r\n"); fwrite( $h, "require( \"../../../config.php\");\r\n\r\n");
if( ($recs=get_records_select( 'game_sudoku_database')) == false) if (($recs = get_records_select( 'game_sudoku_database')) == false) {
print_error('empty'); print_error('empty');
}
$i = 0; $i = 0;
foreach( $recs as $rec) foreach ($recs as $rec) {
{
fwrite( $h, "execute_sql( \"INSERT INTO {game_sudoku_database} ( level, opened, data) ". fwrite( $h, "execute_sql( \"INSERT INTO {game_sudoku_database} ( level, opened, data) ".
"VALUES ($rec->level, $rec->opened, '$rec->data')\", false);\r\n"); "VALUES ($rec->level, $rec->opened, '$rec->data')\", false);\r\n");
if( ++$i % 10 == 0) if (++$i % 10 == 0) {
fwrite( $h, "\r\n"); fwrite( $h, "\r\n");
} }
}
fwrite( $h, "\r\necho'Finished importing';"); fwrite( $h, "\r\necho'Finished importing';");
fclose($h); fclose($h);
} }

258
sudoku/play.php

@ -1,9 +1,22 @@
<?php // $Id: play.php,v 1.23 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/>.
require_once( "../../lib/questionlib.php"); require_once( "../../lib/questionlib.php");
function game_sudoku_continue( $id, $game, $attempt, $sudoku, $endofgame, $context) function game_sudoku_continue( $id, $game, $attempt, $sudoku, $endofgame, $context) {
{
global $CFG, $DB, $USER; global $CFG, $DB, $USER;
if ($endofgame) { if ($endofgame) {
@ -19,7 +32,7 @@ function game_sudoku_continue( $id, $game, $attempt, $sudoku, $endofgame, $conte
$attempt = game_addattempt( $game); $attempt = game_addattempt( $game);
} }
//new game // New game.
srand( (double)microtime() * 1000000); srand( (double)microtime() * 1000000);
$recsudoku = getrandomsudoku(); $recsudoku = getrandomsudoku();
@ -36,7 +49,7 @@ function game_sudoku_continue( $id, $game, $attempt, $sudoku, $endofgame, $conte
$need = 81 - $recsudoku->opened; $need = 81 - $recsudoku->opened;
$closed = game_sudoku_getclosed( $newrec->data); $closed = game_sudoku_getclosed( $newrec->data);
$n = min( count($closed), $need); $n = min( count($closed), $need);
//if the teacher set the maximum number of questions // If the teacher set the maximum number of questions.
if ($game->param2 > 0) { if ($game->param2 > 0) {
if ($game->param2 < $n) { if ($game->param2 < $n) {
$n = $game->param2; $n = $game->param2;
@ -45,13 +58,14 @@ function game_sudoku_continue( $id, $game, $attempt, $sudoku, $endofgame, $conte
$recs = game_questions_selectrandom( $game, CONST_GAME_TRIES_REPETITION * $n); $recs = game_questions_selectrandom( $game, CONST_GAME_TRIES_REPETITION * $n);
if ($recs === false) { if ($recs === false) {
mysql_execute( "DELETE FROM {game_sudoku} WHERE id={$game->id}"); $sql = "DELETE FROM {game_sudoku} WHERE id={$game->id}";
$DB->execute( $sql);
print_error( get_string( 'no_questions', 'game')); print_error( get_string( 'no_questions', 'game'));
} }
$closed = array_rand($closed, $n); $closed = array_rand($closed, $n);
$selected_recs = game_select_from_repetitions( $game, $recs, $n); $selectedrecs = game_select_from_repetitions( $game, $recs, $n);
if (!game_insert_record('game_sudoku', $newrec)) { if (!game_insert_record('game_sudoku', $newrec)) {
print_error('error inserting in game_sudoku'); print_error('error inserting in game_sudoku');
@ -59,15 +73,16 @@ function game_sudoku_continue( $id, $game, $attempt, $sudoku, $endofgame, $conte
$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;
@ -91,23 +106,24 @@ function game_sudoku_continue( $id, $game, $attempt, $sudoku, $endofgame, $conte
game_sudoku_play( $id, $game, $attempt, $newrec, false, false, $context); game_sudoku_play( $id, $game, $attempt, $newrec, false, false, $context);
} }
function game_sudoku_play( $id, $game, $attempt, $sudoku, $onlyshow, $showsolution, $context) function game_sudoku_play( $id, $game, $attempt, $sudoku, $onlyshow, $showsolution, $context) {
{
$offsetquestions = game_sudoku_compute_offsetquestions( $game->sourcemodule, $attempt, $numbers, $correctquestions); $offsetquestions = game_sudoku_compute_offsetquestions( $game->sourcemodule, $attempt, $numbers, $correctquestions);
if ($game->toptext != '') { if ($game->toptext != '') {
echo $game->toptext.'<br>'; echo $game->toptext.'<br>';
} }
game_sudoku_showsudoku( $sudoku->data, $sudoku->guess, true, $showsolution, $offsetquestions, $correctquestions, $id, $attempt, $game); game_sudoku_showsudoku( $sudoku->data, $sudoku->guess, true, $showsolution, $offsetquestions,
switch( $game->sourcemodule) $correctquestions, $id, $attempt, $game);
{ switch ($game->sourcemodule) {
case 'quiz': case 'quiz':
case 'question': case 'question':
game_sudoku_showquestions_quiz( $id, $game, $attempt, $sudoku, $offsetquestions, $numbers, $correctquestions, $onlyshow, $showsolution, $context); game_sudoku_showquestions_quiz( $id, $game, $attempt, $sudoku, $offsetquestions,
$numbers, $correctquestions, $onlyshow, $showsolution, $context);
break; break;
case 'glossary': case 'glossary':
game_sudoku_showquestions_glossary( $id, $game, $attempt, $sudoku, $offsetquestions, $numbers, $correctquestions, $onlyshow, $showsolution); game_sudoku_showquestions_glossary( $id, $game, $attempt, $sudoku, $offsetquestions,
$numbers, $correctquestions, $onlyshow, $showsolution);
break; break;
} }
@ -116,9 +132,8 @@ function game_sudoku_play( $id, $game, $attempt, $sudoku, $onlyshow, $showsoluti
} }
} }
//returns a map with an offset and id of each question // Returns a map with an offset and id of each question.
function game_sudoku_compute_offsetquestions( $sourcemodule, $attempt, &$numbers, &$correctquestions) function game_sudoku_compute_offsetquestions( $sourcemodule, $attempt, &$numbers, &$correctquestions) {
{
global $CFG, $DB; global $CFG, $DB;
$select = "attemptid = $attempt->id"; $select = "attemptid = $attempt->id";
@ -144,33 +159,32 @@ function game_sudoku_compute_offsetquestions( $sourcemodule, $attempt, &$numbers
foreach ($recs as $rec) { foreach ($recs as $rec) {
$offsetquestions[ $rec->col] = $rec->id2; $offsetquestions[ $rec->col] = $rec->id2;
$numbers[ $rec->id2] = $rec->col; $numbers[ $rec->id2] = $rec->col;
if($rec->score == 1) if ( $rec->score == 1) {
$correctquestions[ $rec->col] = 1; $correctquestions[ $rec->col] = 1;
} }
}
ksort( $offsetquestions); ksort( $offsetquestions);
return $offsetquestions; return $offsetquestions;
} }
function getrandomsudoku() function getrandomsudoku() {
{
global $DB; global $DB;
$count = $DB->count_records( 'game_sudoku_database'); $count = $DB->count_records( 'game_sudoku_database');
if( $count == 0) if ($count == 0) {
{
require_once(dirname(__FILE__) . '/../db/importsudoku.php'); require_once(dirname(__FILE__) . '/../db/importsudoku.php');
$count = $DB->count_records( 'game_sudoku_database'); $count = $DB->count_records( 'game_sudoku_database');
if( $count == 0) if ($count == 0) {
return false; return false;
} }
}
$i = mt_rand( 0, $count - 1); $i = mt_rand( 0, $count - 1);
if( ($recs = $DB->get_records( 'game_sudoku_database', null, '', '*', $i, 1)) != false) if (($recs = $DB->get_records( 'game_sudoku_database', null, '', '*', $i, 1)) != false) {
{
foreach ($recs as $rec) { foreach ($recs as $rec) {
return $rec; return $rec;
} }
@ -179,24 +193,22 @@ function getrandomsudoku()
return false; return false;
} }
function game_sudoku_getclosed( $data) {
function game_sudoku_getclosed( $data)
{
$a = array(); $a = array();
$n = game_strlen( $data); $n = game_strlen( $data);
for( $i=1; $i <= $n; $i++) for ($i = 1; $i <= $n; $i++) {
{
$c = game_substr( $data, $i - 1, 1); $c = game_substr( $data, $i - 1, 1);
if( $c >= "1" and $c <= "9") if ($c >= "1" and $c <= "9") {
$a[ $i] = $i; $a[ $i] = $i;
} }
}
return $a; return $a;
} }
function game_sudoku_showsudoku( $data, $guess, $bShowLegend, $bShowSolution, $offsetquestions, $correctquestions, $id, $attempt, $game) function game_sudoku_showsudoku( $data, $guess, $bshowlegend, $bshowsolution, $offsetquestions,
{ $correctquestions, $id, $attempt, $game) {
global $CFG, $DB; global $CFG, $DB;
$correct = $count = 0; $correct = $count = 0;
@ -204,58 +216,49 @@ function game_sudoku_showsudoku( $data, $guess, $bShowLegend, $bShowSolution, $o
echo "<br>\r\n"; echo "<br>\r\n";
echo '<table border="1" style="border-collapse: separate; border-spacing: 0px;">'; echo '<table border="1" style="border-collapse: separate; border-spacing: 0px;">';
$pos = 0; $pos = 0;
for( $i=0; $i <= 2; $i++) for ($i = 0; $i <= 2; $i++) {
{
echo "<tr>"; echo "<tr>";
for( $j=0; $j <= 2; $j++) for ($j = 0; $j <= 2; $j++) {
{
echo '<td><table border="1" width="100%">'; echo '<td><table border="1" width="100%">';
for( $k1=0; $k1 <= 2; $k1++) for ($k1 = 0; $k1 <= 2; $k1++) {
{
echo "<tr>"; echo "<tr>";
for( $k2=0; $k2 <= 2; $k2++) for ($k2 = 0; $k2 <= 2; $k2++) {
{
$s = substr( $data, $pos, 1); $s = substr( $data, $pos, 1);
$g = substr( $guess, $pos, 1); $g = substr( $guess, $pos, 1);
$pos++; $pos++;
if ($g != 0) { if ($g != 0) {
$s = $g; $s = $g;
} }
if( $s >= "1" and $s <= "9") if ($s >= "1" and $s <= "9") {
{ // Closed number.
//closed number if ($bshowlegend) {
if( $bShowLegend) // Show legend.
{ if ($bshowsolution == false) {
//show legend
if( $bShowSolution == false)
{
if (!array_key_exists( $pos, $correctquestions)) { if (!array_key_exists( $pos, $correctquestions)) {
if( array_key_exists( $pos, $offsetquestions)) if (array_key_exists( $pos, $offsetquestions)) {
{
if ($s != $g) { if ($s != $g) {
$s = '<input type="submit" value="A'.$pos.'" onclick="OnCheck( '.$pos.');" />'; $s = '<input type="submit" value="A'.$pos.'" onclick="OnCheck( '.$pos.');" />';
} }
}else if( $g == 0) } else if ($g == 0) {
{
$s = '<input type="submit" value="" onclick="OnCheck( '.$pos.');" />'; $s = '<input type="submit" value="" onclick="OnCheck( '.$pos.');" />';
} }
}else } else {
{ // Correct question.
//correct question
$count++; $count++;
} }
} }
echo '<td width=33% style="text-align: center; padding: .6em; color: red; font-weight: lighter; font-size: 1em;">'.$s.'</td>'; echo '<td width=33% style="text-align: center; padding: .6em; '.
}else ' color: red; font-weight: lighter; font-size: 1em;">'.$s.'</td>';
{ } else {
//not show legend // Not show legend.
echo '<td width=33% style="text-align: center; padding: .6em; color: red; font-weight: lighter; font-size: 1em;">&nbsp;</td>'; echo '<td width=33% style="text-align: center; padding: .6em;'.
' color: red; font-weight: lighter; font-size: 1em;">&nbsp;</td>';
} }
}else } else {
{
$s = strpos( "-ABCDEFGHI", $s); $s = strpos( "-ABCDEFGHI", $s);
$count++; $count++;
echo '<td width=33% style="text-align: center; padding: .6em; color: black; font-weight: lighter; font-size: 1em;">'.$s.'</td>'; echo '<td width=33% style="text-align: center; padding: .6em; '.
' color: black; font-weight: lighter; font-size: 1em;">'.$s.'</td>';
} }
} }
echo "</tr>"; echo "</tr>";
@ -265,6 +268,7 @@ function game_sudoku_showsudoku( $data, $guess, $bShowLegend, $bShowSolution, $o
echo "</tr>"; echo "</tr>";
} }
echo "</table>\r\n"; echo "</table>\r\n";
$href = $CFG->wwwroot.'/mod/game/attempt.php?action=sudokucheckn&id='.$id;
?> ?>
<script language="javascript"> <script language="javascript">
@ -277,12 +281,12 @@ function game_sudoku_showsudoku( $data, $guess, $bShowLegend, $bShowSolution, $o
if (s > "9") if (s > "9")
return; return;
window.location.href = "<?php echo $CFG->wwwroot.'/mod/game/attempt.php?action=sudokucheckn&id='.$id ?>&pos=" + pos + "&num=" + s; window.location.href = "<?php echo $href; ?>&pos=" + pos + "&num=" + s;
} }
</script> </script>
<?php <?php
//Here are the congratulations // Here are the congratulations.
if ($attempt->timefinish) { if ($attempt->timefinish) {
return $count; return $count;
} }
@ -297,7 +301,8 @@ function game_sudoku_showsudoku( $data, $guess, $bShowLegend, $bShowSolution, $o
echo '<B><br>'.get_string( 'win', 'game').'</B><BR>'; echo '<B><br>'.get_string( 'win', 'game').'</B><BR>';
echo '<br>'; echo '<br>';
echo "<a href=\"$CFG->wwwroot/mod/game/attempt.php?id=$id\">".get_string( 'nextgame', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp; '; echo "<a href=\"$CFG->wwwroot/mod/game/attempt.php?id=$id\">".
get_string( 'nextgame', 'game').'</a> &nbsp; &nbsp; &nbsp; &nbsp; ';
echo "<a href=\"$CFG->wwwroot/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'</a> '; echo "<a href=\"$CFG->wwwroot/course/view.php?id=$cm->course\">".get_string( 'finish', 'game').'</a> ';
game_updateattempts( $game, $attempt, 1, 1); game_updateattempts( $game, $attempt, 1, 1);
@ -305,9 +310,7 @@ function game_sudoku_showsudoku( $data, $guess, $bShowLegend, $bShowSolution, $o
return $count; return $count;
} }
function game_sudoku_getquestionlist( $offsetquestions) {
function game_sudoku_getquestionlist( $offsetquestions)
{
$questionlist = ''; $questionlist = '';
foreach ($offsetquestions as $q) { foreach ($offsetquestions as $q) {
if ($q != 0) { if ($q != 0) {
@ -323,8 +326,7 @@ function game_sudoku_getquestionlist( $offsetquestions)
return $questionlist; return $questionlist;
} }
function game_sudoku_getglossaryentries( $game, $offsetentries, &$entrylist, $numbers) function game_sudoku_getglossaryentries( $game, $offsetentries, &$entrylist, $numbers) {
{
global $DB; global $DB;
$entrylist = implode( ',', $offsetentries); $entrylist = implode( ',', $offsetentries);
@ -333,7 +335,7 @@ function game_sudoku_getglossaryentries( $game, $offsetentries, &$entrylist, $nu
print_error( get_string( 'sudoku_noentriesfound', 'game')); print_error( get_string( 'sudoku_noentriesfound', 'game'));
} }
// Load the questions // Load the questions.
if (!$entries = $DB->get_records_select( 'glossary_entries', "id IN ($entrylist)")) { if (!$entries = $DB->get_records_select( 'glossary_entries', "id IN ($entrylist)")) {
print_error( get_string('sudoku_noentriesfound', 'game')); print_error( get_string('sudoku_noentriesfound', 'game'));
} }
@ -341,14 +343,14 @@ function game_sudoku_getglossaryentries( $game, $offsetentries, &$entrylist, $nu
return $entries; return $entries;
} }
function game_sudoku_showquestions_quiz( $id, $game, $attempt, $sudoku, $offsetquestions, $numbers, $correctquestions, $onlyshow, $showsolution, $context) function game_sudoku_showquestions_quiz( $id, $game, $attempt, $sudoku, $offsetquestions, $numbers,
{ $correctquestions, $onlyshow, $showsolution, $context) {
global $CFG; global $CFG;
$questionlist = game_sudoku_getquestionlist( $offsetquestions); $questionlist = game_sudoku_getquestionlist( $offsetquestions);
$questions = game_sudoku_getquestions( $questionlist); $questions = game_sudoku_getquestions( $questionlist);
//I will sort with the number of each question // I will sort with the number of each question.
$questions2 = array(); $questions2 = array();
foreach ($questions as $q) { foreach ($questions as $q) {
$ofs = $numbers[ $q->id]; $ofs = $numbers[ $q->id];
@ -366,14 +368,14 @@ function game_sudoku_showquestions_quiz( $id, $game, $attempt, $sudoku, $offsetq
foreach ($questions2 as $question) { foreach ($questions2 as $question) {
$ofs = $numbers[ $question->id]; $ofs = $numbers[ $question->id];
if (array_key_exists( $ofs, $correctquestions)) { if (array_key_exists( $ofs, $correctquestions)) {
continue; //I don't show the correct answers continue; // I don't show the correct answers.
} }
if( $found == false) if ( $found == false) {
{
$found = true; $found = true;
// Start the form // Start the form.
echo "<form id=\"responseform\" method=\"post\" action=\"{$CFG->wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n"; echo "<form id=\"responseform\" method=\"post\" ".
"action=\"{$CFG->wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n";
if (($onlyshow === false) and ($showsolution === false)) { if (($onlyshow === false) and ($showsolution === false)) {
echo "<br><center><input type=\"submit\" name=\"submit\" value=\"".get_string('sudoku_submit', 'game')."\">"; echo "<br><center><input type=\"submit\" name=\"submit\" value=\"".get_string('sudoku_submit', 'game')."\">";
@ -381,14 +383,14 @@ function game_sudoku_showquestions_quiz( $id, $game, $attempt, $sudoku, $offsetq
get_string('sudoku_finishattemptbutton', 'game')."\">"; get_string('sudoku_finishattemptbutton', 'game')."\">";
} }
// Add a hidden field with the quiz id // Add a hidden field with the quiz id.
echo '<div>'; echo '<div>';
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="sudokucheck" />'; echo '<input type="hidden" name="action" value="sudokucheck" />';
// Print all the questions // Print all the questions.
// Add a hidden field with questionids // Add a hidden field with questionids.
echo '<input type="hidden" name="questionids" value="'.$questionlist."\" />\n"; echo '<input type="hidden" name="questionids" value="'.$questionlist."\" />\n";
} }
@ -397,11 +399,10 @@ function game_sudoku_showquestions_quiz( $id, $game, $attempt, $sudoku, $offsetq
game_print_question( $game, $question, $context); game_print_question( $game, $question, $context);
} }
if( $found) if ($found) {
{
echo "</div>"; echo "</div>";
// Finish the form // Finish the form.
echo '</div>'; echo '</div>';
if (($onlyshow === false) and ($showsolution === false)) { if (($onlyshow === false) and ($showsolution === false)) {
echo "<center><input type=\"submit\" name=\"submit\" value=\"".get_string('sudoku_submit', 'game')."\"></center>\n"; echo "<center><input type=\"submit\" name=\"submit\" value=\"".get_string('sudoku_submit', 'game')."\"></center>\n";
@ -411,14 +412,14 @@ function game_sudoku_showquestions_quiz( $id, $game, $attempt, $sudoku, $offsetq
} }
} }
//show the sudoku and glossaryentries // Show the sudoku and glossaryentries.
function game_sudoku_showquestions_glossary( $id, $game, $attempt, $sudoku, $offsetentries, $numbers, $correctentries, $onlyshow, $showsolution) function game_sudoku_showquestions_glossary( $id, $game, $attempt, $sudoku, $offsetentries, $numbers,
{ $correctentries, $onlyshow, $showsolution) {
global $CFG; global $CFG;
$entries = game_sudoku_getglossaryentries( $game, $offsetentries, $questionlist, $numbers); $entries = game_sudoku_getglossaryentries( $game, $offsetentries, $questionlist, $numbers);
//I will sort with the number of each question // I will sort with the number of each question.
$entries2 = array(); $entries2 = array();
foreach ($entries as $q) { foreach ($entries as $q) {
$ofs = $numbers[ $q->id]; $ofs = $numbers[ $q->id];
@ -431,33 +432,35 @@ function game_sudoku_showquestions_glossary( $id, $game, $attempt, $sudoku, $off
return; return;
} }
/// Start the form // Start the form.
echo "<br><form id=\"responseform\" method=\"post\" action=\"{$CFG->wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n"; echo "<br><form id=\"responseform\" method=\"post\" ".
"action=\"{$CFG->wwwroot}/mod/game/attempt.php\" onclick=\"this.autocomplete='off'\">\n";
if( $onlyshow) if ($onlyshow) {
$hasquestions = false; $hasquestions = false;
else } else {
$hasquestions = ( count($correctentries) < count( $entries2)); $hasquestions = ( count($correctentries) < count( $entries2));
}
if ($hasquestions) { if ($hasquestions) {
echo "<center><input type=\"submit\" name=\"submit\" value=\"".get_string('sudoku_submit', 'game')."\"></center>\n"; echo "<center><input type=\"submit\" name=\"submit\" value=\"".get_string('sudoku_submit', 'game')."\"></center>\n";
} }
// Add a hidden field with the quiz id // Add a hidden field with the quiz id.
echo '<div>'; echo '<div>';
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="sudokucheckg" />'; echo '<input type="hidden" name="action" value="sudokucheckg" />';
/// Print all the questions // Print all the questions.
// Add a hidden field with questionids // Add a hidden field with questionids.
echo '<input type="hidden" name="questionids" value="'.$questionlist."\" />\n"; echo '<input type="hidden" name="questionids" value="'.$questionlist."\" />\n";
$number = 0; $number = 0;
foreach ($entries2 as $entry) { foreach ($entries2 as $entry) {
$ofs = $numbers[ $entry->id]; $ofs = $numbers[ $entry->id];
if (array_key_exists( $ofs, $correctentries)) { if (array_key_exists( $ofs, $correctentries)) {
continue; //I don't show the correct answers continue; // I don't show the correct answers.
} }
$query = new StdClass; $query = new StdClass;
@ -476,7 +479,7 @@ function game_sudoku_showquestions_glossary( $id, $game, $attempt, $sudoku, $off
echo "</div>"; echo "</div>";
// Finish the form // Finish the form.
if ($hasquestions) { if ($hasquestions) {
echo "<center><input type=\"submit\" name=\"submit\" value=\"".get_string('sudoku_submit', 'game')."\"></center>\n"; echo "<center><input type=\"submit\" name=\"submit\" value=\"".get_string('sudoku_submit', 'game')."\"></center>\n";
} }
@ -484,27 +487,25 @@ function game_sudoku_showquestions_glossary( $id, $game, $attempt, $sudoku, $off
echo "</form>\n"; echo "</form>\n";
} }
function game_sudoku_showquestion_onfinish( $id, $game, $attempt, $sudoku) function game_sudoku_showquestion_onfinish( $id, $game, $attempt, $sudoku) {
{
if (!set_field( 'game_attempts', 'finish', 1, 'id', $attempt->id)) { if (!set_field( 'game_attempts', 'finish', 1, 'id', $attempt->id)) {
print_error( "game_sudoku_showquestion_onfinish: Can't update game_attempts id=$attempt->id"); print_error( "game_sudoku_showquestion_onfinish: Can't update game_attempts id=$attempt->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\">".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; ';
echo "<a href=\"{$CFG->wwwroot}?id=$id\">".get_string( 'finish', 'game').'</a> '; echo "<a href=\"{$CFG->wwwroot}?id=$id\">".get_string( 'finish', 'game').'</a> ';
} }
function game_sudoku_checkanswers() function game_sudoku_checkanswers() {
{
$responses = data_submitted(); $responses = data_submitted();
$actions = question_extract_responses($questions, $responses, $event); $actions = question_extract_responses($questions, $responses, $event);
} }
function game_sudoku_check_questions( $id, $game, $attempt, $sudoku, $finishattempt, $course) function game_sudoku_check_questions( $id, $game, $attempt, $sudoku, $finishattempt, $course) {
{
global $QTYPES, $DB; global $QTYPES, $DB;
$responses = data_submitted(); $responses = data_submitted();
@ -528,34 +529,33 @@ function game_sudoku_check_questions( $id, $game, $attempt, $sudoku, $finishatte
$grade = game_grade_responses( $question, $responses, 100, $answertext); $grade = game_grade_responses( $question, $responses, 100, $answertext);
if ($grade < 99) { if ($grade < 99) {
//wrong answer // Wrong answer.
game_update_queries( $game, $attempt, $query, $grade / 100, $answertext); game_update_queries( $game, $attempt, $query, $grade / 100, $answertext);
continue; continue;
} }
//correct answer // Correct answer.
game_update_queries( $game, $attempt, $query, 1, $answertext); game_update_queries( $game, $attempt, $query, 1, $answertext);
} }
game_sudoku_check_last( $id, $game, $attempt, $sudoku, $finishattempt, $course); game_sudoku_check_last( $id, $game, $attempt, $sudoku, $finishattempt, $course);
} }
function game_sudoku_check_glossaryentries( $id, $game, $attempt, $sudoku, $finishattempt, $course) function game_sudoku_check_glossaryentries( $id, $game, $attempt, $sudoku, $finishattempt, $course) {
{
global $QTYPES, $DB; global $QTYPES, $DB;
$responses = data_submitted(); $responses = data_submitted();
//this function returns offsetentries, numbers, correctquestions // This function returns offsetentries, numbers, correctquestions.
$offsetentries = game_sudoku_compute_offsetquestions( $game->sourcemodule, $attempt, $numbers, $correctquestions); $offsetentries = game_sudoku_compute_offsetquestions( $game->sourcemodule, $attempt, $numbers, $correctquestions);
$entrieslist = game_sudoku_getquestionlist( $offsetentries ); $entrieslist = game_sudoku_getquestionlist( $offsetentries );
// Load the glossary entries // Load the glossary entries.
if (!($entries = $DB->get_records_select( 'glossary_entries', "id IN ($entrieslist)"))) { if (!($entries = $DB->get_records_select( 'glossary_entries', "id IN ($entrieslist)"))) {
print_error( get_string('noglossaryentriesfound', 'game')); print_error( get_string('noglossaryentriesfound', 'game'));
} }
foreach($entries as $entry) { foreach (entries as $entry) {
$answerundefined = optional_param('resp'.$entry->id, 'undefined', PARAM_TEXT); $answerundefined = optional_param('resp'.$entry->id, 'undefined', PARAM_TEXT);
if ($answerundefined == 'undefined') { if ($answerundefined == 'undefined') {
continue; continue;
@ -567,10 +567,11 @@ function game_sudoku_check_glossaryentries( $id, $game, $attempt, $sudoku, $fini
if (game_upper( $entry->concept) != game_upper( $answer)) { if (game_upper( $entry->concept) != game_upper( $answer)) {
continue; continue;
} }
//correct answer // Correct answer.
$select = "attemptid=$attempt->id"; $select = "attemptid=$attempt->id";
$select .= " AND glossaryentryid=$entry->id AND col>0"; $select .= " AND glossaryentryid=$entry->id AND col>0";
$select .= " AND questiontext is null"; // check the student guesses not source glossary entry. // Check the student guesses not source glossary entry.
$select .= " AND questiontext is null";
$query = new stdClass(); $query = new stdClass();
if (($query->id = $DB->get_field_select( 'game_queries', 'id', $select)) == 0) { if (($query->id = $DB->get_field_select( 'game_queries', 'id', $select)) == 0) {
@ -586,10 +587,8 @@ function game_sudoku_check_glossaryentries( $id, $game, $attempt, $sudoku, $fini
return true; return true;
} }
// This is the last function after submiting the answers.
//this is the last function after submiting the answers. function game_sudoku_check_last( $id, $game, $attempt, $sudoku, $finishattempt, $course) {
function game_sudoku_check_last( $id, $game, $attempt, $sudoku, $finishattempt, $course)
{
global $CFG, $DB; global $CFG, $DB;
$correct = $DB->get_field_select( 'game_queries', 'COUNT(*) AS c', "attemptid=$attempt->id AND score > 0.9"); $correct = $DB->get_field_select( 'game_queries', 'COUNT(*) AS c', "attemptid=$attempt->id AND score > 0.9");
@ -597,21 +596,18 @@ function game_sudoku_check_last( $id, $game, $attempt, $sudoku, $finishattempt,
if ($all) { if ($all) {
$grade = $correct / $all; $grade = $correct / $all;
}else } else {
{
$grade = 0; $grade = 0;
} }
game_updateattempts( $game, $attempt, $grade, $finishattempt); game_updateattempts( $game, $attempt, $grade, $finishattempt);
} }
function game_sudoku_check_number( $id, $game, $attempt, $sudoku, $pos, $num, $context) function game_sudoku_check_number( $id, $game, $attempt, $sudoku, $pos, $num, $context) {
{
global $DB; global $DB;
$correct = game_substr( $sudoku->data, $pos - 1, 1); $correct = game_substr( $sudoku->data, $pos - 1, 1);
if( $correct != $num) if ($correct != $num) {
{
game_sudoku_play( $id, $game, $attempt, $sudoku, false, false, $context); game_sudoku_play( $id, $game, $attempt, $sudoku, false, false, $context);
return; return;
} }

421
sudoku/sdd/class.SDD.php

@ -1,4 +1,18 @@
<?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/>.
/** /**
* Dump structured data, i.e., Objects and Arrays, in either plain text or * Dump structured data, i.e., Objects and Arrays, in either plain text or
@ -43,27 +57,26 @@
// Add a function to generate "newlines". // Add a function to generate "newlines".
// //
class SDD class sdd {
{ /*
/**
* HTML to be generated flag. * HTML to be generated flag.
*/ */
var $m_htmlFlag ; protected $m_htmlflag;
/** /*
* logging flag. * logging flag.
*/ */
var $m_logging = false ; protected $m_logging = false;
/** /*
* In memory log file. * In memory log file.
*/ */
var $m_log = array() ; protected $m_log = array();
/** /*
* Constructor. * Constructor.
* *
* @access public * @access public
@ -75,29 +88,26 @@ class SDD
* this object. By default, logging is off. * this object. By default, logging is off.
*/ */
function SDD($theHtmlFlag=null, $theLoggingFlag=false) public function sdd($thehtmlflag = null, $theloggingflag = false) {
{ if ($thehtmlflag === null) {
if ($theHtmlFlag === null) $thehtmlflag = (!empty($_SERVER['DOCUMENT_ROOT']));
{
$theHtmlFlag = (!empty($_SERVER['DOCUMENT_ROOT'])) ;
} }
$this->m_htmlFlag = $theHtmlFlag ; $this->m_htmlflag = $thehtmlflag;
$this->m_logging = $theLoggingFlag ; $this->m_logging = $theloggingflag;
} }
/** /*
* Close the log file. * Close the log file.
* *
* @access public * @access public
* @abstract * @abstract
*/ */
function close() public function close() {
{
} }
/** /*
* Dump a structured variable. * Dump a structured variable.
* *
* @static * @static
@ -108,48 +118,30 @@ class SDD
* @return string The data to be displayed. * @return string The data to be displayed.
* @link http://www.php.net/manual/en/reserved.variables.php#reserved.variables.server Uses $_SERVER * @link http://www.php.net/manual/en/reserved.variables.php#reserved.variables.server Uses $_SERVER
*/ */
public function dump(&$thevariable, $thehtmlflag = null) {
function dump(&$theVariable, $theHtmlFlag=null) if ($thehtmlflag === null) {
{ if (empty($this)) {
if ($theHtmlFlag === null) $thehtmlflag = (!empty($_SERVER['DOCUMENT_ROOT']));
{ } else {
if (empty($this)) if (is_subclass_of($this, "sdd")) {
{ $thehtmlflag = $this->m_htmlflag;
$theHtmlFlag = (!empty($_SERVER['DOCUMENT_ROOT'])) ; } else {
} $thehtmlflag = (!empty($_SERVER['DOCUMENT_ROOT']));
else
{
if (is_subclass_of($this, "sdd"))
{
$theHtmlFlag = $this->m_htmlFlag ;
}
else
{
$theHtmlFlag = (!empty($_SERVER['DOCUMENT_ROOT'])) ;
} }
} }
} }
switch (gettype($theVariable)) switch (gettype($thevariable)) {
{
case 'array': case 'array':
{ return SDD::dArray($thevariable, $thehtmlflag);
return SDD::dArray($theVariable, $theHtmlFlag) ;
}
case 'object': case 'object':
{ return SDD::dObject($thevariable, $thehtmlflag);
return SDD::dObject($theVariable, $theHtmlFlag) ;
}
default: default:
{ return SDD::scalar($thevariable, $thehtmlflag);
return SDD::scalar($theVariable, $theHtmlFlag) ;
}
} }
} }
/** /*
* Dump the contents of an array. * Dump the contents of an array.
* *
* @param array $theArray the array whose contents are to be displayed. * @param array $theArray the array whose contents are to be displayed.
@ -160,58 +152,45 @@ class SDD
* @return string The display form of the array. * @return string The display form of the array.
*/ */
function dArray(&$theArray, $theHTMLFlag, $theIndent = "") public function darray(&$thearray, $thehtmlflag, $theindent = "") {
{ $theoutput = array();
$theOutput = array() ;
foreach($theArray as $theIndex => $theValue) foreach ($thearray as $theindex => $thevalue) {
{ if (is_array($thevalue)) {
if (is_array($theValue)) $thestring = ssd::dArray($thevalue, $thehtmlflag, $theindent . " ");
{ $theoutput[$theindex] = substr($thestring, 0, strlen($thestring) - 1);
$theString = SDD::dArray($theValue, $theHTMLFlag, $theIndent . " ") ; } else if (is_object($thevalue)) {
$theOutput[$theIndex] = substr($theString, 0, strlen($theString) - 1) ; $theoutput[$theindex] = sdd::dobject($thevalue, $thehtmlflag);
} } else {
else if (is_object($theValue)) $theoutput[$theindex] = ($thehtmlflag ? preg_replace('|<|s', '&lt;',
{ var_export($thevalue, true)) : var_export($thevalue, true));
$theOutput[$theIndex] = SDD::dObject($theValue, $theHTMLFlag) ;
}
else
{
$theOutput[$theIndex] = ($theHTMLFlag ?
preg_replace('|<|s', '&lt;', var_export($theValue, true)) :
var_export($theValue, true)) ;
} }
} }
if ($theHTMLFlag) if ($thehtmlflag) {
{ $thestring = "<table border=1>\n";
$theString = "<table border=1>\n" ; $thestring .= "<tr><td align=left>Array (</td></tr>\n";
$theString .= "<tr><td align=left>Array (</td></tr>\n" ;
foreach ($theOutput as $theIndex => $theVariableOutput) foreach ($theoutput as $theindex => $thevariableoutput) {
{ $thestring .= "<tr>\n<td align=right>$theindex = ></td><td align=left>\n$thevariableoutput\n</td>\n</tr>\n";
$theString .= "<tr>\n<td align=right>$theIndex = ></td><td align=left>\n$theVariableOutput\n</td>\n</tr>\n" ;
} }
$theString .= "<tr><td align=left>)</td></tr>\n" ; $thestring .= "<tr><td align=left>)</td></tr>\n";
$theString .= "</table>\n" ; $thestring .= "</table>\n";
} } else {
else $thestring = "Array\n$theindent(\n";
{
$theString = "Array\n$theIndent(\n" ;
foreach ($theOutput as $theIndex => $theVariableOutput) foreach ($theoutput as $theindex => $thevariableoutput) {
{ $thestring .= "$theindent [$theindex] => " . $thevariableoutput . "\n";
$theString .= "$theIndent [$theIndex] => " . $theVariableOutput . "\n" ;
} }
$theString .= "$theIndent)\n" ; $thestring .= "$theindent)\n";
} }
return $theString ; return $thestring;
} }
/** /*
* Dump the contents of an object. * Dump the contents of an object.
* *
* Provide a structured display of an object and all the * Provide a structured display of an object and all the
@ -224,139 +203,112 @@ class SDD
* @return string the display form of the object. * @return string the display form of the object.
*/ */
function dObject(&$theObject, $theHTMLFlag) public function dobject(&$theobject, $thehtmlflag) {
{ $theobjectvars = get_object_vars($theobject);
$theObjectVars = get_object_vars($theObject) ;
// /* Get the inheritance tree starting with the object and going
// Get the inheritance tree starting with the object and going * through all the parent classes from there.
// through all the parent classes from there. */
//
$theClass = get_class($theObject) ; $theclass = get_class($theobject);
$theClasses[] = $theClass ; $theclasses[] = $theclass;
while ($theClass = get_parent_class($theClass)) while ($theclass = get_parent_class($theclass)) {
{ $theclasses[] = $theclass;
$theClasses[] = $theClass ;
} }
// /* Get all the class variables for each class in the inheritance
// Get all the class variables for each class in the inheritance * tree. There will be some duplication, but we'll sort that out
// tree. There will be some duplication, but we'll sort that out * in the output process.
// in the output process. */
//
foreach($theClasses as $theClass) foreach ($theclasses as $theclass) {
{ $theclassvars[$theclass] = get_class_vars($theclass);
$theClassVars[$theClass] = get_class_vars($theClass) ;
} }
// /* Put the inheritance tree from base class to most derived order
// Put the inheritance tree from base class to most derived order * (this is how we get rid of duplication of the variable names)
// (this is how we get rid of duplication of the variable names) * Go through the object variables starting with the base class,
// Go through the object variables starting with the base class, * capture the output and delete the variable from the object
// capture the output and delete the variable from the object * variables.
// variables. */
//
$theClasses = array_reverse($theClasses) ; $theclasses = array_reverse($theclasses);
$theOutput = array() ; $theoutput = array();
foreach ($theClasses as $theClass) foreach ($theclasses as $theclass) {
{ $theoutput[$theclass] = array();
$theOutput[$theClass] = array() ;
foreach ($theClassVars[$theClass] as $theVariable => $value) foreach ($theclassvars[$theclass] as $thevariable => $value) {
{ if (array_key_exists($thevariable, $theobjectvars)) {
if (array_key_exists($theVariable, $theObjectVars)) if (is_array($theobjectvars[$thevariable])) {
{ $theoutput[$theclass][] = $thevariable . " = " . sdd::darray($theobjectvars[$thevariable], $thehtmlflag);
if (is_array($theObjectVars[$theVariable])) } else if (is_object($theobjectvars[$thevariable])) {
{ $theoutput[$theclass][] = $thevariable . " = " . sdd::dobject($theobjectvars[$thevariable], $thehtmlflag);
$theOutput[$theClass][] = $theVariable . " = " . SDD::dArray($theObjectVars[$theVariable], $theHTMLFlag) ; } else {
} $theotput[$theclass][] = $thevariable . " = " .
else if (is_object($theObjectVars[$theVariable])) ($thehtmlflag ? preg_replace('|<|s', '&lt;', var_export(
{ $theobjectvars[$thevariable], true)) : var_export($theobjectvars[$thevariable], true));
$theOutput[$theClass][] = $theVariable . " = " . SDD::dObject($theObjectVars[$theVariable], $theHTMLFlag) ;
}
else
{
$theOutput[$theClass][] =
$theVariable . " = " .
($theHTMLFlag ?
preg_replace('|<|s', '&lt;', var_export($theObjectVars[$theVariable], true)) :
var_export($theObjectVars[$theVariable], true)) ;
} }
unset($theObjectVars[$theVariable]) ; unset($theobjectvars[$thevariable]);
} }
} }
} }
// /* Put the classes back in most derived order for generating printable
// Put the classes back in most derived order for generating printable * output.
// output. */
// $theclasses = array_reverse($theclasses);
$theClasses = array_reverse($theClasses) ;
if ($theHTMLFlag) if ($thehtmlflag) {
{ $thestring = "<table>\n<thead>\n";
$theString = "<table>\n<thead>\n" ;
foreach ($theClasses as $theClass) foreach ($theclasses as $theclass) {
{ $thestring .= "<th>\n$theclass\n</th>\n";
$theString .= "<th>\n$theClass\n</th>\n" ;
} }
$theString .= "</thead>\n<tr valign=top>\n" ; $thestring .= "</thead>\n<tr valign=top>\n";
foreach ($theClasses as $theClass)
{
$theString .= "<td>\n<table border=1>\n" ;
foreach ($theOutput[$theClass] as $theVariableOutput) foreach ($theclasses as $theclass) {
{ $thestring .= "<td>\n<table border=1>\n";
$theString .= "<tr>\n<td>\n$theVariableOutput\n</td>\n</tr>\n" ;
}
$theString .= "</table>\n</td>\n" ; foreach ($theoutput[$theclass] as $thevariableoutput) {
$thestring .= "<tr>\n<td>\n$thevariableoutput\n</td>\n</tr>\n";
} }
$theString .= "</tr>\n</table>\n" ; $thestring .= "</table>\n</td>\n";
} }
else
{
$classIndent = "" ; $thestring .= "</tr>\n</table>\n";
} else {
$classindent = "";
$classDataIndent = " " ; $classdataindent = " ";
$theString = "" ; $thestring = "";
foreach ($theClasses as $theClass) foreach ($theclasses as $theclass) {
{ $thestring .= "{$classindent}class $theclass\n\n";
$theString .= "{$classIndent}class $theClass\n\n" ;
foreach ($theOutput[$theClass] as $theVariableOutput) foreach ($theoutput[$theclass] as $thevariableoutput) {
{ $thestring .= "$classdataindent$thevariableoutput\n";
$theString .= "$classDataIndent$theVariableOutput\n" ;
} }
$theString .= "\n" ; $thestring .= "\n";
$classIndent .= " " ; $classindent .= " ";
$classDataIndent .= " " ; $classdataindent .= " ";
} }
} }
return $theString ; return $thestring;
} }
/** /*
* Write a debugging value to a log file. * Write a debugging value to a log file.
* *
* @access public * @access public
@ -367,35 +319,27 @@ class SDD
* stamp. * stamp.
*/ */
function log(&$theData, $theHeader=null) public function log(&$thedata, $theheader = null) {
{ $theheader = date('[Y-m-d H:i:s]: ') . $theheader;
$theHeader = date('[Y-m-d H:i:s]: ') . $theHeader ;
if ($this->m_logging) if ($this->m_logging) {
{ if ($this->m_htmlflag) {
if ($this->m_htmlFlag) $xxx = $this->dump($thedata);
{ if (substr($xxx, 0, 5) == '<pre>') {
$xxx = $this->dump($theData) ; $xxx = '<pre>' . $theheader . substr($xxx, 5);
if (substr($xxx, 0, 5) == '<pre>') } else {
{ $xxx = $theheader . $xxx;
$xxx = '<pre>' . $theHeader . substr($xxx, 5) ;
}
else
{
$xxx = $theHeader . $xxx ;
} }
$this->writeLog($xxx); $this->writeLog($xxx);
} } else {
else $xxx = $theheader . $this->dump($thedata);
{ $this->writelog($xxx);
$xxx = $theHeader . $this->dump($theData) ;
$this->writeLog($xxx) ;
} }
} }
} }
/** /*
* @desc Generate context specific new line equivalents. * @desc Generate context specific new line equivalents.
* @param integer [optional] the number of newlines. * @param integer [optional] the number of newlines.
* @param boolean [optional] true if generating html newlines. * @param boolean [optional] true if generating html newlines.
@ -403,57 +347,42 @@ class SDD
* @access public * @access public
*/ */
function newline($theCount=1, $theHtmlFlag=null) public function newline($thecount = 1, $thehtmlflag = null) {
{ if ($thehtmlflag === null) {
if ($theHtmlFlag === null) if (empty($this)) {
{ $thehtmlflag = (!empty($_SERVER['DOCUMENT_ROOT']));
if (empty($this)) } else {
{ if (is_subclass_of($this, "sdd")) {
$theHtmlFlag = (!empty($_SERVER['DOCUMENT_ROOT'])) ; $thehtmlflag = $this->m_htmlflag;
} } else {
else $thehtmlflag = (!empty($_SERVER['DOCUMENT_ROOT']));
{
if (is_subclass_of($this, "sdd"))
{
$theHtmlFlag = $this->m_htmlFlag ;
}
else
{
$theHtmlFlag = (!empty($_SERVER['DOCUMENT_ROOT'])) ;
} }
} }
} }
if ($theHtmlFlag) if ($thehtmlflag) {
{ return str_repeat("<br />", max($thecount, 0)) . "\n";
return str_repeat("<br />", max($theCount, 0)) . "\n" ; } else {
} return str_repeat("\n", max($thecount, 0));
else
{
return str_repeat("\n", max($theCount, 0)) ;
} }
} }
/** /*
* Dump any scalar value * Dump any scalar value
* *
* @param mixed $theVariable the variable to be dumped. * @param mixed $theVariable the variable to be dumped.
* @param boolean $theHtmlFlag true if html is to be generated. * @param boolean $theHtmlFlag true if html is to be generated.
*/ */
function scalar(&$theVariable, $theHtmlFlag) public function scalar(&$thevariable, $thehtmlflag) {
{ if ($thehtmlflag) {
if ($theHtmlFlag) return "<pre>" . preg_replace('|<|s', '&lt;', var_export($thevariable, true)) . "</pre>";
{ } else {
return "<pre>" . preg_replace('|<|s', '&lt;', var_export($theVariable, true)) . "</pre>" ; return var_export($thevariable, true);
}
else
{
return var_export($theVariable, true) ;
} }
} }
/** /*
* Write data to the log file. * Write data to the log file.
* *
* @access public * @access public
@ -463,33 +392,29 @@ class SDD
* @return integer the number of bytes written into the log file. * @return integer the number of bytes written into the log file.
*/ */
function writeLog(&$theData) public function writelog(&$thedata) {
{ return strlen($this->m_log[] = $thedata);
return strlen($this->m_log[] = $theData) ;
} }
/** /*
* Return the state of the logging flag. * Return the state of the logging flag.
* *
* @access public * @access public
* @return boolean * @return boolean
*/ */
function getLogging() public function getlogging() {
{
return $this->m_logging; return $this->m_logging;
} }
/** /*
* Set the state of the logging flag. * Set the state of the logging flag.
* *
* @access public * @access public
* @return boolean * @return boolean
*/ */
function setLogging($theLogging=false) public function setlogging($thelogging=false) {
{ $this->m_logging = $thelogging;
$this->m_logging = $theLogging ;
} }
} }
?>

64
sudoku/sdd/class.logfile.php

@ -1,4 +1,18 @@
<?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/>.
/** /**
* @author Dick Munroe <munroe@csworks.com> * @author Dick Munroe <munroe@csworks.com>
@ -8,50 +22,44 @@
* @version 1.0.1 * @version 1.0.1
*/ */
// /*
// Edit History: * Edit History:
// *
// Dick Munroe munroe@cworks.com 23-Dec-2004 * Dick Munroe munroe@cworks.com 23-Dec-2004
// Initial version created/ * Initial version created/
// */
include_once('SDD/class.SDD.php') ; require_once('SDD/class.SDD.php');
class logfile extends SDD class logfile extends SDD {
{
/** /*
* The open file handle. * The open file handle.
* *
* @access private * @access private
*/ */
var $m_handle ; protected $m_handle;
/** /*
* Constructor * Constructor
* *
* @access public * @access public
*/ */
function logfile($theFileName) public function logfile($thefilename) {
{ if (file_exists($thefilename)) {
if (file_exists($theFileName)) $this->m_handle = fopen($thefilename, 'a');
{ } else {
$this->m_handle = fopen($theFileName, 'a') ; $this->m_handle = fopen($thefilename, 'w');
}
else
{
$this->m_handle = fopen($theFileName, 'w') ;
} }
} }
function close() public function close() {
{
fclose($this->m_handle); fclose($this->m_handle);
} }
/** /*
* Write a debugging value to a log file. * Write a debugging value to a log file.
* *
* @access public * @access public
@ -60,10 +68,8 @@ class logfile extends SDD
* @return integer number of bytes written to the log. * @return integer number of bytes written to the log.
*/ */
function log(&$theData) public function log(&$thedata) {
{ return fwrite($this->m_handle, date('[Y-m-d H:i:s]: ') . $this->dump($thedata) . "\n");
return fwrite($this->m_handle, date('[Y-m-d H:i:s]: ') . $this->dump($theData) . "\n") ;
} }
} }
?>

Loading…
Cancel
Save