client_soap.php 9.6 KB

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