From 05ae9e78b158ac7b6cbed38cac7288458d1ca6c1 Mon Sep 17 00:00:00 2001 From: Imanol Losada Date: Tue, 31 Mar 2015 15:46:00 -0500 Subject: [PATCH 1/6] Add plugin files - refs BT#9438 --- plugin/resubscription/README.md | 4 + plugin/resubscription/config.php | 9 ++ plugin/resubscription/index.php | 8 ++ plugin/resubscription/install.php | 10 ++ plugin/resubscription/lang/english.php | 13 +++ plugin/resubscription/lang/spanish.php | 13 +++ plugin/resubscription/plugin.php | 10 ++ plugin/resubscription/readme.txt | 2 + .../resubscription/src/HookResubscription.php | 98 +++++++++++++++++++ plugin/resubscription/src/Resubscription.php | 72 ++++++++++++++ plugin/resubscription/uninstall.php | 10 ++ 11 files changed, 249 insertions(+) create mode 100644 plugin/resubscription/README.md create mode 100644 plugin/resubscription/config.php create mode 100644 plugin/resubscription/index.php create mode 100644 plugin/resubscription/install.php create mode 100644 plugin/resubscription/lang/english.php create mode 100644 plugin/resubscription/lang/spanish.php create mode 100644 plugin/resubscription/plugin.php create mode 100644 plugin/resubscription/readme.txt create mode 100644 plugin/resubscription/src/HookResubscription.php create mode 100644 plugin/resubscription/src/Resubscription.php create mode 100644 plugin/resubscription/uninstall.php diff --git a/plugin/resubscription/README.md b/plugin/resubscription/README.md new file mode 100644 index 0000000000..fc64aea406 --- /dev/null +++ b/plugin/resubscription/README.md @@ -0,0 +1,4 @@ +Resubscription +============== + +Limit session resubscriptions diff --git a/plugin/resubscription/config.php b/plugin/resubscription/config.php new file mode 100644 index 0000000000..cb9728ee88 --- /dev/null +++ b/plugin/resubscription/config.php @@ -0,0 +1,9 @@ + + * @package chamilo.plugin.resubscription + */ + +require_once api_get_path(SYS_PATH) . 'main/inc/global.inc.php'; diff --git a/plugin/resubscription/index.php b/plugin/resubscription/index.php new file mode 100644 index 0000000000..8395e46a7b --- /dev/null +++ b/plugin/resubscription/index.php @@ -0,0 +1,8 @@ + + * @package chamilo.plugin.resubscription + */ +require_once __DIR__ . '/config.php'; diff --git a/plugin/resubscription/install.php b/plugin/resubscription/install.php new file mode 100644 index 0000000000..d3246edaf1 --- /dev/null +++ b/plugin/resubscription/install.php @@ -0,0 +1,10 @@ + + * @package chamilo.plugin.resubscription + */ +require_once __DIR__ . '/config.php'; + +Resubscription::create()->install(); diff --git a/plugin/resubscription/lang/english.php b/plugin/resubscription/lang/english.php new file mode 100644 index 0000000000..d8a1cd069e --- /dev/null +++ b/plugin/resubscription/lang/english.php @@ -0,0 +1,13 @@ + + * @package chamilo.plugin.resubscription + */ +$strings['plugin_title'] = 'Resubscription'; +$strings['plugin_comment'] = 'This plugin limits session resubscription.'; + +$strings['resubscription_limit'] = 'Resubscription limit'; +$strings['resubscription_limit_help'] = 'This limits how often a user can be resubscribed'; +$strings['CanResubscribeFromX'] = 'Subscription available from %s'; diff --git a/plugin/resubscription/lang/spanish.php b/plugin/resubscription/lang/spanish.php new file mode 100644 index 0000000000..5f489fb8d3 --- /dev/null +++ b/plugin/resubscription/lang/spanish.php @@ -0,0 +1,13 @@ + + * @package chamilo.plugin.resubscription + */ +$strings['plugin_title'] = 'Reinscripción'; +$strings['plugin_comment'] = 'Este plugin limita las reinscripiones a sesiones.'; + +$strings['resubscription_limit'] = 'Límite de reinscripción'; +$strings['resubscription_limit_help'] = 'Esto limita cada cuánto puede reinscribirse un usuario'; +$strings['CanResubscribeFromX'] = 'Inscripción posible a partir del %s'; diff --git a/plugin/resubscription/plugin.php b/plugin/resubscription/plugin.php new file mode 100644 index 0000000000..bf3a895d90 --- /dev/null +++ b/plugin/resubscription/plugin.php @@ -0,0 +1,10 @@ + + * @package chamilo.plugin.resubscription + */ +require_once __DIR__.'/config.php'; + +$plugin_info = Resubscription::create()->get_info(); diff --git a/plugin/resubscription/readme.txt b/plugin/resubscription/readme.txt new file mode 100644 index 0000000000..53d3045a51 --- /dev/null +++ b/plugin/resubscription/readme.txt @@ -0,0 +1,2 @@ +

Resubscription

+

Limit session resubscriptions

diff --git a/plugin/resubscription/src/HookResubscription.php b/plugin/resubscription/src/HookResubscription.php new file mode 100644 index 0000000000..794ba7ac7a --- /dev/null +++ b/plugin/resubscription/src/HookResubscription.php @@ -0,0 +1,98 @@ + + * @package chamilo.plugin.resubscription + */ +class HookResubscription extends HookObserver implements HookResubscribeObserverInterface +{ + + /** + * Class constructor + */ + public function __construct() + { + parent::__construct( + 'plugin/resubscription/src/Resubscription.php', 'resubscription' + ); + } + + /** + * Limit session resubscription when a Chamilo user is resubscribed to a session + * @param HookCreateUserEventInterface $hook The hook + */ + public function hookResubscribe(HookResubscribeEventInterface $hook) + { + $data = $hook->getEventData(); + if ($data['type'] === HOOK_EVENT_TYPE_PRE) { + $resubscriptionLimit = Resubscription::create()->get('resubscription_limit'); + $limitDate = gmdate('Y-m-d', strtotime(gmdate('Y-m-d')." -$resubscriptionLimit year")); + + $join = " INNER JOIN ".Database::get_main_table(TABLE_MAIN_SESSION)."ON id = id_session"; + + // User sessions and courses + $userSessions = Database::select( + 'id_session, date_end', + Database::get_main_table(TABLE_MAIN_SESSION_USER).$join, + array( + 'where' => array( + 'id_user = ? AND date_end >= ?' => array( + api_get_user_id(), + $limitDate + ) + ), + 'order' => 'date_end DESC' + ) + ); + $userSessionCourses = array(); + foreach ($userSessions as $userSession) { + $userSessionCourseResult = Database::select( + 'course_code', + Database::get_main_table(TABLE_MAIN_SESSION_COURSE), + array( + 'where' => array( + 'id_session = ?' => array( + $userSession['id_session'] + ) + ) + ) + ); + foreach ($userSessionCourseResult as $userSessionCourse) { + if (!isset($userSessionCourses[$userSessionCourse['course_code']])) { + $userSessionCourses[$userSessionCourse['course_code']] = $userSession['date_end']; + } + + } + } + + // Current session and courses + $currentSessionCourseResult = Database::select( + 'course_code', + Database::get_main_table(TABLE_MAIN_SESSION_COURSE), + array( + 'where' => array( + 'id_session = ?' => array( + $data['session_id'] + ) + ) + ) + ); + + // Check if current course code matches with one of the users + foreach ($currentSessionCourseResult as $currentSessionCourse) { + if (isset($userSessionCourses[$currentSessionCourse['course_code']])) { + $endDate = $userSessionCourses[$currentSessionCourse['course_code']]; + $resubscriptionDate = gmdate('Y-m-d', strtotime($endDate." +$resubscriptionLimit year")); + + $icon = Display::return_icon('students.gif', get_lang('Student')); + $canResubscribeFrom = sprintf(get_plugin_lang('CanResubscribeFromX', 'resubscription'), $resubscriptionDate); + $data['result'] = Display::label($icon . ' ' . $canResubscribeFrom, "info"); + } + } + } + } + +} diff --git a/plugin/resubscription/src/Resubscription.php b/plugin/resubscription/src/Resubscription.php new file mode 100644 index 0000000000..a1e9554122 --- /dev/null +++ b/plugin/resubscription/src/Resubscription.php @@ -0,0 +1,72 @@ + + * @package chamilo.plugin.resubscription + */ +class Resubscription extends Plugin implements HookPluginInterface +{ + + /** + * Class constructor + */ + protected function __construct() + { + $parameters = array( + 'resubscription_limit' => 'text' + ); + + parent::__construct('0.1', 'Imanol Losada Oriol', $parameters); + } + + /** + * Instance the plugin + * @staticvar null $result + * @return Resubscription + */ + static function create() + { + static $result = null; + + return $result ? $result : $result = new self(); + } + + /** + * Install the plugin + */ + public function install() + { + $this->installHook(); + } + + /** + * Uninstall the plugin + * @return void + */ + public function uninstall() + { + $this->uninstallHook(); + } + + /** + * Install the Resubscription hook + */ + public function installHook() + { + $hook = HookResubscription::create(); + HookResubscribe::create()->attach($hook); + } + + /** + * Uninstall the Resubscription hook + */ + public function uninstallHook() + { + $hook = HookResubscription::create(); + HookResubscribe::create()->detach($hook); + } + +} diff --git a/plugin/resubscription/uninstall.php b/plugin/resubscription/uninstall.php new file mode 100644 index 0000000000..7a7e9e53a0 --- /dev/null +++ b/plugin/resubscription/uninstall.php @@ -0,0 +1,10 @@ + + * @package chamilo.plugin.resubscription + */ +require_once __DIR__ . '/config.php'; + +Resubscription::create()->uninstall(); From 99f462bc102f65a24571d7f72910ce6753fbd1e4 Mon Sep 17 00:00:00 2001 From: Imanol Losada Date: Tue, 31 Mar 2015 15:46:52 -0500 Subject: [PATCH 2/6] Add hook files - refs BT#9438 --- main/inc/lib/hook/HookResubscribe.php | 33 +++++++++++++++++++ .../HookResubscribeEventInterface.php | 21 ++++++++++++ .../HookResubscribeObserverInterface.php | 20 +++++++++++ 3 files changed, 74 insertions(+) create mode 100644 main/inc/lib/hook/HookResubscribe.php create mode 100644 main/inc/lib/hook/interfaces/HookResubscribeEventInterface.php create mode 100644 main/inc/lib/hook/interfaces/HookResubscribeObserverInterface.php diff --git a/main/inc/lib/hook/HookResubscribe.php b/main/inc/lib/hook/HookResubscribe.php new file mode 100644 index 0000000000..c88bdfa7ac --- /dev/null +++ b/main/inc/lib/hook/HookResubscribe.php @@ -0,0 +1,33 @@ +eventData['type'] = $type; + foreach ($this->observers as $observer) { + $observer->hookResubscribe($this); + } + return 1; + } +} diff --git a/main/inc/lib/hook/interfaces/HookResubscribeEventInterface.php b/main/inc/lib/hook/interfaces/HookResubscribeEventInterface.php new file mode 100644 index 0000000000..e386c032fd --- /dev/null +++ b/main/inc/lib/hook/interfaces/HookResubscribeEventInterface.php @@ -0,0 +1,21 @@ + Date: Tue, 31 Mar 2015 15:47:58 -0500 Subject: [PATCH 3/6] Add hook call - refs BT#9438 --- main/auth/courses_controller.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/main/auth/courses_controller.php b/main/auth/courses_controller.php index f233dce683..45d40a7369 100755 --- a/main/auth/courses_controller.php +++ b/main/auth/courses_controller.php @@ -495,9 +495,20 @@ class CoursesController api_get_path(WEB_CODE_PATH)."inc/email_editor.php?action=subscribe_me_to_session&session=". Security::remove_XSS($sessionData); - return Display::url(get_lang('Subscribe'), $url, array( + $result = Display::url(get_lang('Subscribe'), $url, array( 'class' => 'btn btn-large btn-primary', )); + + $hook = HookResubscribe::create(); + if (!empty($hook)) { + $hook->setEventData(array( + 'session_id' => intval($sessionData), + 'result' => &$result + )); + $hook->notifyResubscribe(HOOK_EVENT_TYPE_PRE); + } + + return $result; } /** From 48db79691beaa847697e550638370574c16c69bb Mon Sep 17 00:00:00 2001 From: Imanol Losada Date: Tue, 31 Mar 2015 16:53:51 -0500 Subject: [PATCH 4/6] Add exception to hook - refs BT#9438 --- main/auth/courses_controller.php | 9 ++++++--- plugin/resubscription/src/HookResubscription.php | 3 +-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/main/auth/courses_controller.php b/main/auth/courses_controller.php index 45d40a7369..3ba40b19b5 100755 --- a/main/auth/courses_controller.php +++ b/main/auth/courses_controller.php @@ -502,10 +502,13 @@ class CoursesController $hook = HookResubscribe::create(); if (!empty($hook)) { $hook->setEventData(array( - 'session_id' => intval($sessionData), - 'result' => &$result + 'session_id' => intval($sessionData) )); - $hook->notifyResubscribe(HOOK_EVENT_TYPE_PRE); + try { + $hook->notifyResubscribe(HOOK_EVENT_TYPE_PRE); + } catch (Exception $exception) { + $result = $exception->getMessage(); + } } return $result; diff --git a/plugin/resubscription/src/HookResubscription.php b/plugin/resubscription/src/HookResubscription.php index 794ba7ac7a..e0c4e0cb0b 100644 --- a/plugin/resubscription/src/HookResubscription.php +++ b/plugin/resubscription/src/HookResubscription.php @@ -86,10 +86,9 @@ class HookResubscription extends HookObserver implements HookResubscribeObserver if (isset($userSessionCourses[$currentSessionCourse['course_code']])) { $endDate = $userSessionCourses[$currentSessionCourse['course_code']]; $resubscriptionDate = gmdate('Y-m-d', strtotime($endDate." +$resubscriptionLimit year")); - $icon = Display::return_icon('students.gif', get_lang('Student')); $canResubscribeFrom = sprintf(get_plugin_lang('CanResubscribeFromX', 'resubscription'), $resubscriptionDate); - $data['result'] = Display::label($icon . ' ' . $canResubscribeFrom, "info"); + throw new Exception(Display::label($icon . ' ' . $canResubscribeFrom, "info")); } } } From 9c1e76cde9955acd68939e6650e4b62d1db2b0bd Mon Sep 17 00:00:00 2001 From: Imanol Losada Date: Tue, 31 Mar 2015 17:37:04 -0500 Subject: [PATCH 5/6] Add select support to plugin configuration page - refs BT#9438 --- main/inc/lib/plugin.class.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/main/inc/lib/plugin.class.php b/main/inc/lib/plugin.class.php index 3bd14ac4dc..05e0d14957 100755 --- a/main/inc/lib/plugin.class.php +++ b/main/inc/lib/plugin.class.php @@ -180,6 +180,11 @@ class Plugin } foreach ($this->fields as $name => $type) { + $options = null; + if (is_array($type) && isset($type['type']) && $type['type'] === "select") { + $options = $type['options']; + $type = $type['type']; + } $value = $this->get($name); @@ -230,6 +235,14 @@ class Plugin $element->_attributes['value'] = 'true'; $checkboxGroup[] = $element; break; + case 'select': + $result->addElement( + $type, + $name, + array($this->get_lang($name), $help), + $options + ); + break; } } From e3839a7a331aca987849d01ccd689b988ef60e70 Mon Sep 17 00:00:00 2001 From: Imanol Losada Date: Tue, 31 Mar 2015 17:38:39 -0500 Subject: [PATCH 6/6] Replace configuration textfield with select - refs BT#9438 --- plugin/resubscription/src/HookResubscription.php | 14 +++++++++++--- plugin/resubscription/src/Resubscription.php | 8 +++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/plugin/resubscription/src/HookResubscription.php b/plugin/resubscription/src/HookResubscription.php index e0c4e0cb0b..e71b675fba 100644 --- a/plugin/resubscription/src/HookResubscription.php +++ b/plugin/resubscription/src/HookResubscription.php @@ -28,8 +28,17 @@ class HookResubscription extends HookObserver implements HookResubscribeObserver { $data = $hook->getEventData(); if ($data['type'] === HOOK_EVENT_TYPE_PRE) { + $resubscriptionLimit = Resubscription::create()->get('resubscription_limit'); - $limitDate = gmdate('Y-m-d', strtotime(gmdate('Y-m-d')." -$resubscriptionLimit year")); + + $limitDate = gmdate('Y-m-d'); + + switch ($resubscriptionLimit) { + case 'calendar_year': + $resubscriptionLimit = "1 year"; + $limitDate = gmdate('Y-m-d', strtotime(gmdate('Y-m-d')." -$resubscriptionLimit")); + break; + } $join = " INNER JOIN ".Database::get_main_table(TABLE_MAIN_SESSION)."ON id = id_session"; @@ -85,7 +94,7 @@ class HookResubscription extends HookObserver implements HookResubscribeObserver foreach ($currentSessionCourseResult as $currentSessionCourse) { if (isset($userSessionCourses[$currentSessionCourse['course_code']])) { $endDate = $userSessionCourses[$currentSessionCourse['course_code']]; - $resubscriptionDate = gmdate('Y-m-d', strtotime($endDate." +$resubscriptionLimit year")); + $resubscriptionDate = gmdate('Y-m-d', strtotime($endDate." +$resubscriptionLimit")); $icon = Display::return_icon('students.gif', get_lang('Student')); $canResubscribeFrom = sprintf(get_plugin_lang('CanResubscribeFromX', 'resubscription'), $resubscriptionDate); throw new Exception(Display::label($icon . ' ' . $canResubscribeFrom, "info")); @@ -93,5 +102,4 @@ class HookResubscription extends HookObserver implements HookResubscribeObserver } } } - } diff --git a/plugin/resubscription/src/Resubscription.php b/plugin/resubscription/src/Resubscription.php index a1e9554122..e30636045b 100644 --- a/plugin/resubscription/src/Resubscription.php +++ b/plugin/resubscription/src/Resubscription.php @@ -15,8 +15,14 @@ class Resubscription extends Plugin implements HookPluginInterface */ protected function __construct() { + $options = array( + 'calendar_year' => get_lang('CalendarYear') + ); $parameters = array( - 'resubscription_limit' => 'text' + 'resubscription_limit' => array( + 'type' => 'select', + 'options' => $options + ) ); parent::__construct('0.1', 'Imanol Losada Oriol', $parameters);