courses_list.rest.php 2.7 KB

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