client_soap.php 12 KB

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