| 1 | 
           efrain | 
           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 | 
            * Grade item created event.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package    core
  | 
        
        
            | 
            | 
           21 | 
            * @copyright  2019 Dmitrii Metelkin <dmitriim@catalyst-au.net>
  | 
        
        
            | 
            | 
           22 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           namespace core\event;
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           /**
  | 
        
        
            | 
            | 
           30 | 
            * Grade item created event class.
  | 
        
        
            | 
            | 
           31 | 
            *
  | 
        
        
            | 
            | 
           32 | 
            * @package    core
  | 
        
        
            | 
            | 
           33 | 
            * @since      Moodle 3.8
  | 
        
        
            | 
            | 
           34 | 
            * @copyright  2019 Dmitrii Metelkin <dmitriim@catalyst-au.net>
  | 
        
        
            | 
            | 
           35 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           36 | 
            */
  | 
        
        
            | 
            | 
           37 | 
           class grade_item_created extends base {
  | 
        
        
            | 
            | 
           38 | 
              | 
        
        
            | 
            | 
           39 | 
               /** @var \grade_item $gradeitem */
  | 
        
        
            | 
            | 
           40 | 
               protected $gradeitem;
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
               /**
  | 
        
        
            | 
            | 
           43 | 
                * Init method.
  | 
        
        
            | 
            | 
           44 | 
                *
  | 
        
        
            | 
            | 
           45 | 
                * @return void
  | 
        
        
            | 
            | 
           46 | 
                */
  | 
        
        
            | 
            | 
           47 | 
               protected function init() {
  | 
        
        
            | 
            | 
           48 | 
                   $this->data['objecttable'] = 'grade_items';
  | 
        
        
            | 
            | 
           49 | 
                   $this->data['crud'] = 'c';
  | 
        
        
            | 
            | 
           50 | 
                   $this->data['edulevel'] = self::LEVEL_OTHER;
  | 
        
        
            | 
            | 
           51 | 
               }
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
               /**
  | 
        
        
            | 
            | 
           54 | 
                * Return localised event name.
  | 
        
        
            | 
            | 
           55 | 
                *
  | 
        
        
            | 
            | 
           56 | 
                * @return string
  | 
        
        
            | 
            | 
           57 | 
                */
  | 
        
        
            | 
            | 
           58 | 
               public static function get_name() {
  | 
        
        
            | 
            | 
           59 | 
                   return get_string('eventgradeitemcreated', 'core_grades');
  | 
        
        
            | 
            | 
           60 | 
               }
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
               /**
  | 
        
        
            | 
            | 
           63 | 
                * Utility method to create a new event.
  | 
        
        
            | 
            | 
           64 | 
                *
  | 
        
        
            | 
            | 
           65 | 
                * @param \grade_item $gradeitem
  | 
        
        
            | 
            | 
           66 | 
                * @return grade_item_created
  | 
        
        
            | 
            | 
           67 | 
                */
  | 
        
        
            | 
            | 
           68 | 
               public static function create_from_grade_item(\grade_item $gradeitem) {
  | 
        
        
            | 
            | 
           69 | 
                   $event = self::create([
  | 
        
        
            | 
            | 
           70 | 
                       'objectid' => $gradeitem->id,
  | 
        
        
            | 
            | 
           71 | 
                       'courseid' => $gradeitem->courseid,
  | 
        
        
            | 
            | 
           72 | 
                       'context' => \context_course::instance($gradeitem->courseid),
  | 
        
        
            | 
            | 
           73 | 
                       'other' => [
  | 
        
        
            | 
            | 
           74 | 
                           'itemname' => $gradeitem->itemname,
  | 
        
        
            | 
            | 
           75 | 
                           'itemtype' => $gradeitem->itemtype,
  | 
        
        
            | 
            | 
           76 | 
                           'itemmodule' => $gradeitem->itemmodule,
  | 
        
        
            | 
            | 
           77 | 
                       ],
  | 
        
        
            | 
            | 
           78 | 
                   ]);
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
            | 
            | 
           80 | 
                   $event->gradeitem = $gradeitem;
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
                   return $event;
  | 
        
        
            | 
            | 
           83 | 
               }
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
               /**
  | 
        
        
            | 
            | 
           86 | 
                * Get grade object.
  | 
        
        
            | 
            | 
           87 | 
                *
  | 
        
        
            | 
            | 
           88 | 
                * @throws \coding_exception
  | 
        
        
            | 
            | 
           89 | 
                * @return \grade_item
  | 
        
        
            | 
            | 
           90 | 
                */
  | 
        
        
            | 
            | 
           91 | 
               public function get_grade_item() {
  | 
        
        
            | 
            | 
           92 | 
                   if ($this->is_restored()) {
  | 
        
        
            | 
            | 
           93 | 
                       throw new \coding_exception('get_grade_item() is intended for event observers only');
  | 
        
        
            | 
            | 
           94 | 
                   }
  | 
        
        
            | 
            | 
           95 | 
              | 
        
        
            | 
            | 
           96 | 
                   if (!isset($this->gradeitem)) {
  | 
        
        
            | 
            | 
           97 | 
                       $this->gradeitem = \grade_item::fetch(['id' => $this->objectid]);
  | 
        
        
            | 
            | 
           98 | 
                   }
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
            | 
            | 
           100 | 
                   return $this->gradeitem;
  | 
        
        
            | 
            | 
           101 | 
               }
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
               /**
  | 
        
        
            | 
            | 
           104 | 
                * Returns description of what happened.
  | 
        
        
            | 
            | 
           105 | 
                *
  | 
        
        
            | 
            | 
           106 | 
                * @return string
  | 
        
        
            | 
            | 
           107 | 
                */
  | 
        
        
            | 
            | 
           108 | 
               public function get_description() {
  | 
        
        
            | 
            | 
           109 | 
                   return "The user with id '" . $this->userid . "' created a grade item with id '" . $this->objectid . "'" .
  | 
        
        
            | 
            | 
           110 | 
                       " of type '" . $this->other['itemtype'] . "' and name '" . $this->other['itemname'] . "'" .
  | 
        
        
            | 
            | 
           111 | 
                       " in the course with the id '" . $this->courseid . "'.";
  | 
        
        
            | 
            | 
           112 | 
               }
  | 
        
        
            | 
            | 
           113 | 
              | 
        
        
            | 
            | 
           114 | 
               /**
  | 
        
        
            | 
            | 
           115 | 
                * Returns relevant URL.
  | 
        
        
            | 
            | 
           116 | 
                * @return \moodle_url
  | 
        
        
            | 
            | 
           117 | 
                */
  | 
        
        
            | 
            | 
           118 | 
               public function get_url() {
  | 
        
        
            | 
            | 
           119 | 
                   $url = new \moodle_url('/grade/edit/tree/index.php');
  | 
        
        
            | 
            | 
           120 | 
                   $url->param('id', $this->courseid);
  | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
                   return $url;
  | 
        
        
            | 
            | 
           123 | 
               }
  | 
        
        
            | 
            | 
           124 | 
              | 
        
        
            | 
            | 
           125 | 
               /**
  | 
        
        
            | 
            | 
           126 | 
                * Custom validation.
  | 
        
        
            | 
            | 
           127 | 
                *
  | 
        
        
            | 
            | 
           128 | 
                * @throws \coding_exception when validation does not pass.
  | 
        
        
            | 
            | 
           129 | 
                */
  | 
        
        
            | 
            | 
           130 | 
               protected function validate_data() {
  | 
        
        
            | 
            | 
           131 | 
                   parent::validate_data();
  | 
        
        
            | 
            | 
           132 | 
              | 
        
        
            | 
            | 
           133 | 
                   if (!array_key_exists('itemname', $this->other)) {
  | 
        
        
            | 
            | 
           134 | 
                       throw new \coding_exception('The \'itemname\' value must be set in other.');
  | 
        
        
            | 
            | 
           135 | 
                   }
  | 
        
        
            | 
            | 
           136 | 
              | 
        
        
            | 
            | 
           137 | 
                   if (!array_key_exists('itemtype', $this->other)) {
  | 
        
        
            | 
            | 
           138 | 
                           throw new \coding_exception('The \'itemtype\' value must be set in other.');
  | 
        
        
            | 
            | 
           139 | 
                   }
  | 
        
        
            | 
            | 
           140 | 
              | 
        
        
            | 
            | 
           141 | 
                   if (!array_key_exists('itemmodule', $this->other)) {
  | 
        
        
            | 
            | 
           142 | 
                       throw new \coding_exception('The \'itemmodule\' value must be set in other.');
  | 
        
        
            | 
            | 
           143 | 
                   }
  | 
        
        
            | 
            | 
           144 | 
               }
  | 
        
        
            | 
            | 
           145 | 
              | 
        
        
            | 
            | 
           146 | 
           }
  |