|
@@ -10,6 +10,8 @@
|
|
|
class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface
|
|
|
{
|
|
|
protected $strings;
|
|
|
+ private $errorMessages;
|
|
|
+
|
|
|
/**
|
|
|
* Constructor
|
|
|
*/
|
|
@@ -29,6 +31,8 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface
|
|
|
);
|
|
|
|
|
|
parent::__construct('1.0', 'Imanol Losada, Daniel Barreto', $parameters);
|
|
|
+
|
|
|
+ $this->errorMessages = array();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -136,115 +140,175 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface
|
|
|
Database::query("DELETE FROM $settingsTable WHERE subkey = 'advanced_subscription'");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Get the error messages list
|
|
|
+ * @return array The message list
|
|
|
+ */
|
|
|
+ public function getErrorMessages()
|
|
|
+ {
|
|
|
+ return $this->errorMessages;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Return true if user is allowed to be added to queue for session subscription
|
|
|
* @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
|
|
|
+ * @param boolean $collectErrors Optional. Default is false. Whether collect all errors or throw exeptions
|
|
|
* @throws Exception
|
|
|
* @return bool
|
|
|
*/
|
|
|
- public function isAllowedToDoRequest($userId, $params = array())
|
|
|
+ public function isAllowedToDoRequest($userId, $params = array(), $collectErrors = false)
|
|
|
{
|
|
|
- $isAllowed = null;
|
|
|
$plugin = self::create();
|
|
|
$wsUrl = $plugin->get('ws_url');
|
|
|
// Student always is connected
|
|
|
$isConnected = true;
|
|
|
- if ($isConnected) {
|
|
|
- $profileCompletedMin = (float) $plugin->get('min_profile_percentage');
|
|
|
- if (is_string($wsUrl) && !empty($wsUrl)) {
|
|
|
- $options = array(
|
|
|
- 'location' => $wsUrl,
|
|
|
- 'uri' => $wsUrl
|
|
|
- );
|
|
|
- $client = new SoapClient(null, $options);
|
|
|
- $userInfo = UserManager::get_user_info_by_id($userId);
|
|
|
- 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;
|
|
|
+
|
|
|
+ if (!$isConnected) {
|
|
|
+ $this->errorMessages[] = $this->get_lang('AdvancedSubscriptionNotConnected');
|
|
|
+
|
|
|
+ if (!$collectErrors) {
|
|
|
+ throw new \Exception($this->get_lang('AdvancedSubscriptionNotConnected'));
|
|
|
}
|
|
|
- if ($profileCompleted >= $profileCompletedMin) {
|
|
|
- $checkInduction = $plugin->get('check_induction');
|
|
|
- // @TODO: check if user have completed at least one induction session
|
|
|
- $completedInduction = true;
|
|
|
- if (!$checkInduction || $completedInduction) {
|
|
|
- $uitMax = $plugin->get('yearly_cost_unit_converter');
|
|
|
- $uitMax *= $plugin->get('yearly_cost_limit');
|
|
|
- $uitUser = 0;
|
|
|
- $now = new DateTime(api_get_utc_datetime());
|
|
|
- $newYearDate = $plugin->get('course_session_credit_year_start_date');
|
|
|
- $newYearDate = !empty($newYearDate) ?
|
|
|
- new \DateTime($newYearDate . $now->format('/Y')) :
|
|
|
- $now;
|
|
|
- $extra = new ExtraFieldValue('session');
|
|
|
- $joinSessionTable = Database::get_main_table(TABLE_MAIN_SESSION_USER) . ' su INNER JOIN ' .
|
|
|
- Database::get_main_table(TABLE_MAIN_SESSION) . ' s ON s.id = su.id_session';
|
|
|
- $whereSessionParams = 'su.relation_type = ? AND s.date_start >= ? AND su.id_user = ?';
|
|
|
- $whereSessionParamsValues = array(
|
|
|
- 0,
|
|
|
- $newYearDate->format('Y-m-d'),
|
|
|
- $userId
|
|
|
- );
|
|
|
- $whereSession = array(
|
|
|
- 'where' => array(
|
|
|
- $whereSessionParams => $whereSessionParamsValues
|
|
|
- )
|
|
|
- );
|
|
|
- $selectSession = 's.id AS id';
|
|
|
- $sessions = Database::select(
|
|
|
- $selectSession,
|
|
|
- $joinSessionTable,
|
|
|
- $whereSession
|
|
|
- );
|
|
|
+ }
|
|
|
|
|
|
- if (is_array($sessions) && count($sessions) > 0) {
|
|
|
- foreach ($sessions as $session) {
|
|
|
- $var = $extra->get_values_by_handler_and_field_variable($session['id'], 'cost');
|
|
|
- $uitUser += $var['field_value'];
|
|
|
- }
|
|
|
- }
|
|
|
- if ($uitMax >= $uitUser) {
|
|
|
- $expendedTimeMax = $plugin->get('yearly_hours_limit');
|
|
|
- $expendedTime = 0;
|
|
|
- if (is_array($sessions) && count($sessions) > 0) {
|
|
|
- foreach ($sessions as $session) {
|
|
|
- $var = $extra->get_values_by_handler_and_field_variable($session['id'], 'teaching_hours');
|
|
|
- $expendedTime += $var['field_value'];
|
|
|
- }
|
|
|
- }
|
|
|
- if ($expendedTimeMax >= $expendedTime) {
|
|
|
- $expendedNumMax = $plugin->get('courses_count_limit');
|
|
|
- $expendedNum = count($sessions);
|
|
|
- if ($expendedNumMax >= $expendedNum) {
|
|
|
- $isAllowed = true;
|
|
|
- } else {
|
|
|
- throw new \Exception($this->get_lang('AdvancedSubscriptionCourseXLimitReached'));
|
|
|
- }
|
|
|
- } else {
|
|
|
- throw new \Exception($this->get_lang('AdvancedSubscriptionTimeXLimitReached'));
|
|
|
- }
|
|
|
- } else {
|
|
|
- throw new \Exception($this->get_lang('AdvancedSubscriptionCostXLimitReached'));
|
|
|
- }
|
|
|
- } else {
|
|
|
- throw new \Exception($this->get_lang('AdvancedSubscriptionIncompleteInduction'));
|
|
|
- }
|
|
|
- } else {
|
|
|
- throw new \Exception($this->get_lang('AdvancedSubscriptionProfileIncomplete'));
|
|
|
+ $profileCompletedMin = (float) $plugin->get('min_profile_percentage');
|
|
|
+
|
|
|
+ if (is_string($wsUrl) && !empty($wsUrl)) {
|
|
|
+ $options = array(
|
|
|
+ 'location' => $wsUrl,
|
|
|
+ 'uri' => $wsUrl
|
|
|
+ );
|
|
|
+ $client = new SoapClient(null, $options);
|
|
|
+ $userInfo = UserManager::get_user_info_by_id($userId);
|
|
|
+ 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 {
|
|
|
- throw new \Exception($this->get_lang('AdvancedSubscriptionNotConnected'));
|
|
|
+ $profileCompleted = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($profileCompleted < $profileCompletedMin) {
|
|
|
+ $errorMessage = sprintf(
|
|
|
+ $this->get_lang('AdvancedSubscriptionProfileIncomplete'),
|
|
|
+ $profileCompletedMin,
|
|
|
+ $profileCompleted
|
|
|
+ );
|
|
|
+
|
|
|
+ $this->errorMessages[] = $errorMessage;
|
|
|
+
|
|
|
+ if (!$collectErrors) {
|
|
|
+ throw new \Exception($errorMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $checkInduction = $plugin->get('check_induction');
|
|
|
+ // @TODO: check if user have completed at least one induction session
|
|
|
+ $completedInduction = true;
|
|
|
+
|
|
|
+ if ($checkInduction && !$completedInduction) {
|
|
|
+ $this->errorMessages[] = $this->get_lang('AdvancedSubscriptionIncompleteInduction');
|
|
|
+
|
|
|
+ if (!$collectErrors) {
|
|
|
+ throw new \Exception($this->get_lang('AdvancedSubscriptionIncompleteInduction'));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $uitMax = $plugin->get('yearly_cost_unit_converter');
|
|
|
+ $uitMax *= $plugin->get('yearly_cost_limit');
|
|
|
+ $uitUser = 0;
|
|
|
+ $now = new DateTime(api_get_utc_datetime());
|
|
|
+ $newYearDate = $plugin->get('course_session_credit_year_start_date');
|
|
|
+ $newYearDate = !empty($newYearDate) ?
|
|
|
+ new \DateTime($newYearDate . $now->format('/Y')) :
|
|
|
+ $now;
|
|
|
+ $extra = new ExtraFieldValue('session');
|
|
|
+ $joinSessionTable = Database::get_main_table(TABLE_MAIN_SESSION_USER) . ' su INNER JOIN ' .
|
|
|
+ Database::get_main_table(TABLE_MAIN_SESSION) . ' s ON s.id = su.id_session';
|
|
|
+ $whereSessionParams = 'su.relation_type = ? AND s.date_start >= ? AND su.id_user = ?';
|
|
|
+ $whereSessionParamsValues = array(
|
|
|
+ 0,
|
|
|
+ $newYearDate->format('Y-m-d'),
|
|
|
+ $userId
|
|
|
+ );
|
|
|
+ $whereSession = array(
|
|
|
+ 'where' => array(
|
|
|
+ $whereSessionParams => $whereSessionParamsValues
|
|
|
+ )
|
|
|
+ );
|
|
|
+ $selectSession = 's.id AS id';
|
|
|
+ $sessions = Database::select(
|
|
|
+ $selectSession,
|
|
|
+ $joinSessionTable,
|
|
|
+ $whereSession
|
|
|
+ );
|
|
|
+
|
|
|
+ if (is_array($sessions) && count($sessions) > 0) {
|
|
|
+ foreach ($sessions as $session) {
|
|
|
+ $var = $extra->get_values_by_handler_and_field_variable($session['id'], 'cost');
|
|
|
+ $uitUser += $var['field_value'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($uitMax < $uitUser) {
|
|
|
+ $errorMessage = sprintf(
|
|
|
+ $this->get_lang('AdvancedSubscriptionCostXLimitReached'),
|
|
|
+ $uitMax
|
|
|
+ );
|
|
|
+
|
|
|
+ $this->errorMessages[] = $errorMessage;
|
|
|
+
|
|
|
+ if (!$collectErrors) {
|
|
|
+ throw new \Exception($errorMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $expendedTimeMax = $plugin->get('yearly_hours_limit');
|
|
|
+ $expendedTime = 0;
|
|
|
+
|
|
|
+ if (is_array($sessions) && count($sessions) > 0) {
|
|
|
+ foreach ($sessions as $session) {
|
|
|
+ $var = $extra->get_values_by_handler_and_field_variable($session['id'], 'teaching_hours');
|
|
|
+ $expendedTime += $var['field_value'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($expendedTimeMax < $expendedTime) {
|
|
|
+ $errorMessage = sprintf(
|
|
|
+ $this->get_lang('AdvancedSubscriptionTimeXLimitReached'),
|
|
|
+ $expendedTimeMax
|
|
|
+ );
|
|
|
+
|
|
|
+ $this->errorMessages[] = $errorMessage;
|
|
|
+
|
|
|
+ if (!$collectErrors) {
|
|
|
+ throw new \Exception($errorMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $expendedNumMax = $plugin->get('courses_count_limit');
|
|
|
+ $expendedNum = count($sessions);
|
|
|
+
|
|
|
+ if ($expendedNumMax < $expendedNum) {
|
|
|
+ $errorMessage = sprintf(
|
|
|
+ $this->get_lang('AdvancedSubscriptionCourseXLimitReached'),
|
|
|
+ $expendedNumMax
|
|
|
+ );
|
|
|
+
|
|
|
+ $this->errorMessages[] = $errorMessage;
|
|
|
+
|
|
|
+ if (!$collectErrors) {
|
|
|
+ throw new \Exception($errorMessage);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- return $isAllowed;
|
|
|
+ return empty($this->errorMessages);
|
|
|
}
|
|
|
|
|
|
/**
|