courses_list.rest.php 2.8 KB

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