1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Defines the import questions form. 19 * 20 * @package qbank_importquestions 21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 26 require_once(__DIR__ . '/../../../config.php'); 27 require_once($CFG->dirroot . '/question/editlib.php'); 28 require_once($CFG->dirroot . '/question/bank/importquestions/classes/form/import_form.php'); 29 require_once($CFG->dirroot . '/question/format.php'); 30 require_once($CFG->dirroot . '/question/renderer.php'); 31 32 use qbank_importquestions\form\question_import_form; 33 34 require_login(); 35 core_question\local\bank\helper::require_plugin_enabled('qbank_importquestions'); 36 list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = 37 question_edit_setup('import', '/question/bank/importquestions/import.php'); 38 39 // Get display strings. 40 $txt = new stdClass(); 41 $txt->importerror = get_string('importerror', 'question'); 42 $txt->importquestions = get_string('importquestions', 'question'); 43 44 list($catid, $catcontext) = explode(',', $pagevars['cat']); 45 if (!$category = $DB->get_record("question_categories", ['id' => $catid])) { 46 throw new moodle_exception('nocategory', 'question'); 47 } 48 49 $categorycontext = context::instance_by_id($category->contextid); 50 $category->context = $categorycontext; 51 // This page can be called without courseid or cmid in which case. 52 // We get the context from the category object. 53 if ($contexts === null) { // Need to get the course from the chosen category. 54 $contexts = new core_question\local\bank\question_edit_contexts($categorycontext); 55 $thiscontext = $contexts->lowest(); 56 if ($thiscontext->contextlevel == CONTEXT_COURSE) { 57 require_login($thiscontext->instanceid, false); 58 } else if ($thiscontext->contextlevel == CONTEXT_MODULE) { 59 list($module, $cm) = get_module_from_cmid($thiscontext->instanceid); 60 require_login($cm->course, false, $cm); 61 } 62 $contexts->require_one_edit_tab_cap($edittab); 63 } 64 65 $PAGE->set_url($thispageurl); 66 67 $importform = new question_import_form($thispageurl, ['contexts' => $contexts->having_one_edit_tab_cap('import'), 68 'defaultcategory' => $pagevars['cat']]); 69 70 if ($importform->is_cancelled()) { 71 redirect($thispageurl); 72 } 73 // Page header. 74 $PAGE->set_title($txt->importquestions); 75 $PAGE->set_heading($COURSE->fullname); 76 $PAGE->activityheader->disable(); 77 78 echo $OUTPUT->header(); 79 80 // Print horizontal nav if needed. 81 $renderer = $PAGE->get_renderer('core_question', 'bank'); 82 83 $qbankaction = new \core_question\output\qbank_action_menu($thispageurl); 84 echo $renderer->render($qbankaction); 85 86 // File upload form submitted. 87 if ($form = $importform->get_data()) { 88 89 // File checks out ok. 90 $fileisgood = false; 91 92 // Work out if this is an uploaded file. 93 // Or one from the filesarea. 94 $realfilename = $importform->get_new_filename('newfile'); 95 $importfile = make_request_directory() . "/{$realfilename}"; 96 if (!$result = $importform->save_file('newfile', $importfile, true)) { 97 throw new moodle_exception('uploadproblem'); 98 } 99 100 $formatfile = $CFG->dirroot . '/question/format/' . $form->format . '/format.php'; 101 if (!is_readable($formatfile)) { 102 throw new moodle_exception('formatnotfound', 'question', '', $form->format); 103 } 104 105 require_once($formatfile); 106 107 $classname = 'qformat_' . $form->format; 108 $qformat = new $classname(); 109 110 // Load data into class. 111 $qformat->setCategory($category); 112 $qformat->setContexts($contexts->having_one_edit_tab_cap('import')); 113 $qformat->setCourse($COURSE); 114 $qformat->setFilename($importfile); 115 $qformat->setRealfilename($realfilename); 116 $qformat->setMatchgrades($form->matchgrades); 117 $qformat->setCatfromfile(!empty($form->catfromfile)); 118 $qformat->setContextfromfile(!empty($form->contextfromfile)); 119 $qformat->setStoponerror($form->stoponerror); 120 121 // Do anything before that we need to. 122 if (!$qformat->importpreprocess()) { 123 throw new moodle_exception('cannotimport', '', $thispageurl->out()); 124 } 125 126 // Process the uploaded file. 127 if (!$qformat->importprocess()) { 128 throw new moodle_exception('cannotimport', '', $thispageurl->out()); 129 } 130 131 // In case anything needs to be done after. 132 if (!$qformat->importpostprocess()) { 133 throw new moodle_exception('cannotimport', '', $thispageurl->out()); 134 } 135 136 // Log the import into this category. 137 $eventparams = [ 138 'contextid' => $qformat->category->contextid, 139 'other' => ['format' => $form->format, 'categoryid' => $qformat->category->id], 140 ]; 141 $event = \core\event\questions_imported::create($eventparams); 142 $event->trigger(); 143 144 $params = $thispageurl->params() + ['category' => $qformat->category->id . ',' . $qformat->category->contextid]; 145 echo $OUTPUT->continue_button(new moodle_url('/question/edit.php', $params)); 146 echo $OUTPUT->footer(); 147 exit; 148 } 149 150 echo $OUTPUT->heading_with_help($txt->importquestions, 'importquestions', 'question'); 151 152 // Print upload form. 153 $importform->display(); 154 echo $OUTPUT->footer();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body