user_info.soap.php 6.0 KB

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