Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
MDL-15573, question export rewrite
  • Loading branch information
Dongsheng Cai committed Nov 5, 2010
1 parent cc77193 commit cde2709
Show file tree
Hide file tree
Showing 16 changed files with 797 additions and 326 deletions.
81 changes: 80 additions & 1 deletion lib/questionlib.php
Expand Up @@ -3270,10 +3270,73 @@ function quiz_rewrite_question_urls($text, $file, $contextid, $component, $filea
function question_pluginfile($course, $context, $component, $filearea, $args, $forcedownload) {
global $DB, $CFG;

list($context, $course, $cm) = get_context_info_array($context->id);
require_login($course, false, $cm);

if ($filearea === 'export') {
require_once($CFG->dirroot . '/question/editlib.php');
$contexts = new question_edit_contexts($context);
// check export capability
$contexts->require_one_edit_tab_cap('export');
$category_id = (int)array_shift($args);
$format = array_shift($args);
$cattofile = array_shift($args);
$contexttofile = array_shift($args);
$filename = array_shift($args);

// load parent class for import/export
require_once($CFG->dirroot . '/question/format.php');
require_once($CFG->dirroot . '/question/editlib.php');
require_once($CFG->dirroot . '/question/format/' . $format . '/format.php');

$classname = 'qformat_' . $format;
if (!class_exists($classname)) {
send_file_not_found();
}

$qformat = new $classname();

if (!$category = $DB->get_record('question_categories', array('id' => $category_id))) {
send_file_not_found();
}

$qformat->setCategory($category);
$qformat->setContexts($contexts->having_one_edit_tab_cap('export'));
$qformat->setCourse($course);

if ($cattofile == 'withcategories') {
$qformat->setCattofile(true);
} else {
$qformat->setCattofile(false);
}

if ($contexttofile == 'withcontexts') {
$qformat->setContexttofile(true);
} else {
$qformat->setContexttofile(false);
}

if (!$qformat->exportpreprocess()) {
send_file_not_found();
print_error('exporterror', 'question', $thispageurl->out());
}

// export data to moodle file pool
if (!$content = $qformat->exportprocess(true)) {
send_file_not_found();
}

//DEBUG
//echo '<textarea cols=90 rows=20>';
//echo $content;
//echo '</textarea>';
//die;
send_file($content, $filename, 0, 0, true, true);
}

$attemptid = (int)array_shift($args);
$questionid = (int)array_shift($args);

require_login($course, false);

if ($attemptid === 0) {
// preview
Expand Down Expand Up @@ -3321,3 +3384,19 @@ function question_check_file_access($question, $state, $options, $contextid, $co
return $QTYPES[$question->qtype]->check_file_access($question, $state, $options, $contextid, $component,
$filearea, $args, $forcedownload);
}

/**
* Create url for question export
*
* @param int $contextid, current context
* @param int $categoryid, categoryid
* @param string $format
* @param string $withcategories
* @param string $ithcontexts
* @param moodle_url export file url
*/
function question_make_export_url($contextid, $categoryid, $format, $withcategories, $withcontexts) {
global $CFG;
$urlbase = "$CFG->httpswwwroot/pluginfile.php";
return moodle_url::make_file_url($urlbase, "/$contextid/question/export/{$categoryid}/{$format}/{$withcategories}/{$withcontexts}/export.xml", true);
}
71 changes: 16 additions & 55 deletions question/export.php
Expand Up @@ -37,68 +37,30 @@
$PAGE->set_heading($COURSE->fullname);
echo $OUTPUT->header();

$exportfilename = default_export_filename($COURSE, $category);
$export_form = new question_export_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('export'), 'defaultcategory'=>$pagevars['cat'],
'defaultfilename'=>$exportfilename));
$export_form = new question_export_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('export'), 'defaultcategory'=>$pagevars['cat']));


if ($from_form = $export_form->get_data()) { /// Filename


if (! is_readable("format/$from_form->format/format.php")) {
if ($from_form = $export_form->get_data()) {
$thiscontext = $contexts->lowest();
if (!is_readable("format/$from_form->format/format.php")) {
print_error('unknowformat', '', '', $from_form->format);
}

// load parent class for import/export
require_once("format.php");

// and then the class for the selected format
require_once("format/$from_form->format/format.php");

$classname = "qformat_$from_form->format";
$qformat = new $classname();
$qformat->setContexts($contexts->having_one_edit_tab_cap('export'));
$qformat->setCategory($category);
$qformat->setCourse($COURSE);

if (empty($from_form->exportfilename)) {
$from_form->exportfilename = default_export_filename($COURSE, $category);
$withcategories = 'nocategories';
if (!empty($from_form->cattofile)) {
$withcategories = 'withcategories';
}
$qformat->setFilename($from_form->exportfilename);
$canaccessbackupdata = has_capability('moodle/backup:backupcourse', $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
print_error('exporterror', 'question', $thispageurl->out());
$withcontexts = 'nocontexts';
if (!empty($from_form->contexttofile)) {
$withcontexts = 'withcontexts';
}

if (! $qformat->exportprocess()) { // Process the export data
print_error('exporterror', 'question', $thispageurl->out());
}
$export_url = question_make_export_url($thiscontext->id, $category->id, $from_form->format, $withcategories, $withcontexts);

if (! $qformat->exportpostprocess()) { // In case anything needs to be done after
print_error('exporterror', 'question', $thispageurl->out());
}
echo "<hr />";

// link to download the finished file
$file_ext = $qformat->export_file_extension();
$filename = $from_form->exportfilename . $file_ext;
if ($canaccessbackupdata) {
$efile = get_file_url($qformat->question_get_export_dir() . '/' . $filename,
array('forcedownload' => 1));
echo '<p><div class="boxaligncenter"><a href="' . $efile . '">' .
get_string('download', 'quiz') . '</a></div></p>';
echo '<p><div class="boxaligncenter"><font size="-1">' .
get_string('downloadextra', 'quiz') . '</font></div></p>';
} else {
$efile = get_file_url($filename, null, 'questionfile');
echo '<p><div class="boxaligncenter">' .
get_string('yourfileshoulddownload', 'question', $efile) . '</div></p>';
$PAGE->requires->js_function_call('document.location.replace', array($efile), false, 1);
}
echo $OUTPUT->box_start();
echo get_string('yourfileshoulddownload', 'question', $export_url->out());
echo $OUTPUT->box_end();

$PAGE->requires->js_function_call('document.location.replace', array($export_url->out()), false, 1);

echo $OUTPUT->continue_button(new moodle_url('edit.php', $thispageurl->params()));
echo $OUTPUT->footer();
Expand All @@ -111,4 +73,3 @@
$export_form->display();

echo $OUTPUT->footer();

6 changes: 0 additions & 6 deletions question/export_form.php
Expand Up @@ -13,7 +13,6 @@ function definition() {

$defaultcategory = $this->_customdata['defaultcategory'];
$contexts = $this->_customdata['contexts'];
$defaultfilename = $this->_customdata['defaultfilename'];
//--------------------------------------------------------------------------------
$mform->addElement('header','fileformat',get_string('fileformat','quiz'));
$fileformatnames = get_import_export_formats('export');
Expand Down Expand Up @@ -46,10 +45,6 @@ function definition() {
// $mform->addElement('select', 'format', get_string('fileformat','quiz'), $fileformatnames);
// $mform->setDefault('format', 'gift');

$mform->addElement('text', 'exportfilename', get_string('exportname', 'quiz'), array('size'=>40));
$mform->setDefault('exportfilename', $defaultfilename);
$mform->setType('exportfilename', PARAM_FILE);

// set a template for the format select elements
$renderer =& $mform->defaultRenderer();
$template = "{help} {element}\n";
Expand All @@ -60,4 +55,3 @@ function definition() {
//--------------------------------------------------------------------------------
}
}

0 comments on commit cde2709

Please sign in to comment.