Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/import.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00027 require_once(dirname(__FILE__) . '/../config.php');
00028 require_once($CFG->dirroot . '/question/editlib.php');
00029 require_once($CFG->dirroot . '/question/import_form.php');
00030 require_once($CFG->dirroot . '/question/format.php');
00031 
00032 list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
00033         question_edit_setup('import', '/question/import.php', false, false);
00034 
00035 // get display strings
00036 $txt = new stdClass();
00037 $txt->importerror = get_string('importerror', 'question');
00038 $txt->importquestions = get_string('importquestions', 'question');
00039 
00040 list($catid, $catcontext) = explode(',', $pagevars['cat']);
00041 if (!$category = $DB->get_record("question_categories", array('id' => $catid))) {
00042     print_error('nocategory', 'question');
00043 }
00044 
00045 $categorycontext = get_context_instance_by_id($category->contextid);
00046 $category->context = $categorycontext;
00047 //this page can be called without courseid or cmid in which case
00048 //we get the context from the category object.
00049 if ($contexts === null) { // need to get the course from the chosen category
00050     $contexts = new question_edit_contexts($categorycontext);
00051     $thiscontext = $contexts->lowest();
00052     if ($thiscontext->contextlevel == CONTEXT_COURSE){
00053         require_login($thiscontext->instanceid, false);
00054     } elseif ($thiscontext->contextlevel == CONTEXT_MODULE){
00055         list($module, $cm) = get_module_from_cmid($thiscontext->instanceid);
00056         require_login($cm->course, false, $cm);
00057     }
00058     $contexts->require_one_edit_tab_cap($edittab);
00059 }
00060 
00061 $PAGE->set_url($thispageurl);
00062 
00063 $import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
00064                                                     'defaultcategory'=>$pagevars['cat']));
00065 
00066 if ($import_form->is_cancelled()){
00067     redirect($thispageurl);
00068 }
00069 //==========
00070 // PAGE HEADER
00071 //==========
00072 $PAGE->set_title($txt->importquestions);
00073 $PAGE->set_heading($COURSE->fullname);
00074 echo $OUTPUT->header();
00075 
00076 // file upload form sumitted
00077 if ($form = $import_form->get_data()) {
00078 
00079     // file checks out ok
00080     $fileisgood = false;
00081 
00082     // work out if this is an uploaded file
00083     // or one from the filesarea.
00084     $realfilename = $import_form->get_new_filename('newfile');
00085 
00086     $importfile = "{$CFG->tempdir}/questionimport/{$realfilename}";
00087     make_temp_directory('questionimport');
00088     if (!$result = $import_form->save_file('newfile', $importfile, true)) {
00089         throw new moodle_exception('uploadproblem');
00090     }
00091 
00092     $formatfile = 'format/' . $form->format . '/format.php';
00093     if (!is_readable($formatfile)) {
00094         throw new moodle_exception('formatnotfound', 'question', '', $form->format);
00095     }
00096 
00097     require_once($formatfile);
00098 
00099     $classname = 'qformat_' . $form->format;
00100     $qformat = new $classname();
00101 
00102     // load data into class
00103     $qformat->setCategory($category);
00104     $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
00105     $qformat->setCourse($COURSE);
00106     $qformat->setFilename($importfile);
00107     $qformat->setRealfilename($realfilename);
00108     $qformat->setMatchgrades($form->matchgrades);
00109     $qformat->setCatfromfile(!empty($form->catfromfile));
00110     $qformat->setContextfromfile(!empty($form->contextfromfile));
00111     $qformat->setStoponerror($form->stoponerror);
00112 
00113     // Do anything before that we need to
00114     if (!$qformat->importpreprocess()) {
00115         print_error('cannotimport', '', $thispageurl->out());
00116     }
00117 
00118     // Process the uploaded file
00119     if (!$qformat->importprocess($category)) {
00120         print_error('cannotimport', '', $thispageurl->out());
00121     }
00122 
00123     // In case anything needs to be done after
00124     if (!$qformat->importpostprocess()) {
00125         print_error('cannotimport', '', $thispageurl->out());
00126     }
00127 
00128     $params = $thispageurl->params() + array(
00129         'category' => $qformat->category->id . ',' . $qformat->category->contextid);
00130     echo $OUTPUT->continue_button(new moodle_url('edit.php', $params));
00131     echo $OUTPUT->footer();
00132     exit;
00133 }
00134 
00135 echo $OUTPUT->heading_with_help($txt->importquestions, 'importquestions', 'question');
00136 
00138 $import_form->display();
00139 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations