advanced_subscription.ajax.php 17 KB

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