MDL-41101 convert assign event triggers to new create_from_xxx helpers
[moodle.git] / mod / assign / classes / event / submission_status_viewed.php
CommitLineData
cf2d7bfb
MN
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 submission status viewed event.
19 *
20 * @property-read array $other {
21 * Extra information about event.
22 *
23 * - int assignid: the id of the assignment.
24 * }
25 *
26 * @package mod_assign
27 * @since Moodle 2.7
28 * @copyright 2014 Mark Nelson <markn@moodle.com>
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 */
31
32namespace mod_assign\event;
33
34defined('MOODLE_INTERNAL') || die();
35
36class submission_status_viewed extends base {
1b90858f
PS
37 /** @var \assign */
38 protected $assign;
39 /**
40 * Flag for prevention of direct create() call.
41 * @var bool
42 */
43 protected static $preventcreatecall = true;
44
45 /**
46 * Create instance of event.
47 *
48 * @since Moodle 2.7
49 *
50 * @param \assign $assign
51 * @return submission_status_viewed
52 */
53 public static function create_from_assign(\assign $assign) {
54 $data = array(
55 'context' => $assign->get_context(),
56 'other' => array(
57 'assignid' => $assign->get_instance()->id,
58 ),
59 );
60 self::$preventcreatecall = false;
61 /** @var submission_status_viewed $event */
62 $event = self::create($data);
63 self::$preventcreatecall = true;
64 $event->assign = $assign;
65 return $event;
66 }
67
68 /**
69 * Get assign instance.
70 *
71 * NOTE: to be used from observers only.
72 *
73 * @since Moodle 2.7
74 *
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 return $this->assign;
82 }
cf2d7bfb
MN
83
84 /**
85 * Init method.
86 *
87 * @return void
88 */
89 protected function init() {
90 $this->data['crud'] = 'r';
91 $this->data['edulevel'] = self::LEVEL_OTHER;
92 }
93
94 /**
95 * Return localised event name.
96 *
97 * @return string
98 */
99 public static function get_name() {
100 return get_string('eventsubmissionstatusviewed', 'mod_assign');
101 }
102
103 /**
104 * Returns description of what happened.
105 *
106 * @return string
107 */
108 public function get_description() {
109 return "The user with the id {$this->userid} has viewed the status of their submission for the assignment with
110 the id {$this->other['assignid']}.";
111 }
112
1b90858f
PS
113 /**
114 * Return legacy data for add_to_log().
115 *
116 * @return array
117 */
118 protected function get_legacy_logdata() {
119 $this->set_legacy_logdata('view', get_string('viewownsubmissionstatus', 'assign'));
120 return parent::get_legacy_logdata();
121 }
122
cf2d7bfb
MN
123 /**
124 * Custom validation.
125 *
126 * @throws \coding_exception
1b90858f 127 * @return void
cf2d7bfb
MN
128 */
129 protected function validate_data() {
1b90858f
PS
130 if (self::$preventcreatecall) {
131 throw new \coding_exception('cannot call submission_status_viewed::create() directly, use submission_status_viewed::create_from_assign() instead.');
132 }
133
cf2d7bfb
MN
134 parent::validate_data();
135
136 if (!isset($this->other['assignid'])) {
137 throw new \coding_exception('The \'assignid\' must be set in other.');
138 }
139 }
140}