webservice_session.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once(dirname(__FILE__).'/../inc/global.inc.php');
  7. $libpath = api_get_path(LIBRARY_PATH);
  8. require_once(dirname(__FILE__).'/webservice.php');
  9. /**
  10. * Web services available for the Session module. This class extends the WS class
  11. */
  12. class WSSession extends WS
  13. {
  14. /**
  15. * Creates a session (helper method)
  16. *
  17. * @param string Name of the session
  18. * @param string Start date, use the 'YYYY-MM-DD' format
  19. * @param string End date, use the 'YYYY-MM-DD' format
  20. * @param int Access delays of the coach (days before)
  21. * @param int Access delays of the coach (days after)
  22. * @param int Nolimit (0 = no limit of time, 1 = limit of time)
  23. * @param int Visibility
  24. * @param string User id field name for the coach
  25. * @param string User id value for the coach
  26. * @param string Original session id field name (use "chamilo_session_id" to use internal id)
  27. * @param string Original session id value
  28. * @param array Array of extra fields
  29. * @return mixed Generated id in case of success, WSError otherwise
  30. */
  31. protected function createSessionHelper(
  32. $name,
  33. $start_date,
  34. $end_date,
  35. $nb_days_access_before,
  36. $nb_days_access_after,
  37. $nolimit,
  38. $visibility,
  39. $user_id_field_name,
  40. $user_id_value,
  41. $session_id_field_name,
  42. $session_id_value,
  43. $extras
  44. ) {
  45. // Verify that coach exists and get its id
  46. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  47. if ($user_id instanceof WSError) {
  48. return $user_id;
  49. }
  50. $coachStartDate = null;
  51. if (!empty($nb_days_access_before) {
  52. $day = intval($nb_days_access_before);
  53. $coachStartDate = date('Y-m-d ', strtotime($start_date. ' + '.$day.' days'));
  54. }
  55. $coachEndDate = null;
  56. if (!empty($nb_days_access_after)) {
  57. $day = intval($nb_days_access_after);
  58. $coachEndDate = date('Y-m-d ', strtotime($end_date. ' + '.$day.' days'));
  59. }
  60. // Try to create the session
  61. $session_id = SessionManager::create_session(
  62. $name,
  63. $start_date,
  64. $end_date,
  65. $start_date,
  66. $end_date,
  67. $coachStartDate,
  68. $coachEndDate,
  69. $user_id,
  70. 0,
  71. $visibility
  72. );
  73. if(!is_int($session_id)) {
  74. return new WSError(301, 'Could not create the session');
  75. } else {
  76. // Add the Original session id to the extra fields
  77. $extras_associative = array();
  78. if($session_id_field_name != "chamilo_session_id") {
  79. $extras_associative[$session_id_field_name] = $session_id_value;
  80. }
  81. foreach($extras as $extra) {
  82. $extras_associative[$extra['field_name']] = $extra['field_value'];
  83. }
  84. // Create the extra fields
  85. foreach($extras_associative as $fname => $fvalue) {
  86. SessionManager::create_session_extra_field($fname, 1, $fname);
  87. SessionManager::update_session_extra_field_value(
  88. $session_id,
  89. $fname,
  90. $fvalue
  91. );
  92. }
  93. return $session_id;
  94. }
  95. }
  96. /**
  97. * Creates a session
  98. *
  99. * @param string API secret key
  100. * @param string Name of the session
  101. * @param string Start date, use the 'YYYY-MM-DD' format
  102. * @param string End date, use the 'YYYY-MM-DD' format
  103. * @param int Access delays of the coach (days before)
  104. * @param int Access delays of the coach (days after)
  105. * @param int Nolimit (0 = no limit of time, 1 = limit of time)
  106. * @param int Visibility
  107. * @param string User id field name for the coach
  108. * @param string User id value for the coach
  109. * @param string Original session id field name (use "chamilo_session_id" to use internal id)
  110. * @param string Original session id value
  111. * @param array Array of extra fields
  112. * @return int Session id generated
  113. */
  114. public function CreateSession($secret_key, $name, $start_date, $end_date, $nb_days_access_before, $nb_days_access_after, $nolimit, $visibility, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $extras) {
  115. $verifKey = $this->verifyKey($secret_key);
  116. if($verifKey instanceof WSError) {
  117. $this->handleError($verifKey);
  118. } else {
  119. $session_id = $this->createSessionHelper($name, $start_date, $end_date, $nb_days_access_before, $nb_days_access_after, $nolimit, $visibility, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $extras);
  120. if($session_id instanceof WSError) {
  121. $this->handleError($session_id);
  122. } else {
  123. return $session_id;
  124. }
  125. }
  126. }
  127. /**
  128. * Deletes a session (helper method)
  129. *
  130. * @param string Session id field name
  131. * @param string Session id value
  132. * @return mixed True in case of success, WSError otherwise
  133. */
  134. protected function deleteSessionHelper($session_id_field_name, $session_id_value) {
  135. $session_id = $this->getSessionId($session_id_field_name, $session_id_value);
  136. if($session_id instanceof WSError) {
  137. return $session_id;
  138. } else {
  139. SessionManager::delete($session_id, true);
  140. return true;
  141. }
  142. }
  143. /**
  144. * Deletes a session
  145. *
  146. * @param string API secret key
  147. * @param string Session id field name
  148. * @param string Session id value
  149. */
  150. public function DeleteSession($secret_key, $session_id_field_name, $session_id_value) {
  151. $verifKey = $this->verifyKey($secret_key);
  152. if($verifKey instanceof WSError) {
  153. $this->handleError($verifKey);
  154. } else {
  155. $result = $this->deleteSessionHelper($session_id_field_name, $session_id_value);
  156. if($result instanceof WSError) {
  157. $this->handleError($result);
  158. }
  159. }
  160. }
  161. /**
  162. * Edits a session (helper method)
  163. *
  164. * @param string Name of the session
  165. * @param string Start date, use the 'YYYY-MM-DD' format
  166. * @param string End date, use the 'YYYY-MM-DD' format
  167. * @param int Access delays of the coach (days before)
  168. * @param int Access delays of the coach (days after)
  169. * @param int Nolimit (0 = no limit of time, 1 = limit of time)
  170. * @param int Visibility
  171. * @param string User id field name for the coach
  172. * @param string User id value for the coach
  173. * @param string Original session id field name (use "chamilo_session_id" to use internal id)
  174. * @param string Original session id value
  175. * @param array Array of extra fields
  176. * @return mixed True on success, WSError otherwise
  177. */
  178. protected function editSessionHelper(
  179. $name,
  180. $start_date,
  181. $end_date,
  182. $nb_days_access_before,
  183. $nb_days_access_after,
  184. $nolimit,
  185. $visibility,
  186. $user_id_field_name,
  187. $user_id_value,
  188. $session_id_field_name,
  189. $session_id_value,
  190. $extras
  191. ) {
  192. $session_id = $this->getSessionId($session_id_field_name, $session_id_value);
  193. if($session_id instanceof WSError) {
  194. return $session_id;
  195. } else {
  196. // Verify that coach exists and get its id
  197. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  198. if ($user_id instanceof WSError) {
  199. return $user_id;
  200. }
  201. $coachStartDate = null;
  202. if (!empty($nb_days_access_before) {
  203. $day = intval($nb_days_access_before);
  204. $coachStartDate = date('Y-m-d ', strtotime($start_date. ' + '.$day.' days'));
  205. }
  206. $coachEndDate = null;
  207. if (!empty($nb_days_access_after)) {
  208. $day = intval($nb_days_access_after);
  209. $coachEndDate = date('Y-m-d ', strtotime($end_date. ' + '.$day.' days'));
  210. }
  211. $result_id = SessionManager::edit_session(
  212. $session_id,
  213. $name,
  214. $start_date,
  215. $end_date,
  216. $start_date,
  217. $end_date,
  218. $coachStartDate,
  219. $coachEndDate,
  220. $user_id,
  221. 0,
  222. (int)$visibility
  223. );
  224. if(!is_int($result_id)) {
  225. return new WSError(302, 'Could not edit the session');
  226. } else {
  227. if(!empty($extras)) {
  228. $extras_associative = array();
  229. foreach($extras as $extra) {
  230. $extras_associative[$extra['field_name']] = $extra['field_value'];
  231. }
  232. // Create the extra fields
  233. foreach($extras_associative as $fname => $fvalue) {
  234. SessionManager::create_session_extra_field($fname, 1, $fname);
  235. SessionManager::update_session_extra_field_value($session_id, $fname, $fvalue);
  236. }
  237. }
  238. return true;
  239. }
  240. }
  241. }
  242. /**
  243. * Edits a session
  244. *
  245. * @param string API secret key
  246. * @param string Name of the session
  247. * @param string Start date, use the 'YYYY-MM-DD' format
  248. * @param string End date, use the 'YYYY-MM-DD' format
  249. * @param int Access delays of the coach (days before)
  250. * @param int Access delays of the coach (days after)
  251. * @param int Nolimit (0 = no limit of time, 1 = limit of time)
  252. * @param int Visibility
  253. * @param string User id field name for the coach
  254. * @param string User id value for the coach
  255. * @param string Original session id field name (use "chamilo_session_id" to use internal id)
  256. * @param string Original session id value
  257. * @param array Array of extra fields
  258. */
  259. public function EditSession($secret_key, $name, $start_date, $end_date, $nb_days_access_before, $nb_days_access_after, $nolimit, $visibility, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $extras) {
  260. $verifKey = $this->verifyKey($secret_key);
  261. if($verifKey instanceof WSError) {
  262. $this->handleError($verifKey);
  263. } else {
  264. $result = $this->editSessionHelper($name, $start_date, $end_date, $nb_days_access_before, $nb_days_access_after, $nolimit, $visibility, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $extras);
  265. if($session_id_value instanceof WSError) {
  266. $this->handleError($result);
  267. }
  268. }
  269. }
  270. /**
  271. * Change user subscription (helper method)
  272. *
  273. * @param string User id field name
  274. * @param string User id value
  275. * @param string Session id field name
  276. * @param string Session id value
  277. * @param int State (1 to subscribe, 0 to unsubscribe)
  278. * @return mixed True on success, WSError otherwise
  279. */
  280. protected function changeUserSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $state) {
  281. $session_id = $this->getSessionId($session_id_field_name, $session_id_value);
  282. if($session_id instanceof WSError) {
  283. return $session_id;
  284. } else {
  285. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  286. if($user_id instanceof WSError) {
  287. return $user_id;
  288. } else {
  289. if($state == 1) {
  290. SessionManager::suscribe_users_to_session($session_id, array($user_id));
  291. } else {
  292. $result = SessionManager::unsubscribe_user_from_session($session_id, $user_id);
  293. if (!$result) {
  294. return new WSError(303, 'There was an error unsubscribing this user from the session');
  295. }
  296. }
  297. return true;
  298. }
  299. }
  300. }
  301. /**
  302. * Subscribe user to a session
  303. *
  304. * @param string API secret key
  305. * @param string User id field name
  306. * @param string User id value
  307. * @param string Session id field name
  308. * @param string Session id value
  309. */
  310. public function SubscribeUserToSession($secret_key, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value) {
  311. $verifKey = $this->verifyKey($secret_key);
  312. if($verifKey instanceof WSError) {
  313. $this->handleError($verifKey);
  314. } else {
  315. $result = $this->changeUserSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, 1);
  316. if($result instanceof WSError) {
  317. $this->handleError($result);
  318. }
  319. }
  320. }
  321. /**
  322. * Subscribe user to a session
  323. *
  324. * @param string API secret key
  325. * @param string User id field name
  326. * @param string User id value
  327. * @param string Session id field name
  328. * @param string Session id value
  329. */
  330. public function UnsubscribeUserFromSession($secret_key, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value) {
  331. $verifKey = $this->verifyKey($secret_key);
  332. if($verifKey instanceof WSError) {
  333. $this->handleError($verifKey);
  334. } else {
  335. $result = $this->changeUserSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, 0);
  336. if($result instanceof WSError) {
  337. $this->handleError($result);
  338. }
  339. }
  340. }
  341. /**
  342. * Change course subscription
  343. *
  344. * @param string Course id field name
  345. * @param string Course id value
  346. * @param string Session id field name
  347. * @param string Session id value
  348. * @param int State (1 to subscribe, 0 to unsubscribe)
  349. * @return mixed True on success, WSError otherwise
  350. */
  351. protected function changeCourseSubscription($course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value, $state) {
  352. $session_id = $this->getSessionId($session_id_field_name, $session_id_value);
  353. if($session_id instanceof WSError) {
  354. return $session_id;
  355. } else {
  356. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  357. if($course_id instanceof WSError) {
  358. return $course_id;
  359. } else {
  360. if($state == 1) {
  361. SessionManager::add_courses_to_session($session_id, array($course_id));
  362. return true;
  363. } else {
  364. $result = SessionManager::unsubscribe_course_from_session($session_id, $course_id);
  365. if ($result) {
  366. return true;
  367. } else {
  368. return new WSError(304, 'Error unsubscribing course from session');
  369. }
  370. }
  371. }
  372. }
  373. }
  374. /**
  375. * Subscribe course to session
  376. *
  377. * @param string API secret key
  378. * @param string Course id field name
  379. * @param string Course id value
  380. * @param string Session id field name
  381. * @param string Session id value
  382. */
  383. public function SubscribeCourseToSession($secret_key, $course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value) {
  384. $verifKey = $this->verifyKey($secret_key);
  385. if($verifKey instanceof WSError) {
  386. $this->handleError($verifKey);
  387. } else {
  388. $result = $this->changeCourseSubscription($course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value, 1);
  389. if($result instanceof WSError) {
  390. $this->handleError($result);
  391. }
  392. }
  393. }
  394. /**
  395. * Unsubscribe course from session
  396. *
  397. * @param string API secret key
  398. * @param string Course id field name
  399. * @param string Course id value
  400. * @param string Session id field name
  401. * @param string Session id value
  402. */
  403. public function UnsubscribeCourseFromSession($secret_key, $course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value) {
  404. $verifKey = $this->verifyKey($secret_key);
  405. if($verifKey instanceof WSError) {
  406. $this->handleError($verifKey);
  407. } else {
  408. $result = $this->changeCourseSubscription($course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value, 0);
  409. if($result instanceof WSError) {
  410. $this->handleError($result);
  411. }
  412. }
  413. }
  414. }