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_feedback response submitted event.
  19   *
  20   * @package    mod_feedback
  21   * @copyright  2013 Ankit Agarwal
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  23   */
  24  
  25  namespace mod_feedback\event;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * The mod_feedback response submitted event class.
  30   *
  31   * This event is triggered when a feedback response is submitted.
  32   *
  33   * @property-read array $other {
  34   *      Extra information about event.
  35   *
  36   *      - int anonymous: if feedback is anonymous.
  37   *      - int cmid: course module id.
  38   *      - int instanceid: id of instance.
  39   * }
  40   *
  41   * @package    mod_feedback
  42   * @since      Moodle 2.6
  43   * @copyright  2013 Ankit Agarwal
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  45   */
  46  class response_submitted extends \core\event\base {
  47  
  48      /**
  49       * Set basic properties for the event.
  50       */
  51      protected function init() {
  52          global $CFG;
  53  
  54          require_once($CFG->dirroot.'/mod/feedback/lib.php');
  55          $this->data['objecttable'] = 'feedback_completed';
  56          $this->data['crud'] = 'c';
  57          $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
  58      }
  59  
  60      /**
  61       * Creates an instance from the record from db table feedback_completed
  62       *
  63       * @param stdClass $completed
  64       * @param stdClass|cm_info $cm
  65       * @return self
  66       */
  67      public static function create_from_record($completed, $cm) {
  68          $event = self::create(array(
  69              'relateduserid' => $completed->userid,
  70              'objectid' => $completed->id,
  71              'context' => \context_module::instance($cm->id),
  72              'anonymous' => ($completed->anonymous_response == FEEDBACK_ANONYMOUS_YES),
  73              'other' => array(
  74                  'cmid' => $cm->id,
  75                  'instanceid' => $completed->feedback,
  76                  'anonymous' => $completed->anonymous_response // Deprecated.
  77              )
  78          ));
  79          $event->add_record_snapshot('feedback_completed', $completed);
  80          return $event;
  81      }
  82  
  83      /**
  84       * Returns localised general event name.
  85       *
  86       * @return string
  87       */
  88      public static function get_name() {
  89          return get_string('eventresponsesubmitted', 'mod_feedback');
  90      }
  91  
  92      /**
  93       * Returns non-localised event description with id's for admin use only.
  94       *
  95       * @return string
  96       */
  97      public function get_description() {
  98          return "The user with id '$this->userid' submitted response for 'feedback' activity with "
  99                  . "course module id '$this->contextinstanceid'.";
 100      }
 101  
 102      /**
 103       * Returns relevant URL based on the anonymous mode of the response.
 104       * @return \moodle_url
 105       */
 106      public function get_url() {
 107          if ($this->anonymous) {
 108              return new \moodle_url('/mod/feedback/show_entries.php', array('id' => $this->other['cmid'],
 109                      'showcompleted' => $this->objectid));
 110          } else {
 111              return new \moodle_url('/mod/feedback/show_entries.php' , array('id' => $this->other['cmid'],
 112                      'userid' => $this->userid, 'showcompleted' => $this->objectid));
 113          }
 114      }
 115  
 116      /**
 117       * Replace add_to_log() statement. Do this only for the case when anonymous mode is off,
 118       * since this is what was happening before.
 119       *
 120       * @return array of parameters to be passed to legacy add_to_log() function.
 121       */
 122      protected function get_legacy_logdata() {
 123          if ($this->anonymous) {
 124              return null;
 125          } else {
 126              return array($this->courseid, 'feedback', 'submit', 'view.php?id=' . $this->other['cmid'],
 127                      $this->other['instanceid'], $this->other['cmid'], $this->relateduserid);
 128          }
 129      }
 130  
 131      /**
 132       * Define whether a user can view the event or not. Make sure no one except admin can see details of an anonymous response.
 133       *
 134       * @deprecated since 2.7
 135       *
 136       * @param int|\stdClass $userorid ID of the user.
 137       * @return bool True if the user can view the event, false otherwise.
 138       */
 139      public function can_view($userorid = null) {
 140          global $USER;
 141          debugging('can_view() method is deprecated, use anonymous flag instead if necessary.', DEBUG_DEVELOPER);
 142  
 143          if (empty($userorid)) {
 144              $userorid = $USER;
 145          }
 146          if ($this->anonymous) {
 147              return is_siteadmin($userorid);
 148          } else {
 149              return has_capability('mod/feedback:viewreports', $this->context, $userorid);
 150          }
 151      }
 152  
 153      /**
 154       * Custom validations.
 155       *
 156       * @throws \coding_exception in case of any problems.
 157       */
 158      protected function validate_data() {
 159          parent::validate_data();
 160  
 161          if (!isset($this->relateduserid)) {
 162              throw new \coding_exception('The \'relateduserid\' must be set.');
 163          }
 164          if (!isset($this->other['anonymous'])) {
 165              throw new \coding_exception('The \'anonymous\' value must be set in other.');
 166          }
 167          if (!isset($this->other['cmid'])) {
 168              throw new \coding_exception('The \'cmid\' value must be set in other.');
 169          }
 170          if (!isset($this->other['instanceid'])) {
 171              throw new \coding_exception('The \'instanceid\' value must be set in other.');
 172          }
 173      }
 174  
 175      public static function get_objectid_mapping() {
 176          return array('db' => 'feedback_completed', 'restore' => 'feedback_completed');
 177      }
 178  
 179      public static function get_other_mapping() {
 180          $othermapped = array();
 181          $othermapped['cmid'] = array('db' => 'course_modules', 'restore' => 'course_module');
 182          $othermapped['instanceid'] = array('db' => 'feedback', 'restore' => 'feedback');
 183  
 184          return $othermapped;
 185      }
 186  }
 187