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