Webservices: Adding error logs when adding user to a course see BT#4158

skala
Julio Montoya 14 years ago
parent d7e5000dfa
commit e6bcf38492
  1. 221
      main/webservices/registration.soap.php

@ -172,8 +172,6 @@ function WSCreateUsers($params) {
// database table definition // database table definition
$table_user = Database::get_main_table(TABLE_MAIN_USER); $table_user = Database::get_main_table(TABLE_MAIN_USER);
$t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD);
$t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
$users_params = $params['users']; $users_params = $params['users'];
$results = array(); $results = array();
@ -373,18 +371,15 @@ $server->register('WSCreateUser', // method name
// Define the method WSCreateUser // Define the method WSCreateUser
function WSCreateUser($params) { function WSCreateUser($params) {
global $_user, $_configuration, $debug;
global $_user, $_configuration; if (!WSHelperVerifyKey($params)) {
if(!WSHelperVerifyKey($params)) {
return return_error(WS_ERROR_SECRET_KEY); return return_error(WS_ERROR_SECRET_KEY);
} }
// database table definition // database table definition
$table_user = Database::get_main_table(TABLE_MAIN_USER); $table_user = Database::get_main_table(TABLE_MAIN_USER);
$t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD);
$t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
$firstName = $params['firstname']; $firstName = $params['firstname'];
$lastName = $params['lastname']; $lastName = $params['lastname'];
$status = $params['status']; $status = $params['status'];
@ -459,30 +454,34 @@ function WSCreateUser($params) {
// First check wether the login already exists // First check wether the login already exists
if (!UserManager::is_username_available($loginName)) { if (!UserManager::is_username_available($loginName)) {
if(api_set_failure('login-pass already taken')) { if ($debug) error_log("Username $loginName is not available");
return 0;
/*
if (api_set_failure('login-pass already taken')) {
return 0; return 0;
} }*/
} }
$password = ($_configuration['password_encryption'] ? api_get_encrypted_password($password) : $password); $password = ($_configuration['password_encryption'] ? api_get_encrypted_password($password) : $password);
$sql = "INSERT INTO $table_user $sql = "INSERT INTO $table_user SET
SET lastname = '".Database::escape_string(trim($lastName))."', lastname = '".Database::escape_string(trim($lastName))."',
firstname = '".Database::escape_string(trim($firstName))."', firstname = '".Database::escape_string(trim($firstName))."',
username = '".Database::escape_string(trim($loginName))."', username = '".Database::escape_string(trim($loginName))."',
status = '".Database::escape_string($status)."', status = '".Database::escape_string($status)."',
password = '".Database::escape_string($password)."', password = '".Database::escape_string($password)."',
email = '".Database::escape_string($email)."', email = '".Database::escape_string($email)."',
official_code = '".Database::escape_string($official_code)."', official_code = '".Database::escape_string($official_code)."',
picture_uri = '".Database::escape_string($picture_uri)."', picture_uri = '".Database::escape_string($picture_uri)."',
creator_id = '".Database::escape_string($creator_id)."', creator_id = '".Database::escape_string($creator_id)."',
auth_source = '".Database::escape_string($auth_source)."', auth_source = '".Database::escape_string($auth_source)."',
phone = '".Database::escape_string($phone)."', phone = '".Database::escape_string($phone)."',
language = '".Database::escape_string($language)."', language = '".Database::escape_string($language)."',
registration_date = now(), registration_date = now(),
expiration_date = '".Database::escape_string($expiration_date)."', expiration_date = '".Database::escape_string($expiration_date)."',
hr_dept_id = '".Database::escape_string($hr_dept_id)."', hr_dept_id = '".Database::escape_string($hr_dept_id)."',
active = '".Database::escape_string($active)."'"; active = '".Database::escape_string($active)."'";
$result = Database::query($sql); $result = Database::query($sql);
if ($result) { if ($result) {
//echo "id returned"; //echo "id returned";
$return = Database::insert_id(); $return = Database::insert_id();
@ -831,7 +830,7 @@ $server->wsdl->addComplexType(
'status' => array('name' => 'status', 'type' => 'xsd:string'), 'status' => array('name' => 'status', 'type' => 'xsd:string'),
'email' => array('name' => 'email', 'type' => 'xsd:string'), 'email' => array('name' => 'email', 'type' => 'xsd:string'),
'loginname' => array('name' => 'loginname', 'type' => 'xsd:string'), 'loginname' => array('name' => 'loginname', 'type' => 'xsd:string'),
'password' => array('name' => 'password', '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'), 'encrypt_method' => array('name' => 'encrypt_method', 'type' => 'xsd:string'),
'language' => array('name' => 'language', 'type' => 'xsd:string'), 'language' => array('name' => 'language', 'type' => 'xsd:string'),
'phone' => array('name' => 'phone', 'type' => 'xsd:string'), 'phone' => array('name' => 'phone', 'type' => 'xsd:string'),
@ -856,20 +855,17 @@ $server->register('WSCreateUserPasswordCrypted', // method name
// Define the method WSCreateUserPasswordCrypted // Define the method WSCreateUserPasswordCrypted
function WSCreateUserPasswordCrypted($params) { function WSCreateUserPasswordCrypted($params) {
global $_user, $_configuration, $debug; global $_user, $_configuration, $debug;
error_log('WSCreateUserPasswordCrypted'); //$debug = true;
error_log(print_r($params,1)); if ($debug) error_log('WSCreateUserPasswordCrypted');
if ($debug) error_log(print_r($params,1));
if (!WSHelperVerifyKey($params)) { if (!WSHelperVerifyKey($params)) {
return return_error(WS_ERROR_SECRET_KEY); return return_error(WS_ERROR_SECRET_KEY);
} }
// Database table definition. // Database table definition.
$table_user = Database::get_main_table(TABLE_MAIN_USER); $table_user = Database::get_main_table(TABLE_MAIN_USER);
$t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD);
$t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
//$users_params = $params['users'];
$result = array(); $result = array();
$orig_user_id_value = array(); $orig_user_id_value = array();
@ -890,19 +886,21 @@ function WSCreateUserPasswordCrypted($params) {
$original_user_id_value = $params['original_user_id_value']; $original_user_id_value = $params['original_user_id_value'];
$orig_user_id_value[] = $params['original_user_id_value']; $orig_user_id_value[] = $params['original_user_id_value'];
$extra_list = $params['extra']; $extra_list = $params['extra'];
$salt = '';
if ($_configuration['password_encryption'] === $encrypt_method ) { if ($_configuration['password_encryption'] === $encrypt_method ) {
if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) { if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) {
$msg = "Encryption $encrypt_method is invalid"; $msg = "Encryption $encrypt_method is invalid";
if ($debug) error_log($msg);
return $msg; return $msg;
} else if ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) { } else if ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) {
$msg = "Encryption $encrypt_method is invalid"; $msg = "Encryption $encrypt_method is invalid";
if ($debug) error_log($msg);
return $msg; return $msg;
} }
} else { } else {
$msg = "This encryption $encrypt_method is not configured"; $msg = "This encryption $encrypt_method is not configured";
if ($debug) error_log($msg);
return $msg; return $msg;
} }
@ -912,17 +910,22 @@ function WSCreateUserPasswordCrypted($params) {
// Check whether x_user_id exists into user_field_values table. // Check whether x_user_id exists into user_field_values table.
$user_id = UserManager::get_user_id_from_original_id($original_user_id_value, $original_user_id_name); $user_id = UserManager::get_user_id_from_original_id($original_user_id_value, $original_user_id_name);
if ($debug) error_log('Ready to create user'); if ($debug) error_log('Ready to create user');
if ($user_id > 0) {
if ($user_id > 0) {
if ($debug) error_log('User found'); if ($debug) error_log('User found');
// Check whether user is not active. // Check whether user is not active
$sql = "SELECT user_id FROM $table_user WHERE user_id ='".$user_id."' AND active= '0'"; //@todo why this condition exists??
$sql = "SELECT user_id FROM $table_user WHERE user_id ='".$user_id."' AND active= '0' ";
$resu = Database::query($sql); $resu = Database::query($sql);
$r_check_user = Database::fetch_row($resu); $r_check_user = Database::fetch_row($resu);
$count_check_user = Database::num_rows($resu); $count_check_user = Database::num_rows($resu);
if ($count_check_user > 0) { if ($count_check_user > 0) {
if ($debug) error_log('User id: '.$user_id.' exists and is NOT active. Updating user and setting setting active = 1');
$sql = "UPDATE $table_user SET $sql = "UPDATE $table_user SET
lastname='".Database::escape_string($lastName)."', lastname='".Database::escape_string($lastName)."',
firstname='".Database::escape_string($firstName)."', firstname='".Database::escape_string($firstName)."',
@ -954,9 +957,11 @@ function WSCreateUserPasswordCrypted($params) {
} }
return $r_check_user[0]; return $r_check_user[0];
} else { } else {
if ($debug) error_log('User is already active'); if ($debug) error_log('User exists but is active. Cant be updated');
return 0; return 0;
} }
} else {
if ($debug) error_log("User not found with original_id = $original_user_id_value and original_name = $original_user_id_name");
} }
// Default language. // Default language.
@ -969,47 +974,48 @@ function WSCreateUserPasswordCrypted($params) {
} else { } else {
$creator_id = ''; $creator_id = '';
} }
// First check wether the login already exists // First check wether the login already exists
if (! UserManager::is_username_available($loginName)) { if (!UserManager::is_username_available($loginName)) {
if(api_set_failure('login-pass already taken')) { if ($debug) error_log("Username $loginName is not available");
return 0;
/*if (api_set_failure('login-pass already taken')) {
if ($debug) error_log('login-pass already taken'); if ($debug) error_log('login-pass already taken');
return 0; return 0;
} }*/
} }
$sql = "INSERT INTO $table_user $sql = "INSERT INTO $table_user SET
SET lastname = '".Database::escape_string(trim($lastName))."', lastname = '".Database::escape_string(trim($lastName))."',
firstname = '".Database::escape_string(trim($firstName))."', firstname = '".Database::escape_string(trim($firstName))."',
username = '".Database::escape_string(trim($loginName))."', username = '".Database::escape_string(trim($loginName))."',
status = '".Database::escape_string($status)."', status = '".Database::escape_string($status)."',
password = '".Database::escape_string($password)."', password = '".Database::escape_string($password)."',
email = '".Database::escape_string($email)."', email = '".Database::escape_string($email)."',
official_code = '".Database::escape_string($official_code)."', official_code = '".Database::escape_string($official_code)."',
picture_uri = '".Database::escape_string($picture_uri)."', picture_uri = '".Database::escape_string($picture_uri)."',
creator_id = '".Database::escape_string($creator_id)."', creator_id = '".Database::escape_string($creator_id)."',
auth_source = '".Database::escape_string($auth_source)."', auth_source = '".Database::escape_string($auth_source)."',
phone = '".Database::escape_string($phone)."', phone = '".Database::escape_string($phone)."',
language = '".Database::escape_string($language)."', language = '".Database::escape_string($language)."',
registration_date = now(), registration_date = '".api_get_utc_datetime()."',
expiration_date = '".Database::escape_string($expiration_date)."', expiration_date = '".Database::escape_string($expiration_date)."',
hr_dept_id = '".Database::escape_string($hr_dept_id)."', hr_dept_id = '".Database::escape_string($hr_dept_id)."',
active = '".Database::escape_string($active)."'"; active = '".Database::escape_string($active)."'";
if ($debug) error_log($sql); if ($debug) error_log($sql);
$result = Database::query($sql); $result = Database::query($sql);
if ($result) { if ($result) {
//echo "id returned"; //echo "id returned";
$return = Database::insert_id(); $return = Database::insert_id();
//Multiple URL
require_once api_get_path(LIBRARY_PATH).'urlmanager.lib.php'; require_once api_get_path(LIBRARY_PATH).'urlmanager.lib.php';
if ($_configuration['multiple_access_urls']) { $url_id = api_get_current_access_url_id();
if (api_get_current_access_url_id() != -1) { UrlManager::add_user_to_url($return, $url_id);
UrlManager::add_user_to_url($return, api_get_current_access_url_id()); if ($debug) error_log("Adding user_id = $return to URL id $url_id ");
} else {
UrlManager::add_user_to_url($return, 1);
}
} else {
// We add by default the access_url_user table with access_url_id = 1
UrlManager::add_user_to_url($return, 1);
}
// Save new fieldlabel into user_field table. // Save new fieldlabel into user_field table.
$field_id = UserManager::create_extra_field($original_user_id_name, 1, $original_user_id_name, ''); $field_id = UserManager::create_extra_field($original_user_id_name, 1, $original_user_id_name, '');
// Save the remote system's id into user_field_value table. // Save the remote system's id into user_field_value table.
@ -1017,8 +1023,8 @@ function WSCreateUserPasswordCrypted($params) {
if (is_array($extra_list) && count($extra_list) > 0) { if (is_array($extra_list) && count($extra_list) > 0) {
foreach ($extra_list as $extra) { foreach ($extra_list as $extra) {
$extra_field_name = $extra['field_name']; $extra_field_name = $extra['field_name'];
$extra_field_value = $extra['field_value']; $extra_field_value = $extra['field_value'];
// save new fieldlabel into user_field table // save new fieldlabel into user_field table
$field_id = UserManager::create_extra_field($extra_field_name, 1, $extra_field_name, ''); $field_id = UserManager::create_extra_field($extra_field_name, 1, $extra_field_name, '');
// save the external system's id into user_field_value table' // save the external system's id into user_field_value table'
@ -1026,6 +1032,8 @@ function WSCreateUserPasswordCrypted($params) {
} }
} }
} else { } else {
$error = Database::error();
if ($debug) error_log($error);
return 0; return 0;
} }
return $return; return $return;
@ -3547,8 +3555,8 @@ $server->wsdl->addComplexType(
'all', 'all',
'', '',
array( array(
'original_user_id_value' => array('name' => 'original_user_id_value', 'type' => 'xsd:string'), 'original_user_id_value' => array('name' => 'original_user_id_value', 'type' => 'xsd:string'),
'original_course_id_value' => array('name' => 'original_course_id_value', 'type' => 'xsd:string'), 'original_course_id_value' => array('name' => 'original_course_id_value', 'type' => 'xsd:string'),
'result' => array('name' => 'result', 'type' => 'xsd:int') 'result' => array('name' => 'result', 'type' => 'xsd:int')
) )
); );
@ -3568,50 +3576,48 @@ $server->register('WSSubscribeUserToCourse', // method name
// define the method WSSubscribeUserToCourse // define the method WSSubscribeUserToCourse
function WSSubscribeUserToCourse($params) { function WSSubscribeUserToCourse($params) {
if(!WSHelperVerifyKey($params)) { if (!WSHelperVerifyKey($params)) {
return return_error(WS_ERROR_SECRET_KEY); return return_error(WS_ERROR_SECRET_KEY);
} }
$results = array(); $results = array();
$userscourses = $params['userscourses']; $userscourses = $params['userscourses'];
foreach($userscourses as $usercourse) { foreach ($userscourses as $usercourse) {
$original_course_id = $usercourse['course_id']; $original_course_id = $usercourse['course_id'];
$original_user_id = $usercourse['user_id']; $original_user_id = $usercourse['user_id'];
$status = STUDENT; $status = STUDENT;
if($usercourse['status']) { if ($usercourse['status']) {
$status = $usercourse['status']; $status = $usercourse['status'];
} }
$result = array( $result = array(
'original_user_id_value' => $original_user_id['original_user_id_value'], 'original_user_id_value' => $original_user_id['original_user_id_value'],
'original_course_id_value' => $original_course_id['original_course_id_value'], 'original_course_id_value' => $original_course_id['original_course_id_value'],
'result' => 1); 'result' => 1);
// Get user id // Get user id
$user_id = UserManager::get_user_id_from_original_id($original_user_id['original_user_id_value'], $original_user_id['original_user_id_name']); $user_id = UserManager::get_user_id_from_original_id($original_user_id['original_user_id_value'], $original_user_id['original_user_id_name']);
if($user_id == 0) { if ($user_id == 0) {
// If user was not found, there was a problem // If user was not found, there was a problem
$result['result'] = 0; $result['result'] = 0;
} else { } else {
// User was found // User was found
$course_id = CourseManager::get_course_id_from_original_id($original_course_id['original_course_id_value'], $original_course_id['original_course_id_name']); $course_id = CourseManager::get_course_id_from_original_id($original_course_id['original_course_id_value'], $original_course_id['original_course_id_name']);
if($course_id == 0) { if ($course_id == 0) {
// Course was not found // Course was not found
$result['result'] = 0; $result['result'] = 0;
} else { } else {
$course_code = CourseManager::get_course_code_from_course_id($course_id); $course_code = CourseManager::get_course_code_from_course_id($course_id);
if (!CourseManager::add_user_to_course($user_id, $course_code, $status)) { if (!CourseManager::add_user_to_course($user_id, $course_code, $status)) {
$result['result'] = 0; $result['result'] = 0;
}
} }
} }
}
$results[] = $result; $results[] = $result;
} }
return $results; return $results;
} }
/** WSSubscribeUserToCourse **/ /** WSSubscribeUserToCourse **/
// Register the data structures used by the service // Register the data structures used by the service
$server->wsdl->addComplexType( $server->wsdl->addComplexType(
@ -3621,8 +3627,8 @@ $server->wsdl->addComplexType(
'all', 'all',
'', '',
array( array(
'course' => array('name' => 'course', 'type' => 'xsd:string'), 'course' => array('name' => 'course', 'type' => 'xsd:string'), //Course string code
'user_id' => array('name' => 'user_id', 'type' => 'xsd:string'), 'user_id' => array('name' => 'user_id', 'type' => 'xsd:string'), //Chamilo user_id
'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string') 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string')
) )
); );
@ -3655,37 +3661,40 @@ $server->register('WSSubscribeUserToCourseSimple', // method
// define the method WSSubscribeUserToCourse // define the method WSSubscribeUserToCourse
function WSSubscribeUserToCourseSimple($params) { function WSSubscribeUserToCourseSimple($params) {
global $debug; global $debug;
$debug = true;
if ($debug) error_log('WSSubscribeUserToCourseSimple'); if ($debug) error_log('WSSubscribeUserToCourseSimple');
if ($debug) error_log('Params '. print_r($params, 1)); if ($debug) error_log('Params '. print_r($params, 1));
if(!WSHelperVerifyKey($params)) { if (!WSHelperVerifyKey($params)) {
return return_error(WS_ERROR_SECRET_KEY); return return_error(WS_ERROR_SECRET_KEY);
} }
$results = array(); $result = array();
$course_code = $params['course']; $course_code = $params['course']; //Course code
$user_id = $params['user_id']; $user_id = $params['user_id']; //chamilo user id
$status = STUDENT; $status = STUDENT;
// Get user id // Get user id
$user_data = UserManager::get_user_info_by_id($user_id); $user_data = UserManager::get_user_info_by_id($user_id);
if(empty($user_data)) {
if ($debug) error_log('User does not exist'); if (empty($user_data)) {
// If user was not found, there was a problem // If user was not found, there was a problem
$result = 'User does not exists'; $result = "User $user_id does not exist";
if ($debug) error_log($result);
return $result; return $result;
} }
if (!empty($course_code)) { if (!empty($course_code)) {
$course_data = CourseManager::get_course_information($course_code); $course_data = CourseManager::get_course_information($course_code);
if(empty($course_data)) { if (empty($course_data)) {
if ($debug) error_log('Course does not exist '.$course_code);
// Course was not found // Course was not found
$result = 'Course does not exist'; $result = "Course $course_code does not exist in the platform ";
if ($debug) error_log($result);
} else { } else {
if ($debug) error_log('Try to register: '.$user_id.' - '.$course_data['code']); if ($debug) error_log('Try to register: user_id= '.$user_id.' to course: '.$course_data['code']);
if (!CourseManager::add_user_to_course($user_id, $course_data['code'], $status)) { if (!CourseManager::add_user_to_course($user_id, $course_data['code'], $status)) {
if ($debug) error_log('User already registered'); $result = 'User was not registered possible reasons: User already registered to the course, Course visibility doesnt allow user subscriptions ';
$result = 'User already registered'; if ($debug) error_log($result);
} else { } else {
if ($debug) error_log('User registered to course'); if ($debug) error_log('User registered to the course: '.$course_data['code']);
$result = 1; $result = 1;
} }
} }

Loading…
Cancel
Save