client_soap.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /*
  4. *
  5. * 1. This script creates users every time the page is executed using the Chamilo Webservices
  6. * 2. The username is generated every time with a random value from 0 to 1000
  7. * 3. The default user extra field (profile) is "uid" is created
  8. *
  9. * when calling the WSCreateUserPasswordCrypted for the first time, you can change this value.
  10. * In this field your third party user_id will be registered.
  11. * See the main/admin/user_fields.php to view the current user fields.
  12. * 4. You need to create manually a course called Test(with code TEST)
  13. * After the user was created the new user will be added to this course via webservices.
  14. *
  15. */
  16. exit;
  17. require_once __DIR__.'/../inc/global.inc.php';
  18. // Create the client instance
  19. $url = api_get_path(WEB_CODE_PATH).'webservices/registration.soap.php?wsdl';
  20. //$url = api_get_path(WEB_CODE_PATH)."webservices/access_url.php?wsdl";
  21. global $_configuration;
  22. // see the main/inc/configuration.php file to get this value
  23. $security_key = $_configuration['security_key'];
  24. $client = new nusoap_client($url, true);
  25. /*$client->xml_encoding = 'UTF-8';
  26. $client->http_encoding = 'UTF-8';
  27. $client->charencoding = 'UTF-8';*/
  28. $soap_error = $client->getError();
  29. if (!empty($soap_error)) {
  30. $error_message = 'Nusoap object creation failed: '.$soap_error;
  31. throw new Exception($error_message);
  32. }
  33. $client->setDebugLevel(10000);
  34. $client->debug_flag = true;
  35. // This should be the IP address of the client
  36. $ip_address = $_SERVER['SERVER_ADDR'];
  37. $ip_address = "192.168.1.54";
  38. $ip_address = "127.0.0.1";
  39. //Secret key
  40. $secret_key = sha1($ip_address.$security_key); // Hash of the combination of IP Address + Chamilo security key
  41. //$secret_key = sha1($security_key);
  42. //Creating a random user_id, this values need to be provided from your system
  43. $random_user_id = rand(0, 1000);
  44. //Creating a random username this values need to be provided from your system
  45. $generate_user_name = 'jbrion'.$random_user_id;
  46. //Creating a password (the username)
  47. $generate_password = sha1($generate_user_name);
  48. $user_field = 'uid';
  49. $sessionField = 'external_session_id';
  50. $params = [
  51. 'firstname' => 'Jon',
  52. 'lastname' => 'Brion',
  53. 'status' => '5', // 5 STUDENT - 1 TEACHER
  54. 'email' => 'jon@example.com',
  55. 'loginname' => $generate_user_name,
  56. 'password' => $generate_password, // encrypted using sha1
  57. 'encrypt_method' => 'bcrypt',
  58. 'language' => 'english',
  59. 'official_code' => 'official',
  60. 'phone' => '00000000',
  61. 'expiration_date' => '0000-00-00',
  62. /* the extra user field that will be automatically created
  63. in the user profile see: main/admin/user_fields.php */
  64. 'original_user_id_name' => $user_field,
  65. // third party user id
  66. 'original_user_id_value' => $random_user_id,
  67. 'secret_key' => $secret_key,
  68. // Extra fields
  69. 'extra' => [
  70. ['field_name' => 'ruc', 'field_value' => '123'],
  71. ['field_name' => 'DNI', 'field_value' => '4200000'],
  72. ],
  73. ];
  74. //1. Create user webservice
  75. $user_id = $client->call(
  76. 'WSCreateUserPasswordCrypted',
  77. ['createUserPasswordCrypted' => $params]
  78. );
  79. // Check for an error
  80. $err = $client->getError();
  81. if ($err) {
  82. // Display the error
  83. echo '<h2>Constructor error</h2><pre>'.$err.'</pre>';
  84. }
  85. $sessionValueRandom = uniqid();
  86. $params = [
  87. 'sessions' => [
  88. [
  89. 'name' => 'session from ws: '.$sessionValueRandom,
  90. 'year_start' => '2015',
  91. 'month_start' => '10',
  92. 'day_start' => '1',
  93. 'year_end' => '',
  94. 'month_end' => '',
  95. 'day_end' => '',
  96. 'nb_days_access_before' => 0,
  97. 'nb_days_access_after' => 0,
  98. 'nolimit' => 1,
  99. 'user_id' => 1,
  100. 'original_session_id_name' => $sessionField,
  101. 'original_session_id_value' => $sessionValueRandom,
  102. 'extra' => '',
  103. ],
  104. ],
  105. 'secret_key' => $secret_key,
  106. ];
  107. $sessionId = $client->call(
  108. 'WSCreateSession',
  109. ['createSession' => $params]
  110. );
  111. $data = [
  112. 'secret_key' => $secret_key,
  113. 'userssessions' => [
  114. [
  115. 'original_user_id_name' => $user_field,
  116. 'original_session_id_value' => $sessionValueRandom,
  117. 'original_session_id_name' => $sessionField,
  118. 'original_user_id_values' => [
  119. [
  120. 'original_user_id_value' => $random_user_id,
  121. ],
  122. ],
  123. ],
  124. ],
  125. ];
  126. $result = $client->call(
  127. 'WSSuscribeUsersToSession',
  128. ['subscribeUsersToSession' => $data]
  129. );
  130. $err = $client->getError();
  131. var_dump($result);
  132. var_dump($err);
  133. var_dump($user_id);
  134. if (!empty($user_id) && is_numeric($user_id)) {
  135. // 2. Get user info of the new user
  136. echo '<h2>Trying to create an user via webservices</h2>';
  137. $original_params = $params;
  138. $params = [
  139. 'original_user_id_value' => $random_user_id, // third party user id
  140. 'original_user_id_name' => $user_field, // the system field in the user profile (See Profiling)
  141. 'secret_key' => $secret_key,
  142. ];
  143. $result = $client->call('WSGetUser', ['GetUser' => $params]);
  144. if ($result) {
  145. echo "Random user was created user_id: $user_id <br /><br />";
  146. echo 'User info: <br />';
  147. print_r($original_params);
  148. echo '<br /><br />';
  149. } else {
  150. echo $result;
  151. }
  152. // 3. Updating user info
  153. $params = [
  154. 'firstname' => 'Jon edited',
  155. 'lastname' => 'Brion edited',
  156. 'status' => '5',
  157. // STUDENT
  158. 'email' => 'jon@example.com',
  159. 'username' => $generate_user_name,
  160. 'password' => $generate_password,
  161. // encrypted using sha1
  162. 'encrypt_method' => 'sha1',
  163. 'phone' => '00000000',
  164. 'expiration_date' => '0000-00-00',
  165. 'original_user_id_name' => $user_field,
  166. // the extra user field that will be automatically created in the user profile see: main/admin/user_fields.php
  167. 'original_user_id_value' => $random_user_id,
  168. // third party user id
  169. 'secret_key' => $secret_key,
  170. 'extra' => [
  171. ['field_name' => 'ruc', 'field_value' => '666 edited'],
  172. ['field_name' => 'DNI', 'field_value' => '888 edited'],
  173. ],
  174. ];
  175. $result = $client->call('WSEditUserPasswordCrypted', ['editUserPasswordCrypted' => $params]);
  176. if ($result) {
  177. echo "Random user was update user_id: $user_id <br /><br />";
  178. echo 'User info: <br />';
  179. print_r($params);
  180. echo '<br /><br />';
  181. } else {
  182. $err = $client->getError();
  183. var_dump($result);
  184. var_dump($err);
  185. }
  186. $params = [
  187. 'ids' => [
  188. [
  189. 'original_user_id_name' => $user_field,
  190. 'original_user_id_value' => $random_user_id,
  191. ],
  192. ],
  193. 'secret_key' => $secret_key,
  194. ];
  195. // Disable user
  196. $result = $client->call('WSDisableUsers', ['user_ids' => $params]);
  197. // Enable user
  198. $result = $client->call('WSEnableUsers', ['user_ids' => $params]);
  199. // 4 Creating course TEST123
  200. $params = [
  201. 'courses' => [
  202. [
  203. 'title' => 'PRUEBA', //Chamilo string course code
  204. 'category_code' => 'LANG',
  205. 'wanted_code' => '',
  206. 'course_language' => 'english',
  207. 'original_course_id_name' => 'course_id_test',
  208. 'original_course_id_value' => '666',
  209. ],
  210. ],
  211. 'secret_key' => $secret_key,
  212. ];
  213. $result = $client->call('WSCreateCourse', ['createCourse' => $params]);
  214. // 5 .Adding user to the course TEST. The course TEST must be created manually in Chamilo
  215. echo '<h2>Trying to add user to a course called TEST via webservices</h2>';
  216. $course_info = api_get_course_info('TEST123');
  217. if (!empty($course_info)) {
  218. $params = [
  219. 'course' => 'TEST', //Chamilo string course code
  220. 'user_id' => $user_id,
  221. 'secret_key' => $secret_key,
  222. ];
  223. $result = $client->call('WSSubscribeUserToCourseSimple', ['subscribeUserToCourseSimple' => $params]);
  224. } else {
  225. echo 'Course TEST does not exists please create one course with code "TEST"';
  226. }
  227. if ($result == 1) {
  228. echo "User $user_id was added to course TEST";
  229. } else {
  230. echo $result;
  231. }
  232. // 4. Adding course Test to the Session Session1
  233. $course_id_list = [
  234. ['course_code' => 'TEST1'],
  235. ['course_code' => 'TEST2'],
  236. ];
  237. $params = [
  238. 'coursessessions' => [
  239. [
  240. 'original_course_id_values' => $course_id_list,
  241. 'original_course_id_name' => 'course_id_name',
  242. 'original_session_id_value' => '1',
  243. 'original_session_id_name' => 'session_id_value',
  244. ],
  245. ],
  246. 'secret_key' => $secret_key,
  247. ];
  248. //$result = $client->call('WSSuscribeCoursesToSession', array('subscribeCoursesToSession' => $params));
  249. // ------------------------
  250. //Calling the WSSubscribeUserToCourse
  251. $course_array = [
  252. 'original_course_id_name' => 'TEST',
  253. 'original_course_id_value' => 'TEST',
  254. ];
  255. $user_array = [
  256. 'original_user_id_value' => $random_user_id,
  257. 'original_user_id_name' => $user_field,
  258. ];
  259. $user_courses = [];
  260. $user_courses[] = [
  261. 'course_id' => $course_array,
  262. 'user_id' => $user_array,
  263. 'status' => '1',
  264. ];
  265. $params = [
  266. 'userscourses' => $user_courses,
  267. 'secret_key' => $secret_key,
  268. ];
  269. $result = $client->call('WSSubscribeUserToCourse', ['subscribeUserToCourse' => $params]);
  270. var_dump($result);
  271. $params = [
  272. 'secret_key' => $secret_key,
  273. 'ids' => [
  274. 'original_user_id_value' => $random_user_id,
  275. 'original_user_id_name' => $user_field,
  276. ],
  277. ];
  278. // Delete user
  279. $result = $client->call('WSDeleteUsers', ['user_ids' => $params]);
  280. exit;
  281. } else {
  282. echo 'User was not created, activate the debug=true in the registration.soap.php file and see the error logs';
  283. }
  284. // Check for an error
  285. $err = $client->getError();
  286. if ($err) {
  287. // Display the error
  288. echo '<h2>Constructor error</h2><pre>'.$err.'</pre>';
  289. }
  290. //1. Create user webservice
  291. $result = $client->call(
  292. 'WSGetPortals',
  293. ['getPortals' => ['secret_key' => $secret_key]]
  294. );
  295. $result = $client->call(
  296. 'WSAddUserToPortal',
  297. ['addUserToPortal' => ['user_id' => 1, 'portal_id' => 1, 'secret_key' => $secret_key]]
  298. );
  299. $result = $client->call(
  300. 'WSGetPortalListFromUser',
  301. ['getPortalListFromUser' => ['user_id' => 1, 'secret_key' => $secret_key]]
  302. );
  303. $result = $client->call(
  304. 'WSGetPortalListFromCourse',
  305. ['getPortalListFromCourse' => ['course_id' => 20, 'secret_key' => $secret_key]]
  306. );
  307. $result = $client->call(
  308. 'WSAddCourseToPortal',
  309. ['addCourseToPortal' => ['course_id' => 20, 'portal_id' => 1, 'secret_key' => $secret_key]]
  310. );
  311. $result = $client->call(
  312. 'WSRemoveUserFromPortal',
  313. ['removeUserFromPortal' => ['course_id' => 20, 'portal_id' => 1, 'secret_key' => $secret_key]]
  314. );
  315. var_dump($user_id); exit;
  316. if ($client->fault) {
  317. echo '<h2>Fault</h2><pre>';
  318. print_r($result);
  319. echo '</pre>';
  320. } else {
  321. // Check for errors
  322. $err = $client->getError();
  323. if ($err) {
  324. // Display the error
  325. echo '<h2>Error</h2><pre>'.$err.'</pre>';
  326. } else {
  327. // Display the result
  328. echo '<h2>There are no errors</h2>';
  329. var_dump($result);
  330. }
  331. }