utf8 related cleanup in glossary import and export code - please test

This commit is contained in:
skodak 2006-03-03 19:49:08 +00:00
parent dcd51df25e
commit 212039c0d8
5 changed files with 106 additions and 143 deletions

View file

@ -2,7 +2,6 @@
require_once("../../config.php");
require_once("lib.php");
global $CFG, $USER;
$id = required_param('id', PARAM_INT); // Course Module ID
@ -34,6 +33,7 @@
$strsearchconcept = get_string("searchconcept", "glossary");
$strsearchindefinition = get_string("searchindefinition", "glossary");
$strsearch = get_string("search");
$strexportfile = get_string("exportfile", "glossary");
$navigation = "";
if ($course->category) {
@ -56,21 +56,20 @@
}
/// Tabbed browsing sections
$lastl = $l;
$lastcat = $cat;
$tab = GLOSSARY_EXPORT_VIEW;
include("tabs.html");
glossary_generate_export_file($glossary,$lastl,$lastcat);
print_string("glosssaryexported","glossary");
$ffurl = "/$course->id/glossary/" . clean_filename(strip_tags(format_string($glossary->name,true))) ."/glossary.xml";
if ($CFG->slasharguments) {
$ffurl = "../../file.php$ffurl" ;
} else {
$ffurl = "../../file.php?file=$ffurl";
}
echo '<p align="center"><a href="' . $ffurl . '" target="_blank">' . get_string("exportedfile","glossary") . '</a></p>';
?>
<form action="exportfile.php" method="post">
<table border="0" cellpadding="6" cellspacing="6" width="100%"><tr>
<tr><td align="center">
<input type="submit" value="<?php p($strexportfile)?>">
</td></tr></table>
<input type="hidden" name="id" value="<?php p($id)?>" />
<input type="hidden" name="l" value="<?php p($l)?>" />
<input type="hidden" name="cat" value="<?php p($cat)?>" />
</form>
<?php
echo '</center>';
glossary_print_tabbed_table_end();

View file

@ -0,0 +1,32 @@
<?php // $Id$
require_once("../../config.php");
require_once("lib.php");
$id = required_param('id', PARAM_INT); // Course Module ID
$l = optional_param('l','', PARAM_ALPHANUM);
$cat = optional_param('cat',0, PARAM_ALPHANUM);
if (! $cm = get_record("course_modules", "id", $id)) {
error("Course Module ID was incorrect");
}
if (! $course = get_record("course", "id", $cm->course)) {
error("Course is misconfigured");
}
if (! $glossary = get_record("glossary", "id", $cm->instance)) {
error("Course module is incorrect");
}
require_login($course->id, false);
if (!isteacher($course->id)) {
error("You must be a teacher to use this page.");
}
$filename = clean_filename(strip_tags(format_string($glossary->name,true))).'.xml';
$content = glossary_generate_export_file($glossary,$l,$cat);
send_file($content, $filename, 0, 0, true, true);
?>

View file

@ -97,10 +97,10 @@
if ( $xmlglossary['NAME'][0]['#'] ) {
unset($glossary);
$glossary->name = addslashes(utf8_decode($xmlglossary['NAME'][0]['#']));
$glossary->name = addslashes($xmlglossary['NAME'][0]['#']);
$glossary->course = $course->id;
$glossary->globalglossary = addslashes($xmlglossary['GLOBALGLOSSARY'][0]['#']);
$glossary->intro = addslashes(utf8_decode($xmlglossary['INTRO'][0]['#']));
$glossary->intro = addslashes($xmlglossary['INTRO'][0]['#']);
$glossary->showspecial = addslashes($xmlglossary['SHOWSPECIAL'][0]['#']);
$glossary->showalphabet = addslashes($xmlglossary['SHOWALPHABET'][0]['#']);
$glossary->showall = addslashes($xmlglossary['SHOWALL'][0]['#']);
@ -210,8 +210,8 @@
// Inserting the entries
$xmlentry = $xmlentries[$i];
unset($newentry);
$newentry->concept = addslashes(trim(utf8_decode($xmlentry['#']['CONCEPT'][0]['#'])));
$newentry->definition = addslashes(utf8_decode($xmlentry['#']['DEFINITION'][0]['#']));
$newentry->concept = trim(addslashes($xmlentry['#']['CONCEPT'][0]['#']));
$newentry->definition = addslashes($xmlentry['#']['DEFINITION'][0]['#']);
if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
$newentry->casesensitive = addslashes($xmlentry['#']['CASESENSITIVE'][0]['#']);
} else {
@ -272,7 +272,7 @@
if (!empty($aliasname)) {
unset($newalias);
$newalias->entryid = $newentry->id;
$newalias->alias = addslashes(trim(utf8_decode($aliasname)));
$newalias->alias = trim(addslashes($aliasname));
$newalias->id = insert_record("glossary_alias",$newalias);
}
}
@ -284,7 +284,7 @@
$xmlcat = $xmlcats[$k];
unset($newcat);
$newcat->name = addslashes(utf8_decode($xmlcat['#']['NAME'][0]['#']));
$newcat->name = addslashes($xmlcat['#']['NAME'][0]['#']);
$newcat->usedynalink = addslashes($xmlcat['#']['USEDYNALINK'][0]['#']);
if ( !$category = get_record("glossary_categories","glossaryid",$glossary->id,"name",$newcat->name) ) {
// Create the category if it does not exist

View file

@ -1626,30 +1626,28 @@ function glossary_print_dynaentry($courseid, $entries, $displayformat = -1) {
}
function glossary_generate_export_file($glossary, $hook = "", $hook = 0) {
global $CFG;
glossary_check_moddata_dir($glossary);
global $CFG;
if (!$h = glossary_open_xml($glossary)) {
error("An error occurred while opening a file to write to.");
}
$co = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$status = fwrite ($h,glossary_start_tag("INFO",1,true));
fwrite ($h,glossary_full_tag("NAME",2,false,$glossary->name));
fwrite ($h,glossary_full_tag("INTRO",2,false,$glossary->intro));
fwrite ($h,glossary_full_tag("STUDENTCANPOST",2,false,$glossary->studentcanpost));
fwrite ($h,glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries));
fwrite ($h,glossary_full_tag("DISPLAYFORMAT",2,false,$glossary->displayformat));
fwrite ($h,glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial));
fwrite ($h,glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet));
fwrite ($h,glossary_full_tag("SHOWALL",2,false,$glossary->showall));
fwrite ($h,glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments));
fwrite ($h,glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink));
fwrite ($h,glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval));
fwrite ($h,glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary));
fwrite ($h,glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage));
$co .= glossary_start_tag("GLOSSARY",0,true);
$co .= glossary_start_tag("INFO",1,true);
$co .= glossary_full_tag("NAME",2,false,$glossary->name);
$co .= glossary_full_tag("INTRO",2,false,$glossary->intro);
$co .= glossary_full_tag("STUDENTCANPOST",2,false,$glossary->studentcanpost);
$co .= glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries);
$co .= glossary_full_tag("DISPLAYFORMAT",2,false,$glossary->displayformat);
$co .= glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial);
$co .= glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet);
$co .= glossary_full_tag("SHOWALL",2,false,$glossary->showall);
$co .= glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments);
$co .= glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink);
$co .= glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval);
$co .= glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary);
$co .= glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage);
if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
$status = fwrite ($h,glossary_start_tag("ENTRIES",2,true));
$co .= glossary_start_tag("ENTRIES",2,true);
foreach ($entries as $entry) {
$permissiongranted = 1;
if ( $hook ) {
@ -1675,81 +1673,54 @@ global $CFG;
}
}
if ( $entry->approved and $permissiongranted ) {
$status = fwrite($h,glossary_start_tag("ENTRY",3,true));
fwrite($h,glossary_full_tag("CONCEPT",4,false,trim($entry->concept)));
fwrite($h,glossary_full_tag("DEFINITION",4,false,$entry->definition));
fwrite($h,glossary_full_tag("FORMAT",4,false,$entry->format));
fwrite($h,glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink));
fwrite($h,glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive));
fwrite($h,glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch));
fwrite($h,glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry));
$co .= glossary_start_tag("ENTRY",3,true);
$co .= glossary_full_tag("CONCEPT",4,false,trim($entry->concept));
$co .= glossary_full_tag("DEFINITION",4,false,$entry->definition);
$co .= glossary_full_tag("FORMAT",4,false,$entry->format);
$co .= glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink);
$co .= glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive);
$co .= glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch);
$co .= glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry);
if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
$status = fwrite ($h,glossary_start_tag("ALIASES",4,true));
$co .= glossary_start_tag("ALIASES",4,true);
foreach ($aliases as $alias) {
$status = fwrite ($h,glossary_start_tag("ALIAS",5,true));
fwrite($h,glossary_full_tag("NAME",6,false,trim($alias->alias)));
$status = fwrite($h,glossary_end_tag("ALIAS",5,true));
$co .= glossary_start_tag("ALIAS",5,true);
$co .= glossary_full_tag("NAME",6,false,trim($alias->alias));
$co .= glossary_end_tag("ALIAS",5,true);
}
$status = fwrite($h,glossary_end_tag("ALIASES",4,true));
$co .= glossary_end_tag("ALIASES",4,true);
}
if ( $catentries = get_records("glossary_entries_categories","entryid",$entry->id) ) {
$status = fwrite ($h,glossary_start_tag("CATEGORIES",4,true));
$co .= glossary_start_tag("CATEGORIES",4,true);
foreach ($catentries as $catentry) {
$category = get_record("glossary_categories","id",$catentry->categoryid);
$status = fwrite ($h,glossary_start_tag("CATEGORY",5,true));
fwrite($h,glossary_full_tag("NAME",6,false,$category->name));
fwrite($h,glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink));
$status = fwrite($h,glossary_end_tag("CATEGORY",5,true));
$co .= glossary_start_tag("CATEGORY",5,true);
$co .= glossary_full_tag("NAME",6,false,$category->name);
$co .= glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink);
$co .= glossary_end_tag("CATEGORY",5,true);
}
$status = fwrite($h,glossary_end_tag("CATEGORIES",4,true));
$co .= glossary_end_tag("CATEGORIES",4,true);
}
$status =fwrite($h,glossary_end_tag("ENTRY",3,true));
$co .= glossary_end_tag("ENTRY",3,true);
}
}
$status =fwrite ($h,glossary_end_tag("ENTRIES",2,true));
$co .= glossary_end_tag("ENTRIES",2,true);
}
$status =fwrite ($h,glossary_end_tag("INFO",1,true));
$co .= glossary_end_tag("INFO",1,true);
$co .= glossary_end_tag("GLOSSARY",0,true);
$h = glossary_close_xml($h);
}
// Functions designed by Eloy Lafuente
//
//Function to create, open and write header of the xml file
function glossary_open_xml($glossary) {
global $CFG;
$status = true;
//Open for writing
$glossaryname = clean_filename(strip_tags(format_string($glossary->name,true)));
$pathname = make_upload_directory("$glossary->course/glossary/$glossaryname");
$filename = "$pathname/glossary.xml";
if (!$h = fopen($filename,"w")) {
notify("Error opening '$filename'");
return false;
}
//Writes the header
$status = fwrite ($h,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
if ($status) {
$status = fwrite ($h,glossary_start_tag("GLOSSARY",0,true));
}
if ($status) {
return $h;
} else {
return false;
}
return $co;
}
/// Functions designed by Eloy Lafuente
/// Functions to create, open and write header of the xml file
// Read import file and convert to current charset
function glossary_read_imported_file($file) {
require_once "../../lib/xmlize.php";
$h = fopen($file,"r");
@ -1761,13 +1732,12 @@ require_once "../../lib/xmlize.php";
}
fclose($h);
}
if (current_charset() != 'UTF-8') {
$textlib = textlib_get_instance();
$line = $textlib->convert($line, 'UTF-8', current_charset());
}
return xmlize($line, 0);
}
//Close the file
function glossary_close_xml($h) {
$status = fwrite ($h,glossary_end_tag("GLOSSARY",0,true));
return fclose($h);
}
//Return the xml start tag
function glossary_start_tag($tag,$level=0,$endline=false) {
@ -1790,53 +1760,17 @@ function glossary_end_tag($tag,$level=0,$endline=true) {
}
//Return the start tag, the contents and the end tag
function glossary_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
function glossary_full_tag($tag,$level=0,$endline=true,$content) {
$st = glossary_start_tag($tag,$level,$endline);
$co="";
if ($to_utf) {
$co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
} else {
$co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
if (current_charset() != 'UFT-8') {
$textlib = textlib_get_instance();
$content = $textlib->convert($content, current_charset(), 'UTF-8');
}
$co = preg_replace("/\r\n|\r/", "\n", s($content));
$et = glossary_end_tag($tag,0,true);
return $st.$co.$et;
}
//Function to check and create the needed moddata dir to
//save all the mod backup files. We always name it moddata
//to be able to restore it, but in restore we check for
//$CFG->moddata !!
function glossary_check_moddata_dir($glossary) {
global $CFG;
$status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course",true);
if ( $status ) {
$status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary",true);
if ( $status ) {
$status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary/". clean_filename(strip_tags(format_string($glossary->name,true))),true);
}
}
return $status;
}
//Function to check if a directory exists
//and, optionally, create it
function glossary_check_dir_exists($dir,$create=false) {
global $CFG;
$status = true;
if(!is_dir($dir)) {
if (!$create) {
$status = false;
} else {
umask(0000);
$status = mkdir ($dir,$CFG->directorypermissions);
}
}
return $status;
}
/*
* Adding grading functions
*/