Skip to content

Commit 4c98c5d

Browse files
committed
feat: apply suggested changes
1 parent da1ff9b commit 4c98c5d

File tree

6 files changed

+42
-36
lines changed

6 files changed

+42
-36
lines changed

classes/local/api/question_data.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,6 @@ public static function from_question_response(question_response $response): self
9898
);
9999
}
100100

101-
/**
102-
* Creates {@see question_data} from the question id.
103-
*
104-
* @param int $id
105-
* @return ?self
106-
* @throws moodle_exception
107-
*/
108-
public static function from_question_id(int $id): ?self {
109-
global $DB;
110-
111-
$field = $DB->get_field('qtype_questionpy', 'questiondata', ['questionid' => $id]);
112-
if ($field === false) {
113-
return null;
114-
}
115-
116-
return self::from_json($field);
117-
}
118-
119101
/**
120102
* Creates {@see question_data} from JSON.
121103
*

classes/question_service.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ public function get_question(int $questionid): object {
9090
}
9191
$result->qpy_id = $record->id;
9292
$result->qpy_package_hash = $record->pkgversionhash;
93-
$result->qpy_state = $record->state;
9493
$result->qpy_is_local = $record->islocal;
94+
$result->qpy_state = $record->state;
95+
$result->qpy_question_data = $record->questiondata;
9596
return $result;
9697
}
9798

question.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class qtype_questionpy_question extends question_graded_automatically_with_count
4747
public string $packagehash;
4848
/** @var string */
4949
public string $questionstate;
50+
/** @var question_data */
51+
public question_data $questiondata;
5052
/** @var stored_file|null */
5153
private ?stored_file $packagefile;
5254

@@ -75,14 +77,18 @@ class qtype_questionpy_question extends question_graded_automatically_with_count
7577
*
7678
* @param string $packagehash
7779
* @param string $questionstate
80+
* @param question_data $questiondata
7881
* @param stored_file|null $packagefile
7982
* @param api $api
8083
*/
81-
public function __construct(string $packagehash, string $questionstate, ?stored_file $packagefile, api $api) {
84+
public function __construct(
85+
string $packagehash, string $questionstate, question_data $questiondata, ?stored_file $packagefile, api $api
86+
) {
8287
parent::__construct();
8388
$this->api = $api;
8489
$this->packagehash = $packagehash;
8590
$this->questionstate = $questionstate;
91+
$this->questiondata = $questiondata;
8692
$this->packagefile = $packagefile;
8793
}
8894

@@ -118,8 +124,7 @@ public function start_attempt(question_attempt_step $step, $variant): void {
118124
global $PAGE;
119125

120126
try {
121-
// Unfortunately, we cannot access attempt data, as the attempt is not stored in the database at this point of time.
122-
$attributes = null;
127+
$attributes = $this->get_requested_attributes();
123128
$attempt = $this->api->package($this->packagehash, $this->packagefile)
124129
->start_attempt($this->questionstate, $variant, $attributes);
125130

@@ -184,18 +189,11 @@ public function apply_attempt_state(question_attempt_step $step) {
184189
/* TODO: This method is also called from question_attempt->regrade and
185190
question_attempt->start_question_based_on, where we shouldn't need to get the UI. */
186191
try {
187-
if ($this->id !== null) {
188-
$questiondata = question_data::from_question_id($this->id);
189-
$requestedattributes = $questiondata->permissions?->attributes;
190-
if ($requestedattributes) {
191-
$attributes = $this->get_bridge()->get_attributes($requestedattributes);
192-
}
193-
}
194-
192+
$attributes = $this->get_requested_attributes();
195193
$attempt = $this->api->package($this->packagehash, $this->packagefile)
196194
->view_attempt(
197195
$this->questionstate,
198-
$attributes ?? null,
196+
$attributes,
199197
$this->attemptstate,
200198
$this->scoringstate,
201199
$lastresponse
@@ -433,11 +431,14 @@ public function make_behaviour(question_attempt $qa, $preferredbehaviour): quest
433431
* Get the QuestionPy bridge used to retrieve additional information about an attempt.
434432
*
435433
* @throws moodle_exception
436-
* @return question_bridge_base
434+
* @return question_bridge_base|null
437435
*/
438-
public function get_bridge(): question_bridge_base {
436+
public function get_bridge(): ?question_bridge_base {
439437
if ($this->bridge === null) {
440-
$this->bridge = question_bridge_base::create($this->get_behaviour()->get_qa());
438+
$attempt = $this->get_behaviour()->get_qa();
439+
if ($attempt->get_database_id() !== null && is_numeric($attempt->get_usage_id())) {
440+
$this->bridge = question_bridge_base::create($attempt);
441+
}
441442
}
442443
return $this->bridge;
443444
}
@@ -454,4 +455,15 @@ public function get_bridge(): question_bridge_base {
454455
public function set_bridge(question_bridge_base $bridge): void {
455456
$this->bridge = $bridge;
456457
}
458+
459+
/**
460+
* Retrieves the requested attributes if any.
461+
*
462+
* @return array|null
463+
* @throws moodle_exception
464+
*/
465+
private function get_requested_attributes(): ?array {
466+
$attributes = $this->questiondata->permissions?->attributes;
467+
return $attributes ? $this->get_bridge()?->get_attributes($attributes) : null;
468+
}
457469
}

questiontype.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
use core\di;
2626
use qtype_questionpy\local\api\api;
27+
use qtype_questionpy\local\api\question_data;
2728
use qtype_questionpy\package_file_service;
2829
use qtype_questionpy\question_service;
2930

@@ -186,8 +187,9 @@ protected function make_question_instance($questiondata) {
186187
return new qtype_questionpy_question(
187188
$questiondata->qpy_package_hash,
188189
$questiondata->qpy_state,
190+
question_data::from_json($questiondata->qpy_question_data),
189191
$packagefile,
190-
$this->api
192+
$this->api,
191193
);
192194
}
193195
}

tests/local/attempt_ui/question_ui_renderer_test.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use PHPUnit\Framework\MockObject\Stub;
2727
use qtype_questionpy\constants;
2828
use qtype_questionpy\local\api\api;
29+
use qtype_questionpy\local\api\question_data;
2930
use qtype_questionpy_question;
3031
use question_attempt;
3132
use question_attempt_step;
@@ -508,7 +509,13 @@ private function create_question_attempt_stub(?string $packagehash = null, ?int
508509
array $lastresponse = []): question_attempt {
509510
$packagehash ??= hash('sha256', random_string(64));
510511
$id ??= mt_rand();
511-
$question = new qtype_questionpy_question($packagehash, '{}', null, $this->createStub(api::class));
512+
$question = new qtype_questionpy_question(
513+
$packagehash,
514+
'{}',
515+
$this->createStub(question_data::class),
516+
null,
517+
$this->createStub(api::class),
518+
);
512519

513520
$step = new question_attempt_step([constants::QT_VAR_RESPONSE => json_encode((object) $lastresponse)]);
514521

tests/question_test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use qtype_questionpy\event\viewing_attempt_failed;
2525
use qtype_questionpy\local\api\api;
2626
use qtype_questionpy\local\api\package_api;
27+
use qtype_questionpy\local\api\question_data;
2728
use qtype_questionpy\local\attempt_ui\question_ui_metadata_extractor;
2829
use qtype_questionpy_question;
2930
use question_bank;
@@ -79,6 +80,7 @@ private function create_question(): qtype_questionpy_question {
7980
$question = new qtype_questionpy_question(
8081
hash('sha256', 'hash'),
8182
'state',
83+
$this->createStub(question_data::class),
8284
packagefile: null,
8385
api: $this->api
8486
);

0 commit comments

Comments
 (0)