Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * Privacy helper.
  19   *
  20   * @package    tool_log
  21   * @copyright  2018 Frédéric Massart
  22   * @author     Frédéric Massart <fred@branchup.tech>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace tool_log\local\privacy;
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_privacy\local\request\transform;
  30  
  31  /**
  32   * Privacy helper class.
  33   *
  34   * @package    tool_log
  35   * @copyright  2018 Frédéric Massart
  36   * @author     Frédéric Massart <fred@branchup.tech>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class helper {
  40      use \tool_log\helper\reader;
  41  
  42      /**
  43       * Returns an event from a standard record.
  44       *
  45       * @see \logstore_standard\log\store::get_log_event()
  46       * @param object $data Log data.
  47       * @return \core\event\base
  48       */
  49      protected static function restore_event_from_standard_record($data) {
  50          $extra = ['origin' => $data->origin, 'ip' => $data->ip, 'realuserid' => $data->realuserid];
  51          $data = (array) $data;
  52          $id = $data['id'];
  53          $data['other'] = self::decode_other($data['other']);
  54          if ($data['other'] === false) {
  55              $data['other'] = [];
  56          }
  57          unset($data['origin']);
  58          unset($data['ip']);
  59          unset($data['realuserid']);
  60          unset($data['id']);
  61  
  62          if (!$event = \core\event\base::restore($data, $extra)) {
  63              return null;
  64          }
  65  
  66          return $event;
  67      }
  68  
  69      /**
  70       * Transform a standard log record for a user.
  71       *
  72       * @param object $record The record.
  73       * @param int $userid The user ID.
  74       * @return array
  75       */
  76      public static function transform_standard_log_record_for_userid($record, $userid) {
  77  
  78          // Restore the event to try to get the name, description and other field.
  79          $restoredevent = static::restore_event_from_standard_record($record);
  80          if ($restoredevent) {
  81              $name = $restoredevent->get_name();
  82              $description = $restoredevent->get_description();
  83              $other = $restoredevent->other;
  84  
  85          } else {
  86              $name = $record->eventname;
  87              $description = "Unknown event ({$name})";
  88              $other = \tool_log\helper\reader::decode_other($record->other);
  89          }
  90  
  91          $realuserid = $record->realuserid;
  92          $isauthor = $record->userid == $userid;
  93          $isrelated = $record->relateduserid == $userid;
  94          $isrealuser = $realuserid == $userid;
  95          $ismasqueraded = $realuserid !== null && $record->userid != $realuserid;
  96          $ismasquerading = $isrealuser && !$isauthor;
  97          $isanonymous = $record->anonymous;
  98  
  99          $data = [
 100              'name' => $name,
 101              'description' => $description,
 102              'timecreated' => transform::datetime($record->timecreated),
 103              'origin' => static::transform_origin($record->origin),
 104              'ip' => $isauthor ? $record->ip : '',
 105              'other' => $other ? $other : []
 106          ];
 107  
 108          if ($isanonymous) {
 109              $data['action_was_done_anonymously'] = transform::yesno($isanonymous);
 110          }
 111          if ($isauthor || !$isanonymous) {
 112              $data['authorid'] = transform::user($record->userid);
 113              $data['author_of_the_action_was_you'] = transform::yesno($isauthor);
 114          }
 115  
 116          if ($record->relateduserid) {
 117              $data['relateduserid'] = transform::user($record->relateduserid);
 118              $data['related_user_was_you'] = transform::yesno($isrelated);
 119          }
 120  
 121          if ($ismasqueraded) {
 122              $data['author_of_the_action_was_masqueraded'] = transform::yesno(true);
 123              if ($ismasquerading || !$isanonymous) {
 124                  $data['masqueradinguserid'] = transform::user($realuserid);
 125                  $data['masquerading_user_was_you'] = transform::yesno($ismasquerading);
 126              }
 127          }
 128  
 129          return $data;
 130      }
 131  
 132      /**
 133       * Transform origin.
 134       *
 135       * @param string $origin The page request origin.
 136       * @return string
 137       */
 138      public static function transform_origin($origin) {
 139          switch ($origin) {
 140              case 'cli':
 141              case 'restore':
 142              case 'web':
 143              case 'ws':
 144                  return get_string('privacy:request:origin:' . $origin, 'tool_log');
 145                  break;
 146          }
 147          return $origin;
 148      }
 149  }