webservice_session.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. $libpath = api_get_path(LIBRARY_PATH);
  8. require_once __DIR__.'/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(
  54. 'Y-m-d ',
  55. strtotime($start_date.' + '.$day.' days')
  56. );
  57. }
  58. $coachEndDate = null;
  59. if (!empty($nb_days_access_after)) {
  60. $day = intval($nb_days_access_after);
  61. $coachEndDate = date(
  62. 'Y-m-d ',
  63. strtotime($end_date.' + '.$day.' days')
  64. );
  65. }
  66. // Try to create the session
  67. $session_id = SessionManager::create_session(
  68. $name,
  69. $start_date,
  70. $end_date,
  71. $start_date,
  72. $end_date,
  73. $coachStartDate,
  74. $coachEndDate,
  75. $user_id,
  76. 0,
  77. $visibility
  78. );
  79. if (!is_int($session_id)) {
  80. return new WSError(301, 'Could not create the session');
  81. } else {
  82. // Add the Original session id to the extra fields
  83. $extras_associative = array();
  84. if ($session_id_field_name != "chamilo_session_id") {
  85. $extras_associative[$session_id_field_name] = $session_id_value;
  86. }
  87. foreach ($extras as $extra) {
  88. $extras_associative[$extra['field_name']] = $extra['field_value'];
  89. }
  90. // Create the extra fields
  91. foreach ($extras_associative as $fname => $fvalue) {
  92. SessionManager::create_session_extra_field($fname, 1, $fname);
  93. SessionManager::update_session_extra_field_value(
  94. $session_id,
  95. $fname,
  96. $fvalue
  97. );
  98. }
  99. return $session_id;
  100. }
  101. }
  102. /**
  103. * Creates a session
  104. *
  105. * @param string API secret key
  106. * @param string Name of the session
  107. * @param string Start date, use the 'YYYY-MM-DD' format
  108. * @param string End date, use the 'YYYY-MM-DD' format
  109. * @param int Access delays of the coach (days before)
  110. * @param int Access delays of the coach (days after)
  111. * @param int Nolimit (0 = no limit of time, 1 = limit of time)
  112. * @param int Visibility
  113. * @param string User id field name for the coach
  114. * @param string User id value for the coach
  115. * @param string Original session id field name (use "chamilo_session_id" to use internal id)
  116. * @param string Original session id value
  117. * @param array Array of extra fields
  118. * @return int Session id generated
  119. */
  120. public function CreateSession(
  121. $secret_key,
  122. $name,
  123. $start_date,
  124. $end_date,
  125. $nb_days_access_before,
  126. $nb_days_access_after,
  127. $nolimit,
  128. $visibility,
  129. $user_id_field_name,
  130. $user_id_value,
  131. $session_id_field_name,
  132. $session_id_value,
  133. $extras
  134. ) {
  135. $verifKey = $this->verifyKey($secret_key);
  136. if ($verifKey instanceof WSError) {
  137. $this->handleError($verifKey);
  138. } else {
  139. $session_id = $this->createSessionHelper(
  140. $name,
  141. $start_date,
  142. $end_date,
  143. $nb_days_access_before,
  144. $nb_days_access_after,
  145. $nolimit,
  146. $visibility,
  147. $user_id_field_name,
  148. $user_id_value,
  149. $session_id_field_name,
  150. $session_id_value,
  151. $extras
  152. );
  153. if ($session_id instanceof WSError) {
  154. $this->handleError($session_id);
  155. } else {
  156. return $session_id;
  157. }
  158. }
  159. }
  160. /**
  161. * Deletes a session (helper method)
  162. *
  163. * @param string Session id field name
  164. * @param string Session id value
  165. * @return mixed True in case of success, WSError otherwise
  166. */
  167. protected function deleteSessionHelper(
  168. $session_id_field_name,
  169. $session_id_value
  170. ) {
  171. $session_id = $this->getSessionId(
  172. $session_id_field_name,
  173. $session_id_value
  174. );
  175. if ($session_id instanceof WSError) {
  176. return $session_id;
  177. } else {
  178. SessionManager::delete($session_id, true);
  179. return true;
  180. }
  181. }
  182. /**
  183. * Deletes a session
  184. *
  185. * @param string API secret key
  186. * @param string Session id field name
  187. * @param string Session id value
  188. */
  189. public function DeleteSession(
  190. $secret_key,
  191. $session_id_field_name,
  192. $session_id_value
  193. ) {
  194. $verifKey = $this->verifyKey($secret_key);
  195. if ($verifKey instanceof WSError) {
  196. $this->handleError($verifKey);
  197. } else {
  198. $result = $this->deleteSessionHelper(
  199. $session_id_field_name,
  200. $session_id_value
  201. );
  202. if ($result instanceof WSError) {
  203. $this->handleError($result);
  204. }
  205. }
  206. }
  207. /**
  208. * Edits a session (helper method)
  209. *
  210. * @param string Name of the session
  211. * @param string Start date, use the 'YYYY-MM-DD' format
  212. * @param string End date, use the 'YYYY-MM-DD' format
  213. * @param int Access delays of the coach (days before)
  214. * @param int Access delays of the coach (days after)
  215. * @param int Nolimit (0 = no limit of time, 1 = limit of time)
  216. * @param int Visibility
  217. * @param string User id field name for the coach
  218. * @param string User id value for the coach
  219. * @param string Original session id field name (use "chamilo_session_id" to use internal id)
  220. * @param string Original session id value
  221. * @param array Array of extra fields
  222. * @return mixed True on success, WSError otherwise
  223. */
  224. protected function editSessionHelper(
  225. $name,
  226. $start_date,
  227. $end_date,
  228. $nb_days_access_before,
  229. $nb_days_access_after,
  230. $nolimit,
  231. $visibility,
  232. $user_id_field_name,
  233. $user_id_value,
  234. $session_id_field_name,
  235. $session_id_value,
  236. $extras
  237. ) {
  238. $session_id = $this->getSessionId(
  239. $session_id_field_name,
  240. $session_id_value
  241. );
  242. if ($session_id instanceof WSError) {
  243. return $session_id;
  244. } else {
  245. // Verify that coach exists and get its id
  246. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  247. if ($user_id instanceof WSError) {
  248. return $user_id;
  249. }
  250. $coachStartDate = null;
  251. if (!empty($nb_days_access_before)) {
  252. $day = intval($nb_days_access_before);
  253. $coachStartDate = date(
  254. 'Y-m-d ',
  255. strtotime($start_date.' + '.$day.' days')
  256. );
  257. }
  258. $coachEndDate = null;
  259. if (!empty($nb_days_access_after)) {
  260. $day = intval($nb_days_access_after);
  261. $coachEndDate = date(
  262. 'Y-m-d ',
  263. strtotime($end_date.' + '.$day.' days')
  264. );
  265. }
  266. $result_id = SessionManager::edit_session(
  267. $session_id,
  268. $name,
  269. $start_date,
  270. $end_date,
  271. $start_date,
  272. $end_date,
  273. $coachStartDate,
  274. $coachEndDate,
  275. $user_id,
  276. 0,
  277. (int) $visibility
  278. );
  279. if (!is_int($result_id)) {
  280. return new WSError(302, 'Could not edit the session');
  281. } else {
  282. if (!empty($extras)) {
  283. $extras_associative = array();
  284. foreach ($extras as $extra) {
  285. $extras_associative[$extra['field_name']] = $extra['field_value'];
  286. }
  287. // Create the extra fields
  288. foreach ($extras_associative as $fname => $fvalue) {
  289. SessionManager::create_session_extra_field(
  290. $fname,
  291. 1,
  292. $fname
  293. );
  294. SessionManager::update_session_extra_field_value(
  295. $session_id,
  296. $fname,
  297. $fvalue
  298. );
  299. }
  300. }
  301. return true;
  302. }
  303. }
  304. }
  305. /**
  306. * Edits a session
  307. *
  308. * @param string API secret key
  309. * @param string Name of the session
  310. * @param string Start date, use the 'YYYY-MM-DD' format
  311. * @param string End date, use the 'YYYY-MM-DD' format
  312. * @param int Access delays of the coach (days before)
  313. * @param int Access delays of the coach (days after)
  314. * @param int Nolimit (0 = no limit of time, 1 = limit of time)
  315. * @param int Visibility
  316. * @param string User id field name for the coach
  317. * @param string User id value for the coach
  318. * @param string Original session id field name (use "chamilo_session_id" to use internal id)
  319. * @param string Original session id value
  320. * @param array Array of extra fields
  321. */
  322. public function EditSession(
  323. $secret_key,
  324. $name,
  325. $start_date,
  326. $end_date,
  327. $nb_days_access_before,
  328. $nb_days_access_after,
  329. $nolimit,
  330. $visibility,
  331. $user_id_field_name,
  332. $user_id_value,
  333. $session_id_field_name,
  334. $session_id_value,
  335. $extras
  336. ) {
  337. $verifKey = $this->verifyKey($secret_key);
  338. if ($verifKey instanceof WSError) {
  339. $this->handleError($verifKey);
  340. } else {
  341. $result = $this->editSessionHelper(
  342. $name,
  343. $start_date,
  344. $end_date,
  345. $nb_days_access_before,
  346. $nb_days_access_after,
  347. $nolimit,
  348. $visibility,
  349. $user_id_field_name,
  350. $user_id_value,
  351. $session_id_field_name,
  352. $session_id_value,
  353. $extras
  354. );
  355. if ($session_id_value instanceof WSError) {
  356. $this->handleError($result);
  357. }
  358. }
  359. }
  360. /**
  361. * Change user subscription (helper method)
  362. *
  363. * @param string User id field name
  364. * @param string User id value
  365. * @param string Session id field name
  366. * @param string Session id value
  367. * @param int State (1 to subscribe, 0 to unsubscribe)
  368. * @return mixed True on success, WSError otherwise
  369. */
  370. protected function changeUserSubscription(
  371. $user_id_field_name,
  372. $user_id_value,
  373. $session_id_field_name,
  374. $session_id_value,
  375. $state
  376. ) {
  377. $session_id = $this->getSessionId(
  378. $session_id_field_name,
  379. $session_id_value
  380. );
  381. if ($session_id instanceof WSError) {
  382. return $session_id;
  383. } else {
  384. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  385. if ($user_id instanceof WSError) {
  386. return $user_id;
  387. } else {
  388. if ($state == 1) {
  389. SessionManager::subscribe_users_to_session(
  390. $session_id,
  391. array($user_id)
  392. );
  393. } else {
  394. $result = SessionManager::unsubscribe_user_from_session(
  395. $session_id,
  396. $user_id
  397. );
  398. if (!$result) {
  399. return new WSError(
  400. 303,
  401. 'There was an error unsubscribing this user from the session'
  402. );
  403. }
  404. }
  405. return true;
  406. }
  407. }
  408. }
  409. /**
  410. * Subscribe user to a session
  411. *
  412. * @param string API secret key
  413. * @param string User id field name
  414. * @param string User id value
  415. * @param string Session id field name
  416. * @param string Session id value
  417. */
  418. public function SubscribeUserToSession(
  419. $secret_key,
  420. $user_id_field_name,
  421. $user_id_value,
  422. $session_id_field_name,
  423. $session_id_value
  424. ) {
  425. $verifKey = $this->verifyKey($secret_key);
  426. if ($verifKey instanceof WSError) {
  427. $this->handleError($verifKey);
  428. } else {
  429. $result = $this->changeUserSubscription(
  430. $user_id_field_name,
  431. $user_id_value,
  432. $session_id_field_name,
  433. $session_id_value,
  434. 1
  435. );
  436. if ($result instanceof WSError) {
  437. $this->handleError($result);
  438. }
  439. }
  440. }
  441. /**
  442. * Subscribe user to a session
  443. *
  444. * @param string API secret key
  445. * @param string User id field name
  446. * @param string User id value
  447. * @param string Session id field name
  448. * @param string Session id value
  449. */
  450. public function UnsubscribeUserFromSession(
  451. $secret_key,
  452. $user_id_field_name,
  453. $user_id_value,
  454. $session_id_field_name,
  455. $session_id_value
  456. ) {
  457. $verifKey = $this->verifyKey($secret_key);
  458. if ($verifKey instanceof WSError) {
  459. $this->handleError($verifKey);
  460. } else {
  461. $result = $this->changeUserSubscription(
  462. $user_id_field_name,
  463. $user_id_value,
  464. $session_id_field_name,
  465. $session_id_value,
  466. 0
  467. );
  468. if ($result instanceof WSError) {
  469. $this->handleError($result);
  470. }
  471. }
  472. }
  473. /**
  474. * Change Teacher subscription (helper method)
  475. *
  476. * @param string User id field name
  477. * @param string User id value
  478. * @param string Session id field name
  479. * @param string Session id value
  480. * @param string Course id field name
  481. * @param string Course id value
  482. * @param int State (1 to subscribe, 0 to unsubscribe)
  483. * @return mixed True on success, WSError otherwise
  484. */
  485. protected function changeTeacherSubscription(
  486. $user_id_field_name,
  487. $user_id_value,
  488. $session_id_field_name,
  489. $session_id_value,
  490. $course_id_field_name,
  491. $course_id_value,
  492. $state
  493. ) {
  494. $session_id = $this->getSessionId(
  495. $session_id_field_name,
  496. $session_id_value
  497. );
  498. if ($session_id instanceof WSError) {
  499. return $session_id;
  500. } else {
  501. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  502. if ($user_id instanceof WSError) {
  503. return $user_id;
  504. } else {
  505. $course_id = $this->getCourseId(
  506. $course_id_field_name,
  507. $course_id_value
  508. );
  509. if ($course_id instanceof WSError) {
  510. return $course_id;
  511. } else {
  512. if ($state == 1) {
  513. SessionManager::set_coach_to_course_session(
  514. $user_id,
  515. $session_id,
  516. $course_id
  517. );
  518. } else {
  519. $user_id = array(0 => $user_id);
  520. $result = SessionManager::removeUsersFromCourseSession(
  521. $user_id,
  522. $session_id,
  523. $course_id
  524. );
  525. if (!$result) {
  526. return new WSError(
  527. 303,
  528. 'There was an error unsubscribing this Teacher from the session'
  529. );
  530. }
  531. }
  532. return true;
  533. }
  534. }
  535. }
  536. }
  537. /**
  538. * Subscribe teacher to a session course
  539. *
  540. * @param string API secret key
  541. * @param string User id field name
  542. * @param string User id value
  543. * @param string Session id field name
  544. * @param string Session id value
  545. * @param string Course id field name
  546. * @param string Course id value
  547. */
  548. public function SubscribeTeacherToSessionCourse(
  549. $secret_key,
  550. $user_id_field_name,
  551. $user_id_value,
  552. $session_id_field_name,
  553. $session_id_value,
  554. $course_id_field_name,
  555. $course_id_value
  556. ) {
  557. $verifKey = $this->verifyKey($secret_key);
  558. if ($verifKey instanceof WSError) {
  559. $this->handleError($verifKey);
  560. } else {
  561. $result = $this->changeUserSubscription(
  562. $user_id_field_name,
  563. $user_id_value,
  564. $session_id_field_name,
  565. $session_id_value,
  566. $course_id_field_name,
  567. $course_id_value,
  568. 1
  569. );
  570. if ($result instanceof WSError) {
  571. $this->handleError($result);
  572. }
  573. }
  574. }
  575. /**
  576. * Subscribe teacher to a session course
  577. *
  578. * @param string API secret key
  579. * @param string User id field name
  580. * @param string User id value
  581. * @param string Session id field name
  582. * @param string Session id value
  583. * @param string Course id field name
  584. * @param string Course id value
  585. */
  586. public function UnsubscribeTeacherFromSessionCourse(
  587. $secret_key,
  588. $user_id_field_name,
  589. $user_id_value,
  590. $session_id_field_name,
  591. $session_id_value,
  592. $course_id_field_name,
  593. $course_id_value
  594. ) {
  595. $verifKey = $this->verifyKey($secret_key);
  596. if ($verifKey instanceof WSError) {
  597. $this->handleError($verifKey);
  598. } else {
  599. $result = $this->changeUserSubscription(
  600. $user_id_field_name,
  601. $user_id_value,
  602. $session_id_field_name,
  603. $session_id_value,
  604. $course_id_field_name,
  605. $course_id_value,
  606. 0
  607. );
  608. if ($result instanceof WSError) {
  609. $this->handleError($result);
  610. }
  611. }
  612. }
  613. /**
  614. * Change course subscription
  615. *
  616. * @param string Course id field name
  617. * @param string Course id value
  618. * @param string Session id field name
  619. * @param string Session id value
  620. * @param int State (1 to subscribe, 0 to unsubscribe)
  621. * @return mixed True on success, WSError otherwise
  622. */
  623. protected function changeCourseSubscription(
  624. $course_id_field_name,
  625. $course_id_value,
  626. $session_id_field_name,
  627. $session_id_value,
  628. $state
  629. ) {
  630. $session_id = $this->getSessionId(
  631. $session_id_field_name,
  632. $session_id_value
  633. );
  634. if ($session_id instanceof WSError) {
  635. return $session_id;
  636. } else {
  637. $course_id = $this->getCourseId(
  638. $course_id_field_name,
  639. $course_id_value
  640. );
  641. if ($course_id instanceof WSError) {
  642. return $course_id;
  643. } else {
  644. if ($state == 1) {
  645. SessionManager::add_courses_to_session(
  646. $session_id,
  647. array($course_id)
  648. );
  649. return true;
  650. } else {
  651. $result = SessionManager::unsubscribe_course_from_session(
  652. $session_id,
  653. $course_id
  654. );
  655. if ($result) {
  656. return true;
  657. } else {
  658. return new WSError(
  659. 304,
  660. 'Error unsubscribing course from session'
  661. );
  662. }
  663. }
  664. }
  665. }
  666. }
  667. /**
  668. * Subscribe course to session
  669. *
  670. * @param string API secret key
  671. * @param string Course id field name
  672. * @param string Course id value
  673. * @param string Session id field name
  674. * @param string Session id value
  675. */
  676. public function SubscribeCourseToSession(
  677. $secret_key,
  678. $course_id_field_name,
  679. $course_id_value,
  680. $session_id_field_name,
  681. $session_id_value
  682. ) {
  683. $verifKey = $this->verifyKey($secret_key);
  684. if ($verifKey instanceof WSError) {
  685. $this->handleError($verifKey);
  686. } else {
  687. $result = $this->changeCourseSubscription(
  688. $course_id_field_name,
  689. $course_id_value,
  690. $session_id_field_name,
  691. $session_id_value,
  692. 1
  693. );
  694. if ($result instanceof WSError) {
  695. $this->handleError($result);
  696. }
  697. }
  698. }
  699. /**
  700. * Unsubscribe course from session
  701. *
  702. * @param string API secret key
  703. * @param string Course id field name
  704. * @param string Course id value
  705. * @param string Session id field name
  706. * @param string Session id value
  707. */
  708. public function UnsubscribeCourseFromSession(
  709. $secret_key,
  710. $course_id_field_name,
  711. $course_id_value,
  712. $session_id_field_name,
  713. $session_id_value
  714. ) {
  715. $verifKey = $this->verifyKey($secret_key);
  716. if ($verifKey instanceof WSError) {
  717. $this->handleError($verifKey);
  718. } else {
  719. $result = $this->changeCourseSubscription(
  720. $course_id_field_name,
  721. $course_id_value,
  722. $session_id_field_name,
  723. $session_id_value,
  724. 0
  725. );
  726. if ($result instanceof WSError) {
  727. $this->handleError($result);
  728. }
  729. }
  730. }
  731. }