courses_list.soap.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // 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' , '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('WSCourseList', // 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#WSCourseList', // 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 Chamilo
  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 WSCourseList($username, $signature, $visibilities = 'public') {
  69. if (empty($username) or empty($signature)) { return -1; }
  70. $securityFromConfiguration = api_get_configuration_value('security_key');
  71. $info = api_get_user_info_from_username($username);
  72. $user_id = $info['user_id'];
  73. if (!UserManager::is_admin($user_id)) { return -1; }
  74. $list = UserManager::get_api_keys($user_id, 'dokeos');
  75. $key = '';
  76. foreach ($list as $key) {
  77. break;
  78. }
  79. $local_key = $username.$key;
  80. if (!api_is_valid_secret_key($signature, $local_key) && !api_is_valid_secret_key($signature, $username.$securityFromConfiguration)) {
  81. return -1; // The secret key is incorrect.
  82. }
  83. //public-registered = open
  84. $vis = array('public' => '3', 'public-registered' => '2', 'private' => '1', 'closed' => '0');
  85. $courses_list = array();
  86. if (!is_array($visibilities)) {
  87. $visibilities = split(',', $visibilities);
  88. }
  89. foreach ($visibilities as $visibility) {
  90. if (!in_array($visibility, array_keys($vis))) {
  91. return array('error_msg' => 'Security check failed');
  92. }
  93. $courses_list_tmp = CourseManager::get_courses_list(null, null, null, null, $vis[$visibility]);
  94. foreach ($courses_list_tmp as $index => $course) {
  95. $course_info = CourseManager::get_course_information($course['code']);
  96. $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']);
  97. }
  98. }
  99. return $courses_list;
  100. }
  101. // Use the request to (try to) invoke the service.
  102. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  103. $server->service($HTTP_RAW_POST_DATA);