Skip to content

Commit da1ff9b

Browse files
committed
feat: send requested lms attributes
1 parent e532c42 commit da1ff9b

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

classes/local/api/package_api.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,19 @@ public function create_question(?string $currentstate, object $formdata): questi
106106
*
107107
* @param string $questionstate
108108
* @param int $variant variant which should be started (`1` for questions with only one variant)
109+
* @param array|null $attributes
109110
* @return attempt_started the attempt's state and metadata. Note that the attempt state never changes after the
110111
* attempt has been started.
111112
* @throws GuzzleException
112113
* @throws request_error
113114
* @throws moodle_exception
114115
*/
115-
public function start_attempt(string $questionstate, int $variant): attempt_started {
116+
public function start_attempt(string $questionstate, int $variant, ?array $attributes): attempt_started {
116117
$options['multipart'] = $this->transform_to_multipart(
117118
[
118119
'variant' => $variant,
119120
'context' => $this->get_context_id(),
121+
'lms_provided_attributes' => $attributes,
120122
],
121123
$questionstate,
122124
);
@@ -128,6 +130,7 @@ public function start_attempt(string $questionstate, int $variant): attempt_star
128130
* View a previously created attempt.
129131
*
130132
* @param string $questionstate
133+
* @param array|null $attributes
131134
* @param string $attemptstate the attempt state previously returned from {@see start_attempt()}
132135
* @param string|null $scoringstate the last scoring state if this attempt has already been scored
133136
* @param object|null $response data currently entered by the student
@@ -136,14 +139,15 @@ public function start_attempt(string $questionstate, int $variant): attempt_star
136139
* @throws request_error
137140
* @throws moodle_exception
138141
*/
139-
public function view_attempt(string $questionstate, string $attemptstate, ?string $scoringstate = null,
142+
public function view_attempt(string $questionstate, ?array $attributes, string $attemptstate, ?string $scoringstate = null,
140143
?object $response = null): attempt {
141144
$options['multipart'] = $this->transform_to_multipart(
142145
[
143146
'attempt_state' => $attemptstate,
144147
'scoring_state' => $scoringstate,
145148
'response' => $response,
146149
'context' => $this->get_context_id(),
150+
'lms_provided_attributes' => $attributes,
147151
],
148152
$questionstate,
149153
);

classes/local/api/question_data.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ 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+
101119
/**
102120
* Creates {@see question_data} from JSON.
103121
*

question.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use qtype_questionpy\local\api\attempt;
2828
use qtype_questionpy\local\api\attempt_ui;
2929
use qtype_questionpy\local\api\package_dependency;
30+
use qtype_questionpy\local\api\question_data;
3031
use qtype_questionpy\local\api\scoring_code;
3132
use qtype_questionpy\local\attempt_ui\question_ui_metadata_extractor;
3233
use qtype_questionpy\question_bridge_base;
@@ -117,7 +118,10 @@ public function start_attempt(question_attempt_step $step, $variant): void {
117118
global $PAGE;
118119

119120
try {
120-
$attempt = $this->api->package($this->packagehash, $this->packagefile)->start_attempt($this->questionstate, $variant);
121+
// Unfortunately, we cannot access attempt data, as the attempt is not stored in the database at this point of time.
122+
$attributes = null;
123+
$attempt = $this->api->package($this->packagehash, $this->packagefile)
124+
->start_attempt($this->questionstate, $variant, $attributes);
121125

122126
$this->attemptstate = $attempt->attemptstate;
123127
$step->set_qt_var(constants::QT_VAR_ATTEMPT_STATE, $attempt->attemptstate);
@@ -180,9 +184,18 @@ public function apply_attempt_state(question_attempt_step $step) {
180184
/* TODO: This method is also called from question_attempt->regrade and
181185
question_attempt->start_question_based_on, where we shouldn't need to get the UI. */
182186
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+
183195
$attempt = $this->api->package($this->packagehash, $this->packagefile)
184196
->view_attempt(
185197
$this->questionstate,
198+
$attributes ?? null,
186199
$this->attemptstate,
187200
$this->scoringstate,
188201
$lastresponse
@@ -419,6 +432,7 @@ public function make_behaviour(question_attempt $qa, $preferredbehaviour): quest
419432
/**
420433
* Get the QuestionPy bridge used to retrieve additional information about an attempt.
421434
*
435+
* @throws moodle_exception
422436
* @return question_bridge_base
423437
*/
424438
public function get_bridge(): question_bridge_base {

0 commit comments

Comments
 (0)