-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestiontype.php
193 lines (172 loc) · 6.82 KB
/
questiontype.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
// This file is part of the QuestionPy Moodle plugin - https://questionpy.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/>.
/**
* Question type class for the QuestionPy question type.
*
* @package qtype_questionpy
* @copyright 2022 Martin Gauk, TU Berlin, innoCampus - www.questionpy.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\di;
use qtype_questionpy\local\api\api;
use qtype_questionpy\package_file_service;
use qtype_questionpy\question_service;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/question/engine/lib.php');
require_once($CFG->dirroot . '/question/type/questionpy/question.php');
/**
* The QuestionPy question type class.
*
* @copyright 2022 Martin Gauk, TU Berlin, innoCampus - www.questionpy.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_questionpy extends question_type {
/** @var api */
private api $api;
/** @var package_file_service */
private package_file_service $packagefileservice;
/** @var question_service */
private question_service $questionservice;
/**
* Initializes the instance. Called by Moodle.
*/
public function __construct() {
parent::__construct();
$this->api = di::get(api::class);
$this->packagefileservice = di::get(package_file_service::class);
$this->questionservice = di::get(question_service::class);
}
/**
* Description
*
* @return bool true if this question type sometimes requires manual grading.
*/
public function is_manual_graded() {
return true;
}
/**
* Whether a quesiton instance has to be graded manually.
*
* @param object $question a question of this type.
* @param string $otherquestionsinuse comma-separate list of other question ids in this attempt.
* @return bool true if a particular instance of this question requires manual grading.
*/
public function is_question_manual_graded($question, $otherquestionsinuse) {
// TODO: could also return false, if $question can be automatically graded.
return $this->is_manual_graded();
}
/**
* Deletes the question-type specific data when a question is deleted.
*
* @param int $questionid the question being deleted.
* @param int $contextid the context this question belongs to.
* @throws moodle_exception
*/
public function delete_question($questionid, $contextid) {
question_service::delete_question($questionid);
parent::delete_question($questionid, $contextid);
}
/**
* Move all the files belonging to this question from one context to another.
*
* @param int $questionid
* @param int $oldcontextid
* @param int $newcontextid
* @return void
*/
public function move_files($questionid, $oldcontextid, $newcontextid) {
parent::move_files($questionid, $oldcontextid, $newcontextid);
$this->move_files_in_answers($questionid, $oldcontextid, $newcontextid);
$this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
$fs = get_file_storage();
$fs->move_area_files_to_new_context($oldcontextid, $newcontextid, 'qtype_questionpy', 'package', $questionid);
}
/**
* Delete all the files belonging to this question.
*
* @param int $questionid
* @param int $contextid
* @return void
*/
protected function delete_files($questionid, $contextid) {
parent::delete_files($questionid, $contextid);
$this->delete_files_in_answers($questionid, $contextid);
$this->delete_files_in_hints($questionid, $contextid);
$fs = get_file_storage();
$fs->delete_area_files($contextid, 'qtype_questionpy', 'package', $questionid);
}
/**
* Calculate the score a monkey would get on a question by clicking randomly.
*
* @param stdClass $questiondata data defining a question, as returned by
* question_bank::load_question_data().
* @return number|null either a fraction estimating what the student would
* score by guessing, or null, if it is not possible to estimate.
*/
public function get_random_guess_score($questiondata) {
// TODO: computing this has to be delegated to the question developer. This has to be requested at the application server.
return 0;
}
/**
* Saves QuestionPy-specific options.
*
* @param object $question this holds the information from the editing form, it is not a standard question object
* @return void
* @throws moodle_exception
*/
public function save_question_options($question) {
$this->questionservice->upsert_question($question);
}
/**
* Loads QuestionPy-specific options for the question.
*
* @param object $question question object to which QuestionPy-specific options should be added
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
public function get_question_options($question): bool {
if (!parent::get_question_options($question)) {
return false;
}
foreach ($this->questionservice->get_question($question->id) as $key => $value) {
$question->{$key} = $value;
}
return true;
}
/**
* Create an appropriate question_definition for the question of this type
* using data loaded from the database.
*
* @param object $questiondata the question data loaded from the database.
* @return question_definition an instance of the appropriate question_definition subclass.
* Still needs to be initialised.
* @throws moodle_exception
*/
protected function make_question_instance($questiondata) {
$packagefile = null;
if ($questiondata->qpy_is_local) {
$packagefile = $this->packagefileservice->get_file_for_local_question($questiondata->qpy_id, $questiondata->contextid);
}
return new qtype_questionpy_question(
$questiondata->qpy_package_hash,
$questiondata->qpy_state,
$packagefile,
$this->api
);
}
}