Skip to content

Commit

Permalink
MDL-55188 events: First deprecation of eventslib.php
Browse files Browse the repository at this point in the history
  • Loading branch information
abgreeve committed Jul 30, 2018
1 parent 2769bf3 commit 5454e72
Show file tree
Hide file tree
Showing 18 changed files with 144 additions and 174 deletions.
1 change: 0 additions & 1 deletion enrol/paypal/ipn.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
// @codingStandardsIgnoreLine This script does not require login.
require("../../config.php");
require_once("lib.php");
require_once($CFG->libdir.'/eventslib.php');
require_once($CFG->libdir.'/enrollib.php');
require_once($CFG->libdir . '/filelib.php');

Expand Down
1 change: 0 additions & 1 deletion error/index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

require('../config.php');
require_once($CFG->libdir.'/eventslib.php');

// Form submitted, do not check referer (original page unknown).
if ($form = data_submitted()) {
Expand Down
3 changes: 0 additions & 3 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,6 @@ function uninstall_plugin($type, $name) {
// delete the capabilities that were defined by this module
capabilities_cleanup($component);

// remove event handlers and dequeue pending events
events_uninstall($component);

// Delete all remaining files in the filepool owned by the component.
$fs = get_file_storage();
$fs->delete_component_files($component);
Expand Down
133 changes: 133 additions & 0 deletions lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -6607,3 +6607,136 @@ function groups_get_all_groups_for_courses($courses) {

return $groups;
}

/**
* Gets the capabilities that have been cached in the database for this
* component.
* @deprecated since Moodle 3.6. Please use the Events 2 API.
* @todo final deprecation. To be removed in Moodle 4.0
*
* @access protected To be used from eventslib only
*
* @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
* @return array of events
*/
function events_get_cached($component) {
global $DB;

debugging('Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.',
DEBUG_DEVELOPER);

$cachedhandlers = array();

if ($storedhandlers = $DB->get_records('events_handlers', array('component'=>$component))) {
foreach ($storedhandlers as $handler) {
$cachedhandlers[$handler->eventname] = array (
'id' => $handler->id,
'handlerfile' => $handler->handlerfile,
'handlerfunction' => $handler->handlerfunction,
'schedule' => $handler->schedule,
'internal' => $handler->internal);
}
}

return $cachedhandlers;
}

/**
* Remove all event handlers and queued events
* @deprecated since Moodle 3.6. Please use the Events 2 API.
* @todo final deprecation. To be removed in Moodle 4.0
*
* @category event
* @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
*/
function events_uninstall($component) {
debugging('Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.',
DEBUG_DEVELOPER);
$cachedhandlers = events_get_cached($component);
events_cleanup($component, $cachedhandlers);

events_get_handlers('reset');
}

/**
* Deletes cached events that are no longer needed by the component.
* @deprecated since Moodle 3.6. Please use the Events 2 API.
* @todo final deprecation. To be removed in Moodle 4.0
*
* @access protected To be used from eventslib only
*
* @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
* @param array $cachedhandlers array of the cached events definitions that will be
* @return int number of unused handlers that have been removed
*/
function events_cleanup($component, $cachedhandlers) {
global $DB;
debugging('Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.',
DEBUG_DEVELOPER);
$deletecount = 0;
foreach ($cachedhandlers as $eventname => $cachedhandler) {
if ($qhandlers = $DB->get_records('events_queue_handlers', array('handlerid'=>$cachedhandler['id']))) {
//debugging("Removing pending events from queue before deleting of event handler: $component - $eventname");
foreach ($qhandlers as $qhandler) {
events_dequeue($qhandler);
}
}
$DB->delete_records('events_handlers', array('eventname'=>$eventname, 'component'=>$component));
$deletecount++;
}

return $deletecount;
}

/**
* Removes this queued handler from the events_queued_handler table
*
* Removes events_queue record from events_queue if no more references to this event object exists
* @deprecated since Moodle 3.6. Please use the Events 2 API.
* @todo final deprecation. To be removed in Moodle 4.0
*
* @access protected To be used from eventslib only
*
* @param stdClass $qhandler A row from the events_queued_handler table
*/
function events_dequeue($qhandler) {
global $DB;
debugging('Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.',
DEBUG_DEVELOPER);
// first delete the queue handler
$DB->delete_records('events_queue_handlers', array('id'=>$qhandler->id));

// if no more queued handler is pointing to the same event - delete the event too
if (!$DB->record_exists('events_queue_handlers', array('queuedeventid'=>$qhandler->queuedeventid))) {
$DB->delete_records('events_queue', array('id'=>$qhandler->queuedeventid));
}
}

/**
* Returns handlers for given event. Uses caching for better perf.
* @deprecated since Moodle 3.6. Please use the Events 2 API.
* @todo final deprecation. To be removed in Moodle 4.0
*
* @access protected To be used from eventslib only
*
* @staticvar array $handlers
* @param string $eventname name of event or 'reset'
* @return array|false array of handlers or false otherwise
*/
function events_get_handlers($eventname) {
global $DB;
static $handlers = array();
debugging('Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.',
DEBUG_DEVELOPER);

if ($eventname === 'reset') {
$handlers = array();
return false;
}

if (!array_key_exists($eventname, $handlers)) {
$handlers[$eventname] = $DB->get_records('events_handlers', array('eventname'=>$eventname));
}

return $handlers[$eventname];
}
141 changes: 0 additions & 141 deletions lib/eventslib.php

This file was deleted.

1 change: 0 additions & 1 deletion lib/phpunit/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ public static function reset_all_data($detectchanges = false) {
accesslib_clear_all_caches(true);
get_string_manager()->reset_caches(true);
reset_text_filters_cache(true);
events_get_handlers('reset');
core_text::reset_caches();
get_message_processors(false, true, true);
filter_manager::reset_caches();
Expand Down
1 change: 0 additions & 1 deletion lib/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,6 @@
require_once($CFG->libdir .'/enrollib.php'); // Enrolment related functions
require_once($CFG->libdir .'/pagelib.php'); // Library that defines the moodle_page class, used for $PAGE
require_once($CFG->libdir .'/blocklib.php'); // Library for controlling blocks
require_once($CFG->libdir .'/eventslib.php'); // Events functions
require_once($CFG->libdir .'/grouplib.php'); // Groups functions
require_once($CFG->libdir .'/sessionlib.php'); // All session and cookie related stuff
require_once($CFG->libdir .'/editorlib.php'); // All text editor related functions and classes
Expand Down
9 changes: 5 additions & 4 deletions lib/tests/eventslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function test_events_update_definition__install() {
global $DB;

events_update_definition('unittest');
$this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
$this->assertDebuggingCalledCount(4);

$dbcount = $DB->count_records('events_handlers', array('component'=>'unittest'));
$handlers = array();
Expand All @@ -68,9 +68,10 @@ public function test_events_update_definition__uninstall() {
global $DB;

events_update_definition('unittest');
$this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
$this->assertDebuggingCalledCount(4);

events_uninstall('unittest');
$this->assertDebuggingCalledCount(4);
$this->assertEquals(0, $DB->count_records('events_handlers', array('component'=>'unittest')), 'All handlers should be uninstalled: %s');
}

Expand All @@ -81,7 +82,7 @@ public function test_events_update_definition__update() {
global $DB;

events_update_definition('unittest');
$this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
$this->assertDebuggingCalledCount(4);

// First modify directly existing handler.
$handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
Expand All @@ -93,7 +94,7 @@ public function test_events_update_definition__update() {

// Update the definition, it should revert the handler back.
events_update_definition('unittest');
$this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
$this->assertDebuggingCalledCount(4);
$handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
$this->assertSame($handler->handlerfunction, $original, 'update should sync db with file definition: %s');
}
Expand Down
6 changes: 6 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ information provided here is intended especially for developers.
- I set the field "<field_string>" to multiline
- I follow "<link_string>"" in the open menu
* Removed the lib/password_compat/lib/password.php file.
* The eventslib.php file has been deleted and its functions have been moved to deprecatedlib.php. The affected functions are:
- events_get_cached()
- events_uninstall()
- events_cleanup()
- events_dequeue()
- events_get_handlers()

=== 3.5 ===

Expand Down
Loading

0 comments on commit 5454e72

Please sign in to comment.