FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
moodle-qtype_combined/questiontype.php at main · Epitech/moodle-qtype_combined · GitHub
This repository was archived by the owner on Nov 18, 2025. It is now read-only.
Epitech
/
moodle-qtype_combined
Public archive
forked from
moodleou/moodle-qtype_combined
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
moodle-qtype_combined
/
questiontype.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
263 lines (222 loc) · 11.2 KB
Breadcrumbs
moodle-qtype_combined
/
questiontype.php
Copy path
File metadata and controls
263 lines (222 loc) · 11.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?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/>.
/**
* Question type class for the combined question type.
*
* @package qtype_combined
* @copyright 2013 The Open University
* @author Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined
(
'
MOODLE_INTERNAL
'
) ||
die
();
require_once
(
$
CFG
->
libdir
.
'
/questionlib.php
'
);
require_once
(
$
CFG
->
dirroot
.
'
/question/engine/lib.php
'
);
require_once
(
$
CFG
->
dirroot
.
'
/question/type/combined/question.php
'
);
require_once
(
$
CFG
->
dirroot
.
'
/question/type/combined/combiner/forquestiontype.php
'
);
require_once
(
$
CFG
->
dirroot
.
'
/question/type/combined/combiner/runtime.php
'
);
/**
* The combined question type.
*
* @copyright 2013 The Open University
* @author Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class
qtype_combined
extends
question_type {
public
function
move_files
(
$
questionid
,
$
oldcontextid
,
$
newcontextid
) {
parent
::
move_files
(
$
questionid
,
$
oldcontextid
,
$
newcontextid
);
$
this
->
move_files_in_combined_feedback
(
$
questionid
,
$
oldcontextid
,
$
newcontextid
);
$
this
->
move_files_in_hints
(
$
questionid
,
$
oldcontextid
,
$
newcontextid
);
$
combiner
=
new
qtype_combined_combiner_for_question_type
();
$
combiner
->
move_subq_files
(
$
questionid
,
$
oldcontextid
,
$
newcontextid
);
}
protected
function
delete_files
(
$
questionid
,
$
contextid
) {
parent
::
delete_files
(
$
questionid
,
$
contextid
);
$
this
->
delete_files_in_combined_feedback
(
$
questionid
,
$
contextid
);
$
this
->
delete_files_in_hints
(
$
questionid
,
$
contextid
);
}
public
function
save_question_options
(
$
fromform
) {
global
$
DB
;
$
combiner
=
new
qtype_combined_combiner_for_question_type
();
if
(!
$
options
=
$
DB
->
get_record
(
'
qtype_combined
'
, [
'
questionid
'
=>
$
fromform
->
id
])) {
$
options
=
new
stdClass
();
$
options
->
questionid
=
$
fromform
->
id
;
$
options
->
correctfeedback
=
''
;
$
options
->
partiallycorrectfeedback
=
''
;
$
options
->
incorrectfeedback
=
''
;
$
options
->
id
=
$
DB
->
insert_record
(
'
qtype_combined
'
,
$
options
);
}
$
options
=
$
this
->
save_combined_feedback_helper
(
$
options
,
$
fromform
,
$
fromform
->
context
,
true
);
$
DB
->
update_record
(
'
qtype_combined
'
,
$
options
);
if
(
isset
(
$
fromform
->
subquestions
)) {
// Question import.
foreach
(
$
fromform
->
subquestions
as
$
subquestion
) {
$
subquestion
->
parent
=
$
fromform
->
id
;
$
subquestion
->
context
=
$
fromform
->
context
;
$
subquestion
->
category
=
$
fromform
->
category
;
$
subquestion
->
idnumber
=
null
;
$
this
->
save_imported_subquestion
(
$
subquestion
);
}
}
else
{
$
combiner
->
save_subqs
(
$
fromform
,
$
fromform
->
context
->
id
);
}
$
this
->
save_hints
(
$
fromform
,
true
);
}
/**
* This is a copy-paste of a bit in the middle of qformat_default::importprocess with changes to fit this situation.
*
* When I came to ugprade this code to Moodle 4.0, I found this comment which is not true:
* "This function will be removed in Moodle 2.6 when core Moodle is refactored so that
* save_question is used to save imported questions."
* Clearly that was never done.
*
* @param $fromimport stdClass Data from question import.
* @return bool|null null if everything went OK, true if there is an error or false if a notice.
*/
protected
function
save_imported_subquestion
(
$
fromimport
) {
global
$
USER
,
$
DB
,
$
OUTPUT
;
$
fromimport
->
stamp
=
make_unique_id_code
();
// Set the unique code (not to be changed).
$
fromimport
->
createdby
=
$
USER
->
id
;
$
fromimport
->
timecreated
=
time
();
$
fromimport
->
modifiedby
=
$
USER
->
id
;
$
fromimport
->
timemodified
=
time
();
$
fileoptions
= [
'
subdirs
'
=>
true
,
'
maxfiles
'
=> -
1
,
'
maxbytes
'
=>
0
,
];
$
fromimport
->
id
=
$
DB
->
insert_record
(
'
question
'
,
$
fromimport
);
if
(
$
DB
->
get_manager
()->
table_exists
(
'
question_bank_entries
'
)) {
// Moodle 4.x.
// Create a bank entry for each question imported.
$
questionbankentry
=
new
\
stdClass
();
$
questionbankentry
->
questioncategoryid
=
$
fromimport
->
category
;
$
questionbankentry
->
idnumber
=
null
;
$
questionbankentry
->
ownerid
=
$
fromimport
->
createdby
;
$
questionbankentry
->
id
=
$
DB
->
insert_record
(
'
question_bank_entries
'
,
$
questionbankentry
);
// Create a version for each question imported.
$
questionversion
=
new
\
stdClass
();
$
questionversion
->
questionbankentryid
=
$
questionbankentry
->
id
;
$
questionversion
->
questionid
=
$
fromimport
->
id
;
$
questionversion
->
version
=
1
;
$
questionversion
->
status
= \
core_question
\
local
\
bank
\question_version_status::
QUESTION_STATUS_READY
;
$
questionversion
->
id
=
$
DB
->
insert_record
(
'
question_versions
'
,
$
questionversion
);
}
if
(
isset
(
$
fromimport
->
questiontextitemid
)) {
$
fromimport
->
questiontext
=
file_save_draft_area_files
(
$
fromimport
->
questiontextitemid
,
$
fromimport
->
context
->
id
,
'
question
'
,
'
questiontext
'
,
$
fromimport
->
id
,
$
fileoptions
,
$
fromimport
->
questiontext
);
}
else
if
(
isset
(
$
fromimport
->
questiontextfiles
)) {
foreach
(
$
fromimport
->
questiontextfiles
as
$
file
) {
question_bank::
get_qtype
(
$
fromimport
->
qtype
)->
import_file
(
$
fromimport
->
context
,
'
question
'
,
'
questiontext
'
,
$
fromimport
->
id
,
$
file
);
}
}
if
(
isset
(
$
fromimport
->
generalfeedbackitemid
)) {
$
fromimport
->
generalfeedback
=
file_save_draft_area_files
(
$
fromimport
->
generalfeedbackitemid
,
$
fromimport
->
context
->
id
,
'
question
'
,
'
generalfeedback
'
,
$
fromimport
->
id
,
$
fileoptions
,
$
fromimport
->
generalfeedback
);
}
else
if
(
isset
(
$
fromimport
->
generalfeedbackfiles
)) {
foreach
(
$
fromimport
->
generalfeedbackfiles
as
$
file
) {
question_bank::
get_qtype
(
$
fromimport
->
qtype
)->
import_file
(
$
fromimport
->
context
,
'
question
'
,
'
generalfeedback
'
,
$
fromimport
->
id
,
$
file
);
}
}
$
DB
->
update_record
(
'
question
'
,
$
fromimport
);
// Now to save all the answers and type-specific options.
$
result
= question_bank::
get_qtype
(
$
fromimport
->
qtype
)->
save_question_options
(
$
fromimport
);
if
(!
empty
(
$
result
->
error
)) {
echo
$
OUTPUT
->
notification
(
$
result
->
error
);
// Can't use $transaction->rollback(); since it requires an exception,
// and I don't want to rewrite this code to change the error handling now.
$
DB
->
force_transaction_rollback
();
return
false
;
}
if
(!
empty
(
$
result
->
notice
)) {
echo
$
OUTPUT
->
notification
(
$
result
->
notice
);
return
true
;
}
return
null
;
}
public
function
make_question
(
$
questiondata
) {
$
question
=
parent
::
make_question
(
$
questiondata
);
$
question
->
combiner
=
new
qtype_combined_combiner_for_run_time_question_instance
();
// Need to process question text to get third param if any.
$
question
->
combiner
->
find_included_subqs_in_question_text
(
$
questiondata
->
questiontext
);
$
question
->
combiner
->
create_subqs_from_subq_data
(
$
questiondata
->
subquestions
);
$
question
->
combiner
->
make_subqs
();
return
$
question
;
}
protected
function
make_hint
(
$
hint
) {
return
question_hint_with_parts::
load_from_record
(
$
hint
);
}
protected
function
initialise_question_instance
(
question_definition
$
question
,
$
questiondata
) {
parent
::
initialise_question_instance
(
$
question
,
$
questiondata
);
$
this
->
initialise_combined_feedback
(
$
question
,
$
questiondata
,
true
);
}
public
function
get_question_options
(
$
question
) {
global
$
DB
;
if
(
false
===
parent
::
get_question_options
(
$
question
)) {
return
false
;
}
$
question
->
options
=
$
DB
->
get_record
(
'
qtype_combined
'
, [
'
questionid
'
=>
$
question
->
id
],
'
*
'
,
MUST_EXIST
);
$
question
->
subquestions
= qtype_combined_combiner_base::
get_subq_data_from_db
(
$
question
->
id
,
true
);
return
true
;
}
public
function
get_random_guess_score
(
$
questiondata
) {
$
overallrandomguessscore
=
0
;
foreach
(
$
questiondata
->
subquestions
as
$
subqdata
) {
$
subqrandomguessscore
= question_bank::
get_qtype
(
$
subqdata
->
qtype
)->
get_random_guess_score
(
$
subqdata
);
$
overallrandomguessscore
+=
$
subqdata
->
defaultmark
*
$
subqrandomguessscore
;
}
return
$
overallrandomguessscore
;
}
public
function
get_possible_responses
(
$
questiondata
) {
$
allpossibleresponses
= [];
foreach
(
$
questiondata
->
subquestions
as
$
subqdata
) {
$
possresponses
= question_bank::
get_qtype
(
$
subqdata
->
qtype
)->
get_possible_responses
(
$
subqdata
);
foreach
(
$
possresponses
as
$
subqid
=>
$
subqpossresponses
) {
$
respclassid
= qtype_combined_type_manager::
response_id
(
$
subqdata
->
name
,
$
subqdata
->
qtype
,
$
subqid
);
foreach
(
$
subqpossresponses
as
$
subqpossresponse
) {
$
subqpossresponse
->
fraction
=
$
subqpossresponse
->
fraction
*
$
subqdata
->
defaultmark
;
}
$
allpossibleresponses
[
$
respclassid
] =
$
subqpossresponses
;
}
}
ksort
(
$
allpossibleresponses
);
return
$
allpossibleresponses
;
}
public
function
export_to_xml
(
$
question
,
qformat_xml
$
format
,
$
extra
=
null
) {
$
output
=
''
;
$
output
.=
$
format
->
write_combined_feedback
(
$
question
->
options
,
$
question
->
id
,
$
question
->
contextid
);
$
output
.=
"
<subquestions>
\n"
;
foreach
(
$
question
->
subquestions
as
$
subquestion
) {
$
output
.=
$
format
->
writequestion
(
$
subquestion
);
}
$
output
.=
"
</subquestions>
\n"
;
return
$
output
;
}
public
function
import_from_xml
(
$
data
,
$
question
,
qformat_xml
$
format
,
$
extra
=
null
) {
if
(!
isset
(
$
data
[
'
@
'
][
'
type
'
]) ||
$
data
[
'
@
'
][
'
type
'
] !=
'
combined
'
) {
return
false
;
}
$
question
=
$
format
->
import_headers
(
$
data
);
$
question
->
qtype
=
'
combined
'
;
$
format
->
import_combined_feedback
(
$
question
,
$
data
,
true
);
$
format
->
import_hints
(
$
question
,
$
data
,
true
,
false
,
$
format
->
get_format
(
$
question
->
questiontextformat
));
$
question
->
subquestions
=
$
format
->
import_questions
(
$
data
[
'
#
'
][
'
subquestions
'
][
0
][
'
#
'
][
'
question
'
]);
return
$
question
;
}
}
Back
|
FazBrowse Home
|
New Git URL