advanced_subscription.ajax.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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::getFirstStudentBoss($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'] : $admin['lastname'].', '.$admin['firstname']
  121. ;
  122. }
  123. unset($admin);
  124. // Set data
  125. $data['action'] = 'confirm';
  126. $data['student'] = $studentArray;
  127. $data['superior'] = $superiorArray;
  128. $data['admins'] = $adminsArray;
  129. $data['session'] = $sessionArray;
  130. $data['signature'] = api_get_setting('Institution');
  131. // Check if student boss exists
  132. if (empty($superiorId)) {
  133. // Student boss does not exist
  134. // Update status to accepted by boss
  135. $res = $plugin->updateQueueStatus($data, ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED);
  136. if (!empty($res)) {
  137. // Prepare admin url
  138. $data['admin_view_url'] = api_get_path(WEB_PLUGIN_PATH).
  139. 'advanced_subscription/src/admin_view.php?s='.$data['sessionId'];
  140. // Send mails
  141. $result['mailIds'] = $plugin->sendMail(
  142. $data,
  143. ADVANCED_SUBSCRIPTION_ACTION_STUDENT_REQUEST_NO_BOSS
  144. );
  145. // Check if mails were sent
  146. if (!empty($result['mailIds'])) {
  147. $result['error'] = false;
  148. $result['errorMessage'] = 'No error';
  149. $result['pass'] = true;
  150. // Check if exist an email to render
  151. if (isset($result['mailIds']['render'])) {
  152. // Render mail
  153. $url = $plugin->getRenderMailUrl(array('queueId' => $result['mailIds']['render']));
  154. header('Location: '.$url);
  155. exit;
  156. }
  157. }
  158. }
  159. } else {
  160. // Student boss does exist
  161. // Get url to be accepted by boss
  162. $data['newStatus'] = ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED;
  163. $data['student']['acceptUrl'] = $plugin->getQueueUrl($data);
  164. // Get url to be rejected by boss
  165. $data['newStatus'] = ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_DISAPPROVED;
  166. $data['student']['rejectUrl'] = $plugin->getQueueUrl($data);
  167. // Send mails
  168. $result['mailIds'] = $plugin->sendMail($data, ADVANCED_SUBSCRIPTION_ACTION_STUDENT_REQUEST);
  169. // Check if mails were sent
  170. if (!empty($result['mailIds'])) {
  171. $result['error'] = false;
  172. $result['errorMessage'] = 'No error';
  173. $result['pass'] = true;
  174. // Check if exist an email to render
  175. if (isset($result['mailIds']['render'])) {
  176. // Render mail
  177. $url = $plugin->getRenderMailUrl(array('queueId' => $result['mailIds']['render']));
  178. header('Location: '.$url);
  179. exit;
  180. }
  181. }
  182. }
  183. } else {
  184. $lastMessageId = $plugin->getLastMessageId($data['studentUserId'], $data['sessionId']);
  185. if ($lastMessageId !== false) {
  186. // Render mail
  187. $url = $plugin->getRenderMailUrl(array('queueId' => $lastMessageId));
  188. header('Location: '.$url);
  189. exit;
  190. } else {
  191. if (is_string($res)) {
  192. $result['errorMessage'] = $res;
  193. } else {
  194. $result['errorMessage'] = 'User can not be subscribed';
  195. }
  196. $result['pass'] = false;
  197. $url = $plugin->getTermsUrl($data, ADVANCED_SUBSCRIPTION_TERMS_MODE_FINAL);
  198. header('Location: '.$url);
  199. exit;
  200. }
  201. }
  202. break;
  203. case 'confirm':
  204. // Check if new status is set
  205. if (isset($data['newStatus'])) {
  206. if ($data['newStatus'] === ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_APPROVED) {
  207. try {
  208. $isAllowToDoRequest = $plugin->isAllowedToDoRequest($data['studentUserId'], $data);
  209. } catch (Exception $ex) {
  210. $messageTemplate = new Template(null, false, false);
  211. $messageTemplate->assign(
  212. 'content',
  213. Display::return_message($ex->getMessage(), 'error', false)
  214. );
  215. $messageTemplate->display_no_layout_template();
  216. $showJSON = false;
  217. break;
  218. }
  219. }
  220. // Update queue status
  221. $res = $plugin->updateQueueStatus($data, $data['newStatus']);
  222. if ($res === true) {
  223. // Prepare data
  224. // Prepare session data
  225. $fieldsArray = array(
  226. 'description',
  227. 'target',
  228. 'mode',
  229. 'publication_end_date',
  230. 'recommended_number_of_participants'
  231. );
  232. $sessionArray = api_get_session_info($data['sessionId']);
  233. $extraSession = new ExtraFieldValue('session');
  234. $extraField = new ExtraField('session');
  235. // Get session fields
  236. $fieldList = $extraField->get_all(array(
  237. 'variable IN ( ?, ?, ?, ?, ?)' => $fieldsArray
  238. ));
  239. // Index session fields
  240. foreach ($fieldList as $field) {
  241. $fields[$field['id']] = $field['variable'];
  242. }
  243. $mergedArray = array_merge(array($data['sessionId']), array_keys($fields));
  244. $sessionFieldValueList = $extraSession->get_all(
  245. array('session_id = ? field_id IN ( ?, ?, ?, ?, ?, ?, ? )' => $mergedArray)
  246. );
  247. foreach ($sessionFieldValueList as $sessionFieldValue) {
  248. // Check if session field value is set in session field list
  249. if (isset($fields[$sessionFieldValue['field_id']])) {
  250. $var = $fields[$sessionFieldValue['field_id']];
  251. $val = $sessionFieldValue['value'];
  252. // Assign session field value to session
  253. $sessionArray[$var] = $val;
  254. }
  255. }
  256. // Prepare student data
  257. $studentArray = api_get_user_info($data['studentUserId']);
  258. $studentArray['picture'] = $studentArray['avatar'];
  259. // Prepare superior data
  260. $superiorId = UserManager::getFirstStudentBoss($data['studentUserId']);
  261. if (!empty($superiorId)) {
  262. $superiorArray = api_get_user_info($superiorId);
  263. } else {
  264. $superiorArray = null;
  265. }
  266. // Prepare admin data
  267. $adminsArray = UserManager::get_all_administrators();
  268. $isWesternNameOrder = api_is_western_name_order();
  269. foreach ($adminsArray as &$admin) {
  270. $admin['complete_name'] = $isWesternNameOrder ?
  271. $admin['firstname'].', '.$admin['lastname'] : $admin['lastname'].', '.$admin['firstname']
  272. ;
  273. }
  274. unset($admin);
  275. // Set data
  276. $data['student'] = $studentArray;
  277. $data['superior'] = $superiorArray;
  278. $data['admins'] = $adminsArray;
  279. $data['session'] = $sessionArray;
  280. $data['signature'] = api_get_setting('Institution');
  281. $data['admin_view_url'] = api_get_path(WEB_PLUGIN_PATH)
  282. . 'advanced_subscription/src/admin_view.php?s='.$data['sessionId'];
  283. // Check if exist and action in data
  284. if (empty($data['mailAction'])) {
  285. // set action in data by new status
  286. switch ($data['newStatus']) {
  287. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED:
  288. $data['mailAction'] = ADVANCED_SUBSCRIPTION_ACTION_SUPERIOR_APPROVE;
  289. break;
  290. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_DISAPPROVED:
  291. $data['mailAction'] = ADVANCED_SUBSCRIPTION_ACTION_SUPERIOR_DISAPPROVE;
  292. break;
  293. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_APPROVED:
  294. $data['mailAction'] = ADVANCED_SUBSCRIPTION_ACTION_ADMIN_APPROVE;
  295. break;
  296. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_DISAPPROVED:
  297. $data['mailAction'] = ADVANCED_SUBSCRIPTION_ACTION_ADMIN_DISAPPROVE;
  298. break;
  299. default:
  300. break;
  301. }
  302. }
  303. // Student Session inscription
  304. if ($data['newStatus'] == ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_APPROVED) {
  305. SessionManager::subscribe_users_to_session(
  306. $data['sessionId'],
  307. array($data['studentUserId']),
  308. null,
  309. false
  310. );
  311. }
  312. // Send mails
  313. $result['mailIds'] = $plugin->sendMail($data, $data['mailAction']);
  314. // Check if mails were sent
  315. if (!empty($result['mailIds'])) {
  316. $result['error'] = false;
  317. $result['errorMessage'] = 'User has been processed';
  318. // Check if exist mail to render
  319. if (isset($result['mailIds']['render'])) {
  320. // Render mail
  321. $url = $plugin->getRenderMailUrl(array('queueId' => $result['mailIds']['render']));
  322. header('Location: '.$url);
  323. exit;
  324. }
  325. }
  326. } else {
  327. $result['errorMessage'] = 'User queue can not be updated';
  328. }
  329. }
  330. break;
  331. default:
  332. $result['errorMessage'] = 'This action does not exist!';
  333. }
  334. }
  335. if ($showJSON) {
  336. // Echo result as json
  337. echo json_encode($result);
  338. }