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   * The mod_choice answer deleted event.
  19   *
  20   * @package    mod_choice
  21   * @copyright  2016 Stephen Bourget
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_choice\event;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * The mod_choice answer deleted event class.
  31   *
  32   * @property-read array $other {
  33   *      Extra information about event.
  34   *
  35   *      - int choiceid: id of choice.
  36   *      - int optionid: id of the option.
  37   * }
  38   *
  39   * @package    mod_choice
  40   * @since      Moodle 3.1
  41   * @copyright  2016 Stephen Bourget
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class answer_deleted extends \core\event\base {
  45  
  46      /**
  47       * Creates an instance of the event from the records
  48       *
  49       * @param stdClass $choiceanswer record from 'choice_answers' table
  50       * @param stdClass $choice record from 'choice' table
  51       * @param stdClass $cm record from 'course_modules' table
  52       * @param stdClass $course
  53       * @return self
  54       */
  55      public static function create_from_object($choiceanswer, $choice, $cm, $course) {
  56          global $USER;
  57          $eventdata = array();
  58          $eventdata['objectid'] = $choiceanswer->id;
  59          $eventdata['context'] = \context_module::instance($cm->id);
  60          $eventdata['userid'] = $USER->id;
  61          $eventdata['courseid'] = $course->id;
  62          $eventdata['relateduserid'] = $choiceanswer->userid;
  63          $eventdata['other'] = array();
  64          $eventdata['other']['choiceid'] = $choice->id;
  65          $eventdata['other']['optionid'] = $choiceanswer->optionid;
  66          $event = self::create($eventdata);
  67          $event->add_record_snapshot('course', $course);
  68          $event->add_record_snapshot('course_modules', $cm);
  69          $event->add_record_snapshot('choice', $choice);
  70          $event->add_record_snapshot('choice_answers', $choiceanswer);
  71          return $event;
  72      }
  73  
  74      /**
  75       * Returns description of what happened.
  76       *
  77       * @return string
  78       */
  79      public function get_description() {
  80          return "The user with id '$this->userid' has deleted the option with id '" . $this->other['optionid'] . "' for the
  81              user with id '$this->relateduserid' from the choice activity with course module id '$this->contextinstanceid'.";
  82      }
  83  
  84      /**
  85       * Return localised event name.
  86       *
  87       * @return string
  88       */
  89      public static function get_name() {
  90          return get_string('eventanswerdeleted', 'mod_choice');
  91      }
  92  
  93      /**
  94       * Get URL related to the action
  95       *
  96       * @return \moodle_url
  97       */
  98      public function get_url() {
  99          return new \moodle_url('/mod/choice/view.php', array('id' => $this->contextinstanceid));
 100      }
 101  
 102      /**
 103       * Init method.
 104       *
 105       * @return void
 106       */
 107      protected function init() {
 108          $this->data['objecttable'] = 'choice_answers';
 109          $this->data['crud'] = 'd';
 110          $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
 111      }
 112  
 113      /**
 114       * Custom validation.
 115       *
 116       * @throws \coding_exception
 117       * @return void
 118       */
 119      protected function validate_data() {
 120          parent::validate_data();
 121  
 122          if (!isset($this->other['choiceid'])) {
 123              throw new \coding_exception('The \'choiceid\' value must be set in other.');
 124          }
 125  
 126          if (!isset($this->other['optionid'])) {
 127              throw new \coding_exception('The \'optionid\' value must be set in other.');
 128          }
 129      }
 130  
 131      public static function get_objectid_mapping() {
 132          return array('db' => 'choice_answers', 'restore' => \core\event\base::NOT_MAPPED);
 133      }
 134  
 135      public static function get_other_mapping() {
 136          $othermapped = array();
 137          $othermapped['choiceid'] = array('db' => 'choice', 'restore' => 'choice');
 138          $othermapped['optionid'] = array('db' => 'choice_options', 'restore' => 'choice_option');
 139  
 140          return $othermapped;
 141      }
 142  }