courses_list.rest.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script provides the caller service with a list
  5. * of courses that have a certain level of visibility
  6. * on this chamilo portal.
  7. * It is set to work with the Chamilo module for Drupal:
  8. * http://drupal.org/project/chamilo.
  9. *
  10. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  11. *
  12. * @package chamilo.webservices
  13. */
  14. require_once __DIR__.'/../inc/global.inc.php';
  15. /**
  16. * Get a list of courses (code, url, title, teacher, language) and return to caller
  17. * Function registered as service. Returns strings in UTF-8.
  18. *
  19. * @param string Security key (the Dokeos install's API key)
  20. * @param mixed Array or string. Type of visibility of course (public, public-registered, private, closed)
  21. *
  22. * @return array Courses list (code=>[title=>'title',url='http://...',teacher=>'...',language=>''],code=>[...],...)
  23. */
  24. function courses_list($security_key, $visibilities = 'public')
  25. {
  26. global $_configuration;
  27. // Check if this script is launch by server and if security key is ok.
  28. if ($security_key != $_configuration['security_key']) {
  29. return ['error_msg' => 'Security check failed'];
  30. }
  31. $vis = [
  32. 'public' => '3',
  33. 'public-registered' => '2',
  34. 'private' => '1',
  35. 'closed' => '0',
  36. ];
  37. $courses_list = [];
  38. if (!is_array($visibilities)) {
  39. $tmp = $visibilities;
  40. $visibilities = [$tmp];
  41. }
  42. foreach ($visibilities as $visibility) {
  43. if (!in_array($visibility, array_keys($vis))) {
  44. return ['error_msg' => 'Security check failed'];
  45. }
  46. $courses_list_tmp = CourseManager::get_courses_list(
  47. null,
  48. null,
  49. null,
  50. null,
  51. $vis[$visibility]
  52. );
  53. foreach ($courses_list_tmp as $index => $course) {
  54. $course_info = CourseManager::get_course_information(
  55. $course['code']
  56. );
  57. $courses_list[$course['code']] = [
  58. 'title' => api_utf8_encode(
  59. $course_info['title']
  60. ),
  61. 'url' => api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/',
  62. 'teacher' => api_utf8_encode($course_info['tutor_name']),
  63. 'language' => $course_info['course_language'],
  64. ];
  65. }
  66. }
  67. return $courses_list;
  68. }
  69. header('Content-Type: text/xml; charset=utf-8');
  70. echo '<?xml version="1.0"?>';
  71. echo '<courseslist>';
  72. if (empty($_POST['security-key']) || empty($_POST['visibility'])) {
  73. echo '<errormsg>Invalid parameters, this script expects a security-key and a visibility parameters</errormsg>';
  74. } else {
  75. $courses_list = courses_list($_POST['security-key'], $_POST['visibility']);
  76. foreach ($courses_list as $code => $cd) {
  77. echo '<course>';
  78. echo '<code>', $code, '</code>';
  79. echo '<title>', $cd['title'], '</title>';
  80. echo '<url>', $cd['url'], '</url>';
  81. echo '<teacher>', $cd['teacher'], '</teacher>';
  82. echo '<language>', $cd['language'], '</language>';
  83. echo '</course>';
  84. }
  85. }
  86. echo '</courseslist>';