user_info.soap.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. global $_configuration;
  81. $info = api_get_user_info_from_username($username);
  82. $user_id = $info['user_id'];
  83. $list = UserManager::get_api_keys($user_id, 'dokeos');
  84. $key = '';
  85. foreach ($list as $key) {
  86. break;
  87. }
  88. $local_key = $username.$key;
  89. if (!api_is_valid_secret_key($signature, $local_key)) {
  90. return -1; // The secret key is incorrect.
  91. }
  92. $courses_list = array();
  93. $courses_list_tmp = CourseManager::get_courses_list_by_user_id($user_id);
  94. foreach ($courses_list_tmp as $index => $course) {
  95. $course_info = CourseManager::get_course_information($course['code']);
  96. $courses_list[] = array(
  97. 'code' => $course['code'],
  98. 'title' => api_utf8_encode($course_info['title']),
  99. 'url' => api_get_path(
  100. WEB_COURSE_PATH
  101. ).$course_info['directory'].'/',
  102. 'teacher' => api_utf8_encode($course_info['tutor_name']),
  103. 'language' => $course_info['course_language'],
  104. );
  105. }
  106. return $courses_list;
  107. }
  108. /* Register WSEventsList function */
  109. // Register the data structures used by the service
  110. $server->wsdl->addComplexType(
  111. 'eventDetails',
  112. 'complexType',
  113. 'struct',
  114. 'all',
  115. '',
  116. array(
  117. 'name' => 'datestart',
  118. 'type' => 'xsd:string',
  119. 'name' => 'dateend',
  120. 'type' => 'xsd:string',
  121. 'name' => 'title',
  122. 'type' => 'xsd:string',
  123. 'name' => 'link',
  124. 'type' => 'xsd:string',
  125. 'name' => 'coursetitle',
  126. 'type' => 'xsd:string',
  127. )
  128. );
  129. $server->wsdl->addComplexType(
  130. 'eventsList',
  131. 'complexType',
  132. 'array',
  133. '',
  134. 'SOAP-ENC:Array',
  135. array(),
  136. array(
  137. array(
  138. 'ref' => 'SOAP-ENC:arrayType',
  139. 'wsdl:arrayType' => 'tns:eventDetails[]',
  140. ),
  141. ),
  142. 'tns:eventDetails'
  143. );
  144. // Register the method to expose
  145. $server->register(
  146. 'WSEventsList',
  147. // method name
  148. array(
  149. 'username' => 'xsd:string',
  150. 'signature' => 'xsd:string',
  151. 'datestart' => 'xsd:int',
  152. 'dateend' => 'xsd:int',
  153. ),
  154. // input parameters
  155. array('return' => 'xsd:Array'),
  156. // output parameters
  157. 'urn:WSUserInfo',
  158. // namespace
  159. 'urn:WSUserInfo#WSEventsList',
  160. // soapaction
  161. 'rpc',
  162. // style
  163. 'encoded',
  164. // use
  165. 'This service returns a list of events of the courses the given user is subscribed to' // documentation
  166. );
  167. /**
  168. * Get a list of events between two dates for the given username
  169. * Function registered as service. Returns strings in UTF-8.
  170. * @param string Username
  171. * @param string User's API key (the user's API key)
  172. * @param int Start date, in YYYYMMDD format
  173. * @param int End date, in YYYYMMDD format
  174. * @return array Events list
  175. */
  176. function WSEventsList($username, $signature, $datestart = 0, $dateend = 0) {
  177. if (empty($username) or empty($signature)) { return -1; }
  178. global $_configuration;
  179. $info = api_get_user_info_from_username($username);
  180. $user_id = $info['user_id'];
  181. $list = UserManager::get_api_keys($user_id, 'dokeos');
  182. $key = '';
  183. foreach ($list as $key) {
  184. break;
  185. }
  186. $local_key = $username.$key;
  187. if (!api_is_valid_secret_key($signature, $local_key)) {
  188. return -1; // The secret key is incorrect.
  189. }
  190. $events_list = array();
  191. $user_id = UserManager::get_user_id_from_username($username);
  192. if ($user_id === false) {
  193. return $events_list;
  194. } // Error in user id recovery.
  195. $ds = substr($datestart, 0, 4).'-'.substr($datestart, 4, 2).'-'.substr($datestart, 6, 2).' 00:00:00';
  196. $de = substr($dateend, 0, 4).'-'.substr($dateend, 4, 2).'-'.substr($dateend, 6, 2).' 00:00:00';
  197. $events_list = Agenda::get_personal_agenda_items_between_dates(
  198. $user_id,
  199. $ds,
  200. $de
  201. );
  202. return $events_list;
  203. }
  204. // Use the request to (try to) invoke the service.
  205. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  206. $server->service($HTTP_RAW_POST_DATA);