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   * Provides the class {@link workshopform_numerrors\privacy\provider}
  19   *
  20   * @package     workshopform_numerrors
  21   * @category    privacy
  22   * @copyright   2018 David Mudrák <david@moodle.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace workshopform_numerrors\privacy;
  27  
  28  use core_privacy\local\request\writer;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * Privacy API implementation for the Number of errors strategy.
  34   *
  35   * @copyright 2018 David Mudrák <david@moodle.com>
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class provider implements \core_privacy\local\metadata\null_provider, \mod_workshop\privacy\workshopform_provider {
  39  
  40      /**
  41       * Explain that this plugin stores no personal data.
  42       *
  43       * @return string
  44       */
  45      public static function get_reason() : string {
  46          return 'privacy:metadata';
  47      }
  48  
  49      /**
  50       * Return details of the filled assessment form.
  51       *
  52       * @param stdClass $user User we are exporting data for
  53       * @param context $context The workshop activity context
  54       * @param array $subcontext Subcontext within the context to export to
  55       * @param int $assessmentid ID of the assessment
  56       */
  57      public static function export_assessment_form(\stdClass $user, \context $context, array $subcontext, int $assessmentid) {
  58          global $DB;
  59  
  60          if ($context->contextlevel != CONTEXT_MODULE) {
  61              throw new \coding_exception('Unexpected context provided');
  62          }
  63  
  64          $sql = "SELECT dim.id, dim.workshopid, dim.description, dim.descriptionformat, dim.grade0, dim.grade1, dim.weight,
  65                         wg.grade, wg.peercomment, wg.peercommentformat
  66                    FROM {course_modules} cm
  67                    JOIN {context} ctx ON ctx.contextlevel = :contextlevel AND ctx.instanceid = cm.id
  68                    JOIN {workshop} w ON cm.instance = w.id
  69                    JOIN {workshopform_numerrors} dim ON dim.workshopid = w.id
  70               LEFT JOIN {workshop_grades} wg ON wg.strategy = :strategy
  71                         AND wg.dimensionid = dim.id AND wg.assessmentid = :assessmentid
  72                   WHERE ctx.id = :contextid
  73                ORDER BY dim.sort";
  74  
  75          $params = [
  76              'strategy' => 'numerrors',
  77              'contextlevel' => CONTEXT_MODULE,
  78              'contextid' => $context->id,
  79              'assessmentid' => $assessmentid,
  80          ];
  81  
  82          $writer = \core_privacy\local\request\writer::with_context($context);
  83          $data = [];
  84          $workshopid = null;
  85          $hasdata = false;
  86          $dimensionids = [];
  87  
  88          foreach ($DB->get_records_sql($sql, $params) as $record) {
  89              if ($record->grade !== null) {
  90                  $hasdata = true;
  91              }
  92              $record->description = $writer->rewrite_pluginfile_urls($subcontext, 'workshopform_numerrors',
  93                  'description', $record->id, $record->description);
  94              $workshopid = $record->workshopid;
  95              $dimensionids[] = $record->id;
  96              unset($record->id);
  97              unset($record->workshopid);
  98              $data[] = $record;
  99          }
 100  
 101          if ($hasdata) {
 102              $writer->export_data($subcontext, (object)['assertions' => $data]);
 103              foreach ($dimensionids as $dimensionid) {
 104                  $writer->export_area_files($subcontext, 'workshopform_numerrors', 'description', $dimensionid);
 105              }
 106              foreach ($DB->get_records('workshopform_numerrors_map', ['workshopid' => $workshopid], 'nonegative',
 107                      'id, nonegative, grade') as $mapping) {
 108                  $writer->export_metadata($subcontext, 'map_'.$mapping->nonegative.'_errors', $mapping->grade,
 109                      get_string('privacy:export:metadata:map', 'workshopform_numerrors', [
 110                          'nonegative' => $mapping->nonegative,
 111                          'grade' => $mapping->grade
 112                      ])
 113                  );
 114              }
 115          }
 116      }
 117  }