Skip to content

Commit

Permalink
MDL-39956 cohort: Convert to new events
Browse files Browse the repository at this point in the history
* \core\event\cohort_created
* \core\event\cohort_updated
* \core\event\cohort_deleted
* \core\event\cohort_member_added
* \core\event\cohort_member_removed
  • Loading branch information
danpoltawski authored and Frederic Massart committed Aug 13, 2013
1 parent 50ff861 commit 04aee04
Show file tree
Hide file tree
Showing 6 changed files with 436 additions and 5 deletions.
40 changes: 35 additions & 5 deletions cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ function cohort_add_cohort($cohort) {

$cohort->id = $DB->insert_record('cohort', $cohort);

events_trigger('cohort_added', $cohort);
$event = \core\event\cohort_created::create(array(
'context' => context::instance_by_id($cohort->contextid),
'objectid' => $cohort->id,
));
$event->add_record_snapshot('cohort', $cohort);
$event->trigger();

return $cohort->id;
}
Expand All @@ -76,7 +81,11 @@ function cohort_update_cohort($cohort) {
$cohort->timemodified = time();
$DB->update_record('cohort', $cohort);

events_trigger('cohort_updated', $cohort);
$event = \core\event\cohort_updated::create(array(
'context' => context::instance_by_id($cohort->contextid),
'objectid' => $cohort->id,
));
$event->trigger();
}

/**
Expand All @@ -94,7 +103,12 @@ function cohort_delete_cohort($cohort) {
$DB->delete_records('cohort_members', array('cohortid'=>$cohort->id));
$DB->delete_records('cohort', array('id'=>$cohort->id));

events_trigger('cohort_deleted', $cohort);
$event = \core\event\cohort_deleted::create(array(
'context' => context::instance_by_id($cohort->contextid),
'objectid' => $cohort->id,
));
$event->add_record_snapshot('cohort', $cohort);
$event->trigger();
}

/**
Expand Down Expand Up @@ -141,7 +155,15 @@ function cohort_add_member($cohortid, $userid) {
$record->timeadded = time();
$DB->insert_record('cohort_members', $record);

events_trigger('cohort_member_added', (object)array('cohortid'=>$cohortid, 'userid'=>$userid));
$cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);

$event = \core\event\cohort_member_added::create(array(
'context' => context::instance_by_id($cohort->contextid),
'objectid' => $cohortid,
'relateduserid' => $userid,
));
$event->add_record_snapshot('cohort', $cohort);
$event->trigger();
}

/**
Expand All @@ -154,7 +176,15 @@ function cohort_remove_member($cohortid, $userid) {
global $DB;
$DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));

events_trigger('cohort_member_removed', (object)array('cohortid'=>$cohortid, 'userid'=>$userid));
$cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);

$event = \core\event\cohort_member_removed::create(array(
'context' => context::instance_by_id($cohort->contextid),
'objectid' => $cohortid,
'relateduserid' => $userid,
));
$event->add_record_snapshot('cohort', $cohort);
$event->trigger();
}

/**
Expand Down
79 changes: 79 additions & 0 deletions lib/classes/event/cohort_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core\event;

/**
* Cohort created event.
*
* @package core
* @copyright 2013 Dan Poltawski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

class cohort_created extends base {
protected function init() {
$this->data['crud'] = 'c';
$this->data['level'] = 50;
$this->data['objecttable'] = 'cohort';
}

/**
* Returns localised general event name.
*
* @return string|\lang_string
*/
public static function get_name() {
//TODO: localise
return 'Cohort created';
}

/**
* Returns localised description of what happened.
*
* @return string|\lang_string
*/
public function get_description() {
//TODO: localise
return 'Cohort '.$this->objectid.' was created by '.$this->userid.' at context '.$this->contextid;
}

/**
* Returns relevant URL.
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/cohort/index.php', array('contextid' => $this->contextid));
}

/**
* Does this event replace legacy event?
*
* @return null|string legacy event name
*/
public function get_legacy_eventname() {
return 'cohort_added';
}

/**
* Legacy event data if get_legacy_eventname() is not empty.
*
* @return mixed
*/
public function get_legacy_eventdata() {
return $this->get_record_snapshot('cohort', $this->objectid);
}
}
79 changes: 79 additions & 0 deletions lib/classes/event/cohort_deleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core\event;

/**
* Cohort deleted event.
*
* @package core
* @copyright 2013 Dan Poltawski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

class cohort_deleted extends base {
protected function init() {
$this->data['crud'] = 'd';
$this->data['level'] = 50;
$this->data['objecttable'] = 'cohort';
}

/**
* Returns localised general event name.
*
* @return string|\lang_string
*/
public static function get_name() {
//TODO: localise
return 'Cohort deleted';
}

/**
* Returns localised description of what happened.
*
* @return string|\lang_string
*/
public function get_description() {
//TODO: localise
return 'Cohort '.$this->objectid.' was deleted by '.$this->userid.' from context '.$this->contextid;
}

/**
* Returns relevant URL.
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/cohort/index.php', array('contextid' => $this->contextid));
}

/**
* Does this event replace legacy event?
*
* @return null|string legacy event name
*/
public function get_legacy_eventname() {
return 'cohort_deleted';
}

/**
* Legacy event data if get_legacy_eventname() is not empty.
*
* @return mixed
*/
public function get_legacy_eventdata() {
return $this->get_record_snapshot('cohort', $this->objectid);
}
}
82 changes: 82 additions & 0 deletions lib/classes/event/cohort_member_added.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core\event;

/**
* User added to a cohort
*
* @package core
* @copyright 2013 Dan Poltawski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

class cohort_member_added extends base {
protected function init() {
$this->data['crud'] = 'c';
$this->data['level'] = 50;
$this->data['objecttable'] = 'cohort';
}

/**
* Returns localised general event name.
*
* @return string|\lang_string
*/
public static function get_name() {
//TODO: localise
return 'User added to a cohort';
}

/**
* Returns localised description of what happened.
*
* @return string|\lang_string
*/
public function get_description() {
//TODO: localise
return 'User '.$this->relateduserid.' was added to cohort '.$this->objectid.' by:'.$this->userid;
}

/**
* Returns relevant URL.
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/cohort/assign.php', array('id' => $this->objectid));
}

/**
* Does this event replace legacy event?
*
* @return null|string legacy event name
*/
public function get_legacy_eventname() {
return 'cohort_member_added';
}

/**
* Legacy event data if get_legacy_eventname() is not empty.
*
* @return mixed
*/
public function get_legacy_eventdata() {
$data = new \stdClass();
$data->cohortid = $this->objectid;
$data->userid = $this->relateduserid;
return $data;
}
}
Loading

0 comments on commit 04aee04

Please sign in to comment.