@ -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
*
*
@ -7,412 +22,412 @@
* @package game
* @package game
**/
**/
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);
}
}
function game_OnExportHTML_cross( $game, $context, $html, $destdir){
remove_dir( $destdir);
}
global $CFG, $DB;
function game_onexporthtml_cross( $game, $context, $html, $destdir) {
if( $html->filename == ''){
global $CFG, $DB;
$html->filename = 'cross';
}
$filename = $html->filename . '.htm';
if ( $html->filename == '') {
$html->filename = 'cross';
}
require( "cross/play.php");
$filename = $html->filename . '.htm';
$attempt = game_getattempt( $game, $crossrec, true);
if( $crossrec == false){
game_cross_new( $game, $attempt->id, $crossm);
$attempt = game_getattempt( $game, $crossrec);
}
$ret = game_export_printheader( $html->title);
require( "cross/play.php");
echo "$ret< br > ";
$attempt = game_getattempt( $game, $crossrec, true);
if ( $crossrec == false) {
game_cross_new( $game, $attempt->id, $crossm);
$attempt = game_getattempt( $game, $crossrec);
}
ob_start();
$ret = game_export_printheader( $html->title);
echo "$ret< br > ";
game_cross_play( 0, $game, $attempt, $crossrec, '', true, false, false, false, $html->checkbutton, true, $html->printbutton, false, $context);
ob_start( );
$output_string = ob_get_contents();
game_cross_play( 0, $game, $attempt, $crossrec, '', true, false, false, false,
ob_end_clean( );
$html->checkbutton, true, $html->printbutton, false, $context );
$course = $DB->get_record( 'course', array( 'id' => $game->course));
$outputstring = ob_get_contents();
ob_end_clean();
$filename = $html->filename . '.htm' ;
$course = $DB->get_record( 'course', array( 'id' => $game->course)) ;
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $output_string) ;
$filename = $html->filename . '.htm' ;
$filename = game_OnExportHTML_cross_repair_questions( $game, $context, $filename, $destdir );
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $outputstring );
game_send_stored_file( $filename);
$filename = game_onexporthtml_cross_repair_questions( $game, $context, $filename, $destdir);
}
function game_OnExportHTML_cross_repair_questions( $game, $context, $filename, $destdir)
game_send_stored_file( $filename);
{
}
global $CFG, $DB;
$file_handle = fopen( $destdir.'/'.$filename, "rb");
$found = false;
$files = array();
$contextcourse = false;
$linesbefore = array();
$linesafter = array();
while (!feof($file_handle) ) {
$line = fgets( $file_handle);
if( $found)
{
if( strpos( $line, 'new Array'))
{
$linesafter[] = $line;
break;
}
$array .= $line;
continue;
}
if( strpos( $line, 'Clue = new Array') === false)
function game_onexporthtml_cross_repair_questions( $game, $context, $filename, $destdir) {
{
global $CFG, $DB;
$linesbefore[] = $line;
continue;
}
$array = $line;
$filehandle = fopen( $destdir.'/'.$filename, "rb");
$found = true;
}
while (!feof($file_handle) ) {
$linesafter[] = fgets( $file_handle);
}
fclose($file_handle);
$found = false;
$files = array();
$search = $CFG->wwwroot.'/pluginfile.php';
$contextcourse = false;
$pos = 0;
$linesbefore = array();
$search = '"'.$CFG->wwwroot.'/pluginfile.php/'.$context->id.'/mod_game/';
$linesafter = array();
$len = strlen( $search);
while (!feof($filehandle) ) {
$start = 0;
$line = fgets( $filehandle);
$filescopied = false;
for(;;)
{
$pos1 = strpos( $array, $search, $start);
if( $pos1 == false)
break;
$pos2 = strpos( $array, '\"', $pos1+$len);
if ($found) {
if( $pos2 == false)
if ( strpos( $line, 'new Array')) {
$linesafter[] = $line;
break;
break;
}
$array .= $line;
continue;
}
//Have to copy the files
if (strpos( $line, 'Clue = new Array') === false) {
$linesbefore[] = $line;
continue;
}
if( $contextcourse === false)
$array = $line;
{
$found = true;
mkdir( $destdir.'/images');
}
if (!$contextcourse = get_context_instance(CONTEXT_COURSE, $game->course)) {
while (!feof($filehandle) ) {
print_error('nocontext');
$linesafter[] = fgets( $filehandle);
}
}
$fs = get_file_storage();
}
$inputs = explode( '/', substr( $array, $pos1+$len, $pos2-$pos1-$len));
fclose($filehandle);
$search = $CFG->wwwroot.'/pluginfile.php';
$pos = 0;
$search = '"'.$CFG->wwwroot.'/pluginfile.php/'.$context->id.'/mod_game/';
$len = strlen( $search);
$start = 0;
$filescopied = false;
for (;;) {
$pos1 = strpos( $array, $search, $start);
if ( $pos1 == false) {
break;
}
$filearea = $inputs[ 0];
$pos2 = strpos( $array, '\"', $pos1 + $len) ;
$id = $inputs[ 1];
if ( $pos2 == false) {
$fileimage = urldecode( $inputs[ 2]);
break ;
$component = 'question';
}
$params = array( 'component' => $component, 'filearea' => $filearea,
// Have to copy the files.
'itemid' => $id, 'filename' => $fileimage, 'contextid' => $context, 'contextid' => $contextcourse->id);
if ($contextcourse === false) {
$rec = $DB->get_record( 'files', $params);
mkdir( $destdir.'/images');
if( $rec == false)
if (!$contextcourse = get_context_instance(CONTEXT_COURSE, $game->course)) {
{
print_error('nocontext');
print_r( $params);
break;
}
}
$fs = get_file_storage();
}
if (!$file = $fs->get_file_by_hash($rec->pathnamehash) or $file->is_directory())
$inputs = explode( '/', substr( $array, $pos1 + $len, $pos2 - $pos1 - $len));
continue;
$posext = strrpos( $fileimage, '.');
$filearea = $inputs[ 0];
$filenoext = substr( $fileimage, $posext);
$id = $inputs[ 1];
$ext = substr( $fileimage, $posext+1);
$fileimage = urldecode( $inputs[ 2]);
for($i=0;;$i++)
$component = 'question';
{
$newfile = $filenoext.$i;
$newfile = md5( $newfile).'.'.$ext;
if( !array_search( $newfile, $files))
break;
}
$file->copy_content_to( $destdir.'/images/'.$newfile);
$filescopied = true;
$array = substr( $array, 0, $pos1+1).'images/'.$newfile.substr( $array, $pos2);
$params = array( 'component' => $component, 'filearea' => $filearea,
'itemid' => $id, 'filename' => $fileimage, 'contextid' => $context, 'contextid' => $contextcourse->id);
$rec = $DB->get_record( 'files', $params);
if ( $rec == false {
break;
}
}
if( $filescopied == false)
if (!$file = $fs->get_file_by_hash($rec->pathnamehash) or $file->is_directory()) {
return $destdir.'/'.$filename;
continue;
}
$linesbefore[] = $array;
$posext = strrpos( $fileimage, '.');
foreach( $linesafter as $line)
$filenoext = substr( $fileimage, $posext);
$linesbefore [] = $line;
$ext = substr( $fileimage, $posext + 1);
file_put_contents( $destdir.'/'.$filename, $linesbefore);
for ($i = 0;; $i++) {
$newfile = $filenoext.$i;
$newfile = md5( $newfile).'.'.$ext;
if (!array_search( $newfile, $files)) {
break;
}
}
$file->copy_content_to( $destdir.'/images/'.$newfile);
$filescopied = true;
$pos = strrpos( $filename, '.');
$array = substr( $array, 0, $pos1 + 1).'images/'.$newfile.substr( $array, $pos2);
if( $pos === false)
}
$filezip = $filename.'.zip';
else
$filezip = substr( $filename, 0, $pos).'.zip';
$filezip = game_create_zip( $destdir, $game->course, $filezip);
if ($filescopied == false) {
return $destdir.'/'.$filename;
}
return $filezip;
$linesbefore[] = $array;
foreach ($linesafter as $line) {
$linesbefore [] = $line;
}
}
file_put_contents( $destdir.'/'.$filename, $linesbefore);
function game_export_printheader( $title, $showbody=true)
$pos = strrpos( $filename, '.');
{
if ($pos === false) {
$ret = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> '."\n";
$filezip = $filename.'.zip';
$ret .= '< html xmlns = "http://www.w3.org/1999/xhtml" dir = "ltr" lang = "el" xml:lang = "el" > '."\n";
} else {
$ret .= "< head > \n";
$filezip = substr( $filename, 0, $pos).'.zip';
$ret .= '< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" / > '."\n";
$ret .= '< META HTTP-EQUIV = "PRAGMA" CONTENT = "NO-CACHE" > '."\n";
$ret .= "< title > $title< / title > \n";
$ret .= "< / head > \n";
if( $showbody)
$ret .= "< body > ";
return $ret;
}
}
function game_OnExportHTML_hangman( $game, $context, $html, $destdir){
$filezip = game_create_zip( $destdir, $game->course, $filezip);
return $filezip;
}
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 .= '< html xmlns = "http://www.w3.org/1999/xhtml" dir = "ltr" lang = "el" xml:lang = "el" > '."\n";
$ret .= "< head > \n";
$ret .= '< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" / > '."\n";
$ret .= '< META HTTP-EQUIV = "PRAGMA" CONTENT = "NO-CACHE" > '."\n";
$ret .= "< title > $title< / title > \n";
$ret .= "< / head > \n";
if ($showbody) {
$ret .= "< body > ";
}
global $CFG, $DB;
return $ret;
}
if( $html->filename == ''){
function game_onexporthtml_hangman( $game, $context, $html, $destdir) {
$html->filename = 'hangman';
}
if( $game->param10 < = 0)
global $CFG, $DB;
$game->param10 = 6;
$filename = $html->filename . '.htm';
if ($html->filename == '') {
$html->filename = 'hangman';
}
$ret = game_export_printheader( $html->title, false);
if ($game->param10 < = 0) {
$ret .= "\r< body onload = \"reset()\" > \r";
$game->param10 = 6;
}
$export_attachment = ( $html->type == 'hangmanp') ;
$filename = $html->filename . '.htm' ;
$map = game_exmportjavame_getanswers( $game, $context, $export_attachment, $destdir, $files );
$ret = game_export_printheader( $html->title, false );
if( $map == false){
$ret .= "\r< body onload = \"reset()\" > \r";
print_error( get_string('no_words', 'game'));
}
$exportattachment = ( $html->type == 'hangmanp');
ob_start();
$map = game_exmportjavame_getanswers( $game, $context, $exportattachment, $destdir, $files);
if ($map == false) {
print_error( get_string('no_words', 'game'));
}
//Here is the code of hangman
ob_start();
require_once( "exporthtml_hangman.php");
$output_string = ob_get_contents();
// Here is the code of hangman.
ob_end_clean( );
require_once( "exporthtml_hangman.php" );
$courseid = $game->course ;
$outputstring = ob_get_contents() ;
$course = $DB->get_record( 'course', array( 'id' => $courseid) );
ob_end_clean( );
$filename = $html->filename . '.htm' ;
$courseid = $game->course ;
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $output_string );
$course = $DB->get_record( 'course', array( 'id' => $courseid) );
if( $html->type != 'hangmanp')
$filename = $html->filename . '.htm';
{
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $outputstring);
//Not copy the standard pictures when we use the "Hangman with pictures"
$src = $CFG->dirroot.'/mod/game/pix/hangman/1';
$handle = opendir( $src);
while (false!==($item = readdir($handle))) {
if($item != '.' & & $item != '..') {
if(!is_dir($src.'/'.$item)) {
$itemdest = $item;
if( strpos( $item, '.') === false)
if ($html->type != 'hangmanp') {
continue;
// Not copy the standard pictures when we use the "Hangman with pictures".
$src = $CFG->dirroot.'/mod/game/pix/hangman/1';
$handle = opendir( $src);
while (false !== ($item = readdir($handle))) {
if ($item != '.' & & $item != '..') {
if (!is_dir($src.'/'.$item)) {
$itemdest = $item;
copy( $src.'/'.$item, $destdir.'/'.$itemdest);
if ( strpos( $item, '.') === false) {
}
continue;
}
}
}
}
$filezip = game_create_zip( $destdir, $courseid, $html->filename.'.zip');
copy( $src.'/'.$item, $destdir.'/'.$itemdest);
game_send_stored_file( $filezip);
}
}
}
}
}
function game_OnExportHTML_millionaire( $game, $context, $html, $destdir){
$filezip = game_create_zip( $destdir, $courseid, $html->filename.'.zip');
game_send_stored_file( $filezip);
}
global $CFG, $DB;
function game_onexporthtml_millionaire( $game, $context, $html, $destdir) {
if( $html->filename == ''){
global $CFG, $DB;
$html->filename = 'millionaire';
}
$filename = $html->filename . '.htm';
if ($html->filename == '') {
$html->filename = 'millionaire';
}
$ret = game_export_printheader( $html->title, false);
$filename = $html->filename . '.htm';
$ret .= "\r< body onload = \"Reset();\" > \r";
//Here is the code of millionaire
$ret = game_export_printheader( $html->title, false);
require( "exporthtml_millionaire.php") ;
$ret .= "\r< body onload = \"Reset();\" > \r" ;
$questions = game_millionaire_html_getquestions( $game, $context, $maxanswers, $maxquestions, $retfeedback, $destdir, $files);
// Here is the code of millionaire.
ob_start( );
require( "exporthtml_millionaire.php" );
game_millionaire_html_print( $game, $questions, $maxanswers);
$questions = game_millionaire_html_getquestions( $game, $context, $maxanswers, $maxquestions, $retfeedback, $destdir, $files);
ob_start();
//End of millionaire code
game_millionaire_html_print( $game, $questions, $maxanswers);
$output_string = ob_get_contents();
ob_end_clean();
$courseid = $game->course;
// End of millionaire code.
$course = $DB->get_record( 'course', array( 'id' => $courseid));
$outputstring = ob_get_contents();
ob_end_clean();
$filename = $html->filename . '.htm';
$courseid = $game->course;
$course = $DB->get_record( 'course', array( 'id' => $courseid));
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $output_string) ;
$filename = $html->filename . '.htm' ;
//Copy the standard pictures of Millionaire
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $outputstring);
$src = $CFG->dirroot.'/mod/game/pix/millionaire/1';
$handle = opendir( $src);
while (false!==($item = readdir($handle))) {
if($item != '.' & & $item != '..') {
if(!is_dir($src.'/'.$item)) {
$itemdest = $item;
if( strpos( $item, '.') === false)
// Copy the standard pictures of Millionaire.
continue;
$src = $CFG->dirroot.'/mod/game/pix/millionaire/1';
$handle = opendir( $src);
while (false !== ($item = readdir($handle))) {
if ($item != '.' & & $item != '..') {
if (!is_dir($src.'/'.$item)) {
$itemdest = $item;
copy( $src.'/'.$item, $destdir.'/'.$itemdest);
if (strpos( $item, '.') === false) {
continue;
}
}
}
}
$filezip = game_create_zip( $destdir, $courseid, $html->filename.'.zip');
copy( $src.'/'.$item, $destdir.'/'.$itemdest);
game_send_stored_file($filezip);
}
}
}
}
function game_OnExportHTML_snakes( $game, $html, $destdir, $context){
$filezip = game_create_zip( $destdir, $courseid, $html->filename.'.zip');
require_once( "exporthtml_millionaire.php");
game_send_stored_file($filezip);
}
global $CFG, $DB;
function game_onexporthtml_snakes( $game, $html, $destdir, $context) {
require_once( "exporthtml_millionaire.php");
if( $html->filename == ''){
global $CFG, $DB;
$html->filename = 'snakes';
}
$filename = $html->filename . '.htm';
if ($html->filename == '') {
$html->filename = 'snakes';
}
$ret = '';
$filename = $html->filename . '.htm ';
$board = game_snakes_get_board( $game) ;
$ret = '' ;
if( ($game->sourcemodule == 'quiz') or ($game->sourcemodule == 'question'))
$board = game_snakes_get_board( $game);
$questionsM = game_millionaire_html_getquestions( $game, $context, $maxquestions, $countofquestionsM, $retfeedback, $files);
else
{
$questionsM = array();
$countofquestionsM = 0;
$retfeedback = '';
}
$questionsS = game_exmportjavame_getanswers( $game, $context, false, $destdir, $files);
ob_start();
if ( ($game->sourcemodule == 'quiz') or ($game->sourcemodule == 'question')) {
$questionsm = game_millionaire_html_getquestions( $game, $context, $maxquestions, $countofquestionsm, $retfeedback, $files);
} else {
$questionsm = array();
$countofquestionsm = 0;
$retfeedback = '';
}
$questionss = game_exmportjavame_getanswers( $game, $context, false, $destdir, $files);
//Here is the code of hangman
ob_start();
require( "exporthtml_snakes.php");
$output_string = ob_get_contents();
// Here is the code of hangman.
ob_end_clean();
require( "exporthtml_snakes.php" );
$courseid = $game->course ;
$outputstring = ob_get_contents() ;
$course = $DB->get_record( 'course', array( 'id' => $courseid) );
ob_end_clean( );
$filename = $html->filename . '.htm';
$courseid = $game->course;
$course = $DB->get_record( 'course', array( 'id' => $courseid));
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $output_string) ;
$filename = $html->filename . '.htm' ;
$src = $CFG->dirroot.'/mod/game/export/html/snakes';
file_put_contents( $destdir.'/'.$filename, $ret . "\r\n" . $outputstring);
game_copyfiles( $src, $destdir);
mkdir( $destdir .'/css');
$src = $CFG->dirroot.'/mod/game/export/html/snakes';
$src = $CFG->dirroot.'/mod/game/export/html/snakes/css';
game_copyfiles( $src, $destdir);
game_copyfiles( $src, $destdir.'/css');
mkdir( $destdir .'/j s');
mkdir( $destdir .'/cs s');
$src = $CFG->dirroot.'/mod/game/export/html/snakes/j s';
$src = $CFG->dirroot.'/mod/game/export/html/snakes/cs s';
game_copyfiles( $src, $destdir.'/j s');
game_copyfiles( $src, $destdir.'/cs s');
mkdir( $destdir .'/images');
mkdir( $destdir .'/js');
$destfile = $destdir.'/images/'.$board->fileboard;
$src = $CFG->dirroot.'/mod/game/export/html/snakes/js';
if( $game->param3 != 0)
game_copyfiles( $src, $destdir.'/js');
{
//Is a standard board
mkdir( $destdir .'/images');
copy( $board->imagesrc, $destfile);
$destfile = $destdir.'/images/'.$board->fileboard;
}else
if ( $game->param3 != 0) {
{
// Is a standard board.
$cmg = get_coursemodule_from_instance('game', $game->id, $game->course);
copy( $board->imagesrc, $destfile);
$modcontext = get_context_instance(CONTEXT_MODULE, $cmg->id);
} else {
$fs = get_file_storage();
$cmg = get_coursemodule_from_instance('game', $game->id, $game->course);
$files = $fs->get_area_files($modcontext->id, 'mod_game', 'snakes_board', $game->id);
$modcontext = get_context_instance(CONTEXT_MODULE, $cmg->id);
foreach ($files as $f) {
$fs = get_file_storage();
if( $f->is_directory())
$files = $fs->get_area_files($modcontext->id, 'mod_game', 'snakes_board', $game->id);
continue;
foreach ($files as $f) {
break;
if ( $f->is_directory()) {
continue;
}
}
$f->copy_content_to( $destfile) ;
break ;
}
}
$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');
game_send_stored_file($filezip);
}
}
function game_copyfiles( $src, $destdir)
$filezip = game_create_zip( $destdir, $courseid, $html->filename.'.zip');
{
game_send_stored_file($filezip);
$handle = opendir( $src);
}
while (($item = readdir($handle)) !== false)
{
if( $item == '.' or $item == '..')
continue;
if( strpos( $item, '.') === false)
function game_copyfiles( $src, $destdir) {
continue;
$handle = opendir( $src);
while (($item = readdir($handle)) !== false) {
if ( $item == '.' or $item == '..') {
continue;
}
if(is_dir($src.'/'.$item))
if ( strpos( $item, '.') === false) {
continue;
continue;
}
if (is_dir($src.'/'.$item)) {
continue;
}
copy( $src.'/'.$item, $destdir.'/'.$item);
copy( $src.'/'.$item, $destdir.'/'.$item);
}
closedir($handle);
}
}
closedir($handle);
}