courses_list.soap.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. $libpath = api_get_path(LIBRARY_PATH);
  14. require_once $libpath.'nusoap/nusoap.php';
  15. // Create the server instance
  16. $server = new soap_server();
  17. // Initialize WSDL support
  18. $server->configureWSDL('WSCourseList', 'urn:WSCourseList');
  19. /* Register DokeosWSCourseList function */
  20. // Register the data structures used by the service
  21. $server->wsdl->addComplexType(
  22. 'courseDetails',
  23. 'complexType',
  24. 'struct',
  25. 'all',
  26. '',
  27. array(
  28. 'name'=>'code' , 'type'=>'xsd:string',
  29. 'name'=>'title' , 'type'=>'xsd:string',
  30. 'name'=>'url' , 'type'=>'xsd:string',
  31. 'name'=>'teacher', 'type'=>'xsd:string',
  32. 'name'=>'language','type'=>'xsd:string',
  33. )
  34. );
  35. $server->wsdl->addComplexType(
  36. 'courseList',
  37. 'complexType',
  38. 'array',
  39. '',
  40. 'SOAP-ENC:Array',
  41. array(),
  42. array(
  43. array('ref'=>'SOAP:ENC:arrayType',
  44. 'wsdl:arrayType'=>'tns:courseDetails[]')
  45. ),
  46. 'tns:courseDetails'
  47. );
  48. // Register the method to expose
  49. $server->register('DokeosWSCourseList', // method name
  50. array('username' => 'xsd:string',
  51. 'signature' => 'xsd:string',
  52. 'visibilities' => 'xsd:string'), // input parameters
  53. array('return' => 'xsd:Array'), // output parameters
  54. 'urn:WSCourseList', // namespace
  55. 'urn:WSCourseList#DokeosWSCourseList', // soapaction
  56. 'rpc', // style
  57. 'encoded', // use
  58. 'This service returns a list of courses' // documentation
  59. );
  60. /**
  61. * Get a list of courses (code, url, title, teacher, language) and return to caller
  62. * Function registered as service. Returns strings in UTF-8.
  63. * @param string User name in Dokeos
  64. * @param string Signature (composed of the sha1(username+apikey)
  65. * @param mixed Array or string. Type of visibility of course (public, public-registered, private, closed)
  66. * @return array Courses list (code=>[title=>'title',url='http://...',teacher=>'...',language=>''],code=>[...],...)
  67. */
  68. function DokeosWSCourseList($username, $signature, $visibilities = 'public') {
  69. if (empty($username) or empty($signature)) { return -1; }
  70. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  71. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  72. global $_configuration;
  73. $info = api_get_user_info_from_username($username);
  74. $user_id = $info['user_id'];
  75. if (!UserManager::is_admin($user_id)) { return -1; }
  76. $list = UserManager::get_api_keys($user_id, 'dokeos');
  77. $key = '';
  78. foreach ($list as $key) {
  79. break;
  80. }
  81. $local_key = $username.$key;
  82. if (!api_is_valid_secret_key($signature, $local_key)) {
  83. return -1; // The secret key is incorrect.
  84. }
  85. // Libraries
  86. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  87. $vis = array('public' => '3', 'public-registered' => '2', 'private' => '1', 'closed' => '0');
  88. $courses_list = array();
  89. if (!is_array($visibilities)) {
  90. $visibilities = split(',', $visibilities);
  91. }
  92. foreach ($visibilities as $visibility) {
  93. if (!in_array($visibility, array_keys($vis))) {
  94. return array('error_msg' => 'Security check failed');
  95. }
  96. $courses_list_tmp = CourseManager::get_courses_list(null, null, null, null, $vis[$visibility]);
  97. foreach ($courses_list_tmp as $index => $course) {
  98. $course_info = CourseManager::get_course_information($course['code']);
  99. $courses_list[] = array('code' => $course['code'], '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']);
  100. }
  101. }
  102. return $courses_list;
  103. }
  104. // Use the request to (try to) invoke the service.
  105. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  106. $server->service($HTTP_RAW_POST_DATA);