diff --git a/mod/assign/classes/event/all_submissions_downloaded.php b/mod/assign/classes/event/all_submissions_downloaded.php index fcbb141c98b01..7b7f81f4c0481 100644 --- a/mod/assign/classes/event/all_submissions_downloaded.php +++ b/mod/assign/classes/event/all_submissions_downloaded.php @@ -35,6 +35,50 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class all_submissions_downloaded extends base { + /** @var \assign */ + protected $assign; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @return all_submissions_downloaded + */ + public static function create_from_assign(\assign $assign) { + $data = array( + 'context' => $assign->get_context(), + 'objectid' => $assign->get_instance()->id + ); + self::$preventcreatecall = false; + /** @var submission_graded $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } /** * Returns description of what happened. @@ -64,4 +108,28 @@ protected function init() { $this->data['edulevel'] = self::LEVEL_TEACHING; $this->data['objecttable'] = 'assign'; } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $this->set_legacy_logdata('download all submissions', get_string('downloadall', 'assign')); + return parent::get_legacy_logdata(); + } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call all_submissions_downloaded::create() directly, use all_submissions_downloaded::create_from_assign() instead.'); + } + + parent::validate_data(); + } } diff --git a/mod/assign/classes/event/assessable_submitted.php b/mod/assign/classes/event/assessable_submitted.php index 7781accf96258..4b86f14431f39 100644 --- a/mod/assign/classes/event/assessable_submitted.php +++ b/mod/assign/classes/event/assessable_submitted.php @@ -41,6 +41,74 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class assessable_submitted extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $submission; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $submission + * @param bool $editable + * @return assessable_submitted + */ + public static function create_from_submission(\assign $assign, \stdClass $submission, $editable) { + $data = array( + 'context' => $assign->get_context(), + 'objectid' => $submission->id, + 'other' => array( + 'submission_editable' => $editable, + ), + ); + self::$preventcreatecall = false; + /** @var assessable_submitted $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->submission = $submission; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get submission instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_submission() { + if ($this->is_restored()) { + throw new \coding_exception('get_submission() is intended for event observers only'); + } + return $this->submission; + } /** * Returns description of what happened. @@ -96,6 +164,16 @@ protected function init() { $this->data['edulevel'] = self::LEVEL_PARTICIPATING; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $this->set_legacy_logdata('submit for grading', $this->assign->format_submission_for_log($this->submission)); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * @@ -103,6 +181,10 @@ protected function init() { * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call assessable_submitted::create() directly, use assessable_submitted::create_from_submission() instead.'); + } + parent::validate_data(); if (!isset($this->other['submission_editable'])) { diff --git a/mod/assign/classes/event/batch_set_marker_allocation_viewed.php b/mod/assign/classes/event/batch_set_marker_allocation_viewed.php index 92d988708d20a..3b1e65ae6a2fc 100644 --- a/mod/assign/classes/event/batch_set_marker_allocation_viewed.php +++ b/mod/assign/classes/event/batch_set_marker_allocation_viewed.php @@ -34,6 +34,52 @@ defined('MOODLE_INTERNAL') || die(); class batch_set_marker_allocation_viewed extends base { + /** @var \assign */ + protected $assign; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @return batch_set_marker_allocation_viewed + */ + public static function create_from_assign(\assign $assign) { + $data = array( + 'context' => $assign->get_context(), + 'other' => array( + 'assignid' => $assign->get_instance()->id, + ), + ); + self::$preventcreatecall = false; + /** @var batch_set_marker_allocation_viewed $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } /** * Init method. @@ -62,12 +108,28 @@ public function get_description() { {$this->other['assignid']}."; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $logmessage = get_string('viewbatchmarkingallocation', 'assign'); + $this->set_legacy_logdata('view batch set marker allocation', $logmessage); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception + * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call batch_set_marker_allocation_viewed::create() directly, use batch_set_marker_allocation_viewed::create_from_assign() instead.'); + } + parent::validate_data(); if (!isset($this->other['assignid'])) { diff --git a/mod/assign/classes/event/batch_set_workflow_state_viewed.php b/mod/assign/classes/event/batch_set_workflow_state_viewed.php index f4a6fb7f239c8..ccf83db7ac47d 100644 --- a/mod/assign/classes/event/batch_set_workflow_state_viewed.php +++ b/mod/assign/classes/event/batch_set_workflow_state_viewed.php @@ -34,6 +34,52 @@ defined('MOODLE_INTERNAL') || die(); class batch_set_workflow_state_viewed extends base { + /** @var \assign */ + protected $assign; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @return batch_set_workflow_state_viewed + */ + public static function create_from_assign(\assign $assign) { + $data = array( + 'context' => $assign->get_context(), + 'other' => array( + 'assignid' => $assign->get_instance()->id, + ), + ); + self::$preventcreatecall = false; + /** @var batch_set_workflow_state_viewed $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } /** * Init method. @@ -61,12 +107,28 @@ public function get_description() { return "The user with the id {$this->userid} viewed the batch set workflow for the assignment with the id {$this->other['assignid']}."; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $logmessage = get_string('viewbatchsetmarkingworkflowstate', 'assign'); + $this->set_legacy_logdata('view batch set marking workflow state', $logmessage); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception + * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call batch_set_workflow_state_viewed::create() directly, use batch_set_workflow_state_viewed::create_from_assign() instead.'); + } + parent::validate_data(); if (!isset($this->other['assignid'])) { diff --git a/mod/assign/classes/event/course_module_instance_list_viewed.php b/mod/assign/classes/event/course_module_instance_list_viewed.php index badd0135779ad..0509ef655a71f 100644 --- a/mod/assign/classes/event/course_module_instance_list_viewed.php +++ b/mod/assign/classes/event/course_module_instance_list_viewed.php @@ -28,5 +28,18 @@ defined('MOODLE_INTERNAL') || die(); class course_module_instance_list_viewed extends \core\event\course_module_instance_list_viewed { - // No code required here as the parent class handles it all. + /** + * Create the event from course record. + * + * @param \stdClass $course + * @return course_module_instance_list_viewed + */ + public static function create_from_course(\stdClass $course) { + $params = array( + 'context' => \context_course::instance($course->id) + ); + $event = \mod_assign\event\course_module_instance_list_viewed::create($params); + $event->add_record_snapshot('course', $course); + return $event; + } } diff --git a/mod/assign/classes/event/extension_granted.php b/mod/assign/classes/event/extension_granted.php index cd66010e276fb..df56b07c238dc 100644 --- a/mod/assign/classes/event/extension_granted.php +++ b/mod/assign/classes/event/extension_granted.php @@ -35,7 +35,52 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class extension_granted extends base { + /** @var \assign */ + protected $assign; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param int $userid + * @return extension_granted + */ + public static function create_from_assign(\assign $assign, $userid) { + $data = array( + 'context' => $assign->get_context(), + 'objectid' => $assign->get_instance()->id, + 'relateduserid' => $userid, + ); + self::$preventcreatecall = false; + /** @var extension_granted $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + return $event; + } + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } /** * Returns description of what happened. * @@ -65,6 +110,16 @@ protected function init() { $this->data['objecttable'] = 'assign'; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $this->set_legacy_logdata('grant extension', $this->relateduserid); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * @@ -72,7 +127,12 @@ protected function init() { * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call extension_granted::create() directly, use extension_granted::create_from_assign() instead.'); + } + parent::validate_data(); + if (!isset($this->relateduserid)) { throw new \coding_exception('relateduserid is a mandatory property.'); } diff --git a/mod/assign/classes/event/feedback_viewed.php b/mod/assign/classes/event/feedback_viewed.php index 494d63011b22b..a3c2d9b5105ac 100644 --- a/mod/assign/classes/event/feedback_viewed.php +++ b/mod/assign/classes/event/feedback_viewed.php @@ -34,6 +34,74 @@ defined('MOODLE_INTERNAL') || die(); class feedback_viewed extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $grade; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $grade + * @return feedback_viewed + */ + public static function create_from_grade(\assign $assign, \stdClass $grade) { + $data = array( + 'objectid' => $grade->id, + 'relateduserid' => $grade->userid, + 'context' => $assign->get_context(), + 'other' => array( + 'assignid' => $assign->get_instance()->id, + ), + ); + self::$preventcreatecall = false; + /** @var feedback_viewed $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->grade = $grade; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get grade instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_grade() { + if ($this->is_restored()) { + throw new \coding_exception('get_grade() is intended for event observers only'); + } + return $this->grade; + } /** * Init method. @@ -63,12 +131,27 @@ public function get_description() { for the assignment with the id {$this->other['assignid']}."; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $logmessage = get_string('viewfeedbackforuser', 'assign', $this->grade->userid); + $this->set_legacy_logdata('view feedback', $logmessage); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call feedback_viewed::create() directly, use feedback_viewed::create_from_grade() instead.'); + } + parent::validate_data(); if (!isset($this->relateduserid)) { diff --git a/mod/assign/classes/event/grading_form_viewed.php b/mod/assign/classes/event/grading_form_viewed.php index 4a50cd7b8b28a..cd4aa19566f97 100644 --- a/mod/assign/classes/event/grading_form_viewed.php +++ b/mod/assign/classes/event/grading_form_viewed.php @@ -34,6 +34,73 @@ defined('MOODLE_INTERNAL') || die(); class grading_form_viewed extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $user; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $user + * @return grading_form_viewed + */ + public static function create_from_user(\assign $assign, \stdClass $user) { + $data = array( + 'relateduserid' => $user->id, + 'context' => $assign->get_context(), + 'other' => array( + 'assignid' => $assign->get_instance()->id, + ), + ); + self::$preventcreatecall = false; + /** @var grading_form_viewed $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->user = $user; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get user instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_user() { + if ($this->is_restored()) { + throw new \coding_exception('get_user() is intended for event observers only'); + } + return $this->user; + } /** * Init method. @@ -62,12 +129,29 @@ public function get_description() { for the assignment with the id {$this->other['assignid']}."; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $msg = new \lang_string('viewgradingformforstudent', + 'assign', + array('id'=>$this->user->id, 'fullname'=>fullname($this->user))); + $this->set_legacy_logdata('view grading form', $msg); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call grading_form_viewed::create() directly, use grading_form_viewed::create_from_user() instead.'); + } + parent::validate_data(); if (!isset($this->relateduserid)) { diff --git a/mod/assign/classes/event/grading_table_viewed.php b/mod/assign/classes/event/grading_table_viewed.php index 5ccc1edfa7838..ea2644114c8db 100644 --- a/mod/assign/classes/event/grading_table_viewed.php +++ b/mod/assign/classes/event/grading_table_viewed.php @@ -34,6 +34,52 @@ defined('MOODLE_INTERNAL') || die(); class grading_table_viewed extends base { + /** @var \assign */ + protected $assign; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @return grading_table_viewed + */ + public static function create_from_assign(\assign $assign) { + $data = array( + 'context' => $assign->get_context(), + 'other' => array( + 'assignid' => $assign->get_instance()->id, + ), + ); + self::$preventcreatecall = false; + /** @var grading_table_viewed $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } /** * Init method. @@ -61,12 +107,28 @@ public function get_description() { return "The user with the id {$this->userid} viewed the grading table for the assignment with the id {$this->other['assignid']}."; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $logmessage = get_string('viewsubmissiongradingtable', 'assign'); + $this->set_legacy_logdata('view submission grading table', $logmessage); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception + * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call grading_table_viewed::create() directly, use grading_table_viewed::create_from_assign() instead.'); + } + parent::validate_data(); if (!isset($this->other['assignid'])) { diff --git a/mod/assign/classes/event/identities_revealed.php b/mod/assign/classes/event/identities_revealed.php index d140a753e7e2a..06c83e756ad68 100644 --- a/mod/assign/classes/event/identities_revealed.php +++ b/mod/assign/classes/event/identities_revealed.php @@ -35,6 +35,50 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class identities_revealed extends base { + /** @var \assign */ + protected $assign; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @return identities_revealed + */ + public static function create_from_assign(\assign $assign) { + $data = array( + 'context' => $assign->get_context(), + 'objectid' => $assign->get_instance()->id + ); + self::$preventcreatecall = false; + /** @var identities_revealed $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } /** * Returns description of what happened. @@ -64,4 +108,28 @@ protected function init() { $this->data['edulevel'] = self::LEVEL_TEACHING; $this->data['objecttable'] = 'assign'; } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $this->set_legacy_logdata('reveal identities', get_string('revealidentities', 'assign')); + return parent::get_legacy_logdata(); + } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call identities_revealed::create() directly, use identities_revealed::create_from_assign() instead.'); + } + + parent::validate_data(); + } } diff --git a/mod/assign/classes/event/marker_updated.php b/mod/assign/classes/event/marker_updated.php index bc036ab7cca54..ce3e57891a76f 100644 --- a/mod/assign/classes/event/marker_updated.php +++ b/mod/assign/classes/event/marker_updated.php @@ -41,6 +41,94 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class marker_updated extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $user; + /** @var \stdClass */ + protected $marker; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $user + * @param \stdClass $marker + * @return marker_updated + */ + public static function create_from_marker(\assign $assign, \stdClass $user, \stdClass $marker) { + $data = array( + 'context' => $assign->get_context(), + 'objectid' => $assign->get_instance()->id, + 'relateduserid' => $user->id, + 'other' => array( + 'markerid' => $marker->id, + ), + ); + self::$preventcreatecall = false; + /** @var marker_updated $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->user = $user; + $event->marker = $marker; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get user instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_user() { + if ($this->is_restored()) { + throw new \coding_exception('get_user() is intended for event observers only'); + } + return $this->user; + } + + /** + * Get marker user instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_marker() { + if ($this->is_restored()) { + throw new \coding_exception('get_marker() is intended for event observers only'); + } + return $this->marker; + } /** * Returns description of what happened. @@ -71,14 +159,30 @@ protected function init() { $this->data['objecttable'] = 'assign'; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $a = array('id' => $this->user->id, 'fullname' => fullname($this->user), 'marker' => fullname($this->marker)); + $logmessage = get_string('setmarkerallocationforlog', 'assign', $a); + $this->set_legacy_logdata('set marking allocation', $logmessage); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception - * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call marker_updated::create() directly, use marker_updated::create_from_marker() instead.'); + } + parent::validate_data(); + if (!isset($this->other['markerid'])) { throw new \coding_exception('markerid must be set in $other.'); } else if (!isset($this->relateduserid)) { diff --git a/mod/assign/classes/event/reveal_identities_confirmation_page_viewed.php b/mod/assign/classes/event/reveal_identities_confirmation_page_viewed.php index 02ddf91acf472..a6a07dc4db3e5 100644 --- a/mod/assign/classes/event/reveal_identities_confirmation_page_viewed.php +++ b/mod/assign/classes/event/reveal_identities_confirmation_page_viewed.php @@ -34,7 +34,52 @@ defined('MOODLE_INTERNAL') || die(); class reveal_identities_confirmation_page_viewed extends base { + /** @var \assign */ + protected $assign; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @return reveal_identities_confirmation_page_viewed + */ + public static function create_from_assign(\assign $assign) { + $data = array( + 'context' => $assign->get_context(), + 'other' => array( + 'assignid' => $assign->get_instance()->id, + ), + ); + self::$preventcreatecall = false; + /** @var reveal_identities_confirmation_page_viewed $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + return $event; + } + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } /** * Init method. */ @@ -62,12 +107,27 @@ public function get_description() { assignment with the id {$this->other['assignid']}."; } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $this->set_legacy_logdata('view', get_string('viewrevealidentitiesconfirm', 'assign')); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call reveal_identities_confirmation_page_viewed::create() directly, use reveal_identities_confirmation_page_viewed::create_from_grade() instead.'); + } + parent::validate_data(); if (!isset($this->other['assignid'])) { diff --git a/mod/assign/classes/event/statement_accepted.php b/mod/assign/classes/event/statement_accepted.php index 671e2383e5050..53ce11feab5c7 100644 --- a/mod/assign/classes/event/statement_accepted.php +++ b/mod/assign/classes/event/statement_accepted.php @@ -35,6 +35,70 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class statement_accepted extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $submission; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $submission + * @return statement_accepted + */ + public static function create_from_submission(\assign $assign, \stdClass $submission) { + $data = array( + 'context' => $assign->get_context(), + 'objectid' => $submission->id + ); + self::$preventcreatecall = false; + /** @var statement_accepted $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->submission = $submission; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get submission instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_submission() { + if ($this->is_restored()) { + throw new \coding_exception('get_submission() is intended for event observers only'); + } + return $this->submission; + } /** * Returns description of what happened. @@ -64,4 +128,30 @@ protected function init() { $this->data['edulevel'] = self::LEVEL_OTHER; $this->data['objecttable'] = 'assign_submission'; } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + global $USER; + $logmessage = get_string('submissionstatementacceptedlog', 'mod_assign', fullname($USER)); // Nasty hack. + $this->set_legacy_logdata('submission statement accepted', $logmessage); + return parent::get_legacy_logdata(); + } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call statement_accepted::create() directly, use statement_accepted::create_from_submission() instead.'); + } + + parent::validate_data(); + } } diff --git a/mod/assign/classes/event/submission_confirmation_form_viewed.php b/mod/assign/classes/event/submission_confirmation_form_viewed.php index bffe8665da285..6e21faa78e277 100644 --- a/mod/assign/classes/event/submission_confirmation_form_viewed.php +++ b/mod/assign/classes/event/submission_confirmation_form_viewed.php @@ -34,6 +34,52 @@ defined('MOODLE_INTERNAL') || die(); class submission_confirmation_form_viewed extends base { + /** @var \assign */ + protected $assign; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @return submission_confirmation_form_viewed + */ + public static function create_from_assign(\assign $assign) { + $data = array( + 'context' => $assign->get_context(), + 'other' => array( + 'assignid' => $assign->get_instance()->id, + ), + ); + self::$preventcreatecall = false; + /** @var submission_confirmation_form_viewed $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } /** * Init method. @@ -62,12 +108,28 @@ public function get_description() { {$this->other['assignid']}."; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $logmessage = get_string('viewownsubmissionform', 'assign'); + $this->set_legacy_logdata('view confirm submit assignment form', $logmessage); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception + * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call submission_confirmation_form_viewed::create() directly, use submission_confirmation_form_viewed::create_from_assign() instead.'); + } + parent::validate_data(); if (!isset($this->other['assignid'])) { diff --git a/mod/assign/classes/event/submission_duplicated.php b/mod/assign/classes/event/submission_duplicated.php index 819299a1ca823..d03f65a0e15bd 100644 --- a/mod/assign/classes/event/submission_duplicated.php +++ b/mod/assign/classes/event/submission_duplicated.php @@ -35,6 +35,70 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class submission_duplicated extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $submission; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $submission + * @return submission_duplicated + */ + public static function create_from_submission(\assign $assign, \stdClass $submission) { + $data = array( + 'objectid' => $submission->id, + 'context' => $assign->get_context(), + ); + self::$preventcreatecall = false; + /** @var submission_duplicated $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->submission = $submission; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get submission instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_submission() { + if ($this->is_restored()) { + throw new \coding_exception('get_submission() is intended for event observers only'); + } + return $this->submission; + } /** * Returns description of what happened. @@ -64,4 +128,27 @@ protected function init() { $this->data['edulevel'] = self::LEVEL_PARTICIPATING; $this->data['objecttable'] = 'assign_submission'; } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $this->set_legacy_logdata('submissioncopied', $this->assign->format_submission_for_log($this->submission)); + return parent::get_legacy_logdata(); + } + + /** + * Custom validation. + * + * @throws \coding_exception + */ + protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call submission_duplicated::create() directly, use submission_duplicated::create_from_submission() instead.'); + } + + parent::validate_data(); + } } diff --git a/mod/assign/classes/event/submission_form_viewed.php b/mod/assign/classes/event/submission_form_viewed.php index fad7509547ba3..3596dc6f1c655 100644 --- a/mod/assign/classes/event/submission_form_viewed.php +++ b/mod/assign/classes/event/submission_form_viewed.php @@ -34,6 +34,73 @@ defined('MOODLE_INTERNAL') || die(); class submission_form_viewed extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $user; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $user + * @return submission_form_viewed + */ + public static function create_from_user(\assign $assign, \stdClass $user) { + $data = array( + 'relateduserid' => $user->id, + 'context' => $assign->get_context(), + 'other' => array( + 'assignid' => $assign->get_instance()->id, + ), + ); + self::$preventcreatecall = false; + /** @var submission_form_viewed $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->user = $user; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get user instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_user() { + if ($this->is_restored()) { + throw new \coding_exception('get_user() is intended for event observers only'); + } + return $this->user; + } /** * Init method. @@ -67,12 +134,32 @@ public function get_description() { {$this->other['assignid']}."; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + if ($this->relateduserid == $this->userid) { + $title = get_string('editsubmission', 'assign'); + } else { + $name = $this->fullname($this->user); + $title = get_string('editsubmissionother', 'assign', $name); + } + $this->set_legacy_logdata('view submit assignment form', $title); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call submission_form_viewed::create() directly, use submission_form_viewed::create_from_user() instead.'); + } + parent::validate_data(); if (!isset($this->relateduserid)) { diff --git a/mod/assign/classes/event/submission_graded.php b/mod/assign/classes/event/submission_graded.php index 3132db235e5c1..3ddaf943a220f 100644 --- a/mod/assign/classes/event/submission_graded.php +++ b/mod/assign/classes/event/submission_graded.php @@ -35,6 +35,71 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class submission_graded extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $grade; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $grade + * @return submission_graded + */ + public static function create_from_grade(\assign $assign, \stdClass $grade) { + $data = array( + 'context' => $assign->get_context(), + 'objectid' => $grade->id, + 'relateduserid' => $grade->userid + ); + self::$preventcreatecall = false; + /** @var submission_graded $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->grade = $grade; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get grade instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_grade() { + if ($this->is_restored()) { + throw new \coding_exception('get_grade() is intended for event observers only'); + } + return $this->grade; + } /** * Returns description of what happened. @@ -65,6 +130,16 @@ protected function init() { $this->data['objecttable'] = 'assign_grades'; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $this->set_legacy_logdata('grade submission', $this->assign->format_grade_for_log($this->grade)); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * @@ -72,7 +147,12 @@ protected function init() { * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call submission_graded::create() directly, use submission_graded::create_from_grade() instead.'); + } + parent::validate_data(); + if (!isset($this->relateduserid)) { throw new \coding_exception('relateduserid is a mandatory property.'); } diff --git a/mod/assign/classes/event/submission_locked.php b/mod/assign/classes/event/submission_locked.php index cf9183ad88c3a..0f325e57de23d 100644 --- a/mod/assign/classes/event/submission_locked.php +++ b/mod/assign/classes/event/submission_locked.php @@ -35,6 +35,71 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class submission_locked extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $user; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $user + * @return submission_locked + */ + public static function create_from_user(\assign $assign, \stdClass $user) { + $data = array( + 'context' => $assign->get_context(), + 'objectid' => $assign->get_instance()->id, + 'relateduserid' => $user->id, + ); + self::$preventcreatecall = false; + /** @var submission_locked $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->user = $user; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get user instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_user() { + if ($this->is_restored()) { + throw new \coding_exception('get_user() is intended for event observers only'); + } + return $this->user; + } /** * Returns description of what happened. @@ -65,14 +130,29 @@ protected function init() { $this->data['objecttable'] = 'assign'; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $logmessage = get_string('locksubmissionforstudent', 'assign', array('id' => $this->user->id, 'fullname' => fullname($this->user))); + $this->set_legacy_logdata('lock submission', $logmessage); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception - * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call submission_locked::create() directly, use submission_locked::create_from_user() instead.'); + } + parent::validate_data(); + if (!isset($this->relateduserid)) { throw new \coding_exception('relateduserid is a mandatory property.'); } diff --git a/mod/assign/classes/event/submission_status_updated.php b/mod/assign/classes/event/submission_status_updated.php index 58290cc3f2ea6..0dca34536fc1a 100644 --- a/mod/assign/classes/event/submission_status_updated.php +++ b/mod/assign/classes/event/submission_status_updated.php @@ -41,6 +41,74 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class submission_status_updated extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $submission; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $submission + * @return submission_status_updated + */ + public static function create_from_submission(\assign $assign, \stdClass $submission) { + $data = array( + 'context' => $assign->get_context(), + 'objectid' => $submission->id, + 'relateduserid' => ($assign->get_instance()->teamsubmission) ? null : $submission->userid, + 'other' => array( + 'newstatus' => $submission->status + ) + ); + self::$preventcreatecall = false; + /** @var submission_status_updated $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->submission = $submission; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get submission instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_submission() { + if ($this->is_restored()) { + throw new \coding_exception('get_submission() is intended for event observers only'); + } + return $this->submission; + } /** * Returns description of what happened. @@ -71,14 +139,32 @@ protected function init() { $this->data['objecttable'] = 'assign_submission'; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + global $DB; + + $user = $DB->get_record('user', array('id' => $this->submission->userid), '*', MUST_EXIST); + $logmessage = get_string('reverttodraftforstudent', 'assign', array('id' => $user->id, 'fullname' => fullname($user))); + $this->set_legacy_logdata('revert submission to draft', $logmessage); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception - * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call submission_status_updated::create() directly, use submission_status_updated::create_from_submission() instead.'); + } + parent::validate_data(); + if (!isset($this->other['newstatus'])) { throw new \coding_exception('newstatus must be set in $other.'); } diff --git a/mod/assign/classes/event/submission_status_viewed.php b/mod/assign/classes/event/submission_status_viewed.php index 011e8126b0f01..20e34cc155f1e 100644 --- a/mod/assign/classes/event/submission_status_viewed.php +++ b/mod/assign/classes/event/submission_status_viewed.php @@ -34,6 +34,52 @@ defined('MOODLE_INTERNAL') || die(); class submission_status_viewed extends base { + /** @var \assign */ + protected $assign; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @return submission_status_viewed + */ + public static function create_from_assign(\assign $assign) { + $data = array( + 'context' => $assign->get_context(), + 'other' => array( + 'assignid' => $assign->get_instance()->id, + ), + ); + self::$preventcreatecall = false; + /** @var submission_status_viewed $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } /** * Init method. @@ -64,12 +110,27 @@ public function get_description() { the id {$this->other['assignid']}."; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $this->set_legacy_logdata('view', get_string('viewownsubmissionstatus', 'assign')); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception + * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call submission_status_viewed::create() directly, use submission_status_viewed::create_from_assign() instead.'); + } + parent::validate_data(); if (!isset($this->other['assignid'])) { diff --git a/mod/assign/classes/event/submission_unlocked.php b/mod/assign/classes/event/submission_unlocked.php index 83d1c767eb8a5..f18e21c8ddf35 100644 --- a/mod/assign/classes/event/submission_unlocked.php +++ b/mod/assign/classes/event/submission_unlocked.php @@ -35,6 +35,71 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class submission_unlocked extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $user; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $user + * @return submission_unlocked + */ + public static function create_from_user(\assign $assign, \stdClass $user) { + $data = array( + 'context' => $assign->get_context(), + 'objectid' => $assign->get_instance()->id, + 'relateduserid' => $user->id, + ); + self::$preventcreatecall = false; + /** @var submission_unlocked $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->user = $user; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get user instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_user() { + if ($this->is_restored()) { + throw new \coding_exception('get_user() is intended for event observers only'); + } + return $this->user; + } /** * Returns description of what happened. @@ -65,14 +130,29 @@ protected function init() { $this->data['objecttable'] = 'assign'; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $logmessage = get_string('unlocksubmissionforstudent', 'assign', array('id' => $this->user->id, 'fullname' => fullname($this->user))); + $this->set_legacy_logdata('unlock submission', $logmessage); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception - * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call submission_unlocked::create() directly, use submission_unlocked::create_from_user() instead.'); + } + parent::validate_data(); + if (!isset($this->relateduserid)) { throw new \coding_exception('relateduserid is a mandatory property.'); } diff --git a/mod/assign/classes/event/submission_viewed.php b/mod/assign/classes/event/submission_viewed.php index da0a1c5456119..4aec8218e3458 100644 --- a/mod/assign/classes/event/submission_viewed.php +++ b/mod/assign/classes/event/submission_viewed.php @@ -34,6 +34,74 @@ defined('MOODLE_INTERNAL') || die(); class submission_viewed extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $submission; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $submission + * @return submission_viewed + */ + public static function create_from_submission(\assign $assign, \stdClass $submission) { + $data = array( + 'objectid' => $submission->id, + 'relateduserid' => $submission->userid, + 'context' => $assign->get_context(), + 'other' => array( + 'assignid' => $assign->get_instance()->id, + ), + ); + self::$preventcreatecall = false; + /** @var submission_viewed $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->submission = $submission; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get submission instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_submission() { + if ($this->is_restored()) { + throw new \coding_exception('get_submission() is intended for event observers only'); + } + return $this->submission; + } /** * Init method. @@ -63,12 +131,27 @@ public function get_description() { assignment with the id {$this->other['assignid']}."; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $logmessage = get_string('viewsubmissionforuser', 'assign', $this->submission->userid); + $this->set_legacy_logdata('view submission', $logmessage); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call submission_viewed::create() directly, use submission_viewed::create_from_submission() instead.'); + } + parent::validate_data(); if (!isset($this->relateduserid)) { diff --git a/mod/assign/classes/event/workflow_state_updated.php b/mod/assign/classes/event/workflow_state_updated.php index 1bd24f1323865..6e3a8ad02ee30 100644 --- a/mod/assign/classes/event/workflow_state_updated.php +++ b/mod/assign/classes/event/workflow_state_updated.php @@ -41,6 +41,75 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class workflow_state_updated extends base { + /** @var \assign */ + protected $assign; + /** @var \stdClass */ + protected $user; + /** + * Flag for prevention of direct create() call. + * @var bool + */ + protected static $preventcreatecall = true; + + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \assign $assign + * @param \stdClass $user + * @param string $state + * @return workflow_state_updated + */ + public static function create_from_user(\assign $assign, \stdClass $user, $state) { + $data = array( + 'context' => $assign->get_context(), + 'objectid' => $assign->get_instance()->id, + 'relateduserid' => $user->id, + 'other' => array( + 'newstate' => $state, + ), + ); + self::$preventcreatecall = false; + /** @var workflow_state_updated $event */ + $event = self::create($data); + self::$preventcreatecall = true; + $event->assign = $assign; + $event->user = $user; + return $event; + } + + /** + * Get assign instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \assign + */ + public function get_assign() { + if ($this->is_restored()) { + throw new \coding_exception('get_assign() is intended for event observers only'); + } + return $this->assign; + } + + /** + * Get user instance. + * + * NOTE: to be used from observers only. + * + * @since Moodle 2.7 + * + * @return \stdClass + */ + public function get_user() { + if ($this->is_restored()) { + throw new \coding_exception('get_user() is intended for event observers only'); + } + return $this->user; + } /** * Returns description of what happened. @@ -71,14 +140,30 @@ protected function init() { $this->data['objecttable'] = 'assign'; } + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + $a = array('id' => $this->user->id, 'fullname' => fullname($this->user), 'state' => $this->other['newstate']); + $logmessage = get_string('setmarkingworkflowstateforlog', 'assign', $a); + $this->set_legacy_logdata('set marking workflow state', $logmessage); + return parent::get_legacy_logdata(); + } + /** * Custom validation. * * @throws \coding_exception - * @return void */ protected function validate_data() { + if (self::$preventcreatecall) { + throw new \coding_exception('cannot call workflow_state_updated::create() directly, use workflow_state_updated::create_from_user() instead.'); + } + parent::validate_data(); + if (!isset($this->other['newstate'])) { throw new \coding_exception('newstate must be set in $other.'); } else if (!isset($this->relateduserid)) { diff --git a/mod/assign/index.php b/mod/assign/index.php index 5971ec682832e..1151ddf36549a 100644 --- a/mod/assign/index.php +++ b/mod/assign/index.php @@ -32,12 +32,7 @@ $PAGE->set_url('/mod/assign/index.php', array('id' => $id)); $PAGE->set_pagelayout('incourse'); -$params = array( - 'context' => context_course::instance($course->id) -); -$event = \mod_assign\event\course_module_instance_list_viewed::create($params); -$event->add_record_snapshot('course', $course); -$event->trigger(); +\mod_assign\event\course_module_instance_list_viewed::create_from_course($course)->trigger(); // Print the header. $strplural = get_string("modulenameplural", "assign"); diff --git a/mod/assign/locallib.php b/mod/assign/locallib.php index 48fbd93fe46c9..6497f5cb9eaed 100644 --- a/mod/assign/locallib.php +++ b/mod/assign/locallib.php @@ -1837,15 +1837,7 @@ public function update_grade($grade) { if ($result) { $this->gradebook_item_update(null, $grade); - - $params = array( - 'context' => $this->context, - 'objectid' => $grade->id, - 'relateduserid' => $grade->userid - ); - $event = \mod_assign\event\submission_graded::create($params); - $event->set_legacy_logdata('grade submission', $this->format_grade_for_log($grade)); - $event->trigger(); + \mod_assign\event\submission_graded::create_from_grade($this, $grade)->trigger(); } return $result; } @@ -2187,17 +2179,8 @@ protected function view_plugin_content($pluginsubtype) { $this->get_return_params())); // Trigger event for viewing a submission. - $logmessage = new lang_string('viewsubmissionforuser', 'assign', $item->userid); - $event = \mod_assign\event\submission_viewed::create(array( - 'objectid' => $item->id, - 'relateduserid' => $item->userid, - 'context' => $this->get_context(), - 'other' => array( - 'assignid' => $this->get_instance()->id - ) - )); - $event->set_legacy_logdata('view submission', $logmessage); - $event->trigger(); + \mod_assign\event\submission_viewed::create_from_submission($this, $item)->trigger(); + } else { $plugin = $this->get_feedback_plugin_by_type($plugintype); if ($gradeid <= 0) { @@ -2219,17 +2202,7 @@ protected function view_plugin_content($pluginsubtype) { $this->get_return_params())); // Trigger event for viewing feedback. - $logmessage = new lang_string('viewfeedbackforuser', 'assign', $item->userid); - $event = \mod_assign\event\feedback_viewed::create(array( - 'objectid' => $item->id, - 'relateduserid' => $item->userid, - 'context' => $this->get_context(), - 'other' => array( - 'assignid' => $this->get_instance()->id - ) - )); - $event->set_legacy_logdata('view feedback', $logmessage); - $event->trigger(); + \mod_assign\event\feedback_viewed::create_from_grade($this, $item)->trigger(); } $o .= $this->view_return_links(); @@ -2534,13 +2507,7 @@ protected function download_submissions() { $result .= $this->get_renderer()->continue_button($url); $result .= $this->view_footer(); } else if ($zipfile = $this->pack_files($filesforzipping)) { - $params = array( - 'context' => $this->context, - 'objectid' => $this->get_instance()->id - ); - $event = \mod_assign\event\all_submissions_downloaded::create($params); - $event->set_legacy_logdata('download all submissions', new lang_string('downloadall', 'assign')); - $event->trigger(); + \mod_assign\event\all_submissions_downloaded::create_from_assign($this)->trigger(); // Send file and delete after sending. send_temp_file($zipfile, $filename); // We will not get here - send_temp_file calls exit. @@ -2958,18 +2925,7 @@ protected function view_single_grade_page($mform) { $o .= $this->get_renderer()->render($history); } - $msg = new lang_string('viewgradingformforstudent', - 'assign', - array('id'=>$user->id, 'fullname'=>fullname($user))); - $event = \mod_assign\event\grading_form_viewed::create(array( - 'relateduserid' => $user->id, - 'context' => $this->get_context(), - 'other' => array( - 'assignid' => $this->get_instance()->id - ) - )); - $event->set_legacy_logdata('view grading form', $msg); - $event->trigger(); + \mod_assign\event\grading_form_viewed::create_from_user($this, $user)->trigger(); $o .= $this->view_footer(); return $o; @@ -3004,14 +2960,7 @@ protected function view_reveal_identities_confirm() { $cancelurl); $o .= $this->view_footer(); - $event = \mod_assign\event\reveal_identities_confirmation_page_viewed::create(array( - 'context' => $this->get_context(), - 'other' => array( - 'assignid' => $this->get_instance()->id - ) - )); - $event->set_legacy_logdata('view', new lang_string('viewrevealidentitiesconfirm', 'assign')); - $event->trigger(); + \mod_assign\event\reveal_identities_confirmation_page_viewed::create_from_assign($this)->trigger(); return $o; } @@ -3230,15 +3179,7 @@ protected function view_grading_page() { $o .= $this->view_footer(); - $logmessage = new lang_string('viewsubmissiongradingtable', 'assign'); - $event = \mod_assign\event\grading_table_viewed::create(array( - 'context' => $this->get_context(), - 'other' => array( - 'assignid' => $this->get_instance()->id - ) - )); - $event->set_legacy_logdata('view submission grading table', $logmessage); - $event->trigger(); + \mod_assign\event\grading_table_viewed::create_from_assign($this)->trigger(); return $o; } @@ -3246,7 +3187,7 @@ protected function view_grading_page() { /** * Capture the output of the plagiarism plugins disclosures and return it as a string. * - * @return void + * @return string */ protected function plagiarism_print_disclosure() { global $CFG; @@ -3366,15 +3307,7 @@ protected function view_edit_submission_page($mform, $notices) { $o .= $this->view_footer(); - $event = \mod_assign\event\submission_form_viewed::create(array( - 'relateduserid' => $userid, - 'context' => $this->get_context(), - 'other' => array( - 'assignid' => $this->get_instance()->id - ) - )); - $event->set_legacy_logdata('view submit assignment form', $title); - $event->trigger(); + \mod_assign\event\submission_form_viewed::create_from_user($this, $user)->trigger(); return $o; } @@ -3593,15 +3526,7 @@ protected function view_batch_set_workflow_state($mform) { $o .= $this->get_renderer()->render(new assign_form('setworkflowstate', $mform)); $o .= $this->view_footer(); - $logmessage = new lang_string('viewbatchsetmarkingworkflowstate', 'assign'); - $event = \mod_assign\event\batch_set_workflow_state_viewed::create(array( - 'context' => $this->get_context(), - 'other' => array( - 'assignid' => $this->get_instance()->id - ) - )); - $event->set_legacy_logdata('view batch set marking workflow state', $logmessage); - $event->trigger(); + \mod_assign\event\batch_set_workflow_state_viewed::create_from_assign($this)->trigger(); return $o; } @@ -3663,15 +3588,7 @@ public function view_batch_markingallocation($mform) { $o .= $this->get_renderer()->render(new assign_form('setworkflowstate', $mform)); $o .= $this->view_footer(); - $logmessage = new lang_string('viewbatchmarkingallocation', 'assign'); - $event = \mod_assign\event\batch_set_marker_allocation_viewed::create(array( - 'context' => $this->get_context(), - 'other' => array( - 'assignid' => $this->get_instance()->id - ) - )); - $event->set_legacy_logdata('view batch set marker allocation', $logmessage); - $event->trigger(); + \mod_assign\event\batch_set_marker_allocation_viewed::create_from_assign($this)->trigger(); return $o; } @@ -3724,15 +3641,7 @@ protected function check_submit_for_grading($mform) { $o .= $this->get_renderer()->render($submitforgradingpage); $o .= $this->view_footer(); - $logmessage = new lang_string('viewownsubmissionform', 'assign'); - $event = \mod_assign\event\submission_confirmation_form_viewed::create(array( - 'context' => $this->get_context(), - 'other' => array( - 'assignid' => $this->get_instance()->id - ) - )); - $event->set_legacy_logdata('view confirm submit assignment form', $logmessage); - $event->trigger(); + \mod_assign\event\submission_confirmation_form_viewed::create_from_assign($this)->trigger(); return $o; } @@ -4119,15 +4028,7 @@ protected function view_submission_page() { $o .= $this->view_footer(); - $params = array( - 'context' => $this->context, - 'other' => array( - 'assignid' => $this->get_instance()->id - ) - ); - $event = \mod_assign\event\submission_status_viewed::create($params); - $event->set_legacy_logdata('view', new lang_string('viewownsubmissionstatus', 'assign')); - $event->trigger(); + \mod_assign\event\submission_status_viewed::create_from_assign($this)->trigger(); return $o; } @@ -4861,31 +4762,13 @@ public function submit_for_grading($data, $notices) { } if (!empty($data->submissionstatement) && $USER->id == $userid) { - $logmessage = get_string('submissionstatementacceptedlog', - 'mod_assign', - fullname($USER)); - $params = array( - 'context' => $this->context, - 'objectid' => $submission->id - ); - $event = \mod_assign\event\statement_accepted::create($params); - $event->set_legacy_logdata('submission statement accepted', $logmessage); - $event->trigger(); + \mod_assign\event\statement_accepted::create_from_submission($this, $submission)->trigger(); } $this->notify_graders($submission); $this->notify_student_submission_receipt($submission); - // Trigger assessable_submitted event on submission. - $params = array( - 'context' => context_module::instance($this->get_course_module()->id), - 'objectid' => $submission->id, - 'other' => array( - 'submission_editable' => false - ) - ); - $event = \mod_assign\event\assessable_submitted::create($params); - $event->set_legacy_logdata('submit for grading', $this->format_submission_for_log($submission)); - $event->trigger(); + \mod_assign\event\assessable_submitted::create_from_submission($this, $submission, false)->trigger(); + return true; } $notices[] = get_string('submissionsclosed', 'assign'); @@ -4995,14 +4878,7 @@ public function save_user_extension($userid, $extensionduedate) { $result = $this->update_user_flags($flags); if ($result) { - $params = array( - 'context' => $this->context, - 'objectid' => $flags->assignment, - 'relateduserid' => $userid - ); - $event = \mod_assign\event\extension_granted::create($params); - $event->set_legacy_logdata('grant extension', $userid); - $event->trigger(); + \mod_assign\event\extension_granted::create_from_assign($this, $userid)->trigger(); } return $result; } @@ -5306,13 +5182,7 @@ public function reveal_identities() { $this->gradebook_item_update(null, $grade); } - $params = array( - 'context' => $this->context, - 'objectid' => $update->id - ); - $event = \mod_assign\event\identities_revealed::create($params); - $event->set_legacy_logdata('reveal identities', new lang_string('revealidentities', 'assign')); - $event->trigger(); + \mod_assign\event\identities_revealed::create_from_assign($this)->trigger(); } /** @@ -5437,10 +5307,12 @@ public function format_grade_for_log(stdClass $grade) { * The size limit for the log file is 255 characters, so be careful not * to include too much information. * + * @deprecated since 2.7 + * * @param stdClass $submission * @return string */ - protected function format_submission_for_log(stdClass $submission) { + public function format_submission_for_log(stdClass $submission) { global $DB; $info = ''; @@ -5538,13 +5410,7 @@ public function copy_previous_attempt(&$notices) { return false; } - $params = array( - 'context' => $this->context, - 'objectid' => $submission->id - ); - $event = \mod_assign\event\submission_duplicated::create($params); - $event->set_legacy_logdata('submissioncopied', $this->format_submission_for_log($submission)); - $event->trigger(); + \mod_assign\event\submission_duplicated::create_from_submission($this, $submission)->trigger(); $complete = COMPLETION_INCOMPLETE; if ($submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED) { @@ -5561,20 +5427,11 @@ public function copy_previous_attempt(&$notices) { // is clear that the submission was copied. $this->notify_student_submission_copied($submission); $this->notify_graders($submission); - // Trigger assessable_submitted event on submission. + // The same logic applies here - we could not notify teachers, // but then they would wonder why there are submitted assignments // and they haven't been notified. - $params = array( - 'context' => context_module::instance($this->get_course_module()->id), - 'objectid' => $submission->id, - 'other' => array( - 'submission_editable' => true - ) - ); - $event = \mod_assign\event\assessable_submitted::create($params); - $event->set_legacy_logdata('submit for grading', $this->format_submission_for_log($submission)); - $event->trigger(); + \mod_assign\event\assessable_submitted::create_from_submission($this, $submission, true)->trigger(); } return true; } @@ -5666,14 +5523,7 @@ public function save_submission(stdClass $data, & $notices) { // Logging. if (isset($data->submissionstatement) && ($userid == $USER->id)) { - $logmessage = new lang_string('submissionstatementacceptedlog', 'mod_assign', fullname($USER)); - $params = array( - 'context' => $this->context, - 'objectid' => $submission->id - ); - $event = \mod_assign\event\statement_accepted::create($params); - $event->set_legacy_logdata('submission statement accepted', $logmessage); - $event->trigger(); + \mod_assign\event\statement_accepted::create_from_submission($this, $submission)->trigger(); } $complete = COMPLETION_INCOMPLETE; @@ -5688,17 +5538,7 @@ public function save_submission(stdClass $data, & $notices) { if (!$instance->submissiondrafts) { $this->notify_student_submission_receipt($submission); $this->notify_graders($submission); - // Trigger assessable_submitted event on submission. - $params = array( - 'context' => context_module::instance($this->get_course_module()->id), - 'objectid' => $submission->id, - 'other' => array( - 'submission_editable' => true - ) - ); - $event = \mod_assign\event\assessable_submitted::create($params); - $event->set_legacy_logdata('submit for grading', $this->format_submission_for_log($submission)); - $event->trigger(); + \mod_assign\event\assessable_submitted::create_from_submission($this, $submission, true)->trigger(); } return true; } @@ -6208,26 +6048,12 @@ public function revert_to_draft($userid) { $grade->grader = $USER->id; $this->update_grade($grade); - $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); - $completion = new completion_info($this->get_course()); if ($completion->is_enabled($this->get_course_module()) && $this->get_instance()->completionsubmit) { $completion->update_state($this->get_course_module(), COMPLETION_INCOMPLETE, $userid); } - $logmessage = new lang_string('reverttodraftforstudent', 'assign', array('id' => $user->id, - 'fullname' => fullname($user))); - $params = array( - 'context' => $this->context, - 'objectid' => $submission->id, - 'relateduserid' => ($this->get_instance()->teamsubmission) ? null : $userid, - 'other' => array( - 'newstatus' => $submission->status - ) - ); - $event = \mod_assign\event\submission_status_updated::create($params); - $event->set_legacy_logdata('revert submission to draft', $logmessage); - $event->trigger(); + \mod_assign\event\submission_status_updated::create_from_submission($this, $submission)->trigger(); return true; } @@ -6274,17 +6100,7 @@ public function lock_submission($userid) { } $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); - - $logmessage = new lang_string('locksubmissionforstudent', 'assign', array('id' => $user->id, - 'fullname' => fullname($user))); - $params = array( - 'context' => $this->context, - 'objectid' => $flags->assignment, - 'relateduserid' => $user->id - ); - $event = \mod_assign\event\submission_locked::create($params); - $event->set_legacy_logdata('lock submission', $logmessage); - $event->trigger(); + \mod_assign\event\submission_locked::create_from_user($this, $user)->trigger(); return true; } @@ -6322,22 +6138,7 @@ protected function process_set_batch_marking_workflow_state() { } $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); - - $params = array('id'=>$user->id, - 'fullname'=>fullname($user), - 'state'=>$state); - $message = new lang_string('setmarkingworkflowstateforlog', 'assign', $params); - $params = array( - 'context' => $this->context, - 'objectid' => $this->get_instance()->id, - 'relateduserid' => $userid, - 'other' => array( - 'newstate' => $state - ) - ); - $event = \mod_assign\event\workflow_state_updated::create($params); - $event->set_legacy_logdata('set marking workflow state', $message); - $event->trigger(); + \mod_assign\event\workflow_state_updated::create_from_user($this, $user, $state)->trigger(); } } } @@ -6372,24 +6173,8 @@ protected function process_set_batch_marking_allocation() { $flags->allocatedmarker = $marker->id; if ($this->update_user_flags($flags)) { - $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); - - $params = array('id'=>$user->id, - 'fullname'=>fullname($user), - 'marker'=>fullname($marker)); - $message = new lang_string('setmarkerallocationforlog', 'assign', $params); - $params = array( - 'context' => $this->context, - 'objectid' => $this->get_instance()->id, - 'relateduserid' => $userid, - 'other' => array( - 'markerid' => $marker->id - ) - ); - $event = \mod_assign\event\marker_updated::create($params); - $event->set_legacy_logdata('set marking allocation', $message); - $event->trigger(); + \mod_assign\event\marker_updated::create_from_marker($this, $user, $marker)->trigger(); } } } @@ -6440,17 +6225,7 @@ public function unlock_submission($userid) { } $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); - - $logmessage = new lang_string('unlocksubmissionforstudent', 'assign', array('id' => $user->id, - 'fullname' => fullname($user))); - $params = array( - 'context' => $this->context, - 'objectid' => $flags->assignment, - 'relateduserid' => $user->id - ); - $event = \mod_assign\event\submission_unlocked::create($params); - $event->set_legacy_logdata('unlock submission', $logmessage); - $event->trigger(); + \mod_assign\event\submission_unlocked::create_from_user($this, $user)->trigger(); return true; }