courses_list.soap.php 4.3 KB

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