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.
   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   * Privacy Subsystem implementation for quiz_responses.
  19   *
  20   * @package    quiz_responses
  21   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace quiz_responses\privacy;
  26  
  27  use core_privacy\local\metadata\collection;
  28  use core_privacy\local\request\writer;
  29  use core_privacy\local\request\transform;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  global $CFG;
  34  require_once($CFG->dirroot . '/question/engine/questionattempt.php');
  35  
  36  /**
  37   * Privacy Subsystem for quiz_responses with user preferences.
  38   *
  39   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class provider implements
  43          \core_privacy\local\metadata\provider,
  44          \core_privacy\local\request\user_preference_provider {
  45  
  46      /**
  47       * Returns meta data about this system.
  48       *
  49       * @param   collection     $collection The initialised collection to add items to.
  50       * @return  collection     A listing of user data stored through this system.
  51       */
  52      public static function get_metadata(collection $collection) : collection {
  53          $collection->add_user_preference('quiz_report_responses_qtext', 'privacy:preference:qtext');
  54          $collection->add_user_preference('quiz_report_responses_resp', 'privacy:preference:resp');
  55          $collection->add_user_preference('quiz_report_responses_right', 'privacy:preference:right');
  56          $collection->add_user_preference('quiz_report_responses_which_tries', 'privacy:preference:which_tries');
  57  
  58          return $collection;
  59      }
  60  
  61      /**
  62       * Export all user preferences for the plugin.
  63       *
  64       * @param   int         $userid The userid of the user whose data is to be exported.
  65       */
  66      public static function export_user_preferences(int $userid) {
  67          $preferences = [
  68                  'qtext',
  69                  'resp',
  70                  'right',
  71              ];
  72  
  73          foreach ($preferences as $key) {
  74              $preference = get_user_preferences("quiz_report_responses_{$key}", null, $userid);
  75              if (null !== $preference) {
  76                  $desc = get_string("privacy:preference:{$key}", 'quiz_responses');
  77                  writer::export_user_preference('quiz_responses', $key, transform::yesno($preference), $desc);
  78              }
  79          }
  80  
  81          $preference = get_user_preferences("quiz_report_responses_which_tries", null, $userid);
  82          if (null !== $preference) {
  83              switch($preference) {
  84                  case \question_attempt::FIRST_TRY:
  85                      $value = get_string("privacy:preference:which_tries:first", 'quiz_responses');
  86                      break;
  87                  case \question_attempt::LAST_TRY:
  88                      $value = get_string("privacy:preference:which_tries:last", 'quiz_responses');
  89                      break;
  90                  case \question_attempt::ALL_TRIES:
  91                      $value = get_string("privacy:preference:which_tries:all", 'quiz_responses');
  92                      break;
  93              }
  94              $desc = get_string("privacy:preference:which_tries", 'quiz_responses');
  95  
  96              writer::export_user_preference('quiz_responses', 'which_tries', $value, $desc);
  97          }
  98      }
  99  }