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   * Event for when a new blog entry is associated with a context.
  18   *
  19   * @package    core
  20   * @copyright  2013 onwards Ankit Agarwal
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  namespace core\event;
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Class for event to be triggered when a new blog entry is associated with a context.
  29   *
  30   * @property-read array $other {
  31   *      Extra information about event.
  32   *
  33   *      - string associatetype: type of blog association, course/coursemodule.
  34   *      - int blogid: id of blog.
  35   *      - int associateid: id of associate.
  36   *      - string subject: blog subject.
  37   * }
  38   *
  39   * @package    core
  40   * @since      Moodle 2.7
  41   * @copyright  2013 onwards Ankit Agarwal
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class blog_association_created extends base {
  45  
  46      /**
  47       * Set basic properties for the event.
  48       */
  49      protected function init() {
  50          $this->context = \context_system::instance();
  51          $this->data['objecttable'] = 'blog_association';
  52          $this->data['crud'] = 'c';
  53          $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
  54      }
  55  
  56      /**
  57       * Returns localised general event name.
  58       *
  59       * @return string
  60       */
  61      public static function get_name() {
  62          return get_string('eventblogassociationadded', 'core_blog');
  63      }
  64  
  65      /**
  66       * Returns non-localised event description with id's for admin use only.
  67       *
  68       * @return string
  69       */
  70      public function get_description() {
  71          return "The user with id '$this->userid' associated the context '{$this->other['associatetype']}' with id " .
  72              "'{$this->other['associateid']}' to the blog entry with id '{$this->other['blogid']}'.";
  73      }
  74  
  75      /**
  76       * Returns relevant URL.
  77       * @return \moodle_url
  78       */
  79      public function get_url() {
  80          return new \moodle_url('/blog/index.php', array('entryid' => $this->other['blogid']));
  81      }
  82  
  83      /**
  84       * replace add_to_log() statement.
  85       *
  86       * @return array of parameters to be passed to legacy add_to_log() function.
  87       */
  88      protected function get_legacy_logdata() {
  89          if ($this->other['associatetype'] === 'course') {
  90              return array (SITEID, 'blog', 'add association', 'index.php?userid=' . $this->relateduserid. '&entryid=' .
  91                      $this->other['blogid'], $this->other['subject'], 0, $this->relateduserid);
  92          }
  93  
  94          return array (SITEID, 'blog', 'add association', 'index.php?userid=' . $this->relateduserid. '&entryid=' .
  95                  $this->other['blogid'], $this->other['subject'], $this->other['associateid'], $this->relateduserid);
  96      }
  97  
  98      /**
  99       * Custom validations.
 100       *
 101       * @throws \coding_exception when validation fails.
 102       * @return void
 103       */
 104      protected function validate_data() {
 105          parent::validate_data();
 106  
 107          if (!isset($this->relateduserid)) {
 108              throw new \coding_exception('The \'relateduserid\' must be set.');
 109          }
 110  
 111          if (empty($this->other['associatetype']) || ($this->other['associatetype'] !== 'course'
 112                  && $this->other['associatetype'] !== 'coursemodule')) {
 113              throw new \coding_exception('The \'associatetype\' value must be set in other and be a valid type.');
 114          }
 115  
 116          if (!isset($this->other['blogid'])) {
 117              throw new \coding_exception('The \'blogid\' value must be set in other.');
 118          }
 119  
 120          if (!isset($this->other['associateid'])) {
 121              throw new \coding_exception('The \'associateid\' value must be set in other.');
 122          }
 123  
 124          if (!isset($this->other['subject'])) {
 125              throw new \coding_exception('The \'subject\' value must be set in other.');
 126          }
 127      }
 128  
 129      public static function get_objectid_mapping() {
 130          // Blogs are not included in backups, so no mapping required for restore.
 131          return array('db' => 'blog_association', 'restore' => base::NOT_MAPPED);
 132      }
 133  
 134      public static function get_other_mapping() {
 135          // Blogs are not included in backups, so no mapping required for restore.
 136          $othermapped = array();
 137          $othermapped['blogid'] = array('db' => 'post', 'restore' => base::NOT_MAPPED);
 138          // The associateid field is varying (context->instanceid) so cannot be mapped.
 139  
 140          return $othermapped;
 141      }
 142  }