webservice_session.php 23 KB

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