advanced_subscription.ajax.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script to receipt request to subscribe and confirmation action to queue
  5. * @author Daniel Alejandro Barreto Alva <daniel.barreto@beeznest.com>
  6. * @package chamilo.plugin.advanced_subscription
  7. */
  8. /**
  9. * Init
  10. */
  11. require_once __DIR__ . '/../config.php';
  12. $plugin = AdvancedSubscriptionPlugin::create();
  13. // Get validation hash
  14. $hash = Security::remove_XSS($_REQUEST['v']);
  15. // Get data from request (GET or POST)
  16. $data['action'] = Security::remove_XSS($_REQUEST['a']);
  17. $data['sessionId'] = intval($_REQUEST['s']);
  18. $data['currentUserId'] = intval($_REQUEST['current_user_id']);
  19. $data['studentUserId'] = intval($_REQUEST['u']);
  20. $data['queueId'] = intval($_REQUEST['q']);
  21. $data['newStatus'] = intval($_REQUEST['e']);
  22. // Student always is connected
  23. // $data['is_connected'] = isset($_REQUEST['is_connected']) ? boolval($_REQUEST['is_connected']) : false;
  24. $data['is_connected'] = true;
  25. $data['profile_completed'] = isset($_REQUEST['profile_completed']) ? floatval($_REQUEST['profile_completed']) : 0;
  26. $data['accept_terms'] = isset($_REQUEST['accept_terms']) ? intval($_REQUEST['accept_terms']) : 0;
  27. $data['courseId'] = isset($_REQUEST['c']) ? intval($_REQUEST['c']) : 0;
  28. // Init result array
  29. $result = array('error' => true, 'errorMessage' => get_lang('ThereWasAnError'));
  30. // Check if data is valid or is for start subscription
  31. $verified = $plugin->checkHash($data, $hash) || $data['action'] == 'subscribe';
  32. if ($verified) {
  33. switch($data['action']) {
  34. case 'check': // Check minimum requirements
  35. try {
  36. $res = AdvancedSubscriptionPlugin::create()->isAllowedToDoRequest($data['studentUserId'], $data);
  37. if ($res) {
  38. $result['error'] = false;
  39. $result['errorMessage'] = 'No error';
  40. $result['pass'] = true;
  41. } else {
  42. $result['errorMessage'] = 'User can not be subscribed';
  43. $result['pass'] = false;
  44. }
  45. } catch (\Exception $e) {
  46. $result['errorMessage'] = $e->getMessage();
  47. }
  48. break;
  49. case 'subscribe': // Subscription
  50. // Start subscription to queue
  51. $res = AdvancedSubscriptionPlugin::create()->startSubscription($data['studentUserId'], $data['sessionId'], $data);
  52. // Check if queue subscription was successful
  53. if ($res === true) {
  54. $legalEnabled = api_get_plugin_setting('courselegal', 'tool_enable');
  55. if ($legalEnabled) {
  56. // Save terms confirmation
  57. CourseLegalPlugin::create()->saveUserLegal($data['studentUserId'], $data['courseId'], $data['sessionId'], false);
  58. }
  59. // Prepare data
  60. // Get session data
  61. // Assign variables
  62. $fieldsArray = array('description', 'target', 'mode', 'publication_end_date', 'recommended_number_of_participants');
  63. $sessionArray = api_get_session_info($data['sessionId']);
  64. $extraSession = new ExtraFieldValue('session');
  65. $extraField = new ExtraField('session');
  66. // Get session fields
  67. $fieldList = $extraField->get_all(array(
  68. 'field_variable IN ( ?, ?, ?, ?, ?)' => $fieldsArray
  69. ));
  70. // Index session fields
  71. foreach ($fieldList as $field) {
  72. $fields[$field['id']] = $field['field_variable'];
  73. }
  74. $mergedArray = array_merge(array($data['sessionId']), array_keys($fields));
  75. $sessionFieldValueList = $extraSession->get_all(array('session_id = ? field_id IN ( ?, ?, ?, ?, ?, ?, ? )' => $mergedArray));
  76. foreach ($sessionFieldValueList as $sessionFieldValue) {
  77. // Check if session field value is set in session field list
  78. if (isset($fields[$sessionFieldValue['field_id']])) {
  79. $var = $fields[$sessionFieldValue['field_id']];
  80. $val = $sessionFieldValue['field_value'];
  81. // Assign session field value to session
  82. $sessionArray[$var] = $val;
  83. }
  84. }
  85. // Get student data
  86. $studentArray = api_get_user_info($data['studentUserId']);
  87. $studentArray['picture'] = UserManager::get_user_picture_path_by_id($studentArray['user_id'], 'web', false, true);
  88. $studentArray['picture'] = UserManager::get_picture_user($studentArray['user_id'], $studentArray['picture']['file'], 22, USER_IMAGE_SIZE_MEDIUM);
  89. // Get superior data if exist
  90. $superiorId = UserManager::getStudentBoss($data['studentUserId']);
  91. if (!empty($superiorId)) {
  92. $superiorArray = api_get_user_info($superiorId);
  93. } else {
  94. $superiorArray = null;
  95. }
  96. // Get admin data
  97. $adminsArray = UserManager::get_all_administrators();
  98. $isWesternNameOrder = api_is_western_name_order();
  99. foreach ($adminsArray as &$admin) {
  100. $admin['complete_name'] = $isWesternNameOrder ?
  101. $admin['firstname'] . ', ' . $admin['lastname'] :
  102. $admin['lastname'] . ', ' . $admin['firstname']
  103. ;
  104. }
  105. unset($admin);
  106. // Set data
  107. $data['action'] = 'confirm';
  108. $data['student'] = $studentArray;
  109. $data['superior'] = $superiorArray;
  110. $data['admins'] = $adminsArray;
  111. $data['session'] = $sessionArray;
  112. $data['signature'] = api_get_setting('Institution');
  113. // Check if student boss exists
  114. if (empty($superiorId)) {
  115. // Student boss does not exist
  116. // Update status to accepted by boss
  117. $res = $plugin->updateQueueStatus($data, ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED);
  118. if (!empty($res)) {
  119. // Prepare admin url
  120. $data['admin_view_url'] = api_get_path(WEB_PLUGIN_PATH) .
  121. 'advanced_subscription/src/admin_view.php?s=' . $data['sessionId'];
  122. // Send mails
  123. $result['mailIds'] = $plugin->sendMail($data, ADVANCED_SUBSCRIPTION_ACTION_STUDENT_REQUEST_NO_BOSS);
  124. // Check if mails were sent
  125. if (!empty($result['mailIds'])) {
  126. $result['error'] = false;
  127. $result['errorMessage'] = 'No error';
  128. $result['pass'] = true;
  129. // Check if exist an email to render
  130. if (isset($result['mailIds']['render'])) {
  131. // Render mail
  132. $url = $plugin->getRenderMailUrl(array('queueId' => $result['mailIds']['render']));
  133. Header::location($url);
  134. exit;
  135. }
  136. }
  137. }
  138. } else {
  139. // Student boss does exist
  140. // Get url to be accepted by boss
  141. $data['newStatus'] = ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED;
  142. $data['student']['acceptUrl'] = $plugin->getQueueUrl($data);
  143. // Get url to be rejected by boss
  144. $data['newStatus'] = ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_DISAPPROVED;
  145. $data['student']['rejectUrl'] = $plugin->getQueueUrl($data);
  146. // Send mails
  147. $result['mailIds'] = $plugin->sendMail($data, ADVANCED_SUBSCRIPTION_ACTION_STUDENT_REQUEST);
  148. // Check if mails were sent
  149. if (!empty($result['mailIds'])) {
  150. $result['error'] = false;
  151. $result['errorMessage'] = 'No error';
  152. $result['pass'] = true;
  153. // Check if exist an email to render
  154. if (isset($result['mailIds']['render'])) {
  155. // Render mail
  156. $url = $plugin->getRenderMailUrl(array('queueId' => $result['mailIds']['render']));
  157. Header::location($url);
  158. exit;
  159. }
  160. }
  161. }
  162. } else {
  163. $lastMessageId = $plugin->getLastMessageId($data['studentUserId'], $data['sessionId']);
  164. if ($lastMessageId !== false) {
  165. // Render mail
  166. $url = $plugin->getRenderMailUrl(array('queueId' => $lastMessageId));
  167. Header::location($url);
  168. exit;
  169. } else {
  170. if (is_string($res)) {
  171. $result['errorMessage'] = $res;
  172. } else {
  173. $result['errorMessage'] = 'User can not be subscribed';
  174. }
  175. $result['pass'] = false;
  176. }
  177. }
  178. break;
  179. case 'confirm':
  180. // Check if new status is set
  181. if (isset($data['newStatus'])) {
  182. // Update queue status
  183. $res = $plugin->updateQueueStatus($data, $data['newStatus']);
  184. if ($res === true) {
  185. // Prepare data
  186. // Prepare session data
  187. $fieldsArray = array('description', 'target', 'mode', 'publication_end_date', 'recommended_number_of_participants');
  188. $sessionArray = api_get_session_info($data['sessionId']);
  189. $extraSession = new ExtraFieldValue('session');
  190. $extraField = new ExtraField('session');
  191. // Get session fields
  192. $fieldList = $extraField->get_all(array(
  193. 'field_variable IN ( ?, ?, ?, ?, ?)' => $fieldsArray
  194. ));
  195. // Index session fields
  196. foreach ($fieldList as $field) {
  197. $fields[$field['id']] = $field['field_variable'];
  198. }
  199. $mergedArray = array_merge(array($data['sessionId']), array_keys($fields));
  200. $sessionFieldValueList = $extraSession->get_all(array('session_id = ? field_id IN ( ?, ?, ?, ?, ?, ?, ? )' => $mergedArray));
  201. foreach ($sessionFieldValueList as $sessionFieldValue) {
  202. // Check if session field value is set in session field list
  203. if (isset($fields[$sessionFieldValue['field_id']])) {
  204. $var = $fields[$sessionFieldValue['field_id']];
  205. $val = $sessionFieldValue['field_value'];
  206. // Assign session field value to session
  207. $sessionArray[$var] = $val;
  208. }
  209. }
  210. // Prepare student data
  211. $studentArray = api_get_user_info($data['studentUserId']);
  212. $studentArray['picture'] = UserManager::get_user_picture_path_by_id($studentArray['user_id'], 'web', false, true);
  213. $studentArray['picture'] = UserManager::get_picture_user($studentArray['user_id'], $studentArray['picture']['file'], 22, USER_IMAGE_SIZE_MEDIUM);
  214. // Prepare superior data
  215. $superiorId = UserManager::getStudentBoss($data['studentUserId']);
  216. if (!empty($superiorId)) {
  217. $superiorArray = api_get_user_info($superiorId);
  218. } else {
  219. $superiorArray = null;
  220. }
  221. // Prepare admin data
  222. $adminsArray = UserManager::get_all_administrators();
  223. $isWesternNameOrder = api_is_western_name_order();
  224. foreach ($adminsArray as &$admin) {
  225. $admin['complete_name'] = $isWesternNameOrder ?
  226. $admin['firstname'] . ', ' . $admin['lastname'] :
  227. $admin['lastname'] . ', ' . $admin['firstname']
  228. ;
  229. }
  230. unset($admin);
  231. // Set data
  232. $data['student'] = $studentArray;
  233. $data['superior'] = $superiorArray;
  234. $data['admins'] = $adminsArray;
  235. $data['session'] = $sessionArray;
  236. $data['signature'] = api_get_setting('Institution');
  237. $data['admin_view_url'] = api_get_path(WEB_PLUGIN_PATH) . 'advanced_subscription/src/admin_view.php?s=' . $data['sessionId'];
  238. // Check if exist and action in data
  239. if (empty($data['mailAction'])) {
  240. // set action in data by new status
  241. switch ($data['newStatus']) {
  242. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED:
  243. $data['mailAction'] = ADVANCED_SUBSCRIPTION_ACTION_SUPERIOR_APPROVE;
  244. break;
  245. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_DISAPPROVED:
  246. $data['mailAction'] = ADVANCED_SUBSCRIPTION_ACTION_SUPERIOR_DISAPPROVE;
  247. break;
  248. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_APPROVED:
  249. $data['mailAction'] = ADVANCED_SUBSCRIPTION_ACTION_ADMIN_APPROVE;
  250. break;
  251. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_DISAPPROVED:
  252. $data['mailAction'] = ADVANCED_SUBSCRIPTION_ACTION_ADMIN_DISAPPROVE;
  253. break;
  254. default:
  255. break;
  256. }
  257. }
  258. // Student Session inscription
  259. if ($data['newStatus'] == ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_APPROVED) {
  260. SessionManager::suscribe_users_to_session($data['sessionId'], array($data['studentUserId']), null, false);
  261. }
  262. // Send mails
  263. $result['mailIds'] = $plugin->sendMail($data, $data['mailAction']);
  264. // Check if mails were sent
  265. if (!empty($result['mailIds'])) {
  266. $result['error'] = false;
  267. $result['errorMessage'] = 'User has been processed';
  268. // Check if exist mail to render
  269. if (isset($result['mailIds']['render'])) {
  270. // Render mail
  271. $url = $plugin->getRenderMailUrl(array('queueId' => $result['mailIds']['render']));
  272. Header::location($url);
  273. exit;
  274. }
  275. }
  276. } else {
  277. $result['errorMessage'] = 'User queue can not be updated';
  278. }
  279. }
  280. break;
  281. default:
  282. $result['errorMessage'] = 'This action does not exist!';
  283. }
  284. }
  285. // Echo result as json
  286. echo json_encode($result);