webservice_session.php 14 KB

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