Merge pull request #635 from ilosada/BT9438

B#9438 - Limit session resubscription
1.10.x
Julio 11 years ago
commit 3d0617d42e
  1. 16
      main/auth/courses_controller.php
  2. 33
      main/inc/lib/hook/HookResubscribe.php
  3. 21
      main/inc/lib/hook/interfaces/HookResubscribeEventInterface.php
  4. 20
      main/inc/lib/hook/interfaces/HookResubscribeObserverInterface.php
  5. 13
      main/inc/lib/plugin.class.php
  6. 4
      plugin/resubscription/README.md
  7. 9
      plugin/resubscription/config.php
  8. 8
      plugin/resubscription/index.php
  9. 10
      plugin/resubscription/install.php
  10. 13
      plugin/resubscription/lang/english.php
  11. 13
      plugin/resubscription/lang/spanish.php
  12. 10
      plugin/resubscription/plugin.php
  13. 2
      plugin/resubscription/readme.txt
  14. 105
      plugin/resubscription/src/HookResubscription.php
  15. 78
      plugin/resubscription/src/Resubscription.php
  16. 10
      plugin/resubscription/uninstall.php

@ -495,9 +495,23 @@ 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)
));
try {
$hook->notifyResubscribe(HOOK_EVENT_TYPE_PRE);
} catch (Exception $exception) {
$result = $exception->getMessage();
}
}
return $result;
}
/**

@ -0,0 +1,33 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Class HookResubscribe
* @var \SplObjectStorage $observers
*/
class HookResubscribe extends HookEvent implements HookResubscribeEventInterface
{
/**
* Constructor
*/
protected function __construct()
{
parent::__construct('HookResubscribe');
}
/**
* Update all the observers
* @param int $type
*
* @return int
*/
public function notifyResubscribe($type)
{
/** @var \HookResubscribeObserverInterface $observer */
$this->eventData['type'] = $type;
foreach ($this->observers as $observer) {
$observer->hookResubscribe($this);
}
return 1;
}
}

@ -0,0 +1,21 @@
<?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 HookResubscribeEventInterface
*/
interface HookResubscribeEventInterface extends HookEventInterface
{
/**
* Update all the observers
* @param int $type
*
* @return int
*/
public function notifyResubscribe($type);
}

@ -0,0 +1,20 @@
<?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 ResubscribeHookInterface
*/
interface HookResubscribeObserverInterface extends HookObserverInterface
{
/**
* @param HookResubscribeObserverInterface $hook
*
* @return int
*/
public function hookResubscribe(HookResubscribeEventInterface $hook);
}

@ -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;
}
}

@ -0,0 +1,4 @@
Resubscription
==============
Limit session resubscriptions

@ -0,0 +1,9 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Config the plugin
* @author Imanol Losada Oriol <imanol.losada@beeznest.com>
* @package chamilo.plugin.resubscription
*/
require_once api_get_path(SYS_PATH) . 'main/inc/global.inc.php';

@ -0,0 +1,8 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Config the plugin
* @author Imanol Losada Oriol <imanol.losada@beeznest.com>
* @package chamilo.plugin.resubscription
*/
require_once __DIR__ . '/config.php';

@ -0,0 +1,10 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Initialization install
* @author Imanol Losada Oriol <imanol.losada@beeznest.com>
* @package chamilo.plugin.resubscription
*/
require_once __DIR__ . '/config.php';
Resubscription::create()->install();

@ -0,0 +1,13 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Strings to english L10n
* @author Imanol Losada Oriol <imanol.losada@beeznest.com>
* @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';

@ -0,0 +1,13 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Strings to spanish L10n
* @author Imanol Losada Oriol <imanol.losada@beeznest.com>
* @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';

@ -0,0 +1,10 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Get the plugin info
* @author Imanol Losada Oriol <imanol.losada@beeznest.com>
* @package chamilo.plugin.resubscription
*/
require_once __DIR__.'/config.php';
$plugin_info = Resubscription::create()->get_info();

@ -0,0 +1,2 @@
<h1>Resubscription</h1>
<p>Limit session resubscriptions</p>

@ -0,0 +1,105 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Hook to limit session resubscriptions
*
* @author Imanol Losada Oriol <imanol.losada@beeznest.com>
* @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');
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";
// 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"));
$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"));
}
}
}
}
}

@ -0,0 +1,78 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Limit session resubscriptions
*
* @author Imanol Losada Oriol <imanol.losada@beeznest.com>
* @package chamilo.plugin.resubscription
*/
class Resubscription extends Plugin implements HookPluginInterface
{
/**
* Class constructor
*/
protected function __construct()
{
$options = array(
'calendar_year' => get_lang('CalendarYear')
);
$parameters = array(
'resubscription_limit' => array(
'type' => 'select',
'options' => $options
)
);
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);
}
}

@ -0,0 +1,10 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Initialization uninstall
* @author Imanol Losada Oriol <imanol.losada@beeznest.com>
* @package chamilo.plugin.resubscription
*/
require_once __DIR__ . '/config.php';
Resubscription::create()->uninstall();
Loading…
Cancel
Save