courses_list.rest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php //$id: $
  2. /**
  3. * This script provides the caller service with a list
  4. * of courses that have a certain level of visibility
  5. * on this dokeos portal.
  6. * It is set to work with the Dokeos module for Drupal:
  7. * http://drupal.org/project/dokeos
  8. *
  9. * See license terms in /dokeos_license.txt
  10. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  11. */
  12. require_once '../inc/global.inc.php';
  13. /**
  14. * Get a list of courses (code, url, title, teacher, language) and return to caller
  15. * Function registered as service. Returns strings in UTF-8.
  16. * @param string Security key (the Dokeos install's API key)
  17. * @param mixed Array or string. Type of visibility of course (public, public-registered, private, closed)
  18. * @return array Courses list (code=>[title=>'title',url='http://...',teacher=>'...',language=>''],code=>[...],...)
  19. */
  20. function courses_list($security_key, $visibilities = 'public') {
  21. global $_configuration;
  22. // Check if this script is launch by server and if security key is ok.
  23. if ($security_key != $_configuration['security_key']) {
  24. return array('error_msg' => 'Security check failed');
  25. }
  26. // Libraries
  27. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  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('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']);
  42. }
  43. }
  44. return $courses_list;
  45. }
  46. header('Content-Type: text/xml; charset=utf-8');
  47. echo '<?xml version="1.0"?>';
  48. echo '<courseslist>';
  49. if (empty($_POST['security-key']) || empty($_POST['visibility'])) {
  50. echo '<errormsg>Invalid parameters, this script expects a security-key and a visibility parameters</errormsg>';
  51. } else {
  52. $courses_list = courses_list($_POST['security-key'], $_POST['visibility']);
  53. foreach ($courses_list as $code => $cd) {
  54. echo '<course>';
  55. echo '<code>' , $code , '</code>';
  56. echo '<title>' , $cd['title'] , '</title>';
  57. echo '<url>' , $cd['url'] , '</url>';
  58. echo '<teacher>' , $cd['teacher'] , '</teacher>';
  59. echo '<language>' , $cd['language'] , '</language>';
  60. echo '</course>';
  61. }
  62. }
  63. echo '</courseslist>';