Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file defines the question attempt step class, and a few related classes.
  19   *
  20   * @package    moodlecore
  21   * @subpackage questionengine
  22   * @copyright  2009 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * Stores one step in a {@see question_attempt}.
  32   *
  33   * The most important attributes of a step are the state, which is one of the
  34   * {@see question_state} constants, the fraction, which may be null, or a
  35   * number bewteen the attempt's minfraction and maxfraction, and the array of submitted
  36   * data, about which more later.
  37   *
  38   * A step also tracks the time it was created, and the user responsible for
  39   * creating it.
  40   *
  41   * The submitted data is basically just an array of name => value pairs, with
  42   * certain conventions about the to divide the variables into five = 2 x 2 + 1 categories.
  43   *
  44   * Variables may either belong to the behaviour, in which case the
  45   * name starts with a '-', or they may belong to the question type in which case
  46   * they name does not start with a '-'.
  47   *
  48   * Second, variables may either be ones that came form the original request, in
  49   * which case the name does not start with an _, or they are cached values that
  50   * were created during processing, in which case the name does start with an _.
  51   *
  52   * In addition, we can store 'metadata', typically only in the first step of a
  53   * question attempt. These are stored with the initial characters ':_'.
  54   *
  55   * That is, each name will start with one of '', '_', '-', '-_' or ':_'. The remainder
  56   * of the name was supposed to match the regex [a-z][a-z0-9]* - but this has never
  57   * been enforced. Question types exist which break this rule. E.g. qtype_combined.
  58   * Perhpas now, an accurate regex would be [a-z][a-z0-9_:]*.
  59   *
  60   * These variables can be accessed with {@see get_behaviour_var()} and {@see get_qt_var()},
  61   * - to be clear, ->get_behaviour_var('x') gets the variable with name '-x' -
  62   * and values whose names start with '_' can be set using {@see set_behaviour_var()}
  63   * and {@see set_qt_var()}. There are some other methods like {@see has_behaviour_var()}
  64   * to check wether a varaible with a particular name is set, and {@see get_behaviour_data()}
  65   * to get all the behaviour data as an associative array. There are also
  66   * {@see get_metadata_var()}, {@see set_metadata_var()} and {@see has_metadata_var()},
  67   *
  68   * @copyright  2009 The Open University
  69   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  70   */
  71  class question_attempt_step {
  72      /**
  73       * @var integer if this attempts is stored in the question_attempts table,
  74       * the id of that row.
  75       */
  76      private $id = null;
  77  
  78      /**
  79       * @var question_state one of the {@see question_state} constants.
  80       * The state after this step.
  81       */
  82      private $state;
  83  
  84      /**
  85       * @var null|number the fraction (grade on a scale of
  86       * minfraction .. maxfraction, normally 0..1) or null.
  87       */
  88      private $fraction = null;
  89  
  90      /** @var integer the timestamp when this step was created. */
  91      private $timecreated;
  92  
  93      /** @var integer the id of the user resonsible for creating this step. */
  94      private $userid;
  95  
  96      /** @var array name => value pairs. The submitted data. */
  97      private $data;
  98  
  99      /** @var array name => array of {@see stored_file}s. Caches the contents of file areas. */
 100      private $files = array();
 101  
 102      /** @var stdClass User information. */
 103      private $user = null;
 104  
 105      /**
 106       * You should not need to call this constructor in your own code. Steps are
 107       * normally created by {@see question_attempt} methods like
 108       * {@see question_attempt::process_action()}.
 109       * @param array $data the submitted data that defines this step.
 110       * @param int $timestamp the time to record for the action. (If not given, use now.)
 111       * @param int $userid the user to attribute the aciton to. (If not given, use the current user.)
 112       * @param int $existingstepid if this step is going to replace an existing step
 113       *      (for example, during a regrade) this is the id of the previous step we are replacing.
 114       */
 115      public function __construct($data = array(), $timecreated = null, $userid = null,
 116              $existingstepid = null) {
 117          global $USER;
 118  
 119          if (!is_array($data)) {
 120              throw new coding_exception('$data must be an array when constructing a question_attempt_step.');
 121          }
 122          $this->state = question_state::$unprocessed;
 123          $this->data = $data;
 124          if (is_null($timecreated)) {
 125              $this->timecreated = time();
 126          } else {
 127              $this->timecreated = $timecreated;
 128          }
 129          if (is_null($userid)) {
 130              $this->userid = $USER->id;
 131          } else {
 132              $this->userid = $userid;
 133          }
 134  
 135          if (!is_null($existingstepid)) {
 136              $this->id = $existingstepid;
 137          }
 138      }
 139  
 140      /**
 141       * @return int|null The id of this step in the database. null if this step
 142       * is not stored in the database.
 143       */
 144      public function get_id() {
 145          return $this->id;
 146      }
 147  
 148      /** @return question_state The state after this step. */
 149      public function get_state() {
 150          return $this->state;
 151      }
 152  
 153      /**
 154       * Set the state. Normally only called by behaviours.
 155       * @param question_state $state one of the {@see question_state} constants.
 156       */
 157      public function set_state($state) {
 158          $this->state = $state;
 159      }
 160  
 161      /**
 162       * @return null|number the fraction (grade on a scale of
 163       * minfraction .. maxfraction, normally 0..1),
 164       * or null if this step has not been marked.
 165       */
 166      public function get_fraction() {
 167          return $this->fraction;
 168      }
 169  
 170      /**
 171       * Set the fraction. Normally only called by behaviours.
 172       * @param null|number $fraction the fraction to set.
 173       */
 174      public function set_fraction($fraction) {
 175          $this->fraction = $fraction;
 176      }
 177  
 178      /** @return int the id of the user resonsible for creating this step. */
 179      public function get_user_id() {
 180          return $this->userid;
 181      }
 182  
 183      /**
 184       * Update full user information for step.
 185       *
 186       * @param stdClass $user Full user object.
 187       * @throws coding_exception
 188       */
 189      public function add_full_user_object(stdClass $user): void {
 190          if ($user->id != $this->userid) {
 191              throw new coding_exception('Wrong user passed to add_full_user_object');
 192          }
 193          $this->user = $user;
 194      }
 195  
 196      /**
 197       * Return the full user object.
 198       *
 199       * @return stdClass Get full user object.
 200       */
 201      public function get_user(): stdClass {
 202          return $this->user;
 203      }
 204  
 205      /**
 206       * Get full name of user who did action.
 207       *
 208       * @return string full name of user.
 209       */
 210      public function get_user_fullname(): string {
 211          return fullname($this->user);
 212      }
 213  
 214      /** @return int the timestamp when this step was created. */
 215      public function get_timecreated() {
 216          return $this->timecreated;
 217      }
 218  
 219      /**
 220       * @param string $name the name of a question type variable to look for in the submitted data.
 221       * @return bool whether a variable with this name exists in the question type data.
 222       */
 223      public function has_qt_var($name) {
 224          return array_key_exists($name, $this->data);
 225      }
 226  
 227      /**
 228       * @param string $name the name of a question type variable to look for in the submitted data.
 229       * @return string the requested variable, or null if the variable is not set.
 230       */
 231      public function get_qt_var($name) {
 232          if (!$this->has_qt_var($name)) {
 233              return null;
 234          }
 235          return $this->data[$name];
 236      }
 237  
 238      /**
 239       * Set a cached question type variable.
 240       * @param string $name the name of the variable to set. Must match _[a-z][a-z0-9]*.
 241       * @param string $value the value to set.
 242       */
 243      public function set_qt_var($name, $value) {
 244          if ($name[0] != '_') {
 245              throw new coding_exception('Cannot set question type data ' . $name .
 246                      ' on an attempt step. You can only set variables with names begining with _.');
 247          }
 248          $this->data[$name] = $value;
 249      }
 250  
 251      /**
 252       * Get the latest set of files for a particular question type variable of
 253       * type question_attempt::PARAM_FILES.
 254       *
 255       * @param string $name the name of the associated variable.
 256       * @param int $contextid contextid of the question attempt
 257       * @return array of {@see stored_files}.
 258       */
 259      public function get_qt_files($name, $contextid) {
 260          if (array_key_exists($name, $this->files)) {
 261              return $this->files[$name];
 262          }
 263  
 264          if (!$this->has_qt_var($name)) {
 265              $this->files[$name] = array();
 266              return array();
 267          }
 268  
 269          $fs = get_file_storage();
 270          $this->files[$name] = $fs->get_area_files($contextid, 'question',
 271                  'response_' . $name, $this->id, 'sortorder', false);
 272  
 273          return $this->files[$name];
 274      }
 275  
 276      /**
 277       * Prepare a draft file are for the files belonging the a response variable
 278       * of this step.
 279       *
 280       * @param string $name the variable name the files belong to.
 281       * @param int $contextid the id of the context the quba belongs to.
 282       * @return int the draft itemid.
 283       */
 284      public function prepare_response_files_draft_itemid($name, $contextid) {
 285          list($draftid, $notused) = $this->prepare_response_files_draft_itemid_with_text(
 286                  $name, $contextid, null);
 287          return $draftid;
 288      }
 289  
 290      /**
 291       * Prepare a draft file are for the files belonging the a response variable
 292       * of this step, while rewriting the URLs in some text.
 293       *
 294       * @param string $name the variable name the files belong to.
 295       * @param int $contextid the id of the context the quba belongs to.
 296       * @param string $text the text to update the URLs in.
 297       * @return array(int, string) the draft itemid and the text with URLs rewritten.
 298       */
 299      public function prepare_response_files_draft_itemid_with_text($name, $contextid, $text) {
 300          $draftid = 0; // Will be filled in by file_prepare_draft_area.
 301          $newtext = file_prepare_draft_area($draftid, $contextid, 'question',
 302                  'response_' . $name, $this->id, null, $text);
 303          return array($draftid, $newtext);
 304      }
 305  
 306      /**
 307       * Rewrite the @@PLUGINFILE@@ tokens in a response variable from this step
 308       * that contains links to file. Normally you should probably call
 309       * {@see question_attempt::rewrite_response_pluginfile_urls()} instead of
 310       * calling this method directly.
 311       *
 312       * @param string $text the text to update the URLs in.
 313       * @param int $contextid the id of the context the quba belongs to.
 314       * @param string $name the variable name the files belong to.
 315       * @param array $extra extra file path components.
 316       * @return string the rewritten text.
 317       */
 318      public function rewrite_response_pluginfile_urls($text, $contextid, $name, $extras) {
 319          return question_rewrite_question_urls($text, 'pluginfile.php', $contextid,
 320                  'question', 'response_' . $name, $extras, $this->id);
 321      }
 322  
 323      /**
 324       * Get all the question type variables.
 325       * @param array name => value pairs.
 326       */
 327      public function get_qt_data() {
 328          $result = array();
 329          foreach ($this->data as $name => $value) {
 330              if ($name[0] != '-' && $name[0] != ':') {
 331                  $result[$name] = $value;
 332              }
 333          }
 334          return $result;
 335      }
 336  
 337      /**
 338       * @param string $name the name of a behaviour variable to look for in the submitted data.
 339       * @return bool whether a variable with this name exists in the question type data.
 340       */
 341      public function has_behaviour_var($name) {
 342          return array_key_exists('-' . $name, $this->data);
 343      }
 344  
 345      /**
 346       * @param string $name the name of a behaviour variable to look for in the submitted data.
 347       * @return string the requested variable, or null if the variable is not set.
 348       */
 349      public function get_behaviour_var($name) {
 350          if (!$this->has_behaviour_var($name)) {
 351              return null;
 352          }
 353          return $this->data['-' . $name];
 354      }
 355  
 356      /**
 357       * Set a cached behaviour variable.
 358       * @param string $name the name of the variable to set. Must match _[a-z][a-z0-9]*.
 359       * @param string $value the value to set.
 360       */
 361      public function set_behaviour_var($name, $value) {
 362          if ($name[0] != '_') {
 363              throw new coding_exception('Cannot set question type data ' . $name .
 364                      ' on an attempt step. You can only set variables with names begining with _.');
 365          }
 366          return $this->data['-' . $name] = $value;
 367      }
 368  
 369      /**
 370       * Get all the behaviour variables.
 371       *
 372       * @return array name => value pairs. NOTE! the name has the leading - stripped off.
 373       *      (If you don't understand the note, read the comment at the top of this class :-))
 374       */
 375      public function get_behaviour_data() {
 376          $result = array();
 377          foreach ($this->data as $name => $value) {
 378              if ($name[0] == '-') {
 379                  $result[substr($name, 1)] = $value;
 380              }
 381          }
 382          return $result;
 383      }
 384  
 385      /**
 386       * Get all the submitted data, but not the cached data. behaviour
 387       * variables have the - at the start of their name. This is only really
 388       * intended for use by {@see question_attempt::regrade()}, it should not
 389       * be considered part of the public API.
 390       * @param array name => value pairs.
 391       */
 392      public function get_submitted_data() {
 393          $result = array();
 394          foreach ($this->data as $name => $value) {
 395              if ($name[0] == '_' || ($name[0] == '-' && $name[1] == '_')) {
 396                  continue;
 397              }
 398              $result[$name] = $value;
 399          }
 400          return $result;
 401      }
 402  
 403      /**
 404       * Get all the data. behaviour variables have the - at the start of
 405       * their name. This is only intended for internal use, for example by
 406       * {@see question_engine_data_mapper::insert_question_attempt_step()},
 407       * however, it can occasionally be useful in test code. It should not be
 408       * considered part of the public API of this class.
 409       * @param array name => value pairs.
 410       */
 411      public function get_all_data() {
 412          return $this->data;
 413      }
 414  
 415      /**
 416       * Set a metadata variable.
 417       *
 418       * Do not call this method directly from  your code. It is for internal
 419       * use only. You should call {@see question_usage::set_question_attempt_metadata()}.
 420       *
 421       * @param string $name the name of the variable to set. [a-z][a-z0-9]*.
 422       * @param string $value the value to set.
 423       */
 424      public function set_metadata_var($name, $value) {
 425          $this->data[':_' . $name] = $value;
 426      }
 427  
 428      /**
 429       * Whether this step has a metadata variable.
 430       *
 431       * Do not call this method directly from  your code. It is for internal
 432       * use only. You should call {@see question_usage::get_question_attempt_metadata()}.
 433       *
 434       * @param string $name the name of the variable to set. [a-z][a-z0-9]*.
 435       * @return bool the value to set previously, or null if this variable was never set.
 436       */
 437      public function has_metadata_var($name) {
 438          return isset($this->data[':_' . $name]);
 439      }
 440  
 441      /**
 442       * Get a metadata variable.
 443       *
 444       * Do not call this method directly from  your code. It is for internal
 445       * use only. You should call {@see question_usage::get_question_attempt_metadata()}.
 446       *
 447       * @param string $name the name of the variable to set. [a-z][a-z0-9]*.
 448       * @return string the value to set previously, or null if this variable was never set.
 449       */
 450      public function get_metadata_var($name) {
 451          if (!$this->has_metadata_var($name)) {
 452              return null;
 453          }
 454          return $this->data[':_' . $name];
 455      }
 456  
 457      /**
 458       * Create a question_attempt_step from records loaded from the database.
 459       * @param Iterator $records Raw records loaded from the database.
 460       * @param int $stepid The id of the records to extract.
 461       * @param string $qtype The question type of which this is an attempt.
 462       *      If not given, each record must include a qtype field.
 463       * @return question_attempt_step The newly constructed question_attempt_step.
 464       */
 465      public static function load_from_records($records, $attemptstepid, $qtype = null) {
 466          $currentrec = $records->current();
 467          while ($currentrec->attemptstepid != $attemptstepid) {
 468              $records->next();
 469              if (!$records->valid()) {
 470                  throw new coding_exception('Question attempt step ' . $attemptstepid .
 471                          ' not found in the database.');
 472              }
 473              $currentrec = $records->current();
 474          }
 475  
 476          $record = $currentrec;
 477          $contextid = null;
 478          $data = array();
 479          while ($currentrec && $currentrec->attemptstepid == $attemptstepid) {
 480              if (!is_null($currentrec->name)) {
 481                  $data[$currentrec->name] = $currentrec->value;
 482              }
 483              $records->next();
 484              if ($records->valid()) {
 485                  $currentrec = $records->current();
 486              } else {
 487                  $currentrec = false;
 488              }
 489          }
 490  
 491          $step = new question_attempt_step_read_only($data, $record->timecreated, $record->userid);
 492          $step->state = question_state::get($record->state);
 493          $step->id = $record->attemptstepid;
 494          if (!is_null($record->fraction)) {
 495              $step->fraction = $record->fraction + 0;
 496          }
 497  
 498          // This next chunk of code requires getting $contextid and $qtype here.
 499          // Somehow, we need to get that information to this point by modifying
 500          // all the paths by which this method can be called.
 501          // Can we only return files when it's possible? Should there be some kind of warning?
 502          if (is_null($qtype)) {
 503              $qtype = $record->qtype;
 504          }
 505          foreach (question_bank::get_qtype($qtype)->response_file_areas() as $area) {
 506              if (empty($step->data[$area])) {
 507                  continue;
 508              }
 509  
 510              $step->data[$area] = new question_file_loader($step, $area, $step->data[$area], $record->contextid);
 511          }
 512  
 513          return $step;
 514      }
 515  }
 516  
 517  
 518  /**
 519   * A subclass of {@see question_attempt_step} used when processing a new submission.
 520   *
 521   * When we are processing some new submitted data, which may or may not lead to
 522   * a new step being added to the {@see question_usage_by_activity} we create an
 523   * instance of this class. which is then passed to the question behaviour and question
 524   * type for processing. At the end of processing we then may, or may not, keep it.
 525   *
 526   * @copyright  2010 The Open University
 527   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 528   */
 529  class question_attempt_pending_step extends question_attempt_step {
 530      /** @var string the new response summary, if there is one. */
 531      protected $newresponsesummary = null;
 532  
 533      /** @var int the new variant number, if there is one. */
 534      protected $newvariant = null;
 535  
 536      /**
 537       * If as a result of processing this step, the response summary for the
 538       * question attempt should changed, you should call this method to set the
 539       * new summary.
 540       * @param string $responsesummary the new response summary.
 541       */
 542      public function set_new_response_summary($responsesummary) {
 543          $this->newresponsesummary = $responsesummary;
 544      }
 545  
 546      /**
 547       * Get the new response summary, if there is one.
 548       * @return string the new response summary, or null if it has not changed.
 549       */
 550      public function get_new_response_summary() {
 551          return $this->newresponsesummary;
 552      }
 553  
 554      /**
 555       * Whether this processing this step has changed the response summary.
 556       * @return bool true if there is a new response summary.
 557       */
 558      public function response_summary_changed() {
 559          return !is_null($this->newresponsesummary);
 560      }
 561  
 562      /**
 563       * If as a result of processing this step, you identify that this variant of the
 564       * question is actually identical to the another one, you may change the
 565       * variant number recorded, in order to give better statistics. For an example
 566       * see qbehaviour_opaque.
 567       * @param int $variant the new variant number.
 568       */
 569      public function set_new_variant_number($variant) {
 570          $this->newvariant = $variant;
 571      }
 572  
 573      /**
 574       * Get the new variant number, if there is one.
 575       * @return int the new variant number, or null if it has not changed.
 576       */
 577      public function get_new_variant_number() {
 578          return $this->newvariant;
 579      }
 580  
 581      /**
 582       * Whether this processing this step has changed the variant number.
 583       * @return bool true if there is a new variant number.
 584       */
 585      public function variant_number_changed() {
 586          return !is_null($this->newvariant);
 587      }
 588  }
 589  
 590  
 591  /**
 592   * A subclass of {@see question_attempt_step} that cannot be modified.
 593   *
 594   * @copyright  2009 The Open University
 595   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 596   */
 597  class question_attempt_step_read_only extends question_attempt_step {
 598      public function set_state($state) {
 599          throw new coding_exception('Cannot modify a question_attempt_step_read_only.');
 600      }
 601      public function set_fraction($fraction) {
 602          throw new coding_exception('Cannot modify a question_attempt_step_read_only.');
 603      }
 604      public function set_qt_var($name, $value) {
 605          throw new coding_exception('Cannot modify a question_attempt_step_read_only.');
 606      }
 607      public function set_behaviour_var($name, $value) {
 608          throw new coding_exception('Cannot modify a question_attempt_step_read_only.');
 609      }
 610  }
 611  
 612  
 613  /**
 614   * A null {@see question_attempt_step} returned from
 615   * {@see question_attempt::get_last_step()} etc. when a an attempt has just been
 616   * created and there is no actual step.
 617   *
 618   * @copyright  2009 The Open University
 619   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 620   */
 621  class question_null_step {
 622      public function get_state() {
 623          return question_state::$notstarted;
 624      }
 625  
 626      public function set_state($state) {
 627          throw new coding_exception('This question has not been started.');
 628      }
 629  
 630      public function get_fraction() {
 631          return null;
 632      }
 633  }
 634  
 635  
 636  /**
 637   * This is an adapter class that wraps a {@see question_attempt_step} and
 638   * modifies the get/set_*_data methods so that they operate only on the parts
 639   * that belong to a particular subquestion, as indicated by an extra prefix.
 640   *
 641   * @copyright  2010 The Open University
 642   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 643   */
 644  class question_attempt_step_subquestion_adapter extends question_attempt_step {
 645      /** @var question_attempt_step the step we are wrapping. */
 646      protected $realstep;
 647      /** @var string the exta prefix on fields we work with. */
 648      protected $extraprefix;
 649  
 650      /**
 651       * Constructor.
 652       * @param question_attempt_step $realqas the step to wrap. (Can be null if you
 653       *      just want to call add/remove.prefix.)
 654       * @param string $extraprefix the extra prefix that is used for date fields.
 655       */
 656      public function __construct($realqas, $extraprefix) {
 657          $this->realqas = $realqas;
 658          $this->extraprefix = $extraprefix;
 659      }
 660  
 661      /**
 662       * Add the extra prefix to a field name.
 663       * @param string $field the plain field name.
 664       * @return string the field name with the extra bit of prefix added.
 665       */
 666      public function add_prefix($field) {
 667          if (substr($field, 0, 2) === '-_') {
 668              return '-_' . $this->extraprefix . substr($field, 2);
 669          } else if (substr($field, 0, 1) === '-') {
 670              return '-' . $this->extraprefix . substr($field, 1);
 671          } else if (substr($field, 0, 1) === '_') {
 672              return '_' . $this->extraprefix . substr($field, 1);
 673          } else {
 674              return $this->extraprefix . $field;
 675          }
 676      }
 677  
 678      /**
 679       * Remove the extra prefix from a field name if it is present.
 680       * @param string $field the extended field name.
 681       * @return string the field name with the extra bit of prefix removed, or
 682       * null if the extre prefix was not present.
 683       */
 684      public function remove_prefix($field) {
 685          if (preg_match('~^(-?_?)' . preg_quote($this->extraprefix, '~') . '(.*)$~', $field, $matches)) {
 686              return $matches[1] . $matches[2];
 687          } else {
 688              return null;
 689          }
 690      }
 691  
 692      /**
 693       * Filter some data to keep only those entries where the key contains
 694       * extraprefix, and remove the extra prefix from the reutrned arrary.
 695       * @param array $data some of the data stored in this step.
 696       * @return array the data with the keys ajusted using {@see remove_prefix()}.
 697       */
 698      public function filter_array($data) {
 699          $result = array();
 700          foreach ($data as $fullname => $value) {
 701              if ($name = $this->remove_prefix($fullname)) {
 702                  $result[$name] = $value;
 703              }
 704          }
 705          return $result;
 706      }
 707  
 708      public function get_state() {
 709          return $this->realqas->get_state();
 710      }
 711  
 712      public function set_state($state) {
 713          throw new coding_exception('Cannot modify a question_attempt_step_subquestion_adapter.');
 714      }
 715  
 716      public function get_fraction() {
 717          return $this->realqas->get_fraction();
 718      }
 719  
 720      public function set_fraction($fraction) {
 721          throw new coding_exception('Cannot modify a question_attempt_step_subquestion_adapter.');
 722      }
 723  
 724      public function get_user_id() {
 725          return $this->realqas->get_user_id();
 726      }
 727  
 728      public function get_timecreated() {
 729          return $this->realqas->get_timecreated();
 730      }
 731  
 732      public function has_qt_var($name) {
 733          return $this->realqas->has_qt_var($this->add_prefix($name));
 734      }
 735  
 736      public function get_qt_var($name) {
 737          return $this->realqas->get_qt_var($this->add_prefix($name));
 738      }
 739  
 740      public function set_qt_var($name, $value) {
 741          $this->realqas->set_qt_var($this->add_prefix($name), $value);
 742      }
 743  
 744      public function get_qt_data() {
 745          return $this->filter_array($this->realqas->get_qt_data());
 746      }
 747  
 748      public function has_behaviour_var($name) {
 749          return $this->realqas->has_behaviour_var($this->add_prefix($name));
 750      }
 751  
 752      public function get_behaviour_var($name) {
 753          return $this->realqas->get_behaviour_var($this->add_prefix($name));
 754      }
 755  
 756      public function set_behaviour_var($name, $value) {
 757          return $this->realqas->set_behaviour_var($this->add_prefix($name), $value);
 758      }
 759  
 760      public function get_behaviour_data() {
 761          return $this->filter_array($this->realqas->get_behaviour_data());
 762      }
 763  
 764      public function get_submitted_data() {
 765          return $this->filter_array($this->realqas->get_submitted_data());
 766      }
 767  
 768      public function get_all_data() {
 769          return $this->filter_array($this->realqas->get_all_data());
 770      }
 771  
 772      public function get_qt_files($name, $contextid) {
 773          throw new coding_exception('No attempt has yet been made to implement files support in ' .
 774                  'question_attempt_step_subquestion_adapter.');
 775      }
 776  
 777      public function prepare_response_files_draft_itemid($name, $contextid) {
 778          throw new coding_exception('No attempt has yet been made to implement files support in ' .
 779                  'question_attempt_step_subquestion_adapter.');
 780      }
 781  
 782      public function prepare_response_files_draft_itemid_with_text($name, $contextid, $text) {
 783          throw new coding_exception('No attempt has yet been made to implement files support in ' .
 784                  'question_attempt_step_subquestion_adapter.');
 785      }
 786  
 787      public function rewrite_response_pluginfile_urls($text, $contextid, $name, $extras) {
 788          throw new coding_exception('No attempt has yet been made to implement files support in ' .
 789                  'question_attempt_step_subquestion_adapter.');
 790      }
 791  }