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.

Differences Between: [Versions 401 and 402] [Versions 401 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   * The mod_assign remove submission form viewed event.
  19   *
  20   * @package    mod_assign
  21   * @copyright  2019 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_assign\event;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * The mod_assign remove submission form viewed event class.
  31   *
  32   * @package    mod_assign
  33   * @copyright  2019 Damyon Wiese
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class remove_submission_form_viewed extends base {
  37  
  38      /**
  39       * Flag for prevention of direct create() call.
  40       * @var bool
  41       */
  42      protected static $preventcreatecall = true;
  43  
  44      /**
  45       * Create instance of event.
  46       *
  47       * @param \assign $assign
  48       * @param \stdClass $user
  49       * @return remove_submission_form_viewed
  50       */
  51      public static function create_from_user(\assign $assign, \stdClass $user) {
  52          $data = array(
  53              'relateduserid' => $user->id,
  54              'context' => $assign->get_context(),
  55              'other' => array(
  56                  'assignid' => $assign->get_instance()->id,
  57              ),
  58          );
  59          self::$preventcreatecall = false;
  60          /** @var remove_submission_form_viewed $event */
  61          $event = self::create($data);
  62          self::$preventcreatecall = true;
  63          $event->set_assign($assign);
  64          $event->add_record_snapshot('user', $user);
  65          return $event;
  66      }
  67  
  68      /**
  69       * Init method.
  70       */
  71      protected function init() {
  72          $this->data['crud'] = 'r';
  73          $this->data['edulevel'] = self::LEVEL_OTHER;
  74      }
  75  
  76      /**
  77       * Returns localised general event name.
  78       *
  79       * @return string
  80       */
  81      public static function get_name() {
  82          return get_string('eventremovesubmissionformviewed', 'mod_assign');
  83      }
  84  
  85      /**
  86       * Returns description of what happened.
  87       *
  88       * @return string
  89       */
  90      public function get_description() {
  91          if ($this->userid != $this->relateduserid) {
  92              return "The user with id '$this->userid' viewed the " .
  93                  "remove submission form for the user with id '$this->relateduserid' " .
  94                  "for the assignment with course module id '$this->contextinstanceid'.";
  95          }
  96  
  97          return "The user with id '$this->userid' viewed their remove submission form for the assignment with course module id " .
  98              "'$this->contextinstanceid'.";
  99      }
 100  
 101      /**
 102       * Return legacy data for add_to_log().
 103       *
 104       * @return array
 105       */
 106      protected function get_legacy_logdata() {
 107          if ($this->relateduserid == $this->userid) {
 108              $title = get_string('removesubmission', 'assign');
 109          } else {
 110              $user = $this->get_record_snapshot('user', $this->relateduserid);
 111              $title = get_string('removesubmissionforstudent', 'assign', array('id' => $user->id, 'fullname' => fullname($user)));
 112          }
 113          $this->set_legacy_logdata('view remove submission assignment form', $title);
 114          return parent::get_legacy_logdata();
 115      }
 116  
 117      /**
 118       * Custom validation.
 119       *
 120       * @throws \coding_exception
 121       */
 122      protected function validate_data() {
 123          if (self::$preventcreatecall) {
 124              throw new \coding_exception('cannot call remove_submission_form_viewed::create() directly, use ' .
 125                                          'remove_submission_form_viewed::create_from_user() instead.');
 126          }
 127  
 128          parent::validate_data();
 129  
 130          if (!isset($this->relateduserid)) {
 131              throw new \coding_exception('The \'relateduserid\' must be set.');
 132          }
 133  
 134          if (!isset($this->other['assignid'])) {
 135              throw new \coding_exception('The \'assignid\' value must be set in other.');
 136          }
 137      }
 138  
 139      /**
 140       * This is documented in the parent class.
 141       *
 142       * @return array an array of other values and their corresponding mapping
 143       */
 144      public static function get_other_mapping() {
 145          $othermapped = array();
 146          $othermapped['assignid'] = array('db' => 'assign', 'restore' => 'assign');
 147  
 148          return $othermapped;
 149      }
 150  }