Fix WSSuscribeUsersToSession

1.10.x
jmontoyaa 9 years ago
parent 576ea02eb2
commit 7b0919aa8b
  1. 76
      main/webservices/client_soap.php
  2. 162
      main/webservices/registration.soap.php

@ -19,7 +19,7 @@ $libpath = api_get_path(LIBRARY_PATH);
// Create the client instance
$url = api_get_path(WEB_CODE_PATH)."webservices/registration.soap.php?wsdl";
$url = api_get_path(WEB_CODE_PATH)."webservices/access_url.php?wsdl";
//$url = api_get_path(WEB_CODE_PATH)."webservices/access_url.php?wsdl";
global $_configuration;
// see the main/inc/configuration.php file to get this value
@ -36,16 +36,17 @@ if (!empty($soap_error)) {
$error_message = 'Nusoap object creation failed: ' . $soap_error;
throw new Exception($error_message);
}
$client->setDebugLevel(10000);
$client->debug_flag = true;
// This should be the IP address of the client
$ip_address = $_SERVER['SERVER_ADDR'];
$ip_address = "192.168.1.54";
$ip_address = "127.0.0.1";
//Secret key
//$secret_key = sha1($ip_address.$security_key);// Hash of the combination of IP Address + Chamilo security key
$secret_key = sha1($security_key);
$secret_key = sha1($ip_address.$security_key);// Hash of the combination of IP Address + Chamilo security key
//$secret_key = sha1($security_key);
//Creating a random user_id, this values need to be provided from your system
$random_user_id = rand(0, 1000);
@ -54,6 +55,7 @@ $generate_user_name = 'jbrion'.$random_user_id;
//Creating a password (the username)
$generate_password = sha1($generate_user_name);
$user_field = 'uid';
$sessionField = 'external_session_id';
$params = array(
'firstname' => 'Jon',
@ -86,6 +88,72 @@ $user_id = $client->call(
array('createUserPasswordCrypted' => $params)
);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
$sessionValueRandom = uniqid();
$params = [
'sessions' => [
[
'name' => 'session from ws: '.$sessionValueRandom,
'year_start' => '2015',
'month_start' => '10',
'day_start' => '1',
'year_end' => '',
'month_end' => '',
'day_end' => '',
'nb_days_access_before' => 0,
'nb_days_access_after' => 0,
'nolimit' => 1,
'user_id' => 1,
'original_session_id_name' => $sessionField,
'original_session_id_value' => $sessionValueRandom,
'extra' => ''
]
],
'secret_key' => $secret_key,
];
$user_id = $client->call(
'WSCreateSession',
array('createSession' => $params)
);
$data = [
'secret_key' => $secret_key,
'userssessions' => [
[
'original_user_id_name' => $user_field,
'original_session_id_value' => $sessionValueRandom,
'original_session_id_name' => $sessionField,
'original_user_id_values' => [
[
'original_user_id_value' => $random_user_id
]
]
],
],
];
$result = $client->call(
'WSSuscribeUsersToSession',
array('subscribeUsersToSession' => $data)
);
$err = $client->getError();
var_dump($result);
var_dump($err);
if (!empty($user_id) && is_numeric($user_id)) {
// 2. Get user info of the new user

@ -10,7 +10,7 @@ use Chamilo\UserBundle\Entity\User;
require_once '../inc/global.inc.php';
$libpath = api_get_path(LIBRARY_PATH);
$debug = false;
$debug = true;
define('WS_ERROR_SECRET_KEY', 1);
define('WS_ERROR_NOT_FOUND_RESULT', 2);
@ -133,7 +133,8 @@ $server->wsdl->addComplexType(
'',
'SOAP-ENC:Array',
array(),
array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType' => 'tns:extras[]')),'tns:extras'
array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType' => 'tns:extras[]')),
'tns:extras'
);
$server->wsdl->addComplexType(
@ -165,7 +166,8 @@ $server->wsdl->addComplexType(
'',
'SOAP-ENC:Array',
array(),
array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType' => 'tns:usersParams[]')),'tns:usersParams'
array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType' => 'tns:usersParams[]')),
'tns:usersParams'
);
$server->wsdl->addComplexType(
@ -3700,7 +3702,7 @@ $server->wsdl->addComplexType(
'SOAP-ENC:Array',
array(),
array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:createSessionParam[]')),
'tns:createSessionParam'
'tns:createSessionParamList'
);
// Register the data structures used by the service
@ -3763,6 +3765,7 @@ function WSCreateSession($params)
}
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
error_log(print_r($params, 1));
$sessions_params = $params['sessions'];
$results = array();
@ -3791,9 +3794,9 @@ function WSCreateSession($params)
$original_session_id_name
);
if (empty($sessionId)) {
if (!empty($sessionId)) {
if ($debug) {
error_log("session '$name' exists");
error_log("session with external session id '$original_session_id_value' with '$name' exists");
}
$results[] = 0;
continue;
@ -3840,21 +3843,29 @@ function WSCreateSession($params)
$results[] = 0;
continue;
} else {
$startDate = new DateTime($date_start);
$endDate = new DateTime($date_end);
$diffStart = new DateInterval($nb_days_access_before);
$diffEnd = new DateInterval($nb_days_access_after);
$coachStartDate = $startDate->sub($diffStart);
$coachEndDate = $endDate->add($diffEnd);
$coachStartDate = '';
if ($date_start) {
$startDate = new DateTime($date_start);
$diffStart = new DateInterval($nb_days_access_before);
$coachStartDate = $startDate->sub($diffStart);
$coachStartDate = $coachStartDate->format('Y-m-d H:i:s');
}
$coachEndDate = '';
if ($date_end) {
$endDate = new DateTime($date_end);
$diffEnd = new DateInterval($nb_days_access_after);
$coachEndDate = $endDate->add($diffEnd);
$coachEndDate = $coachEndDate->format('Y-m-d H:i:s');
}
$id_session = SessionManager::create_session(
$name,
$date_start,
$date_end,
$date_start,
$date_end,
$coachStartDate->format('Y-m-d H:i:s'),
$coachEndDate->format('Y-m-d H:i:s'),
$coachStartDate,
$coachEndDate,
$id_coach,
0,
0,
@ -4135,32 +4146,6 @@ function WSEditSession($params)
}
/* Register WSSubscribeUserToCourse function */
// Register the data structures used by the service
$server->wsdl->addComplexType(
'originalUsersList',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:deleteSessionParams[]')),
'tns:originalUsersList'
);
$server->wsdl->addComplexType(
'subscribeUserToCourseParams',
'complexType',
'struct',
'all',
'',
array(
'original_user_id_values' => array('name' => 'original_user_id_values', 'type' => 'tns:originalUsersList'),
'original_user_id_name' => array('name' => 'original_user_id_name', 'type' => 'xsd:string'),
'original_course_id_value' => array('name' => 'original_course_id_value', 'type' => 'xsd:string'),
'original_course_id_name' => array('name' => 'original_course_id_value', 'type' => 'xsd:string')
)
);
/* Register WSDeleteSession function */
$server->wsdl->addComplexType(
@ -4890,70 +4875,113 @@ function WSUnSubscribeUserFromCourseSimple($params)
return $result;
}
/* Register WSSuscribeUsersToSession function */
// Register the data structures used by the service
$server->wsdl->addComplexType(
'subscribeUsersToSessionParams',
'subscribeUserToCourseParams',
'complexType',
'struct',
'all',
'',
array(
'original_user_id_values' => array('name' => 'original_user_id_values', 'type' => 'tns:originalUsersList'),
'original_user_id_name' => array('name' => 'original_user_id_name', 'type' => 'xsd:string'),
'original_user_id_values' => array('name' => 'original_user_id_values', 'type' => 'tns:originalUsersList'),
'original_user_id_name' => array('name' => 'original_user_id_name', 'type' => 'xsd:string'),
'original_course_id_value' => array('name' => 'original_course_id_value', 'type' => 'xsd:string'),
'original_course_id_name' => array('name' => 'original_course_id_value', 'type' => 'xsd:string')
)
);
// Prepare output params, in this case will return an array.
$server->wsdl->addComplexType(
'result_subscribeUsersToSession',
'complexType',
'struct',
'all',
'',
array(
'original_user_id_values' => array('name' => 'original_user_id_values', 'type' => 'xsd:string'),
'original_session_id_value' => array('name' => 'original_session_id_value', 'type' => 'xsd:string'),
'original_session_id_name' => array('name' => 'original_session_id_name', 'type' => 'xsd:string')
'result' => array('name' => 'result', 'type' => 'xsd:string')
)
);
$server->wsdl->addComplexType(
'subscribeUsersToSessionParamsList',
'results_subscribeUsersToSession',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:subscribeUsersToSessionParams[]')),
'tns:subscribeUsersToSessionParams'
array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_subscribeUsersToSession[]')),
'tns:result_subscribeUsersToSession'
);
$server->wsdl->addComplexType(
'subscribeUsersToSession',
'originalUserItem',
'complexType',
'struct',
'all',
'',
array(
'userssessions' => array('name' => 'userssessions', 'type' => 'tns:subscribeUsersToSessionParamsList'),
'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string')
'original_user_id_value' => array('name' => 'original_user_id_value', 'type' => 'xsd:string')
)
);
// Prepare output params, in this case will return an array.
// Register the data structures used by the service
$server->wsdl->addComplexType(
'result_subscribeUsersToSession',
'originalUsersList',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:originalUserItem[]')),
'tns:originalUserItem'
);
/* Register WSSuscribeUsersToSession function */
// Register the data structures used by the service
$server->wsdl->addComplexType(
'subscribeUsersToSessionParams',
'complexType',
'struct',
'all',
'',
array(
'original_user_id_values' => array('name' => 'original_user_id_values', 'type' => 'xsd:string'),
'original_user_id_values' => array('name' => 'original_user_id_values', 'type' => 'tns:originalUsersList'),
'original_user_id_name' => array('name' => 'original_user_id_name', 'type' => 'xsd:string'),
'original_session_id_value' => array('name' => 'original_session_id_value', 'type' => 'xsd:string'),
'result' => array('name' => 'result', 'type' => 'xsd:string')
'original_session_id_name' => array('name' => 'original_session_id_name', 'type' => 'xsd:string')
)
);
$server->wsdl->addComplexType(
'results_subscribeUsersToSession',
'subscribeUsersToSessionParamsList',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_subscribeUsersToSession[]')),
'tns:result_subscribeUsersToSession'
array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:subscribeUsersToSessionParams[]')),
'tns:subscribeUsersToSessionParams'
);
$server->wsdl->addComplexType(
'subscribeUsersToSession',
'complexType',
'struct',
'all',
'',
array(
'userssessions' => array('name' => 'userssessions', 'type' => 'tns:subscribeUsersToSessionParamsList'),
'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string')
)
);
// Register the method to expose
$server->register('WSSuscribeUsersToSession', // method name
array('subscribeUsersToSession' => 'tns:subscribeUsersToSession'), // input parameters
@ -4968,10 +4996,10 @@ $server->register('WSSuscribeUsersToSession', // method
// define the method WSSuscribeUsersToSession
function WSSuscribeUsersToSession($params)
{
global $debug;
if (!WSHelperVerifyKey($params)) {
return return_error(WS_ERROR_SECRET_KEY);
}
$user_table = Database::get_main_table(TABLE_MAIN_USER);
$tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
$tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
@ -4979,11 +5007,14 @@ function WSSuscribeUsersToSession($params)
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$userssessions_params = $params['userssessions'];
if ($debug) {
error_log(print_r($params, 1));
}
$results = array();
$orig_user_id_value = array();
$orig_session_id_value = array();
foreach ($userssessions_params as $usersession_params) {
$original_session_id_value = $usersession_params['original_session_id_value'];
$original_session_id_name = $usersession_params['original_session_id_name'];
$original_user_id_name = $usersession_params['original_user_id_name'];
@ -5003,9 +5034,14 @@ function WSSuscribeUsersToSession($params)
$usersList = array();
foreach ($original_user_id_values as $key => $row_original_user_list) {
$user_id = UserManager::get_user_id_from_original_id(
$original_user_id_values[$key],
$original_user_id_name[$key]
$row_original_user_list['original_user_id_value'],
$original_user_id_name
);
if ($debug) {
error_log(" User to subscribe: $user_id");
}
if ($user_id == 0) {
continue; // user_id doesn't exist.
} else {

Loading…
Cancel
Save