courses_list.rest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 '../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. $securityFromConfiguration = api_get_configuration_value('security_key');
  24. // Check if this script is launch by server and if security key is ok.
  25. if ($security_key != $securityFromConfiguration) {
  26. return array('error_msg' => 'Security check failed');
  27. }
  28. $vis = array('public' => '3', 'public-registered' => '2', 'private' => '1', 'closed' => '0');
  29. $courses_list = array();
  30. if (!is_array($visibilities)) {
  31. $tmp = $visibilities;
  32. $visibilities = array($tmp);
  33. }
  34. foreach ($visibilities as $visibility) {
  35. if (!in_array($visibility, array_keys($vis))) {
  36. return array('error_msg' => 'Security check failed');
  37. }
  38. $courses_list_tmp = CourseManager::get_courses_list(null, null, null, null, $vis[$visibility]);
  39. foreach ($courses_list_tmp as $index => $course) {
  40. $course_info = CourseManager::get_course_information($course['code']);
  41. $courses_list[$course['code']] = array(
  42. 'title' => api_utf8_encode($course_info['title']),
  43. 'url' => api_get_path(WEB_COURSE_PATH) . $course_info['directory'] . '/',
  44. 'teacher' => api_utf8_encode($course_info['tutor_name']),
  45. 'language' => $course_info['course_language']
  46. );
  47. }
  48. }
  49. return $courses_list;
  50. }
  51. header('Content-Type: text/xml; charset=utf-8');
  52. echo '<?xml version="1.0"?>';
  53. echo '<courseslist>';
  54. if (empty($_POST['security-key']) || empty($_POST['visibility'])) {
  55. echo '<errormsg>Invalid parameters, this script expects a security-key and a visibility parameters</errormsg>';
  56. } else {
  57. $courses_list = courses_list($_POST['security-key'], $_POST['visibility']);
  58. foreach ($courses_list as $code => $cd) {
  59. echo '<course>';
  60. echo '<code>' , $code , '</code>';
  61. echo '<title>' , $cd['title'] , '</title>';
  62. echo '<url>' , $cd['url'] , '</url>';
  63. echo '<teacher>' , $cd['teacher'] , '</teacher>';
  64. echo '<language>' , $cd['language'] , '</language>';
  65. echo '</course>';
  66. }
  67. }
  68. echo '</courseslist>';