courses_list.soap.php 4.3 KB

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