forked from moodleou/moodle-qtype_pmatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadresponses.php
131 lines (104 loc) · 5.31 KB
/
uploadresponses.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This is a quick and dirty script to test a question against a list of
* responses in a .csv file.
*
* The CSV file should contain two columns, the first contains 0 or 1 (or
* any number between) for whether that response should be considered correct.
* The second column contains the response. The first row in the file is ignored
* (on the assumption that it contains the column headers "mark","response".)
*
* @package qtype_pmatch
* @copyright 2015 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->libdir . '/formslib.php');
/**
* The upload form.
*
* @copyright 2015 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_pmatch_test_form extends moodleform {
protected function definition() {
$this->_form->addElement('header', 'header', get_string('testquestionformheader', 'qtype_pmatch'));
$this->_form->addElement('static', 'help', '', get_string('testquestionforminfo', 'qtype_pmatch'));
$this->_form->addElement('filepicker', 'responsesfile', get_string('testquestionformuploadlabel', 'qtype_pmatch'));
$this->_form->addRule('responsesfile', null, 'required', null, 'client');
$this->_form->addElement('hidden', 'id', 0);
$this->_form->setType('id', PARAM_INT);
$this->_form->addElement('submit', 'submitbutton', get_string('testquestionuploadresponses', 'qtype_pmatch'));
}
}
$questionid = required_param('id', PARAM_INT);
$questiondata = $DB->get_record('question', array('id' => $questionid), '*', MUST_EXIST);
if ($questiondata->qtype != 'pmatch') {
throw new coding_exception('That is not a pattern-match question.');
}
require_login();
question_require_capability_on($questiondata, 'view');
$canedit = question_has_capability_on($questiondata, 'edit');
$question = question_bank::load_question($questionid);
$context = context::instance_by_id($question->contextid);
$PAGE->set_url('/question/type/pmatch/uploadresponses.php', array('id' => $questionid));
$PAGE->set_context($context);
$PAGE->set_title(get_string('testquestionformtitle', 'qtype_pmatch'));
$PAGE->set_heading(get_string('testquestionformtitle', 'qtype_pmatch'));
if ($context->contextlevel == CONTEXT_MODULE) {
// Calling $PAGE->set_context should be enough, but it seems that it is not.
// Therefore, we get the right $cm and $course, and set things up ourselves.
$cm = get_coursemodule_from_id(false, $context->instanceid, 0, false, MUST_EXIST);
$PAGE->set_cm($cm, $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST));
}
$form = new qtype_pmatch_test_form($PAGE->url);
$form->set_data(array('id' => $questionid));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('testquestionformtitle', 'qtype_pmatch') . ': ' .
get_string('testquestionheader', 'qtype_pmatch', format_string($questiondata->name)));
// Display link back to test question.
echo html_writer::tag('p', html_writer::link(new moodle_url('/question/type/pmatch/testquestion.php',
array('id' => $questionid)), 'Back to Test question'));
if ($fromform = $form->get_data()) {
$filename = $form->get_new_filename('responsesfile');
make_temp_directory('questionimport');
$responsefile = "{$CFG->tempdir}/questionimport/{$filename}";
if (!$result = $form->save_file('responsesfile', $responsefile, true)) {
throw new moodle_exception('uploadproblem');
}
list($responses, $problems) = qtype_pmatch\test_responses::load_responses_from_file($responsefile, $question);
// Save responses to the database.
$feedback = \qtype_pmatch\test_responses::add_responses($responses);
$feedback->problems = $problems;
// Display feedback.
echo html_writer::div(get_string('savedxresponses', 'qtype_pmatch', ($feedback->saved)));
if (count($feedback->duplicates)) {
echo html_writer::div(get_string('xresponsesduplicated', 'qtype_pmatch', (count($feedback->duplicates))));
echo html_writer::alist($feedback->duplicates);
}
if (count($feedback->problems)) {
echo html_writer::div(get_string('xresponsesproblems', 'qtype_pmatch', (count($feedback->problems))));
echo html_writer::alist($feedback->problems);
}
echo $OUTPUT->heading(get_string('testquestionuploadanother', 'qtype_pmatch'));
}
$form->display();
// Display link back to test question.
echo html_writer::tag('p', html_writer::link(new moodle_url('/question/type/pmatch/testquestion.php',
array('id' => $questionid)), 'Back to Test question'));
echo $OUTPUT->footer();