client_soap.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. exit; //Uncomment this in order to execute the page
  12. require_once '../inc/global.inc.php';
  13. $libpath = api_get_path(LIBRARY_PATH);
  14. require_once $libpath.'nusoap/nusoap.php';
  15. // Create the client instancete one course with code
  16. $url = api_get_path(WEB_CODE_PATH)."webservices/registration.soap.php?wsdl";
  17. //$url = api_get_path(WEB_CODE_PATH)."webservices/lp.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->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.13";
  34. //Secret key
  35. $secret_key = sha1($ip_address.$security_key);// Hash of the combination of IP Address + Chamilo security key
  36. //Creating a random user_id, this values need to be provided from your system
  37. $random_user_id = rand(0, 1000);
  38. //Creating a random username this values need to be provided from your system
  39. $generate_user_name = 'jbrion'.$random_user_id;
  40. //Creating a password (the username)
  41. $generate_password = sha1($generate_user_name);
  42. $user_field = 'external_user_id';
  43. $externalCourseId = 'external_course_id';
  44. /*$file = base64_encode(file_get_contents('/home/jmontoya/Downloads/Oefeningen_Gezondheid_deel_1.zip'));
  45. $params = [
  46. 'secret_key' => $secret_key,
  47. 'file_data' => $file,
  48. 'filename' => 'Oefeningen_Gezondheid_deel_1.zip',
  49. 'course_id_name' => 'external_course_id',
  50. 'course_id_value' => '2',
  51. 'session_id_name' => 'external_session_id',
  52. 'session_id_value' => '1',
  53. ];
  54. //1. Create user webservice
  55. $result = $client->call(
  56. 'WSImportLP',
  57. array('params' => $params)
  58. );
  59. if ($result) {
  60. // print_r($params);
  61. echo '<br /><br />';
  62. } else {
  63. $err = $client->getError();
  64. // var_dump($result);
  65. var_dump($err);
  66. }
  67. var_dump($result);exit;
  68. */
  69. $params = array(
  70. 'firstname' => 'Jon',
  71. 'lastname' => 'Brion',
  72. 'status' => '5', // 5 STUDENT - 1 TEACHER
  73. 'email' => 'jon@example.com',
  74. 'loginname' => $generate_user_name,
  75. 'password' => $generate_password, // encrypted using sha1
  76. 'encrypt_method' => 'sha1',
  77. 'language' => 'english',
  78. 'official_code' => 'official',
  79. 'phone' => '00000000',
  80. 'expiration_date' => '0000-00-00',
  81. /* the extra user field that will be automatically created
  82. in the user profile see: main/admin/user_fields.php */
  83. 'original_user_id_name' => $user_field,
  84. // third party user id
  85. 'original_user_id_value' => $random_user_id,
  86. 'secret_key' => $secret_key,
  87. //Extra fields
  88. 'extra' => array(
  89. array('field_name' => 'ruc', 'field_value' => '123'),
  90. array('field_name' => 'DNI', 'field_value' => '4200000')
  91. ),
  92. );
  93. //1. Create user webservice
  94. $user_id = $client->call(
  95. 'WSCreateUserPasswordCrypted',
  96. array('createUserPasswordCrypted' => $params)
  97. );
  98. if (!empty($user_id) && is_numeric($user_id)) {
  99. // 2. Get user info of the new user
  100. echo '<h2>Trying to create an user via webservices</h2>';
  101. $original_params = $params;
  102. $params = array(
  103. 'original_user_id_value' => $random_user_id,
  104. // third party user id
  105. 'original_user_id_name' => $user_field,
  106. // the system field in the user profile (See Profiling)
  107. 'secret_key' => $secret_key,
  108. );
  109. $result = $client->call('WSGetUser', array('GetUser' => $params));
  110. if ($result) {
  111. echo "Random user was created user_id: $user_id <br /><br />";
  112. echo 'User info: <br />';
  113. print_r($original_params);
  114. echo '<br /><br />';
  115. } else {
  116. echo $result;
  117. }
  118. //3. Updating user info
  119. $params = array(
  120. 'firstname' => 'Jon edited',
  121. 'lastname' => 'Brion edited',
  122. 'status' => '5',
  123. // STUDENT
  124. 'email' => 'jon@example.com',
  125. 'username' => $generate_user_name,
  126. 'password' => $generate_password,
  127. // encrypted using sha1
  128. 'encrypt_method' => 'sha1',
  129. 'phone' => '00000000',
  130. 'expiration_date' => '0000-00-00',
  131. '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
  132. 'original_user_id_value' => $random_user_id, // third party user id
  133. 'secret_key' => $secret_key,
  134. 'extra' => array(
  135. array('field_name' => 'ruc', 'field_value' => '666 edited'),
  136. array('field_name' => 'DNI', 'field_value' => '888 edited'),
  137. )
  138. );
  139. $result = $client->call('WSEditUserPasswordCrypted', array('editUserPasswordCrypted' => $params));
  140. if ($result) {
  141. echo "Random user was update user_id: $user_id <br /><br />";
  142. echo 'User info: <br />';
  143. print_r($params);
  144. echo '<br /><br />';
  145. } else {
  146. $err = $client->getError();
  147. var_dump($result);
  148. var_dump($err);
  149. }
  150. $params = array(
  151. 'ids' => array(
  152. array(
  153. 'original_user_id_name' => $user_field,
  154. 'original_user_id_value' => $random_user_id
  155. )
  156. ),
  157. 'secret_key' => $secret_key
  158. );
  159. // Disable user
  160. echo "Random user was disable user_id: $user_id <br /><br />";
  161. $result = $client->call('WSDisableUsers', array('user_ids' => $params));
  162. // Enable user
  163. $result = $client->call('WSEnableUsers', array('user_ids' => $params));
  164. echo "Random user was enable user_id: $user_id <br /><br />";
  165. $externalCourseIdValue = '666'.$random_user_id;
  166. //4 Creating course TEST123
  167. $params = array(
  168. 'courses' => array(
  169. array(
  170. 'title' => 'PRUEBA '.$random_user_id, //Chamilo string course code
  171. 'category_code' => 'LANG',
  172. 'wanted_code' => $externalCourseIdValue,
  173. 'course_language' => 'english',
  174. 'original_course_id_name' => $externalCourseId,
  175. 'original_course_id_value' => $externalCourseIdValue,
  176. )
  177. ),
  178. 'secret_key'=> $secret_key,
  179. );
  180. //5 .Adding user to the course TEST. The course TEST must be created manually in Chamilo
  181. echo "<h2>Trying to Create course called $externalCourseIdValue via webservices</h2>";
  182. var_dump($params);
  183. $result = $client->call('WSCreateCourse', array('createCourse' => $params));
  184. //5 .Adding user to the course TEST. The course TEST must be created manually in Chamilo
  185. echo "<h2>Trying to add user to a course called $externalCourseIdValue via webservices</h2>";
  186. $course_info = api_get_course_info($externalCourseIdValue);
  187. if (!empty($course_info)) {
  188. $params = array(
  189. 'course' => $externalCourseIdValue, //Chamilo string course code
  190. 'user_id' => $user_id,
  191. 'secret_key' => $secret_key,
  192. );
  193. $result = $client->call('WSSubscribeUserToCourseSimple', array('subscribeUserToCourseSimple' => $params));
  194. echo "Course $externalCourseIdValue was created<br/>";
  195. } else {
  196. echo "Course $externalCourseIdValue does not exists<br/>";
  197. exit;
  198. }
  199. if ($result == 1) {
  200. echo "User $user_id was added to course $externalCourseIdValue<br/>";
  201. } else {
  202. echo $result;
  203. }
  204. $params = [
  205. 'original_user_id_value' => $random_user_id,
  206. 'original_user_id_name' => $user_field,
  207. 'original_course_id_value' => $externalCourseIdValue,
  208. 'original_course_id_name' => $externalCourseId,
  209. 'secret_key' => $secret_key,
  210. ];
  211. //5 .Adding user to the course TEST. The course TEST must be created manually in Chamilo
  212. echo "<h2>Trying to remove user to a course called $externalCourseIdValue via webservices</h2>";
  213. $result = $client->call('WSUnSubscribeUserFromCourseSimple', array('unSubscribeUserFromCourseSimple' => $params));
  214. if ($result) {
  215. var_dump($result);
  216. echo 'User removed from course';
  217. } else {
  218. $err = $client->getError();
  219. var_dump($result);
  220. var_dump($err);
  221. }
  222. exit;
  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(
  261. 'WSSubscribeUserToCourse',
  262. array('subscribeUserToCourse' => $params)
  263. );
  264. var_dump($result);
  265. } else {
  266. echo 'User was not created, activate the debug=true in the registration.soap.php file and see the error logs';
  267. }
  268. // Check for an error
  269. $err = $client->getError();
  270. if ($err) {
  271. // Display the error
  272. echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
  273. }
  274. if ($client->fault) {
  275. echo '<h2>Fault</h2><pre>';
  276. print_r($result);
  277. echo '</pre>';
  278. } else {
  279. // Check for errors
  280. $err = $client->getError();
  281. if ($err) {
  282. // Display the error
  283. echo '<h2>Error</h2><pre>' . $err . '</pre>';
  284. } else {
  285. // Display the result
  286. echo '<h2>There are no errors</h2>';
  287. var_dump($result);
  288. }
  289. }