Skip to content

Commit

Permalink
Poet - Completing the event restructuring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Churchward committed Apr 24, 2019
1 parent ba47b58 commit c660e72
Show file tree
Hide file tree
Showing 20 changed files with 711 additions and 123 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ sudo: true
addons:
firefox: "47.0.1"
postgresql: "9.4"
mysql: "8.0.2"
apt:
packages:
- oracle-java9-installer
Expand Down
22 changes: 0 additions & 22 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,6 @@ public static function all_events($event) {
}
}

/**
* Triggered via course_deleted event.
* - Removes course metadata
*
* @param \core\event\course_deleted $event
* @return bool true on success
*/
public static function course_deleted(\core\event\course_deleted $event) {
return self::delete_metadata(CONTEXT_COURSE, $event->objectid);
}

/**
* Triggered via module_deleted event.
* - Removes module metadata
*
* @param \core\event\course_module_deleted $event
* @return bool true on success
*/
public static function course_module_deleted(\core\event\course_module_deleted $event) {
return self::delete_metadata(CONTEXT_MODULE, $event->objectid);
}

/**
* Delete metadata for appropriate contextlevel fields.
* - Removes user metadata
Expand Down
43 changes: 43 additions & 0 deletions context/category/classes/observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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/>.

/**
* @package local_metadata
* @subpackage metadatacontext_category
* @author Mike Churchward <mike.churchward@poetopensource.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2017, onwards Poet
*/

namespace metadatacontext_category;

defined('MOODLE_INTERNAL') || die();

/**
* Local metadatacontext_category event handler.
*/
class observer {
/**
* Triggered via course_category_deleted event.
* - Removes category metadata
*
* @param \core\event\course_category_deleted $event
* @return bool true on success
*/
public static function course_category_deleted(\core\event\course_category_deleted $event) {
return \local_metadata\observer::delete_metadata(CONTEXT_COURSECAT, $event->objectid);
}
}
31 changes: 31 additions & 0 deletions context/category/db/events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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/>.

/**
* @package local_metadata
* @subpackage metadatacontext_category
* @author Mike Churchward <mike.churchward@poetopensource.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2017, onwards Poet
*/

defined('MOODLE_INTERNAL') || die;

$observers = [
['eventname' => '\core\event\course_category_deleted',
'callback' => '\metadatacontext_category\observer::course_category_deleted'
],
];
79 changes: 79 additions & 0 deletions context/category/tests/metadatacontext_category_event_test.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/>.

/**
* @package local_metadata
* @subpackage metadatacontext_category
* @author Mike Churchward <mike.churchward@poetopensource.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2017, onwards Poet
*/

/**
* Test class for course category metadatacontext events.
*
* @package local_metadata
* @subpackage metadatacontext_category
* @copyright 2017, onwards Poet
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* @group local_metadata
* @group metadatacontext_category
*/
class metadatacontext_category_event_testcase extends local_metadataevent_testcase {

/**
* Setup tasks.
*/
public function setUp() {
$this->category = [];
$this->category[] = $this->getDataGenerator()->create_category();
$this->category[] = $this->getDataGenerator()->create_category();

parent::setUp();
}

/**
* Performs unit tests for category deleted event.
*/
public function test_categorydeleted() {
global $DB;

$this->resetAfterTest(true);

// Create a custom field of textarea type.
$id1 = $this->generator->create_metadata_field(CONTEXT_COURSECAT, 'frogdesc', 'Description of frog');
$this->generator->create_metadata($id1, $this->category[0]->id, 'Leopard frog');
$this->generator->create_metadata($id1, $this->category[1]->id, 'Bullfrog');

// Confirm expected data.
$this->assertEquals(1, $DB->count_records('local_metadata_field'));
$this->assertEquals(2, $DB->count_records('local_metadata'));
$this->assertEquals(1, $DB->count_records('local_metadata', ['instanceid' => $this->category[0]->id]));

// Deleting the course should trigger the event.
$this->category[0]->delete_full();

// Check the field data has been deleted.
$this->assertEquals(1, $DB->count_records('local_metadata_field'));
$this->assertEquals(1, $DB->count_records('local_metadata'));
$this->assertEquals(0, $DB->count_records('local_metadata', ['instanceid' => $this->category[0]->id]));
}
}
2 changes: 1 addition & 1 deletion context/category/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2017070103;
$plugin->version = 2017070104;
$plugin->release = 'BETA3.3.4 (Build 2018062800)';
$plugin->maturity = MATURITY_BETA;
$plugin->requires = 2016052300; // Requires this Moodle version.
Expand Down
43 changes: 43 additions & 0 deletions context/cohort/classes/observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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/>.

/**
* @package local_metadata
* @subpackage metadatacontext_cohort
* @author Mike Churchward <mike.churchward@poetopensource.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2017, onwards Poet
*/

namespace metadatacontext_cohort;

defined('MOODLE_INTERNAL') || die();

/**
* Local metadatacontext_cohort event handler.
*/
class observer {
/**
* Triggered via cohort_deleted event.
* - Removes cohort metadata
*
* @param \core\event\cohort_deleted $event
* @return bool true on success
*/
public static function cohort_deleted(\core\event\cohort_deleted $event) {
return \local_metadata\observer::delete_metadata(CONTEXT_COHORT, $event->objectid);
}
}
31 changes: 31 additions & 0 deletions context/cohort/db/events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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/>.

/**
* @package local_metadata
* @subpackage metadatacontext_cohort
* @author Mike Churchward <mike.churchward@poetopensource.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2017, onwards Poet
*/

defined('MOODLE_INTERNAL') || die;

$observers = [
['eventname' => '\core\event\cohort_deleted',
'callback' => '\metadatacontext_cohort\observer::cohort_deleted'
],
];
80 changes: 80 additions & 0 deletions context/cohort/tests/metadatacontext_cohort_event_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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/>.

/**
* @package local_metadata
* @subpackage metadatacontext_cohort
* @author Mike Churchward <mike.churchward@poetopensource.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2017, onwards Poet
*/

/**
* Test class for cohort metadatacontext events.
*
* @package local_metadata
* @subpackage metadatacontext_cohort
* @copyright 2017, onwards Poet
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* @group local_metadata
* @group metadatacontext_cohort
*/
class metadatacontext_cohort_event_testcase extends local_metadataevent_testcase {

/**
* Setup tasks.
*/
public function setUp() {
$this->cohort = [];
$this->cohort[] = $this->getDataGenerator()->create_cohort();
$this->cohort[] = $this->getDataGenerator()->create_cohort();

parent::setUp();
}

/**
* Performs unit tests for cohort deleted event.
*/
public function test_cohortdeleted() {
global $DB, $CFG;
require_once($CFG->dirroot . '/local/metadata/context/cohort/classes/context_handler.php');

$this->resetAfterTest(true);

// Create a custom field of textarea type.
$id1 = $this->generator->create_metadata_field(CONTEXT_COHORT, 'frogdesc', 'Description of frog');
$this->generator->create_metadata($id1, $this->cohort[0]->id, 'Leopard frog');
$this->generator->create_metadata($id1, $this->cohort[1]->id, 'Bullfrog');

// Confirm expected data.
$this->assertEquals(1, $DB->count_records('local_metadata_field'));
$this->assertEquals(2, $DB->count_records('local_metadata'));
$this->assertEquals(1, $DB->count_records('local_metadata', ['instanceid' => $this->cohort[0]->id]));

// Deleting the cohort should trigger the event.
cohort_delete_cohort($this->cohort[0]);

// Check the field data has been deleted.
$this->assertEquals(1, $DB->count_records('local_metadata_field'));
$this->assertEquals(1, $DB->count_records('local_metadata'));
$this->assertEquals(0, $DB->count_records('local_metadata', ['instanceid' => $this->cohort[0]->id]));
}
}
Loading

0 comments on commit c660e72

Please sign in to comment.