MDL-28526 glossary: preserve images across export/import

This commit is contained in:
Marina Glancy 2015-03-13 17:44:49 +08:00
parent a149d6a177
commit 27d01f7c8c
2 changed files with 82 additions and 0 deletions

View File

@ -82,6 +82,7 @@ if ($xml = glossary_read_imported_file($result)) {
$importedcats = 0;
$entriesrejected = 0;
$rejections = '';
$glossarycontext = $context;
if ($data->dest == 'newglossary') {
// If the user chose to create a new glossary
@ -147,6 +148,8 @@ if ($xml = glossary_read_imported_file($result)) {
echo $OUTPUT->footer();
exit;
} else {
$glossarycontext = context_module::instance($glossary->coursemodule);
glossary_xml_import_files($xmlglossary, 'INTROFILES', $glossarycontext->id, 'intro', 0);
echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
}
} else {
@ -259,6 +262,15 @@ if ($xml = glossary_read_imported_file($result)) {
}
}
}
// Import files embedded in the entry text.
glossary_xml_import_files($xmlentry['#'], 'ENTRYFILES', $glossarycontext->id, 'entry', $newentry->id);
// Import files attached to the entry.
if (glossary_xml_import_files($xmlentry['#'], 'ATTACHMENTFILES', $glossarycontext->id, 'attachment', $newentry->id)) {
$DB->update_record("glossary_entries", array('id' => $newentry->id, 'attachment' => '1'));
}
} else {
$entriesrejected++;
if ( $newentry->concept and $newentry->definition ) {

View File

@ -2262,6 +2262,9 @@ function glossary_generate_export_csv($entries, $aliases, $categories) {
function glossary_generate_export_file($glossary, $ignored = "", $hook = 0) {
global $CFG, $DB;
$cm = get_coursemodule_from_instance('glossary', $glossary->id, $glossary->course);
$context = context_module::instance($cm->id);
$co = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$co .= glossary_start_tag("GLOSSARY",0,true);
@ -2279,6 +2282,7 @@ function glossary_generate_export_file($glossary, $ignored = "", $hook = 0) {
$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);
$co .= glossary_xml_export_files('INTROFILES', 2, $context->id, 'intro', 0);
if ( $entries = $DB->get_records("glossary_entries", array("glossaryid"=>$glossary->id))) {
$co .= glossary_start_tag("ENTRIES",2,true);
@ -2338,6 +2342,12 @@ function glossary_generate_export_file($glossary, $ignored = "", $hook = 0) {
$co .= glossary_end_tag("CATEGORIES",4,true);
}
// Export files embedded in entries.
$co .= glossary_xml_export_files('ENTRYFILES', 4, $context->id, 'entry', $entry->id);
// Export attachments.
$co .= glossary_xml_export_files('ATTACHMENTFILES', 4, $context->id, 'attachment', $entry->id);
$co .= glossary_end_tag("ENTRY",3,true);
}
}
@ -2420,6 +2430,66 @@ function glossary_full_tag($tag,$level=0,$endline=true,$content) {
return $st.$co.$et;
}
/**
* Prepares file area to export as part of XML export
*
* @param string $tag XML tag to use for the group
* @param int $taglevel
* @param int $contextid
* @param string $filearea
* @param int $itemid
* @return string
*/
function glossary_xml_export_files($tag, $taglevel, $contextid, $filearea, $itemid) {
$co = '';
$fs = get_file_storage();
if ($files = $fs->get_area_files(
$contextid, 'mod_glossary', $filearea, $itemid, 'itemid,filepath,filename', false)) {
$co .= glossary_start_tag($tag, $taglevel, true);
foreach ($files as $file) {
$co .= glossary_start_tag('FILE', $taglevel + 1, true);
$co .= glossary_full_tag('FILENAME', $taglevel + 2, false, $file->get_filename());
$co .= glossary_full_tag('FILEPATH', $taglevel + 2, false, $file->get_filepath());
$co .= glossary_full_tag('CONTENTS', $taglevel + 2, false, base64_encode($file->get_content()));
$co .= glossary_end_tag('FILE', $taglevel + 1);
}
$co .= glossary_end_tag($tag, $taglevel);
}
return $co;
}
/**
* Parses files from XML import and inserts them into file system
*
* @param array $xmlparent parent element in parsed XML tree
* @param string $tag
* @param int $contextid
* @param string $filearea
* @param int $itemid
* @return int
*/
function glossary_xml_import_files($xmlparent, $tag, $contextid, $filearea, $itemid) {
$count = 0;
if (isset($xmlparent[$tag][0]['#']['FILE'])) {
$fs = get_file_storage();
$files = $xmlparent[$tag][0]['#']['FILE'];
foreach ($files as $file) {
$filerecord = array(
'contextid' => $contextid,
'component' => 'mod_glossary',
'filearea' => $filearea,
'itemid' => $itemid,
'filepath' => $file['#']['FILEPATH'][0]['#'],
'filename' => $file['#']['FILENAME'][0]['#'],
);
$content = $file['#']['CONTENTS'][0]['#'];
$fs->create_file_from_string($filerecord, base64_decode($content));
$count++;
}
}
return $count;
}
/**
* How many unrated entries are in the given glossary for a given user?
*