Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/format.php');
29
require_once($CFG->dirroot . '/question/renderer.php');
30
 
31
use qbank_importquestions\form\question_import_form;
32
 
33
require_login();
34
core_question\local\bank\helper::require_plugin_enabled('qbank_importquestions');
35
list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
36
        question_edit_setup('import', '/question/bank/importquestions/import.php');
37
 
38
// Get display strings.
39
$txt = new stdClass();
40
$txt->importerror = get_string('importerror', 'question');
41
$txt->importquestions = get_string('importquestions', 'question');
42
 
43
list($catid, $catcontext) = explode(',', $pagevars['cat']);
44
if (!$category = $DB->get_record("question_categories", ['id' => $catid])) {
45
    throw new moodle_exception('nocategory', 'question');
46
}
47
 
48
$categorycontext = context::instance_by_id($category->contextid);
49
$category->context = $categorycontext;
1441 ariadna 50
// This page can be called without cmid in which case.
1 efrain 51
// We get the context from the category object.
52
if ($contexts === null) { // Need to get the course from the chosen category.
53
    $contexts = new core_question\local\bank\question_edit_contexts($categorycontext);
54
    $thiscontext = $contexts->lowest();
1441 ariadna 55
    [$module, $cm] = get_module_from_cmid($thiscontext->instanceid);
56
    require_login($cm->course, false, $cm);
1 efrain 57
    $contexts->require_one_edit_tab_cap($edittab);
58
}
59
 
60
$PAGE->set_url($thispageurl);
61
 
62
$importform = new question_import_form($thispageurl, ['contexts' => $contexts->having_one_edit_tab_cap('import'),
63
    'defaultcategory' => $pagevars['cat']]);
64
 
65
if ($importform->is_cancelled()) {
66
    redirect($thispageurl);
67
}
68
// Page header.
69
$PAGE->set_title($txt->importquestions);
70
$PAGE->set_heading($COURSE->fullname);
71
$PAGE->activityheader->disable();
72
 
73
echo $OUTPUT->header();
74
 
75
// Print horizontal nav if needed.
76
$renderer = $PAGE->get_renderer('core_question', 'bank');
77
 
78
$qbankaction = new \core_question\output\qbank_action_menu($thispageurl);
79
echo $renderer->render($qbankaction);
80
 
81
// File upload form submitted.
82
if ($form = $importform->get_data()) {
83
 
84
    // File checks out ok.
85
    $fileisgood = false;
86
 
87
    // Work out if this is an uploaded file.
88
    // Or one from the filesarea.
89
    $realfilename = $importform->get_new_filename('newfile');
90
    $importfile = make_request_directory() . "/{$realfilename}";
91
    if (!$result = $importform->save_file('newfile', $importfile, true)) {
92
        throw new moodle_exception('uploadproblem');
93
    }
94
 
95
    $formatfile = $CFG->dirroot . '/question/format/' . $form->format . '/format.php';
96
    if (!is_readable($formatfile)) {
97
        throw new moodle_exception('formatnotfound', 'question', '', $form->format);
98
    }
99
 
100
    require_once($formatfile);
101
 
102
    $classname = 'qformat_' . $form->format;
103
    $qformat = new $classname();
104
 
105
    // Load data into class.
106
    $qformat->setCategory($category);
107
    $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
108
    $qformat->setCourse($COURSE);
109
    $qformat->setFilename($importfile);
110
    $qformat->setRealfilename($realfilename);
111
    $qformat->setMatchgrades($form->matchgrades);
112
    $qformat->setCatfromfile(!empty($form->catfromfile));
113
    $qformat->setContextfromfile(!empty($form->contextfromfile));
114
    $qformat->setStoponerror($form->stoponerror);
115
 
116
    // Do anything before that we need to.
117
    if (!$qformat->importpreprocess()) {
118
        throw new moodle_exception('cannotimport', '', $thispageurl->out());
119
    }
120
 
121
    // Process the uploaded file.
122
    if (!$qformat->importprocess()) {
123
        throw new moodle_exception('cannotimport', '', $thispageurl->out());
124
    }
125
 
126
    // In case anything needs to be done after.
127
    if (!$qformat->importpostprocess()) {
128
        throw new moodle_exception('cannotimport', '', $thispageurl->out());
129
    }
130
 
131
    // Log the import into this category.
132
    $eventparams = [
133
            'contextid' => $qformat->category->contextid,
134
            'other' => ['format' => $form->format, 'categoryid' => $qformat->category->id],
135
    ];
136
    $event = \core\event\questions_imported::create($eventparams);
137
    $event->trigger();
138
 
139
    $params = $thispageurl->params() + ['category' => $qformat->category->id . ',' . $qformat->category->contextid];
140
    echo $OUTPUT->continue_button(new moodle_url('/question/edit.php', $params));
141
    echo $OUTPUT->footer();
142
    exit;
143
}
144
 
145
echo $OUTPUT->heading_with_help($txt->importquestions, 'importquestions', 'question');
146
 
147
// Print upload form.
148
$importform->display();
149
echo $OUTPUT->footer();