courses_list.rest.php 3.1 KB

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