Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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  namespace mod_assign\event;
  18  
  19  use assign;
  20  use coding_exception;
  21  use stdClass;
  22  
  23  /**
  24   * The mod_assign submission removed event class.
  25   *
  26   * @property-read array $other {
  27   *      Extra information about the event.
  28   *
  29   *      - int submissionid: ID number of this submission.
  30   *      - int submissionattempt: Number of attempts made on this submission.
  31   *      - string submissionstatus: Status of the submission.
  32   *      - int groupid: (optional) The group ID if this is a teamsubmission.
  33   *      - string groupname: (optional) The name of the group if this is a teamsubmission.
  34   * }
  35   *
  36   * @package    mod_assign
  37   * @since      Moodle 4.0
  38   * @copyright  2022 TU Berlin
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class submission_removed extends base {
  42      /**
  43       * Create instance of event.
  44       *
  45       * @param assign $assign
  46       * @param stdClass $submission
  47       * @return submission_removed
  48       * @throws coding_exception
  49       */
  50      public static function create_from_submission(assign $assign, stdClass $submission): submission_removed {
  51          $groupname = null;
  52          $groupid = 0;
  53          if (empty($submission->userid) && !empty($submission->groupid)) {
  54              $groupname = groups_get_group_name($submission->groupid);
  55              $groupid = $submission->groupid;
  56          }
  57          $data = [
  58              'context' => $assign->get_context(),
  59              'objectid' => $submission->id,
  60              'relateduserid' => $assign->get_instance()->teamsubmission ? null : $submission->userid,
  61              'anonymous' => $assign->is_blind_marking() ? 1 : 0,
  62              'other' => [
  63                  'submissionid' => $submission->id,
  64                  'submissionattempt' => $submission->attemptnumber,
  65                  'submissionstatus' => $submission->status,
  66                  'groupid' => $groupid,
  67                  'groupname' => $groupname
  68              ]
  69          ];
  70          /** @var submission_removed $event */
  71          $event = self::create($data);
  72          $event->set_assign($assign);
  73          $event->add_record_snapshot('assign_submission', $submission);
  74          return $event;
  75      }
  76  
  77      /**
  78       * Init method.
  79       */
  80      protected function init() {
  81          $this->data['crud'] = 'd';
  82          $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
  83          $this->data['objecttable'] = 'assign_submission';
  84      }
  85  
  86      /**
  87       * Returns localised general event name.
  88       *
  89       * @return string
  90       * @throws coding_exception
  91       */
  92      public static function get_name(): string {
  93          return get_string('eventsubmissionremoved', 'mod_assign');
  94      }
  95  
  96      /**
  97       * Returns non-localised description of what happened.
  98       *
  99       * @return string
 100       */
 101      public function get_description(): string {
 102          $descriptionstring = "The user with id '$this->userid' removed the submission with id '$this->objectid' in " .
 103              "the assignment with course module id '$this->contextinstanceid' submitted by ";
 104          if (!empty($this->other['groupid'])) {
 105              $groupname = $this->other['groupname'];
 106              $groupid = $this->other['groupid'];
 107              $descriptionstring .= "the group '$groupname' with id '$groupid'.";
 108          } else {
 109              $descriptionstring .= "the user with id '$this->relateduserid'.";
 110          }
 111          return $descriptionstring;
 112      }
 113  
 114      /**
 115       * Custom validation.
 116       *
 117       * @return void
 118       * @throws coding_exception
 119       */
 120      protected function validate_data() {
 121          parent::validate_data();
 122          if (!isset($this->other['submissionid'])) {
 123              throw new coding_exception('The \'submissionid\' value must be set in other.');
 124          }
 125          if (!isset($this->other['submissionattempt'])) {
 126              throw new coding_exception('The \'submissionattempt\' value must be set in other.');
 127          }
 128          if (!isset($this->other['submissionstatus'])) {
 129              throw new coding_exception('The \'submissionstatus\' value must be set in other.');
 130          }
 131      }
 132  
 133      /**
 134       * Get objectid mapping.
 135       *
 136       * @return array
 137       */
 138      public static function get_objectid_mapping(): array {
 139          return ['db' => 'assign_submission', 'restore' => 'submission'];
 140      }
 141  
 142      /**
 143       * Get other mapping.
 144       *
 145       * @return array
 146       */
 147      public static function get_other_mapping(): array {
 148          $othermapped = [];
 149          $othermapped['submissionid'] = ['db' => 'assign_submission', 'restore' => 'submission'];
 150          $othermapped['groupid'] = ['db' => 'groups', 'restore' => 'group'];
 151  
 152          return $othermapped;
 153      }
 154  }