client_soap.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. global $_configuration;
  18. // see the main/inc/configuration.php file to get this value
  19. $security_key = $_configuration['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->debug_flag = true;
  30. // This should be the IP address of the client
  31. $ip_address = $_SERVER['SERVER_ADDR'];
  32. $ip_address = "192.168.1.54";
  33. //Secret key
  34. $secret_key = sha1($ip_address.$security_key);// Hash of the combination of IP Address + Chamilo security key
  35. //Creating a random user_id, this values need to be provided from your system
  36. $random_user_id = rand(0, 1000);
  37. //Creating a random username this values need to be provided from your system
  38. $generate_user_name = 'jbrion'.$random_user_id;
  39. //Creating a password (the username)
  40. $generate_password = sha1($generate_user_name);
  41. $user_field = 'uid';
  42. $params = array(
  43. 'firstname' => 'Jon',
  44. 'lastname' => 'Brion',
  45. 'status' => '5', // 5 STUDENT - 1 TEACHER
  46. 'email' => 'jon@example.com',
  47. 'loginname' => $generate_user_name,
  48. 'password' => $generate_password, // encrypted using sha1
  49. 'encrypt_method' => 'sha1',
  50. 'language' => 'english',
  51. 'official_code' => 'official',
  52. 'phone' => '00000000',
  53. 'expiration_date' => '0000-00-00',
  54. /* the extra user field that will be automatically created
  55. in the user profile see: main/admin/user_fields.php */
  56. 'original_user_id_name' => $user_field,
  57. // third party user id
  58. 'original_user_id_value' => $random_user_id,
  59. 'secret_key' => $secret_key,
  60. //Extra fields
  61. 'extra' => array(
  62. array('field_name' => 'ruc', 'field_value' => '123'),
  63. array('field_name' => 'DNI', 'field_value' => '4200000')
  64. ),
  65. );
  66. //1. Create user webservice
  67. $user_id = $client->call(
  68. 'WSCreateUserPasswordCrypted',
  69. array('createUserPasswordCrypted' => $params)
  70. );
  71. if (!empty($user_id) && is_numeric($user_id)) {
  72. // 2. Get user info of the new user
  73. echo '<h2>Trying to create an user via webservices</h2>';
  74. $original_params = $params;
  75. $params = array(
  76. 'original_user_id_value' => $random_user_id, // third party user id
  77. 'original_user_id_name' => $user_field, // the system field in the user profile (See Profiling)
  78. 'secret_key' => $secret_key
  79. );
  80. $result = $client->call('WSGetUser', array('GetUser' => $params));
  81. if ($result) {
  82. echo "Random user was created user_id: $user_id <br /><br />";
  83. echo 'User info: <br />';
  84. print_r($original_params);
  85. echo '<br /><br />';
  86. } else {
  87. echo $result;
  88. }
  89. //3. Updating user info
  90. $params = array(
  91. 'firstname' => 'Jon edited',
  92. 'lastname' => 'Brion edited',
  93. 'status' => '5', // STUDENT
  94. 'email' => 'jon@example.com',
  95. 'username' => $generate_user_name,
  96. 'password' => $generate_password, // encrypted using sha1
  97. 'encrypt_method' => 'sha1',
  98. 'phone' => '00000000',
  99. 'expiration_date' => '0000-00-00',
  100. '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
  101. 'original_user_id_value' => $random_user_id, // third party user id
  102. 'secret_key' => $secret_key,
  103. 'extra' => array(
  104. array('field_name' => 'ruc', 'field_value' => '666 edited'),
  105. array('field_name' => 'DNI', 'field_value' => '888 edited')
  106. ),
  107. );
  108. $result = $client->call('WSEditUserPasswordCrypted', array('editUserPasswordCrypted' => $params));
  109. if ($result) {
  110. echo "Random user was update user_id: $user_id <br /><br />";
  111. echo 'User info: <br />';
  112. print_r($params);
  113. echo '<br /><br />';
  114. } else {
  115. $err = $client->getError();
  116. var_dump($result);
  117. var_dump($err);
  118. }
  119. $params = array(
  120. 'ids' => array(
  121. array(
  122. 'original_user_id_name' => $user_field,
  123. 'original_user_id_value' => $random_user_id
  124. )
  125. ),
  126. 'secret_key' => $secret_key
  127. );
  128. //Disable user
  129. $result = $client->call('WSDisableUsers', array('user_ids' => $params));
  130. //Enable user
  131. $result = $client->call('WSEnableUsers', array('user_ids' => $params));
  132. //4 Creating course TEST123
  133. $params = array(
  134. 'courses' => array(
  135. array(
  136. 'title' => 'PRUEBA', //Chamilo string course code
  137. 'category_code' => 'LANG',
  138. 'wanted_code' => '',
  139. 'course_language' => 'english',
  140. 'original_course_id_name' => 'course_id_test',
  141. 'original_course_id_value' => '666',
  142. )
  143. ),
  144. 'secret_key'=> $secret_key,
  145. );
  146. $result = $client->call('WSCreateCourse', array('createCourse' => $params));
  147. //5 .Adding user to the course TEST. The course TEST must be created manually in Chamilo
  148. echo '<h2>Trying to add user to a course called TEST via webservices</h2>';
  149. $course_info = api_get_course_info('TEST123');
  150. if (!empty($course_info)) {
  151. $params = array(
  152. 'course' => 'TEST', //Chamilo string course code
  153. 'user_id' => $user_id,
  154. 'secret_key' => $secret_key
  155. );
  156. $result = $client->call('WSSubscribeUserToCourseSimple', array('subscribeUserToCourseSimple' => $params));
  157. } else {
  158. echo 'Course TEST does not exists please create one course with code "TEST"';
  159. }
  160. if ($result == 1) {
  161. echo "User $user_id was added to course TEST";
  162. } else {
  163. echo $result;
  164. }
  165. //4. Adding course Test to the Session Session1
  166. $course_id_list = array (
  167. array('course_code' => 'TEST1'),
  168. array('course_code' => 'TEST2')
  169. );
  170. $params = array('coursessessions' => array(
  171. array('original_course_id_values' => $course_id_list,
  172. 'original_course_id_name' => 'course_id_name',
  173. 'original_session_id_value' => '1',
  174. 'original_session_id_name' => 'session_id_value')
  175. ),
  176. 'secret_key' => $secret_key);
  177. //$result = $client->call('WSSuscribeCoursesToSession', array('subscribeCoursesToSession' => $params));
  178. // ------------------------
  179. //Calling the WSSubscribeUserToCourse
  180. /*
  181. $course_array = array( 'original_course_id_name' => 'TEST',
  182. 'original_course_id_value' => 'TEST'
  183. );
  184. $user_array = array('original_user_id_value' => $user_id,
  185. 'original_user_id_name' => 'name');
  186. $user_courses = array();
  187. $user_courses[] = array ( 'course_id' => $course_array,
  188. 'user_id' => $user_array,
  189. 'status' => '1'
  190. );
  191. $params = array (
  192. 'userscourses' => $user_courses,
  193. 'secret_key' => $secret_key);
  194. $result = $client->call('WSSubscribeUserToCourse', array('subscribeUserToCourse' => $params));
  195. var_dump($result);*/
  196. } else {
  197. echo 'User was not created, activate the debug=true in the registration.soap.php file and see the error logs';
  198. }
  199. // Check for an error
  200. $err = $client->getError();
  201. if ($err) {
  202. // Display the error
  203. echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
  204. }
  205. if ($client->fault) {
  206. echo '<h2>Fault</h2><pre>';
  207. print_r($result);
  208. echo '</pre>';
  209. } else {
  210. // Check for errors
  211. $err = $client->getError();
  212. if ($err) {
  213. // Display the error
  214. echo '<h2>Error</h2><pre>' . $err . '</pre>';
  215. } else {
  216. // Display the result
  217. echo '<h2>There are no errors</h2>';
  218. var_dump($result);
  219. }
  220. }