advanced_subscription.ajax.php 18 KB

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