webservice_session.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 Teacher subscription (helper method)
  343. *
  344. * @param string User id field name
  345. * @param string User id value
  346. * @param string Session id field name
  347. * @param string Session id value
  348. * @param string Course id field name
  349. * @param string Course id value
  350. * @param int State (1 to subscribe, 0 to unsubscribe)
  351. * @return mixed True on success, WSError otherwise
  352. */
  353. protected function changeTeacherSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $course_id_field_name, $course_id_value, $state)
  354. {
  355. $session_id = $this->getSessionId($session_id_field_name, $session_id_value);
  356. if ($session_id instanceof WSError) {
  357. return $session_id;
  358. } else {
  359. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  360. if ($user_id instanceof WSError) {
  361. return $user_id;
  362. } else {
  363. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  364. if ($course_id instanceof WSError) {
  365. return $course_id;
  366. } else {
  367. if ($state == 1) {
  368. SessionManager::set_coach_to_course_session($user_id, $session_id, $course_id);
  369. } else {
  370. $user_id = array (0 => $user_id);
  371. $result = SessionManager::removeUsersFromCourseSession($user_id, $session_id, $course_id);
  372. if (!$result) {
  373. return new WSError(303, 'There was an error unsubscribing this Teacher from the session');
  374. }
  375. }
  376. return true;
  377. }
  378. }
  379. }
  380. }
  381. /**
  382. * Subscribe teacher to a session course
  383. *
  384. * @param string API secret key
  385. * @param string User id field name
  386. * @param string User id value
  387. * @param string Session id field name
  388. * @param string Session id value
  389. * @param string Course id field name
  390. * @param string Course id value
  391. */
  392. public function SubscribeTeacherToSessionCourse($secret_key, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $course_id_field_name, $course_id_value)
  393. {
  394. $verifKey = $this->verifyKey($secret_key);
  395. if ($verifKey instanceof WSError) {
  396. $this->handleError($verifKey);
  397. } else {
  398. $result = $this->changeUserSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $course_id_field_name, $course_id_value, 1);
  399. if ($result instanceof WSError) {
  400. $this->handleError($result);
  401. }
  402. }
  403. }
  404. /**
  405. * Subscribe teacher to a session course
  406. *
  407. * @param string API secret key
  408. * @param string User id field name
  409. * @param string User id value
  410. * @param string Session id field name
  411. * @param string Session id value
  412. * @param string Course id field name
  413. * @param string Course id value
  414. */
  415. public function UnsubscribeTeacherFromSessionCourse($secret_key, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $course_id_field_name, $course_id_value)
  416. {
  417. $verifKey = $this->verifyKey($secret_key);
  418. if ($verifKey instanceof WSError) {
  419. $this->handleError($verifKey);
  420. } else {
  421. $result = $this->changeUserSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $course_id_field_name, $course_id_value, 0);
  422. if ($result instanceof WSError) {
  423. $this->handleError($result);
  424. }
  425. }
  426. }
  427. /**
  428. * Change course subscription
  429. *
  430. * @param string Course id field name
  431. * @param string Course id value
  432. * @param string Session id field name
  433. * @param string Session id value
  434. * @param int State (1 to subscribe, 0 to unsubscribe)
  435. * @return mixed True on success, WSError otherwise
  436. */
  437. protected function changeCourseSubscription($course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value, $state) {
  438. $session_id = $this->getSessionId($session_id_field_name, $session_id_value);
  439. if($session_id instanceof WSError) {
  440. return $session_id;
  441. } else {
  442. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  443. if($course_id instanceof WSError) {
  444. return $course_id;
  445. } else {
  446. if($state == 1) {
  447. SessionManager::add_courses_to_session($session_id, array($course_id));
  448. return true;
  449. } else {
  450. $result = SessionManager::unsubscribe_course_from_session($session_id, $course_id);
  451. if ($result) {
  452. return true;
  453. } else {
  454. return new WSError(304, 'Error unsubscribing course from session');
  455. }
  456. }
  457. }
  458. }
  459. }
  460. /**
  461. * Subscribe course to session
  462. *
  463. * @param string API secret key
  464. * @param string Course id field name
  465. * @param string Course id value
  466. * @param string Session id field name
  467. * @param string Session id value
  468. */
  469. public function SubscribeCourseToSession($secret_key, $course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value) {
  470. $verifKey = $this->verifyKey($secret_key);
  471. if($verifKey instanceof WSError) {
  472. $this->handleError($verifKey);
  473. } else {
  474. $result = $this->changeCourseSubscription($course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value, 1);
  475. if($result instanceof WSError) {
  476. $this->handleError($result);
  477. }
  478. }
  479. }
  480. /**
  481. * Unsubscribe course from session
  482. *
  483. * @param string API secret key
  484. * @param string Course id field name
  485. * @param string Course id value
  486. * @param string Session id field name
  487. * @param string Session id value
  488. */
  489. public function UnsubscribeCourseFromSession($secret_key, $course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value) {
  490. $verifKey = $this->verifyKey($secret_key);
  491. if($verifKey instanceof WSError) {
  492. $this->handleError($verifKey);
  493. } else {
  494. $result = $this->changeCourseSubscription($course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value, 0);
  495. if($result instanceof WSError) {
  496. $this->handleError($result);
  497. }
  498. }
  499. }
  500. }