From 26f4fa7e154d69faf5ee1aa65ae17e57575a5f77 Mon Sep 17 00:00:00 2001
From: tjhunt
Date: Tue, 8 Jul 2008 11:16:21 +0000
Subject: [PATCH] MDL-11905 - Users with the capability to export questions may
not have the capability to access backupdata. Therefore we need to change
where question export files are stored. I know this is after the 1.9.2 code
freeze, but I discussed this fix with Petr, and he reviewed it last night and
we decided it was better to commit it before the 1.9.2 release.
---
lang/en_utf8/question.php | 1 +
question/export.php | 73 +++++++++++++++++++++------------------
question/exportfile.php | 17 +++++++++
question/format.php | 18 ++++++++--
4 files changed, 74 insertions(+), 35 deletions(-)
create mode 100644 question/exportfile.php
diff --git a/lang/en_utf8/question.php b/lang/en_utf8/question.php
index f3681b4a67e..994f34693c2 100644
--- a/lang/en_utf8/question.php
+++ b/lang/en_utf8/question.php
@@ -91,4 +91,5 @@ $string['tofilecontext'] = 'Write context to file';
$string['unknown'] = 'Unknown';
$string['unknownquestiontype'] = 'Unknown question type: $a.';
$string['unpublished'] = 'unshared';
+$string['yourfileshoulddownload'] = 'Your export file should start to download shortly. If not, please click here.';
?>
diff --git a/question/export.php b/question/export.php
index 77babbaecb2..1362b11ddbf 100644
--- a/question/export.php
+++ b/question/export.php
@@ -17,20 +17,7 @@
// get display strings
- $txt = new object;
- $txt->category = get_string('category', 'quiz');
- $txt->download = get_string('download', 'quiz');
- $txt->downloadextra = get_string('downloadextra', 'quiz');
- $txt->exporterror = get_string('exporterror', 'quiz');
- $txt->exportname = get_string('exportname', 'quiz');
- $txt->exportquestions = get_string('exportquestions', 'quiz');
- $txt->fileformat = get_string('fileformat', 'quiz');
- $txt->exportcategory = get_string('exportcategory', 'quiz');
- $txt->modulename = get_string('modulename', 'quiz');
- $txt->modulenameplural = get_string('modulenameplural', 'quiz');
- $txt->tofile = get_string('tofile', 'quiz');
-
-
+ $strexportquestions = get_string('exportquestions', 'quiz');
// make sure we are using the user's most recent category choice
if (empty($categoryid)) {
@@ -52,9 +39,9 @@
$navlinks = array();
$navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$COURSE->id", 'type' => 'activity');
$navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
- $navlinks[] = array('name' => $txt->exportquestions, 'link' => '', 'type' => 'title');
+ $navlinks[] = array('name' => $strexportquestions, 'link' => '', 'type' => 'title');
$navigation = build_navigation($navlinks);
- print_header_simple($txt->exportquestions, '', $navigation, "", "", true, $strupdatemodule);
+ print_header_simple($strexportquestions, '', $navigation, "", "", true, $strupdatemodule);
$currenttab = 'edit';
$mode = 'export';
@@ -63,10 +50,10 @@
} else {
// Print basic page layout.
$navlinks = array();
- $navlinks[] = array('name' => $txt->exportquestions, 'link' => '', 'type' => 'title');
+ $navlinks[] = array('name' => $strexportquestions, 'link' => '', 'type' => 'title');
$navigation = build_navigation($navlinks);
- print_header_simple($txt->exportquestions, '', $navigation);
+ print_header_simple($strexportquestions, '', $navigation);
// print tabs
$currenttab = 'export';
include('tabs.php');
@@ -100,42 +87,62 @@
$from_form->exportfilename = default_export_filename($COURSE, $category);
}
$qformat->setFilename($from_form->exportfilename);
+ $canaccessbackupdata = has_capability('moodle/site:backup', $contexts->lowest());
+ $qformat->set_can_access_backupdata($canaccessbackupdata);
$qformat->setCattofile(!empty($from_form->cattofile));
$qformat->setContexttofile(!empty($from_form->contexttofile));
if (! $qformat->exportpreprocess()) { // Do anything before that we need to
- error($txt->exporterror, $thispageurl->out());
+ error(get_string('exporterror', 'quiz'), $thispageurl->out());
}
if (! $qformat->exportprocess()) { // Process the export data
- error($txt->exporterror, $thispageurl->out());
+ error(get_string('exporterror', 'quiz'), $thispageurl->out());
}
if (! $qformat->exportpostprocess()) { // In case anything needs to be done after
- error($txt->exporterror, $thispageurl->out());
+ error(get_string('exporterror', 'quiz'), $thispageurl->out());
}
echo "
";
// link to download the finished file
$file_ext = $qformat->export_file_extension();
- if ($CFG->slasharguments) {
- $efile = "{$CFG->wwwroot}/file.php/".$qformat->question_get_export_dir()."/$from_form->exportfilename".$file_ext."?forcedownload=1";
- }
- else {
- $efile = "{$CFG->wwwroot}/file.php?file=/".$qformat->question_get_export_dir()."/$from_form->exportfilename".$file_ext."&forcedownload=1";
- }
- echo "
";
- echo "$txt->downloadextra
";
+ $filename = $from_form->exportfilename . $file_ext;
+ if ($canaccessbackupdata) {
+ $efile = get_file_url($qformat->question_get_export_dir() . '/' . $filename,
+ array('forcedownload' => 1));
+ echo '';
+ echo '' .
+ get_string('downloadextra', 'quiz') . '
';
+ } else {
+ if ($CFG->slasharguments) {
+ $efile = $CFG->wwwroot . '/question/exportfile.php/' . rawurlencode($filename);
+ } else {
+ $efile = $CFG->wwwroot . '/question/exportfile.php/?file=' . rawurlencode($filename);
+ }
+
+ echo '' .
+ get_string('yourfileshoulddownload', 'question', $efile) . '
';
+ echo '
+';
+ }
+
+ print_continue('edit.php?' . $thispageurl->get_query_string());
print_footer($COURSE);
exit;
}
/// Display export form
-
-
- print_heading_with_help($txt->exportquestions, 'export', 'quiz');
+ print_heading_with_help($strexportquestions, 'export', 'quiz');
$export_form->display();
diff --git a/question/exportfile.php b/question/exportfile.php
new file mode 100644
index 00000000000..fa36f79e8f8
--- /dev/null
+++ b/question/exportfile.php
@@ -0,0 +1,17 @@
+libdir . '/filelib.php');
+
+ // Note: file.php always calls require_login() with $setwantsurltome=false
+ // in order to avoid messing redirects. MDL-14495
+ require_login(0, true, null, false);
+
+ $relativepath = get_file_argument('question/exportfile.php');
+ if (!$relativepath) {
+ error('No valid arguments supplied or incorrect server configuration');
+ }
+
+ $pathname = $CFG->dataroot . '/temp/questionexport/' . $USER->id . '/' . $relativepath;
+
+ send_temp_file($pathname, $relativepath);
+?>
\ No newline at end of file
diff --git a/question/format.php b/question/format.php
index 1223204f5e8..dddaa3a522a 100644
--- a/question/format.php
+++ b/question/format.php
@@ -24,6 +24,7 @@ class qformat_default {
var $importerrors = 0;
var $stoponerror = true;
var $translator = null;
+ var $canaccessbackupdata = true;
// functions to indicate import/export functionality
@@ -134,6 +135,14 @@ class qformat_default {
$this->stoponerror = $stoponerror;
}
+ /**
+ * @param boolean $canaccess Whether the current use can access the backup data folder. Determines
+ * where export files are saved.
+ */
+ function set_can_access_backupdata($canaccess) {
+ $this->canaccessbackupdata = $canaccess;
+ }
+
/***********************
* IMPORTING FUNCTIONS
***********************/
@@ -767,8 +776,13 @@ class qformat_default {
* @return string file path
*/
function question_get_export_dir() {
- $dirname = get_string("exportfilename","quiz");
- $path = $this->course->id.'/backupdata/'.$dirname; // backupdata is protected directory
+ global $USER;
+ if ($this->canaccessbackupdata) {
+ $dirname = get_string("exportfilename","quiz");
+ $path = $this->course->id.'/backupdata/'.$dirname; // backupdata is protected directory
+ } else {
+ $path = 'temp/questionexport/' . $USER->id;
+ }
return $path;
}