Improve documentation, delete useless method, improve variables uses - refs BT#9092

1.10.x
Daniel Barreto 10 years ago
parent 95ad0f51b9
commit 413584a896
  1. 20
      main/admin/index.php
  2. 6
      main/inc/lib/database.constants.inc.php
  3. 42
      main/inc/lib/hook/HookAdminBlock.class.php
  4. 106
      main/inc/lib/hook/HookEvent.class.php
  5. 29
      main/inc/lib/hook/HookInterfaces.php
  6. 241
      main/inc/lib/hook/HookManagement.class.php
  7. 15
      main/inc/lib/hook/HookObserver.class.php
  8. 43
      main/inc/lib/hook/HookWSRegistration.class.php
  9. 8
      main/inc/lib/main_api.lib.php
  10. 8
      main/inc/lib/usermanager.lib.php
  11. 6
      main/webservices/registration.soap.php

@ -61,9 +61,17 @@ if (isset($_GET['msg']) && isset($_GET['type'])) {
$blocks = array();
// Instantiate Hook Event for Admin Block
$hook = HookAdminBlock::create();
if (!empty($hook)) {
$hook->notifyAdminBlock(HOOK_TYPE_PRE);
// If not empty, then notify Pre process to Hook Observers for Admin Block
$hook->setEventData(array('blocks' => $blocks));
$data = $hook->notifyAdminBlock(HOOK_EVENT_TYPE_PRE);
// Check if blocks data is not null
if (isset($data['blocks'])) {
// Get modified blocks
$blocks = $data['blocks'];
}
}
/* Users */
@ -343,8 +351,16 @@ if (api_is_platform_admin()) {
$blocks['version_check']['items'] = null;
$blocks['version_check']['class'] = 'block-admin-version_check';
// Check Hook Event for Admin Block Object
if (!empty($hook)) {
$hook->notifyAdminBlock(HOOK_TYPE_POST);
// If not empty, then notify Pre process to Hook Observers for Admin Block
$hook->setEventData(array('blocks' => $blocks));
$data = $hook->notifyAdminBlock(HOOK_EVENT_TYPE_PRE);
// Check if blocks data is not null
if (isset($data['blocks'])) {
// Get modified blocks
$blocks = $data['blocks'];
}
}
}

@ -348,6 +348,6 @@ define('TABLE_GRADE_MODEL', 'grade_model');
define('TABLE_GRADE_MODEL_COMPONENTS', 'grade_components');
// Hook tables
define('TABLE_PLUGIN_HOOK_OBSERVER', 'hook_observer');
define('TABLE_PLUGIN_HOOK_CALL', 'hook_call');
define('TABLE_PLUGIN_HOOK_EVENT', 'hook_event');
define('TABLE_HOOK_OBSERVER', 'hook_observer');
define('TABLE_HOOK_CALL', 'hook_call');
define('TABLE_HOOK_EVENT', 'hook_event');

@ -1,32 +1,48 @@
<?php
/* For licensing terms, see /license.txt /
/**
* Created by PhpStorm.
* User: dbarreto
* Date: 19/12/14
* Time: 09:45 AM
* This file contains a Hook Event class for Admin Block.
* @package chamilo.library.hook
*/
/**
* Class HookAdminBlock
* This class is a Hook event implementing Admin Block Event interface.
* This class is used to modify admin block by notifying Hook Observer for Admin Block
*/
class HookAdminBlock extends HookEvent implements HookAdminBlockEventInterface {
/**
* Constructor
*/
protected function __construct()
{
parent::__construct('HookAdminBlock');
}
/**
* @param int $type
* @return int
* Notify Hook observers for Admin Block event
* @param int $type Set the type of hook event called. 0: HOOK_TYPE_PRE, 1: HOOK_TYPE_POST
* @return array|int
*/
public function notifyAdminBlock($type)
{
/** @var \HookAdminBlockObserverInterface $observer */
global $blocks;
$this->eventData['blocks'] = $blocks;
$this->eventData['type'] = $type;
foreach ($this->observers as $observer) {
$data = $observer->hookAdminBlock($this);
$blocks = $data['blocks'];
// Save data
if (isset($this->eventData['blocks'])) {
$this->eventData['type'] = $type;
// Call all registered hook observers for admin block
foreach ($this->observers as $observer) {
$data = $observer->hookAdminBlock($this);
if (isset($data['blocks'])) {
// Get modified data
$this->eventData['blocks'] = $data['blocks'];
}
}
return $this->eventData;
}
return 1;
return 0;
}
}

@ -1,34 +1,40 @@
<?php
/* For licensing terms, see /license.txt */
/**
* This file contains an abstract Hook event class
* Used for Hook Events (e.g Create user, Webservice registration)
* @package chamilo.library.hook
*/
/**
* Class HookEvent
* This abstract class implements Hook Event Interface to build the base
* for Hook Events. This class have some public static method,
* e.g for create Hook Events
*/
abstract class HookEvent implements HookEventInterface
{
public $observers;
public $eventName;
public $eventData;
public $manager;
public static $hook;
/**
* Construct Method
* @param $eventName
* @param string $eventName
* @throws Exception
*/
protected function __construct($eventName)
{
if (self::isHookPluginActive()) {
$this->observers = new SplObjectStorage();
$this->eventName = $eventName;
$this->eventData = array();
$this->plugin = HookManagement::create();
$this->loadAttachments();
} else {
throw new \Exception('Hook Management Plugin is not active');
}
$this->observers = new SplObjectStorage();
$this->eventName = $eventName;
$this->eventData = array();
$this->manager = HookManagement::create();
}
/**
* Return the singleton instance of Hook event.
* If Hook Management plugin is not enabled, will return NULL
* @return HookEventInterface|null
*/
public static function create()
@ -57,15 +63,14 @@ abstract class HookEvent implements HookEventInterface
*/
public function attach(HookObserverInterface $observer)
{
global $_hook;
$observerClass = get_class($observer);
$_hook[$this->eventName][$observerClass] = array(
self::$hook[$this->eventName][$observerClass] = array(
'class_name' => $observerClass,
'path' => $observer->getPath(),
'plugin_name' => $observer->getPluginName(),
);
$this->observers->attach($observer);
$this->plugin->insertHook($this->eventName, $observerClass, HOOK_TYPE_ALL);
$this->manager->insertHook($this->eventName, $observerClass, HOOK_EVENT_TYPE_ALL);
}
/**
@ -78,15 +83,13 @@ abstract class HookEvent implements HookEventInterface
*/
public function detach(HookObserverInterface $observer)
{
global $_hook;
$observerClass = get_class($observer);
unset($_hook[$this->eventName][$observerClass]);
unset(self::$hook[$this->eventName][$observerClass]);
$this->observers->detach($observer);
$this->plugin->deleteHook($this->eventName, $observerClass, HOOK_TYPE_ALL);
$this->manager->deleteHook($this->eventName, $observerClass, HOOK_EVENT_TYPE_ALL);
}
/**
* (PHP 5 &gt;= 5.1.0)<br/>
* Notify an observer
* @link http://php.net/manual/en/splsubject.notify.php
* @return void
@ -127,40 +130,13 @@ abstract class HookEvent implements HookEventInterface
return $this;
}
/**
* Load all hook observer already registered from Session or Database
* @return $this
*/
public function loadAttachments()
{
global $_hook;
if (isset($_hook[$this->eventName]) && is_array($_hook[$this->eventName])) {
foreach ($_hook[$this->eventName] as $hookObserver => $val) {
self::autoLoadHooks($hookObserver, $val['path']);
$hookObserverInstance = $hookObserver::create();
$this->observers->attach($hookObserverInstance);
}
} else {
// Load from Database and save into global name
$_hook[$this->eventName] = $this->plugin->listHookObservers($this->eventName);
if (isset($_hook[$this->eventName]) && is_array($_hook[$this->eventName])) {
foreach ($_hook[$this->eventName] as $hookObserver => $val) {
self::autoLoadHooks($hookObserver, $val['path']);
$hookObserverInstance = $hookObserver::create();
$this->observers->attach($hookObserverInstance);
}
}
}
}
/**
* Detach all hook observers
* @return $this
*/
public function detachAll()
{
global $_hook;
$_hook[$this->eventName] = null;
self::$hook[$this->eventName] = null;
$this->observers->removeAll($this->observers);
}
@ -172,38 +148,4 @@ abstract class HookEvent implements HookEventInterface
{
$this->observers->removeAll($this->observers);
}
/**
* Return true if HookManagement is active. Else, false.
* This is needed to check if hook event can be instantiated
* @return boolean
*/
public static function isHookPluginActive()
{
// Hook Management was a plugin, now is in library
// Then, always return true
/*
$isActive = false;
$appPlugin = new AppPlugin();
$pluginList = $appPlugin->getInstalledPluginListName();
if (in_array(HOOK_MANAGEMENT, $pluginList)) {
$isActive = true;
}
return $isActive;
*/
return true;
}
/**
* Hook Auto Loader. Search for Hook Observers from plugins
* @param string $observerClass
* @param string $path
* @return int
*/
public static function autoLoadHooks($observerClass, $path)
{
Autoload::$map[$observerClass] = $path;
}
}

@ -1,5 +1,10 @@
<?php
/* For licensing terms, see /license.txt */
/**
* This file contains all Hook interfaces and their relation.
* They are used for Hook classes
* @package chamilo.library.hook
*/
/**
* Interface HookEventInterface
@ -28,7 +33,6 @@ interface HookEventInterface
/**
* Return the singleton instance of Hook event.
* If Hook Management plugin is not enabled, will return NULL
* @return HookEventInterface|null
*/
public static function create();
@ -73,13 +77,6 @@ interface HookEventInterface
*/
public function detachAll();
/**
* Return true if HookManagement plugin is active. Else, false.
* This is needed to store attachments into Database inside Hook plugin tables
* @return boolean
*/
public static function isHookPluginActive();
/**
* Hook Auto Loader. Search for Hook Observers from plugins
* @param string $observerClass
@ -93,7 +90,6 @@ interface HookObserverInterface
{
/**
* Return the singleton instance of Hook observer.
* If Hook Management plugin is not enabled, will return NULL
* @return HookEventInterface|null
*/
public static function create();
@ -113,14 +109,6 @@ interface HookObserverInterface
interface HookManagementInterface
{
/**
* Initialize Database storing hooks (events, observers, calls)
* This should be called right after installDatabase method
* @return int
*/
public function initDatabase();
/**
* Insert hook into Database. Return insert id
* @param string $eventName
@ -168,9 +156,9 @@ interface HookManagementInterface
/**
* Return the hook call id identified by hook event, hook observer and type
* @param $eventName
* @param $observerClassName
* @param $type
* @param string $eventName
* @param string $observerClassName
* @param int $type
* @return mixed
*/
public function getHookCallId($eventName, $observerClassName, $type);
@ -178,6 +166,7 @@ interface HookManagementInterface
/**
* Interface HookPluginInterface
* This interface should be implemented by plugins to implements Hook Observer
*/
interface HookPluginInterface
{

@ -12,9 +12,9 @@ class HookManagement implements HookManagementInterface
*/
protected function __construct()
{
$this->tables[TABLE_PLUGIN_HOOK_OBSERVER] = Database::get_main_table(TABLE_PLUGIN_HOOK_OBSERVER);
$this->tables[TABLE_PLUGIN_HOOK_EVENT] = Database::get_main_table(TABLE_PLUGIN_HOOK_EVENT);
$this->tables[TABLE_PLUGIN_HOOK_CALL] = Database::get_main_table(TABLE_PLUGIN_HOOK_CALL);
$this->tables[TABLE_HOOK_OBSERVER] = Database::get_main_table(TABLE_HOOK_OBSERVER);
$this->tables[TABLE_HOOK_EVENT] = Database::get_main_table(TABLE_HOOK_EVENT);
$this->tables[TABLE_HOOK_CALL] = Database::get_main_table(TABLE_HOOK_CALL);
$this->hookCalls = $this->listAllHookCalls();
$this->hookEvents = $this->listAllHookEvents();
@ -26,174 +26,13 @@ class HookManagement implements HookManagementInterface
* @staticvar null $result
* @return HookManagement
*/
static function create()
public static function create()
{
static $result = null;
return $result ? $result : $result = new self();
}
/**
* Install the plugin
* @return void
*/
public function install()
{
$this->installDatabase();
$this->initDatabase();
}
/**
* Uninstall the plugin
* @return void
*/
public function uninstall()
{
$this->uninstallDatabase();
}
/**
* Create the database tables for the plugin
* @return void
*/
private function installDatabase()
{
$sql = 'CREATE TABLE IF NOT EXISTS ' . $this->tables[TABLE_PLUGIN_HOOK_OBSERVER] . '( ' .
'id int UNSIGNED NOT NULL AUTO_INCREMENT, ' .
'class_name varchar(255) UNIQUE, ' .
'path varchar(255) NOT NULL, ' .
'plugin_name varchar(255) NULL, ' .
'PRIMARY KEY PK_hook_management_hook_observer (id) ' .
'); ';
Database::query($sql);
$sql = 'CREATE TABLE IF NOT EXISTS ' . $this->tables[TABLE_PLUGIN_HOOK_EVENT] . '( ' .
'id int UNSIGNED NOT NULL AUTO_INCREMENT, ' .
'class_name varchar(255) UNIQUE, ' .
'description varchar(255), ' .
'PRIMARY KEY PK_hook_management_hook_event (id) ' .
'); ';
Database::query($sql);
$sql = 'CREATE TABLE IF NOT EXISTS ' . $this->tables[TABLE_PLUGIN_HOOK_CALL] . '( ' .
'id int UNSIGNED NOT NULL AUTO_INCREMENT, ' .
'hook_event_id int UNSIGNED NOT NULL, ' .
'hook_observer_id int UNSIGNED NOT NULL, ' .
'type tinyint NOT NULL, ' .
'hook_order int UNSIGNED NOT NULL, ' .
'enabled tinyint NOT NULL, ' .
'PRIMARY KEY PK_hook_management_hook_call (id) ' .
'); ';
Database::query($sql);
}
/**
* Initialize Database storing hooks (events, observers, calls)
* This should be called right after installDatabase method
* @return int
*/
public function initDatabase()
{
// Search hook events
$hookEvents = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, '\HookEvent')) {
$interfaces = class_implements($class);
$hookInterfaces = array();
foreach ($interfaces as $interface) {
$hookInterface = (preg_filter('/Hook(.+)EventInterface/', '$1', $interface));
if (!empty($hookInterface)) {
$hookInterfaces[] = $hookInterface;
}
}
$hookEvents[$class] = $hookInterfaces;
}
}
// Search hook observers
$hookObservers = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, '\HookObserver')) {
$interfaces = class_implements($class);
$hookInterfaces = array();
foreach ($interfaces as $interface) {
$hookInterface = (preg_filter('/Hook(.+)ObserverInterface/', '$1', $interface));
if (!empty($hookInterface)) {
$hookInterfaces[] =$hookInterface;
}
}
$hookObservers[$class] = $hookInterfaces;
}
}
// Search hook calls
$hookCalls = array();
foreach ($hookEvents as $hookEvent => $eventInterfaces) {
if (!empty($eventInterfaces)) {
$order = 0;
foreach ($hookObservers as $hookObserver => $observerInterfaces) {
if ($observerInterfaces === $eventInterfaces) {
$order += 1;
$hookCalls[] = array($hookEvent, $hookObserver, HOOK_TYPE_PRE, $order);
$hookCalls[] = array($hookEvent, $hookObserver, HOOK_TYPE_POST, $order);
}
}
}
}
// Insert hook events
foreach ($hookEvents as $hookEvent => $v) {
$attributes = array(
'class_name' => $hookEvent,
'description' => get_lang('HookDescription' . $hookEvent),
);
$id = Database::insert($this->tables[TABLE_PLUGIN_HOOK_EVENT], $attributes);
// store hook event into property
$this->hookEvents[$hookEvent] = $id;
}
// Insert hook observer
foreach ($hookObservers as $hookObserver => $v) {
$object = $hookObserver::create();
$attributes = array(
'class_name' => $hookObserver,
'path' => $object->getPath(),
'plugin_name' => $object->getPluginName(),
);
$id = Database::insert($this->tables[TABLE_PLUGIN_HOOK_OBSERVER], $attributes);
// store hook observer into property
$this->hookObservers[$hookObserver] = $id;
}
// Insert hook call
foreach ($hookCalls as $hookCall) {
$attributes = array(
'hook_event_id' => $this->hookEvents[$hookCall[0]],
'hook_observer_id' => $this->hookObservers[$hookCall[1]],
'type' => $hookCall[2],
'hook_order' => $hookCall[3],
'enabled' => 0,
);
// store hook call into property
$id = Database::insert($this->tables[TABLE_PLUGIN_HOOK_CALL], $attributes);
$this->hookCalls[$hookCall[0]][$hookCall[1]][$hookCall[2]] = $id;
}
return 1;
}
/**
* Drop the database tables for the plugin
* @return void
*/
private function uninstallDatabase()
{
$sql = 'DROP TABLE IF EXISTS ' . $this->tables[TABLE_PLUGIN_HOOK_CALL] . '; ';
Database::query($sql);
$sql = 'DROP TABLE IF EXISTS ' . $this->tables[TABLE_PLUGIN_HOOK_EVENT] . '; ';
Database::query($sql);
$sql = 'DROP TABLE IF EXISTS ' . $this->tables[TABLE_PLUGIN_HOOK_OBSERVER] . '; ';
Database::query($sql);
}
/**
* Insert hook into Database. Return insert id
* @param string $eventName
@ -203,14 +42,14 @@ class HookManagement implements HookManagementInterface
*/
public function insertHook($eventName, $observerClassName, $type)
{
if ($type === HOOK_TYPE_ALL) {
$this->insertHook($eventName, $observerClassName, HOOK_TYPE_PRE);
$this->insertHook($eventName, $observerClassName, HOOK_TYPE_POST);
if ($type === HOOK_EVENT_TYPE_ALL) {
$this->insertHook($eventName, $observerClassName, HOOK_EVENT_TYPE_PRE);
$this->insertHook($eventName, $observerClassName, HOOK_EVENT_TYPE_POST);
} else {
$this->insertHookIfNotExist($eventName, $observerClassName);
// Check if exists hook call
$row = Database::select('id, enabled',
$this->tables[TABLE_PLUGIN_HOOK_CALL],
$this->tables[TABLE_HOOK_CALL],
array(
'where' => array(
'hook_event_id = ? ' => $this->hookEvents[$eventName],
@ -224,7 +63,7 @@ class HookManagement implements HookManagementInterface
// Check if is hook call is active
if ((int) $row['enabled'] === 0) {
Database::update(
$this->tables[TABLE_PLUGIN_HOOK_CALL],
$this->tables[TABLE_HOOK_CALL],
array(
'enabled' => 1,
),
@ -247,14 +86,14 @@ class HookManagement implements HookManagementInterface
*/
public function deleteHook($eventName, $observerClassName, $type)
{
if ($type === HOOK_TYPE_ALL) {
$this->insertHook($eventName, $observerClassName, HOOK_TYPE_PRE);
$this->insertHook($eventName, $observerClassName, HOOK_TYPE_POST);
if ($type === HOOK_EVENT_TYPE_ALL) {
$this->insertHook($eventName, $observerClassName, HOOK_EVENT_TYPE_PRE);
$this->insertHook($eventName, $observerClassName, HOOK_EVENT_TYPE_POST);
} else {
$this->insertHookIfNotExist($eventName, $observerClassName);
Database::update(
$this->tables[TABLE_PLUGIN_HOOK_CALL],
$this->tables[TABLE_HOOK_CALL],
array(
'enabled' => 0,
),
@ -278,7 +117,7 @@ class HookManagement implements HookManagementInterface
foreach ($hookOrders as $oldOrder => $newOrder)
{
$res = Database::update(
$this->tables[TABLE_PLUGIN_HOOK_CALL],
$this->tables[TABLE_HOOK_CALL],
array(
'hook_order ' => $newOrder,
),
@ -304,10 +143,10 @@ class HookManagement implements HookManagementInterface
public function listHookObservers($eventName)
{
$array = array();
$joinTable = $this->tables[TABLE_PLUGIN_HOOK_CALL] . 'hc ' .
' INNER JOIN ' . $this->tables[TABLE_PLUGIN_HOOK_EVENT] . 'he ' .
$joinTable = $this->tables[TABLE_HOOK_CALL] . 'hc ' .
' INNER JOIN ' . $this->tables[TABLE_HOOK_EVENT] . 'he ' .
' ON hc.hook_event_id = he.id ' .
' INNER JOIN ' . $this->tables[TABLE_PLUGIN_HOOK_OBSERVER] . ' ho ' .
' INNER JOIN ' . $this->tables[TABLE_HOOK_OBSERVER] . ' ho ' .
' ON hc.hook_observer_id = ho.id ';
$columns = 'ho.class_name, ho.path, ho.plugin_name, hc.enabled';
$where = array('where' => array('he.class_name = ? ' => $eventName, 'AND hc.enabled = ? ' => 1));
@ -328,7 +167,7 @@ class HookManagement implements HookManagementInterface
{
$array = array();
$columns = 'id, class_name';
$rows = Database::select($columns, $this->tables[TABLE_PLUGIN_HOOK_OBSERVER]);
$rows = Database::select($columns, $this->tables[TABLE_HOOK_OBSERVER]);
foreach ($rows as $row) {
$array[$row['class_name']] = $row['id'];
@ -345,7 +184,7 @@ class HookManagement implements HookManagementInterface
{
$array = array();
$columns = 'id, class_name';
$rows = Database::select($columns, $this->tables[TABLE_PLUGIN_HOOK_EVENT]);
$rows = Database::select($columns, $this->tables[TABLE_HOOK_EVENT]);
foreach ($rows as $row) {
$array[$row['class_name']] = $row['id'];
@ -361,10 +200,10 @@ class HookManagement implements HookManagementInterface
public function listAllHookCalls()
{
$array = array();
$joinTable = $this->tables[TABLE_PLUGIN_HOOK_CALL] . 'hc ' .
' INNER JOIN ' . $this->tables[TABLE_PLUGIN_HOOK_EVENT] . 'he ' .
$joinTable = $this->tables[TABLE_HOOK_CALL] . 'hc ' .
' INNER JOIN ' . $this->tables[TABLE_HOOK_EVENT] . 'he ' .
' ON hc.hook_event_id = he.id ' .
' INNER JOIN ' . $this->tables[TABLE_PLUGIN_HOOK_OBSERVER] . ' ho ' .
' INNER JOIN ' . $this->tables[TABLE_HOOK_OBSERVER] . ' ho ' .
' ON hc.hook_observer_id = ho.id ';
$columns = 'he.class_name AS event_class_name, ho.class_name AS observer_class_name, hc.id AS id, hc.type AS type';
$rows = Database::select($columns, $joinTable);
@ -391,7 +230,7 @@ class HookManagement implements HookManagementInterface
'class_name' => $eventName,
'description' => get_lang('HookDescription' . $eventName),
);
$id = Database::insert($this->tables[TABLE_PLUGIN_HOOK_EVENT], $attributes);
$id = Database::insert($this->tables[TABLE_HOOK_EVENT], $attributes);
$this->hookEvents[$eventName] = $id;
}
@ -403,7 +242,7 @@ class HookManagement implements HookManagementInterface
'path' => $object->getPath(),
'plugin_name' => $object->getPluginName(),
);
$id = Database::insert($this->tables[TABLE_PLUGIN_HOOK_OBSERVER], $attributes);
$id = Database::insert($this->tables[TABLE_HOOK_OBSERVER], $attributes);
$this->hookObservers[$observerClassName] = $id;
}
@ -416,11 +255,11 @@ class HookManagement implements HookManagementInterface
$row = Database::select(
'MAX(hook_order) as hook_order',
$this->tables[TABLE_PLUGIN_HOOK_CALL],
$this->tables[TABLE_HOOK_CALL],
array(
'where' => array(
'hook_event_id = ? ' =>$this->hookEvents[$eventName],
'AND type = ? ' => HOOK_TYPE_PRE,
'AND type = ? ' => HOOK_EVENT_TYPE_PRE,
),
),
'ASSOC'
@ -428,27 +267,27 @@ class HookManagement implements HookManagementInterface
// Check if exists hook call
$id = Database::insert(
$this->tables[TABLE_PLUGIN_HOOK_CALL],
$this->tables[TABLE_HOOK_CALL],
array(
'hook_event_id' => $this->hookEvents[$eventName],
'hook_observer_id' => $this->hookObservers[$observerClassName],
'type' => HOOK_TYPE_PRE,
'type' => HOOK_EVENT_TYPE_PRE,
'enabled' => 0,
'hook_order' => $row['hook_order'] + 1,
)
);
$this->hookCalls[$eventName][$observerClassName][HOOK_TYPE_PRE] = $id;
$this->hookCalls[$eventName][$observerClassName][HOOK_EVENT_TYPE_PRE] = $id;
// HOOK TYPE POST
$row = Database::select(
'MAX(hook_order) as hook_order',
$this->tables[TABLE_PLUGIN_HOOK_CALL],
$this->tables[TABLE_HOOK_CALL],
array(
'where' => array(
'hook_event_id = ? ' =>$this->hookEvents[$eventName],
'AND type = ? ' => HOOK_TYPE_POST,
'AND type = ? ' => HOOK_EVENT_TYPE_POST,
),
),
'ASSOC'
@ -456,17 +295,17 @@ class HookManagement implements HookManagementInterface
// Check if exists hook call
$id = Database::insert(
$this->tables[TABLE_PLUGIN_HOOK_CALL],
$this->tables[TABLE_HOOK_CALL],
array(
'hook_event_id' => $this->hookEvents[$eventName],
'hook_observer_id' => $this->hookObservers[$observerClassName],
'type' => HOOK_TYPE_POST,
'type' => HOOK_EVENT_TYPE_POST,
'enabled' => 0,
'hook_order' => $row['hook_order'] + 1,
)
);
$this->hookCalls[$eventName][$observerClassName][HOOK_TYPE_POST] = $id;
$this->hookCalls[$eventName][$observerClassName][HOOK_EVENT_TYPE_POST] = $id;
} elseif (isset($eventName) && !isset($observerClassName)) {
foreach ($this->hookObservers as $observer => $id) {
@ -483,9 +322,9 @@ class HookManagement implements HookManagementInterface
/**
* Return the hook call id identified by hook event, hook observer and type
* @param $eventName
* @param $observerClassName
* @param $type
* @param string $eventName
* @param string $observerClassName
* @param int $type
* @return mixed
*/
public function getHookCallId($eventName, $observerClassName, $type)
@ -493,10 +332,10 @@ class HookManagement implements HookManagementInterface
$eventName = Database::escape_string($eventName);
$observerClassName($observerClassName);
$type = Database::escape_string($type);
$joinTable = $this->tables[TABLE_PLUGIN_HOOK_CALL] . 'hc ' .
' INNER JOIN ' . $this->tables[TABLE_PLUGIN_HOOK_EVENT] . 'he ' .
$joinTable = $this->tables[TABLE_HOOK_CALL] . 'hc ' .
' INNER JOIN ' . $this->tables[TABLE_HOOK_EVENT] . 'he ' .
' ON hc.hook_event_id = he.id ' .
' INNER JOIN ' . $this->tables[TABLE_PLUGIN_HOOK_OBSERVER] . ' ho ' .
' INNER JOIN ' . $this->tables[TABLE_HOOK_OBSERVER] . ' ho ' .
' ON hc.hook_observer_id = ho.id ';
$row = Database::select(
'id',

@ -1,7 +1,18 @@
<?php
/* For licensing terms, see /license.txt */
/**
* This file contains an abstract Hook observer class
* Used for Hook Observers in plugins, called when a hook event happens
* (e.g Create user, Webservice registration)
* @package chamilo.library.hook
*/
/**
* Class HookObserver
* This abstract class implements Hook Observer Interface to build the base
* for Hook Observer. This class have some public static method,
* e.g for create Hook Observers
*/
abstract class HookObserver implements HookObserverInterface
{
public $path;
@ -9,6 +20,8 @@ abstract class HookObserver implements HookObserverInterface
/**
* Construct method
* Save the path of Hook Observer class implementation and
* the plugin name where this class is included
* @param string $path
* @param string $pluginName
*/

@ -1,38 +1,53 @@
<?php
/* For licensing terms, see /license.txt /
/**
* Created by PhpStorm.
* User: dbarreto
* Date: 19/12/14
* Time: 09:45 AM
* This file contains a Hook Event class for Admin Block.
* @package chamilo.library.hook
*/
/**
* Class HookWSRegistration
* This class is a Hook event implementing Webservice Registration Event interface.
* This class is used to modify ws for registration by notifying Hook Observer for Webservice registration
*/
class HookWSRegistration extends HookEvent implements HookWSRegistrationEventInterface
{
/**
* Construct
*/
protected function __construct()
{
parent::__construct('HookWSRegistration');
}
/**
* @param int $type
* Notify all Hook observer for WS Registration.
* This save "server" (soap server) and send to Hook observer to be modified
* (e.g. add more registration webservice)
* @param int $type Set the type of hook event called. 0: HOOK_TYPE_PRE, 1: HOOK_TYPE_POST
* @return int
*/
public function notifyWSRegistration($type)
{
/** @var \HookWSRegistrationObserverInterface $observer */
// check if already have server data
if (!isset($this->eventData['server'])) {
global $server;
$this->eventData['server'] = $server;
}
$this->eventData['type'] = $type;
foreach ($this->observers as $observer) {
$data = $observer->hookWSRegistration($this);
$this->eventData['server'] = $data['server'];
if (isset($server)) {
$server = $this->eventData['server'] ;
// Save Hook event type data
$this->eventData['type'] = $type;
foreach ($this->observers as $observer) {
// Notify all registered observers
$data = $observer->hookWSRegistration($this);
// check if server is not null
if (isset($data['server'])) {
// Get modified server
$this->eventData['server'] = $data['server'];
}
}
return $this->eventData;
}
return 1;
}
}

@ -355,10 +355,10 @@ define('CATALOG_COURSES', 0);
define('CATALOG_SESSIONS', 1);
define('CATALOG_COURSES_SESSIONS', 2);
//Hook
define('HOOK_TYPE_PRE', 0);
define('HOOK_TYPE_POST', 1);
define('HOOK_TYPE_ALL', 10);
//Hook type events, pre-proccess and post-process. All means to be executed for both hook event types
define('HOOK_EVENT_TYPE_PRE', 0);
define('HOOK_EVENT_TYPE_POST', 1);
define('HOOK_EVENT_TYPE_ALL', 10);
define('HOOK_MANAGEMENT', 'hookmanagement');

@ -82,7 +82,7 @@ class UserManager
) {
$hook = HookCreateUser::create();
if (!empty($hook)) {
$hook->notifyCreateUser(HOOK_TYPE_PRE);
$hook->notifyCreateUser(HOOK_EVENT_TYPE_PRE);
}
global $_user, $_configuration;
$original_password = $password;
@ -247,7 +247,7 @@ class UserManager
self::update_extra_field_value($return, 'already_logged_in', 'false');
if (!empty($hook)) {
$hook->notifyCreateUser(HOOK_TYPE_POST);
$hook->notifyCreateUser(HOOK_EVENT_TYPE_POST);
}
return $return;
}
@ -591,7 +591,7 @@ class UserManager
) {
$hook = HookUpdateUser::create();
if (!empty($hook)) {
$hook->notifyUpdateUser(HOOK_TYPE_PRE);
$hook->notifyUpdateUser(HOOK_EVENT_TYPE_PRE);
}
global $_configuration;
$original_password = $password;
@ -702,7 +702,7 @@ class UserManager
}
if (!empty($hook)) {
$hook->notifyUpdateUser(HOOK_TYPE_POST);
$hook->notifyUpdateUser(HOOK_EVENT_TYPE_POST);
}
return $return;

@ -83,8 +83,7 @@ $server = new soap_server();
$hook = HookWSRegistration::create();
if (!empty($hook)) {
$hook->setEventData(array('server' => $server));
$hook->notifyWSRegistration(HOOK_TYPE_PRE);
$res = $hook->getEventData();
$res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_PRE);
if (!empty($res['server'])) {
$server = $res['server'];
}
@ -5546,8 +5545,7 @@ function WSUserSubscribedInCourse ($params)
// Add more webservices by Hooks
if (!empty($hook)) {
$hook->setEventData(array('server' => $server));
$hook->notifyWSRegistration(HOOK_TYPE_POST);
$res = $hook->getEventData();
$res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_POST);
if (!empty($res['server'])) {
$server = $res['server'];
}

Loading…
Cancel
Save