@ -1,4 +1,19 @@
<?php // $Id: exporthtml.php,v 1.23 2012/07/25 11:16:03 bdaloukas Exp $
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see < http: / / www . gnu . org / licenses / > .
/**
/**
* This page export the game to html for games: cross, hangman
* This page export the game to html for games: cross, hangman
*
*
@ -10,30 +25,30 @@
require_once( "locallib.php");
require_once( "locallib.php");
require_once( "exportjavame.php");
require_once( "exportjavame.php");
function game_OnExportHTML( $game, $context, $html) {
function game_onexporthtml( $game, $context, $html) {
global $CFG;
global $CFG;
$destdir = game_export_createtempdir();
$destdir = game_export_createtempdir();
switch( $game->gamekind) {
switch( $game->gamekind) {
case 'cross';
case 'cross';
game_OnExportHTML _cross( $game, $context, $html, $destdir);
game_onexporthtml _cross( $game, $context, $html, $destdir);
break;
break;
case 'hangman':
case 'hangman':
game_OnExportHTML _hangman( $game, $context, $html, $destdir);
game_onexporthtml _hangman( $game, $context, $html, $destdir);
break;
break;
case 'snakes':
case 'snakes':
game_OnExportHTML _snakes( $game, $html, $destdir, $context);
game_onexporthtml _snakes( $game, $html, $destdir, $context);
break;
break;
case 'millionaire':
case 'millionaire':
game_OnExportHTML _millionaire( $game, $context, $html, $destdir);
game_onexporthtml _millionaire( $game, $context, $html, $destdir);
break;
break;
}
}
remove_dir( $destdir);
remove_dir( $destdir);
}
}
function game_OnExportHTML_cross( $game, $context, $html, $destdir) {
function game_onexporthtml_cross( $game, $context, $html, $destdir) {
global $CFG, $DB;
global $CFG, $DB;
@ -55,40 +70,38 @@
ob_start();
ob_start();
game_cross_play( 0, $game, $attempt, $crossrec, '', true, false, false, false, $html->checkbutton, true, $html->printbutton, false, $context);
game_cross_play( 0, $game, $attempt, $crossrec, '', true, false, false, false,
$html->checkbutton, true, $html->printbutton, false, $context);
$output_ string = ob_get_contents();
$outputstring = ob_get_contents();
ob_end_clean();
ob_end_clean();
$course = $DB->get_record( 'course', array( 'id' => $game->course));
$course = $DB->get_record( 'course', array( 'id' => $game->course));
$filename = $html->filename . '.htm';
$filename = $html->filename . '.htm';
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $output_ string);
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $outputstring);
$filename = game_OnExportHTML _cross_repair_questions( $game, $context, $filename, $destdir);
$filename = game_onexporthtml _cross_repair_questions( $game, $context, $filename, $destdir);
game_send_stored_file( $filename);
game_send_stored_file( $filename);
}
}
function game_OnExportHTML_cross_repair_questions( $game, $context, $filename, $destdir)
function game_onexporthtml_cross_repair_questions( $game, $context, $filename, $destdir) {
{
global $CFG, $DB;
global $CFG, $DB;
$file_ handle = fopen( $destdir.'/'.$filename, "rb");
$filehandle = fopen( $destdir.'/'.$filename, "rb");
$found = false;
$found = false;
$files = array();
$files = array();
$contextcourse = false;
$contextcourse = false;
$linesbefore = array();
$linesbefore = array();
$linesafter = array();
$linesafter = array();
while (!feof($file_ handle) ) {
while (!feof($filehandle) ) {
$line = fgets( $file_ handle);
$line = fgets( $filehandle);
if( $found)
if ($found) {
{
if ( strpos( $line, 'new Array')) {
if( strpos( $line, 'new Array'))
{
$linesafter[] = $line;
$linesafter[] = $line;
break;
break;
}
}
@ -96,8 +109,7 @@
continue;
continue;
}
}
if( strpos( $line, 'Clue = new Array') === false)
if (strpos( $line, 'Clue = new Array') === false) {
{
$linesbefore[] = $line;
$linesbefore[] = $line;
continue;
continue;
}
}
@ -105,11 +117,11 @@
$array = $line;
$array = $line;
$found = true;
$found = true;
}
}
while (!feof($file_ handle) ) {
while (!feof($filehandle) ) {
$linesafter[] = fgets( $file_ handle);
$linesafter[] = fgets( $filehandle);
}
}
fclose($file_ handle);
fclose($filehandle);
$search = $CFG->wwwroot.'/pluginfile.php';
$search = $CFG->wwwroot.'/pluginfile.php';
$pos = 0;
$pos = 0;
@ -117,20 +129,19 @@
$len = strlen( $search);
$len = strlen( $search);
$start = 0;
$start = 0;
$filescopied = false;
$filescopied = false;
for(;;)
for (;;) {
{
$pos1 = strpos( $array, $search, $start);
$pos1 = strpos( $array, $search, $start);
if( $pos1 == false)
if ( $pos1 == false) {
break;
break;
}
$pos2 = strpos( $array, '\"', $pos1 + $len);
$pos2 = strpos( $array, '\"', $pos1 + $len);
if( $pos2 == false)
if ( $pos2 == false) {
break;
break;
}
//Have to copy the files
// Have to copy the files.
if ($contextcourse === false) {
if( $contextcourse === false)
{
mkdir( $destdir.'/images');
mkdir( $destdir.'/images');
if (!$contextcourse = get_context_instance(CONTEXT_COURSE, $game->course)) {
if (!$contextcourse = get_context_instance(CONTEXT_COURSE, $game->course)) {
print_error('nocontext');
print_error('nocontext');
@ -148,52 +159,53 @@
$params = array( 'component' => $component, 'filearea' => $filearea,
$params = array( 'component' => $component, 'filearea' => $filearea,
'itemid' => $id, 'filename' => $fileimage, 'contextid' => $context, 'contextid' => $contextcourse->id);
'itemid' => $id, 'filename' => $fileimage, 'contextid' => $context, 'contextid' => $contextcourse->id);
$rec = $DB->get_record( 'files', $params);
$rec = $DB->get_record( 'files', $params);
if( $rec == false)
if ( $rec == false {
{
print_r( $params);
break;
break;
}
}
if (!$file = $fs->get_file_by_hash($rec->pathnamehash) or $file->is_directory())
if (!$file = $fs->get_file_by_hash($rec->pathnamehash) or $file->is_directory()) {
continue;
continue;
}
$posext = strrpos( $fileimage, '.');
$posext = strrpos( $fileimage, '.');
$filenoext = substr( $fileimage, $posext);
$filenoext = substr( $fileimage, $posext);
$ext = substr( $fileimage, $posext + 1);
$ext = substr( $fileimage, $posext + 1);
for($i=0;;$i++)
for ($i = 0;; $i++) {
{
$newfile = $filenoext.$i;
$newfile = $filenoext.$i;
$newfile = md5( $newfile).'.'.$ext;
$newfile = md5( $newfile).'.'.$ext;
if( !array_search( $newfile, $files))
if (!array_search( $newfile, $files)) {
break;
break;
}
}
}
$file->copy_content_to( $destdir.'/images/'.$newfile);
$file->copy_content_to( $destdir.'/images/'.$newfile);
$filescopied = true;
$filescopied = true;
$array = substr( $array, 0, $pos1 + 1).'images/'.$newfile.substr( $array, $pos2);
$array = substr( $array, 0, $pos1 + 1).'images/'.$newfile.substr( $array, $pos2);
}
}
if( $filescopied == false)
if ($filescopied == false) {
return $destdir.'/'.$filename;
return $destdir.'/'.$filename;
}
$linesbefore[] = $array;
$linesbefore[] = $array;
foreach( $linesafter as $line)
foreach ( $linesafter as $line) {
$linesbefore [] = $line;
$linesbefore [] = $line;
}
file_put_contents( $destdir.'/'.$filename, $linesbefore);
file_put_contents( $destdir.'/'.$filename, $linesbefore);
$pos = strrpos( $filename, '.');
$pos = strrpos( $filename, '.');
if( $pos === false)
if ($pos === false) {
$filezip = $filename.'.zip';
$filezip = $filename.'.zip';
else
} else {
$filezip = substr( $filename, 0, $pos).'.zip';
$filezip = substr( $filename, 0, $pos).'.zip';
}
$filezip = game_create_zip( $destdir, $game->course, $filezip);
$filezip = game_create_zip( $destdir, $game->course, $filezip);
return $filezip;
return $filezip;
}
}
function game_export_printheader( $title, $showbody=true)
function game_export_printheader( $title, $showbody=true) {
{
$ret = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> '."\n";
$ret = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> '."\n";
$ret .= '< html xmlns = "http://www.w3.org/1999/xhtml" dir = "ltr" lang = "el" xml:lang = "el" > '."\n";
$ret .= '< html xmlns = "http://www.w3.org/1999/xhtml" dir = "ltr" lang = "el" xml:lang = "el" > '."\n";
$ret .= "< head > \n";
$ret .= "< head > \n";
@ -201,13 +213,14 @@
$ret .= '< META HTTP-EQUIV = "PRAGMA" CONTENT = "NO-CACHE" > '."\n";
$ret .= '< META HTTP-EQUIV = "PRAGMA" CONTENT = "NO-CACHE" > '."\n";
$ret .= "< title > $title< / title > \n";
$ret .= "< title > $title< / title > \n";
$ret .= "< / head > \n";
$ret .= "< / head > \n";
if( $showbody)
if ($showbody) {
$ret .= "< body > ";
$ret .= "< body > ";
}
return $ret;
return $ret;
}
}
function game_OnExportHTML_hangman( $game, $context, $html, $destdir) {
function game_onexporthtml_hangman( $game, $context, $html, $destdir) {
global $CFG, $DB;
global $CFG, $DB;
@ -215,38 +228,38 @@
$html->filename = 'hangman';
$html->filename = 'hangman';
}
}
if( $game->param10 < = 0)
if ($game->param10 < = 0) {
$game->param10 = 6;
$game->param10 = 6;
}
$filename = $html->filename . '.htm';
$filename = $html->filename . '.htm';
$ret = game_export_printheader( $html->title, false);
$ret = game_export_printheader( $html->title, false);
$ret .= "\r< body onload = \"reset()\" > \r";
$ret .= "\r< body onload = \"reset()\" > \r";
$export_ attachment = ( $html->type == 'hangmanp');
$exportattachment = ( $html->type == 'hangmanp');
$map = game_exmportjavame_getanswers( $game, $context, $export_ attachment, $destdir, $files);
$map = game_exmportjavame_getanswers( $game, $context, $exportattachment, $destdir, $files);
if ($map == false) {
if ($map == false) {
print_error( get_string('no_words', 'game'));
print_error( get_string('no_words', 'game'));
}
}
ob_start();
ob_start();
//Here is the code of hangman
// Here is the code of hangman.
require_once( "exporthtml_hangman.php");
require_once( "exporthtml_hangman.php");
$output_ string = ob_get_contents();
$outputstring = ob_get_contents();
ob_end_clean();
ob_end_clean();
$courseid = $game->course;
$courseid = $game->course;
$course = $DB->get_record( 'course', array( 'id' => $courseid));
$course = $DB->get_record( 'course', array( 'id' => $courseid));
$filename = $html->filename . '.htm';
$filename = $html->filename . '.htm';
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $output_ string);
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $outputstring);
if( $html->type != 'hangmanp')
if ($html->type != 'hangmanp') {
{
// Not copy the standard pictures when we use the "Hangman with pictures".
//Not copy the standard pictures when we use the "Hangman with pictures"
$src = $CFG->dirroot.'/mod/game/pix/hangman/1';
$src = $CFG->dirroot.'/mod/game/pix/hangman/1';
$handle = opendir( $src);
$handle = opendir( $src);
while (false !== ($item = readdir($handle))) {
while (false !== ($item = readdir($handle))) {
@ -254,8 +267,9 @@
if (!is_dir($src.'/'.$item)) {
if (!is_dir($src.'/'.$item)) {
$itemdest = $item;
$itemdest = $item;
if( strpos( $item, '.') === false)
if ( strpos( $item, '.') === false) {
continue;
continue;
}
copy( $src.'/'.$item, $destdir.'/'.$itemdest);
copy( $src.'/'.$item, $destdir.'/'.$itemdest);
}
}
@ -267,7 +281,7 @@
game_send_stored_file( $filezip);
game_send_stored_file( $filezip);
}
}
function game_OnExportHTML_millionaire( $game, $context, $html, $destdir) {
function game_onexporthtml_millionaire( $game, $context, $html, $destdir) {
global $CFG, $DB;
global $CFG, $DB;
@ -280,7 +294,7 @@
$ret = game_export_printheader( $html->title, false);
$ret = game_export_printheader( $html->title, false);
$ret .= "\r< body onload = \"Reset();\" > \r";
$ret .= "\r< body onload = \"Reset();\" > \r";
//Here is the code of millionaire
// Here is the code of millionaire.
require( "exporthtml_millionaire.php");
require( "exporthtml_millionaire.php");
$questions = game_millionaire_html_getquestions( $game, $context, $maxanswers, $maxquestions, $retfeedback, $destdir, $files);
$questions = game_millionaire_html_getquestions( $game, $context, $maxanswers, $maxquestions, $retfeedback, $destdir, $files);
@ -288,8 +302,8 @@
game_millionaire_html_print( $game, $questions, $maxanswers);
game_millionaire_html_print( $game, $questions, $maxanswers);
//End of millionaire code
// End of millionaire code.
$output_ string = ob_get_contents();
$outputstring = ob_get_contents();
ob_end_clean();
ob_end_clean();
$courseid = $game->course;
$courseid = $game->course;
@ -297,9 +311,9 @@
$filename = $html->filename . '.htm';
$filename = $html->filename . '.htm';
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $output_ string);
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $outputstring);
//Copy the standard pictures of Millionaire
// Copy the standard pictures of Millionaire.
$src = $CFG->dirroot.'/mod/game/pix/millionaire/1';
$src = $CFG->dirroot.'/mod/game/pix/millionaire/1';
$handle = opendir( $src);
$handle = opendir( $src);
while (false !== ($item = readdir($handle))) {
while (false !== ($item = readdir($handle))) {
@ -307,8 +321,9 @@
if (!is_dir($src.'/'.$item)) {
if (!is_dir($src.'/'.$item)) {
$itemdest = $item;
$itemdest = $item;
if( strpos( $item, '.') === false)
if (strpos( $item, '.') === false) {
continue;
continue;
}
copy( $src.'/'.$item, $destdir.'/'.$itemdest);
copy( $src.'/'.$item, $destdir.'/'.$itemdest);
}
}
@ -319,7 +334,7 @@
game_send_stored_file($filezip);
game_send_stored_file($filezip);
}
}
function game_OnExportHTML_snakes( $game, $html, $destdir, $context) {
function game_onexporthtml_snakes( $game, $html, $destdir, $context) {
require_once( "exporthtml_millionaire.php");
require_once( "exporthtml_millionaire.php");
global $CFG, $DB;
global $CFG, $DB;
@ -334,22 +349,21 @@
$board = game_snakes_get_board( $game);
$board = game_snakes_get_board( $game);
if( ($game->sourcemodule == 'quiz') or ($game->sourcemodule == 'question'))
if ( ($game->sourcemodule == 'quiz') or ($game->sourcemodule == 'question')) {
$questionsM = game_millionaire_html_getquestions( $game, $context, $maxquestions, $countofquestionsM, $retfeedback, $files);
$questionsm = game_millionaire_html_getquestions( $game, $context, $maxquestions, $countofquestionsm, $retfeedback, $files);
else
} else {
{
$questionsm = array();
$questionsM = array();
$countofquestionsm = 0;
$countofquestionsM = 0;
$retfeedback = '';
$retfeedback = '';
}
}
$questionsS = game_exmportjavame_getanswers( $game, $context, false, $destdir, $files);
$questionss = game_exmportjavame_getanswers( $game, $context, false, $destdir, $files);
ob_start();
ob_start();
//Here is the code of hangman
// Here is the code of hangman.
require( "exporthtml_snakes.php");
require( "exporthtml_snakes.php");
$output_ string = ob_get_contents();
$outputstring = ob_get_contents();
ob_end_clean();
ob_end_clean();
$courseid = $game->course;
$courseid = $game->course;
@ -357,7 +371,7 @@
$filename = $html->filename . '.htm';
$filename = $html->filename . '.htm';
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $output_ string);
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $outputstring);
$src = $CFG->dirroot.'/mod/game/export/html/snakes';
$src = $CFG->dirroot.'/mod/game/export/html/snakes';
game_copyfiles( $src, $destdir);
game_copyfiles( $src, $destdir);
@ -372,45 +386,46 @@
mkdir( $destdir .'/images');
mkdir( $destdir .'/images');
$destfile = $destdir.'/images/'.$board->fileboard;
$destfile = $destdir.'/images/'.$board->fileboard;
if( $game->param3 != 0)
if ( $game->param3 != 0) {
{
// Is a standard board.
//Is a standard board
copy( $board->imagesrc, $destfile);
copy( $board->imagesrc, $destfile);
}else
} else {
{
$cmg = get_coursemodule_from_instance('game', $game->id, $game->course);
$cmg = get_coursemodule_from_instance('game', $game->id, $game->course);
$modcontext = get_context_instance(CONTEXT_MODULE, $cmg->id);
$modcontext = get_context_instance(CONTEXT_MODULE, $cmg->id);
$fs = get_file_storage();
$fs = get_file_storage();
$files = $fs->get_area_files($modcontext->id, 'mod_game', 'snakes_board', $game->id);
$files = $fs->get_area_files($modcontext->id, 'mod_game', 'snakes_board', $game->id);
foreach ($files as $f) {
foreach ($files as $f) {
if( $f->is_directory())
if ( $f->is_directory()) {
continue;
continue;
}
break;
break;
}
}
$f->copy_content_to( $destfile);
$f->copy_content_to( $destfile);
}
}
$a = array( 'player1.png', 'dice1.png', 'dice2.png', 'dice3.png', 'dice4.png', 'dice5.png', 'dice6.png', 'numbers.png');
$a = array( 'player1.png', 'dice1.png', 'dice2.png', 'dice3.png', 'dice4.png', 'dice5.png', 'dice6.png', 'numbers.png');
foreach( $a as $file)
foreach ( $a as $file) {
copy( $CFG->dirroot.'/mod/game/snakes/1/'.$file, $destdir.'/images/'.$file);
copy( $CFG->dirroot.'/mod/game/snakes/1/'.$file, $destdir.'/images/'.$file);
}
$filezip = game_create_zip( $destdir, $courseid, $html->filename.'.zip');
$filezip = game_create_zip( $destdir, $courseid, $html->filename.'.zip');
game_send_stored_file($filezip);
game_send_stored_file($filezip);
}
}
function game_copyfiles( $src, $destdir)
function game_copyfiles( $src, $destdir) {
{
$handle = opendir( $src);
$handle = opendir( $src);
while (($item = readdir($handle)) !== false)
while (($item = readdir($handle)) !== false) {
{
if ( $item == '.' or $item == '..') {
if( $item == '.' or $item == '..')
continue;
continue;
}
if( strpos( $item, '.') === false)
if ( strpos( $item, '.') === false) {
continue;
continue;
}
if(is_dir($src.'/'.$item))
if (is_dir($src.'/'.$item)) {
continue;
continue;
}
copy( $src.'/'.$item, $destdir.'/'.$item);
copy( $src.'/'.$item, $destdir.'/'.$item);
}
}