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 @@ +
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();