courses_list.soap.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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('WSCourseList', // method name
  55. array('username' => 'xsd:string',
  56. 'signature' => 'xsd:string',
  57. 'visibilities' => 'xsd:string'), // input parameters
  58. array('return' => 'xsd:Array'), // output parameters
  59. 'urn:WSCourseList', // namespace
  60. 'urn:WSCourseList#WSCourseList', // soapaction
  61. 'rpc', // style
  62. 'encoded', // use
  63. 'This service returns a list of courses' // documentation
  64. );
  65. /**
  66. * Get a list of courses (code, url, title, teacher, language) and return to caller
  67. * Function registered as service. Returns strings in UTF-8.
  68. * @param string User name in Chamilo
  69. * @param string Signature (composed of the sha1(username+apikey)
  70. * @param mixed Array or string. Type of visibility of course (public, public-registered, private, closed)
  71. * @return array Courses list (code=>[title=>'title',url='http://...',teacher=>'...',language=>''],code=>[...],...)
  72. */
  73. function WSCourseList($username, $signature, $visibilities = 'public')
  74. {
  75. if (empty($username) or empty($signature)) { return -1; }
  76. global $_configuration;
  77. $info = api_get_user_info_from_username($username);
  78. $user_id = $info['user_id'];
  79. if (!UserManager::is_admin($user_id)) {
  80. return -1;
  81. }
  82. $list = UserManager::get_api_keys($user_id, 'dokeos');
  83. $key = '';
  84. foreach ($list as $key) {
  85. break;
  86. }
  87. $local_key = $username.$key;
  88. if (!api_is_valid_secret_key($signature, $local_key) && !api_is_valid_secret_key($signature, $username.$_configuration['security_key'])) {
  89. return -1; // The secret key is incorrect.
  90. }
  91. //public-registered = open
  92. $vis = array('public' => '3', 'public-registered' => '2', 'private' => '1', 'closed' => '0');
  93. $courses_list = array();
  94. if (!is_array($visibilities)) {
  95. $visibilities = split(',', $visibilities);
  96. }
  97. foreach ($visibilities as $visibility) {
  98. if (!in_array($visibility, array_keys($vis))) {
  99. return array('error_msg' => 'Security check failed');
  100. }
  101. $courses_list_tmp = CourseManager::get_courses_list(
  102. null,
  103. null,
  104. null,
  105. null,
  106. $vis[$visibility]
  107. );
  108. foreach ($courses_list_tmp as $index => $course) {
  109. $course_info = CourseManager::get_course_information($course['code']);
  110. $courses_list[] = array(
  111. 'code' => $course['code'],
  112. 'title' => api_utf8_encode($course_info['title']),
  113. 'url' => api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/',
  114. 'teacher' => api_utf8_encode($course_info['tutor_name']),
  115. 'language' => $course_info['course_language'],
  116. );
  117. }
  118. }
  119. return $courses_list;
  120. }
  121. // Use the request to (try to) invoke the service.
  122. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  123. $server->service($HTTP_RAW_POST_DATA);