advanced_subscription.ajax.php 15 KB

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