@ -1,4 +1,19 @@
<?php // $Id: importodt.php,v 1.5 2012/07/25 11:16:04 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 is a very rough importer for odt
* This is a very rough importer for odt
*
*
@ -22,35 +37,36 @@
print_error( get_string( 'bookquiz_not_select_book', 'game'));
print_error( get_string( 'bookquiz_not_select_book', 'game'));
}
}
if ($form = data_submitted())
if ($form = data_submitted()) {
{ /// Filename
// Filename.
if (empty($_FILES['newfile'])) {
if (empty($_FILES['newfile']))
// File was just uploaded.
{ // file was just uploaded
notify(get_string("uploadproblem") );
notify(get_string("uploadproblem") );
}
}
if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0))
if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
{
notify(get_string("uploadnofilefound") );
notify(get_string("uploadnofilefound") );
} else
} else {
{ // Valid file is found
// Valid file is found.
if ( readdata( $course->id, 'game', $dirtemp, $r_ levels, $r_ titles, $r_ texts, $dirfordelete))
if (readdata( $course->id, 'game', $dirtemp, $rlevels, $rtitles, $rtexts, $dirfordelete)) {
{ // first try to reall all of the data in
// First try to reall all of the data in.
if ($overwrite) {
if ($overwrite) {
game_bookquiz_deletebook( $course->id, $bookid);
game_bookquiz_deletebook( $course->id, $bookid);
}
}
$pageobjects = extract_data( $course->id, 'book', $bookid, $dirtemp, $subchapter, $r_levels, $r_titles, $r_texts); // parse all the html files into objects
// Parse all the html files into objects.
clean_temp( $dirfordelete); // all done with files so dump em
$pageobjects = extract_data( $course->id, 'book', $bookid, $dirtemp, $subchapter, $rlevels, $rtitles, $rtexts);
clean_temp( $dirfordelete); // All done with files so dump em.
$objects = game_bookquiz_create_objects( $pageobjects, $bookid); // function to preps the data to be sent to DB
// Function to preps the data to be sent to DB.
$objects = game_bookquiz_create_objects( $pageobjects, $bookid);
if( !game_bookquiz_save_objects($objects))
if (!game_bookquiz_save_objects( $objects)) {
{ // sends it to DB
// Sends it to DB.
print_error('could not save');
print_error('could not save');
}
}
}else
} else {
print_error('could not get data');
print_error('could not get data');
}
print_continue("{$CFG->wwwroot}/mod/game/view.php?id=$cm->id");
print_continue("{$CFG->wwwroot}/mod/game/view.php?id=$cm->id");
echo $OUTPUT->footer($course);
echo $OUTPUT->footer($course);
@ -58,7 +74,7 @@
}
}
}
}
/ // Print upload form
// Print upload form.
print_heading_with_help( get_string( "bookquiz_import_odt", "game"), "importodt", "game");
print_heading_with_help( get_string( "bookquiz_import_odt", "game"), "importodt", "game");
@ -97,90 +113,84 @@
echo $OUTPUT->footer($course);
echo $OUTPUT->footer($course);
// START OF FUNCTIONS
// START OF FUNCTIONS.
//the r_basedir variable contains the directory where the temp files are
// The rbasedir variable contains the directory where the temp files are.
//At the end the directory must be deleted
// At the end the directory must be deleted.
function readdata( $courseid, $modname, & $r_basedir, & $r_levels, & $r_titles, & $r_texts, & $dirfordelete)
function readdata( $courseid, $modname, & $rbasedir, & $rlevels, & $rtitles, & $rtexts, & $dirfordelete) {
{
// This function expects a odt file to be uploaded. Then it parses the content.xml to determine.
// this function expects a odt file to be uploaded. Then it parses
// Then copies the images.
// the content.xml to determine.
// Then copies the image
global $CFG;
global $CFG;
// create a random upload directory in temp
// Create a random upload directory in temp.
$newdir = $CFG->dataroot."/temp/$modname";
$newdir = $CFG->dataroot."/temp/$modname";
if (!file_exists( $newdir))
if (!file_exists( $newdir)) {
mkdir( $newdir);
mkdir( $newdir);
}
$i = 1;
$i = 1;
srand((double)microtime() * 1000000);
srand((double)microtime() * 1000000);
while(true)
while (true) {
{
$rbasedir = "$modname/$i-".rand(0, 10000);
$r_basedir = "$modname/$i-".rand(0,10000);
$newdir = $CFG->dataroot.'/temp/'.$rbasedir;
$newdir = $CFG->dataroot.'/temp/'.$r_basedir;
if (!file_exists( $newdir)) {
if (!file_exists( $newdir))
{
mkdir( $newdir);
mkdir( $newdir);
$newdir .= '/';
$newdir .= '/';
break;
break;
}
}
$i++;
$i++;
}
}
$dirfordelete = $r_ basedir;
$dirfordelete = $r basedir;
$r_ basedir .= '/';
$r basedir .= '/';
$zipfile = $_FILES["newfile"]["name"];
$zipfile = $_FILES["newfile"]["name"];
$tempzipfile = $_FILES["newfile"]["tmp_name"];
$tempzipfile = $_FILES["newfile"]["tmp_name"];
// create our directory
// Creates our directory.
$path_parts = pathinfo($zipfile);
$pathparts = pathinfo($zipfile);
$dirname = substr($zipfile, 0, strpos($zipfile, '.'.$path_parts['extension'])); // take off the extension
// Takes off the extension.
$dirname = substr($zipfile, 0, strpos($zipfile, '.'.$pathparts['extension']));
if (!file_exists($newdir.$dirname)) {
if (!file_exists($newdir.$dirname)) {
mkdir($newdir.$dirname);
mkdir($newdir.$dirname);
}
}
// move our uploaded file to temp/game
// Move our uploaded file to temp/game.
move_uploaded_file( $tempzipfile, $newdir.$zipfile);
move_uploaded_file( $tempzipfile, $newdir.$zipfile);
//if the file ends with .lnk then use .odt instead
// If the file ends with .lnk then use .odt instead.
if( substr( $zipfile, -4) == ".lnk")
if (substr( $zipfile, -4) == ".lnk") {
$zipfile = substr( $zipfile, 0, -4).".odt";
$zipfile = substr( $zipfile, 0, -4).".odt";
}
// u nzip it!
// U nzip it!
unzip_file ( $newdir.$zipfile, $newdir.$dirname, false);
unzip_file ( $newdir.$zipfile, $newdir.$dirname, false);
$r_basedir .= $dirname; // update the base
$rbasedir .= $dirname; // Update the base.
$newdir .= $dirname;
$newdir .= $dirname;
// t his is the file where we get the names of the files for the slides (in the correct order too)
// T his is the file where we get the names of the files for the slides (in the correct order too).
$content = $newdir.'/content.xml';
$content = $newdir.'/content.xml';
$data = file_get_contents( $content);
$data = file_get_contents( $content);
$content = $newdir.'/styles.xml';
$content = $newdir.'/styles.xml';
if (file_exists( $content)) {
if (file_exists( $content)) {
$datastyle = file_get_contents( $content);
$datastyle = file_get_contents( $content);
}else
} else {
{
$datastyle = '';
$datastyle = '';
}
}
oo_game_convert_ver2( $data, $datastyle, $r_levels, $r_titles, $r_ texts);
oo_game_convert_ver2( $data, $datastyle, $rlevels, $rtitles, $r texts);
return true;
return true;
}
}
function oo_game_convert_ver2( $data, $datastyle, & $rlevels, & $rtitles, & $rtexts) {
$rlevels = array();
$rtitles = array();
$rtexts = array();
////////////////////////
// We have tables, encode it here so all < text:p in them don ' t get preg_match_all few lines later .
function oo_game_convert_ver2( $data, $datastyle, & $r_levels, & $r_titles, & $r_texts)
if (ereg('table:table', $data)) {
{
$r_levels = array();
$r_titles = array();
$r_texts = array();
// we have tables, encode it here so all < text:p in them don ' t get preg_match_all few lines later
IF(ereg('table:table', $data))
{
$data = str_replace('< table:table ' , ' < text:p text:style-name = "RKRK" > < table:table ' , $ data ) ;
$data = str_replace('< table:table ' , ' < text:p text:style-name = "RKRK" > < table:table ' , $ data ) ;
$data = str_replace('< / table:table > ', '< / table:table > < / text:p > ', $data);
$data = str_replace('< / table:table > ', '< / table:table > < / text:p > ', $data);
$data = preg_replace('#< table:table ( . * ? ) < / table:table > #es', "base64_encode('\\1')", $data);
$data = preg_replace('#< table:table ( . * ? ) < / table:table > #es', "base64_encode('\\1')", $data);
@ -191,65 +201,63 @@ function readdata( $courseid, $modname, &$r_basedir, &$r_levels, &$r_titles, &$r
game_bookquiz_convert_ver2_computestyles( $data, $styles, false);
game_bookquiz_convert_ver2_computestyles( $data, $styles, false);
game_bookquiz_splitsections($data, $positions, $inputs, $titles, $titleframes, $texts);
game_bookquiz_splitsections($data, $positions, $inputs, $titles, $titleframes, $texts);
for( $i=0; $i < count ( $ positions ) ; $ i + + )
for ($i = 0; $i < count ( $ positions ) ; $ i + + ) {
{
preg_match_all( "#text:outline-level=\"([0-9]*)\"#es", $inputs[ $i], $matches);
preg_match_all( "#text:outline-level=\"([0-9]*)\"#es", $inputs[ $i], $matches);
$levels = $matches[ 1];
$levels = $matches[ 1];
if (count( $levels) > 0) {
if (count( $levels) > 0) {
$level = $levels[ 0];
$level = $levels[ 0];
}else
} else {
{
$level = 0;
$level = 0;
}
}
$r_ levels[] = $level;
$r levels[] = $level;
$r_ titles[] = strip_tags( $titles[ $i]);
$r titles[] = strip_tags( $titles[ $i]);
$textframe = game_bookquiz_convert($titleframes[ $i], $styles, $images);
$textframe = game_bookquiz_convert($titleframes[ $i], $styles, $images);
$text = game_bookquiz_convert($texts[ $i], $styles, $images);
$text = game_bookquiz_convert($texts[ $i], $styles, $images);
if ($textframe != '') {
if ($textframe != '') {
$text = $textframe.'< BR > '.$text;
$text = $textframe.'< br > '.$text;
}
}
echo "< hr > < b > ".$titles[ $i]."< / b > < br > ".$text."\r\n\r\n\r\n\r\n";
echo "< hr > < b > ".$titles[ $i]."< / b > < br > ".$text."\r\n\r\n\r\n\r\n";
$r_ texts[] = $text;
$r texts[] = $text;
}
}
}
}
function extract_data( $courseid, $modname, $id, $basedir, $subchapter, $levels, $titles, $texts)
function extract_data( $courseid, $modname, $id, $basedir, $subchapter, $levels, $titles, $texts) {
{
global $CFG;
global $CFG;
global $matches;
global $matches;
$dirtemp = $CFG->dataroot.'/temp/'.$basedir;
$dirtemp = $CFG->dataroot.'/temp/'.$basedir;
for ($i = 0; $i < count ( $ levels ) ; $ i + + ) {
for ($i = 0; $i < count ( $ levels ) ; $ i + + ) {
echo $levels[ $i]." ".$titles[ $i]."< BR > ";
echo $levels[ $i]." ".$titles[ $i]."< br > ";
}
}
$extractedpages = array();
$extractedpages = array();
// directory for images
// Directory for images.
make_mod_upload_directory( $courseid); // make sure moddata is made
make_mod_upload_directory( $courseid); // Make sure moddata is made.
make_upload_directory( $courseid.'/moddata/'.$modname, false);
make_upload_directory( $courseid.'/moddata/'.$modname, false);
make_upload_directory( $courseid.'/moddata/'.$modname."/".$id, false); // we store our images in a subfolder in here
// We store our images in a subfolder in here.
make_upload_directory( $courseid.'/moddata/'.$modname."/".$id, false);
$imagedir = $CFG->dataroot.'/'.$courseid.'/moddata/'.$modname."/".$id;
$imagedir = $CFG->dataroot.'/'.$courseid.'/moddata/'.$modname."/".$id;
if ($CFG->slasharguments)
if ($CFG->slasharguments) {
$imagelink = $CFG->wwwroot.'/file.php/'.$courseid.'/moddata/'.$modname."/".$id;
$imagelink = $CFG->wwwroot.'/file.php/'.$courseid.'/moddata/'.$modname."/".$id;
else
} else {
$imagelink = $CFG->wwwroot.'/file.php?file=/'.$courseid.'/moddata/'.$modname."/".$id;
$imagelink = $CFG->wwwroot.'/file.php?file=/'.$courseid.'/moddata/'.$modname."/".$id;
}
// try to make a unique subfolder to store the images
// Try to make a unique subfolder to store the images.
$i = 1;
$i = 1;
while(true)
while (true) {
{
$newdir = $imagedir.'/'.$i;
$newdir = $imagedir.'/'.$i;
if (!file_exists( $newdir))
if (!file_exists( $newdir)) {
{
// Ok doesnt exist so make the directory and update our paths.
// ok doesnt exist so make the directory and update our paths
mkdir( $newdir);
mkdir( $newdir);
$imagedir = $newdir;
$imagedir = $newdir;
$imagelink = $imagelink.'/'.$i;
$imagelink = $imagelink.'/'.$i;
@ -258,16 +266,14 @@ function extract_data( $courseid, $modname, $id, $basedir, $subchapter, $levels,
$i++;
$i++;
}
}
for( $i=0; $i < count ( $ titles ) ; $ i + + )
for ($i = 0; $i < count ( $ titles ) ; $ i + + ) {
{
// Start building our page.
// start building our page
$page = new stdClass;
$page = new stdClass;
$page->title = $titles[ $i];
$page->title = $titles[ $i];
$page->content = $texts[ $i];
$page->content = $texts[ $i];
//$page->source = $path_parts['basename']; // need for book only
$page->subchapter = ( $levels[ $i] >= 2);
$page->subchapter = ( $levels[ $i] >= 2);
//check if the nexts are subchapters
// Check if the nexts are subchapters.
for ($j = $i + 1; $j < count ( $ titles ) ; $ j + + ) {
for ($j = $i + 1; $j < count ( $ titles ) ; $ j + + ) {
if ($levels[ $j] > 2) {
if ($levels[ $j] > 2) {
$page->content .= '< br > < b > < u > '.$titles[ $j].'< / u > < / b > < br > '.$texts[ $j];
$page->content .= '< br > < b > < u > '.$titles[ $j].'< / u > < / b > < br > '.$texts[ $j];
@ -279,8 +285,7 @@ function extract_data( $courseid, $modname, $id, $basedir, $subchapter, $levels,
preg_match_all('#="Pictures/([a-z .A-Z_0-9]*)"#es', $page->content, $imgs);
preg_match_all('#="Pictures/([a-z .A-Z_0-9]*)"#es', $page->content, $imgs);
foreach ($imgs[1] as $img)
foreach ($imgs[1] as $img) {
{
$src = $dirtemp.'/Pictures/'.$img;
$src = $dirtemp.'/Pictures/'.$img;
$dest = $imagedir.'/'.$img;
$dest = $imagedir.'/'.$img;
rename( $src, $dest);
rename( $src, $dest);
@ -288,63 +293,56 @@ function extract_data( $courseid, $modname, $id, $basedir, $subchapter, $levels,
$page->content = str_replace( "Pictures/$img", $imagelink."/".$img, $page->content);
$page->content = str_replace( "Pictures/$img", $imagelink."/".$img, $page->content);
}
}
// add the page to the array;
// Add the page to the array.
$extractedpages[] = $page;
$extractedpages[] = $page;
} // End $pages foreach loop.
} // end $pages foreach loop
return $extractedpages;
return $extractedpages;
}
}
/**
/*
Clean up the temp directory
Clean up the temp directory
*/
*/
function clean_temp( $base)
function clean_temp( $base) {
{
global $CFG;
global $CFG;
// this function is broken, use it to clean up later
// This function is broken, use it to clean up later.
// s hould only clean up what we made as well because someone else could be importing ppt as well
// S hould only clean up what we made as well because someone else could be importing ppt as well.
$dir = $CFG->dataroot.'/temp/'.$base;
$dir = $CFG->dataroot.'/temp/'.$base;
remove_dir( $dir);
remove_dir( $dir);
//game_full_rmdir( $dir);
}
}
/*
/**
Creates objects an chapter object that is to be inserted into the database
Creates objects an chapter object that is to be inserted into the database
*/
*/
function game_bookquiz_create_objects( $pageobjects, $bookid) {
function game_bookquiz_create_objects( $pageobjects, $bookid)
{
global $DB;
global $DB;
$chapters = array();
$chapters = array();
$lastpagenum = $DB->get_field('book_chapters', 'MAX(pagenum) as num', array( 'bookid' => $bookid));
$lastpagenum = $DB->get_field('book_chapters', 'MAX(pagenum) as num', array( 'bookid' => $bookid));
foreach ($pageobjects as $pageobject)
foreach ($pageobjects as $pageobject) {
{
$chapter = new stdClass;
$chapter = new stdClass;
// same for all chapters
// Same for all chapters.
$chapter->bookid = $bookid;
$chapter->bookid = $bookid;
$chapter->pagenum = ++$lastpagenum;
$chapter->pagenum = ++$lastpagenum;
$chapter->timecreated = time();
$chapter->timecreated = time();
$chapter->timemodified = time();
$chapter->timemodified = time();
$chapter->subchapter = 0;
$chapter->subchapter = 0;
if ($pageobject->title == '')
if ($pageobject->title == '') {
$chapter->title = "Page $count"; // no title set so make a generic one
$chapter->title = "Page $count"; // No title set so make a generic one.
else
} else {
$chapter->title = addslashes($pageobject->title);
$chapter->title = addslashes($pageobject->title);
}
$chapter->subchapter = $pageobject->subchapter;
$chapter->subchapter = $pageobject->subchapter;
$content = str_replace("\n", '', $pageobject->content);
$content = str_replace("\n", '', $pageobject->content);
$content = str_replace("\r", '', $content);
$content = str_replace("\r", '', $content);
$content = str_replace(' ', '', $content); // p uts in returns?
$content = str_replace(' ', '', $content); // P uts in returns?
$content = '< p > '.$content.'< / p > ';
$content = '< p > '.$content.'< / p > ';
$chapter->content = addslashes( $content);
$chapter->content = addslashes( $content);
@ -355,16 +353,12 @@ function game_bookquiz_create_objects( $pageobjects, $bookid)
return $chapters;
return $chapters;
}
}
/**
// Save the chapter objects to the database.
Save the chapter objects to the database
function game_bookquiz_save_objects($chapters) {
*/
function game_bookquiz_save_objects($chapters)
{
global $DB;
global $DB;
// nothing fancy, just save them all in order
// Nothing fancy, just save them all in order.
foreach ($chapters as $chapter)
foreach ($chapters as $chapter) {
{
if (!$newid = $DB->insert_record('book_chapters', $chapter)) {
if (!$newid = $DB->insert_record('book_chapters', $chapter)) {
print_error('Could not insert to table book_chapters');
print_error('Could not insert to table book_chapters');
}
}
@ -373,9 +367,8 @@ function game_bookquiz_save_objects($chapters)
return true;
return true;
}
}
//splits the data to
// Splits the data to.
function game_bookquiz_splitsections($data, & $positions, & $inputs, & $titles, & $titleframes, & $texts)
function game_bookquiz_splitsections($data, & $positions, & $inputs, & $titles, & $titleframes, & $texts) {
{
preg_match_all('#< text:h ( . * ? ) > (.*?)< / text:h > #es', $data, $matches, PREG_OFFSET_CAPTURE);
preg_match_all('#< text:h ( . * ? ) > (.*?)< / text:h > #es', $data, $matches, PREG_OFFSET_CAPTURE);
$in = $matches[ 1];
$in = $matches[ 1];
@ -387,8 +380,7 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
$oldposition = 0;
$oldposition = 0;
$oldlen = 0;
$oldlen = 0;
for($i=0; $i < count ( $ in ) ; $ i + + )
for ($i = 0; $i < count ( $ in ) ; $ i + + ) {
{
$inputs[] = $in[ $i][ 0];
$inputs[] = $in[ $i][ 0];
$newposition = $in[ $i][ 1];
$newposition = $in[ $i][ 1];
@ -397,12 +389,11 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
$titlenet = $title[ $i][ 0];
$titlenet = $title[ $i][ 0];
$titleframe = '';
$titleframe = '';
//frames inside header
// Frames inside header.
preg_match_all('#< draw:frame ( . * ? ) > (.*?)< / draw:frame > #es', $titlenet, $titlematches, PREG_OFFSET_CAPTURE);
preg_match_all('#< draw:frame ( . * ? ) > (.*?)< / draw:frame > #es', $titlenet, $titlematches, PREG_OFFSET_CAPTURE);
$frames = $titlematches[ 2];
$frames = $titlematches[ 2];
if (count( $frames) > 0) {
if (count( $frames) > 0) {
for($j=0; $j < count ( $ frames ) ; $ j + + )
for ($j = 0; $j < count ( $ frames ) ; $ j + + ) {
{
$titleframe .= $frames[ $j][ 0];
$titleframe .= $frames[ $j][ 0];
$titlenet = substr( $titlenet, $frames[ $j][ 1] + strlen( $frames[ $j][ 0]) + 13);
$titlenet = substr( $titlenet, $frames[ $j][ 1] + strlen( $frames[ $j][ 0]) + 13);
}
}
@ -417,51 +408,45 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
$oldlen = strlen( $title[ $i][ 0]) + strlen( $in[ $i][ 0]) + 10;
$oldlen = strlen( $title[ $i][ 0]) + strlen( $in[ $i][ 0]) + 10;
$oldposition = $newposition;
$oldposition = $newposition;
}
}
$newposition = strlen( $data);
$newposition = strlen( $data);
$texts[] = substr( $data, $oldposition + $oldlen, $newposition - $oldposition - $oldlen);
$texts[] = substr( $data, $oldposition + $oldlen, $newposition - $oldposition - $oldlen);
}
}
function game_bookquiz_convert( $data, $styles, & $images)
function game_bookquiz_convert( $data, $styles, & $images) {
{
$images = array();
$images = array();
// get data
// Get data.
preg_match_all('#< text:p text:style-name = "([a-z A-Z_0-9]*)" > (.*?)< / text:p > #es', $data, $text);
preg_match_all('#< text:p text:style-name = "([a-z A-Z_0-9]*)" > (.*?)< / text:p > #es', $data, $text);
$originals = $text[ 0];
$originals = $text[ 0];
$names = $text[ 1];
$names = $text[ 1];
$texts = $text[ 2];
$texts = $text[ 2];
for( $i=0; $i < count ( $ texts ) ; $ i + + )
for ($i = 0; $i < count ( $ texts ) ; $ i + + ) {
{
$name = $names[ $i];
$name = $names[ $i];
$text = $texts[ $i];
$text = $texts[ $i];
//repairs draw:frame
// Repairs draw:frame.
$pattern = "#< draw:frame draw:style-name = \"([a-z . A-Z_0-9 ] * ) \ " ( . * ? ) < draw:image xlink:href = \"Pictures/([a-z . A-Z_0-9 ] * ) ( . * ? ) < / draw:frame > #es";
$pattern = "#< draw:frame draw:style-name = \"([a-z . A-Z_0-9 ] * ) \ " ( . * ? ) < draw:image xlink:href = \"Pictures/([a-z . A-Z_0-9 ] * ) ( . * ? ) < / draw:frame > #es";
preg_match_all( $pattern, $text, $matches);
preg_match_all( $pattern, $text, $matches);
if (count( $matches[ 1]) ) {
if (count( $matches[ 1]) ) {
$new = game_bookquiz_convert_image( $matches, $styles, $images);
$new = game_bookquiz_convert_image( $matches, $styles, $images);
$data = str_replace( $originals[ $i], $new, $data);
$data = str_replace( $originals[ $i], $new, $data);
}else IF($name == 'RKRK')
} else if ($name == 'RKRK') {
{
$new = game_bookquiz_convert_RKRK( $text);
$new = game_bookquiz_convert_RKRK( $text);
$data = str_replace( $originals[ $i], $new, $data);
$data = str_replace( $originals[ $i], $new, $data);
}else
} else {
{
$new = '< P > '.game_bookquiz_convert_text( $text, $styles).'< / P > ';
$new = '< P > '.game_bookquiz_convert_text( $text, $styles).'< / P > ';
$data = str_replace( $originals[ $i], $new, $data);
$data = str_replace( $originals[ $i], $new, $data);
}
}
}
}
// repairs text:span text:style-name
// Repairs text:span text:style-name.
preg_match_all( '#< text:span text:style-name = "([a-z .A-Z_0-9]*)" > (.*?)< / text:span > #es', $data, $text);
preg_match_all( '#< text:span text:style-name = "([a-z .A-Z_0-9]*)" > (.*?)< / text:span > #es', $data, $text);
$originals = $text[ 0];
$originals = $text[ 0];
$names = $text[ 1];
$names = $text[ 1];
$texts = $text[ 2];
$texts = $text[ 2];
for( $i=0; $i < count ( $ texts ) ; $ i + + )
for ($i = 0; $i < count ( $ texts ) ; $ i + + ) {
{
$name = $names[ $i];
$name = $names[ $i];
$text = $texts[ $i];
$text = $texts[ $i];
@ -470,24 +455,21 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
if (count( $matches[ 1]) ) {
if (count( $matches[ 1]) ) {
$new = game_bookquiz_convert_image( $matches, $styles, $images);
$new = game_bookquiz_convert_image( $matches, $styles, $images);
$data = str_replace( $originals[ $i], $new, $data);
$data = str_replace( $originals[ $i], $new, $data);
}else IF($name == 'RKRK')
} else if ($name == 'RKRK') {
{
$new = game_bookquiz_convert_RKRK( $text);
$new = game_bookquiz_convert_RKRK( $text);
$data = str_replace( $originals[ $i], $new, $data);
$data = str_replace( $originals[ $i], $new, $data);
}else
} else {
{
$new = "< span " . $ styles [ $ name ] . ' > '.game_bookquiz_convert_text( $text, $styles).'< / span > ';
$new = "< span " . $ styles [ $ name ] . ' > '.game_bookquiz_convert_text( $text, $styles).'< / span > ';
$data = str_replace( $originals[ $i], $new, $data);
$data = str_replace( $originals[ $i], $new, $data);
}
}
}
}
// repairs text:a
// Repairs text:a.
preg_match_all( '#< text:a ( . * ? ) xlink:href = "(.*?)" > (.*?)< / text:a > #es', $data, $text);
preg_match_all( '#< text:a ( . * ? ) xlink:href = "(.*?)" > (.*?)< / text:a > #es', $data, $text);
$originals = $text[ 0];
$originals = $text[ 0];
$hrefs = $text[ 2];
$hrefs = $text[ 2];
$texts = $text[ 3];
$texts = $text[ 3];
for( $i=0; $i < count ( $ texts ) ; $ i + + )
for ($i = 0; $i < count ( $ texts ) ; $ i + + ) {
{
$href = $hrefs[ $i];
$href = $hrefs[ $i];
$text = $texts[ $i];
$text = $texts[ $i];
@ -495,37 +477,32 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
$data = str_replace( $originals[ $i], $new, $data);
$data = str_replace( $originals[ $i], $new, $data);
}
}
//repair text:list
// Repair text:list.
preg_match_all( '#< text:list text:style-name = "([a-z A-Z_0-9]*)" > (.*?)< / text:list > #es', $data, $text);
preg_match_all( '#< text:list text:style-name = "([a-z A-Z_0-9]*)" > (.*?)< / text:list > #es', $data, $text);
$originals = $text[ 0];
$originals = $text[ 0];
$names = $text[ 1];
$names = $text[ 1];
$texts = $text[ 2];
$texts = $text[ 2];
for( $i=0; $i < count ( $ texts ) ; $ i + + )
for ($i = 0; $i < count ( $ texts ) ; $ i + + ) {
{
$new = '< UL > '.$texts[ $i].'< / UL > ';
$new = '< UL > '.$texts[ $i].'< / UL > ';
$data = str_replace( $originals[ $i], $new, $data);
$data = str_replace( $originals[ $i], $new, $data);
//I have to repair the listitems
// I have to repair the listitems.
preg_match_all( '#< text:list-item > (.*?)< / text:list-item > #es', $data, $listitems);
preg_match_all( '#< text:list-item > (.*?)< / text:list-item > #es', $data, $listitems);
$originallistitems = $listitems[ 0];
$originallistitems = $listitems[ 0];
$items = $listitems[ 1];
$items = $listitems[ 1];
for ($j = 0; $j < count ( $ items ) ; $ j + + ) {
for ($j = 0; $j < count ( $ items ) ; $ j + + ) {
$new = '< LI > '.$items[ $j];
$new = '< LI > '.$items[ $j];
$data = str_replace( $originallistitems[ $j], $new, $data);
$data = str_replace( $originallistitems[ $j], $new, $data);
}
}
}
}
$data = str_replace( '< text:line-break / > ', '< br > ', $data);
$data = str_replace( '< text:line-break / > ', '< br > ', $data);
return $data;
return $data;
}
}
function game_bookquiz_convert_text( $text, $styles)
function game_bookquiz_convert_text( $text, $styles) {
{
$pattern = "#< text:span text:style-name = \"([a-z . A-Z_0-9 ] * ) \ " > (.*?)< / text:span > #es";
$pattern = "#< text:span text:style-name = \"([a-z . A-Z_0-9 ] * ) \ " > (.*?)< / text:span > #es";
preg_match_all( $pattern, $text, $matches);
preg_match_all( $pattern, $text, $matches);
@ -533,8 +510,7 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
$names = $matches[ 1];
$names = $matches[ 1];
$spantexts = $matches[ 2];
$spantexts = $matches[ 2];
for( $i=0; $i < count ( $ names ) ; $ i + + )
for ($i = 0; $i < count ( $ names ) ; $ i + + ) {
{
$name = $names[ $i];
$name = $names[ $i];
$style = $styles[ $name];
$style = $styles[ $name];
@ -545,8 +521,7 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
return $text;
return $text;
}
}
function game_bookquiz_convert_image( $matches, $xmlstyles, & $images)
function game_bookquiz_convert_image( $matches, $xmlstyles, & $images) {
{
$ret = '';
$ret = '';
$styles = $matches[ 1];
$styles = $matches[ 1];
@ -562,134 +537,46 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
return $ret;
return $ret;
}
}
function game_bookquiz_convert_RKRK( $text)
function game_bookquiz_convert_rkrk( $text) {
{
$table = base64_decode($text);
$table = base64_decode($text);
$table = stripslashes($table);
$table = stripslashes($table);
$table = strtr($table, array('< / table:table > ' => '< / table > ', '< table:table-row > ' => '< tr > ', '< / table:table-row > ' => '< / tr > ', '< / table:table-cell > ' => '< / td > ', '< / table:table-header-rows > ' => '', '< table:table-header-rows > ' => '', '>' => ">\n", '< / text:p > ' => ''));
$table = strtr($table, array('< / table:table > ' => '< / table > ', '< table:table-row > ' => '< tr > ', '< / table:table-row > ' => '< / tr > ', '< / table:table-cell > ' => '< / td > ', '< / table:table-header-rows > ' => '', '< table:table-header-rows > ' => '', '>' => ">\n", '< / text:p > ' => ''));
//preg_match_all('#table:name="([a-z A-Z_0-9]*)" table:style-name="([a-z A-Z_0-9]*)">#es', $table, $repl);
preg_match_all('#table:name="(.*?)" table:style-name="(.*?)">#es', $table, $repl);
preg_match_all('#table:name="(.*?)" table:style-name="(.*?)">#es', $table, $repl);
foreach($repl[0] as $val)
foreach ($repl[0] as $val) {
{
//$table = str_replace($val, '< table border = "1" > < tr > < td > ', $table);
$table = str_replace($val, '< table border = "1" > < tr > ', $table);
$table = str_replace($val, '< table border = "1" > < tr > ', $table);
}
}
//preg_match_all('#< text:p text:style-name = "([a-z A-Z_0-9]*)" > #es', $table, $repl);
preg_match_all('#< text:p text:style-name = "(.*?)" > #es', $table, $repl);
preg_match_all('#< text:p text:style-name = "(.*?)" > #es', $table, $repl);
foreach($repl[0] as $key => $val)
foreach ($repl[0] as $key => $val) {
{
$table = str_replace($val, '', $table);
$table = str_replace($val, '', $table);
}
}
preg_match_all('#< table:table-column ( . * ? ) " > #es', $table, $repl);
preg_match_all('#< table:table-column ( . * ? ) " > #es', $table, $repl);
foreach($repl[0] as $val)
foreach ($repl[0] as $val) {
{
$table = str_replace($val, '', $table);
$table = str_replace($val, '', $table);
}
}
//preg_match_all('#< table:table-cell table:style-name = "([\.a-z A-Z_0-9]*)" office:value-type = "([a-z A-Z_0-9]*)" > #es', $table, $repl);
preg_match_all('#< table:table-cell table:style-name = "(.*?)" office:value-type = (.*?)" > #es', $table, $repl);
preg_match_all('#< table:table-cell table:style-name = "(.*?)" office:value-type = (.*?)" > #es', $table, $repl);
foreach($repl[0] as $val)
foreach ($repl[0] as $val) {
{
$table = str_replace($val, '< td > ', $table);
$table = str_replace($val, '< td > ', $table);
}
}
//maybe there are a lot of pictures inside a table
// Maybe there are a lot of pictures inside a table.
preg_match_all('#xlink:href="Pictures/([a-z.A-Z_0-9]*)"#es', $table, $repl);
preg_match_all('#xlink:href="Pictures/([a-z.A-Z_0-9]*)"#es', $table, $repl);
foreach( $repl[ 1] as $picture)
foreach ($repl[ 1] as $picture) {
{
$table = str_replace('< draw:image xlink:href = "Pictures/'.$picture.'" xlink:type = "simple" xlink:show = "embed" xlink:actuate = "onLoad" / > ', '< img src = "Pictures/'.$picture.'" > ', $table);
$table = str_replace('< draw:image xlink:href = "Pictures/'.$picture.'" xlink:type = "simple" xlink:show = "embed" xlink:actuate = "onLoad" / > ', '< img src = "Pictures/'.$picture.'" > ', $table);
}
}
if( strpos( $table, "< / table > ") === false)
if (strpos( $table, "< / table > ") === false) {
$table .= "< / table > ";
$table .= "< / table > ";
}
$ret = '< BR > '.$table.'< BR > ';
$ret = '< BR > '.$table.'< BR > ';
return $ret;
return $ret;
}
}
/*
function game_bookquiz_oo_unzip($file, $dir)
{
unzip_file ( $file, $dir, false);
$dir .= '/';
if( file_exists( $dir.'content.xml')){
$content = file_get_contents( $dir.'content.xml');
}else
{
$content = '';
}
if( file_exists( $dir.'styles.xml')){
$contentstyles = file_get_contents( $dir.'styles.xml');
}else
{
$contentstyles = '';
}
$img = array();
$handle = opendir($dir.'Pictures');
while (false!==($item = readdir($handle))) {
if($item != '.' & & $item != '..') {
if(!is_dir($dir.'/'.$item)) {
$img[ $item] = file_get_contents( $dir.'/'.$item);
}else{
unlink($dir.'/'.$item);
}
}
}
}
*/
function old_game_bookquiz_oo_unzip($file, $save, $dir)
{
IF($zip = game_zip_open($file))
{
while ($zip_entry = game_zip_read($zip))
{
$filename = game_zip_entry_name($zip_entry);
IF($filename == 'content.xml' and game_zip_entry_open($zip, $zip_entry, "r"))
{
$content = game_zip_entry_read($zip_entry, game_zip_entry_filesize($zip_entry));
game_zip_entry_close($zip_entry);
}
IF( $filename == 'styles.xml' and game_zip_entry_open($zip, $zip_entry, "r"))
{
$contentstyles = game_zip_entry_read($zip_entry, game_zip_entry_filesize($zip_entry));
game_zip_entry_close($zip_entry);
}
IF(ereg('Pictures/', $filename) and !ereg('Object', $filename) and game_zip_entry_open($zip, $zip_entry, "r"))
function game_bookquiz_deletebook( $courseid, $bookid) {
{
$img[$filename] = game_zip_entry_read($zip_entry, game_zip_entry_filesize($zip_entry));
game_zip_entry_close($zip_entry);
}
}
IF(isset($content))
{
IF($save == false)
return array($content, $img);
else
{
file_put_contents("$dir/content.xml", $content);
IF(isset($contentstyles)){
file_put_contents("$dir/styles.xml", $contentstyles);
}
IF(is_array($img))
{
IF(!is_dir("$dir/Pictures"))
mkdir( "$dir/Pictures");
foreach($img as $key => $val)
file_put_contents("$dir/$key", $val);
}
}
}
}
}
function game_bookquiz_deletebook( $courseid, $bookid)
{
global $CFG;
global $CFG;
if (!delete_records( 'book_chapters', 'bookid', $bookid)) {
if (!delete_records( 'book_chapters', 'bookid', $bookid)) {
@ -697,11 +584,9 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
}
}
game_full_rmdir( "$CFG->dataroot/$courseid/moddata/book/$bookid");
game_full_rmdir( "$CFG->dataroot/$courseid/moddata/book/$bookid");
}
}
function game_bookquiz_convert_ver2_computestyles( $data, & $styles, $isstyle)
function game_bookquiz_convert_ver2_computestyles( $data, & $styles, $isstyle) {
{
preg_match_all('#< style :style style:name = "(.*?)" ( . * ? ) > ( . * ? ) < / s t y l e : s t y l e > # e s ' , $ d a t a , $ s t y l e ) ;
preg_match_all('#< style :style style:name = "(.*?)" ( . * ? ) > ( . * ? ) < / s t y l e : s t y l e > # e s ' , $ d a t a , $ s t y l e ) ;
$stylenames = $style[ 1];
$stylenames = $style[ 1];
@ -720,15 +605,17 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
if ($pos1 > $pos2) {
if ($pos1 > $pos2) {
break;
break;
}
}
//is a parent style
// Is a parent style.
$s = substr( $styledatas[ $i], 0, $pos2 + 2);
$s = substr( $styledatas[ $i], 0, $pos2 + 2);
game_bookquiz_convertstyle_parent( $s, $styles);
game_bookquiz_convertstyle_parent( $s, $styles);
$styledatas[ $i] = substr( $styledatas[ $i], $pos2 + 2);
$styledatas[ $i] = substr( $styledatas[ $i], $pos2 + 2);
$change = true;
$change = true;
}
}
if ($change) {
if ($change) {
//Must to recompute name, styledatas, styleinfos
// Must to recompute name, styledatas, styleinfos.
preg_match_all('#< style :style style:name = "(.*?)" ( . * ? ) > ( . * ? ) < / s t y l e : s t y l e > # e s ' , $ d a t a , $ s t y l e ) ;
preg_match_all('#< style :style style:name = "(.*?)" ( . * ? ) > ( . * ? ) < / s t y l e : s t y l e > # e s ' , $ d a t a , $ s t y l e ) ;
$name = $style[ 1][ 0];
$name = $style[ 1][ 0];
$styleinfos[ $i] = $style[ 2][ 0];
$styleinfos[ $i] = $style[ 2][ 0];
@ -739,8 +626,7 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
}
}
}
}
function game_bookquiz_convertstyle_parent( $data, & $styles)
function game_bookquiz_convertstyle_parent( $data, & $styles) {
{
$styleitems = array();
$styleitems = array();
preg_match_all( '#(.*?)style:name="(.*?)"(.*?)style:parent-style-name="(.*?)"(.*?)#es', $data, $infos);
preg_match_all( '#(.*?)style:name="(.*?)"(.*?)style:parent-style-name="(.*?)"(.*?)#es', $data, $infos);
@ -748,7 +634,7 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
$parents = $infos[ 4];
$parents = $infos[ 4];
if (count( $parents)) {
if (count( $parents)) {
if (array_key_exists( $parents[ 0], $styles)) {
if (array_key_exists( $parents[ 0], $styles)) {
//i s a child style. Must to copy the properties of the parent style
// I s a child style. Must to copy the properties of the parent style.
$a = explode( ';', $styles[ $parents[ 0]]);
$a = explode( ';', $styles[ $parents[ 0]]);
foreach ($a as $s) {
foreach ($a as $s) {
$pos = strpos( $s, ':');
$pos = strpos( $s, ':');
@ -767,17 +653,17 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
foreach ($styleitems as $key => $item) {
foreach ($styleitems as $key => $item) {
$style .= ';'.$key.':'.$item;
$style .= ';'.$key.':'.$item;
}
}
$styles[ $name] = substr( $style, 1);
$styles[ $name] = substr( $style, 1);
}
}
function game_bookquiz_convertstyle( $data, $styleinfo, $styles)
function game_bookquiz_convertstyle( $data, $styleinfo, $styles) {
{
$styleitems = array();
$styleitems = array();
preg_match_all( '#< style :paragraph-properties ( . * ? ) / > # e s ' , $ d a t a , $ i n f o s ) ;
preg_match_all( '#< style :paragraph-properties ( . * ? ) / > # e s ' , $ d a t a , $ i n f o s ) ;
$lines = $infos[ 1];
$lines = $infos[ 1];
if (count( $lines)) {
if (count( $lines)) {
$line = $lines[ 0]; //print_object( $lines) ;
$line = $lines[ 0] ;
if ($line != '') {
if ($line != '') {
game_bookquiz_convertstyle_paragraph( $line, $styleitems);
game_bookquiz_convertstyle_paragraph( $line, $styleitems);
@ -806,8 +692,7 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
return substr( $style, 1);
return substr( $style, 1);
}
}
function game_bookquiz_convertstyle_paragraph( $line, & $styleitems)
function game_bookquiz_convertstyle_paragraph( $line, & $styleitems) {
{
preg_match_all( '#(.*?)=(.*?) #es', $line.' ', $datas);
preg_match_all( '#(.*?)=(.*?) #es', $line.' ', $datas);
$data1 = $datas[ 1];
$data1 = $datas[ 1];
$data2 = $datas[ 2];
$data2 = $datas[ 2];
@ -824,7 +709,6 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
switch ($eq1) {
switch ($eq1) {
case 'fo:text-align':
case 'fo:text-align':
$styleitems[ 'align'] = $eq2;
$styleitems[ 'align'] = $eq2;
//print_object( $styleitems);
break;
break;
case 'fo:background-color':
case 'fo:background-color':
$styleitems[ 'background-color'] = $eq2;
$styleitems[ 'background-color'] = $eq2;
@ -833,8 +717,7 @@ function game_bookquiz_splitsections($data, &$positions, &$inputs, &$titles, &$t
}
}
}
}
function game_bookquiz_convertstyle_textproperties( $line, & $styleitems)
function game_bookquiz_convertstyle_textproperties( $line, & $styleitems) {
{
preg_match_all( '#(.*?)=(.*?) #es', $line.' ', $datas);
preg_match_all( '#(.*?)=(.*?) #es', $line.' ', $datas);
$data1 = $datas[ 1];
$data1 = $datas[ 1];
$data2 = $datas[ 2];
$data2 = $datas[ 2];