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. * @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. $showJSON = true;
  31. // Check if data is valid or is for start subscription
  32. $verified = $plugin->checkHash($data, $hash) || $data['action'] == 'subscribe';
  33. if ($verified) {
  34. switch ($data['action']) {
  35. case 'check': // Check minimum requirements
  36. try {
  37. $res = AdvancedSubscriptionPlugin::create()->isAllowedToDoRequest($data['studentUserId'], $data);
  38. if ($res) {
  39. $result['error'] = false;
  40. $result['errorMessage'] = 'No error';
  41. $result['pass'] = true;
  42. } else {
  43. $result['errorMessage'] = 'User can not be subscribed';
  44. $result['pass'] = false;
  45. }
  46. } catch (\Exception $e) {
  47. $result['errorMessage'] = $e->getMessage();
  48. }
  49. break;
  50. case 'subscribe': // Subscription
  51. // Start subscription to queue
  52. $res = AdvancedSubscriptionPlugin::create()->startSubscription(
  53. $data['studentUserId'],
  54. $data['sessionId'],
  55. $data
  56. );
  57. // Check if queue subscription was successful
  58. if ($res === true) {
  59. $legalEnabled = api_get_plugin_setting('courselegal', 'tool_enable');
  60. if ($legalEnabled) {
  61. // Save terms confirmation
  62. CourseLegalPlugin::create()->saveUserLegal(
  63. $data['studentUserId'],
  64. $data['courseId'],
  65. $data['sessionId'],
  66. false
  67. );
  68. }
  69. // Prepare data
  70. // Get session data
  71. // Assign variables
  72. $fieldsArray = array(
  73. 'description',
  74. 'target',
  75. 'mode',
  76. 'publication_end_date',
  77. 'recommended_number_of_participants'
  78. );
  79. $sessionArray = api_get_session_info($data['sessionId']);
  80. $extraSession = new ExtraFieldValue('session');
  81. $extraField = new ExtraField('session');
  82. // Get session fields
  83. $fieldList = $extraField->get_all(array(
  84. 'variable IN ( ?, ?, ?, ?, ?)' => $fieldsArray
  85. ));
  86. // Index session fields
  87. foreach ($fieldList as $field) {
  88. $fields[$field['id']] = $field['variable'];
  89. }
  90. $mergedArray = array_merge(array($data['sessionId']), array_keys($fields));
  91. $sessionFieldValueList = $extraSession->get_all(
  92. array(
  93. 'item_id = ? field_id IN ( ?, ?, ?, ?, ?, ?, ? )' => $mergedArray
  94. )
  95. );
  96. foreach ($sessionFieldValueList as $sessionFieldValue) {
  97. // Check if session field value is set in session field list
  98. if (isset($fields[$sessionFieldValue['field_id']])) {
  99. $var = $fields[$sessionFieldValue['field_id']];
  100. $val = $sessionFieldValue['value'];
  101. // Assign session field value to session
  102. $sessionArray[$var] = $val;
  103. }
  104. }
  105. // Get student data
  106. $studentArray = api_get_user_info($data['studentUserId']);
  107. $studentArray['picture'] = $studentArray['avatar'];
  108. // Get superior data if exist
  109. $superiorId = UserManager::getStudentBoss($data['studentUserId']);
  110. if (!empty($superiorId)) {
  111. $superiorArray = api_get_user_info($superiorId);
  112. } else {
  113. $superiorArray = null;
  114. }
  115. // Get admin data
  116. $adminsArray = UserManager::get_all_administrators();
  117. $isWesternNameOrder = api_is_western_name_order();
  118. foreach ($adminsArray as &$admin) {
  119. $admin['complete_name'] = $isWesternNameOrder ?
  120. $admin['firstname'] . ', ' . $admin['lastname'] :
  121. $admin['lastname'] . ', ' . $admin['firstname']
  122. ;
  123. }
  124. unset($admin);
  125. // Set data
  126. $data['action'] = 'confirm';
  127. $data['student'] = $studentArray;
  128. $data['superior'] = $superiorArray;
  129. $data['admins'] = $adminsArray;
  130. $data['session'] = $sessionArray;
  131. $data['signature'] = api_get_setting('platform.institution');
  132. // Check if student boss exists
  133. if (empty($superiorId)) {
  134. // Student boss does not exist
  135. // Update status to accepted by boss
  136. $res = $plugin->updateQueueStatus($data, ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED);
  137. if (!empty($res)) {
  138. // Prepare admin url
  139. $data['admin_view_url'] = api_get_path(WEB_PLUGIN_PATH) .
  140. 'advanced_subscription/src/admin_view.php?s=' . $data['sessionId'];
  141. // Send mails
  142. $result['mailIds'] = $plugin->sendMail(
  143. $data,
  144. ADVANCED_SUBSCRIPTION_ACTION_STUDENT_REQUEST_NO_BOSS
  145. );
  146. // Check if mails were sent
  147. if (!empty($result['mailIds'])) {
  148. $result['error'] = false;
  149. $result['errorMessage'] = 'No error';
  150. $result['pass'] = true;
  151. // Check if exist an email to render
  152. if (isset($result['mailIds']['render'])) {
  153. // Render mail
  154. $url = $plugin->getRenderMailUrl(array('queueId' => $result['mailIds']['render']));
  155. header('Location: '.$url);
  156. exit;
  157. }
  158. }
  159. }
  160. } else {
  161. // Student boss does exist
  162. // Get url to be accepted by boss
  163. $data['newStatus'] = ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED;
  164. $data['student']['acceptUrl'] = $plugin->getQueueUrl($data);
  165. // Get url to be rejected by boss
  166. $data['newStatus'] = ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_DISAPPROVED;
  167. $data['student']['rejectUrl'] = $plugin->getQueueUrl($data);
  168. // Send mails
  169. $result['mailIds'] = $plugin->sendMail($data, ADVANCED_SUBSCRIPTION_ACTION_STUDENT_REQUEST);
  170. // Check if mails were sent
  171. if (!empty($result['mailIds'])) {
  172. $result['error'] = false;
  173. $result['errorMessage'] = 'No error';
  174. $result['pass'] = true;
  175. // Check if exist an email to render
  176. if (isset($result['mailIds']['render'])) {
  177. // Render mail
  178. $url = $plugin->getRenderMailUrl(array('queueId' => $result['mailIds']['render']));
  179. header('Location: '.$url);
  180. exit;
  181. }
  182. }
  183. }
  184. } else {
  185. $lastMessageId = $plugin->getLastMessageId($data['studentUserId'], $data['sessionId']);
  186. if ($lastMessageId !== false) {
  187. // Render mail
  188. $url = $plugin->getRenderMailUrl(array('queueId' => $lastMessageId));
  189. header('Location: '.$url);
  190. exit;
  191. } else {
  192. if (is_string($res)) {
  193. $result['errorMessage'] = $res;
  194. } else {
  195. $result['errorMessage'] = 'User can not be subscribed';
  196. }
  197. $result['pass'] = false;
  198. $url = $plugin->getTermsUrl($data, ADVANCED_SUBSCRIPTION_TERMS_MODE_FINAL);
  199. header('Location: '.$url);
  200. exit;
  201. }
  202. }
  203. break;
  204. case 'confirm':
  205. // Check if new status is set
  206. if (isset($data['newStatus'])) {
  207. if ($data['newStatus'] === ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_APPROVED) {
  208. try {
  209. $isAllowToDoRequest = $plugin->isAllowedToDoRequest($data['studentUserId'], $data);
  210. } catch (Exception $ex) {
  211. $messageTemplate = new Template(null, false, false);
  212. $messageTemplate->assign(
  213. 'content',
  214. Display::return_message($ex->getMessage(), 'error', false)
  215. );
  216. $messageTemplate->display_no_layout_template();
  217. $showJSON = false;
  218. break;
  219. }
  220. }
  221. // Update queue status
  222. $res = $plugin->updateQueueStatus($data, $data['newStatus']);
  223. if ($res === true) {
  224. // Prepare data
  225. // Prepare session data
  226. $fieldsArray = array(
  227. 'description',
  228. 'target',
  229. 'mode',
  230. 'publication_end_date',
  231. 'recommended_number_of_participants'
  232. );
  233. $sessionArray = api_get_session_info($data['sessionId']);
  234. $extraSession = new ExtraFieldValue('session');
  235. $extraField = new ExtraField('session');
  236. // Get session fields
  237. $fieldList = $extraField->get_all(array(
  238. 'variable IN ( ?, ?, ?, ?, ?)' => $fieldsArray
  239. ));
  240. // Index session fields
  241. foreach ($fieldList as $field) {
  242. $fields[$field['id']] = $field['variable'];
  243. }
  244. $mergedArray = array_merge(array($data['sessionId']), array_keys($fields));
  245. $sessionFieldValueList = $extraSession->get_all(
  246. array('session_id = ? field_id IN ( ?, ?, ?, ?, ?, ?, ? )' => $mergedArray)
  247. );
  248. foreach ($sessionFieldValueList as $sessionFieldValue) {
  249. // Check if session field value is set in session field list
  250. if (isset($fields[$sessionFieldValue['field_id']])) {
  251. $var = $fields[$sessionFieldValue['field_id']];
  252. $val = $sessionFieldValue['value'];
  253. // Assign session field value to session
  254. $sessionArray[$var] = $val;
  255. }
  256. }
  257. // Prepare student data
  258. $studentArray = api_get_user_info($data['studentUserId']);
  259. $studentArray['picture'] = $studentArray['avatar'];
  260. // Prepare superior data
  261. $superiorId = UserManager::getStudentBoss($data['studentUserId']);
  262. if (!empty($superiorId)) {
  263. $superiorArray = api_get_user_info($superiorId);
  264. } else {
  265. $superiorArray = null;
  266. }
  267. // Prepare admin data
  268. $adminsArray = UserManager::get_all_administrators();
  269. $isWesternNameOrder = api_is_western_name_order();
  270. foreach ($adminsArray as &$admin) {
  271. $admin['complete_name'] = $isWesternNameOrder ?
  272. $admin['firstname'] . ', ' . $admin['lastname'] :
  273. $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('platform.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::suscribe_users_to_session(
  308. $data['sessionId'],
  309. array($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(array('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. }