client_soap.php 12 KB

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