-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion.php
1430 lines (1210 loc) · 58.2 KB
/
question.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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Stateful
//
// Stateful 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.
//
// Stateful 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 Stateful. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types = 1);
defined('MOODLE_INTERNAL') || die();
require_once __DIR__ . '/stateful/core/model.class.php';
require_once __DIR__ . '/stateful/core/state_carrier.php';
require_once __DIR__ . '/stateful/utils.class.php';
require_once __DIR__ . '/stateful/core/functionbuilder.class.php';
require_once __DIR__ . '/stateful/input2/input.controller.php';
require_once __DIR__ . '/stateful/handling/validation.php';
require_once __DIR__ . '/../../behaviour/stateful/behaviour.php';
require_once __DIR__ . '/stacklib.php';
require_once __DIR__ . '/locallib.php';
class qtype_stateful_question extends question_definition implements
question_stateful, stateful_model {
// These are the properties we simply copy from STACK.
/**
* @var string STACK specific: variables, as authored by the teacher.
*/
public $questionvariables;
/**
* @var string STACK specific: used to separate random variants.
*/
public $questionnote;
/**
* @var stack_options STACK specific: question-level options.
*/
public $options;
// Then the more specific ones.
// Store the scenes. Keyed by name ordered by definition order.
public $scenes;
// And the full definitions for the state variables. Keyed by name ordered by definition order.
public $variables;
// The name of the entry scene.
public $entryscene;
public $stackversion;
public $statefulversion;
// Compiledcache is a complex set of expressions and lists stored in the database.
// Basically the precompiled PRT functions and such.
public $compiledcache;
// Data that will not get used in execution, just holding it for export actions.
public $genericmeta;
public $parlength;
public $variants;
// The state storage access object from the behaviour.
public $state = null;
// Code readability vs. efficiency.
const SCENE_CURRENT = -1;
const SCENE_PATH = -2;
// e.g. teachers answers evaluated for this scene.
private $casparams = [];
// As we need to init the input values we may need to keep track on which
// scene was last initted.
private $lastsceneinited = null;
public $security = null;
// Current active input controller.
// stateful_input_controller
public $inputs;
// Scenetext gets rendered during init lets keep a hold on in.
private $scenetext;
// General feedback is also rendered during init.
private $modelsolution;
// Some flags to track execution, in case we are using the question
// through simpler APIs that do things direct.
public $sceneinitialised = false;
public $inputvalidated = false;
public $prtsprocessed = false;
public $inputvalidationrendered = false;
/**
* @var castext2_processor an accesspoint to the question attempt for
* the castext2 post-processing logic for pluginfile url-writing.
*/
public $castextprocessor = null;
public function make_behaviour(
question_attempt $qa,
$preferredbehaviour
) {
$this->castextprocessor = new stateful_castext2_default_processor($qa);
return question_engine::make_behaviour('stateful', $qa,
$preferredbehaviour);
}
/**
* Gives the question access to the state storage. You can assume that this
* has been called early in the process, before anything that requires
* evaluation of responses.
* @param qbehaviour_stateful_state_storage access to the storage of this step.
*/
public function set_state(qbehaviour_stateful_state_storage $state) {
$this->state = $state;
}
public function get_state() {
return $this->state;
}
public function get_state_array(): array {
$r = array('RANDOM_SEED' => $this->seed);
$r['SCENE_CURRENT'] = $this->state->get(self::SCENE_CURRENT);
$r['SCENE_PATH'] = $this->state->get(self::SCENE_PATH);
foreach ($this->variables as $variable) {
$r[$variable->name] = $this->state->get($variable->number);
}
return $r;
}
/**
* Asks the question for an array defining the numeric identifiers of all the state variables it needs stored or retrieved from storage. Always, called before set_state. Also provides matching names for debug displays.
* @return array of integer identifiers mapped to variable names.
*/
public function get_state_variable_identifiers(): array{
// These are the stored main variables. SCENE_NEXT is not stored.
$r = [self::SCENE_CURRENT => 'SCENE_CURRENT',
self::SCENE_PATH => 'SCENE_PATH'];
// These are the ones coming from the logic.
foreach ($this->variables as $variable) {
$r[$variable->number] = $variable->name;
}
return $r;
}
public function get_scene_sequence_number(qbehaviour_stateful_state_storage
$state=null): int {
$path = [];
if ($state !== null) {
$path = stateful_utils::string_to_list($state->get(self::SCENE_PATH, '[]'));
} else {
$path = stateful_utils::string_to_list($this->state->get(self::SCENE_PATH, '[]'));
}
return count($path);
}
public function is_valid_input(array $values): bool{
$scene = $this->get_current_scene();
if ($this->lastsceneinited !== $scene->name) {
$this->init_from_state();
}
$valid = $this->validate_input($values);
return $valid;
}
public function is_in_end_scene(): bool{
$scene = $this->get_current_scene();
return count($scene->inputs) === 0;
}
public function get_max_fraction() {
return $this->defaultmark;
}
public function get_min_fraction() {
return 0.0;
}
//private function get_current_scene(): stateful_scene {
public function get_current_scene() {
// Use this while the behaviour state passing fails.
if ($this->state === null) {
echo '<pre>';
debug_print_backtrace();
echo '</pre>';
}
return $this->scenes[stack_utils::maxima_string_to_php_string($this->
state->get(self::SCENE_CURRENT, stack_utils::
php_string_to_maxima_string($this->
entryscene)))];
}
private function validate_input(array $input): bool {
// To validate input we need to build a session with:
// - Question-variables
// - State-variables
// - Scene-variables
// - The validation statements of the inputs.
// Should the last set be empty we do not need to evaluate
// anything in CAS.
$scene = $this->get_current_scene();
$valid = true;
$sec = $this->security;
$validationstatements = $this->inputs->collect_validation_statements($input, $sec);
$this->inputvalidated = true;
if (count($validationstatements) === 0) {
return $this->inputs->all_valid();
}
$statements = [];
if (is_numeric($this->seed) || is_integer($this->seed)) {
$statements[] = new stack_secure_loader('RANDOM_SEED:' . $this->seed, 'validate_input');
} else {
$statements[] = stack_ast_container_silent::make_from_teacher_source('RANDOM_SEED:' . $this->seed, 'validate_input', $sec);
}
// If we are using localisation we should tell the CAS side logic about it.
// For castext rendering and other tasks.
if (count($this->get_compiled('langs')) > 0) {
$ml = new stack_multilang();
$selected = $ml->pick_lang($this->get_compiled('langs'));
$statements[] = new stack_secure_loader('%_STACK_LANG:' .
stack_utils::php_string_to_maxima_string($selected), 'language setting');
}
// The question-variables.
$statements[] = $this->get_compiled('qv');
// Then call them.
$statements[] = new stack_secure_loader('_question_variables(RANDOM_SEED)', 'validate_input');
// The state-variables
foreach ($this->variables as $statevar) {
$statements[] = stateful_state_carrier::make_from_teacher_source($statevar->name .
':' .
$this->state->get($statevar->number), 'validate_input', $sec);
}
$statements[] = stack_ast_container_silent::make_from_teacher_source('SCENE_CURRENT:' . $this->
state->get(self::SCENE_CURRENT), 'validate_input', $sec);
$statements[] = stack_ast_container_silent::make_from_teacher_source('SCENE_PATH:' . $this->
state->get(self::SCENE_PATH), 'validate_input', $sec);
$statements[] = stack_ast_container_silent::make_from_teacher_source('SCENE_NEXT:false', 'validate_input', $sec);
// And the scene variables.
$svfunction = $this->get_compiled('scene-' . $scene->name .
'-variables');
$statements[] = $svfunction;
// And to call it we need its signature.
$statements[] = new stack_secure_loader(explode(':=', $svfunction->
get_evaluationform())[0], 'validate_input');
// Actual validation happens in simp:false.
$statements[] = new stack_secure_loader('simp:false', 'validate_input');
// Include the validation statements into the session.
$statements = array_merge($statements, $validationstatements);
// And execute.
$session = new stack_cas_session2($statements, $this->options, $this->seed);
$session->errclass = 'stateful_cas_error';
$session->instantiate();
// the input controller now has all the information it needs.
return $this->inputs->all_valid_and_validated_or_blank();
}
// Generates validation boxes in cases where PRTs are not being evaluated.
// Otherwise this will be inlined to PRT evaluation.
public function render_validation(array $input): void {
$scene = $this->get_current_scene();
if ($this->lastsceneinited !== $scene->name) {
$this->init_from_state();
}
$validationrender = $this->inputs->collect_validation_render_statements();
if (count($validationrender) > 0) {
// So we need to construct the session for these.
$sec = $this->security;
$statements = [stack_ast_container_silent::make_from_teacher_source('RANDOM_SEED:' . $this
->seed, 'render_validation', $sec)];
// If we are using localisation we should tell the CAS side logic about it.
// For castext rendering and other tasks.
if (count($this->get_compiled('langs')) > 0) {
$ml = new stack_multilang();
$selected = $ml->pick_lang($this->get_compiled('langs'));
$statements[] = new stack_secure_loader('%_STACK_LANG:' .
stack_utils::php_string_to_maxima_string($selected), 'language setting');
}
// 1. We need the question variables for this seed.
$qvfunction = $this->get_compiled('qv');
$statements[] = $qvfunction;
// Then call it.
$statements[] = new stack_secure_loader('_question_variables(RANDOM_SEED)', 'render_validation');
// 2. Add the state into it.
foreach ($this->variables as $statevar) {
$statements[] = stateful_state_carrier::make_from_teacher_source($statevar->name .
':' .
$this->state->get($statevar->number), 'render_validation', $sec);
}
$statements[] = stack_ast_container_silent::make_from_teacher_source('SCENE_CURRENT:' . $this->
state->get(self::SCENE_CURRENT), 'render_validation', $sec);
$statements[] = stack_ast_container_silent::make_from_teacher_source('SCENE_PATH:' . $this->
state->get(self::SCENE_PATH), 'render_validation', $sec);
$statements[] = stack_ast_container_silent::make_from_teacher_source('SCENE_NEXT:false', 'render_validation', $sec);
// 3. We also need the scene variables for this seed and state.
$svfunction = $this->get_compiled('scene-' . $scene->name .
'-variables');
$statements[] = $svfunction;
// And to call it we need its signature.
$statements[] = new stack_secure_loader(explode(':=', $svfunction->
get_evaluationform())[0], 'render_validation');
// 4. Include input values.
$statements[] = new stack_secure_loader('simp:false', 'render_validation');
$statements = array_merge($statements, $this->inputs->collect_cas_values());
// 5. The validation statements...
$statements = array_merge($statements, $validationrender);
foreach ($statements as $statement) {
if ($statement->get_valid() !== true) {
throw new stateful_exception(stateful_string(
'error_in_processing'));
}
}
$session = new stack_cas_session2($statements, $this->options, $this->seed);
$session->errclass = 'stateful_cas_error';
$session->instantiate();
// Check that error.
if ($session->get_errors() !== null && $session->get_errors() !== '') {
throw new stateful_exception(stateful_string(
'error_in_input_processing_cas_evaluation', $session->get_errors()));
}
}
$this->inputvalidationrendered = true;
}
public function process_input(
array $input,
bool $return_feedback = false
): array{
$scene = $this->get_current_scene();
if ($this->lastsceneinited !== $scene->name) {
$this->init_from_state();
}
$r = ['_attemptstatus' => question_state::$todo, '_summary' => '',
'_feedback' => []]
;
$this->prtsprocessed = true;
// 0. Stop processing if something is invalid, just in case something
// might be missed or done too early.
// 0.1. Also stop if we are dealing with the testcases.
if (!$this->is_valid_input($input) || (isset($input['testCaseFill']) && $input['testCaseFill'] !== '-')) {
$r['_attemptstatus'] = question_state::$invalid;
$summary = ['[UNCONFIRMED OR INVALID INPUT]'];
$summary[] = $this->inputs->summarise();
$r['_summary'] = implode(', ', $summary);
foreach ($scene->prts as $prt) {
$prtid = 'prt-result-' . $this->get_scene_sequence_number($this
->state) . '-' . $prt->name;
$this->casparams[$prtid] = 'NOEVAL';
}
$this->render_validation($input);
return $r;
}
// 0.1. If this is an end scene why do we continue at all?
if ($this->is_in_end_scene()) {
foreach ($scene->prts as $prt) {
$prtid = 'prt-result-' . $this->get_scene_sequence_number($this
->state) . '-' . $prt->name;
$this->casparams[$prtid] = 'NOEVAL';
}
$r['_attemptstatus'] = question_state::$complete;
$r['_summary'] = '[END]';
$this->render_validation($input);
return $r;
}
// 1. Do we have input and is it enough to trigger any PRTs?
$prts = [];
foreach ($scene->prts as $prt) {
$required = $this->get_compiled('scene-' . $scene->name . '-prt-' .
$prt->name . '|inputs');
if ($this->inputs->has_valid_for($required, false)) {
// All seem to be available for this PRT.
$prts[$prt->name] = $this->get_compiled('scene-' . $scene->name
. '-prt-' . $prt->name);
} else {
$prtid = 'prt-result-' . $this->get_scene_sequence_number($this
->state) . '-' . $prt->name;
$this->casparams[$prtid] = 'NOEVAL';
}
}
$summary = [];
// 2. Now we have the processing functions for all PRTs we may need to process
// lets build the scope for them.
if (count($prts) > 0) {
$summary[] = '[CLASSIFYING INPUT]';
$sec = $this->security;
$statements = [stack_ast_container_silent::make_from_teacher_source('RANDOM_SEED:' . $this
->seed, 'process_input', $sec)];
// If we are using localisation we should tell the CAS side logic about it.
// For castext rendering and other tasks.
if (count($this->get_compiled('langs')) > 0) {
$ml = new stack_multilang();
$selected = $ml->pick_lang($this->get_compiled('langs'));
$statements[] = new stack_secure_loader('%_STACK_LANG:' .
stack_utils::php_string_to_maxima_string($selected), 'language setting');
}
// 1. We need the question variables for this seed.
$qvfunction = $this->get_compiled('qv');
$statements[] = $qvfunction;
// Then call it.
$statements[] = new stack_secure_loader('_question_variables(RANDOM_SEED)', 'process_input');
// 2. Add the state into it.
foreach ($this->variables as $statevar) {
$statements[] = stateful_state_carrier::make_from_teacher_source($statevar->name .
':' .
$this->state->get($statevar->number), 'process_input', $sec);
}
$statements[] = stack_ast_container_silent::make_from_teacher_source('SCENE_CURRENT:' . $this->
state->get(self::SCENE_CURRENT), 'process_input', $sec);
$statements[] = stack_ast_container_silent::make_from_teacher_source('SCENE_PATH:' . $this->
state->get(self::SCENE_PATH), 'process_input', $sec);
$statements[] = stack_ast_container_silent::make_from_teacher_source('SCENE_NEXT:false', 'process_input', $sec);
// 3. We also need the scene variables for this seed and state.
$svfunction = $this->get_compiled('scene-' . $scene->name .
'-variables');
$statements[] = $svfunction;
// And to call it we need its signature.
$statements[] = new stack_secure_loader(explode(':=', $svfunction->
get_evaluationform())[0], 'process_input');
// 4. Include input values.
$statements[] = new stack_secure_loader('simp:false', 'process_input');
$summary[] = $this->inputs->summarise();
$statements = array_merge($statements, $this->inputs->collect_cas_values());
// 4.5 Include validation display values.
$statements = array_merge($statements, $this->inputs->collect_validation_render_statements());
// 5. Define the PRT functions.
foreach ($prts as $prtfunction) {
$statements[] = $prtfunction;
}
// Ensure simp:false after validation display logic.
$statements[] = new stack_secure_loader('simp:false', 'process_input');
// 6. Call them and catch the values.
$i = 0;
$prtresults = array();
foreach ($prts as $prtfunction) {
$sta = stack_ast_container::make_from_teacher_source('__PRT_RESULT_' . $i .
':' .
explode(':=', $prtfunction->get_evaluationform())[0], 'process_input', $sec);
$prtresults[$i] = $sta;
// Ensure that each gets called with simp:false.
$statements[] = new stack_secure_loader('simp:false', 'process_input');
$statements[] = $sta;
$i = $i + 1;
}
// After that simplify.
$statements[] = new stack_secure_loader('simp:true', 'process_input');
// 7. Ensure output of state variables for storage.
// First append to the path... and output new values for the SCENE-vars
$state = array();
$state['SCENE_PATH'] = stateful_state_carrier::make_from_teacher_source(
'SCENE_PATH:if not is(SCENE_NEXT=false) then (SCENE_PATH:append(SCENE_PATH,[SCENE_CURRENT]),SCENE_CURRENT:SCENE_NEXT,SCENE_PATH) else SCENE_PATH'
, 'process_input', $sec);
$state['SCENE_CURRENT'] = stateful_state_carrier::make_from_teacher_source(
'SCENE_CURRENT:SCENE_CURRENT', 'process_input', $sec);
$state['SCENE_NEXT'] = stateful_state_carrier::make_from_teacher_source('SCENE_NEXT:SCENE_NEXT', 'process_input', $sec);
foreach ($this->variables as $statevar) {
// Then cause output of current values of other vars.
$state[$statevar->name] = stateful_state_carrier::make_from_teacher_source($statevar->name . ':' .
$statevar->name, 'process_input', $sec);
}
$statements = array_merge($statements, array_values($state));
// Again instantiate, and validate already validated.
// TODO: that session that does not require this.
foreach ($statements as $statement) {
if ($statement->get_valid() !== true) {
throw new stateful_exception(stateful_string(
'error_in_processing'));
}
}
$session = new stack_cas_session2($statements, $this->options, $this
->seed);
$session->errclass = 'stateful_cas_error';
$session->instantiate();
// Check that error.
if ($session->get_errors() !== null && $session->get_errors() !== '') {
throw new stateful_exception(stateful_string(
'error_in_input_processing_cas_evaluation', $session->get_errors()));
}
// 8. Read in the results and state. Also store the prt result to cache.
$i = 0;
foreach ($prts as $name => $duh) {
$r[$name] = $prtresults[$i]->get_value();
// Now we need the sequence number to identify for which scene
// the prt results apply.
$prtid = 'prt-result-' . $this->get_scene_sequence_number($this
->state) . '-' . $name;
$this->casparams[$prtid] = $r[$name];
if ($prtresults[$i]->is_list(true) > 3) {
$feedback = $prtresults[$i]->get_list_element(4, true);
$list = $prtresults[$i]->get_evaluated();
if ($list instanceof MP_Root) {
$list = $list->items[0];
}
if ($list instanceof MP_Statement) {
$list = $list->statement;
}
// Do some cleaning so that we do not store certain things.
$short = clone $list;
$short->items = array_slice($short->items, 0, 4);
$r['_feedback'][$name] = $feedback;
$r[$name] = $list->toString();
}
$i = $i + 1;
}
// 9. If scene change happened initialise inputs. And store the changed state.
// state only changes if scene changes.
// And scene changes may have been deactivated.
if ($state['SCENE_NEXT']->get_evaluated_state() !== 'false' && ! isset($input['%deactivate%'])) {
foreach ($this->variables as $statevar) {
$this->state->set($statevar->number, $state[
$statevar->name]->get_evaluated_state());
}
$this->state->set(self::SCENE_PATH, $state[
'SCENE_PATH']->get_evaluated_state());
$this->state->set(self::SCENE_CURRENT, $state['SCENE_NEXT']->get_evaluated_state());
$this->init_from_state();
// Ensure reinitilaisation of input in cases where
// input names are shared in following scenes.
$this->validate_input($input);
$this->render_validation($input);
$summary[0] = '[INPUT CAUSED STATE CHANGE]';
$r['_attemptstatus'] = question_state::$complete;
} else {
// Evaluating, whether this was wrong or not is not sensible.
$r['_attemptstatus'] = question_state::$complete;
}
} else {
$summary[] = '[NO ACTIONABLE INPUT]';
$summary[] = $this->inputs->summarise();
$r['_attemptstatus'] = question_state::$todo;
// We do still need to produce validation renders if we
// have content for them.
$this->render_validation($input);
}
$r['_summary'] = implode(', ', $summary);
if (!$return_feedback) {
unset($r['_feedback']);
}
return $r;
}
public function evaluate_total_grade(
array $sequence,
bool $penalties
):
array{
$score = 0.0;
// Get the path.
$path = stateful_utils::string_to_list($this->state->get(self::
SCENE_PATH, '[' . stack_utils::php_string_to_maxima_string(
$this->entryscene) .
']'));
// Note that the path does not include current scene.
$path[] = $this->state->get(self::SCENE_CURRENT, $this->entryscene);
// Collect scores of all PRTs. For all scenes
$prts = [];
// For each step in the path check matching stored partial results.
$i = 0;
foreach ($path as $scenename) {
if (!array_key_exists($i, $sequence)) {
$i++;
continue;
}
$scene = $this->scenes[stack_utils::maxima_string_to_php_string(
$scenename)];
$sceneprt = [];
foreach ($scene->prts as $prt) {
switch ($prt->scoremode) {
case 'best':
case 'first':
case 'bestn':
$sceneprt[$prt->name] = ['mode' => $prt->scoremode,
'modeparam' => $prt->scoremodeparameters, 'penalty' =>
0, 'value' => $prt->value
];
break;
}
}
foreach ($sequence[$i] as $attempt) {
foreach ($attempt as $prtname => $res) {
if (array_key_exists($prtname, $sceneprt)) {
// If it does not exist it probably is a no score PRT.
// [_PATH,_RESULTS,_SCORE,_PENALTY] is the structure.
$res = stateful_utils::string_to_list($res);
if (count($res) > 3) {
// If length is les than 4 then that PRT did not
// trigger completly.
$sc = floatval($res[2]);
$pen = floatval($res[3]);
if ($penalties) {
$sceneprt[$prtname]['penalty'] = $sceneprt[
$prtname]['penalty'] + $pen;
$sceneprt[$prtname]['penalty'] = min($sceneprt[
$prtname]['penalty'], 1.0);
$sc = max($sc - $sceneprt[$prtname]['penalty'],
0);
}
if (array_key_exists('score', $sceneprt[$prtname])
&& $sc > $sceneprt[$prtname]['score']) {
$sceneprt[$prtname]['score'] = $sc;
} else if (!array_key_exists('score', $sceneprt[
$prtname])) {
$sceneprt[$prtname]['score'] = $sc;
}
}
}
}
// Reset the prt data and transfer to the outter layer as scene
// visitation score.
foreach ($sceneprt as $prtname => $prtscore) {
if (array_key_exists('score', $prtscore)) {
$visit = ['score' => $prtscore['score'], 'mode' =>
$prtscore['mode'], 'modeparam' => $prtscore[
'modeparam'], 'value' => $prtscore[
'value']];
if (!array_key_exists($scene->name . '/|/' . $prtname,
$prts)) {
$prts[$scene->name . '/|/' . $prtname] = [];
}
$prts[$scene->name . '/|/' . $prtname][] = $visit;
// Reset.
$prtscore['penalty'] = 0;
unset($prtscore['score']);
}
}
}
$i++;
}
// Now the visitations to scenes have been collected, calculate
// agregates based on individual rules.
foreach ($prts as $visits) {
$aggregate = -1;
$first = $visits[0];
$ordered = [];
foreach ($visits as $visit) {
$ordered[] = $visit['score'];
}
rsort($ordered, SORT_NUMERIC);
switch ($first['mode']) {
case 'best':
// Just pick the max.
$aggregate = $ordered[0];
break;
case 'bestn':
// Select N largest.
$aggregate = 0;
for ($k = 0; $k < intval($first['modeparam']) && $k < count(
$ordered); $k++) {
$aggregate = $aggregate + $ordered[$k];
}
break;
case 'first':
$aggregate = $first['score'];
break;
}
if ($aggregate > -1) {
// Scale the score by PRT value.
$score = $score + ($aggregate * floatval($first['value']));
}
}
// OK so we need to return this to 0...1
if (floatval($this->defaultmark) == 0.0) {
$score = 0.0;
} else {
$score = $score / floatval($this->defaultmark);
}
return ['total' => $score];
}
// Pretty much the standard way but the $state object has been given with set_state
// and the only thing that we want to store and pass with the step is the seed.
// Key here is that when we start an attempt we need to initialise the state variable
// values and set them to the $state.
public function start_attempt(
question_attempt_step $step,
$variant
) {
$variants = array();
if (!($this->variants === '' || $this->variants === '{}' || $this->variants === null)) {
$variants = json_decode($this->variants, true);
}
if (isset($variants['_set']) && isset($variants[$variants['_set']])) {
$this->seed = (int) $variants[$variants['_set']][$variant % count($variants[$variants['_set']])];
} else {
$this->seed = (int) $variant;
}
$step->set_qt_var('_seed', $this->seed);
$this->sceneinitialised = false;
$this->inputvalidated = false;
$this->prtsprocessed = false;
$this->inputvalidationrendered = false;
// If we are being called by the AJAX-validation, we can ignore these
// the relevant ones are called once it has defiend additinal details.
if (strpos($_SERVER['PHP_SELF'],"lib/ajax/service.php") === false) {
$this->init_state_vars();
$this->init_from_state();
}
}
// Pretty much the standard way but the $state object has been given with set_state
// and the only thing that we want to store and pass with the step is the seed.
public function apply_attempt_state(question_attempt_step $step) {
// We keep the seed in the initial step for other things to see as opposed to
// storing it as a state variable.
$this->seed = (int) $step->get_qt_var('_seed');
$this->sceneinitialised = false;
$this->inputvalidated = false;
$this->prtsprocessed = false;
$this->inputvalidationrendered = false;
// Now we have a problem. For whatever reason some parts of the question
// engine do not call this through the behaviour and thus the behaviour
// cannot provide us the state. Therefore we need to react to that.
if ($this->state->get(self::SCENE_CURRENT, null) == null) {
// echo "<pre>";
// debug_print_backtrace();
// echo "</pre>";
} else {
$this->init_from_state();
}
}
private function init_state_vars() {
// Generates initial values for state variables and saves them to the state object.
// We can use pretty much standard STACK cas sessions here.
// 1. We need the question variables for this seed.
$statements = [];
if (is_numeric($this->seed) || is_integer($this->seed)) {
$statements[] = new stack_secure_loader('RANDOM_SEED:' . $this->seed, 'init_state_vars');
} else {
$statements[] = stack_ast_container_silent::make_from_teacher_source('RANDOM_SEED:' . $this->seed, 'init_state_vars', $this->security);
}
// If we are using localisation we should tell the CAS side logic about it.
// For castext rendering and other tasks.
if (count($this->get_compiled('langs')) > 0) {
$ml = new stack_multilang();
$selected = $ml->pick_lang($this->get_compiled('langs'));
$statements[] = new stack_secure_loader('%_STACK_LANG:' .
stack_utils::php_string_to_maxima_string($selected), 'language setting');
}
// First define the function that evaluates the question variables.
// This comes from stateful_function_builder::question_variables.
$statements[] = $this->get_compiled('qv');
// Then call it.
$statements[] = new stack_secure_loader(
'_question_variables(RANDOM_SEED)', '');
// Now call the initialisations for each variable.
$vars = array();
foreach ($this->variables as $statevar) {
$var = stateful_state_carrier::make_from_teacher_source(
$statevar->name . ':' . $statevar->initialvalue);
$vars[$statevar->name] = $var;
$statements[] = $var;
}
// Validate just in case. All the compiled bits have already been validated.
// So we might skip this. But lets keep this short in this phase of development.
foreach ($statements as $statement) {
if ($statement->get_valid() !== true) {
throw new stateful_exception(stateful_string(
'error_in_state_variable_initialisation'));
}
}
$session = new stack_cas_session2($statements, $this->options, $this->
seed);
$session->errclass = 'stateful_cas_error';
$session->instantiate();
// Check for error.
if ($session->get_errors() !== null && $session->get_errors() !== '') { throw new stateful_exception(stateful_string(
'error_in_state_variable_cas_initialisation', $session->get_errors()));
}
// Read in.
foreach ($this->variables as $statevar) {
$this->state->set($statevar->number, $vars[$statevar->name]->get_evaluated_state());
}
// Also the SCENE ones.
$this->state->set(self::SCENE_PATH, '[]');
$this->state->set(self::SCENE_CURRENT, stack_utils::
php_string_to_maxima_string($this->entryscene));
}
private function init_from_state() {
// Basic initialisation consists of initialisation of the inputs and rendering of the scene text.
// For that we need to evaluate the teachers answers for each input and not much more.
// But only for the active scene.
// NOTE: after a scene transition this needs to be called again to initialise
// the inputs of the new scene.
$scene = $this->get_current_scene();
// Build the input controller.
$cache = $this->get_compiled('scene-' . $scene->name .
'|io-cache');
// Note that we use the cached JSON declarations instead of
// the objects. Also cache updates do not happen here.
$this->inputs = stateful_input_controller::make_from_objects($scene->inputs, $scene->vboxes, $cache);
$this->security = new stack_cas_security($this->has_units(), '', '', $this->get_compiled('forbiddenkeys'));
$statements = [];
if (is_numeric($this->seed) || is_integer($this->seed)) {
$statements[] = new stack_secure_loader('RANDOM_SEED:' . $this->seed, 'init_from_state');
} else {
$statements[] = stack_ast_container_silent::make_from_teacher_source('RANDOM_SEED:' . $this->seed, 'init_from_state', $this->security);
}
// If we are using localisation we should tell the CAS side logic about it.
// For castext rendering and other tasks.
if (count($this->get_compiled('langs')) > 0) {
$ml = new stack_multilang();
$selected = $ml->pick_lang($this->get_compiled('langs'));
$statements[] = new stack_secure_loader('%_STACK_LANG:' .
stack_utils::php_string_to_maxima_string($selected), 'language setting');
}
// 1. We need the question variables for this seed.
$statements[] = $this->get_compiled('qv');
// Then call it.
$statements[] = new stack_secure_loader(
'_question_variables(RANDOM_SEED)', '');
// 2. Add the state into it.
foreach ($this->variables as $statevar) {
$statements[] = stateful_state_carrier::make_from_teacher_source(
$statevar->name . ':' . $this->state->get($statevar->number));
}
$statements[] = stack_ast_container_silent::make_from_teacher_source(
'SCENE_CURRENT:' . $this->state->get(self::SCENE_CURRENT));
$statements[] = stack_ast_container_silent::make_from_teacher_source(
'SCENE_PATH:' . $this->state->get(self::SCENE_PATH));
// 3. We also need the scene variables for this seed and state.
$svfunction = $this->get_compiled('scene-' . $scene->name .
'-variables');
$statements[] = $svfunction;
// And to call it we need its signature.
$statements[] = new stack_secure_loader(explode(':=', $svfunction->
get_evaluationform())[0], '');
// 4. Scene-text. and the model solution.
$scenetext = $this->get_compiled('scene-' . $scene->name . '-text');
$statements[] = $scenetext;
$modelsolution = $this->get_compiled('modelsolution');
$statements[] = $modelsolution;
// 5. Input initialisation. Note that the input controller handles all.
$statements = array_merge($statements, $this->inputs->collect_initialisation_statements());
// Note that as nothing changes the state here we do not output it and
// store it after instanttiation. That happens only when we A) initialise
// state variables, or B) process input.
// Validate just in case. All the compiled bits have already been validated.
// So we might skip this. But lets keep this short in this phase of development.
// TODO: build a non validating version of cassession that can be given such
// already validated things to evaluate, maybe one with better control on what
// is outputted, the old one outputs quite a lot of things that are not used.
foreach ($statements as $statement) {
if ($statement->get_valid() !== true) {
throw new stateful_exception(stateful_string(
'error_in_initialisation'));
}
}
// Instantiate
$session = new stack_cas_session2($statements, $this->options, $this
->seed);
$session->errclass = 'stateful_cas_error';
$session->instantiate();
// Check that error.
if ($session->get_errors() !== null && $session->get_errors() !== '') {
throw new stateful_exception(stateful_string(
'error_in_cas_initialisation', $session->get_errors()));
}
// Give the input initialisation results to the inputs.
// Earlier we gave them to them only at the time of validation
// but as some of the results affect the expected-data it became
// difficult to have the correct expectations for selecting
// what to give to the validation.
$this->inputs->deliver_initialisation_results();
$this->sceneinitialised = true;
$this->lastsceneinited = $scene->name;
// Take in the scene-text.
$this->scenetext = $scenetext;
$this->modelsolution = $modelsolution;
}
// All things should exist in compiled form. But this might be some exotic situation.
// As exotic as the first execution... Also does STACK validation disabling at the same time.
// Public because testing uses this.
public function get_compiled(string $thing) {
global $DB;
// If we have cached stuff but still in JSON form.