MDL-39956 cohort: Event clean up and made ready for integration

* Added more PHP Documentation
* Localised strings for get_name()
* get_legacy_eventname() is now static
* get_legacy_eventdata() is now protected
* Testing legacy event data
* Added missing defined('MOODLE_INTERNAL') checks
* Added missing call to add_record_snapshot()
This commit is contained in:
Frederic Massart 2013-08-13 17:26:35 +08:00
parent 4fece33d9a
commit 25a66af722
8 changed files with 160 additions and 75 deletions

View file

@ -85,6 +85,7 @@ function cohort_update_cohort($cohort) {
'context' => context::instance_by_id($cohort->contextid),
'objectid' => $cohort->id,
));
$event->add_record_snapshot('cohort', $cohort);
$event->trigger();
}

View file

@ -104,6 +104,8 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
$this->assertEquals('cohort', $event->objecttable);
$this->assertEquals($id, $event->objectid);
$this->assertEquals($cohort->contextid, $event->contextid);
$this->assertEquals($cohort, $event->get_record_snapshot('cohort', $id));
$this->assertEventLegacyData($cohort, $event);
}
public function test_cohort_update_cohort() {
@ -172,6 +174,8 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
$this->assertEquals('cohort', $event->objecttable);
$this->assertEquals($updatedcohort->id, $event->objectid);
$this->assertEquals($updatedcohort->contextid, $event->contextid);
$this->assertEquals($cohort, $event->get_record_snapshot('cohort', $id));
$this->assertEventLegacyData($cohort, $event);
}
public function test_cohort_delete_cohort() {
@ -207,6 +211,8 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
$this->assertInstanceOf('\core\event\cohort_deleted', $event);
$this->assertEquals('cohort', $event->objecttable);
$this->assertEquals($cohort->id, $event->objectid);
$this->assertEquals($cohort, $event->get_record_snapshot('cohort', $cohort->id));
$this->assertEventLegacyData($cohort, $event);
}
public function test_cohort_delete_category() {
@ -240,9 +246,9 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
public function test_cohort_add_member_event() {
global $USER;
$this->resetAfterTest();
// Setup the data.
$this->resetAfterTest();
$cohort = $this->getDataGenerator()->create_cohort();
$user = $this->getDataGenerator()->create_user();
@ -263,6 +269,7 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
$this->assertEquals($cohort->id, $event->objectid);
$this->assertEquals($user->id, $event->relateduserid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEventLegacyData((object) array('cohortid' => $cohort->id, 'userid' => $user->id), $event);
}
public function test_cohort_remove_member() {
@ -282,9 +289,9 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
public function test_cohort_remove_member_event() {
global $USER;
$this->resetAfterTest();
// Setup the data.
$this->resetAfterTest();
$cohort = $this->getDataGenerator()->create_cohort();
$user = $this->getDataGenerator()->create_user();
cohort_add_member($cohort->id, $user->id);
@ -305,6 +312,7 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
$this->assertEquals($cohort->id, $event->objectid);
$this->assertEquals($user->id, $event->relateduserid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEventLegacyData((object) array('cohortid' => $cohort->id, 'userid' => $user->id), $event);
}
public function test_cohort_is_member() {

View file

@ -45,6 +45,11 @@ $string['delconfirm'] = 'Do you really want to delete cohort \'{$a}\'?';
$string['description'] = 'Description';
$string['duplicateidnumber'] = 'Cohort with the same ID number already exists';
$string['editcohort'] = 'Edit cohort';
$string['event_cohort_created'] = 'Cohort created';
$string['event_cohort_deleted'] = 'Cohort deleted';
$string['event_cohort_member_added'] = 'User added to a cohort';
$string['event_cohort_member_removed'] = 'User removed from a cohort';
$string['event_cohort_updated'] = 'Cohort updated';
$string['external'] = 'External cohort';
$string['idnumber'] = 'Cohort ID';
$string['memberscount'] = 'Cohort size';

View file

@ -14,19 +14,34 @@
// 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.
* Cohort updated event.
*
* @package core
* @copyright 2013 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
/**
* Cohort created event class.
*
* @package core
* @copyright 2013 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cohort_created extends base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'c';
// TODO MDL-41040.
$this->data['level'] = 50;
$this->data['objecttable'] = 'cohort';
}
@ -34,25 +49,24 @@ class cohort_created extends base {
/**
* Returns localised general event name.
*
* @return string|\lang_string
* @return string
*/
public static function get_name() {
//TODO: localise
return 'Cohort created';
return get_string('event_cohort_created', 'core_cohort');
}
/**
* Returns localised description of what happened.
* Returns description of what happened.
*
* @return string|\lang_string
* @return 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() {
@ -60,20 +74,20 @@ class cohort_created extends base {
}
/**
* Does this event replace legacy event?
* Return legacy event name.
*
* @return null|string legacy event name
* @return string legacy event name
*/
public function get_legacy_eventname() {
public static function get_legacy_eventname() {
return 'cohort_added';
}
/**
* Legacy event data if get_legacy_eventname() is not empty.
* Return legacy event data.
*
* @return mixed
* @return stdClass
*/
public function get_legacy_eventdata() {
protected function get_legacy_eventdata() {
return $this->get_record_snapshot('cohort', $this->objectid);
}
}

View file

@ -14,8 +14,6 @@
// 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.
*
@ -24,9 +22,26 @@ namespace core\event;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
/**
* Cohort deleted event class.
*
* @package core
* @copyright 2013 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cohort_deleted extends base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'd';
// TODO MDL-41040.
$this->data['level'] = 50;
$this->data['objecttable'] = 'cohort';
}
@ -34,25 +49,24 @@ class cohort_deleted extends base {
/**
* Returns localised general event name.
*
* @return string|\lang_string
* @return string
*/
public static function get_name() {
//TODO: localise
return 'Cohort deleted';
return get_string('event_core_deleted', 'core_cohort');
}
/**
* Returns localised description of what happened.
* Returns description of what happened.
*
* @return string|\lang_string
* @return 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() {
@ -60,20 +74,20 @@ class cohort_deleted extends base {
}
/**
* Does this event replace legacy event?
* Return legacy event name.
*
* @return null|string legacy event name
*/
public function get_legacy_eventname() {
public static function get_legacy_eventname() {
return 'cohort_deleted';
}
/**
* Legacy event data if get_legacy_eventname() is not empty.
* Return legacy event data.
*
* @return mixed
* @return stdClass
*/
public function get_legacy_eventdata() {
protected function get_legacy_eventdata() {
return $this->get_record_snapshot('cohort', $this->objectid);
}
}

View file

@ -14,19 +14,34 @@
// 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
* User added to a cohort event.
*
* @package core
* @copyright 2013 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
/**
* User added to a cohort event class.
*
* @package core
* @copyright 2013 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cohort_member_added extends base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'c';
// TODO MDL-41040.
$this->data['level'] = 50;
$this->data['objecttable'] = 'cohort';
}
@ -34,25 +49,24 @@ class cohort_member_added extends base {
/**
* Returns localised general event name.
*
* @return string|\lang_string
* @return string
*/
public static function get_name() {
//TODO: localise
return 'User added to a cohort';
return get_string('event_cohort_member_added', 'core_cohort');
}
/**
* Returns localised description of what happened.
* Returns description of what happened.
*
* @return string|\lang_string
* @return string
*/
public function get_description() {
//TODO: localise
return 'User '.$this->relateduserid.' was added to cohort '.$this->objectid.' by:'.$this->userid;
return 'User '.$this->relateduserid.' was added to cohort '.$this->objectid.' by user '.$this->userid;
}
/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
@ -60,20 +74,20 @@ class cohort_member_added extends base {
}
/**
* Does this event replace legacy event?
* Return legacy event name.
*
* @return null|string legacy event name
* @return string legacy event name.
*/
public function get_legacy_eventname() {
public static function get_legacy_eventname() {
return 'cohort_member_added';
}
/**
* Legacy event data if get_legacy_eventname() is not empty.
* Return legacy event data.
*
* @return mixed
* @return stdClass
*/
public function get_legacy_eventdata() {
protected function get_legacy_eventdata() {
$data = new \stdClass();
$data->cohortid = $this->objectid;
$data->userid = $this->relateduserid;

View file

@ -14,10 +14,19 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* User removed from a cohort event.
*
* @package core
* @copyright 2013 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
/**
* User removed from a cohort
* User removed from a cohort event class.
*
* @package core
* @copyright 2013 Dan Poltawski <dan@moodle.com>
@ -25,8 +34,15 @@ namespace core\event;
*/
class cohort_member_removed extends base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'c';
$this->data['crud'] = 'd';
// TODO MDL-41040.
$this->data['level'] = 50;
$this->data['objecttable'] = 'cohort';
}
@ -34,25 +50,24 @@ class cohort_member_removed extends base {
/**
* Returns localised general event name.
*
* @return string|\lang_string
* @return string
*/
public static function get_name() {
//TODO: localise
return 'User removed from a cohort';
return get_string('event_cohort_member_removed', 'core_cohort');
}
/**
* Returns localised description of what happened.
* Returns description of what happened.
*
* @return string|\lang_string
* @return string
*/
public function get_description() {
//TODO: localise
return 'User '.$this->relateduserid.' was removed from cohort '.$this->objectid.' by: '.$this->userid;
return 'User '.$this->relateduserid.' was removed from cohort '.$this->objectid.' by user '.$this->userid;
}
/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
@ -60,20 +75,20 @@ class cohort_member_removed extends base {
}
/**
* Does this event replace legacy event?
* Return legacy event name.
*
* @return null|string legacy event name
* @return string legacy event name.
*/
public function get_legacy_eventname() {
public static function get_legacy_eventname() {
return 'cohort_member_removed';
}
/**
* Legacy event data if get_legacy_eventname() is not empty.
* Return legacy event data.
*
* @return mixed
* @return stdClass
*/
public function get_legacy_eventdata() {
protected function get_legacy_eventdata() {
$data = new \stdClass();
$data->cohortid = $this->objectid;
$data->userid = $this->relateduserid;

View file

@ -14,8 +14,6 @@
// 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 updated event.
*
@ -24,9 +22,26 @@ namespace core\event;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
/**
* Cohort updated event class.
*
* @package core
* @copyright 2013 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cohort_updated extends base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'u';
// TODO MDL-41040.
$this->data['level'] = 50;
$this->data['objecttable'] = 'cohort';
}
@ -34,25 +49,24 @@ class cohort_updated extends base {
/**
* Returns localised general event name.
*
* @return string|\lang_string
* @return string
*/
public static function get_name() {
//TODO: localise
return 'Cohort updated';
return get_string('event_cohort_updated', 'core_cohort');
}
/**
* Returns localised description of what happened.
* Returns description of what happened.
*
* @return string|\lang_string
* @return string
*/
public function get_description() {
//TODO: localise
return 'Cohort '.$this->objectid.' was updated by '.$this->userid.' at context '.$this->contextid;
}
/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
@ -60,20 +74,20 @@ class cohort_updated extends base {
}
/**
* Does this event replace legacy event?
* Return legacy event name.
*
* @return null|string legacy event name
* @return string legacy event name.
*/
public function get_legacy_eventname() {
public static function get_legacy_eventname() {
return 'cohort_updated';
}
/**
* Legacy event data if get_legacy_eventname() is not empty.
* Return legacy event data.
*
* @return mixed
* @return stdClass
*/
public function get_legacy_eventdata() {
protected function get_legacy_eventdata() {
return $this->get_record_snapshot('cohort', $this->objectid);
}
}