webservice_session.php 14 KB

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