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  /**
  18   * The mod_choice answer created event.
  19   *
  20   * @package    mod_choice
  21   * @copyright  2016 Marina Glancy
  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 created 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.2
  41   * @copyright  2016 Marina Glancy
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class answer_created 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 added 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('eventanswercreated', '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'] = 'c';
 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      /**
 132       * This is used when restoring course logs where it is required that we
 133       * map the objectid to it's new value in the new course.
 134       *
 135       * @return string the name of the restore mapping the objectid links to
 136       */
 137      public static function get_objectid_mapping() {
 138          return array('db' => 'choice_answers', 'restore' => 'answer');
 139      }
 140  
 141      /**
 142       * This is used when restoring course logs where it is required that we
 143       * map the information in 'other' to it's new value in the new course.
 144       *
 145       * @return array an array of other values and their corresponding mapping
 146       */
 147      public static function get_other_mapping() {
 148          $othermapped = array();
 149          $othermapped['choiceid'] = array('db' => 'choice', 'restore' => 'choice');
 150          $othermapped['optionid'] = array('db' => 'choice_options', 'restore' => 'choice_option');
 151  
 152          return $othermapped;
 153      }
 154  }