courses_list.soap.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. *
  12. * @package chamilo.webservices
  13. */
  14. require_once __DIR__.'/../inc/global.inc.php';
  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. [
  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. [],
  47. [
  48. ['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. ['username' => 'xsd:string',
  57. 'signature' => 'xsd:string',
  58. 'visibilities' => 'xsd:string', ], // input parameters
  59. ['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. *
  70. * @param string User name in Chamilo
  71. * @param string Signature (composed of the sha1(username+apikey)
  72. * @param mixed Array or string. Type of visibility of course (public, public-registered, private, closed)
  73. *
  74. * @return array Courses list (code=>[title=>'title',url='http://...',teacher=>'...',language=>''],code=>[...],...)
  75. */
  76. function WSCourseList($username, $signature, $visibilities = 'public')
  77. {
  78. if (empty($username) or empty($signature)) {
  79. return -1;
  80. }
  81. global $_configuration;
  82. $info = api_get_user_info_from_username($username);
  83. $user_id = $info['user_id'];
  84. if (!UserManager::is_admin($user_id)) {
  85. return -1;
  86. }
  87. $list = UserManager::get_api_keys($user_id, 'dokeos');
  88. $key = '';
  89. foreach ($list as $key) {
  90. break;
  91. }
  92. $local_key = $username.$key;
  93. if (!api_is_valid_secret_key($signature, $local_key) &&
  94. !api_is_valid_secret_key($signature, $username.$_configuration['security_key'])
  95. ) {
  96. return -1; // The secret key is incorrect.
  97. }
  98. //public-registered = open
  99. $vis = ['public' => '3', 'public-registered' => '2', 'private' => '1', 'closed' => '0'];
  100. $courses_list = [];
  101. if (!is_array($visibilities)) {
  102. $visibilities = split(',', $visibilities);
  103. }
  104. foreach ($visibilities as $visibility) {
  105. if (!in_array($visibility, array_keys($vis))) {
  106. return ['error_msg' => 'Security check failed'];
  107. }
  108. $courses_list_tmp = CourseManager::get_courses_list(
  109. null,
  110. null,
  111. null,
  112. null,
  113. $vis[$visibility]
  114. );
  115. foreach ($courses_list_tmp as $index => $course) {
  116. $course_info = CourseManager::get_course_information($course['code']);
  117. $courses_list[] = [
  118. 'code' => $course['code'],
  119. 'title' => api_utf8_encode($course_info['title']),
  120. 'url' => api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/',
  121. 'teacher' => api_utf8_encode($course_info['tutor_name']),
  122. 'language' => $course_info['course_language'],
  123. ];
  124. }
  125. }
  126. return $courses_list;
  127. }
  128. // Use the request to (try to) invoke the service.
  129. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  130. $server->service($HTTP_RAW_POST_DATA);