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 abstract base event.
  19   *
  20   * @package    mod_assign
  21   * @copyright  2014 Mark Nelson
  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 abstract base event class.
  31   *
  32   * Most mod_assign events can extend this class.
  33   *
  34   * @package    mod_assign
  35   * @since      Moodle 2.7
  36   * @copyright  2014 Mark Nelson
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  abstract class base extends \core\event\base {
  40  
  41      /** @var \assign */
  42      protected $assign;
  43  
  44      /**
  45       * Legacy log data.
  46       *
  47       * @var array
  48       */
  49      protected $legacylogdata;
  50  
  51      /**
  52       * Set assign instance for this event.
  53       * @param \assign $assign
  54       * @throws \coding_exception
  55       */
  56      public function set_assign(\assign $assign) {
  57          if ($this->is_triggered()) {
  58              throw new \coding_exception('set_assign() must be done before triggerring of event');
  59          }
  60          if ($assign->get_context()->id != $this->get_context()->id) {
  61              throw new \coding_exception('Invalid assign isntance supplied!');
  62          }
  63          if ($assign->is_blind_marking()) {
  64              $this->data['anonymous'] = 1;
  65          }
  66          $this->assign = $assign;
  67      }
  68  
  69      /**
  70       * Get assign instance.
  71       *
  72       * NOTE: to be used from observers only.
  73       *
  74       * @throws \coding_exception
  75       * @return \assign
  76       */
  77      public function get_assign() {
  78          if ($this->is_restored()) {
  79              throw new \coding_exception('get_assign() is intended for event observers only');
  80          }
  81          if (!isset($this->assign)) {
  82              debugging('assign property should be initialised in each event', DEBUG_DEVELOPER);
  83              global $CFG;
  84              require_once($CFG->dirroot . '/mod/assign/locallib.php');
  85              $cm = get_coursemodule_from_id('assign', $this->contextinstanceid, 0, false, MUST_EXIST);
  86              $course = get_course($cm->course);
  87              $this->assign = new \assign($this->get_context(), $cm, $course);
  88          }
  89          return $this->assign;
  90      }
  91  
  92  
  93      /**
  94       * Returns relevant URL.
  95       *
  96       * @return \moodle_url
  97       */
  98      public function get_url() {
  99          return new \moodle_url('/mod/assign/view.php', array('id' => $this->contextinstanceid));
 100      }
 101  
 102      /**
 103       * Sets the legacy event log data.
 104       *
 105       * @param string $action The current action
 106       * @param string $info A detailed description of the change. But no more than 255 characters.
 107       * @param string $url The url to the assign module instance.
 108       */
 109      public function set_legacy_logdata($action = '', $info = '', $url = '') {
 110          $fullurl = 'view.php?id=' . $this->contextinstanceid;
 111          if ($url != '') {
 112              $fullurl .= '&' . $url;
 113          }
 114  
 115          $this->legacylogdata = array($this->courseid, 'assign', $action, $fullurl, $info, $this->contextinstanceid);
 116      }
 117  
 118      /**
 119       * Return legacy data for add_to_log().
 120       *
 121       * @return array
 122       */
 123      protected function get_legacy_logdata() {
 124          if (isset($this->legacylogdata)) {
 125              return $this->legacylogdata;
 126          }
 127  
 128          return null;
 129      }
 130  
 131      /**
 132       * Custom validation.
 133       *
 134       * @throws \coding_exception
 135       */
 136      protected function validate_data() {
 137          parent::validate_data();
 138  
 139          if ($this->contextlevel != CONTEXT_MODULE) {
 140              throw new \coding_exception('Context level must be CONTEXT_MODULE.');
 141          }
 142      }
 143  }