courses_list.soap.php 4.1 KB

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