diff --git a/mod/assign/classes/event/assessable_submitted.php b/mod/assign/classes/event/assessable_submitted.php index 362490ba22292..09f8fe45325cd 100644 --- a/mod/assign/classes/event/assessable_submitted.php +++ b/mod/assign/classes/event/assessable_submitted.php @@ -41,14 +41,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class assessable_submitted extends base { - /** @var \stdClass */ - protected $submission; - /** - * Flag for prevention of direct create() call. - * @var bool - */ - protected static $preventcreatecall = true; - /** * Create instance of event. * @@ -67,31 +59,13 @@ public static function create_from_submission(\assign $assign, \stdClass $submis 'submission_editable' => $editable, ), ); - self::$preventcreatecall = false; /** @var assessable_submitted $event */ $event = self::create($data); - self::$preventcreatecall = true; $event->set_assign($assign); - $event->submission = $submission; + $event->add_record_snapshot('assign_submission', $submission); return $event; } - /** - * 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. * @@ -152,7 +126,8 @@ protected function init() { * @return array */ protected function get_legacy_logdata() { - $this->set_legacy_logdata('submit for grading', $this->assign->format_submission_for_log($this->submission)); + $submission = $this->get_record_snapshot('assign_submission', $this->objectid); + $this->set_legacy_logdata('submit for grading', $this->assign->format_submission_for_log($submission)); return parent::get_legacy_logdata(); } @@ -163,10 +138,6 @@ protected function get_legacy_logdata() { * @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/feedback_updated.php b/mod/assign/classes/event/feedback_updated.php index 9b9c5ec6cbcf3..2bab6fe3d525a 100644 --- a/mod/assign/classes/event/feedback_updated.php +++ b/mod/assign/classes/event/feedback_updated.php @@ -41,6 +41,28 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class feedback_updated extends base { + /** + * Create instance of event. + * + * @param \assign $assign + * @param \stdClass $grade + * @return feedback_updated + */ + 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, + ), + ); + /** @var feedback_updated $event */ + $event = self::create($data); + $event->set_assign($assign); + $event->add_record_snapshot('assign_grades', $grade); + return $event; + } /** * Init method. diff --git a/mod/assign/classes/event/feedback_viewed.php b/mod/assign/classes/event/feedback_viewed.php index 9166c9d54aa95..18fdec31b2c47 100644 --- a/mod/assign/classes/event/feedback_viewed.php +++ b/mod/assign/classes/event/feedback_viewed.php @@ -41,14 +41,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class feedback_viewed extends base { - /** @var \stdClass */ - protected $grade; - /** - * Flag for prevention of direct create() call. - * @var bool - */ - protected static $preventcreatecall = true; - /** * Create instance of event. * @@ -65,29 +57,13 @@ public static function create_from_grade(\assign $assign, \stdClass $grade) { 'assignid' => $assign->get_instance()->id, ), ); - self::$preventcreatecall = false; /** @var feedback_viewed $event */ $event = self::create($data); - self::$preventcreatecall = true; $event->set_assign($assign); - $event->grade = $grade; + $event->add_record_snapshot('assign_grades', $grade); return $event; } - /** - * Get grade instance. - * - * NOTE: to be used from observers only. - * - * @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. */ @@ -122,7 +98,7 @@ public function get_description() { * @return array */ protected function get_legacy_logdata() { - $logmessage = get_string('viewfeedbackforuser', 'assign', $this->grade->userid); + $logmessage = get_string('viewfeedbackforuser', 'assign', $this->relateduserid); $this->set_legacy_logdata('view feedback', $logmessage); return parent::get_legacy_logdata(); } @@ -133,10 +109,6 @@ protected function get_legacy_logdata() { * @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/statement_accepted.php b/mod/assign/classes/event/statement_accepted.php index f18ee0a71707a..f9b452f08bec5 100644 --- a/mod/assign/classes/event/statement_accepted.php +++ b/mod/assign/classes/event/statement_accepted.php @@ -35,8 +35,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class statement_accepted extends base { - /** @var \stdClass */ - protected $submission; /** * Flag for prevention of direct create() call. * @var bool @@ -62,26 +60,10 @@ public static function create_from_submission(\assign $assign, \stdClass $submis $event = self::create($data); self::$preventcreatecall = true; $event->set_assign($assign); - $event->submission = $submission; + $event->add_record_snapshot('assign_submission', $submission); return $event; } - /** - * 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. * diff --git a/mod/assign/classes/event/submission_duplicated.php b/mod/assign/classes/event/submission_duplicated.php index 0ed8cb59ac8e0..894ac486fb69b 100644 --- a/mod/assign/classes/event/submission_duplicated.php +++ b/mod/assign/classes/event/submission_duplicated.php @@ -35,8 +35,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class submission_duplicated extends base { - /** @var \stdClass */ - protected $submission; /** * Flag for prevention of direct create() call. * @var bool @@ -62,26 +60,10 @@ public static function create_from_submission(\assign $assign, \stdClass $submis $event = self::create($data); self::$preventcreatecall = true; $event->set_assign($assign); - $event->submission = $submission; + $event->add_record_snapshot('assign_submission', $submission); return $event; } - /** - * 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. * @@ -117,7 +99,8 @@ protected function init() { * @return array */ protected function get_legacy_logdata() { - $this->set_legacy_logdata('submissioncopied', $this->assign->format_submission_for_log($this->submission)); + $submission = $this->get_record_snapshot('assign_submission', $this->objectid); + $this->set_legacy_logdata('submissioncopied', $this->assign->format_submission_for_log($submission)); return parent::get_legacy_logdata(); } diff --git a/mod/assign/classes/event/submission_graded.php b/mod/assign/classes/event/submission_graded.php index d30bdc9917c5f..5f04c83342d5f 100644 --- a/mod/assign/classes/event/submission_graded.php +++ b/mod/assign/classes/event/submission_graded.php @@ -35,8 +35,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class submission_graded extends base { - /** @var \stdClass */ - protected $grade; /** * Flag for prevention of direct create() call. * @var bool @@ -63,26 +61,10 @@ public static function create_from_grade(\assign $assign, \stdClass $grade) { $event = self::create($data); self::$preventcreatecall = true; $event->set_assign($assign); - $event->grade = $grade; + $event->add_record_snapshot('assign_grades', $grade); return $event; } - /** - * 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. * @@ -118,7 +100,8 @@ protected function init() { * @return array */ protected function get_legacy_logdata() { - $this->set_legacy_logdata('grade submission', $this->assign->format_grade_for_log($this->grade)); + $grade = $this->get_record_snapshot('assign_grades', $this->objectid); + $this->set_legacy_logdata('grade submission', $this->assign->format_grade_for_log($grade)); return parent::get_legacy_logdata(); } diff --git a/mod/assign/classes/event/submission_status_updated.php b/mod/assign/classes/event/submission_status_updated.php index 2f27a298493dd..4d1b2078b3842 100644 --- a/mod/assign/classes/event/submission_status_updated.php +++ b/mod/assign/classes/event/submission_status_updated.php @@ -41,14 +41,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class submission_status_updated extends base { - /** @var \stdClass */ - protected $submission; - /** - * Flag for prevention of direct create() call. - * @var bool - */ - protected static $preventcreatecall = true; - /** * Create instance of event. * @@ -67,31 +59,13 @@ public static function create_from_submission(\assign $assign, \stdClass $submis 'newstatus' => $submission->status ) ); - self::$preventcreatecall = false; /** @var submission_status_updated $event */ $event = self::create($data); - self::$preventcreatecall = true; $event->set_assign($assign); - $event->submission = $submission; + $event->add_record_snapshot('assign_submission', $submission); return $event; } - /** - * 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. * @@ -127,9 +101,8 @@ protected function init() { * @return array */ protected function get_legacy_logdata() { - global $DB; - - $user = $DB->get_record('user', array('id' => $this->submission->userid), '*', MUST_EXIST); + $submission = $this->get_record_snapshot('assign_submission', $this->objectid); + $user = $this->get_record_snapshot('user', $submission->userid); $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(); @@ -141,10 +114,6 @@ protected function get_legacy_logdata() { * @throws \coding_exception */ 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'])) { diff --git a/mod/assign/classes/event/submission_viewed.php b/mod/assign/classes/event/submission_viewed.php index 31cb225060181..b19723085e35e 100644 --- a/mod/assign/classes/event/submission_viewed.php +++ b/mod/assign/classes/event/submission_viewed.php @@ -41,14 +41,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class submission_viewed extends base { - /** @var \stdClass */ - protected $submission; - /** - * Flag for prevention of direct create() call. - * @var bool - */ - protected static $preventcreatecall = true; - /** * Create instance of event. * @@ -65,29 +57,13 @@ public static function create_from_submission(\assign $assign, \stdClass $submis 'assignid' => $assign->get_instance()->id, ), ); - self::$preventcreatecall = false; /** @var submission_viewed $event */ $event = self::create($data); - self::$preventcreatecall = true; $event->set_assign($assign); - $event->submission = $submission; + $event->add_record_snapshot('assign_submission', $submission); return $event; } - /** - * Get submission instance. - * - * NOTE: to be used from observers only. - * - * @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. */ @@ -122,7 +98,7 @@ public function get_description() { * @return array */ protected function get_legacy_logdata() { - $logmessage = get_string('viewsubmissionforuser', 'assign', $this->submission->userid); + $logmessage = get_string('viewsubmissionforuser', 'assign', $this->relateduserid); $this->set_legacy_logdata('view submission', $logmessage); return parent::get_legacy_logdata(); } @@ -133,10 +109,6 @@ protected function get_legacy_logdata() { * @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/feedback/offline/locallib.php b/mod/assign/feedback/offline/locallib.php index a16448bd5441c..8845e3cc26496 100644 --- a/mod/assign/feedback/offline/locallib.php +++ b/mod/assign/feedback/offline/locallib.php @@ -194,13 +194,7 @@ public function process_import_grades($draftid, $importid, $ignoremodified) { 'text'=>$newvalue)); // Trigger event for updating the feedback. - $event = \mod_assign\event\feedback_updated::create(array( - 'relateduserid' => $user->id, - 'context' => $this->assignment->get_context(), - 'other' => array( - 'assignid' => $this->assignment->get_instance()->id - ) - )); + $event = \mod_assign\event\feedback_updated::create_from_grade($this->assignment, $grade); $event->set_legacy_logdata('save grading feedback', $logdesc); $event->trigger(); } diff --git a/mod/assign/locallib.php b/mod/assign/locallib.php index d2af9fe7e57fb..75239c0acb51e 100644 --- a/mod/assign/locallib.php +++ b/mod/assign/locallib.php @@ -2012,8 +2012,7 @@ public function get_group_submission($userid, $groupid, $create, $attemptnumber= $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT; $sid = $DB->insert_record('assign_submission', $submission); - $submission->id = $sid; - return $submission; + return $DB->get_record('assign_submission', array('id' => $sid)); } return false; } @@ -2616,8 +2615,7 @@ public function get_user_submission($userid, $create, $attemptnumber=-1) { $submission->attemptnumber = 0; } $sid = $DB->insert_record('assign_submission', $submission); - $submission->id = $sid; - return $submission; + return $DB->get_record('assign_submission', array('id' => $sid)); } return false; } @@ -5284,6 +5282,8 @@ protected function process_save_grading_options() { * 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 $grade * @return string */ diff --git a/mod/assign/submission/file/locallib.php b/mod/assign/submission/file/locallib.php index eda470d816bef..981d5ea753d38 100644 --- a/mod/assign/submission/file/locallib.php +++ b/mod/assign/submission/file/locallib.php @@ -260,6 +260,7 @@ public function save(stdClass $submission, stdClass $data) { $params['objectid'] = $filesubmission->id; $event = \assignsubmission_file\event\submission_updated::create($params); + $event->set_assign($this->assignment); $event->trigger(); return $updatestatus; } else { @@ -272,6 +273,7 @@ public function save(stdClass $submission, stdClass $data) { $params['objectid'] = $filesubmission->id; $event = \assignsubmission_file\event\submission_created::create($params); + $event->set_assign($this->assignment); $event->trigger(); return $filesubmission->id > 0; } diff --git a/mod/assign/submission/onlinetext/locallib.php b/mod/assign/submission/onlinetext/locallib.php index 4e66f58353ccc..c234954602e87 100644 --- a/mod/assign/submission/onlinetext/locallib.php +++ b/mod/assign/submission/onlinetext/locallib.php @@ -192,6 +192,7 @@ public function save(stdClass $submission, stdClass $data) { $params['objectid'] = $onlinetextsubmission->id; $updatestatus = $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission); $event = \assignsubmission_onlinetext\event\submission_updated::create($params); + $event->set_assign($this->assignment); $event->trigger(); return $updatestatus; } else { @@ -205,6 +206,7 @@ public function save(stdClass $submission, stdClass $data) { $onlinetextsubmission->id = $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission); $params['objectid'] = $onlinetextsubmission->id; $event = \assignsubmission_onlinetext\event\submission_created::create($params); + $event->set_assign($this->assignment); $event->trigger(); return $onlinetextsubmission->id > 0; }