client_soap.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. require_once $libpath.'nusoap/nusoap.php';
  16. // Create the client instance
  17. $url = api_get_path(WEB_CODE_PATH)."webservices/registration.soap.php?wsdl";
  18. $security_key = $_configuration['security_key']; // see the main/inc/configuration.php file to get this value
  19. $client = new nusoap_client($url, true);
  20. $soap_error = $client->getError();
  21. if (!empty($soap_error)) {
  22. $error_message = 'Nusoap object creation failed: ' . $soap_error;
  23. throw new Exception($error_message);
  24. }
  25. $client->debug_flag = true;
  26. $ip_address = $_SERVER['SERVER_ADDR']; // This should be the IP address of the client
  27. //Secret key
  28. $secret_key = sha1($ip_address.$security_key);// Hash of the combination of IP Address + Chamilo security key
  29. //Creating a random user name and a random user id, this values need to be provided from your system
  30. $random_user_id = rand(0, 1000);
  31. $generate_user_name = 'jbrion'.$random_user_id;
  32. $generate_password = sha1($generate_user_name);
  33. $user_field = 'uid';
  34. // 1. Create user
  35. $params = array( 'firstname' => 'Jon',
  36. 'lastname' => 'Brion',
  37. 'status' => '5', // STUDENT
  38. 'email' => 'jon@example.com',
  39. 'loginname' => $generate_user_name,
  40. 'password' => $generate_password, // encrypted using sha1
  41. 'encrypt_method' => 'sha1',
  42. 'language' => 'english',
  43. 'official_code' => 'official',
  44. 'phone' => '00000000',
  45. 'expiration_date' => '0000-00-00',
  46. '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
  47. 'original_user_id_value' => $random_user_id, // third party user id
  48. 'secret_key' => $secret_key
  49. );
  50. $user_id = $client->call('WSCreateUserPasswordCrypted', array('createUserPasswordCrypted' => $params));
  51. if (!empty($user_id) && is_numeric($user_id)) {
  52. // 2. Get user info of the user
  53. echo '<h2>Trying to create an user via webservices</h2>';
  54. $original_params = $params;
  55. $params = array('original_user_id_value' => $random_user_id, // third party user id
  56. 'original_user_id_name' => $user_field, // the system field in the user profile (See Profiling)
  57. 'secret_key' => $secret_key);
  58. $result = $client->call('WSGetUser', array('GetUser' => $params));
  59. if ($result) {
  60. echo "Random user was created user_id: $user_id <br /><br />";
  61. echo 'User info: <br />';
  62. print_r($original_params);
  63. echo '<br /><br />';
  64. } else {
  65. echo $result;
  66. }
  67. //3.Adding user to the course TEST. The course TEST must be created manually in Chamilo
  68. echo '<h2>Trying to add user to a course called TEST via webservices</h2>';
  69. $course_info = api_get_course_info('TEST');
  70. if (!empty($course_info)) {
  71. $params = array('course' => 'TEST', //Chamilo string course code
  72. 'user_id' => $user_id,
  73. 'secret_key' => $secret_key);
  74. $result = $client->call('WSSubscribeUserToCourseSimple', array('subscribeUserToCourseSimple' => $params));
  75. } else {
  76. echo 'Course TEST does not exists please create one course with code "TEST"';
  77. }
  78. if ($result == 1) {
  79. echo "User $user_id was added to course TEST";
  80. } else {
  81. echo $result;
  82. }
  83. //4. Adding course Test to the Session Session1
  84. $course_id_list = array (
  85. array('course_code' => 'TEST1'),
  86. array('course_code' => 'TEST2')
  87. );
  88. $params = array('coursessessions' => array(
  89. array('original_course_id_values' => $course_id_list,
  90. 'original_course_id_name' => 'course_id_name',
  91. 'original_session_id_value' => '1',
  92. 'original_session_id_name' => 'session_id_value')
  93. ),
  94. 'secret_key' => $secret_key);
  95. //$result = $client->call('WSSuscribeCoursesToSession', array('subscribeCoursesToSession' => $params));
  96. // ------------------------
  97. //Calling the WSSubscribeUserToCourse
  98. /*
  99. $course_array = array( 'original_course_id_name' => 'TEST',
  100. 'original_course_id_value' => 'TEST'
  101. );
  102. $user_array = array('original_user_id_value' => $user_id,
  103. 'original_user_id_name' => 'name');
  104. $user_courses = array();
  105. $user_courses[] = array ( 'course_id' => $course_array,
  106. 'user_id' => $user_array,
  107. 'status' => '1'
  108. );
  109. $params = array (
  110. 'userscourses' => $user_courses,
  111. 'secret_key' => $secret_key);
  112. $result = $client->call('WSSubscribeUserToCourse', array('subscribeUserToCourse' => $params));
  113. var_dump($result);*/
  114. } else {
  115. echo 'User was not created, activate the debug=true in the registration.soap.php file and see the error logs';
  116. }
  117. // Check for an error
  118. $err = $client->getError();
  119. if ($err) {
  120. // Display the error
  121. echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
  122. }
  123. if ($client->fault) {
  124. echo '<h2>Fault</h2><pre>';
  125. print_r($result);
  126. echo '</pre>';
  127. } else {
  128. // Check for errors
  129. $err = $client->getError();
  130. if ($err) {
  131. // Display the error
  132. echo '<h2>Error</h2><pre>' . $err . '</pre>';
  133. } else {
  134. // Display the result
  135. echo '<h2>There are no errors</h2>';
  136. var_dump($result);
  137. }
  138. }