2727use qtype_questionpy \local \api \attempt ;
2828use qtype_questionpy \local \api \attempt_ui ;
2929use qtype_questionpy \local \api \package_dependency ;
30+ use qtype_questionpy \local \api \question_data ;
3031use qtype_questionpy \local \api \scoring_code ;
3132use qtype_questionpy \local \attempt_ui \question_ui_metadata_extractor ;
3233use qtype_questionpy \question_bridge_base ;
@@ -46,6 +47,8 @@ class qtype_questionpy_question extends question_graded_automatically_with_count
4647 public string $ packagehash ;
4748 /** @var string */
4849 public string $ questionstate ;
50+ /** @var question_data */
51+ public question_data $ questiondata ;
4952 /** @var stored_file|null */
5053 private ?stored_file $ packagefile ;
5154
@@ -74,14 +77,18 @@ class qtype_questionpy_question extends question_graded_automatically_with_count
7477 *
7578 * @param string $packagehash
7679 * @param string $questionstate
80+ * @param question_data $questiondata
7781 * @param stored_file|null $packagefile
7882 * @param api $api
7983 */
80- 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+ ) {
8187 parent ::__construct ();
8288 $ this ->api = $ api ;
8389 $ this ->packagehash = $ packagehash ;
8490 $ this ->questionstate = $ questionstate ;
91+ $ this ->questiondata = $ questiondata ;
8592 $ this ->packagefile = $ packagefile ;
8693 }
8794
@@ -117,7 +124,9 @@ public function start_attempt(question_attempt_step $step, $variant): void {
117124 global $ PAGE ;
118125
119126 try {
120- $ attempt = $ this ->api ->package ($ this ->packagehash , $ this ->packagefile )->start_attempt ($ this ->questionstate , $ variant );
127+ $ attributes = $ this ->get_requested_attributes ();
128+ $ attempt = $ this ->api ->package ($ this ->packagehash , $ this ->packagefile )
129+ ->start_attempt ($ this ->questionstate , $ variant , $ attributes );
121130
122131 $ this ->attemptstate = $ attempt ->attemptstate ;
123132 $ step ->set_qt_var (constants::QT_VAR_ATTEMPT_STATE , $ attempt ->attemptstate );
@@ -180,9 +189,11 @@ public function apply_attempt_state(question_attempt_step $step) {
180189 /* TODO: This method is also called from question_attempt->regrade and
181190 question_attempt->start_question_based_on, where we shouldn't need to get the UI. */
182191 try {
192+ $ attributes = $ this ->get_requested_attributes ();
183193 $ attempt = $ this ->api ->package ($ this ->packagehash , $ this ->packagefile )
184194 ->view_attempt (
185195 $ this ->questionstate ,
196+ $ attributes ,
186197 $ this ->attemptstate ,
187198 $ this ->scoringstate ,
188199 $ lastresponse
@@ -419,11 +430,15 @@ public function make_behaviour(question_attempt $qa, $preferredbehaviour): quest
419430 /**
420431 * Get the QuestionPy bridge used to retrieve additional information about an attempt.
421432 *
422- * @return question_bridge_base
433+ * @throws moodle_exception
434+ * @return question_bridge_base|null
423435 */
424- public function get_bridge (): question_bridge_base {
436+ public function get_bridge (): ? question_bridge_base {
425437 if ($ this ->bridge === null ) {
426- $ 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+ }
427442 }
428443 return $ this ->bridge ;
429444 }
@@ -440,4 +455,15 @@ public function get_bridge(): question_bridge_base {
440455 public function set_bridge (question_bridge_base $ bridge ): void {
441456 $ this ->bridge = $ bridge ;
442457 }
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+ }
443469}
0 commit comments