Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400]

   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 qtype_multichoice.
  19   *
  20   * @package    qtype_multichoice
  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 qtype_multichoice\privacy;
  26  
  27  use \core_privacy\local\metadata\collection;
  28  use \core_privacy\local\request\transform;
  29  use \core_privacy\local\request\user_preference_provider;
  30  use \core_privacy\local\request\writer;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  /**
  35   * Privacy Subsystem for qtype_multichoice implementing user_preference_provider.
  36   *
  37   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class provider implements
  41          // This component has data.
  42          // We need to return default options that have been set a user preferences.
  43          \core_privacy\local\metadata\provider,
  44          \core_privacy\local\request\user_preference_provider
  45  {
  46  
  47      /**
  48       * Returns meta data about this system.
  49       *
  50       * @param   collection     $collection The initialised collection to add items to.
  51       * @return  collection     A listing of user data stored through this system.
  52       */
  53      public static function get_metadata(collection $collection) : collection {
  54          $collection->add_user_preference('qtype_multichoice_defaultmark', 'privacy:preference:defaultmark');
  55          $collection->add_user_preference('qtype_multichoice_penalty', 'privacy:preference:penalty');
  56          $collection->add_user_preference('qtype_multichoice_single', 'privacy:preference:single');
  57          $collection->add_user_preference('qtype_multichoice_shuffleanswers', 'privacy:preference:shuffleanswers');
  58          $collection->add_user_preference('qtype_multichoice_answernumbering', 'privacy:preference:answernumbering');
  59          $collection->add_user_preference('qtype_multichoice_showstandardinstruction', 'privacy:preference:showstandardinstruction');
  60          return $collection;
  61      }
  62  
  63      /**
  64       * Export all user preferences for the plugin.
  65       *
  66       * @param int $userid The userid of the user whose data is to be exported.
  67       */
  68      public static function export_user_preferences(int $userid) {
  69          $preference = get_user_preferences('qtype_multichoice_defaultmark', null, $userid);
  70          if (null !== $preference) {
  71              $desc = get_string('privacy:preference:defaultmark', 'qtype_multichoice');
  72              writer::export_user_preference('qtype_multichoice', 'defaultmark', $preference, $desc);
  73          }
  74  
  75          $preference = get_user_preferences('qtype_multichoice_penalty', null, $userid);
  76          if (null !== $preference) {
  77              $desc = get_string('privacy:preference:penalty', 'qtype_multichoice');
  78              writer::export_user_preference('qtype_multichoice', 'penalty', transform::percentage($preference), $desc);
  79          }
  80  
  81          $preference = get_user_preferences('qtype_multichoice_single', null, $userid);
  82          if (null !== $preference) {
  83              if ($preference) {
  84                  $stringvalue = get_string('answersingleyes', 'qtype_multichoice');
  85              } else {
  86                  $stringvalue = get_string('answersingleno', 'qtype_multichoice');
  87              }
  88              $desc = get_string('privacy:preference:single', 'qtype_multichoice');
  89              writer::export_user_preference('qtype_multichoice', 'single', $stringvalue, $desc);
  90          }
  91  
  92          $preference = get_user_preferences('qtype_multichoice_answernumbering', null, $userid);
  93          if (null !== $preference) {
  94              $desc = get_string('privacy:preference:answernumbering', 'qtype_multichoice');
  95              writer::export_user_preference('qtype_multichoice', 'answernumbering',
  96                      get_string('answernumbering' . $preference, 'qtype_multichoice'), $desc);
  97          }
  98  
  99          $preferences = [
 100                  'shuffleanswers',
 101                  'showstandardinstruction'
 102          ];
 103          foreach ($preferences as $key) {
 104              $preference = get_user_preferences("qtype_multichoice_{$key}", null, $userid);
 105              if (null !== $preference) {
 106                  $desc = get_string("privacy:preference:{$key}", 'qtype_multichoice');
 107                  writer::export_user_preference('qtype_multichoice', $key, transform::yesno($preference), $desc);
 108              }
 109          }
 110      }
 111  }