This repository was archived by the owner on Jan 4, 2023. It is now read-only.
forked from trampgeek/moodle-qtype_coderunner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion.php
More file actions
585 lines (507 loc) · 22.8 KB
/
question.php
File metadata and controls
585 lines (507 loc) · 22.8 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
<?php
// This file is part of CodeRunner - http://coderunner.org.nz/
//
// CodeRunner 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.
//
// CodeRunner 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 CodeRunner. If not, see <http://www.gnu.org/licenses/>.
/**
* coderunner question definition classes.
*
* @package qtype
* @subpackage coderunner
* @copyright Richard Lobb, 2011, The University of Canterbury
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/question/behaviour/adaptive/behaviour.php');
require_once($CFG->dirroot . '/question/engine/questionattemptstep.php');
require_once($CFG->dirroot . '/question/behaviour/adaptive_adapted_for_coderunner/behaviour.php');
require_once($CFG->dirroot . '/question/type/coderunner/questiontype.php');
use qtype_coderunner\constants;
/**
* Represents a 'CodeRunner' question.
*/
class qtype_coderunner_question extends question_graded_automatically {
public $testcases = null; // Array of testcases.
/**
* Start a new attempt at this question, storing any information that will
* be needed later in the step. It is retrieved and applied by
* apply_attempt_state.
*
* For CodeRunner questions we pre-process the template parameters for any
* randomisation required, storing the processed template parameters in
* the question_attempt_step.
*
* @param question_attempt_step The first step of the {@link question_attempt}
* being started. Can be used to store state. Is set to null during
* question validation, and must then be ignored.
* @param int $varant which variant of this question to start. Will be between
* 1 and {@link get_num_variants()} inclusive.
*/
public function start_attempt(question_attempt_step $step=null, $variant=null) {
global $USER;
$user = $USER;
$this->student = $user;
if ($step !== null) {
parent::start_attempt($step, $variant);
$step->set_qt_var('_STUDENT', serialize($user));
}
$seed = mt_rand();
if ($step !== null) {
$step->set_qt_var('_mtrandseed', $seed);
}
$this->setup_template_params($seed);
if ($this->twigall) {
$this->twig_all();
}
}
// Retrieve the saved random number seed and reconstruct the template
// parameters to the state they were left after start_attempt was called.
// Also twig expand the rest of the question fields if $this->twigall is true.
public function apply_attempt_state(question_attempt_step $step) {
parent::apply_attempt_state($step);
$this->student = unserialize($step->get_qt_var('_STUDENT'));
// Short-lived legacy code, for use at U of C only
$templateparams = $step->get_qt_var('_templateparamsinstance');
if ($templateparams !== null) {
$this->templateparams = $templateparams;
if ($step->get_qt_var('_twigall')) {
$this->twigall = true;
}
} else {
// Non-legacy code lands here
$seed = $step->get_qt_var('_mtrandseed');
if ($seed === null) {
// Rendering a question that was begun before randomisation
// was introduced into the code
$seed = mt_rand();
}
$this->setup_template_params($seed);
}
if ($this->twigall) {
$this->twig_all();
}
}
/**
* Override default behaviour so that we can use a specialised behaviour
* that caches test results returned by the call to grade_response().
*
* @param question_attempt $qa the attempt we are creating an behaviour for.
* @param string $preferredbehaviour the requested type of behaviour.
* @return question_behaviour the new behaviour object.
*/
public function make_behaviour(question_attempt $qa, $preferredbehaviour) {
// Regardless of the preferred behaviour, always use an adaptive
// behaviour.
return new qbehaviour_adaptive_adapted_for_coderunner($qa, $preferredbehaviour);
}
public function get_expected_data() {
return array('answer' => PARAM_RAW,
'language' => PARAM_NOTAGS);
}
public function summarise_response(array $response) {
if (isset($response['answer'])) {
return $response['answer'];
} else {
return null;
}
}
public function is_gradable_response(array $response) {
return array_key_exists('answer', $response) &&
$response['answer'] !== '' &&
strlen($response['answer']) >= constants::FUNC_MIN_LENGTH;
}
public function is_complete_response(array $response) {
return $this->is_gradable_response($response);
}
/**
* In situations where is_gradable_response() returns false, this method
* should generate a description of what the problem is.
* @return string the message.
*/
public function get_validation_error(array $response) {
if (array_key_exists('answer', $response)) {
if ($response['answer'] === '') {
return get_string('answerrequired', 'qtype_coderunner');
} else if (strlen($response['answer']) < constants::FUNC_MIN_LENGTH) {
return get_string('answertooshort', 'qtype_coderunner');
}
}
if (array_key_exists('_testoutcome', $response)) {
$outcome = unserialize($response['_testoutcome']);
return $outcome->errormessage;
} else {
return get_string('unknownerror', 'qtype_coderunner');
}
}
/** This function is used by the question engine to prevent regrading of
* unchanged submissions.
*
* @param array $prevresponse
* @param array $newresponse
* @return boolean
*/
public function is_same_response(array $prevresponse, array $newresponse) {
return question_utils::arrays_same_at_key_missing_is_blank(
$prevresponse, $newresponse, 'answer') &&
question_utils::arrays_same_at_key_missing_is_blank(
$prevresponse, $newresponse, 'language');
}
public function get_correct_response() {
return $this->get_correct_answer();
}
public function get_correct_answer() {
// Return the sample answer, if supplied.
if (!isset($this->answer)) {
return null;
} else {
$answer = array('answer' => $this->answer);
// For multilanguage questions we also need to specify the language.
if (!empty($this->acelang) && strpos($this->acelang, ',') !== false) {
list($langs, $defaultlang) = qtype_coderunner_util::extract_languages($this->acelang);
$default = empty($defaultlang) ? $langs[0] : $defaultlang;
$answer['language'] = $default;
}
return $answer;
}
}
/**
* Grade the given student's response.
* This implementation assumes a modified behaviour that will accept a
* third array element in its response, containing data to be cached and
* served up again in the response on subsequent calls.
* @param array $response the qt_data for the current pending step. The
* two relevant keys are '_testoutcome', which is a cached copy of the
* grading outcome if this response has already been graded and 'answer'
* (the student's answer) otherwise.
* @param bool $isprecheck true iff this grading is occurring because the
* student clicked the precheck button
* @return 3-element array of the mark (0 - 1), the question_state (
* gradedright, gradedwrong, gradedpartial, invalid) and the full
* qtype_coderunner_testing_outcome object to be cached. The invalid
* state is used when a sandbox error occurs.
* @throws coding_exception
*/
public function grade_response(array $response, $isprecheck=false) {
if ($isprecheck && empty($this->precheck)) {
throw new coding_exception("Unexpected precheck");
}
$language = empty($response['language']) ? '' : $response['language'];
$gradingreqd = true;
if (!empty($response['_testoutcome'])) {
$testoutcomeserial = $response['_testoutcome'];
$testoutcome = unserialize($testoutcomeserial);
if ($testoutcome instanceof qtype_coderunner_testing_outcome // Ignore legacy-format outcomes.
&& $testoutcome->isprecheck == $isprecheck) {
$gradingreqd = false; // Already graded and with same precheck state.
}
}
if ($gradingreqd) {
// We haven't already graded this submission or we graded it with
// a different precheck setting.
$code = $response['answer'];
$testcases = $this->filter_testcases($isprecheck, $this->precheck);
$runner = new qtype_coderunner_jobrunner();
$testoutcome = $runner->run_tests($this, $code, $testcases, $isprecheck, $language);
$testoutcomeserial = serialize($testoutcome);
}
$datatocache = array('_testoutcome' => $testoutcomeserial);
if ($testoutcome->run_failed()) {
return array(0, question_state::$invalid, $datatocache);
} else if ($testoutcome->all_correct()) {
return array(1, question_state::$gradedright, $datatocache);
} else if ($this->allornothing &&
!($this->grader === 'TemplateGrader' && $this->iscombinatortemplate)) {
return array(0, question_state::$gradedwrong, $datatocache);
} else {
// Allow partial marks if not allornothing or if it's a combinator template grader.
return array($testoutcome->mark_as_fraction(),
question_state::$gradedpartial, $datatocache);
}
}
/**
* @return an array of result column specifiers, each being a 2-element
* array of a column header and the testcase field to be displayed
*/
public function result_columns() {
if (isset($this->resultcolumns) && $this->resultcolumns) {
$resultcolumns = json_decode($this->resultcolumns);
} else {
// Use default column headers, equivalent to json_decode of (in English):
// '[["Test", "testcode"], ["Input", "stdin"], ["Expected", "expected"], ["Got", "got"]]'.
$resultcolumns = array(
array(get_string('testcolhdr', 'qtype_coderunner'), 'testcode'),
array(get_string('inputcolhdr', 'qtype_coderunner'), 'stdin'),
array(get_string('expectedcolhdr', 'qtype_coderunner'), 'expected'),
array(get_string('gotcolhdr', 'qtype_coderunner'), 'got'),
);
}
return $resultcolumns;
}
// Return an array of all the use_as_example testcases.
public function example_testcases() {
return array_filter($this->testcases, function($tc) {
return $tc->useasexample;
});
}
// Twig expand all text fields of the question except the templateparam field
// (which should have been expanded when the question was started) and
// the template itself.
// Done only if randomisation is specified within the template params.
private function twig_all() {
// Before twig expanding all fields, copy the template parameters
// into $this->parameters.
if (!empty($this->templateparams)) {
$this->parameters = json_decode($this->templateparams);
} else {
$this->parameters = array();
}
// Twig expand everything in a context that includes the template
// parameters and the STUDENT and QUESTION objects. The only thing
// guaranteed about the QUESTION object is the parameters field - use
// other fields at your peril (since the order in which they are
// expanded might vary in the future).
$this->questiontext = $this->twig_expand($this->questiontext);
$this->generalfeedback = $this->twig_expand($this->generalfeedback);
$this->answer = $this->twig_expand($this->answer);
$this->answerpreload = $this->twig_expand($this->answerpreload);
foreach ($this->testcases as $key => $test) {
foreach (['testcode', 'stdin', 'expected', 'extra'] as $field) {
$text = $this->testcases[$key]->$field;
$this->testcases[$key]->$field = $this->twig_expand($text);
}
}
}
/**
* Return Twig-expanded version of the given text. The
* Twig environment includes the question itself (this) and the template
* parameters. Additional twig environment parameters are passed in via
* $twigparams. Template parameters are hoisted if required.
* @param string $text Text to be twig expanded.
* @param associative array $twigparams Extra twig environment parameters
*/
public function twig_expand($text, $twigparams=array()) {
$twig = qtype_coderunner_twig::get_twig_environment();
$twigparams['QUESTION'] = $this;
if ($this->hoisttemplateparams) {
foreach ($this->parameters as $key => $value) {
$twigparams[$key] = $value;
}
}
$twigparams['STUDENT'] = new qtype_coderunner_student($this->student);
return $twig->render($text, $twigparams);
}
/**
* Define the template parameters for this question by Twig-expanding
* both our own template params and our prototype template params and
* merging the two.
* @param type $seed The random number seed to set for Twig randomisation
*/
private function setup_template_params($seed) {
$twig = qtype_coderunner_twig::get_twig_environment();
$twigparams = array('STUDENT' => new qtype_coderunner_student($this->student));
mt_srand($seed);
$ournewtemplateparams = $twig->render($this->templateparams, $twigparams);
if (isset($this->prototypetemplateparams)) {
$prototypenewtemplateparams = $twig->render($this->prototypetemplateparams, $twigparams);
$this->templateparams = qtype_coderunner_util::merge_json($prototypenewtemplateparams, $ournewtemplateparams);
} else {
// Missing prototype?
$this->templateparams = $ournewtemplateparams;
}
}
// Extract and return the appropriate subset of the set of question testcases
// given $isprecheckrun (true iff this was a run initiated by clicking
// precheck) and the question's prechecksetting (0, 1, 2, 3, 4 for Disable,
// Empty, Examples, Selected and All respectively).
protected function filter_testcases($isprecheckrun, $prechecksetting) {
if (!$isprecheckrun) {
if ($prechecksetting != constants::PRECHECK_SELECTED) {
return $this->testcases;
} else {
return $this->selected_testcases(false);
}
} else { // This is a precheck run.
if ($prechecksetting == constants::PRECHECK_EMPTY) {
return array($this->empty_testcase());
} else if ($prechecksetting == constants::PRECHECK_EXAMPLES) {
return $this->example_testcases();
} else if ($prechecksetting == constants::PRECHECK_SELECTED) {
return $this->selected_testcases(true);
} else if ($prechecksetting == constants::PRECHECK_ALL) {
return $this->testcases;
} else {
throw new coding_exception('Precheck clicked but no precheck button?!');
}
}
}
// Return the appropriate subset of questions in the case that the question
// precheck setting is "selected", given whether or not this is a precheckrun.
protected function selected_testcases($isprecheckrun) {
$testcases = array();
foreach ($this->testcases as $testcase) {
if (($isprecheckrun && $testcase->testtype != constants::TESTTYPE_NORMAL) ||
(!$isprecheckrun && $testcase->testtype != constants::TESTTYPE_PRECHECK)) {
$testcases[] = $testcase;
}
}
return $testcases;
}
// Return an empty testcase - an artifical testcase with all fields
// empty or zero except for a mark of 1.
private function empty_testcase() {
return (object) array(
'testtype' => 0,
'testcode' => '',
'stdin' => '',
'expected' => '',
'extra' => '',
'display' => 'HIDE',
'useasexample' => 0,
'hiderestiffail' => 0,
'mark' => 1
);
}
/* ================================================================
* Interface methods for use by jobrunner.
================================================================*/
// Return the template.
public function get_template() {
return $this->template;
}
// Return the programming language used to run the code.
public function get_language() {
return $this->language;
}
// Get the showsource boolean.
public function get_show_source() {
return $this->showsource;
}
// Return the regular expression used to split the combinator template
// output into individual tests.
public function get_test_splitter_re() {
return $this->testsplitterre;
}
// Return whether or not the template is a combinator.
public function get_is_combinator() {
return $this->iscombinatortemplate;
}
// Return whether or not multiple stdins are allowed when using combinator.
public function allow_multiple_stdins() {
return $this->allowmultiplestdins;
}
// Return an instance of the sandbox to be used to run code for this question.
public function get_sandbox() {
global $CFG;
$sandbox = $this->sandbox; // Get the specified sandbox (if question has one).
if ($sandbox === null) { // No sandbox specified. Use best we can find.
$sandboxinstance = qtype_coderunner_sandbox::get_best_sandbox($this->language);
if ($sandboxinstance === null) {
throw new qtype_coderunner_exception("Language {$this->language} is not available on this system");
}
} else {
$sandboxinstance = qtype_coderunner_sandbox::get_instance($sandbox);
if ($sandboxinstance === null) {
throw new qtype_coderunner_exception("Question is configured to use a non-existent or disabled sandbox ($sandbox)");
}
}
return $sandboxinstance;
}
// Get an instance of the grader to be used to grade this question.
public function get_grader() {
global $CFG;
$grader = $this->grader == null ? constants::DEFAULT_GRADER : $this->grader;
if ($grader === 'CombinatorTemplateGrader') { // Legacy grader type.
$grader = 'TemplateGrader';
assert($this->iscombinatortemplate);
}
$graders = qtype_coderunner_grader::available_graders();
$graderclass = $graders[$grader];
$graderinstance = new $graderclass();
return $graderinstance;
}
// Return all the datafiles to use for a run, namely all the files
// uploaded with this question itself plus all the files uploaded with the
// prototype.
public function get_files() {
if ($this->prototypetype != 0) { // Is this a prototype question?
$files = array(); // Don't load the files twice.
} else {
// Load any files from the prototype.
$this->get_prototype();
$files = self::get_data_files($this->prototype, $this->prototype->questionid);
}
$files += self::get_data_files($this, $this->id); // Add in files for this question.
return $files;
}
// Get the sandbox parameters for a run.
public function get_sandbox_params() {
if (isset($this->sandboxparams)) {
$sandboxparams = json_decode($this->sandboxparams, true);
} else {
$sandboxparams = array();
}
if (isset($this->cputimelimitsecs)) {
$sandboxparams['cputime'] = intval($this->cputimelimitsecs);
}
if (isset($this->memlimitmb)) {
$sandboxparams['memorylimit'] = intval($this->memlimitmb);
}
if (isset($this->templateparams) && $this->templateparams != '') {
$this->parameters = json_decode($this->templateparams);
}
return $sandboxparams;
}
/**
* Load the prototype for this question and store in $this->prototype
*/
public function get_prototype() {
if (!isset($this->prototype)) {
$context = qtype_coderunner::question_context($this);
$this->prototype = qtype_coderunner::get_prototype($this->coderunnertype, $context);
}
}
/**
* Return an associative array mapping filename to datafile contents
* for all the datafiles associated with a given question (which may
* be a real question or, in the case of a prototype, the question_options
* row) and the questionid from the mdl_questions table.
*/
private static function get_data_files($question, $questionid) {
global $DB, $USER;
// If not given in the question object get the contextid from the database.
if (isset($question->contextid)) {
$contextid = $question->contextid;
} else {
$context = qtype_coderunner::question_context($question);
$contextid = $context->id;
}
$fs = get_file_storage();
$filemap = array();
if (isset($question->filemanagerdraftid)) {
// If we're just validating a question, get files from user draft area.
$draftid = $question->filemanagerdraftid;
$context = context_user::instance($USER->id);
$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, '', false);
} else {
// Otherwise, get the stored files for this question.
$files = $fs->get_area_files($contextid, 'qtype_coderunner', 'datafile', $questionid);
}
foreach ($files as $f) {
$name = $f->get_filename();
if ($name !== '.') {
$filemap[$f->get_filename()] = $f->get_content();
}
}
return $filemap;
}
}