user_info.soap.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script provides the caller service with user details.
  5. * It is set to work with the Chamilo module for Drupal:
  6. * http://drupal.org/project/chamilo
  7. *
  8. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  9. * @package chamilo.webservices
  10. */
  11. //require_once '../inc/global.inc.php';
  12. $libpath = api_get_path(LIBRARY_PATH);
  13. // Create the server instance
  14. $server = new soap_server();
  15. // Initialize WSDL support
  16. $server->configureWSDL('WSUserInfo', 'urn:WSUserInfo');
  17. /* Register WSCourseList function */
  18. // Register the data structures used by the service
  19. $server->wsdl->addComplexType(
  20. 'courseDetails',
  21. 'complexType',
  22. 'struct',
  23. 'all',
  24. '',
  25. array(
  26. 'name'=>'code' , 'type'=>'xsd:string',
  27. 'name'=>'title' , 'type'=>'xsd:string',
  28. 'name'=>'url' , 'type'=>'xsd:string',
  29. 'name'=>'teacher', 'type'=>'xsd:string',
  30. 'name'=>'language','type'=>'xsd:string',
  31. )
  32. );
  33. $server->wsdl->addComplexType(
  34. 'courseList',
  35. 'complexType',
  36. 'array',
  37. '',
  38. 'SOAP-ENC:Array',
  39. array(),
  40. array(
  41. array('ref'=>'SOAP-ENC:arrayType',
  42. 'wsdl:arrayType'=>'tns:courseDetails[]')
  43. ),
  44. 'tns:courseDetails'
  45. );
  46. // Register the method to expose
  47. $server->register('WSCourseListOfUser', // method name
  48. array('username' => 'xsd:string',
  49. 'signature' => 'xsd:string'), // input parameters
  50. array('return' => 'xsd:Array'), // output parameters
  51. 'urn:WSUserInfo', // namespace
  52. 'urn:WSUserInfo#WSUserInfo', // soapaction
  53. 'rpc', // style
  54. 'encoded', // use
  55. 'This service returns a list of courses' // documentation
  56. );
  57. /**
  58. * Get a list of courses (code, url, title, teacher, language) for a specific
  59. * user and return to caller
  60. * Function registered as service. Returns strings in UTF-8.
  61. * @param string User name in Chamilo
  62. * @param string Signature (composed of the sha1(username+apikey)
  63. * @return array Courses list (code=>[title=>'title',url='http://...',teacher=>'...',language=>''],code=>[...],...)
  64. */
  65. function WSCourseListOfUser($username, $signature)
  66. {
  67. if (empty($username) or empty($signature)) {
  68. return -1;
  69. }
  70. $info = api_get_user_info_from_username($username);
  71. $user_id = $info['user_id'];
  72. $list = UserManager::get_api_keys($user_id, 'dokeos');
  73. $key = '';
  74. foreach ($list as $key) {
  75. break;
  76. }
  77. $local_key = $username.$key;
  78. if (!api_is_valid_secret_key($signature, $local_key)) {
  79. return -1; // The secret key is incorrect.
  80. }
  81. $courses_list = array();
  82. $courses_list_tmp = CourseManager::get_courses_list_by_user_id($user_id);
  83. foreach ($courses_list_tmp as $index => $course) {
  84. $course_info = CourseManager::get_course_information($course['code']);
  85. $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']);
  86. }
  87. return $courses_list;
  88. }
  89. /* Register WSEventsList function */
  90. // Register the data structures used by the service
  91. $server->wsdl->addComplexType(
  92. 'eventDetails',
  93. 'complexType',
  94. 'struct',
  95. 'all',
  96. '',
  97. array(
  98. 'name'=>'datestart','type'=>'xsd:string',
  99. 'name'=>'dateend','type'=>'xsd:string',
  100. 'name'=>'title','type'=>'xsd:string',
  101. 'name'=>'link','type'=>'xsd:string',
  102. 'name'=>'coursetitle','type'=>'xsd:string',
  103. )
  104. );
  105. $server->wsdl->addComplexType(
  106. 'eventsList',
  107. 'complexType',
  108. 'array',
  109. '',
  110. 'SOAP-ENC:Array',
  111. array(),
  112. array(
  113. array('ref'=>'SOAP-ENC:arrayType',
  114. 'wsdl:arrayType'=>'tns:eventDetails[]')
  115. ),
  116. 'tns:eventDetails'
  117. );
  118. // Register the method to expose
  119. $server->register('WSEventsList', // method name
  120. array('username' => 'xsd:string',
  121. 'signature' => 'xsd:string',
  122. 'datestart' => 'xsd:int',
  123. 'dateend' => 'xsd:int'), // input parameters
  124. array('return' => 'xsd:Array'), // output parameters
  125. 'urn:WSUserInfo', // namespace
  126. 'urn:WSUserInfo#WSEventsList', // soapaction
  127. 'rpc', // style
  128. 'encoded', // use
  129. 'This service returns a list of events of the courses the given user is subscribed to' // documentation
  130. );
  131. /**
  132. * Get a list of events between two dates for the given username
  133. * Function registered as service. Returns strings in UTF-8.
  134. * @param string Username
  135. * @param string User's API key (the user's API key)
  136. * @param int Start date, in YYYYMMDD format
  137. * @param int End date, in YYYYMMDD format
  138. * @return array Events list
  139. */
  140. function WSEventsList($username, $signature, $datestart = 0, $dateend = 0)
  141. {
  142. if (empty($username) or empty($signature)) {
  143. return -1;
  144. }
  145. $info = api_get_user_info_from_username($username);
  146. $user_id = $info['user_id'];
  147. $list = UserManager::get_api_keys($user_id, 'dokeos');
  148. $key = '';
  149. foreach ($list as $key) {
  150. break;
  151. }
  152. $local_key = $username.$key;
  153. if (!api_is_valid_secret_key($signature, $local_key)) {
  154. return -1; // The secret key is incorrect.
  155. }
  156. $events_list = array();
  157. $user_id = UserManager::get_user_id_from_username($username);
  158. if ($user_id === false) { return $events_list; } // Error in user id recovery.
  159. $ds = substr($datestart,0,4).'-'.substr($datestart,4,2).'-'.substr($datestart,6,2).' 00:00:00';
  160. $de = substr($dateend,0,4).'-'.substr($dateend,4,2).'-'.substr($dateend,6,2).' 00:00:00';
  161. $events_list = Agenda::get_personal_agenda_items_between_dates($user_id, $ds, $de);
  162. return $events_list;
  163. }
  164. // Use the request to (try to) invoke the service.
  165. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  166. $server->service($HTTP_RAW_POST_DATA);