Skip to content
Browse files

MDL-41101 use snapshots for submissions and grades in assign events

  • Loading branch information...
1 parent bf5b441 commit 4781ff2e5fdb00bc4a216ec8387b07a36ea61580 @skodak skodak committed with Petr Skoda Apr 16, 2014
View
35 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,32 +59,14 @@ 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.
*
* @return string
@@ -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'])) {
View
22 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.
View
32 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,30 +57,14 @@ 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.
*/
protected function init() {
@@ -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)) {
View
20 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,27 +60,11 @@ 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.
*
* @return string
View
23 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,27 +60,11 @@ 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.
*
* @return string
@@ -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();
}
View
23 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,27 +61,11 @@ 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.
*
* @return string
@@ -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();
}
View
37 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,32 +59,14 @@ 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.
*
* @return string
@@ -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'])) {
View
32 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,30 +57,14 @@ 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.
*/
protected function init() {
@@ -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)) {
View
8 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();
}
View
8 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
*/
View
2 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;
}
View
2 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;
}

0 comments on commit 4781ff2

Please sign in to comment.
Something went wrong with that request. Please try again.