Skip to content

Commit

Permalink
question export MDL-25324 Better file names for question exports.
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Nov 19, 2010
1 parent 5b74449 commit b80d424
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 49 deletions.
66 changes: 24 additions & 42 deletions lib/questionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2622,47 +2622,29 @@ function get_import_export_formats( $type ) {


/**
* Create default export filename
*
* @return string default export filename
* @param object $course
* @param object $category
* Create a reasonable default file name for exporting questions from a particular
* category.
* @param object $course the course the questions are in.
* @param object $category the question category.
* @return string the filename.
*/
function default_export_filename($course,$category) {
//Take off some characters in the filename !!
$takeoff = array(" ", ":", "/", "\\", "|");
$export_word = str_replace($takeoff,"_",moodle_strtolower(get_string("exportfilename","quiz")));
//If non-translated, use "export"
if (substr($export_word,0,1) == "[") {
$export_word= "export";
}

//Calculate the date format string
$export_date_format = str_replace(" ","_",get_string("exportnameformat","quiz"));
//If non-translated, use "%Y%m%d-%H%M"
if (substr($export_date_format,0,1) == "[") {
$export_date_format = "%%Y%%m%%d-%%H%%M";
}

//Calculate the shortname
$export_shortname = clean_filename($course->shortname);
if (empty($export_shortname) or $export_shortname == '_' ) {
$export_shortname = $course->id;
}

//Calculate the category name
$export_categoryname = clean_filename($category->name);

//Calculate the final export filename
//The export word
$export_name = $export_word."-";
//The shortname
$export_name .= moodle_strtolower($export_shortname)."-";
//The category name
$export_name .= moodle_strtolower($export_categoryname)."-";
//The date format
$export_name .= userdate(time(),$export_date_format,99,false);
//Extension is supplied by format later.
function question_default_export_filename($course, $category) {
// We build a string that is an appropriate name (questions) from the lang pack,
// then the corse shortname, then the question category name, then a timestamp.

$base = clean_filename(get_string('exportfilename', 'question'));

$dateformat = str_replace(' ', '_', get_string('exportnameformat', 'question'));
$timestamp = clean_filename(userdate(time(), $dateformat, 99, false));

$shortname = clean_filename($course->shortname);
if ($shortname == '' || $shortname == '_' ) {
$shortname = $course->id;
}

$categoryname = clean_filename(format_string($category->name));

return "{$base}-{$shortname}-{$categoryname}-{$timestamp}";

return $export_name;
}
Expand Down Expand Up @@ -3276,8 +3258,8 @@ function question_check_file_access($question, $state, $options, $contextid, $co
* @param string $ithcontexts
* @param moodle_url export file url
*/
function question_make_export_url($contextid, $categoryid, $format, $withcategories, $withcontexts) {
function question_make_export_url($contextid, $categoryid, $format, $withcategories, $withcontexts, $filename) {
global $CFG;
$urlbase = "$CFG->httpswwwroot/pluginfile.php";
return moodle_url::make_file_url($urlbase, "/$contextid/question/export/{$categoryid}/{$format}/{$withcategories}/{$withcontexts}/export.xml", true);
return moodle_url::make_file_url($urlbase, "/$contextid/question/export/{$categoryid}/{$format}/{$withcategories}/{$withcontexts}/{$filename}", true);
}
7 changes: 6 additions & 1 deletion question/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@
$withcontexts = 'withcontexts';
}

$export_url = question_make_export_url($thiscontext->id, $category->id, $from_form->format, $withcategories, $withcontexts);
$classname = 'qformat_' . $from_form->format;
$qformat = new $classname();
$filename = question_default_export_filename($COURSE, $category) .
$qformat->export_file_extension();
$export_url = question_make_export_url($thiscontext->id, $category->id,
$from_form->format, $withcategories, $withcontexts, $filename);

echo $OUTPUT->box_start();
echo get_string('yourfileshoulddownload', 'question', $export_url->out());
Expand Down
6 changes: 0 additions & 6 deletions question/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,12 +698,6 @@ function exportprocess() {
// export the question displaying message
$count++;

//echo '<hr />';
//echo $OUTPUT->container_start();
//echo '<strong>' . $count . '.</strong>&nbsp;';
//echo $this->format_question_text($question);
//echo $OUTPUT->container_end();

if (question_has_capability_on($question, 'view', $question->category)) {
// files used by questiontext
$files = $fs->get_area_files($contextid, 'question', 'questiontext', $question->id);
Expand Down
4 changes: 4 additions & 0 deletions question/format/gift/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ function provide_export() {
return true;
}

function export_file_extension() {
return '.txt';
}

function answerweightparser(&$answer) {
$answer = substr($answer, 1); // removes initial %
$end_position = strpos($answer, "%");
Expand Down

0 comments on commit b80d424

Please sign in to comment.