From cbc87100f7516d0562f0f31b423f0a6037f80c34 Mon Sep 17 00:00:00 2001 From: thepurpleblob Date: Mon, 13 Mar 2006 19:32:42 +0000 Subject: [PATCH] Added ability to import files already in the course files area. (Bug #3896) Also started tidying up the code, more to do. --- question/import.php | 160 ++++++++++++++++++++++++++++++-------------- 1 file changed, 110 insertions(+), 50 deletions(-) diff --git a/question/import.php b/question/import.php index 57a6998874..4f24f4a8c9 100644 --- a/question/import.php +++ b/question/import.php @@ -11,11 +11,27 @@ require_once("../config.php"); require_once($CFG->libdir.'/questionlib.php'); + require_once($CFG->dirroot.'/lib/uploadlib.php'); + // get parameters + $params = new stdClass; + $params->choosefile = optional_param('choosefile','',PARAM_PATH); $categoryid = optional_param('category', 0, PARAM_INT); $courseid = optional_param('course', 0, PARAM_INT); $format = optional_param('format','',PARAM_FILE); + // get display strings + $txt = new stdClass(); + $txt->category = get_string('category','quiz'); + $txt->choosefile = get_string('choosefile','quiz'); + $txt->file = get_string('file'); + $txt->fileformat = get_string('fileformat','quiz'); + $txt->importfilearea = get_string('importfilearea','quiz'); + $txt->importfileupload = get_string('importfileupload','quiz'); + $txt->upload = get_string('upload'); + $txt->uploadproblem = get_string('uploadproblem'); + $txt->uploadthisfile = get_string('uploadthisfile'); + if (!$categoryid) { // try to get category from modform $showcatmenu = true; // will ensure that user can choose category if (isset($SESSION->modform)) { @@ -55,8 +71,9 @@ $strquizzes = get_string('modulenameplural', 'quiz'); $streditingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz"); - - /// Header: + //========== + // PAGE HEADER + //========== if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) { $strupdatemodule = isteacheredit($course->id) @@ -78,20 +95,39 @@ } - if (!empty($format)) { /// Filename + // file upload form sumitted + if (!empty($format) and confirm_sesskey() ) { - if (!confirm_sesskey()) { - error( 'sesskey error' ); - } + // file checks out ok + $fileisgood = false; - if (empty($_FILES['newfile'])) { // file was just uploaded - notify(get_string("uploadproblem") ); + // work out if this is an uploaded file + // or one from the filesarea. + if (!empty($params->choosefile)) { + $importfile = "{$CFG->dataroot}/{$course->id}/{$params->choosefile}"; + if (file_exists($importfile)) { + $fileisgood = true; + } + else { + notify($txt->uploadproblem); + } + } + else { + // must be upload file + if (empty($_FILES['newfile'])) { + notify( $txt->uploadproblem ); + } + else if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) { + notify( $txt->uploadproblem ); + } + else { + $importfile = $_FILES['newfile']['tmp_name']; + $fileisgood = true; + } } - - if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) { - notify(get_string("uploadnofilefound") ); - } else { // Valid file is found + // process if we are happy file is ok + if ($fileisgood) { if (! is_readable("format/$format/format.php")) { error( get_string('formatnotfound','quiz', $format) ); @@ -108,7 +144,7 @@ "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id"); } - if (! $qformat->importprocess($_FILES['newfile']['tmp_name'])) { // Process the uploaded file + if (! $qformat->importprocess($importfile) ) { // Process the uploaded file error( get_string('importerror','quiz'), "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id"); } @@ -159,43 +195,67 @@ $catmenu[$cat->id] = $cat->indentedname; } } - - print_simple_box_start("center"); - echo "
\n"; - echo "\n"; - echo "\n"; - - echo "\n"; - - echo "\n"; - - echo "\n"; - - echo "
"; - print_string("category", "quiz"); - echo ":"; - if (!showcatmenu) { // category already specified - echo question_category_coursename($category); - echo " id\" />"; - } else { // no category specified, let user choose - choose_from_menu($catmenu, "category", $category->id, ""); - } - echo "
"; - print_string("fileformat", "quiz"); - echo ":"; - choose_from_menu($fileformatnames, "format", "gift", ""); - helpbutton("import", $strimportquestions, "quiz"); - echo "
"; - print_string("upload"); - echo ":"; - require_once($CFG->dirroot.'/lib/uploadlib.php'); - upload_print_form_fragment(1,array('newfile'),null,false,null,$course->maxbytes,0,false); - echo "
 "; - echo " "; - echo "
\n"; - echo "
\n"; - print_simple_box_end(); - + + //========== + // DISPLAY + //========== + + ?> + +
+ + + + + + + + + + + + +
category; ?>:id, ""); ?>
fileformat; ?>:
+ + importfileupload; ?> + + + + + + + + + + +
upload; ?>:maxbytes,0,false); ?>
 
+ + importfilearea; ?> + + + + + + + + + + +
file; ?>:
 id}&choose=form.choosefile", + "coursefiles", $txt->choosefile, 500, 750, $txt->choosefile); ?> +
+ +
+ + -- 2.39.5