Skip to content

Commit

Permalink
MDL-41101 use snapshots for submissions and grades in assign events
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak authored and Petr Skoda committed Apr 17, 2014
1 parent bf5b441 commit 4781ff2
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 196 deletions.
35 changes: 3 additions & 32 deletions mod/assign/classes/event/assessable_submitted.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class assessable_submitted extends base { 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. * Create instance of event.
* *
Expand All @@ -67,31 +59,13 @@ public static function create_from_submission(\assign $assign, \stdClass $submis
'submission_editable' => $editable, 'submission_editable' => $editable,
), ),
); );
self::$preventcreatecall = false;
/** @var assessable_submitted $event */ /** @var assessable_submitted $event */
$event = self::create($data); $event = self::create($data);
self::$preventcreatecall = true;
$event->set_assign($assign); $event->set_assign($assign);
$event->submission = $submission; $event->add_record_snapshot('assign_submission', $submission);
return $event; 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. * Returns description of what happened.
* *
Expand Down Expand Up @@ -152,7 +126,8 @@ protected function init() {
* @return array * @return array
*/ */
protected function get_legacy_logdata() { 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(); return parent::get_legacy_logdata();
} }


Expand All @@ -163,10 +138,6 @@ protected function get_legacy_logdata() {
* @return void * @return void
*/ */
protected function validate_data() { 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(); parent::validate_data();


if (!isset($this->other['submission_editable'])) { if (!isset($this->other['submission_editable'])) {
Expand Down
22 changes: 22 additions & 0 deletions mod/assign/classes/event/feedback_updated.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class feedback_updated extends base { 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. * Init method.
Expand Down
32 changes: 2 additions & 30 deletions mod/assign/classes/event/feedback_viewed.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class feedback_viewed extends base { 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. * Create instance of event.
* *
Expand All @@ -65,29 +57,13 @@ public static function create_from_grade(\assign $assign, \stdClass $grade) {
'assignid' => $assign->get_instance()->id, 'assignid' => $assign->get_instance()->id,
), ),
); );
self::$preventcreatecall = false;
/** @var feedback_viewed $event */ /** @var feedback_viewed $event */
$event = self::create($data); $event = self::create($data);
self::$preventcreatecall = true;
$event->set_assign($assign); $event->set_assign($assign);
$event->grade = $grade; $event->add_record_snapshot('assign_grades', $grade);
return $event; 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. * Init method.
*/ */
Expand Down Expand Up @@ -122,7 +98,7 @@ public function get_description() {
* @return array * @return array
*/ */
protected function get_legacy_logdata() { 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); $this->set_legacy_logdata('view feedback', $logmessage);
return parent::get_legacy_logdata(); return parent::get_legacy_logdata();
} }
Expand All @@ -133,10 +109,6 @@ protected function get_legacy_logdata() {
* @throws \coding_exception * @throws \coding_exception
*/ */
protected function validate_data() { 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(); parent::validate_data();


if (!isset($this->relateduserid)) { if (!isset($this->relateduserid)) {
Expand Down
20 changes: 1 addition & 19 deletions mod/assign/classes/event/statement_accepted.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class statement_accepted extends base { class statement_accepted extends base {
/** @var \stdClass */
protected $submission;
/** /**
* Flag for prevention of direct create() call. * Flag for prevention of direct create() call.
* @var bool * @var bool
Expand All @@ -62,26 +60,10 @@ public static function create_from_submission(\assign $assign, \stdClass $submis
$event = self::create($data); $event = self::create($data);
self::$preventcreatecall = true; self::$preventcreatecall = true;
$event->set_assign($assign); $event->set_assign($assign);
$event->submission = $submission; $event->add_record_snapshot('assign_submission', $submission);
return $event; 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. * Returns description of what happened.
* *
Expand Down
23 changes: 3 additions & 20 deletions mod/assign/classes/event/submission_duplicated.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class submission_duplicated extends base { class submission_duplicated extends base {
/** @var \stdClass */
protected $submission;
/** /**
* Flag for prevention of direct create() call. * Flag for prevention of direct create() call.
* @var bool * @var bool
Expand All @@ -62,26 +60,10 @@ public static function create_from_submission(\assign $assign, \stdClass $submis
$event = self::create($data); $event = self::create($data);
self::$preventcreatecall = true; self::$preventcreatecall = true;
$event->set_assign($assign); $event->set_assign($assign);
$event->submission = $submission; $event->add_record_snapshot('assign_submission', $submission);
return $event; 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. * Returns description of what happened.
* *
Expand Down Expand Up @@ -117,7 +99,8 @@ protected function init() {
* @return array * @return array
*/ */
protected function get_legacy_logdata() { 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(); return parent::get_legacy_logdata();
} }


Expand Down
23 changes: 3 additions & 20 deletions mod/assign/classes/event/submission_graded.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class submission_graded extends base { class submission_graded extends base {
/** @var \stdClass */
protected $grade;
/** /**
* Flag for prevention of direct create() call. * Flag for prevention of direct create() call.
* @var bool * @var bool
Expand All @@ -63,26 +61,10 @@ public static function create_from_grade(\assign $assign, \stdClass $grade) {
$event = self::create($data); $event = self::create($data);
self::$preventcreatecall = true; self::$preventcreatecall = true;
$event->set_assign($assign); $event->set_assign($assign);
$event->grade = $grade; $event->add_record_snapshot('assign_grades', $grade);
return $event; 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. * Returns description of what happened.
* *
Expand Down Expand Up @@ -118,7 +100,8 @@ protected function init() {
* @return array * @return array
*/ */
protected function get_legacy_logdata() { 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(); return parent::get_legacy_logdata();
} }


Expand Down
37 changes: 3 additions & 34 deletions mod/assign/classes/event/submission_status_updated.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class submission_status_updated extends base { 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. * Create instance of event.
* *
Expand All @@ -67,31 +59,13 @@ public static function create_from_submission(\assign $assign, \stdClass $submis
'newstatus' => $submission->status 'newstatus' => $submission->status
) )
); );
self::$preventcreatecall = false;
/** @var submission_status_updated $event */ /** @var submission_status_updated $event */
$event = self::create($data); $event = self::create($data);
self::$preventcreatecall = true;
$event->set_assign($assign); $event->set_assign($assign);
$event->submission = $submission; $event->add_record_snapshot('assign_submission', $submission);
return $event; 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. * Returns description of what happened.
* *
Expand Down Expand Up @@ -127,9 +101,8 @@ protected function init() {
* @return array * @return array
*/ */
protected function get_legacy_logdata() { protected function get_legacy_logdata() {
global $DB; $submission = $this->get_record_snapshot('assign_submission', $this->objectid);

$user = $this->get_record_snapshot('user', $submission->userid);
$user = $DB->get_record('user', array('id' => $this->submission->userid), '*', MUST_EXIST);
$logmessage = get_string('reverttodraftforstudent', 'assign', array('id' => $user->id, 'fullname' => fullname($user))); $logmessage = get_string('reverttodraftforstudent', 'assign', array('id' => $user->id, 'fullname' => fullname($user)));
$this->set_legacy_logdata('revert submission to draft', $logmessage); $this->set_legacy_logdata('revert submission to draft', $logmessage);
return parent::get_legacy_logdata(); return parent::get_legacy_logdata();
Expand All @@ -141,10 +114,6 @@ protected function get_legacy_logdata() {
* @throws \coding_exception * @throws \coding_exception
*/ */
protected function validate_data() { 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(); parent::validate_data();


if (!isset($this->other['newstatus'])) { if (!isset($this->other['newstatus'])) {
Expand Down
Loading

0 comments on commit 4781ff2

Please sign in to comment.