Allow subscription to open session - refs BT#10175

1.10.x
Angel Fernando Quiroz Campos 9 years ago
parent e1be9c9864
commit f0f62976b2
  1. 6
      plugin/advanced_subscription/lang/english.php
  2. 6
      plugin/advanced_subscription/lang/spanish.php
  3. 80
      plugin/advanced_subscription/src/AdvancedSubscriptionPlugin.php
  4. 2
      plugin/advanced_subscription/src/HookAdvancedSubscription.php
  5. 59
      plugin/advanced_subscription/src/open_session.php
  6. 54
      plugin/advanced_subscription/views/open_session.tpl

@ -55,6 +55,12 @@ $strings['MailRemindSuperior'] = 'Subscription request are pending your approval
$strings['MailRemindAdmin'] = 'Course subscription are pending your approval';
// TPL langs
$strings['SessionXWithoutVacancies'] = "The course \"%s\" has no vacancies.";
$strings['SuccessSubscriptionToSessionX'] = "<h4>¡Congratulations!</h4>Your subscription to \"%s\" course has been completed successfully.";
$strings['SubscriptionToOpenSession'] = "Subscription to open course";
$strings['GoToSessionX'] = "Go to \"%s\" course";
$strings['YouAreAlreadySubscribedToSessionX'] = "You are already subscribed to \"%s\" course.";
// Admin view
$strings['SelectASession'] = 'Select a training session';
$strings['SessionName'] = 'Session name';

@ -55,6 +55,12 @@ $strings['MailRemindSuperior'] = 'Solicitudes de inscripción estan pendientes d
$strings['MailRemindAdmin'] = 'Inscripciones de cursos estan pendientes de tu aprobación';
// TPL translations
$strings['SessionXWithoutVacancies'] = "El curso \"%s\" no tiene cupos disponibles.";
$strings['SuccessSubscriptionToSessionX'] = "<h4>¡Felicitaciones!</h4> Tu inscripción al curso \"%s\" se realizó correctamente.";
$strings['SubscriptionToOpenSession'] = "Inscripcion a curso abierto";
$strings['GoToSessionX'] = "Ir al curso \"%s\"";
$strings['YouAreAlreadySubscribedToSessionX'] = "Usted ya está inscrito al curso \"%s\".";
// Admin view
$strings['SelectASession'] = 'Elija una sesión de formación';
$strings['SessionName'] = 'Nombre de la sesión';

@ -145,6 +145,60 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface
return $this->errorMessages;
}
/**
* Check if is allowed subscribe to open session
* @param array $params WS params
* @return boolean
*/
public function isAllowedSubscribeToOpenSession($params)
{
$self = self::create();
$wsUrl = $self->get('ws_url');
if (!empty($wsUrl)) {
$client = new SoapClient(
null,
['location' => $wsUrl, 'uri' => $wsUrl]
);
$userInfo = api_get_user_info($params['user_id'], false, false, true);
try {
$profileCompleted = $client->__soapCall(
'getProfileCompletionPercentage',
$userInfo['extra']['drupal_user_id']
);
} catch (\Exception $e) {
$profileCompleted = 0;
}
} elseif (isset($params['profile_completed'])) {
$profileCompleted = (float) $params['profile_completed'];
} else {
$profileCompleted = 0;
}
$profileCompletedMin = (float) $self->get('min_profile_percentage');
if ($profileCompleted < $profileCompletedMin) {
$this->errorMessages[] = sprintf(
$this->get_lang('AdvancedSubscriptionProfileIncomplete'),
$profileCompletedMin,
$profileCompleted
);
}
$vacancy = $self->getVacancy($params['session_id']);
$sessionInfo = api_get_session_info($params['session_id']);
if ($sessionInfo['nbr_users'] >= $vacancy) {
$this->errorMessages[] = sprintf(
$this->get_lang('SessionXWithoutVacancies'),
$sessionInfo['name']
);
}
return empty($this->errorMessages);
}
/**
* Return true if user is allowed to be added to queue for session subscription
* @param int $userId
@ -1025,6 +1079,32 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface
return $url;
}
/**
* Get a url for subscribe a user in session
* @param int $userId The user ID
* @param array $params Params from WS
* @return string
*/
public function getOpenSessionUrl($userId, $params)
{
$params['secret_key'] = null;
$params['user_id'] = null;
$params['user_field'] = null;
$params['is_connected'] = null;
$urlParams = array_merge($params, ['user_id' => $userId]);
$url = api_get_path(WEB_PLUGIN_PATH);
$url .= 'advanced_subscription/src/open_session.php?';
$url .= http_build_query($urlParams);
return 'javascript:void(window.open(\''
. $url
.'\',\'AdvancedSubscriptionTerms\', \'toolbar=no,location=no,'
. 'status=no,menubar=no,scrollbars=yes,resizable=yes,width=700px,'
. 'height=600px\', \'100\' ))';
}
/**
* Return the url to enter to subscription queue to session
* @param $params

@ -494,7 +494,7 @@ class HookAdvancedSubscription extends HookObserver implements
// Vacancy and queue status cases:
if ($isOpen) {
// Go to Course session
$data['action_url'] = self::$plugin->getSessionUrl($sessionId);
$data['action_url'] = self::$plugin->getOpenSessionUrl($userId, $params);
} else {
try {
$isAllowed = self::$plugin->isAllowedToDoRequest($userId, $params);

@ -0,0 +1,59 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Validate requirements for a open session
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
* @package chamilo.plugin.advanced_subscription
*/
require_once __DIR__ . '/../config.php';
$plugin = AdvancedSubscriptionPlugin::create();
if (!isset($_GET['session_id'], $_GET['user_id'], $_GET['profile_completed'])) {
die;
}
$sessionInfo = api_get_session_info($_GET['session_id']);
$tpl = new Template(
$plugin->get_lang('plugin_title'),
false,
false,
false,
false,
false
);
$tpl->assign('session', $sessionInfo);
if (SessionManager::isUserSubscribedAsStudent(
$_GET['session_id'],
$_GET['user_id']
)) {
$tpl->assign('is_subscribed', false);
$tpl->assign(
'errorMessages',
[sprintf(
$plugin->get_lang('YouAreAlreadySubscribedToSessionX'),
$sessionInfo['name']
)]
);
} else {
if (!$plugin->isAllowedSubscribeToOpenSession($_GET)) {
$tpl->assign('is_subscribed', false);
$tpl->assign('errorMessages', $plugin->getErrorMessages());
} else {
SessionManager::suscribe_users_to_session(
$_GET['session_id'],
[$_GET['user_id']],
SESSION_VISIBLE_READ_ONLY,
false
);
$tpl->assign('is_subscribed', true);
}
}
$content = $tpl->fetch('/advanced_subscription/views/open_session.tpl');
$tpl->assign('content', $content);
$tpl->display_one_col_template();

@ -0,0 +1,54 @@
<link href="{{ _p.web_plugin }}advanced_subscription/views/css/style.css" rel="stylesheet" type="text/css">
<script>
$(document).on('ready', function () {
$('#asp-close-window').on('click', function (e) {
e.preventDefault();
window.close();
});
$('#asp-go-to').on('click', function (e) {
e.preventDefault();
window.close();
window.opener.location.href = '{{ _p.web_main ~ 'session/index.php?session_id=' ~ session.id }}';
});
});
</script>
<h2 class="legal-terms-title legal-terms-popup">
{{ "SubscriptionToOpenSession"|get_plugin_lang('AdvancedSubscriptionPlugin') }}
</h2>
{% if not is_subscribed %}
<div class="alert alert-warning legal-terms-popup">
<ul>
{% for errorMessage in errorMessages %}
<li>{{ errorMessage }}</li>
{% endfor %}
</ul>
</div>
<div class="legal-terms-buttons legal-terms-popup">
<a
class="btn btn-success btn-advanced-subscription btn-accept"
href="#" id="asp-close-window">
<i class="fa fa-check"></i>
{{ "AcceptInfinitive"|get_plugin_lang('AdvancedSubscriptionPlugin') }}
</a>
</div>
{% else %}
<div class="alert alert-success legal-terms-popup">
{{ 'SuccessSubscriptionToSessionX'|get_plugin_lang('AdvancedSubscriptionPlugin') }}
</div>
<div class="text-right legal-terms-popup">
<a
class="btn btn-success btn-advanced-subscription btn-accept"
href="#" id="asp-go-to">
<i class="fa fa-external-link"></i>
{{ "GoToSessionX"|get_plugin_lang('AdvancedSubscriptionPlugin')|format(session.name) }}
</a>
</div>
{% endif %}
Loading…
Cancel
Save