Skip to content

Commit

Permalink
Add AdvancedSubscriptions class methods - refs BT#9092
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Barreto committed Feb 10, 2015
1 parent 60257aa commit 791e457
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 91 deletions.
7 changes: 5 additions & 2 deletions plugin/advancedsubscription/lang/spanish.php
Expand Up @@ -7,8 +7,8 @@
$strings['ws_url_help'] = 'La URL de la cual se solicitará información para el proceso de la inscripción avanzada';
$strings['check_induction'] = 'Activar requerimiento de curso inducción';
$strings['check_induction_help'] = 'Escoja si se requiere que se complete los cursos de inducción';
$strings['tool_enable'] = 'Suscripción avanzada activada';
$strings['tool_enable_help'] = "Escoja si desea activar la suscripción avanzada.";
$strings['tool_enable'] = 'Inscripción avanzada activada';
$strings['tool_enable_help'] = "Escoja si desea activar la inscripción avanzada.";
$strings['yearly_cost_limit'] = 'Límite de UITs';
$strings['yearly_cost_limit_help'] = "El límite de UITs de cursos que se pueden llevar en un año calendario del año actual.";
$strings['yearly_hours_limit'] = 'Límite de horas lectivas';
Expand All @@ -29,5 +29,8 @@
$strings['AdvancedSubscriptionTimeXLimitReached'] = "Lo sentimos, usted ya ha alcanzado el límite anual de %s horas para los cursos que ha seguido este año";
$strings['AdvancedSubscriptionCourseXLimitReached'] = "Lo sentimos, usted ya ha alcanzado el límite anual de %s cursos que ha seguido este año";
$strings['AdvancedSubscriptionNotMoreAble'] = "Lo sentimos, usted ya no cumple con las condiciones iniciales para poder inscribirse al curso";
$strings['AdvancedSubscriptionIncompleteParams'] = "Los parámetros enviados no están completos o no son los correctos.";

$strings['AdvancedSubscriptionIsNotEnabled'] = "La inscripción avanzada no está activada";

//Needed in order to show the plugin title
218 changes: 129 additions & 89 deletions plugin/advancedsubscription/src/advanced_subscription_plugin.class.php
Expand Up @@ -9,6 +9,11 @@

class AdvancedSubscriptionPlugin extends Plugin
{
const ADVSUB_QUEUE_STATUS_START = 0;
const ADVSUB_QUEUE_STATUS_BOSS_DISAPPROVED = 1;
const ADVSUB_QUEUE_STATUS_BOSS_APPROVED = 2;
const ADVSUB_QUEUE_STATUS_ADMIN_DISAPPROVED = 3;
const ADVSUB_QUEUE_STATUS_ADMIN_APPROVED = 10;
/**
* Constructor
*/
Expand Down Expand Up @@ -57,7 +62,7 @@ public function install()
*/
public function uninstall()
{
$this->unistallDatabase();
$this->uninstallDatabase();
}

/**
Expand Down Expand Up @@ -127,60 +132,68 @@ private function uninstallDatabase()

/**
* Return true if user is able to be added to queue for session subscription
* @param $userId
* @param int $userId
* @param array $params MUST have keys:
* "is_connected" Indicate if the user is online on external web
* "profile_completed" Percentage of completed profile, given by WS
* @throws Exception
* @return bool
*/
public function isAbleToRequest($userId)
public function isAbleToRequest($userId, $params = array())
{
$isAble = false;
$advSubPlugin = self::create();
$wsUrl = $advSubPlugin->get('ws_url');
// @TODO: Get connection status from user by WS
$isConnected = true;
if ($isConnected) {
$profileCompletedMin = $advSubPlugin->get('min_profile_percentage');
// @TODO: Get completed profile percentage by WS
$profileCompleted = 100.0;
if ($profileCompleted > $profileCompletedMin) {
$checkInduction = $advSubPlugin->get('check_induction');
// @TODO: check if user have completed at least one induction session
$completedInduction = true;
if (!$checkInduction || $completedInduction) {
$uitMax = $advSubPlugin->get('yearly_cost_unit_converter');
$uitMax *= $advSubPlugin->get('yearly_cost_limit');
// @TODO: Get UIT completed by user this year by WS
$uitUser = 0;
if ($uitMax > $uitUser) {
$expendedTimeMax = $advSubPlugin->get('yearly_hours_limit');
// @TODO: Get Expended time from user data
$expendedTime = 0;
if ($expendedTimeMax > $expendedTime) {
$expendedNumMax = $advSubPlugin->get('courses_count_limit');
// @TODO: Get Expended num from user
$expendedNum = 0;
if ($expendedNumMax > $expendedNum) {
$isAble = true;
if (isset($params['is_connected']) && isset($params['profile_completed'])) {
$isAble = false;
$advSubPlugin = self::create();
$wsUrl = $advSubPlugin->get('ws_url');
// @TODO: Get connection status from user by WS
$isConnected = $params['is_connected'];
if ($isConnected) {
$profileCompletedMin = $advSubPlugin->get('min_profile_percentage');
// @TODO: Get completed profile percentage by WS
$profileCompleted = (float) $params['profile_completed'];
if ($profileCompleted > $profileCompletedMin) {
$checkInduction = $advSubPlugin->get('check_induction');
// @TODO: check if user have completed at least one induction session
$completedInduction = true;
if (!$checkInduction || $completedInduction) {
$uitMax = $advSubPlugin->get('yearly_cost_unit_converter');
$uitMax *= $advSubPlugin->get('yearly_cost_limit');
// @TODO: Get UIT completed by user this year by WS
$uitUser = 0;
if ($uitMax > $uitUser) {
$expendedTimeMax = $advSubPlugin->get('yearly_hours_limit');
// @TODO: Get Expended time from user data
$expendedTime = 0;
if ($expendedTimeMax > $expendedTime) {
$expendedNumMax = $advSubPlugin->get('courses_count_limit');
// @TODO: Get Expended num from user
$expendedNum = 0;
if ($expendedNumMax > $expendedNum) {
$isAble = true;
} else {
throw new \Exception(get_lang('AdvancedSubscriptionCourseXLimitReached'));
}
} else {
throw new \Exception(get_lang('AdvancedSubscriptionCourseXLimitReached'));
throw new \Exception(get_lang('AdvancedSubscriptionTimeXLimitReached'));
}
} else {
throw new \Exception(get_lang('AdvancedSubscriptionTimeXLimitReached'));
throw new \Exception(get_lang('AdvancedSubscriptionCostXLimitReached'));
}
} else {
throw new \Exception(get_lang('AdvancedSubscriptionCostXLimitReached'));
throw new \Exception(get_lang('AdvancedSubscriptionIncompleteInduction'));
}
} else {
throw new \Exception(get_lang('AdvancedSubscriptionIncompleteInduction'));
throw new \Exception(get_lang('AdvancedSubscriptionProfileIncomplete'));
}
} else {
throw new \Exception(get_lang('AdvancedSubscriptionProfileIncomplete'));
throw new \Exception(get_lang('AdvancedSubscriptionNotConnected'));
}

return $isAble;
} else {
throw new \Exception(get_lang('AdvancedSubscriptionNotConnected'));
throw new \Exception($this->get_lang('AdvancedSubscriptionIncompleteParams'));
}

return $isAble;
}

/**
Expand Down Expand Up @@ -223,71 +236,98 @@ public function startSubscription($userId, $sessionId)
}

/**
* Check whether the tour should be displayed to the user
* @param string $currentPageClass The class of the current page
* @param int $userId The user id
* @return boolean If the user has seen the tour return false, otherwise return true
* Check if session is open for subscription
* @param $sessionId
* @param string $fieldVariable
* @return bool
*/
public function checkTourForUser($currentPageClass, $userId)
public function isSessionOpen($sessionId, $fieldVariable = 'es_abierta')
{
$pAdvSubQueueTable = Database::get_main_table(TABLE_ADV_SUB_QUEUE);
$pAdvSubMailTable = Database::get_main_table(TABLE_ADV_SUB_MAIL);
$pAdvSubMailTypeTable = Database::get_main_table(TABLE_ADV_SUB_MAIL_TYPE);
$pAdvSubMailStatusTable = Database::get_main_table(TABLE_ADV_SUB_MAIL_STATUS);
$pluginTourLogTable = Database::get_main_table(TABLE_TOUR_LOG);

$checkResult = Database::select('count(1) as qty', $pluginTourLogTable, array(
'where' => array(
"page_class = '?' AND " => $currentPageClass,
"user_id = ?" => intval($userId)
)), 'first');

if ($checkResult !== false) {
if ($checkResult['qty'] > 0) {
return false;
$sessionId = (int) $sessionId;
$fieldVariable = Database::escape_string($fieldVariable);
$isOpen = false;
if ($sessionId > 0 && !empty($fieldVariable)) {
$sfTable = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
$sfvTable = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
$joinTable = $sfvTable . ' sfv INNER JOIN ' . $sfTable . ' sf ON sfv.field_id = sf.id ';
$row = Database::select(
'sfv.field_value as field_value',
$joinTable,
array(
'where' => array(
'sfv.session_id = ? AND ' => $sessionId,
'sf.field_variable = ?' => $fieldVariable,
)
)
);
if (isset($row[0]) && is_array($row[0])) {
$isOpen = (bool) $row[0]['field_value'];
}
}

return true;
return $isOpen;
}

/**
* Set the tour as seen
* @param string $currentPageClass The class of the current page
* @param int $userId The user id
* @return void
*/
public function saveCompletedTour($currentPageClass, $userId)
public function approvedByBoss()
{
$pAdvSubQueueTable = Database::get_main_table(TABLE_ADV_SUB_QUEUE);
$pAdvSubMailTable = Database::get_main_table(TABLE_ADV_SUB_MAIL);
$pAdvSubMailTypeTable = Database::get_main_table(TABLE_ADV_SUB_MAIL_TYPE);
$pAdvSubMailStatusTable = Database::get_main_table(TABLE_ADV_SUB_MAIL_STATUS);
$pluginTourLogTable = Database::get_main_table(TABLE_TOUR_LOG);

Database::insert($pluginTourLogTable, array(
'page_class' => $currentPageClass,
'user_id' => intval($userId),
'visualization_datetime' => api_get_utc_datetime()
));
}

/**
* Get the configuration to show the tour in pages
* @return array The config data
*/
public function getTourConfig()
public function disapprovedByBoss()
{
$pAdvSubQueueTable = Database::get_main_table(TABLE_ADV_SUB_QUEUE);
$pAdvSubMailTable = Database::get_main_table(TABLE_ADV_SUB_MAIL);
$pAdvSubMailTypeTable = Database::get_main_table(TABLE_ADV_SUB_MAIL_TYPE);
$pAdvSubMailStatusTable = Database::get_main_table(TABLE_ADV_SUB_MAIL_STATUS);
$pluginPath = api_get_path(PLUGIN_PATH) . 'tour/';

$jsonContent = file_get_contents($pluginPath . 'config/tour.json');
}

$jsonData = json_decode($jsonContent, true);
public function approvedByAdmin()
{

return $jsonData;
}

public function disapprovedByAdmin()
{

}

public function confirmTermsAndConditions()
{

}

public function checkToken()
{

}

public function sendMail()
{

}

/**
* Count the users in queue filtered by params (sessions, status)
* @param array $params Input array containing the set of
* session and status to count from queue
* e.g:
* array('sessions' => array(215, 218, 345, 502),
* 'status' => array(0, 1, 2))
* @return int
*/
public function countQueueByParams($params)
{
$count = 0;
if (!empty($params) && is_array($params)) {
$advsubQueueTable = Database::get_main_table(TABLE_ADV_SUB_QUEUE);
$where['1 = ? '] = 1;
if (isset($params['sessions']) && is_array($params['sessions'])) {
$where['AND session_id IN ( ? ) '] = implode($params['sessions']);
}
if (isset($params['status']) && is_array($params['status'])) {
$where['AND status IN ( ? ) '] = implode($params['status']);
}
$where['where'] = $where;
$count = Database::select('COUNT(*)', $advsubQueueTable, $where);
$count = $count[0];
}
return $count;
}
}
30 changes: 30 additions & 0 deletions plugin/advancedsubscription/src/index.php
@@ -1 +1,31 @@
<?php
/* For license terms, see /license.txt */
/**
* Index of the Buy Courses plugin courses list
* @package chamilo.plugin.advancedsubscription
*/
/**
*
*/
$plugin = AdvancedSubscriptionPlugin::create();

if (api_is_platform_admin()) {
$isAdmin = api_is_platform_admin();
$title = $plugin->get_lang('CourseListOnSale');
$templateName = $plugin->get_lang('BuyCourses');

$tpl = new Template($templateName);
$tpl->assign('isAdmin', $isAdmin);
$tpl->assign('title', $title);
$tpl->assign('BuySessions', $plugin->get_lang('BuySessions'));
$tpl->assign('BuyCourses', $templateName);
$tpl->assign('ConfigurationOfSessionsAndPrices', $plugin->get_lang('ConfigurationOfSessionsAndPrices'));
$tpl->assign('ConfigurationOfCoursesAndPrices', $plugin->get_lang('ConfigurationOfCoursesAndPrices'));
$tpl->assign('ConfigurationOfPayments', $plugin->get_lang('ConfigurationOfPayments'));
$tpl->assign('OrdersPendingOfPayment', $plugin->get_lang('OrdersPendingOfPayment'));
$listing_tpl = 'buycourses/view/index.tpl';
$content = $tpl->fetch($listing_tpl);
$tpl->assign('content', $content);
// If the user is NOT an administrator, redirect it to course/session buy list
$isAdmin ? $tpl->display_one_col_template() : header('Location: src/list.php');
}

0 comments on commit 791e457

Please sign in to comment.