Commit 91be4c3d authored by Son Nguyen's avatar Son Nguyen
Browse files

Enhance the exporting process (Bug #1081309)



1. During the exporting, put the error messages into $SESSION.
2. Refactor the displayMessages() function to display all messages
or hide the previous one.
3. Refactor the function print_export_footer() to handle html code in
lang strings.

Change-Id: I589822dc360e8f5bcac0298f9534824513214160
Signed-off-by: default avatarSon Nguyen <son.nguyen@catalyst.net.nz>
parent 31ded6b2
......@@ -224,7 +224,6 @@ if (!empty($exporterrors)) {
// the download. Here it would be nice to trigger the download for everyone,
// but alas this is not possible for people without javascript.
$SESSION->set('exportfile', $zipfile);
$wwwroot = get_config('wwwroot');
$strexportgeneratedsuccessfullyjs = get_string('exportgeneratedsuccessfullyjs', 'export', '<a href="' . $wwwroot . '" target="_top">', '</a>');
$strexportgeneratedsuccessfully = get_string('exportgeneratedsuccessfully', 'export', '<a href="bulkdownload.php" target="_top">', '</a>');
print_export_footer($strexportgeneratedsuccessfully, $strexportgeneratedsuccessfullyjs, 'bulkdownload.php');
$continueurljs = get_config('wwwroot');
$continueurl = 'bulkdownload.php';
print_export_footer(get_string('exportgeneratedsuccessfully1', 'export'), $continueurl, $continueurljs, 'bulkdownload.php');
......@@ -41,6 +41,7 @@ class HtmlExportFile extends HtmlExportArtefactPlugin {
private $otherfiles = array();
public function dump_export_data() {
global $SESSION;
$this->owner = $this->exporter->get('user')->get('id');
......@@ -89,7 +90,7 @@ class HtmlExportFile extends HtmlExportArtefactPlugin {
}
$dest = $this->extrafileroot . $id . '-' . PluginExportHtml::sanitise_path($this->artefactdata[$id]->get('title'));
if (!copy($this->artefactdata[$id]->get_path(), $dest)) {
throw new SystemException("Unable to copy artefact $id's file");
$SESSION->add_error_msg(get_string('unabletocopyartefact', 'export', $this->artefactdata[$id]->get('title')));
}
}
}
......@@ -114,21 +115,27 @@ class HtmlExportFile extends HtmlExportArtefactPlugin {
* Puts all profile icons in the static/profileicons/ directory
*/
private function populate_profileicons() {
global $SESSION;
$profileiconsdir = $this->exporter->get('exportdir') . '/' . $this->exporter->get('rootdir') . '/static/profileicons/';
$removekeys = array();
foreach ($this->artefactdata as $artefactid => $artefact) {
if ($artefact->get('artefacttype') == 'profileicon') {
$removekeys[] = $artefactid;
if (!copy($artefact->get_path(), $profileiconsdir . PluginExportHtml::sanitise_path($artefact->get('title')))) {
throw new SystemException("Unable to copy profile icon $artefactid into export");
if (!$profileiconpath = $artefact->get_path()) {
$SESSION->add_error_msg(get_string('nonexistentprofileicon', 'export', $artefact->get('title')));
}
else if (!copy($profileiconpath, $profileiconsdir . PluginExportHtml::sanitise_path($artefact->get('title')))) {
$SESSION->add_error_msg(get_string('unabletocopyprofileicon', 'export', $artefact->get('title')));
}
// Make sure we grab a nicely resized version too
$maxdimension = 200;
$resizedpath = get_dataroot_image_path('artefact/file/profileicons/', $artefactid, $maxdimension);
if (!copy($resizedpath, $profileiconsdir . $maxdimension . 'px-' . PluginExportHtml::sanitise_path($artefact->get('title')))) {
throw new SystemException("Unable to copy resized profile icon {$maxdimension}px-{$artefact->get('title')} into export");
if (!$resizedpath = get_dataroot_image_path('artefact/file/profileicons/', $artefactid, $maxdimension)) {
$SESSION->add_error_msg(get_string('nonexistentresizedprofileicon', 'export', $maxdimension . 'px-' . $artefact->get('title')));
}
else if (!copy($resizedpath, $profileiconsdir . $maxdimension . 'px-' . PluginExportHtml::sanitise_path($artefact->get('title')))) {
$SESSION->add_error_msg(get_string('unabletocopyresizedprofileicon', 'export', $maxdimension . 'px-' . $artefact->get('title')));
}
}
}
......@@ -149,6 +156,7 @@ class HtmlExportFile extends HtmlExportArtefactPlugin {
* @param int $parentid The folder to start from - can be null
*/
private function populate_filedir($filesystemdirectory, $level, $parentid) {
global $SESSION;
foreach ($this->artefactdata as $artefactid => $artefact) {
if ($artefact->get('parent') == $parentid && $artefact->get('owner') == $this->owner) {
if ($artefact->get('artefacttype') == 'folder') {
......@@ -160,7 +168,7 @@ class HtmlExportFile extends HtmlExportArtefactPlugin {
else {
$artefact = artefact_instance_from_id($artefactid);
if (!$artefact->get_path() || !copy($artefact->get_path(), $filesystemdirectory . PluginExportHtml::sanitise_path($artefact->get('title')))) {
throw new SystemException(get_string('nonexistentfile', 'export', $artefact->get('title')));
$SESSION->add_error_msg(get_string('nonexistentfile', 'export', $artefact->get('title')));
}
}
}
......
......@@ -111,7 +111,14 @@ try {
// the download. Here it would be nice to trigger the download for everyone,
// but alas this is not possible for people without javascript.
$SESSION->set('exportfile', $exporter->get('exportdir') . $zipfile);
$wwwroot = get_config('wwwroot');
$strexportgeneratedsuccessfullyjs = get_string('exportgeneratedsuccessfullyjs', 'export', '<a href="' . $wwwroot . 'export/" target="_top">', '</a>');
$strexportgeneratedsuccessfully = get_string('exportgeneratedsuccessfully', 'export', '<a href="download.php" target="_top">', '</a>');
print_export_footer($strexportgeneratedsuccessfully, $strexportgeneratedsuccessfullyjs, $exporter->get('messages'), 'download.php');
$continueurl = 'download.php';
$continueurljs = get_config('wwwroot') . 'export/';
$result = $SESSION->get('messages');
if (empty($result)) {
$strexport = get_string('exportgeneratedsuccessfully1', 'export');
}
else {
$SESSION->clear('messages');
$strexport = get_string('exportgeneratedwitherrors', 'export');
}
print_export_footer($strexport, $continueurl, $continueurljs, $result, 'download.php');
......@@ -121,7 +121,7 @@ class PluginExportHtml extends PluginExport {
* Main export routine
*/
public function export() {
global $THEME;
global $THEME, $SESSION;
raise_memory_limit('128M');
$summaries = array();
......@@ -221,13 +221,12 @@ class PluginExportHtml extends PluginExport {
foreach ($copydata as $from => $to) {
$to = $this->get('exportdir') . '/' . $this->get('rootdir') . $to;
if (!check_dir_exists(dirname($to))) {
throw new SystemException("Could not create directory $todir");
$SESSION->add_error_msg(get_string('couldnotcreatedirectory', 'export', $todir));
}
if (!copy($from, $to)) {
throw new SystemException("Could not copy static file $from");
$SESSION->add_error_msg(get_string('couldnotcopystaticfile', 'export', $from));
}
}
// zip everything up
$this->notify_progress_callback(90, get_string('creatingzipfile', 'export'));
......@@ -469,7 +468,7 @@ class PluginExportHtml extends PluginExport {
* Copies the static files (stylesheets etc.) into the export
*/
private function copy_static_files() {
global $THEME;
global $THEME, $SESSION;
require_once('file.php');
$staticdir = $this->get('exportdir') . '/' . $this->get('rootdir') . '/static/';
$directoriestocopy = array();
......@@ -494,20 +493,20 @@ class PluginExportHtml extends PluginExport {
foreach ($this->pluginstaticdirs as $dir) {
$destinationdir = str_replace('export/html/', '', $dir);
if (!check_dir_exists($staticdir . $destinationdir)) {
throw new SystemException("Could not create static directory $destinationdir");
$SESSION->add_error_msg(get_string('couldnotcreatestaticdirectory', 'export', $destinationdir));
}
$directoriestocopy[get_config('docroot') . 'artefact/' . $dir] = $staticdir . $destinationdir;
}
foreach ($directoriestocopy as $from => $to) {
if (!copyr($from, $to)) {
throw new SystemException("Could not copy $from to $to");
$SESSION->add_error_msg(get_string('couldnotcopyfilesfromto', 'export', $from, $to));
}
}
foreach ($filestocopy as $from => $to) {
if (!copy($from, $to)) {
throw new SystemException("Could not copy static file $from");
if (!is_file($from) || !copy($from, $to)) {
$SESSION->add_error_msg(get_string('couldnotcopystaticfile', 'export', $from));
}
}
}
......
......@@ -123,6 +123,7 @@ class PluginExportLeap extends PluginExport {
* main export routine
*/
public function export() {
global $SESSION;
// the xml stuff
$this->export_header();
$this->setup_links();
......@@ -155,14 +156,16 @@ class PluginExportLeap extends PluginExport {
// write out xml to a file
if (!file_put_contents($this->exportdir . $this->leapfile, $this->xml)) {
throw new SystemException("Couldn't write LEAP data to the file");
$SESSION->add_error_msg(get_string('couldnotwriteLEAPdata', 'export'));
}
// copy attachments over
foreach ($this->attachments as $id => $fileinfo) {
$existingfile = $fileinfo->file;
$desiredname = $fileinfo->name;
copy($existingfile, $this->exportdir . $this->filedir . $id . '-' . $desiredname);
if (!is_file($existingfile) || !copy($existingfile, $this->exportdir . $this->filedir . $id . '-' . $desiredname)) {
$SESSION->add_error_msg(get_string('couldnotcopyattachment', 'export', $desiredname));
}
}
$this->notify_progress_callback(95, get_string('creatingzipfile', 'export'));
......@@ -571,8 +574,9 @@ class PluginExportLeap extends PluginExport {
* @return filename string use this to pass to add_enclosure_link
*/
public function add_attachment($filepath, $newname) {
global $SESSION;
if (!file_exists($filepath) || empty($newname)) {
throw new FileNotFoundException(get_string('nonexistentfile', 'export', $newname));
$SESSION->add_error_msg(get_string('nonexistentfile', 'export', $newname));
}
$newname = substr(str_replace('/', '_', $newname), 0, 245);
$this->attachments[] = (object)array('file' => $filepath, 'name' => $newname);
......
......@@ -150,19 +150,20 @@ function makeMessage(message, type) {
}
/* Appends a status message to the end of elemid */
function displayMessage(message, type) {
function displayMessage(message, type, hideprevmsg) {
// ensure we have type 'ok', 'error', or 'info' (the default)
if (!type || (type != 'ok' && type != 'error')) {
type = 'info';
}
var oldmessage = getFirstElementByTagAndClassName('div', null, 'messages');
var message = makeMessage(message, type);
appendChildNodes('messages', message);
if (oldmessage) {
fade(oldmessage, {afterFinish: partial(removeElement, oldmessage)});
if (typeof hideprevmsg === 'undefined' || hideprevmsg == true) {
var oldmessage = getFirstElementByTagAndClassName('div', null, 'messages');
if (oldmessage) {
fade(oldmessage, {afterFinish: partial(removeElement, oldmessage)});
}
}
}
......@@ -767,4 +768,4 @@ function is_FF() {
return true;
}
return false;
}
\ No newline at end of file
}
......@@ -33,9 +33,12 @@ $string['clicktopreview'] = 'Click to preview';
$string['collectionstoexport'] = 'Collections to export';
$string['creatingzipfile'] = 'Creating zip file';
$string['Done'] = 'Done';
$string['Export'] = 'Export';
$string['Export'] = 'Export';
$string['clickheretodownload'] = 'Click here to download it';
$string['continue'] = 'Continue';
$string['exportgeneratedsuccessfully'] = 'Export generated successfully. %sClick here to download it%s';
$string['exportgeneratedsuccessfullyjs'] = 'Export generated successfully. %sContinue%s';
$string['exportgeneratedsuccessfully1'] = 'Export generated successfully.';
$string['exportgeneratedwitherrors'] = 'Export generated with some errors.';
$string['exportingartefactplugindata'] = 'Exporting artefact plugin data';
$string['exportingartefacts'] = 'Exporting artefacts';
$string['exportingartefactsprogress'] = 'Exporting artefacts: %s/%s';
......@@ -50,7 +53,18 @@ $string['justsomecollections'] = 'Just some of my collections';
$string['justsomeviews'] = 'Just some of my pages';
$string['includefeedback'] = 'Include user feedback';
$string['includefeedbackdescription'] = 'All user comments will be included in the HTML export.';
$string['nonexistentfile'] = "Tried to add non-existent file: '%s'";
$string['nonexistentfile'] = "Tried to add non-existent file '%s'";
$string['nonexistentprofileicon'] = "Tried to add non-existent profile icon '%s'";
$string['nonexistentresizedprofileicon'] = "Tried to add non-existent resized profile icon '%s'";
$string['unabletocopyartefact'] = "Unable to copy artefact file '%s'";
$string['unabletocopyprofileicon'] = "Unable to copy profile icon '%s'";
$string['unabletocopyresizedprofileicon'] = "Unable to copy resized profile icon '%s'";
$string['couldnotcreatedirectory'] = "Could not create directory '%s'";
$string['couldnotcreatestaticdirectory'] = "Could not create static directory '%s'";
$string['couldnotcopystaticfile'] = "Could not copy static file '%s'";
$string['couldnotcopyattachment'] = "Could not copy attachment '%s'";
$string['couldnotcopyfilesfromto'] = "Could not copy files from the directory '%s' to '%s'";
$string['couldnotwriteLEAPdata'] = "Could not write LEAP data to the file";
$string['pleasewaitwhileyourexportisbeinggenerated'] = 'Please wait while your export is being generated...';
$string['reverseselection'] = 'Reverse selection';
$string['selectall'] = 'Select all';
......
......@@ -67,22 +67,24 @@ function print_iframe_progress_handler($percent, $status) {
echo '<p class="progress-text">' . hsc($status) . "</p>\n";
}
function print_export_footer($strexportgeneratedsuccessfully, $strexportgeneratedsuccessfullyjs, $jsmessages=array(), $newlocation) {
function print_export_footer($strexportgenerated, $continueurl, $continueurljs, $jsmessages=array(), $newlocation) {
?>
<script type="text/javascript">
document.write('<div class="progress-bar" style="width: 100%;"><p><?php echo $strexportgeneratedsuccessfullyjs; ?></p></div>');
document.write('<div class="progress-bar" style="width: 100%;"><p><?php echo $strexportgenerated . ' <a href="' . $continueurljs . '" target="_top">' . get_string('continue', 'export') . '</a>'; ?></p></div>');
if (!window.opera) {
// Opera can't handle this for some reason - it vomits out the
// download inline in the iframe
document.location = '<?php echo $newlocation; ?>';
}
var messages = <?php echo json_encode(join('; ', $jsmessages)); ?>;
var messages = <?php echo json_encode($jsmessages); ?>;
if (messages) {
parent.displayMessage(messages);
for (var i = 0; i < messages.length; i++) {
parent.displayMessage(messages[i].msg, messages[i].type, false);
}
}
</script>
<div class="progress-bar" style="width: 100%;">
<p><?php echo $strexportgeneratedsuccessfully; ?></p>
<p><?php echo $strexportgenerated . ' <a href="' . $continueurl . '" target="_top">' . get_string('clickheretodownload', 'export') . '</a>'; ?></p>
</div>
</body>
</html>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment