From 917d5538c827e3e9447b1c5e8d87f7908817ad51 Mon Sep 17 00:00:00 2001 From: jmontoyaa Date: Mon, 8 Jan 2018 08:47:51 +0100 Subject: [PATCH] Minor - format code add error_logs --- main/webservices/client_soap.php | 277 ++++++++++++------------- main/webservices/registration.soap.php | 102 ++++++--- 2 files changed, 204 insertions(+), 175 deletions(-) diff --git a/main/webservices/client_soap.php b/main/webservices/client_soap.php index dd02fb44cc..5661889f0d 100755 --- a/main/webservices/client_soap.php +++ b/main/webservices/client_soap.php @@ -3,20 +3,21 @@ /* * - * 1. This script creates users everytime the page is executed using the Chamilo Webservices - * 2. The username is generated everytime with a random value from 0 to 1000 - * 3. The default user extra field (profile) is "uid" is created when calling the WSCreateUserPasswordCrypted for the first time, you can change this value. - * In this field your third party user_id will be registered. See the main/admin/user_fields.php to view the current user fields. - * 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. - + * 1. This script creates users every time the page is executed using the Chamilo Webservices + * 2. The username is generated every time with a random value from 0 to 1000 + * 3. The default user extra field (profile) is "uid" is created + * + * when calling the WSCreateUserPasswordCrypted for the first time, you can change this value. + * In this field your third party user_id will be registered. + * See the main/admin/user_fields.php to view the current user fields. + * 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. * */ - -exit; //Uncomment this in order to execute the page - +exit; require_once __DIR__.'/../inc/global.inc.php'; // Create the client instance -$url = api_get_path(WEB_CODE_PATH)."webservices/registration.soap.php?wsdl"; +$url = api_get_path(WEB_CODE_PATH).'webservices/registration.soap.php?wsdl'; //$url = api_get_path(WEB_CODE_PATH)."webservices/access_url.php?wsdl"; global $_configuration; @@ -55,35 +56,35 @@ $generate_password = sha1($generate_user_name); $user_field = 'uid'; $sessionField = 'external_session_id'; -$params = array( - 'firstname' => 'Jon', - 'lastname' => 'Brion', - 'status' => '5', // 5 STUDENT - 1 TEACHER - 'email' => 'jon@example.com', - 'loginname' => $generate_user_name, - 'password' => $generate_password, // encrypted using sha1 - 'encrypt_method' => 'sha1', - 'language' => 'english', - 'official_code' => 'official', - 'phone' => '00000000', - 'expiration_date' => '0000-00-00', +$params = [ + 'firstname' => 'Jon', + 'lastname' => 'Brion', + 'status' => '5', // 5 STUDENT - 1 TEACHER + 'email' => 'jon@example.com', + 'loginname' => $generate_user_name, + 'password' => $generate_password, // encrypted using sha1 + 'encrypt_method' => 'bcrypt', + 'language' => 'english', + 'official_code' => 'official', + 'phone' => '00000000', + 'expiration_date' => '0000-00-00', /* the extra user field that will be automatically created in the user profile see: main/admin/user_fields.php */ - 'original_user_id_name' => $user_field, + 'original_user_id_name' => $user_field, // third party user id - 'original_user_id_value' => $random_user_id, - 'secret_key' => $secret_key, + 'original_user_id_value' => $random_user_id, + 'secret_key' => $secret_key, // Extra fields - 'extra' => array( - array('field_name' => 'ruc', 'field_value' => '123'), - array('field_name' => 'DNI', 'field_value' => '4200000') - ), -); + 'extra' => [ + ['field_name' => 'ruc', 'field_value' => '123'], + ['field_name' => 'DNI', 'field_value' => '4200000'], + ], +]; //1. Create user webservice $user_id = $client->call( 'WSCreateUserPasswordCrypted', - array('createUserPasswordCrypted' => $params) + ['createUserPasswordCrypted' => $params] ); // Check for an error @@ -94,7 +95,6 @@ if ($err) { echo '

Constructor error

'.$err.'
'; } - $sessionValueRandom = uniqid(); $params = [ @@ -119,9 +119,9 @@ $params = [ 'secret_key' => $secret_key, ]; -$user_id = $client->call( +$sessionId = $client->call( 'WSCreateSession', - array('createSession' => $params) + ['createSession' => $params] ); @@ -143,28 +143,25 @@ $data = [ $result = $client->call( 'WSSuscribeUsersToSession', - array('subscribeUsersToSession' => $data) + ['subscribeUsersToSession' => $data] ); $err = $client->getError(); var_dump($result); var_dump($err); - - - +var_dump($user_id); if (!empty($user_id) && is_numeric($user_id)) { - // 2. Get user info of the new user echo '

Trying to create an user via webservices

'; $original_params = $params; - $params = array( - 'original_user_id_value' => $random_user_id, // third party user id - 'original_user_id_name' => $user_field, // the system field in the user profile (See Profiling) - 'secret_key' => $secret_key - ); + $params = [ + 'original_user_id_value' => $random_user_id, // third party user id + 'original_user_id_name' => $user_field, // the system field in the user profile (See Profiling) + 'secret_key' => $secret_key, + ]; - $result = $client->call('WSGetUser', array('GetUser' => $params)); + $result = $client->call('WSGetUser', ['GetUser' => $params]); if ($result) { echo "Random user was created user_id: $user_id

"; @@ -175,27 +172,30 @@ if (!empty($user_id) && is_numeric($user_id)) { echo $result; } - //3. Updating user info - - $params = array( - 'firstname' => 'Jon edited', - 'lastname' => 'Brion edited', - 'status' => '5', // STUDENT - 'email' => 'jon@example.com', - 'username' => $generate_user_name, - 'password' => $generate_password, // encrypted using sha1 - 'encrypt_method' => 'sha1', - 'phone' => '00000000', - 'expiration_date' => '0000-00-00', - '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 - 'original_user_id_value' => $random_user_id, // third party user id - 'secret_key' => $secret_key, - 'extra' => array( - array('field_name' => 'ruc', 'field_value' => '666 edited'), - array('field_name' => 'DNI', 'field_value' => '888 edited') - ), - ); - $result = $client->call('WSEditUserPasswordCrypted', array('editUserPasswordCrypted' => $params)); + // 3. Updating user info + $params = [ + 'firstname' => 'Jon edited', + 'lastname' => 'Brion edited', + 'status' => '5', + // STUDENT + 'email' => 'jon@example.com', + 'username' => $generate_user_name, + 'password' => $generate_password, + // encrypted using sha1 + 'encrypt_method' => 'sha1', + 'phone' => '00000000', + 'expiration_date' => '0000-00-00', + '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 + 'original_user_id_value' => $random_user_id, + // third party user id + 'secret_key' => $secret_key, + 'extra' => [ + ['field_name' => 'ruc', 'field_value' => '666 edited'], + ['field_name' => 'DNI', 'field_value' => '888 edited'], + ], + ]; + $result = $client->call('WSEditUserPasswordCrypted', ['editUserPasswordCrypted' => $params]); if ($result) { echo "Random user was update user_id: $user_id

"; @@ -208,53 +208,51 @@ if (!empty($user_id) && is_numeric($user_id)) { var_dump($err); } - $params = array( - 'ids' => array( - array( + $params = [ + 'ids' => [ + [ 'original_user_id_name' => $user_field, 'original_user_id_value' => $random_user_id - ) - ), + ] + ], 'secret_key' => $secret_key - ); - - //Disable user - $result = $client->call('WSDisableUsers', array('user_ids' => $params)); - - //Enable user - $result = $client->call('WSEnableUsers', array('user_ids' => $params)); + ]; + // Disable user + $result = $client->call('WSDisableUsers', ['user_ids' => $params]); - //4 Creating course TEST123 + // Enable user + $result = $client->call('WSEnableUsers', ['user_ids' => $params]); - $params = array( - 'courses' => array( - array( - 'title' => 'PRUEBA', //Chamilo string course code - 'category_code' => 'LANG', - 'wanted_code' => '', + // 4 Creating course TEST123 + $params = [ + 'courses' => [ + [ + 'title' => 'PRUEBA', //Chamilo string course code + 'category_code' => 'LANG', + 'wanted_code' => '', 'course_language' => 'english', 'original_course_id_name' => 'course_id_test', 'original_course_id_value' => '666', - ) - ), - 'secret_key'=> $secret_key, - ); + ], + ], + 'secret_key' => $secret_key, + ]; - $result = $client->call('WSCreateCourse', array('createCourse' => $params)); + $result = $client->call('WSCreateCourse', ['createCourse' => $params]); - //5 .Adding user to the course TEST. The course TEST must be created manually in Chamilo + // 5 .Adding user to the course TEST. The course TEST must be created manually in Chamilo echo '

Trying to add user to a course called TEST via webservices

'; $course_info = api_get_course_info('TEST123'); if (!empty($course_info)) { - $params = array( - 'course' => 'TEST', //Chamilo string course code - 'user_id' => $user_id, - 'secret_key' => $secret_key - ); - $result = $client->call('WSSubscribeUserToCourseSimple', array('subscribeUserToCourseSimple' => $params)); + $params = [ + 'course' => 'TEST', //Chamilo string course code + 'user_id' => $user_id, + 'secret_key' => $secret_key, + ]; + $result = $client->call('WSSubscribeUserToCourseSimple', ['subscribeUserToCourseSimple' => $params]); } else { echo 'Course TEST does not exists please create one course with code "TEST"'; } @@ -265,55 +263,62 @@ if (!empty($user_id) && is_numeric($user_id)) { echo $result; } - //4. Adding course Test to the Session Session1 - - $course_id_list = array( - array('course_code' => 'TEST1'), - array('course_code' => 'TEST2'), - ); - $params = array( - 'coursessessions' => array( - array( + // 4. Adding course Test to the Session Session1 + $course_id_list = [ + ['course_code' => 'TEST1'], + ['course_code' => 'TEST2'], + ]; + $params = [ + 'coursessessions' => [ + [ 'original_course_id_values' => $course_id_list, 'original_course_id_name' => 'course_id_name', 'original_session_id_value' => '1', 'original_session_id_name' => 'session_id_value', - ), - ), + ], + ], 'secret_key' => $secret_key, - ); + ]; //$result = $client->call('WSSuscribeCoursesToSession', array('subscribeCoursesToSession' => $params)); - - // ------------------------ //Calling the WSSubscribeUserToCourse - - $course_array = array( + $course_array = [ 'original_course_id_name' => 'TEST', 'original_course_id_value' => 'TEST', - ); + ]; - $user_array = array( - 'original_user_id_value' => $user_id, - 'original_user_id_name' => 'name', - ); - $user_courses = array(); + $user_array = [ + 'original_user_id_value' => $random_user_id, + 'original_user_id_name' => $user_field, + ]; + $user_courses = []; - $user_courses[] = array( + $user_courses[] = [ 'course_id' => $course_array, 'user_id' => $user_array, - 'status' => '1', - ); + 'status' => '1' + ]; - $params = array( + $params = [ 'userscourses' => $user_courses, 'secret_key' => $secret_key, - ); + ]; - $result = $client->call('WSSubscribeUserToCourse', array('subscribeUserToCourse' => $params)); + $result = $client->call('WSSubscribeUserToCourse', ['subscribeUserToCourse' => $params]); var_dump($result); + $params = [ + 'secret_key' => $secret_key, + 'ids' => [ + 'original_user_id_value' => $random_user_id, + 'original_user_id_name' => $user_field, + ] + ]; + + // Delete user + $result = $client->call('WSDeleteUsers', ['user_ids' => $params]); + exit; } else { echo 'User was not created, activate the debug=true in the registration.soap.php file and see the error logs'; } @@ -326,51 +331,41 @@ if ($err) { echo '

Constructor error

'.$err.'
'; } - //1. Create user webservice $result = $client->call( 'WSGetPortals', - array('getPortals' => ['secret_key' => $secret_key]) + ['getPortals' => ['secret_key' => $secret_key]] ); $result = $client->call( 'WSAddUserToPortal', - array('addUserToPortal' => ['user_id' => 1, 'portal_id'=> 1, 'secret_key' => $secret_key]) + ['addUserToPortal' => ['user_id' => 1, 'portal_id' => 1, 'secret_key' => $secret_key]] ); $result = $client->call( 'WSGetPortalListFromUser', - array('getPortalListFromUser' => ['user_id' => 1, 'secret_key' => $secret_key]) + ['getPortalListFromUser' => ['user_id' => 1, 'secret_key' => $secret_key]] ); $result = $client->call( 'WSGetPortalListFromCourse', - array('getPortalListFromCourse' => ['course_id' => 20, 'secret_key' => $secret_key]) + ['getPortalListFromCourse' => ['course_id' => 20, 'secret_key' => $secret_key]] ); $result = $client->call( 'WSAddCourseToPortal', - array('addCourseToPortal' => ['course_id' => 20, 'portal_id' => 1, 'secret_key' => $secret_key]) + ['addCourseToPortal' => ['course_id' => 20, 'portal_id' => 1, 'secret_key' => $secret_key]] ); - $result = $client->call( 'WSRemoveUserFromPortal', - array('removeUserFromPortal' => ['course_id' => 20, 'portal_id'=> 1, 'secret_key' => $secret_key]) + ['removeUserFromPortal' => ['course_id' => 20, 'portal_id' => 1, 'secret_key' => $secret_key]] ); - - var_dump($user_id); exit; - - - - - - if ($client->fault) { echo '

Fault

';
     print_r($result);
diff --git a/main/webservices/registration.soap.php b/main/webservices/registration.soap.php
index 4cb4642e43..068a058e89 100755
--- a/main/webservices/registration.soap.php
+++ b/main/webservices/registration.soap.php
@@ -1215,21 +1215,21 @@ $server->wsdl->addComplexType(
     'all',
     '',
     array(
-        'firstname'                 => array('name' => 'firstname', 'type' => 'xsd:string'),
-        'lastname'                  => array('name' => 'lastname', 'type' => 'xsd:string'),
-        'status'                    => array('name' => 'status', 'type' => 'xsd:string'),
-        'email'                     => array('name' => 'email', 'type' => 'xsd:string'),
-        'loginname'                 => array('name' => 'loginname', 'type' => 'xsd:string'),
-        'password'                  => array('name' => 'password', 'type' => 'xsd:string'), //encripted password using the encrypt_method
-        'encrypt_method'            => array('name' => 'encrypt_method', 'type' => 'xsd:string'),
-        'language'                  => array('name' => 'language', 'type' => 'xsd:string'),
-        'phone'                     => array('name' => 'phone', 'type' => 'xsd:string'),
-        'expiration_date'           => array('name' => 'expiration_date', 'type' => 'xsd:string'),
-        'official_code'             => array('name' => 'official_code', 'type' => 'xsd:string'),
-        'original_user_id_name'     => array('name' => 'original_user_id_name', 'type' => 'xsd:string'),
-        'original_user_id_value'    => array('name' => 'original_user_id_value', 'type' => 'xsd:string'),
-        'extra'                     => array('name' => 'extra', 'type' => 'tns:extrasList'),
-        'secret_key'                => array('name' => 'secret_key', 'type' => 'xsd:string')
+        'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),
+        'lastname' => array('name' => 'lastname', 'type' => 'xsd:string'),
+        'status' => array('name' => 'status', 'type' => 'xsd:string'),
+        'email' => array('name' => 'email', 'type' => 'xsd:string'),
+        'loginname' => array('name' => 'loginname', 'type' => 'xsd:string'),
+        'password' => array('name' => 'password', 'type' => 'xsd:string'), //encripted password using the encrypt_method
+        'encrypt_method' => array('name' => 'encrypt_method', 'type' => 'xsd:string'),
+        'language' => array('name' => 'language', 'type' => 'xsd:string'),
+        'phone' => array('name' => 'phone', 'type' => 'xsd:string'),
+        'expiration_date' => array('name' => 'expiration_date', 'type' => 'xsd:string'),
+        'official_code' => array('name' => 'official_code', 'type' => 'xsd:string'),
+        'original_user_id_name' => array('name' => 'original_user_id_name', 'type' => 'xsd:string'),
+        'original_user_id_value' => array('name' => 'original_user_id_value', 'type' => 'xsd:string'),
+        'extra' => array('name' => 'extra', 'type' => 'tns:extrasList'),
+        'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
     )
 );
 
@@ -1250,9 +1250,10 @@ function WSCreateUserPasswordCrypted($params)
 {
     global $_configuration, $debug;
     $debug = 1;
-    if ($debug) error_log('WSCreateUserPasswordCrypted');
-    if ($debug) error_log(print_r($params, 1));
-
+    if ($debug) {
+        error_log('WSCreateUserPasswordCrypted');
+        error_log(print_r($params, 1));
+    }
     if (!WSHelperVerifyKey($params)) {
         return returnError(WS_ERROR_SECRET_KEY);
     }
@@ -1371,7 +1372,11 @@ function WSCreateUserPasswordCrypted($params)
             return 0;
         }
     } else {
-        if ($debug) error_log("User not found with original_id = $original_user_id_value and original_name = $original_user_id_name");
+        if ($debug) {
+            error_log(
+                "User not found with original_id = $original_user_id_value and original_name = $original_user_id_name"
+            );
+        }
     }
 
     // Default language.
@@ -1383,12 +1388,16 @@ function WSCreateUserPasswordCrypted($params)
 
     // First check wether the login already exists
     if (!UserManager::is_username_available($loginName)) {
-        if ($debug) error_log("Username $loginName is not available");
+        if ($debug) {
+            error_log("Username $loginName is not available");
+        }
         return 0;
     }
 
     $queryExpirationDate = '';
-    if (!empty($params['expiration_date'])) $queryExpirationDate = "expiration_date     = '".Database::escape_string($expiration_date)."', ";
+    if (!empty($params['expiration_date'])) {
+        $queryExpirationDate = "expiration_date     = '".Database::escape_string($expiration_date)."', ";
+    }
 
     $sql = "INSERT INTO $table_user SET
             lastname            = '".Database::escape_string(trim($lastName))."',
@@ -1413,12 +1422,17 @@ function WSCreateUserPasswordCrypted($params)
     Database::query($sql);
     $return = Database::insert_id();
     if ($return) {
+        if ($debug) {
+            error_log("New user created. user_id = $return");
+        }
         $sql = "UPDATE $table_user SET user_id = id WHERE id = $return";
         Database::query($sql);
 
         $url_id = api_get_current_access_url_id();
         UrlManager::add_user_to_url($return, $url_id);
-        if ($debug) error_log("Adding user_id = $return to URL id $url_id ");
+        if ($debug) {
+            error_log("Adding user_id = $return to URL id $url_id ");
+        }
 
         // Create extra field for the original_user_id_name
         UserManager::create_extra_field(
@@ -1437,8 +1451,8 @@ function WSCreateUserPasswordCrypted($params)
         // Create extra fields
         if (is_array($extra_list) && count($extra_list) > 0) {
             foreach ($extra_list as $extra) {
-                $extra_field_name   = $extra['field_name'];
-                $extra_field_value  = $extra['field_value'];
+                $extra_field_name = $extra['field_name'];
+                $extra_field_value = $extra['field_value'];
                 // save new fieldlabel into user_field table
                 UserManager::create_extra_field(
                     $extra_field_name,
@@ -1455,11 +1469,15 @@ function WSCreateUserPasswordCrypted($params)
             }
         }
     } else {
-        if ($debug) error_log('Error while inserting a user');
+        if ($debug) {
+            error_log('Error while inserting a user');
+        }
 
         return 0;
     }
-
+    if ($debug) {
+        error_log("Return value: $return");
+    }
     return $return;
 }
 
@@ -2666,6 +2684,12 @@ $server->wsdl->addComplexType(
 
 function WSHelperActionOnUsers($params, $type)
 {
+    $debug = 1;
+    if ($debug) {
+        error_log("WSHelperActionOnUsers");
+        error_log(print_r($params, 1));
+    }
+
     if (!WSHelperVerifyKey($params)) {
         return returnError(WS_ERROR_SECRET_KEY);
     }
@@ -2681,18 +2705,24 @@ function WSHelperActionOnUsers($params, $type)
             $original_user_id['original_user_id_name']
         );
         if ($user_id > 0) {
-            if ($type == "delete") {
+            if ($debug) {
+                error_log("User found: $user_id");
+            }
+            if ($type == 'delete') {
                 $result = UserManager::delete_user($user_id);
-            } else if ($type == "disable") {
+            } elseif ($type == "disable") {
                 $result = UserManager::disable($user_id);
-            } else if ($type == "enable") {
+            } elseif ($type == "enable") {
                 $result = UserManager::enable($user_id);
             }
+        } else {
+            if ($debug) {
+                error_log("User id not found: $user_id");
+            }
         }
         $results[] = $result ? 1 : 0;
     }
 
-
     $count_results = count($results);
     $output = array();
     for ($i = 0; $i < $count_results; $i++) {
@@ -2718,7 +2748,7 @@ $server->register(
 
 function WSDeleteUsers($params)
 {
-    return WSHelperActionOnUsers($params, "delete");
+    return WSHelperActionOnUsers($params, 'delete');
 }
 
 /** WSDisableUsers **/
@@ -4534,7 +4564,9 @@ function WSSubscribeUserToCourse($params)
     if (!WSHelperVerifyKey($params)) {
         return returnError(WS_ERROR_SECRET_KEY);
     }
-    if ($debug) error_log('WSSubscribeUserToCourse params: '.print_r($params, 1));
+    if ($debug) {
+        error_log('WSSubscribeUserToCourse params: '.print_r($params, 1));
+    }
 
     $results = array();
     $userscourses = $params['userscourses'];
@@ -4553,7 +4585,9 @@ function WSSubscribeUserToCourse($params)
             $original_user_id['original_user_id_value'],
             $original_user_id['original_user_id_name']
         );
-        if ($debug) error_log('WSSubscribeUserToCourse user_id: '.$user_id);
+        if ($debug) {
+            error_log('WSSubscribeUserToCourse user_id: '.$user_id);
+        }
 
         if ($user_id == 0) {
             // If user was not found, there was a problem
@@ -4565,7 +4599,7 @@ function WSSubscribeUserToCourse($params)
                 $original_course_id['original_course_id_name']
             );
 
-            $courseCode = $courseInfo['code'];
+            $courseCode = isset($courseInfo['code']) ? $courseInfo['code'] : '';
 
             if (empty($courseCode)) {
                 // Course was not found