Skip to content

Commit

Permalink
MDL-41101 convert assign event triggers to new create_from_xxx helpers
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 b7656ff commit 1b90858
Show file tree
Hide file tree
Showing 25 changed files with 1,728 additions and 269 deletions.
68 changes: 68 additions & 0 deletions mod/assign/classes/event/all_submissions_downloaded.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
}
82 changes: 82 additions & 0 deletions mod/assign/classes/event/assessable_submitted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -96,13 +164,27 @@ 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.
*
* @throws \coding_exception
* @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'])) {
Expand Down
62 changes: 62 additions & 0 deletions mod/assign/classes/event/batch_set_marker_allocation_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'])) {
Expand Down
62 changes: 62 additions & 0 deletions mod/assign/classes/event/batch_set_workflow_state_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'])) {
Expand Down
15 changes: 14 additions & 1 deletion mod/assign/classes/event/course_module_instance_list_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading

0 comments on commit 1b90858

Please sign in to comment.