user_info.soap.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 __DIR__.'/../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',
  27. 'type' => 'xsd:string',
  28. 'name' => 'title',
  29. 'type' => 'xsd:string',
  30. 'name' => 'url',
  31. 'type' => 'xsd:string',
  32. 'name' => 'teacher',
  33. 'type' => 'xsd:string',
  34. 'name' => 'language',
  35. 'type' => 'xsd:string',
  36. )
  37. );
  38. $server->wsdl->addComplexType(
  39. 'courseList',
  40. 'complexType',
  41. 'array',
  42. '',
  43. 'SOAP-ENC:Array',
  44. array(),
  45. array(
  46. array(
  47. 'ref' => 'SOAP-ENC:arrayType',
  48. 'wsdl:arrayType' => 'tns:courseDetails[]',
  49. ),
  50. ),
  51. 'tns:courseDetails'
  52. );
  53. // Register the method to expose
  54. $server->register(
  55. 'WSCourseListOfUser', // method name
  56. array(
  57. 'username' => 'xsd:string',
  58. 'signature' => 'xsd:string',
  59. ), // input parameters
  60. array('return' => 'xsd:Array'), // output parameters
  61. 'urn:WSUserInfo', // namespace
  62. 'urn:WSUserInfo#WSUserInfo', // soapaction
  63. 'rpc', // style
  64. 'encoded', // use
  65. 'This service returns a list of courses' // documentation
  66. );
  67. /**
  68. * Get a list of courses (code, url, title, teacher, language) for a specific
  69. * user and return to caller
  70. * Function registered as service. Returns strings in UTF-8.
  71. * @param string User name in Chamilo
  72. * @param string Signature (composed of the sha1(username+apikey)
  73. * @return array Courses list (code=>[title=>'title',url='http://...',teacher=>'...',language=>''],code=>[...],...)
  74. */
  75. function WSCourseListOfUser($username, $signature)
  76. {
  77. if (empty($username) or empty($signature)) {
  78. return -1;
  79. }
  80. $info = api_get_user_info_from_username($username);
  81. $user_id = $info['user_id'];
  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)) {
  89. return -1; // The secret key is incorrect.
  90. }
  91. $courses_list = array();
  92. $courses_list_tmp = CourseManager::get_courses_list_by_user_id($user_id);
  93. foreach ($courses_list_tmp as $index => $course) {
  94. $course_info = CourseManager::get_course_information($course['code']);
  95. $courses_list[] = array(
  96. 'code' => $course['code'],
  97. 'title' => api_utf8_encode($course_info['title']),
  98. 'url' => api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/',
  99. 'teacher' => api_utf8_encode($course_info['tutor_name']),
  100. 'language' => $course_info['course_language'],
  101. );
  102. }
  103. return $courses_list;
  104. }
  105. /* Register WSEventsList function */
  106. // Register the data structures used by the service
  107. $server->wsdl->addComplexType(
  108. 'eventDetails',
  109. 'complexType',
  110. 'struct',
  111. 'all',
  112. '',
  113. array(
  114. 'name' => 'datestart',
  115. 'type' => 'xsd:string',
  116. 'name' => 'dateend',
  117. 'type' => 'xsd:string',
  118. 'name' => 'title',
  119. 'type' => 'xsd:string',
  120. 'name' => 'link',
  121. 'type' => 'xsd:string',
  122. 'name' => 'coursetitle',
  123. 'type' => 'xsd:string',
  124. )
  125. );
  126. $server->wsdl->addComplexType(
  127. 'eventsList',
  128. 'complexType',
  129. 'array',
  130. '',
  131. 'SOAP-ENC:Array',
  132. array(),
  133. array(
  134. array(
  135. 'ref' => 'SOAP-ENC:arrayType',
  136. 'wsdl:arrayType' => 'tns:eventDetails[]',
  137. ),
  138. ),
  139. 'tns:eventDetails'
  140. );
  141. // Register the method to expose
  142. $server->register(
  143. 'WSEventsList',
  144. // method name
  145. array(
  146. 'username' => 'xsd:string',
  147. 'signature' => 'xsd:string',
  148. 'datestart' => 'xsd:int',
  149. 'dateend' => 'xsd:int',
  150. ),
  151. // input parameters
  152. array('return' => 'xsd:Array'),
  153. // output parameters
  154. 'urn:WSUserInfo',
  155. // namespace
  156. 'urn:WSUserInfo#WSEventsList',
  157. // soapaction
  158. 'rpc',
  159. // style
  160. 'encoded',
  161. // use
  162. 'This service returns a list of events of the courses the given user is subscribed to' // documentation
  163. );
  164. /**
  165. * Get a list of events between two dates for the given username
  166. * Function registered as service. Returns strings in UTF-8.
  167. * @param string Username
  168. * @param string User's API key (the user's API key)
  169. * @param int Start date, in YYYYMMDD format
  170. * @param int End date, in YYYYMMDD format
  171. * @return array Events list
  172. */
  173. function WSEventsList($username, $signature, $datestart = 0, $dateend = 0) {
  174. if (empty($username) or empty($signature)) { return -1; }
  175. $info = api_get_user_info_from_username($username);
  176. $user_id = $info['user_id'];
  177. $list = UserManager::get_api_keys($user_id, 'dokeos');
  178. $key = '';
  179. foreach ($list as $key) {
  180. break;
  181. }
  182. $local_key = $username.$key;
  183. if (!api_is_valid_secret_key($signature, $local_key)) {
  184. return -1; // The secret key is incorrect.
  185. }
  186. $events_list = array();
  187. $user_id = UserManager::get_user_id_from_username($username);
  188. if ($user_id === false) {
  189. return $events_list;
  190. } // Error in user id recovery.
  191. $ds = substr($datestart, 0, 4).'-'.substr($datestart, 4, 2).'-'.substr($datestart, 6, 2).' 00:00:00';
  192. $de = substr($dateend, 0, 4).'-'.substr($dateend, 4, 2).'-'.substr($dateend, 6, 2).' 00:00:00';
  193. $events_list = Agenda::get_personal_agenda_items_between_dates(
  194. $user_id,
  195. $ds,
  196. $de
  197. );
  198. return $events_list;
  199. }
  200. // Use the request to (try to) invoke the service.
  201. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  202. $server->service($HTTP_RAW_POST_DATA);