webservice_session.php 14 KB

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