upgrade
This commit is contained in:
28
main/webservices/README.md
Normal file
28
main/webservices/README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
= Chamilo Webservices =
|
||||
|
||||
Chamilo webservices are not the greatest API you can find around, but they
|
||||
kind of work, as long as you don't get fooled by the many files in this
|
||||
folder.
|
||||
|
||||
The main maintained script is registration.soap.php
|
||||
The way to call it is relatively well described in the example:
|
||||
client_soap.php
|
||||
|
||||
Basically, we have a weird way of authenticating you (until we release APIv2
|
||||
with OAuth or similar authentication methods). We ask you to include the
|
||||
public IP of the server calling the webservice inside the key, and to combine
|
||||
that key with the $_configuration['security_key'] value in
|
||||
app/config/configuration.php.
|
||||
|
||||
You can get your own public IP by doing a wget on the testip.php file in this
|
||||
folder (you can do that automatically through a file_get_contents() or fopen()
|
||||
as well, if you need to).
|
||||
There is a way to alter this mechanism by adding a specific IP to the file
|
||||
webservice-auth-ip.conf.php.
|
||||
|
||||
Once you're all setup with the key to connect to Chamilo, just call your
|
||||
webservices like you would normally do through SOAP (that's where the
|
||||
client_soap.php file can really get you through).
|
||||
|
||||
Most of the other files are (failed) attempts at redesigning the API. We hope
|
||||
we'll get the chance to provide a better API soon.
|
||||
490
main/webservices/access_url.php
Normal file
490
main/webservices/access_url.php
Normal file
@@ -0,0 +1,490 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
|
||||
api_protect_webservices();
|
||||
|
||||
$debug = true;
|
||||
|
||||
define('WS_ERROR_SECRET_KEY', 1);
|
||||
define('WS_ERROR_NOT_FOUND_RESULT', 2);
|
||||
define('WS_ERROR_INVALID_INPUT', 3);
|
||||
define('WS_ERROR_SETTING', 4);
|
||||
|
||||
/**
|
||||
* @param int $code
|
||||
*/
|
||||
function return_error($code)
|
||||
{
|
||||
$fault = null;
|
||||
switch ($code) {
|
||||
case WS_ERROR_SECRET_KEY:
|
||||
$fault = new soap_fault(
|
||||
'Server',
|
||||
'',
|
||||
'Secret key is not correct or params are not correctly set'
|
||||
);
|
||||
break;
|
||||
case WS_ERROR_NOT_FOUND_RESULT:
|
||||
$fault = new soap_fault(
|
||||
'Server',
|
||||
'',
|
||||
'No result was found for this query'
|
||||
);
|
||||
break;
|
||||
case WS_ERROR_INVALID_INPUT:
|
||||
$fault = new soap_fault(
|
||||
'Server',
|
||||
'',
|
||||
'The input variables are invalid o are not correctly set'
|
||||
);
|
||||
break;
|
||||
case WS_ERROR_SETTING:
|
||||
$fault = new soap_fault(
|
||||
'Server',
|
||||
'',
|
||||
'Please check the configuration for this webservice'
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
return $fault;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function WSHelperVerifyKey($params)
|
||||
{
|
||||
global $_configuration, $debug;
|
||||
if (is_array($params)) {
|
||||
$secret_key = $params['secret_key'];
|
||||
} else {
|
||||
$secret_key = $params;
|
||||
}
|
||||
//error_log(print_r($params,1));
|
||||
$check_ip = false;
|
||||
$ip_matches = false;
|
||||
$ip = trim($_SERVER['REMOTE_ADDR']);
|
||||
// if we are behind a reverse proxy, assume it will send the
|
||||
// HTTP_X_FORWARDED_FOR header and use this IP instead
|
||||
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||
list($ip1, $ip2) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
||||
$ip = trim($ip1);
|
||||
}
|
||||
if ($debug) {
|
||||
error_log("ip: $ip");
|
||||
}
|
||||
// Check if a file that limits access from webservices exists and contains
|
||||
// the restraining check
|
||||
if (is_file('webservice-auth-ip.conf.php')) {
|
||||
include 'webservice-auth-ip.conf.php';
|
||||
if ($debug) {
|
||||
error_log("webservice-auth-ip.conf.php file included");
|
||||
}
|
||||
if (!empty($ws_auth_ip)) {
|
||||
$check_ip = true;
|
||||
$ip_matches = api_check_ip_in_range($ip, $ws_auth_ip);
|
||||
if ($debug) {
|
||||
error_log("ip_matches: $ip_matches");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($debug) {
|
||||
error_log("checkip ".intval($check_ip));
|
||||
}
|
||||
|
||||
if ($check_ip) {
|
||||
$security_key = $_configuration['security_key'];
|
||||
} else {
|
||||
$security_key = $ip.$_configuration['security_key'];
|
||||
//error_log($secret_key.'-'.$security_key);
|
||||
}
|
||||
|
||||
$result = api_is_valid_secret_key($secret_key, $security_key);
|
||||
//error_log($secret_key.'-'.$security_key);
|
||||
if ($debug) {
|
||||
error_log('WSHelperVerifyKey result: '.intval($result));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Create the server instance
|
||||
$server = new soap_server();
|
||||
|
||||
/** @var HookWSRegistration $hook */
|
||||
$hook = HookWSRegistration::create();
|
||||
if (!empty($hook)) {
|
||||
$hook->setEventData(['server' => $server]);
|
||||
$res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_PRE);
|
||||
if (!empty($res['server'])) {
|
||||
$server = $res['server'];
|
||||
}
|
||||
}
|
||||
|
||||
$server->soap_defencoding = 'UTF-8';
|
||||
|
||||
// Initialize WSDL support
|
||||
$server->configureWSDL('WSAccessUrl', 'urn:WSAccessUrl');
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'portalItem',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'id' => ['name' => 'id', 'type' => 'xsd:string'],
|
||||
'url' => ['name' => 'url', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'portalList',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[
|
||||
[
|
||||
'ref' => 'SOAP-ENC:arrayType',
|
||||
'wsdl:arrayType' => 'tns:portalItem[]',
|
||||
],
|
||||
],
|
||||
'tns:portalItem'
|
||||
);
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'getPortals',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSGetPortals', // method name
|
||||
['getPortals' => 'tns:getPortals'], // input parameters
|
||||
['return' => 'tns:portalList'], // output parameters
|
||||
'urn:WSAccessUrl', // namespace
|
||||
'urn:WSAccessUrl#WSGetPortals', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service adds a user to portal' // documentation
|
||||
);
|
||||
|
||||
// Define the method WSAddUserToPortal
|
||||
function WSGetPortals($params)
|
||||
{
|
||||
global $debug;
|
||||
if (!WSHelperVerifyKey($params['secret_key'])) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
$urlData = UrlManager::get_url_data();
|
||||
|
||||
$return = [];
|
||||
foreach ($urlData as $data) {
|
||||
$return[] = [
|
||||
'id' => $data['id'],
|
||||
'url' => $data['url'],
|
||||
];
|
||||
}
|
||||
if ($debug) {
|
||||
error_log(print_r($return, 1));
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'AddUserToPortal',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
|
||||
'portal_id' => ['name' => 'portal_id', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSAddUserToPortal', // method name
|
||||
['addUserToPortal' => 'tns:AddUserToPortal'], // input parameters
|
||||
['return' => 'xsd:string'], // output parameters
|
||||
'urn:WSAccessUrl', // namespace
|
||||
'urn:WSAccessUrl#WSAddUserToPortal', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service adds a user to portal' // documentation
|
||||
);
|
||||
|
||||
// Define the method WSAddUserToPortal
|
||||
function WSAddUserToPortal($params)
|
||||
{
|
||||
if (!WSHelperVerifyKey($params['secret_key'])) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
$userId = $params['user_id'];
|
||||
$portalId = $params['portal_id'];
|
||||
|
||||
UrlManager::add_user_to_url($userId, $portalId);
|
||||
|
||||
$result = UrlManager::relation_url_user_exist($userId, $portalId);
|
||||
if (!empty($result)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSRemoveUserFromPortal', // method name
|
||||
['removeUserFromPortal' => 'tns:AddUserToPortal'], // input parameters
|
||||
['return' => 'xsd:string'], // output parameters
|
||||
'urn:WSAccessUrl', // namespace
|
||||
'urn:WSAccessUrl#WSRemoveUserFromPortal', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service remove a user from a portal' // documentation
|
||||
);
|
||||
|
||||
// Define the method WSDeleteUserFromGroup
|
||||
function WSRemoveUserFromPortal($params)
|
||||
{
|
||||
if (!WSHelperVerifyKey($params['secret_key'])) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
$userId = $params['user_id'];
|
||||
$portalId = $params['portal_id'];
|
||||
|
||||
UrlManager::delete_url_rel_user($userId, $portalId);
|
||||
|
||||
$result = UrlManager::relation_url_user_exist($userId, $portalId);
|
||||
if (empty($result)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'getPortalListFromUser',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSGetPortalListFromUser', // method name
|
||||
['getPortalListFromUser' => 'tns:getPortalListFromUser'], // input parameters
|
||||
['return' => 'tns:portalList'], // output parameters
|
||||
'urn:WSAccessUrl', // namespace
|
||||
'urn:WSAccessUrl#WSGetPortalListFromUser', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service remove a user from a portal' // documentation
|
||||
);
|
||||
|
||||
// Define the method WSDeleteUserFromGroup
|
||||
function WSGetPortalListFromUser($params)
|
||||
{
|
||||
if (!WSHelperVerifyKey($params['secret_key'])) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
$userId = $params['user_id'];
|
||||
|
||||
$result = UrlManager::get_access_url_from_user($userId);
|
||||
if (!empty($result)) {
|
||||
foreach ($result as &$data) {
|
||||
$data['id'] = $data['access_url_id'];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Course ws
|
||||
$server->wsdl->addComplexType(
|
||||
'getPortalListFromCourse',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
|
||||
'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSGetPortalListFromCourse', // method name
|
||||
['getPortalListFromCourse' => 'tns:getPortalListFromCourse'], // input parameters
|
||||
['return' => 'tns:portalList'], // output parameters
|
||||
'urn:WSAccessUrl', // namespace
|
||||
'urn:WSAccessUrl#getPortalListFromCourse', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service remove a user from a portal' // documentation
|
||||
);
|
||||
|
||||
// Define the method WSDeleteUserFromGroup
|
||||
function WSGetPortalListFromCourse($params)
|
||||
{
|
||||
if (!WSHelperVerifyKey($params['secret_key'])) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
$courseInfo = CourseManager::getCourseInfoFromOriginalId(
|
||||
$params['original_course_id_value'],
|
||||
$params['original_course_id_name']
|
||||
);
|
||||
|
||||
$courseId = $courseInfo['real_id'];
|
||||
|
||||
$result = UrlManager::get_access_url_from_course($courseId);
|
||||
|
||||
if (!empty($result)) {
|
||||
foreach ($result as &$data) {
|
||||
$data['id'] = $data['access_url_id'];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'addCourseToPortal',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
'portal_id' => ['name' => 'portal_id', 'type' => 'xsd:string'],
|
||||
'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
|
||||
'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSAddCourseToPortal', // method name
|
||||
['addCourseToPortal' => 'tns:addCourseToPortal'], // input parameters
|
||||
['return' => 'xsd:string'], // output parameters
|
||||
'urn:WSAccessUrl', // namespace
|
||||
'urn:WSAccessUrl#WSAddCourseToPortal', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service adds a course to portal' // documentation
|
||||
);
|
||||
|
||||
// Define the method WSAddUserToPortal
|
||||
function WSAddCourseToPortal($params)
|
||||
{
|
||||
if (!WSHelperVerifyKey($params['secret_key'])) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
$courseInfo = CourseManager::getCourseInfoFromOriginalId(
|
||||
$params['original_course_id_value'],
|
||||
$params['original_course_id_name']
|
||||
);
|
||||
|
||||
$courseId = $courseInfo['real_id'];
|
||||
$portalId = $params['portal_id'];
|
||||
|
||||
UrlManager::add_course_to_url($courseId, $portalId);
|
||||
|
||||
$result = UrlManager::relation_url_course_exist($courseId, $portalId);
|
||||
|
||||
return intval($result);
|
||||
}
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSRemoveCourseFromPortal', // method name
|
||||
['removeCourseFromPortal' => 'tns:addCourseToPortal'], // input parameters
|
||||
['return' => 'xsd:string'], // output parameters
|
||||
'urn:WSAccessUrl', // namespace
|
||||
'urn:WSAccessUrl#WSRemoveCourseFromPortal', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service remove a course from a portal' // documentation
|
||||
);
|
||||
|
||||
// Define the method WSDeleteUserFromGroup
|
||||
function WSRemoveCourseFromPortal($params)
|
||||
{
|
||||
if (!WSHelperVerifyKey($params['secret_key'])) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
$courseInfo = CourseManager::getCourseInfoFromOriginalId(
|
||||
$params['original_course_id_value'],
|
||||
$params['original_course_id_name']
|
||||
);
|
||||
|
||||
$courseId = $courseInfo['real_id'];
|
||||
$portalId = $params['portal_id'];
|
||||
|
||||
UrlManager::delete_url_rel_course($courseId, $portalId);
|
||||
$result = UrlManager::relation_url_course_exist($courseId, $portalId);
|
||||
|
||||
if (empty($result)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Delete user from group Web Service end */
|
||||
|
||||
// Add more webservices through hooks from plugins
|
||||
if (!empty($hook)) {
|
||||
$hook->setEventData(['server' => $server]);
|
||||
$res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_POST);
|
||||
if (!empty($res['server'])) {
|
||||
$server = $res['server'];
|
||||
}
|
||||
}
|
||||
|
||||
// Use the request to (try to) invoke the service
|
||||
$GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents('php://input');
|
||||
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
|
||||
|
||||
// If you send your data in utf8 then this value must be false.
|
||||
$decodeUTF8 = api_get_setting('registration.soap.php.decode_utf8');
|
||||
if ($decodeUTF8 === 'true') {
|
||||
$server->decode_utf8 = true;
|
||||
} else {
|
||||
$server->decode_utf8 = false;
|
||||
}
|
||||
$server->service($HTTP_RAW_POST_DATA);
|
||||
183
main/webservices/additional_webservices.php
Normal file
183
main/webservices/additional_webservices.php
Normal file
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*
|
||||
* @author Francis Gonzales
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
|
||||
$enableThis = api_get_configuration_value('webservice_remote_ppt2png_enable');
|
||||
if (!$enableThis) {
|
||||
echo "Remote PPT2PNG service is disabled. \n";
|
||||
echo "To enable, add \$_configuration['webservice_remote_ppt2png_enable'] = true; to your configuration.php";
|
||||
exit;
|
||||
}
|
||||
api_protect_webservices();
|
||||
|
||||
/**
|
||||
* Function to convert from ppt to png
|
||||
* This function is used from Chamilo Rapid Lesson.
|
||||
*
|
||||
* @param array $pptData
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function wsConvertPpt($pptData)
|
||||
{
|
||||
global $_configuration;
|
||||
$ip = trim($_SERVER['REMOTE_ADDR']);
|
||||
// If an IP filter array is defined in configuration.php,
|
||||
// check if this IP is allowed
|
||||
if (!empty($_configuration['ppt2lp_ip_filter'])) {
|
||||
if (!in_array($ip, $_configuration['ppt2lp_ip_filter'])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$fileData = $pptData['file_data'];
|
||||
// Clean filename to avoid hacks. Prevents "&" and ";" to be used in filename, notably
|
||||
|
||||
if (strpos($pptData['file_name'], '..') !== false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$sanitizedFileName = $pptData['file_name'];
|
||||
$dataInfo = pathinfo($sanitizedFileName);
|
||||
$fileName = basename($sanitizedFileName, '.'.$dataInfo['extension']);
|
||||
// Add additional cleaning of .php and .htaccess files
|
||||
$fullFileName = Security::filter_filename($sanitizedFileName);
|
||||
$size = $pptData['service_ppt2lp_size'];
|
||||
$w = 800;
|
||||
$h = 600;
|
||||
if (!empty($size)) {
|
||||
list($w, $h) = explode('x', $size);
|
||||
$w = (int) $w;
|
||||
$h = (int) $h;
|
||||
}
|
||||
|
||||
$tempArchivePath = api_get_path(SYS_ARCHIVE_PATH);
|
||||
$tempPath = $tempArchivePath.'wsConvert/'.$fileName.'/';
|
||||
$tempPathNewFiles = $tempArchivePath.'wsConvert/'.$fileName.'-n/';
|
||||
|
||||
$oldumask = umask(0);
|
||||
//$perms = api_get_permissions_for_new_directories();
|
||||
// Set permissions the most permissively possible: these files will
|
||||
// be deleted below and we need a parallel process to be able to write them
|
||||
$perms = api_get_permissions_for_new_directories();
|
||||
pptConverterDirectoriesCreate($tempPath, $tempPathNewFiles, $fileName, $perms);
|
||||
|
||||
$file = base64_decode($fileData);
|
||||
file_put_contents($tempPath.$fullFileName, $file);
|
||||
|
||||
$cmd = pptConverterGetCommandBaseParams(
|
||||
$w,
|
||||
$h,
|
||||
$tempPath.$fullFileName,
|
||||
$tempPathNewFiles.$fileName.'.html'
|
||||
);
|
||||
|
||||
//$perms = api_get_permissions_for_new_files();
|
||||
chmod($tempPathNewFiles.$fileName, $perms);
|
||||
|
||||
$files = [];
|
||||
$return = 0;
|
||||
$shell = exec($cmd, $files, $return);
|
||||
umask($oldumask);
|
||||
|
||||
if ($return === 0) {
|
||||
$images = [];
|
||||
if (is_array($files) && !empty($files)) {
|
||||
foreach ($files as $file) {
|
||||
$imageData = explode('||', $file);
|
||||
$images[$imageData[1]] = base64_encode(file_get_contents($tempPathNewFiles.$fileName.'/'.$imageData[1]));
|
||||
}
|
||||
}
|
||||
$data = [
|
||||
'files' => $files,
|
||||
'images' => $images,
|
||||
];
|
||||
|
||||
deleteDirectory($tempPath);
|
||||
deleteDirectory($tempPathNewFiles);
|
||||
|
||||
return serialize($data);
|
||||
} else {
|
||||
deleteDirectory($tempPath);
|
||||
deleteDirectory($tempPathNewFiles);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $directoryPath
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function deleteDirectory($directoryPath)
|
||||
{
|
||||
$files = array_diff(scandir($directoryPath), ['.', '..']);
|
||||
foreach ($files as $file) {
|
||||
if (is_dir("$directoryPath/$file")) {
|
||||
deleteDirectory("$directoryPath/$file");
|
||||
} else {
|
||||
unlink("$directoryPath/$file");
|
||||
}
|
||||
}
|
||||
|
||||
return rmdir($directoryPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to create the directory structure for the PPT converter.
|
||||
*
|
||||
* @param string $tempPath
|
||||
* @param string $tempPathNewFiles
|
||||
* @param string $fileName
|
||||
* @param string $perms
|
||||
*/
|
||||
function pptConverterDirectoriesCreate($tempPath, $tempPathNewFiles, $fileName, $perms)
|
||||
{
|
||||
if (!is_dir($tempPath)) {
|
||||
mkdir($tempPath, $perms, true);
|
||||
}
|
||||
if (!is_dir($tempPathNewFiles)) {
|
||||
mkdir($tempPathNewFiles, $perms, true);
|
||||
}
|
||||
if (!is_dir($tempPathNewFiles.$fileName)) {
|
||||
mkdir($tempPathNewFiles.$fileName, $perms, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to build the command line parameters for the converter.
|
||||
*
|
||||
* @return string $cmd
|
||||
*/
|
||||
function pptConverterGetCommandBaseParams(int $w, int $h, string $inputPath, string $outputPath)
|
||||
{
|
||||
$cd = '';
|
||||
|
||||
if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
|
||||
$converterPath = str_replace('/', '\\', api_get_path(SYS_PATH).'main/inc/lib/ppt2png');
|
||||
$classPath = $converterPath.';'.$converterPath.'/jodconverter-2.2.2.jar;'.$converterPath.'/jodconverter-cli-2.2.2.jar';
|
||||
$cmd = 'java -Dfile.encoding=UTF-8 -cp "'.$classPath.'"';
|
||||
} else {
|
||||
$converterPath = api_get_path(SYS_PATH).'main/inc/lib/ppt2png';
|
||||
$classPath = ' -Dfile.encoding=UTF-8 -cp .:jodconverter-2.2.2.jar:jodconverter-cli-2.2.2.jar';
|
||||
$cd = 'cd '.$converterPath.' && ';
|
||||
$cmd = 'java '.$classPath;
|
||||
}
|
||||
|
||||
$cmd .= ' DokeosConverter';
|
||||
$cmd .= ' -p '.api_get_setting('service_ppt2lp', 'port');
|
||||
$cmd .= ' -w '.$w.' -h '.$h;
|
||||
$cmd .= ' -d oogie '.Security::sanitizeExecParam($inputPath).' '.Security::sanitizeExecParam($outputPath);
|
||||
|
||||
return $cd.escapeshellcmd($cmd);
|
||||
}
|
||||
|
||||
$uri = api_get_path(WEB_CODE_PATH).'webservices/';
|
||||
$server = new SoapServer(null, ['uri' => $uri]);
|
||||
$server->addFunction("wsConvertPpt");
|
||||
$server->handle();
|
||||
109
main/webservices/api/example/add_courses_session.php
Normal file
109
main/webservices/api/example/add_courses_session.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of addCoursesToSession() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function addCoursesToSession($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
'action' => 'add_courses_session',
|
||||
// data of the user who makes the request
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
// data of courses and session
|
||||
'id_session' => 1,
|
||||
'list_courses' => [
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Courses not assigned to session because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data[0];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//adding courses with id 5, 6, 7 to session with id 1
|
||||
if (addCoursesToSession($apiKey)) {
|
||||
echo 'Courses successfully added';
|
||||
}
|
||||
110
main/webservices/api/example/add_users_session.php
Normal file
110
main/webservices/api/example/add_users_session.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function addUsersToSession($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'add_users_session',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
// data for users and session
|
||||
'id_session' => 1,
|
||||
'list_users' => [
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Users not assigned to session because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data']['status'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//adding users with id 5, 6, 7 to session with id 1
|
||||
if (addUsersToSession($apiKey)) {
|
||||
echo 'Users successfully added';
|
||||
}
|
||||
58
main/webservices/api/example/authenticate.php
Normal file
58
main/webservices/api/example/authenticate.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
echo 'user API Key: '.$apiKey;
|
||||
103
main/webservices/api/example/course_agenda.php
Normal file
103
main/webservices/api/example/course_agenda.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
* @param $courseId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseAgenda($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_agenda',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course agenda because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the list of calendar events from inside the given course.
|
||||
$courseAgenda = getCourseAgenda($apiKey, 1);
|
||||
echo json_encode($courseAgenda);
|
||||
105
main/webservices/api/example/course_announcement.php
Normal file
105
main/webservices/api/example/course_announcement.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
* @param $courseId
|
||||
* @param $announcementId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getCourseAnnouncement($apiKey, $courseId, $announcementId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_announcement',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
'announcement ' => $announcementId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get announcement because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the announcement published in the given course.
|
||||
$courseAnnouncement = getCourseAnnouncement($apiKey, 1, 1);
|
||||
echo json_encode($courseAnnouncement);
|
||||
103
main/webservices/api/example/course_announcements.php
Normal file
103
main/webservices/api/example/course_announcements.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
* @param $courseId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseAnnouncements($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_announcements',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get announcements because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the announcements published in the given course.
|
||||
$courseAnnouncements = getCourseAnnouncements($apiKey, 1);
|
||||
echo json_encode($courseAnnouncements);
|
||||
103
main/webservices/api/example/course_descriptions.php
Normal file
103
main/webservices/api/example/course_descriptions.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
* @param $courseId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseDescription($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_descriptions',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course description because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the list of documents in the given course.
|
||||
$courseDescription = getCourseDocuments($apiKey, 1);
|
||||
echo json_encode($courseDescription);
|
||||
104
main/webservices/api/example/course_documents.php
Normal file
104
main/webservices/api/example/course_documents.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
* @param $courseId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseDocuments($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_documents',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course documents because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the list of documents in the given course.
|
||||
$courseDescription = getCourseDocuments($apiKey, 1);
|
||||
echo json_encode($courseDescription);
|
||||
106
main/webservices/api/example/course_forum.php
Normal file
106
main/webservices/api/example/course_forum.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
* @param $courseId
|
||||
* @param $forumId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseForum($apiKey, $courseId, $forumId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_forum',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
'forum' => $forumId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course documents because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get details about a specific forum.
|
||||
$courseForum = getCourseForum($apiKey, 1, 1);
|
||||
echo json_encode($courseForum);
|
||||
104
main/webservices/api/example/course_forumcategories.php
Normal file
104
main/webservices/api/example/course_forumcategories.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
* @param $courseId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseForumCategories($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_forumcategories',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course documents because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//A list of forum categories
|
||||
$courseForumCategories = getCourseForumCategories($apiKey, 1);
|
||||
echo json_encode($courseForumCategories);
|
||||
107
main/webservices/api/example/course_forumthread.php
Normal file
107
main/webservices/api/example/course_forumthread.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
* @param $courseId
|
||||
* @param $threadId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseForumThread($apiKey, $courseId, $forumId, $threadId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_forumthread',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
'forum' => $forumId,
|
||||
'thread' => $threadId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course documents because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get details about a specific forum thread.
|
||||
$courseForumThread = getCourseForumThread($apiKey, 1, 1, 1);
|
||||
echo json_encode($courseForumThread);
|
||||
103
main/webservices/api/example/course_info.php
Normal file
103
main/webservices/api/example/course_info.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getCourseInfo($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_info',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant get course info because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get information about one course in particular
|
||||
$userMessages = getCourseInfo($apiKey, 1);
|
||||
echo json_encode($userMessages);
|
||||
106
main/webservices/api/example/course_learnpath.php
Normal file
106
main/webservices/api/example/course_learnpath.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
* @param $courseId
|
||||
* @param $lpId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseForumThread($apiKey, $courseId, $lpId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_learnpath',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
'lp_id' => $lpId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course documents because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get details about a specific forum thread.
|
||||
$courseForumThread = getCourseForumThread($apiKey, 1, 1, 1);
|
||||
echo json_encode($courseForumThread);
|
||||
113
main/webservices/api/example/save_session.php
Normal file
113
main/webservices/api/example/save_session.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function createSession($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'save_session',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
// data for new session
|
||||
'name' => 'Test Session',
|
||||
'coach_username' => 1, // user_id of CoachUsername that needs to already exist in Chamilo
|
||||
'access_start_date' => '2020-01-15 15:00:00',
|
||||
'access_end_date' => '2021-01-15 15:00:00',
|
||||
'description' => 'My complete text description of the session',
|
||||
'extra' => [
|
||||
[
|
||||
'extra_Price' => '200', // the "Price" session extra field needs to be already created in Chamilo
|
||||
],
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant save session because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data']['id_session'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Creating a new session Test Session
|
||||
$sessionId = createSession($apiKey);
|
||||
echo 'ID of new session: '.$sessionId;
|
||||
119
main/webservices/api/example/save_user.php
Normal file
119
main/webservices/api/example/save_user.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function createUser($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'save_user',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
// data for new user
|
||||
'firstname' => 'Test User',
|
||||
'lastname' => 'Chamilo',
|
||||
'status' => 5, // student
|
||||
'email' => 'testuser@example.com',
|
||||
'loginname' => 'restuser',
|
||||
'password' => 'restuser',
|
||||
'original_user_id_name' => 'myplatform_user_id', // field to identify the user in the external system
|
||||
'original_user_id_value' => '1234', // ID for the user in the external system
|
||||
'extra' => [
|
||||
[
|
||||
'field_name' => 'age',
|
||||
'field_value' => 29,
|
||||
],
|
||||
],
|
||||
'language' => 'english',
|
||||
//'phone' => '',
|
||||
//'expiration_date' => '',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('User not created because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data[0];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Creating a new user restuser
|
||||
$userId = createUser($apiKey);
|
||||
echo 'ID of new user: '.$userId;
|
||||
114
main/webservices/api/example/update_user_from_username.php
Normal file
114
main/webservices/api/example/update_user_from_username.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function updateUserFromUsername($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'update_user_from_username',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
// data for the user to be updated
|
||||
'loginName' => 'TestUser',
|
||||
'firstname' => 'Test User',
|
||||
'lastname' => 'Chamilo',
|
||||
'status' => 5, // student
|
||||
'email' => 'testuser@example.com',
|
||||
'enabled' => 1,
|
||||
'extra' => [
|
||||
[
|
||||
'field_name' => 'age', // The "age" user extra field needs to already be created on Chamilo
|
||||
'field_value' => 35,
|
||||
],
|
||||
],
|
||||
'language' => 'english',
|
||||
'expiration_date' => '2025-12-31 23:59:59',
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('User not updated because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data[0];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//update user TestUser
|
||||
if (updateUserFromUsername($apiKey)) {
|
||||
echo 'User updated successfully';
|
||||
}
|
||||
101
main/webservices/api/example/user_courses.php
Normal file
101
main/webservices/api/example/user_courses.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getUserCourses($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_courses',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant get user courses because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get a list of courses of the user calling this service.
|
||||
$userMessages = getUserCourses($apiKey);
|
||||
echo json_encode($userMessages);
|
||||
106
main/webservices/api/example/user_message_read.php
Normal file
106
main/webservices/api/example/user_message_read.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getUserMessageRead($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_message_read',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'messages' => [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant get read messages because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Mark a specific message (in the social network interface) as read.
|
||||
$userMessages = getUserMessageRead($apiKey);
|
||||
echo json_encode($userMessages);
|
||||
107
main/webservices/api/example/user_message_unread.php
Normal file
107
main/webservices/api/example/user_message_unread.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getUserMessageUnread($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_message_unread',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'messages' => [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant get unread messages because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Mark a specific message (in the social network interface) as "unread".
|
||||
$userMessages = getUserMessageUnread($apiKey);
|
||||
echo json_encode($userMessages);
|
||||
102
main/webservices/api/example/user_messages.php
Normal file
102
main/webservices/api/example/user_messages.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getUserMessages($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_messages',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant get user messages because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Read user mesages
|
||||
$userMessages = getUserMessages($apiKey);
|
||||
echo json_encode($userMessages);
|
||||
102
main/webservices/api/example/user_profile.php
Normal file
102
main/webservices/api/example/user_profile.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getUserProfile($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_profile',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get user profile because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the list of calendar events from inside the given course.
|
||||
$userProfile = getUserProfile($apiKey);
|
||||
echo json_encode($userProfile);
|
||||
102
main/webservices/api/example/user_sessions.php
Normal file
102
main/webservices/api/example/user_sessions.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getUserSessions($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_sessions',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get user profile because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the list of sessions of the current user
|
||||
$userSessions = getUserSessions($apiKey);
|
||||
echo json_encode($userSessions);
|
||||
107
main/webservices/api/example/username_exist.php
Normal file
107
main/webservices/api/example/username_exist.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getUserNameExist($apiKey, $loginname)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'username_exist',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'loginname' => $loginname,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get user profile because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'][0];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Return if a username already exist
|
||||
$userNameExist = getUserNameExist($apiKey, 'admin');
|
||||
if ($userNameExist == true) {
|
||||
echo "User name exist";
|
||||
} else {
|
||||
echo "User doesnt name exist";
|
||||
}
|
||||
347
main/webservices/api/tests/CreateSessionFromModelTest.php
Normal file
347
main/webservices/api/tests/CreateSessionFromModelTest.php
Normal file
@@ -0,0 +1,347 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/V2TestCase.php';
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* Class CreateSessionFromModelTest
|
||||
*
|
||||
* CREATE_SESSION_FROM_MODEL webservice unit tests
|
||||
*/
|
||||
class CreateSessionFromModelTest extends V2TestCase
|
||||
{
|
||||
public function action()
|
||||
{
|
||||
return 'create_session_from_model';
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a session from a simple model session
|
||||
* asserts that it was created with the supplied data
|
||||
*/
|
||||
public function testFromASimpleModel()
|
||||
{
|
||||
// create a model session
|
||||
$modelSessionId = SessionManager::create_session(
|
||||
'Model session'.time(),
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
// call the webservice to create the new session from the model session,
|
||||
// and assert it returns an integer
|
||||
$name = 'New session'.time();
|
||||
$startDate = '2019-09-01 00:00:00';
|
||||
$endDate = '2019-12-31 00:00:00';
|
||||
|
||||
$newSessionId = $this->integer(
|
||||
[
|
||||
'modelSessionId' => $modelSessionId,
|
||||
'sessionName' => $name,
|
||||
'startDate' => $startDate,
|
||||
'endDate' => $endDate,
|
||||
]
|
||||
);
|
||||
|
||||
// assert the session was created and given the returned session id
|
||||
$entityManager = Database::getManager();
|
||||
$repository = $entityManager->getRepository('ChamiloCoreBundle:Session');
|
||||
$newSession = $repository->find($newSessionId);
|
||||
$this->assertIsObject($newSession);
|
||||
|
||||
// assert the new session got the right data
|
||||
$this->assertSame($name, $newSession->getName());
|
||||
// FIXME account for UTC / local timezone shift
|
||||
// $this->assertSame($endDate, $newSession->getDisplayEndDate());
|
||||
// $this->assertSame($startDate, $newSession->getAccessStartDate());
|
||||
// $this->assertSame($endDate, $newSession->getAccessEndDate());
|
||||
// $this->assertSame($startDate, $newSession->getCoachAccessStartDate());
|
||||
// $this->assertSame($endDate, $newSession->getCoachAccessEndDate());
|
||||
|
||||
// clean up
|
||||
SessionManager::delete($modelSessionId);
|
||||
SessionManager::delete($newSessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a session from a model session subscribed to a promotion
|
||||
* asserts that the promotion is inherited by the new session
|
||||
*/
|
||||
public function testFromAModelWithAPromotion()
|
||||
{
|
||||
// create a promotion
|
||||
$career = new Career();
|
||||
$careerId = $career->save(['name' => 'test career'.time()]);
|
||||
$promotion = new Promotion();
|
||||
$promotionId = $promotion->save(['career_id' => $careerId, 'name' => 'test promo'.time()]);
|
||||
|
||||
// create a model session
|
||||
$modelSessionId = SessionManager::create_session(
|
||||
'Model session'.time(),
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
// subscribe the model session to the promotion - the new session will inherit this too
|
||||
SessionManager::subscribe_sessions_to_promotion($promotionId, [$modelSessionId]);
|
||||
|
||||
// call the webservice to create the new session from the model session,
|
||||
// and assert it returns an integer
|
||||
$newSessionId = $this->integer(
|
||||
[
|
||||
'modelSessionId' => $modelSessionId,
|
||||
'sessionName' => 'New session'.time(),
|
||||
'startDate' => '2019-09-01 00:00',
|
||||
'endDate' => '2019-12-31 00:00',
|
||||
]
|
||||
);
|
||||
|
||||
// assert both sessions are in the promotion
|
||||
$promotionSessions = SessionManager::get_all_sessions_by_promotion($promotionId);
|
||||
$this->assertSame(2, count($promotionSessions));
|
||||
$this->assertArrayHasKey($newSessionId, $promotionSessions);
|
||||
$this->assertArrayHasKey($modelSessionId, $promotionSessions);
|
||||
|
||||
// clean up
|
||||
SessionManager::delete($modelSessionId);
|
||||
SessionManager::delete($newSessionId);
|
||||
$promotion->delete($promotionId);
|
||||
$career->delete($careerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a session from a model session with a different URL
|
||||
* asserts that the new session has the called web server URL
|
||||
*/
|
||||
public function testFromAModelWithADifferentURL()
|
||||
{
|
||||
if (!api_is_multiple_url_enabled()) {
|
||||
$this->markTestSkipped('needs multiple URL enabled');
|
||||
}
|
||||
|
||||
// create a model session
|
||||
$modelSessionId = SessionManager::create_session(
|
||||
'Model session'.time(),
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
// move the session to a non-standard URL
|
||||
$urlId = UrlManager::add('https://www.url.org/chamilo-lms/'.time(), 'Non-default URL', 1);
|
||||
UrlManager::update_urls_rel_session([$modelSessionId], $urlId);
|
||||
|
||||
// call the webservice to create the new session from the model session,
|
||||
// and assert it returns an integer
|
||||
$newSessionId = $this->integer(
|
||||
[
|
||||
'modelSessionId' => $modelSessionId,
|
||||
'sessionName' => 'Name of the new session'.time(),
|
||||
'startDate' => '2019-09-01 00:00',
|
||||
'endDate' => '2019-12-31 00:00',
|
||||
]
|
||||
);
|
||||
|
||||
// assert the current url was set on the new session
|
||||
$urls = UrlManager::get_access_url_from_session($newSessionId);
|
||||
$this->assertSame(1, count($urls));
|
||||
$this->assertSame(api_get_current_access_url_id(), intval($urls[0]['access_url_id']));
|
||||
|
||||
// clean up
|
||||
SessionManager::delete($modelSessionId);
|
||||
SessionManager::delete($newSessionId);
|
||||
UrlManager::delete($urlId);
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a session from a model session with courses
|
||||
* asserts that the new session inherits the same courses
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testFromAModelWithCourses()
|
||||
{
|
||||
// create a model session
|
||||
$modelSessionId = SessionManager::create_session(
|
||||
'Model session'.time(),
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
// create courses and add them the the model session
|
||||
$courseCodes = ['course A'.time(), 'course B'.time(), 'course C'.time()];
|
||||
$courseList = [];
|
||||
$authorId = UserManager::get_user_id_from_username(self::WEBSERVICE_USERNAME);
|
||||
foreach ($courseCodes as $code) {
|
||||
$course = CourseManager::create_course(['code' => $code, 'title' => $code], $authorId);
|
||||
$courseList[] = $course['real_id'];
|
||||
}
|
||||
SessionManager::add_courses_to_session($modelSessionId, $courseList);
|
||||
|
||||
// call the webservice to create the new session from the model session,
|
||||
// and assert it returns an integer
|
||||
$newSessionId = $this->integer(
|
||||
[
|
||||
'modelSessionId' => $modelSessionId,
|
||||
'sessionName' => 'Name of the new session'.time(),
|
||||
'startDate' => '2019-09-01 00:00',
|
||||
'endDate' => '2019-12-31 00:00',
|
||||
]
|
||||
);
|
||||
|
||||
// assert the new session inherited the model session courses
|
||||
$modelCourseList = array_keys(SessionManager::get_course_list_by_session_id($modelSessionId));
|
||||
$newCourseList = array_keys(SessionManager::get_course_list_by_session_id($newSessionId));
|
||||
$this->assertSame($modelCourseList, $newCourseList);
|
||||
|
||||
// clean up
|
||||
foreach ($courseCodes as $code) {
|
||||
CourseManager::delete_course($code);
|
||||
}
|
||||
SessionManager::delete($modelSessionId);
|
||||
SessionManager::delete($newSessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a session from a model session with extra fields
|
||||
* asserts that the new session inherits the extra fields
|
||||
* and that specifying other extra field values works
|
||||
*/
|
||||
public function testFromAModelWithExtraFields()
|
||||
{
|
||||
// create a model session
|
||||
$modelSessionId = SessionManager::create_session(
|
||||
'Model session'.time(),
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
// create an extra field and set its value in the model session
|
||||
// the new session will be given a different value in this field
|
||||
$extraFieldModel = new ExtraField('session');
|
||||
$firstExtraFieldName = 'extraField'.time();
|
||||
$firstExtraFieldNameForModelSession = 'extra field value for model';
|
||||
$firstExtraFieldNameForNewSession = 'extra field value for new session';
|
||||
$firstExtraFieldId = $extraFieldModel->save(
|
||||
[
|
||||
'field_type' => ExtraField::FIELD_TYPE_TEXT,
|
||||
'variable' => $firstExtraFieldName,
|
||||
'display_text' => $firstExtraFieldName,
|
||||
'visible_to_self' => 1,
|
||||
'visible_to_others' => 1,
|
||||
'changeable' => 1,
|
||||
'filter' => 1,
|
||||
]
|
||||
);
|
||||
SessionManager::update_session_extra_field_value(
|
||||
$modelSessionId,
|
||||
$firstExtraFieldName,
|
||||
$firstExtraFieldNameForModelSession
|
||||
);
|
||||
|
||||
// create a second extra field and set its value in the model session
|
||||
// the new session will inherit the same value in this field
|
||||
$secondExtraFieldName = 'secondExtraField'.time();
|
||||
$secondExtraFieldValue = 'second extra field value';
|
||||
$secondExtraFieldId = $extraFieldModel->save(
|
||||
[
|
||||
'field_type' => ExtraField::FIELD_TYPE_TEXT,
|
||||
'variable' => $secondExtraFieldName,
|
||||
'display_text' => $secondExtraFieldName,
|
||||
'visible_to_self' => 1,
|
||||
'visible_to_others' => 1,
|
||||
'changeable' => 1,
|
||||
'filter' => 1,
|
||||
]
|
||||
);
|
||||
SessionManager::update_session_extra_field_value(
|
||||
$modelSessionId,
|
||||
$secondExtraFieldName,
|
||||
$secondExtraFieldValue
|
||||
);
|
||||
|
||||
// call the webservice to create the new session from the model session,
|
||||
// specifying a different value in the first extra field
|
||||
// and assert it returns an integer
|
||||
$newSessionId = $this->integer(
|
||||
[
|
||||
'modelSessionId' => $modelSessionId,
|
||||
'sessionName' => 'Name of the new session'.time(),
|
||||
'startDate' => '2019-09-01 00:00',
|
||||
'endDate' => '2019-12-31 00:00',
|
||||
'extraFields' => [
|
||||
$firstExtraFieldName => $firstExtraFieldNameForNewSession,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
// assert the new session has its own new value in the first extra field
|
||||
$extraFieldValueModel = new ExtraFieldValue('session');
|
||||
$extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable(
|
||||
$newSessionId,
|
||||
$firstExtraFieldName
|
||||
);
|
||||
$this->assertNotFalse($extraFieldValue);
|
||||
$this->assertSame($firstExtraFieldNameForNewSession, $extraFieldValue['value']);
|
||||
|
||||
// assert the model session still has its own original value in the first extra field
|
||||
$extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable(
|
||||
$modelSessionId,
|
||||
$firstExtraFieldName
|
||||
);
|
||||
$this->assertNotFalse($extraFieldValue);
|
||||
$this->assertSame($firstExtraFieldNameForModelSession, $extraFieldValue['value']);
|
||||
|
||||
// assert the new session has inherited the model session value in the second extra field
|
||||
$extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable(
|
||||
$newSessionId,
|
||||
$secondExtraFieldName
|
||||
);
|
||||
$this->assertNotFalse($extraFieldValue);
|
||||
$this->assertSame($secondExtraFieldValue, $extraFieldValue['value']);
|
||||
|
||||
// assert the model session still has the same value in the second extra field
|
||||
$extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable(
|
||||
$modelSessionId,
|
||||
$secondExtraFieldName
|
||||
);
|
||||
$this->assertNotFalse($extraFieldValue);
|
||||
$this->assertSame($secondExtraFieldValue, $extraFieldValue['value']);
|
||||
|
||||
// clean up
|
||||
SessionManager::delete($modelSessionId);
|
||||
SessionManager::delete($newSessionId);
|
||||
$extraFieldModel->delete($firstExtraFieldId);
|
||||
$extraFieldModel->delete($secondExtraFieldId);
|
||||
}
|
||||
}
|
||||
30
main/webservices/api/tests/GetCourseLpProgressTest.php
Normal file
30
main/webservices/api/tests/GetCourseLpProgressTest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/V2TestCase.php';
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
|
||||
class GetCourseLpProgressTest extends V2TestCase
|
||||
{
|
||||
public function action()
|
||||
{
|
||||
return 'course_lp_progress';
|
||||
}
|
||||
|
||||
public function testCourseList()
|
||||
{
|
||||
$result = $this->dataArray();
|
||||
$this->assertIsArray($result);
|
||||
|
||||
//$result = $this->dataArray();
|
||||
|
||||
//$this->assertIsObject($result);
|
||||
//$this->assertObjectHasAttribute('courses', $result);
|
||||
|
||||
//$this->assertSame(1, count($urls));
|
||||
|
||||
// expect the web service to return false
|
||||
//$this->assertFalse($this->boolean(['loginname' => $loginName]));
|
||||
}
|
||||
}
|
||||
135
main/webservices/api/tests/GetSessionFromExtraFieldTest.php
Normal file
135
main/webservices/api/tests/GetSessionFromExtraFieldTest.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/V2TestCase.php';
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
|
||||
|
||||
/**
|
||||
* Class GetSessionFromExtraFieldTest
|
||||
*
|
||||
* GET_SESSION_FROM_EXTRA_FIELD webservice unit tests
|
||||
*/
|
||||
class GetSessionFromExtraFieldTest extends V2TestCase
|
||||
{
|
||||
public function action()
|
||||
{
|
||||
return 'get_session_from_extra_field';
|
||||
}
|
||||
|
||||
/**
|
||||
* creates two extra fields and 2 sessions
|
||||
* asserts that the sessions can be found one by one but not together
|
||||
*
|
||||
*/
|
||||
public function test()
|
||||
{
|
||||
// create 2 extra fields
|
||||
$extraFieldModel = new ExtraField('session');
|
||||
$firstExtraFieldName = 'firstExtraField'.time();
|
||||
$firstExtraFieldId = $extraFieldModel->save(
|
||||
[
|
||||
'field_type' => ExtraField::FIELD_TYPE_TEXT,
|
||||
'variable' => $firstExtraFieldName,
|
||||
'display_text' => $firstExtraFieldName,
|
||||
'visible_to_self' => 1,
|
||||
'visible_to_others' => 1,
|
||||
'changeable' => 1,
|
||||
'filter' => 1,
|
||||
]
|
||||
);
|
||||
$secondExtraFieldName = 'secondExtraField'.time();
|
||||
$secondExtraFieldId = $extraFieldModel->save(
|
||||
[
|
||||
'field_type' => ExtraField::FIELD_TYPE_TEXT,
|
||||
'variable' => $secondExtraFieldName,
|
||||
'display_text' => $secondExtraFieldName,
|
||||
'visible_to_self' => 1,
|
||||
'visible_to_others' => 1,
|
||||
'changeable' => 1,
|
||||
'filter' => 1,
|
||||
]
|
||||
);
|
||||
|
||||
// create 2 sessions
|
||||
$firstSessionId = SessionManager::create_session(
|
||||
'First session'.time(),
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
null,
|
||||
null
|
||||
);
|
||||
$secondSessionId = SessionManager::create_session(
|
||||
'Second session'.time(),
|
||||
'2019-09-01 00:00',
|
||||
'2019-12-31 00:00',
|
||||
'2019-09-01 00:00',
|
||||
'2019-12-31 00:00',
|
||||
'2019-09-01 00:00',
|
||||
'2019-12-31 00:00',
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
// assign unique distinct value in first field to each session
|
||||
SessionManager::update_session_extra_field_value($firstSessionId, $firstExtraFieldName, $firstSessionId);
|
||||
SessionManager::update_session_extra_field_value($secondSessionId, $firstExtraFieldName, $secondSessionId);
|
||||
|
||||
// assign the same value in second field to all sessions
|
||||
$commonValue = 'common value';
|
||||
SessionManager::update_session_extra_field_value($firstSessionId, $secondExtraFieldName, $commonValue);
|
||||
SessionManager::update_session_extra_field_value($secondSessionId, $secondExtraFieldName, $commonValue);
|
||||
|
||||
// assert that the correct session id is returned using each unique value
|
||||
$this->assertSame(
|
||||
$firstSessionId,
|
||||
$this->integer(
|
||||
[
|
||||
'field_name' => $firstExtraFieldName,
|
||||
'field_value' => $firstSessionId,
|
||||
]
|
||||
)
|
||||
);
|
||||
$this->assertSame(
|
||||
$secondSessionId,
|
||||
$this->integer(
|
||||
[
|
||||
'field_name' => $firstExtraFieldName,
|
||||
'field_value' => $secondSessionId,
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
// assert search for common value in second field generates the right error message
|
||||
$this->assertSame(
|
||||
get_lang('MoreThanOneSessionMatched'),
|
||||
$this->errorMessageString(
|
||||
[
|
||||
'field_name' => $secondExtraFieldName,
|
||||
'field_value' => $commonValue,
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
// assert search for unknown value generates the right error message
|
||||
$this->assertSame(
|
||||
get_lang('NoSessionMatched'),
|
||||
$this->errorMessageString(
|
||||
[
|
||||
'field_name' => $secondExtraFieldName,
|
||||
'field_value' => 'non-existent value',
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
// clean up
|
||||
SessionManager::delete($firstSessionId);
|
||||
SessionManager::delete($secondSessionId);
|
||||
$extraFieldModel->delete($firstExtraFieldId);
|
||||
$extraFieldModel->delete($secondExtraFieldId);
|
||||
}
|
||||
}
|
||||
2
main/webservices/api/tests/README
Normal file
2
main/webservices/api/tests/README
Normal file
@@ -0,0 +1,2 @@
|
||||
Command to run the tests :
|
||||
vendor/phpunit/phpunit/phpunit main/webservices/api/tests
|
||||
94
main/webservices/api/tests/SaveUserJsonTest.php
Normal file
94
main/webservices/api/tests/SaveUserJsonTest.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/V2TestCase.php';
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* Class SaveUserJsonTest
|
||||
*
|
||||
* SAVE_USER_JSON webservice unit tests
|
||||
*/
|
||||
class SaveUserJsonTest extends V2TestCase
|
||||
{
|
||||
public function action()
|
||||
{
|
||||
return 'save_user_json';
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a minimal test user
|
||||
* asserts that it was created with the supplied data
|
||||
*
|
||||
* @throws Exception if it cannot delete the created test user
|
||||
*/
|
||||
public function testCreateAMinimalUser()
|
||||
{
|
||||
// call the web service with minimal information
|
||||
$loginName = 'testUser'.time();
|
||||
$email = 'testUser@local';
|
||||
$status = 5;
|
||||
$json = json_encode(
|
||||
[
|
||||
'loginname' => $loginName,
|
||||
'firstname' => 'Małgorzata',
|
||||
'lastname' => 'Summer',
|
||||
'original_user_id_name' => 'external_user_id',
|
||||
'original_user_id_value' => $loginName,
|
||||
'email' => $email,
|
||||
'status' => $status,
|
||||
'password' => 'test',
|
||||
]
|
||||
);
|
||||
$userId = $this->integer([ 'json' => $json ]);
|
||||
|
||||
// assert the user was saved and given the returned user id
|
||||
$user = UserManager::getManager()->find($userId);
|
||||
$this->assertNotNull($user, 'the returned userId does not point to an user');
|
||||
|
||||
// assert each field was filled with provided information
|
||||
$this->assertSame($loginName, $user->getUserName());
|
||||
$this->assertSame($email, $user->getEmail());
|
||||
$this->assertSame($status, $user->getStatus());
|
||||
|
||||
// clean up
|
||||
UserManager::delete_user($userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a test user with an extra field asserts that the extra field values were saved.
|
||||
*
|
||||
* @throws Exception if it cannot delete the created test user
|
||||
*/
|
||||
public function testCreateAUserWithExtraFields()
|
||||
{
|
||||
// call the web service
|
||||
$extraFieldName = 'age';
|
||||
$extraFieldOriginalValue = '29';
|
||||
$loginName = 'testUser'.time();
|
||||
$json = json_encode(
|
||||
[
|
||||
'loginname' => $loginName,
|
||||
'email' => 'testUser@local',
|
||||
'original_user_id_name' => 'external_user_id',
|
||||
'original_user_id_value' => $loginName,
|
||||
'status' => 5,
|
||||
'password' => 'test',
|
||||
'firstname' => 'Małgorzata',
|
||||
'lastname' => 'Summer',
|
||||
'extra' => [
|
||||
['field_name' => $extraFieldName, 'field_value' => $extraFieldOriginalValue],
|
||||
],
|
||||
]
|
||||
);
|
||||
$userId = $this->integer(['json' => $json]);
|
||||
|
||||
// assert user extra field value was saved
|
||||
$savedValue = (new ExtraFieldValue('user'))->get_values_by_handler_and_field_variable($userId, $extraFieldName);
|
||||
$this->assertNotFalse($savedValue);
|
||||
$this->assertSame($extraFieldOriginalValue, $savedValue['value']);
|
||||
|
||||
// clean up
|
||||
UserManager::delete_user($userId);
|
||||
}
|
||||
}
|
||||
92
main/webservices/api/tests/SaveUserTest.php
Normal file
92
main/webservices/api/tests/SaveUserTest.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/V2TestCase.php';
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* Class SaveUserTest
|
||||
*
|
||||
* SAVE_USER webservice unit tests
|
||||
*/
|
||||
class SaveUserTest extends V2TestCase
|
||||
{
|
||||
public function action()
|
||||
{
|
||||
return 'save_user';
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a minimal test user
|
||||
* asserts that it was created with the supplied data
|
||||
*
|
||||
* @throws Exception if it cannot delete the created test user
|
||||
*/
|
||||
public function testCreateAMinimalUser()
|
||||
{
|
||||
// call the web service with minimal information
|
||||
$loginName = 'testUser'.time();
|
||||
$email = 'testUser@local';
|
||||
$status = 5;
|
||||
$userId = $this->integer(
|
||||
[
|
||||
'loginname' => $loginName,
|
||||
'firstname' => 'Małgorzata',
|
||||
'lastname' => 'Summer',
|
||||
'original_user_id_name' => 'external_user_id',
|
||||
'original_user_id_value' => $loginName,
|
||||
'email' => $email,
|
||||
'status' => $status,
|
||||
'password' => 'test',
|
||||
]
|
||||
);
|
||||
|
||||
// assert the user was saved and given the returned user id
|
||||
$user = UserManager::getManager()->find($userId);
|
||||
$this->assertNotNull($user, 'the returned userId does not point to an user');
|
||||
|
||||
// assert each field was filled with provided information
|
||||
$this->assertSame($loginName, $user->getUserName());
|
||||
$this->assertSame($email, $user->getEmail());
|
||||
$this->assertSame($status, $user->getStatus());
|
||||
|
||||
// clean up
|
||||
UserManager::delete_user($userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a test user with an extra field asserts that the extra field values were saved.
|
||||
*
|
||||
* @throws Exception if it cannot delete the created test user
|
||||
*/
|
||||
public function testCreateAUserWithExtraFields()
|
||||
{
|
||||
// call the web service
|
||||
$extraFieldName = 'age';
|
||||
$extraFieldOriginalValue = '29';
|
||||
$loginName = 'testUser'.time();
|
||||
$userId = $this->integer(
|
||||
[
|
||||
'loginname' => $loginName,
|
||||
'email' => 'testUser@local',
|
||||
'original_user_id_name' => 'external_user_id',
|
||||
'original_user_id_value' => $loginName,
|
||||
'status' => 5,
|
||||
'password' => 'test',
|
||||
'firstname' => 'Małgorzata',
|
||||
'lastname' => 'Summer',
|
||||
'extra' => [
|
||||
['field_name' => $extraFieldName, 'field_value' => $extraFieldOriginalValue],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
// assert user extra field value was saved
|
||||
$savedValue = (new ExtraFieldValue('user'))->get_values_by_handler_and_field_variable($userId, $extraFieldName);
|
||||
$this->assertNotFalse($savedValue);
|
||||
$this->assertSame($extraFieldOriginalValue, $savedValue['value']);
|
||||
|
||||
// clean up
|
||||
UserManager::delete_user($userId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/V2TestCase.php';
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
|
||||
|
||||
/**
|
||||
* Class SubscribeUserToSessionFromUsernameTest
|
||||
*
|
||||
* SUBSCRIBE_USER_TO_SESSION_FROM_USERNAME webservice unit tests
|
||||
*/
|
||||
class SubscribeUserToSessionFromUsernameTest extends V2TestCase
|
||||
{
|
||||
public function action()
|
||||
{
|
||||
return 'subscribe_user_to_session_from_username';
|
||||
}
|
||||
|
||||
/**
|
||||
* subscribes a test user to a test session that already has another user subscribed
|
||||
* asserts that the user was subscribed to the session
|
||||
* asserts that the other user was not unsubscribed from the session
|
||||
*/
|
||||
public function testSubscribeWithoutUnsubscribe()
|
||||
{
|
||||
// create a test session
|
||||
$sessionId = SessionManager::create_session(
|
||||
'Session to subscribe'.time(),
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
'2019-01-01 00:00',
|
||||
'2019-08-31 00:00',
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
// create a test user
|
||||
$loginName = 'tester'.time();
|
||||
$userId = UserManager::create_user('Tester', 'Tester', 5, 'tester@local', $loginName, 'xXxxXxxXX');
|
||||
|
||||
// create another user and subscribe it to the session
|
||||
$anotherUserId = UserManager::create_user('Tester 2', 'Tester 2', 5, 'tester2@local', $loginName.'t2', 'xXxxX');
|
||||
SessionManager::subscribeUsersToSession($sessionId, [$anotherUserId]);
|
||||
|
||||
// call the webservice to subscribe the first user to the session
|
||||
$subscribed = $this->boolean(['sessionId' => $sessionId, 'loginname' => $loginName]);
|
||||
$this->assertTrue($subscribed);
|
||||
|
||||
// assert we now have two users subscribed to the session
|
||||
$sessionRelUsers = Database::getManager()
|
||||
->getRepository('ChamiloCoreBundle:SessionRelUser')
|
||||
->findBy(['session' => $sessionId]);
|
||||
$this->assertSame(2, count($sessionRelUsers));
|
||||
|
||||
// clean up
|
||||
UserManager::delete_users([$userId, $anotherUserId]);
|
||||
SessionManager::delete($sessionId);
|
||||
}
|
||||
}
|
||||
98
main/webservices/api/tests/UpdateUserFromUsernameTest.php
Normal file
98
main/webservices/api/tests/UpdateUserFromUsernameTest.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/V2TestCase.php';
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* Class UpdateUserFromUsernameTest
|
||||
*
|
||||
* UPDATE_USER_FROM_USERNAME webservice unit tests
|
||||
*/
|
||||
class UpdateUserFromUsernameTest extends V2TestCase
|
||||
{
|
||||
public function action()
|
||||
{
|
||||
return 'update_user_from_username';
|
||||
}
|
||||
|
||||
/**
|
||||
* updates a user
|
||||
* asserts that its data was updated, including extra fields
|
||||
*
|
||||
* @throws Exception if it cannot delete the created test user
|
||||
*/
|
||||
public function test()
|
||||
{
|
||||
// create a user with initial data and extra field values
|
||||
$loginName = 'testUser'.time();
|
||||
$userId = UserManager::create_user(
|
||||
'Initial first name',
|
||||
'Initial last name',
|
||||
5,
|
||||
'initial.email@local',
|
||||
$loginName,
|
||||
'xXxxXxxXX'
|
||||
);
|
||||
|
||||
// create an extra field and initialise its value for the user
|
||||
$extraFieldModel = new ExtraField('user');
|
||||
$extraFieldName = 'extraUserField'.time();
|
||||
$extraFieldId = $extraFieldModel->save(
|
||||
[
|
||||
'field_type' => ExtraField::FIELD_TYPE_TEXT,
|
||||
'variable' => $extraFieldName,
|
||||
'display_text' => $extraFieldName,
|
||||
'visible_to_self' => 1,
|
||||
'visible_to_others' => 1,
|
||||
'changeable' => 1,
|
||||
'filter' => 1,
|
||||
]
|
||||
);
|
||||
SessionManager::update_session_extra_field_value($userId, $extraFieldName, 'extra field initial value');
|
||||
|
||||
// update user with new data and extra field data
|
||||
$newFirstName = 'New first name';
|
||||
$newLastName = 'New last name';
|
||||
$newStatus = 1;
|
||||
$newEmail = 'new.address@local';
|
||||
$parameters = [
|
||||
'firstname' => $newFirstName,
|
||||
'lastname' => $newLastName,
|
||||
'status' => $newStatus,
|
||||
'email' => $newEmail,
|
||||
];
|
||||
$extraFieldNewValue = 'extra field new value';
|
||||
$parameters['extra'] = [
|
||||
['field_name' => $extraFieldName, 'field_value' => $extraFieldNewValue],
|
||||
];
|
||||
$parameters['loginname'] = $loginName;
|
||||
$updated = $this->boolean($parameters);
|
||||
$this->assertTrue($updated);
|
||||
|
||||
// assert the webservice reports an error with a non-existent login name
|
||||
$parameters['loginname'] = 'santaClaus';
|
||||
$this->assertSame(get_lang('UserNotFound'), $this->errorMessageString($parameters));
|
||||
|
||||
// compare each saved value to the original
|
||||
/** @var User $user */
|
||||
$userManager = UserManager::getManager();
|
||||
$user = $userManager->find($userId);
|
||||
$userManager->reloadUser($user);
|
||||
$this->assertSame($newFirstName, $user->getFirstname());
|
||||
$this->assertSame($newLastName, $user->getLastname());
|
||||
$this->assertSame($newStatus, $user->getStatus());
|
||||
$this->assertSame($newEmail, $user->getEmail());
|
||||
|
||||
// assert extra field values have been updated
|
||||
$extraFieldValueModel = new ExtraFieldValue('user');
|
||||
$extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable($userId, $extraFieldName);
|
||||
$this->assertNotFalse($extraFieldValue);
|
||||
$this->assertSame($extraFieldNewValue, $extraFieldValue['value']);
|
||||
|
||||
// clean up
|
||||
UserManager::delete_user($userId);
|
||||
$extraFieldModel->delete($extraFieldId);
|
||||
}
|
||||
}
|
||||
37
main/webservices/api/tests/UpdateUserPauseTrainingTest.php
Normal file
37
main/webservices/api/tests/UpdateUserPauseTrainingTest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/V2TestCase.php';
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* UPDATE_USER_PAUSE_TRAINING webservice unit tests
|
||||
*/
|
||||
class UpdateUserPauseTrainingTest extends V2TestCase
|
||||
{
|
||||
public function action()
|
||||
{
|
||||
return Rest::UPDATE_USER_PAUSE_TRAINING;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a minimal test user
|
||||
* asserts that it was created with the supplied data
|
||||
*
|
||||
* @throws Exception if it cannot delete the created test user
|
||||
*/
|
||||
public function testUpdate()
|
||||
{
|
||||
$params = [
|
||||
'user_id' => 1,
|
||||
'pause_formation' => 0,
|
||||
'start_pause_date' => '2020-06-30 10:00',
|
||||
'end_pause_date' => '2020-06-30 11:00',
|
||||
'disable_emails' => 1,
|
||||
];
|
||||
$userId = $this->integer($params);
|
||||
|
||||
// assert each field was filled with provided information
|
||||
$this->assertSame($userId, 1);
|
||||
}
|
||||
}
|
||||
62
main/webservices/api/tests/UsernameExistTest.php
Normal file
62
main/webservices/api/tests/UsernameExistTest.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/V2TestCase.php';
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* Class UsernameExistTest
|
||||
*
|
||||
* USERNAME_EXIST webservice unit tests
|
||||
*/
|
||||
class UsernameExistTest extends V2TestCase
|
||||
{
|
||||
public function action()
|
||||
{
|
||||
return 'username_exist';
|
||||
}
|
||||
|
||||
/**
|
||||
* test nonexistence of a username which does not exist
|
||||
* assert that the webservice returns false
|
||||
*/
|
||||
public function testUsernameWhichDoesNotExist()
|
||||
{
|
||||
// generate a random name which does not exist in the database
|
||||
do {
|
||||
$loginName = rand();
|
||||
} while (UserManager::get_user_id_from_username($loginName));
|
||||
|
||||
// expect the web service to return false
|
||||
$this->assertFalse($this->boolean(['loginname' => $loginName]));
|
||||
}
|
||||
|
||||
/**
|
||||
* test existence of a username which does exist
|
||||
* assert that the webservice returns true
|
||||
*/
|
||||
public function testUsernameWhichDoesExist()
|
||||
{
|
||||
// generate a random name which does not exist in the database
|
||||
do {
|
||||
$loginName = rand();
|
||||
} while (UserManager::get_user_id_from_username($loginName));
|
||||
|
||||
// create a test user with this login name
|
||||
$userId = UserManager::create_user(
|
||||
$loginName,
|
||||
$loginName,
|
||||
STUDENT,
|
||||
$loginName.'@local',
|
||||
$loginName,
|
||||
$loginName
|
||||
);
|
||||
|
||||
// expect the web service to return true
|
||||
$this->assertTrue($this->boolean(['loginname' => $loginName]));
|
||||
|
||||
// clean up
|
||||
UserManager::delete_users([$userId]);
|
||||
}
|
||||
}
|
||||
194
main/webservices/api/tests/V2TestCase.php
Normal file
194
main/webservices/api/tests/V2TestCase.php
Normal file
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
require_once __DIR__.'/../../../inc/global.inc.php';
|
||||
|
||||
/**
|
||||
* Class V2Test
|
||||
*
|
||||
* Base class for all WebService API v2 tests
|
||||
*/
|
||||
abstract class V2TestCase extends TestCase
|
||||
{
|
||||
const WEBSERVICE_USERNAME = 'admin';
|
||||
const WEBSERVICE_PASSWORD = 'admin';
|
||||
const RELATIVE_URI = 'webservices/api/v2.php';
|
||||
/**
|
||||
* @var Client $client
|
||||
*/
|
||||
private $client;
|
||||
private $apiKey;
|
||||
|
||||
/**
|
||||
* Initialises the HTTP client and retrieves the API key from the server
|
||||
*
|
||||
* @throws Exception when it cannot get the API key
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->client = new Client(
|
||||
[
|
||||
'base_uri' => api_get_path(WEB_CODE_PATH),
|
||||
]
|
||||
);
|
||||
|
||||
$response = $this->client->post(
|
||||
self::RELATIVE_URI,
|
||||
[
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => self::WEBSERVICE_USERNAME,
|
||||
'password' => self::WEBSERVICE_PASSWORD,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if (200 === $response->getStatusCode()) {
|
||||
$decodedResponse = json_decode($response->getBody()->getContents(), false, 3, JSON_THROW_ON_ERROR);
|
||||
|
||||
if (is_object($decodedResponse)) {
|
||||
$this->apiKey = $decodedResponse->data->apiKey;
|
||||
} else {
|
||||
throw new Exception('The returned JSON document is not an object');
|
||||
}
|
||||
} else {
|
||||
throw new Exception($response->getReasonPhrase());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts an action request and assert the server returns an error message
|
||||
*
|
||||
* @param array $parameters parameters to send with the request
|
||||
*
|
||||
* @return string the "message" error string returned by the webservice
|
||||
*/
|
||||
protected function errorMessageString($parameters = [])
|
||||
{
|
||||
$decodedResponse = $this->decodedResponse($parameters);
|
||||
$this->assertIsObject($decodedResponse);
|
||||
$this->assertTrue(property_exists($decodedResponse, 'error'));
|
||||
$error = $decodedResponse->error;
|
||||
$this->assertIsBool(true, $error);
|
||||
$this->assertTrue($error, 'error is not true: '.print_r($decodedResponse, true));
|
||||
$this->assertTrue(property_exists($decodedResponse, 'message'));
|
||||
$message = $decodedResponse->message;
|
||||
$this->assertIsString($message);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts an action request to the web server, asserts it returns a valid JSON-encoded response and decodes it
|
||||
* supplied parameters complete or override the generated base parameters username, api_key and action
|
||||
*
|
||||
* @param array $parameters parameters to send with the request as an associative array
|
||||
*
|
||||
* @return mixed the decoded response (usually an object with properties data, error, message)
|
||||
*/
|
||||
protected function decodedResponse($parameters = [])
|
||||
{
|
||||
$baseParams = [
|
||||
'username' => self::WEBSERVICE_USERNAME,
|
||||
'api_key' => $this->apiKey,
|
||||
'action' => $this->action(),
|
||||
];
|
||||
|
||||
$response = $this->client->post(self::RELATIVE_URI, ['form_params' => array_merge($baseParams, $parameters)]);
|
||||
|
||||
$this->assertNotNull($response);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
|
||||
$decodedResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
// Help debug
|
||||
if (null === $decodedResponse) {
|
||||
var_dump($this->action(), $response->getBody()->getContents());
|
||||
}
|
||||
|
||||
$this->assertNotNull($decodedResponse);
|
||||
|
||||
return $decodedResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the name of the webservice, to be passed as the "action" with the HTTP request
|
||||
*
|
||||
* @return string name of the webservice action to call
|
||||
*/
|
||||
abstract protected function action();
|
||||
|
||||
/**
|
||||
* Posts an action request and assert it returns an integer value in the "data" property
|
||||
*
|
||||
* @param array $parameters parameters to send with the request
|
||||
*
|
||||
* @return integer the integer value
|
||||
*/
|
||||
protected function integer($parameters = [])
|
||||
{
|
||||
$value = $this->singleElementValue($parameters);
|
||||
$this->assertIsInt($value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts an action request and assert the server returns a single value in the "data" array of the returned object
|
||||
*
|
||||
* @param array $parameters parameters to send with the request
|
||||
*
|
||||
* @return mixed the unique element of the "data" array
|
||||
*/
|
||||
protected function singleElementValue($parameters = [])
|
||||
{
|
||||
$data = $this->dataArray($parameters);
|
||||
$this->assertSame(1, count($data));
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts an action request and assert the server returns a "data" array
|
||||
*
|
||||
* @param array $parameters parameters to send with the request
|
||||
*
|
||||
* @return array the "data" array returned by the webservice
|
||||
*/
|
||||
protected function dataArray($parameters = [])
|
||||
{
|
||||
$decodedResponse = $this->decodedResponse($parameters);
|
||||
$this->assertIsObject($decodedResponse);
|
||||
$this->assertTrue(
|
||||
property_exists($decodedResponse, 'data'),
|
||||
'response data property is missing: '.print_r($decodedResponse, true)
|
||||
);
|
||||
|
||||
$data = $decodedResponse->data;
|
||||
$this->assertIsArray($data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts an action request and assert it returns an array with a single boolean value
|
||||
*
|
||||
* @param array $parameters parameters to send with the request
|
||||
*
|
||||
* @return boolean the boolean value
|
||||
*/
|
||||
protected function boolean($parameters = [])
|
||||
{
|
||||
$value = $this->singleElementValue($parameters);
|
||||
$this->assertIsBool($value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
1100
main/webservices/api/v2.php
Normal file
1100
main/webservices/api/v2.php
Normal file
File diff suppressed because it is too large
Load Diff
27
main/webservices/chamilo.py
Normal file
27
main/webservices/chamilo.py
Normal file
@@ -0,0 +1,27 @@
|
||||
# This file can be used as a base to configure OpenERP's web service module
|
||||
# to connect to a Chamilo portal and make use of its SOAP services.
|
||||
# This file is useless here, and should be placed on the OpenERP side after
|
||||
# being correctly configured (change variables below) in order to be put
|
||||
# to good use
|
||||
# @author Gustavo Maggi - Latinux
|
||||
|
||||
## Configuration section - set the variables below to their right values
|
||||
# Chamilo's root directory URL. Replace by your own URL without the trailing /
|
||||
# Examples: http://campus.server.com or http://campus.server.com/chamilo
|
||||
url_root = 'http://192.168.1.1/chamilo'
|
||||
# Get the Chamilo secret API key
|
||||
# can be found in main/inc/conf/configuration.php, around line 115
|
||||
security_key= 'abcdef1234567890abcdef1234567890'
|
||||
|
||||
## Connexion preparation
|
||||
# Connexion - do not change anything here
|
||||
from SOAPpy import SOAPProxy
|
||||
server = SOAPProxy(url_root+'/main/webservices/soap.php' )
|
||||
# Import libraries to calculate the SHA1 hash
|
||||
import hashlib
|
||||
import urllib2 as wget
|
||||
# Get the IP of the current host, as detected by Chamilo (testip.php)
|
||||
my_ip = wget.urlopen(url_root+'/main/webservices/testip.php').readlines()[0][:-1]
|
||||
# Get the secret key - now the Chamilo webservices can be called
|
||||
secret_key = hashlib.sha1(my_ip+security_key).hexdigest()
|
||||
|
||||
380
main/webservices/client_soap.php
Normal file
380
main/webservices/client_soap.php
Normal file
@@ -0,0 +1,380 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/*
|
||||
*
|
||||
* 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;
|
||||
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/access_url.php?wsdl";
|
||||
|
||||
global $_configuration;
|
||||
// see the main/inc/configuration.php file to get this value
|
||||
$security_key = $_configuration['security_key'];
|
||||
|
||||
$client = new nusoap_client($url, true);
|
||||
/*$client->xml_encoding = 'UTF-8';
|
||||
$client->http_encoding = 'UTF-8';
|
||||
$client->charencoding = 'UTF-8';*/
|
||||
|
||||
$soap_error = $client->getError();
|
||||
|
||||
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);
|
||||
|
||||
//Creating a random user_id, this values need to be provided from your system
|
||||
$random_user_id = rand(0, 1000);
|
||||
//Creating a random username this values need to be provided from your system
|
||||
$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 = [
|
||||
'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,
|
||||
// third party user id
|
||||
'original_user_id_value' => $random_user_id,
|
||||
'secret_key' => $secret_key,
|
||||
// Extra fields
|
||||
'extra' => [
|
||||
['field_name' => 'ruc', 'field_value' => '123'],
|
||||
['field_name' => 'DNI', 'field_value' => '4200000'],
|
||||
],
|
||||
];
|
||||
|
||||
//1. Create user webservice
|
||||
$user_id = $client->call(
|
||||
'WSCreateUserPasswordCrypted',
|
||||
['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,
|
||||
];
|
||||
|
||||
$sessionId = $client->call(
|
||||
'WSCreateSession',
|
||||
['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',
|
||||
['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 '<h2>Trying to create an user via webservices</h2>';
|
||||
$original_params = $params;
|
||||
|
||||
$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', ['GetUser' => $params]);
|
||||
|
||||
if ($result) {
|
||||
echo "Random user was created user_id: $user_id <br /><br />";
|
||||
echo 'User info: <br />';
|
||||
print_r($original_params);
|
||||
echo '<br /><br />';
|
||||
} else {
|
||||
echo $result;
|
||||
}
|
||||
|
||||
// 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 <br /><br />";
|
||||
echo 'User info: <br />';
|
||||
print_r($params);
|
||||
echo '<br /><br />';
|
||||
} else {
|
||||
$err = $client->getError();
|
||||
var_dump($result);
|
||||
var_dump($err);
|
||||
}
|
||||
|
||||
$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', ['user_ids' => $params]);
|
||||
|
||||
// Enable user
|
||||
$result = $client->call('WSEnableUsers', ['user_ids' => $params]);
|
||||
|
||||
// 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,
|
||||
];
|
||||
|
||||
$result = $client->call('WSCreateCourse', ['createCourse' => $params]);
|
||||
|
||||
// 5 .Adding user to the course TEST. The course TEST must be created manually in Chamilo
|
||||
echo '<h2>Trying to add user to a course called TEST via webservices</h2>';
|
||||
|
||||
$course_info = api_get_course_info('TEST123');
|
||||
|
||||
if (!empty($course_info)) {
|
||||
$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"';
|
||||
}
|
||||
|
||||
if ($result == 1) {
|
||||
echo "User $user_id was added to course TEST";
|
||||
} else {
|
||||
echo $result;
|
||||
}
|
||||
|
||||
// 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 = [
|
||||
'original_course_id_name' => 'TEST',
|
||||
'original_course_id_value' => 'TEST',
|
||||
];
|
||||
|
||||
$user_array = [
|
||||
'original_user_id_value' => $random_user_id,
|
||||
'original_user_id_name' => $user_field,
|
||||
];
|
||||
$user_courses = [];
|
||||
|
||||
$user_courses[] = [
|
||||
'course_id' => $course_array,
|
||||
'user_id' => $user_array,
|
||||
'status' => '1',
|
||||
];
|
||||
|
||||
$params = [
|
||||
'userscourses' => $user_courses,
|
||||
'secret_key' => $secret_key,
|
||||
];
|
||||
|
||||
$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';
|
||||
}
|
||||
|
||||
// Check for an error
|
||||
$err = $client->getError();
|
||||
|
||||
if ($err) {
|
||||
// Display the error
|
||||
echo '<h2>Constructor error</h2><pre>'.$err.'</pre>';
|
||||
}
|
||||
|
||||
//1. Create user webservice
|
||||
$result = $client->call(
|
||||
'WSGetPortals',
|
||||
['getPortals' => ['secret_key' => $secret_key]]
|
||||
);
|
||||
|
||||
$result = $client->call(
|
||||
'WSAddUserToPortal',
|
||||
['addUserToPortal' => ['user_id' => 1, 'portal_id' => 1, 'secret_key' => $secret_key]]
|
||||
);
|
||||
|
||||
$result = $client->call(
|
||||
'WSGetPortalListFromUser',
|
||||
['getPortalListFromUser' => ['user_id' => 1, 'secret_key' => $secret_key]]
|
||||
);
|
||||
|
||||
$result = $client->call(
|
||||
'WSGetPortalListFromCourse',
|
||||
['getPortalListFromCourse' => ['course_id' => 20, 'secret_key' => $secret_key]]
|
||||
);
|
||||
|
||||
$result = $client->call(
|
||||
'WSAddCourseToPortal',
|
||||
['addCourseToPortal' => ['course_id' => 20, 'portal_id' => 1, 'secret_key' => $secret_key]]
|
||||
);
|
||||
|
||||
$result = $client->call(
|
||||
'WSRemoveUserFromPortal',
|
||||
['removeUserFromPortal' => ['course_id' => 20, 'portal_id' => 1, 'secret_key' => $secret_key]]
|
||||
);
|
||||
var_dump($user_id); exit;
|
||||
|
||||
if ($client->fault) {
|
||||
echo '<h2>Fault</h2><pre>';
|
||||
print_r($result);
|
||||
echo '</pre>';
|
||||
} else {
|
||||
// Check for errors
|
||||
$err = $client->getError();
|
||||
if ($err) {
|
||||
// Display the error
|
||||
echo '<h2>Error</h2><pre>'.$err.'</pre>';
|
||||
} else {
|
||||
// Display the result
|
||||
echo '<h2>There are no errors</h2>';
|
||||
var_dump($result);
|
||||
}
|
||||
}
|
||||
122
main/webservices/cm_soap.php
Normal file
122
main/webservices/cm_soap.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/cm_webservice.php';
|
||||
|
||||
/**
|
||||
* SOAP error handler. Handles an error sending a SOAP fault.
|
||||
*/
|
||||
class WSCMSoapErrorHandler implements WSCMErrorHandler
|
||||
{
|
||||
/**
|
||||
* Handles the error by sending a SOAP fault through the server.
|
||||
*
|
||||
* @param WSError Error to handle
|
||||
*/
|
||||
public function handle($error)
|
||||
{
|
||||
$server = WSCMSoapServer::singleton();
|
||||
$server->fault(strval($error->code), $error->message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SOAP server wrapper implementing a Singleton.
|
||||
*/
|
||||
class WSCMSoapServer
|
||||
{
|
||||
/**
|
||||
* SOAP server instance.
|
||||
*
|
||||
* @var soap_server
|
||||
*/
|
||||
private static $_instance;
|
||||
|
||||
/**
|
||||
* Private constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Singleton method.
|
||||
*/
|
||||
public static function singleton()
|
||||
{
|
||||
if (!isset(self::$_instance)) {
|
||||
self::$_instance = new soap_server();
|
||||
// Set the error handler
|
||||
WSCMError::setErrorHandler(new WSCMSoapErrorHandler());
|
||||
// Configure the service
|
||||
self::$_instance->configureWSDL('WSCMService', 'urn:WSCMService');
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
}
|
||||
|
||||
$s = WSCMSoapServer::singleton();
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'code' => ['name' => 'code', 'type' => 'xsd:int'],
|
||||
'message' => ['name' => 'message', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'extra_field',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'field_name' => ['name' => 'field_name', 'type' => 'xsd:string'],
|
||||
'field_value' => ['name' => 'field_value', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCM.verifyUserPass',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCM.encryptPass',
|
||||
['password' => 'xsd:string'],
|
||||
['return' => 'xsd:string']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCM.test',
|
||||
[],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
''
|
||||
);
|
||||
|
||||
require_once __DIR__.'/cm_soap_inbox.php';
|
||||
require_once __DIR__.'/cm_soap_user.php';
|
||||
require_once __DIR__.'/cm_soap_courses.php';
|
||||
require_once __DIR__.'/cm_soap_announcements.php';
|
||||
require_once __DIR__.'/cm_soap_forum.php';
|
||||
|
||||
// Use the request to (try to) invoke the service
|
||||
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
|
||||
$s->service($HTTP_RAW_POST_DATA);
|
||||
45
main/webservices/cm_soap_announcements.php
Normal file
45
main/webservices/cm_soap_announcements.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/cm_webservice_announcements.php';
|
||||
require_once __DIR__.'/cm_soap.php';
|
||||
|
||||
/**
|
||||
* Configures the WSCourse SOAP service.
|
||||
*/
|
||||
$s = WSCMSoapServer::singleton();
|
||||
|
||||
$s->register(
|
||||
'WSCMAnnouncements.get_announcements_id',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'course_code' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o ID dos anuncios visiveis a um usuario de uma disciplina.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMAnnouncements.get_announcement_data',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'course_code' => 'xsd:string',
|
||||
'announcement_id' => 'xsd:string',
|
||||
'field' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o conteudo do campo informado de um anuncio de chave ID. Campos retornaveis: sender, date, title e content'
|
||||
);
|
||||
290
main/webservices/cm_soap_course.php
Normal file
290
main/webservices/cm_soap_course.php
Normal file
@@ -0,0 +1,290 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/cm_webservice_course.php';
|
||||
require_once __DIR__.'/cm_soap.php';
|
||||
|
||||
/**
|
||||
* Configures the WSCourse SOAP service.
|
||||
*/
|
||||
$s = WSCMSoapServer::singleton();
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_id',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_id_field_name' => ['name' => 'course_id_field_name', 'type' => 'xsd:string'],
|
||||
'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'],
|
||||
'result' => ['name' => 'result', 'type' => 'tns:result'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_result_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course_result[]']],
|
||||
'tns:course_result'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.DeleteCourse',
|
||||
['secret_key' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.DeleteCourses',
|
||||
['secret_key' => 'xsd:string', 'courses' => 'tns:course_id[]'],
|
||||
['return' => 'tns:course_result_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.CreateCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'title' => 'xsd:string',
|
||||
'category_code' => 'xsd:string',
|
||||
'wanted_code' => 'xsd:string',
|
||||
'tutor_name' => 'xsd:string',
|
||||
'course_admin_user_id_field_name' => 'xsd:string',
|
||||
'course_admin_user_id_value' => 'xsd:string',
|
||||
'language' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'extras' => 'tns:extra_field[]',
|
||||
],
|
||||
['return' => 'xsd:int']
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_create',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'title' => ['name' => 'title', 'type' => 'xsd:string'],
|
||||
'category_code' => ['name' => 'category_code', 'type' => 'xsd:string'],
|
||||
'wanted_code' => ['name' => 'wanted_code', 'type' => 'xsd:int'],
|
||||
'tutor_name' => ['name' => 'tutor_name', 'type' => 'xsd:string'],
|
||||
'course_admin_user_id_field_name' => ['name' => 'course_admin_user_id_field_name', 'type' => 'xsd:string'],
|
||||
'course_admin_user_id_value' => ['name' => 'course_admin_user_id_value', 'type' => 'xsd:string'],
|
||||
'language' => ['name' => 'language', 'type' => 'xsd:string'],
|
||||
'course_id_field_name' => ['name' => 'course_id_field_name', 'type' => 'xsd:string'],
|
||||
'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'],
|
||||
'extras' => ['name' => 'extras', 'type' => 'tns:extra_field[]'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_create_result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'],
|
||||
'course_id_generated' => ['name' => 'course_id_generated', 'type' => 'xsd:int'],
|
||||
'result' => ['name' => 'result', 'type' => 'tns:result'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_create_result_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course_create_result[]']],
|
||||
'tns:course_create_result'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.CreateCourses',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'courses' => 'tns:course_create[]',
|
||||
],
|
||||
['return' => 'tns:course_create_result_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.EditCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'title' => 'xsd:string',
|
||||
'category_code' => 'xsd:string',
|
||||
'department_name' => 'xsd:string',
|
||||
'department_url' => 'xsd:string',
|
||||
'language' => 'xsd:string',
|
||||
'visibility' => 'xsd:int',
|
||||
'subscribe' => 'xsd:int',
|
||||
'unsubscribe' => 'xsd:int',
|
||||
'visual_code' => 'xsd:string',
|
||||
'extras' => 'tns:extra_field[]',
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'id' => ['name' => 'id', 'type' => 'xsd:int'],
|
||||
'code' => ['name' => 'code', 'type' => 'xsd:string'],
|
||||
'title' => ['name' => 'title', 'type' => 'xsd:string'],
|
||||
'language' => ['name' => 'language', 'type' => 'xsd:string'],
|
||||
'visibility' => ['name' => 'visibility', 'type' => 'xsd:int'],
|
||||
'category_name' => ['name' => 'category_name', 'type' => 'xsd:string'],
|
||||
'number_students' => ['name' => 'number_students', 'type' => 'xsd:int'],
|
||||
'external_course_id' => ['name' => 'external_course_id', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course[]']],
|
||||
'tns:course'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.ListCourses',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
],
|
||||
['return' => 'tns:course_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.SubscribeUserToCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'status' => 'xsd:int',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.UnsubscribeUserFromCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_description',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_desc_id' => ['name' => 'course_desc_id', 'type' => 'xsd:int'],
|
||||
'course_desc_title' => ['name' => 'course_desc_title', 'type' => 'xsd:string'],
|
||||
'course_desc_content' => ['name' => 'course_desc_content', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_description_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course_description[]']],
|
||||
'tns:course_description'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.GetCourseDescriptions',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
],
|
||||
['return' => 'tns:course_description_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.EditCourseDescription',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'course_desc_id' => 'xsd:int',
|
||||
'course_desc_title' => 'xsd:string',
|
||||
'course_desc_content' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.unreadMessage',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna a quantidade de mensagens nao lidas na caixa de entrada do usuario.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.getIdMessage',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o ID das mensagens.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourse.nada',
|
||||
['username' => 'xsd:string', 'password' => 'xsd:string'],
|
||||
['return' => 'xsd:string']
|
||||
);
|
||||
42
main/webservices/cm_soap_courses.php
Normal file
42
main/webservices/cm_soap_courses.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/cm_webservice_courses.php';
|
||||
require_once __DIR__.'/cm_soap.php';
|
||||
|
||||
/**
|
||||
* Configures the WSCourse SOAP service.
|
||||
*/
|
||||
$s = WSCMSoapServer::singleton();
|
||||
|
||||
$s->register(
|
||||
'WSCMCourses.get_courses_code',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o CODE dos cursos do username.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMCourses.get_course_title',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'course_code' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o titulo/nome do curso de course_code informado'
|
||||
);
|
||||
145
main/webservices/cm_soap_forum.php
Normal file
145
main/webservices/cm_soap_forum.php
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/cm_webservice_forum.php';
|
||||
require_once __DIR__.'/cm_soap.php';
|
||||
|
||||
/**
|
||||
* Configures the WSCourse SOAP service.
|
||||
*/
|
||||
$s = WSCMSoapServer::singleton();
|
||||
|
||||
$s->register(
|
||||
'WSCMForum.get_foruns_id',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'course_code' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o ID dos foruns de uma disciplina.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMForum.get_forum_title',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'course_code' => 'xsd:string',
|
||||
'forum_id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o valor do titulo de um forum_id.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMForum.get_forum_threads_id',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'course_code' => 'xsd:string',
|
||||
'forum_id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o ID das threads de um forum_id.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMForum.get_forum_thread_data',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'course_code' => 'xsd:string',
|
||||
'thread_id' => 'xsd:string',
|
||||
'field' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o campo field de um thread_id. Campos possiveis: title, date, sender, sender_name.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMForum.get_forum_thread_title',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'course_code' => 'xsd:string',
|
||||
'thread_id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o campo title de uma thread_id.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMForum.get_posts_id',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'course_code' => 'xsd:string',
|
||||
'thread_id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o ID dos posts de uma thread.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMForum.get_post_data',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'course_code' => 'xsd:string',
|
||||
'post_id' => 'xsd:string',
|
||||
'field' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o campo field de um post_id. Campos possiveis: title, text, date, sender ou sender_name.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMForum.send_post',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'course_code' => 'xsd:string',
|
||||
'forum_id' => 'xsd:string',
|
||||
'thread_id' => 'xsd:string',
|
||||
'title' => 'xsd:string',
|
||||
'content' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Envia um novo post ao forum_id.'
|
||||
);
|
||||
108
main/webservices/cm_soap_inbox.php
Normal file
108
main/webservices/cm_soap_inbox.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/cm_webservice_inbox.php';
|
||||
require_once __DIR__.'/cm_soap.php';
|
||||
|
||||
/**
|
||||
* Configures the WSCourse SOAP service.
|
||||
*/
|
||||
$s = WSCMSoapServer::singleton();
|
||||
|
||||
$s->register(
|
||||
'WSCMInbox.unreadMessage',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna a quantidade de mensagens nao lidas na caixa de entrada do usuario.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMInbox.get_message_id',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'from' => 'xsd:string',
|
||||
'number_of_items' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o ID das mensagens de entrada entre o intervalo de from até number_of_items.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMInbox.get_message_data',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'id' => 'xsd:string',
|
||||
'field' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o conteudo do campo informado em field da mensagem de entrada id. Os campos retornados sao: sender, title, date, status e content.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMInbox.get_message_id_sent',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'from' => 'xsd:string',
|
||||
'number_of_items' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o ID das mensagens de saida entre o intervalo de from até number_of_items.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMInbox.get_message_data_sent',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'id' => 'xsd:string',
|
||||
'field' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o conteudo do campo informado em field da mensagem de saida id. Os campos retornados sao: sender, title, date, status e content.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMInbox.message_send',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'receiver_user_id' => 'xsd:string',
|
||||
'subject' => 'xsd:string',
|
||||
'content' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Envia uma mensagem via rede social. Retorna o id da mensagem enviada.'
|
||||
);
|
||||
105
main/webservices/cm_soap_user.php
Normal file
105
main/webservices/cm_soap_user.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/cm_webservice_user.php';
|
||||
require_once __DIR__.'/cm_soap.php';
|
||||
|
||||
/**
|
||||
* Configures the WSCourse SOAP service.
|
||||
*/
|
||||
$s = WSCMSoapServer::singleton();
|
||||
|
||||
$s->register(
|
||||
'WSCMUser.find_id_user',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'name' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o id de um usuario que contenha o parametro \'nome\' nos campos nome, sobrenome ou email (ordenado por nome).'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMUser.get_user_name',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'id' => 'xsd:string',
|
||||
'field' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o primeiro, ultimo ou os dois nomes de um usuarios. No campo field deve ser informado firstname, lastname, bothfl (para fistname lastname) ou bothlf (para lastname firstname)'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMUser.get_link_user_picture',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Retorna o link para a imagem do perfil do usuario.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMUser.send_invitation',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'userfriend_id' => 'xsd:string',
|
||||
'content_message' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Envia um convite para estabelecer amizado no portal. O campo userfriend_id o id do possivel amigo e o campo content_message e a mensagem de solicitacao.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMUser.accept_friend',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'userfriend_id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Aceita o convite realizado pelo userfriend_id.'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCMUser.denied_invitation',
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'userfriend_id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string'],
|
||||
'urn:WSCMService',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'Recusa o contive de amizade feito pelo usuario userfriend_id.'
|
||||
);
|
||||
322
main/webservices/cm_webservice.php
Normal file
322
main/webservices/cm_webservice.php
Normal file
@@ -0,0 +1,322 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
use Chamilo\UserBundle\Entity\User;
|
||||
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
|
||||
api_protect_webservices();
|
||||
|
||||
/**
|
||||
* Error returned by one of the methods of the web service. Contains an error code and an error message.
|
||||
*/
|
||||
class WSCMError
|
||||
{
|
||||
/**
|
||||
* Error code.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $code;
|
||||
|
||||
/**
|
||||
* Error message.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $message;
|
||||
/**
|
||||
* Error handler. This needs to be a class that implements the interface WSErrorHandler.
|
||||
*
|
||||
* @var WSErrorHandler
|
||||
*/
|
||||
protected static $_handler;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int Error code
|
||||
* @param string Error message
|
||||
*/
|
||||
public function __construct($code, $message)
|
||||
{
|
||||
$this->code = $code;
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the error handler.
|
||||
*
|
||||
* @param WSErrorHandler $handler Error handler
|
||||
*/
|
||||
public static function setErrorHandler($handler)
|
||||
{
|
||||
if ($handler instanceof WSErrorHandler) {
|
||||
self::$_handler = $handler;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the error handler.
|
||||
*
|
||||
* @return WSErrorHandler Error handler
|
||||
*/
|
||||
public static function getErrorHandler()
|
||||
{
|
||||
return self::$_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the error into an array.
|
||||
*
|
||||
* @return array Associative array with code and message
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return ['code' => $this->code, 'message' => $this->message];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface that must be implemented by any error handler.
|
||||
*/
|
||||
interface WSCMErrorHandler
|
||||
{
|
||||
/**
|
||||
* Handle method.
|
||||
*
|
||||
* @param WSError $error Error
|
||||
*/
|
||||
public function handle($error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main class of the webservice. Webservice classes extend this class.
|
||||
*/
|
||||
class WSCM
|
||||
{
|
||||
/**
|
||||
* Chamilo configuration.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_configuration;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->_configuration = $GLOBALS['_configuration'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies if the user is valid.
|
||||
*
|
||||
* @param string $username of the user in chamilo
|
||||
* @param string $pass of the same user (in MD5 of SHA)
|
||||
*
|
||||
* @return mixed "valid" if username e password are correct! Else, return a message error
|
||||
*/
|
||||
public function verifyUserPass($username, $pass)
|
||||
{
|
||||
$login = $username;
|
||||
$password = $pass;
|
||||
|
||||
$userRepo = UserManager::getRepository();
|
||||
/** @var User $uData */
|
||||
$uData = $userRepo->findOneBy([
|
||||
'username' => trim(addslashes($login)),
|
||||
]);
|
||||
|
||||
if ($uData) {
|
||||
if ($uData->getAuthSource() == PLATFORM_AUTH_SOURCE) {
|
||||
$passwordEncoded = UserManager::encryptPassword($password, $uData);
|
||||
// Check the user's password
|
||||
if ($passwordEncoded == $uData->getPassword() && (trim($login) == $uData->getUsername())) {
|
||||
// Check if the account is active (not locked)
|
||||
if ($uData->getActive()) {
|
||||
// Check if the expiration date has not been reached
|
||||
$now = new DateTime();
|
||||
if ($uData->getExpirationDate() > $now || !$uData->getExpirationDate()) {
|
||||
return "valid";
|
||||
} else {
|
||||
return get_lang('AccountExpired');
|
||||
}
|
||||
} else {
|
||||
return get_lang('AccountInactive');
|
||||
}
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
} else {
|
||||
return get_lang('AccountURLInactive');
|
||||
}
|
||||
}
|
||||
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function. Returns the string success.
|
||||
*
|
||||
* @return string Success
|
||||
*/
|
||||
public function test()
|
||||
{
|
||||
return "success";
|
||||
}
|
||||
|
||||
/**
|
||||
* *Strictly* reverts PHP's nl2br() effects (whether it was used in XHTML mode or not).
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function nl2br_revert($string)
|
||||
{
|
||||
return preg_replace('`<br(?: /)?>([\\n\\r])`', '$1', $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the API key.
|
||||
*
|
||||
* @param string $secret_key Secret key
|
||||
*
|
||||
* @return mixed WSError in case of failure, null in case of success
|
||||
*/
|
||||
protected function verifyKey($secret_key)
|
||||
{
|
||||
$ip = trim($_SERVER['REMOTE_ADDR']);
|
||||
// if we are behind a reverse proxy, assume it will send the
|
||||
// HTTP_X_FORWARDED_FOR header and use this IP instead
|
||||
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||
list($ip1, $ip2) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
||||
$ip = trim($ip1);
|
||||
}
|
||||
$security_key = $ip.$this->_configuration['security_key'];
|
||||
|
||||
if (!api_is_valid_secret_key($secret_key, $security_key)) {
|
||||
return new WSCMError(1, "API key is invalid");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the real user id based on the user id field name and value.
|
||||
* Note that if the user id field name is "chamilo_user_id", it will use the user id
|
||||
* in the system database.
|
||||
*
|
||||
* @param string $user_id_field_name User id field name
|
||||
* @param string $user_id_value User id value
|
||||
*
|
||||
* @return mixed System user id if the user was found, WSError otherwise
|
||||
*/
|
||||
protected function getUserId($user_id_field_name, $user_id_value)
|
||||
{
|
||||
if ($user_id_field_name == "chamilo_user_id") {
|
||||
if (UserManager::is_user_id_valid(intval($user_id_value))) {
|
||||
return intval($user_id_value);
|
||||
} else {
|
||||
return new WSCMError(100, "User not found");
|
||||
}
|
||||
} else {
|
||||
$user_id = UserManager::get_user_id_from_original_id(
|
||||
$user_id_value,
|
||||
$user_id_field_name
|
||||
);
|
||||
if ($user_id == 0) {
|
||||
return new WSCMError(100, "User not found");
|
||||
} else {
|
||||
return $user_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the real course id based on the course id field name and value.
|
||||
* Note that if the course id field name is "chamilo_course_id", it will use the course id
|
||||
* in the system database.
|
||||
*
|
||||
* @param string $course_id_field_name Course id field name
|
||||
* @param string $course_id_value Course id value
|
||||
*
|
||||
* @return mixed System course id if the course was found, WSError otherwise
|
||||
*/
|
||||
protected function getCourseId($course_id_field_name, $course_id_value)
|
||||
{
|
||||
if ($course_id_field_name == "chamilo_course_id") {
|
||||
if (CourseManager::get_course_code_from_course_id($course_id_value) != null) {
|
||||
return intval($course_id_value);
|
||||
} else {
|
||||
return new WSCMError(200, "Course not found");
|
||||
}
|
||||
} else {
|
||||
$courseId = CourseManager::get_course_code_from_original_id(
|
||||
$course_id_value,
|
||||
$course_id_field_name
|
||||
);
|
||||
if (empty($courseId)) {
|
||||
return new WSCMError(200, "Course not found");
|
||||
} else {
|
||||
return $courseId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the real session id based on the session id field name and value.
|
||||
* Note that if the session id field name is "chamilo_session_id", it will use the session id
|
||||
* in the system database.
|
||||
*
|
||||
* @param string $session_id_field_name Session id field name
|
||||
* @param string $session_id_value Session id value
|
||||
*
|
||||
* @return mixed System session id if the session was found, WSError otherwise
|
||||
*/
|
||||
protected function getSessionId($session_id_field_name, $session_id_value)
|
||||
{
|
||||
if ($session_id_field_name == "chamilo_session_id") {
|
||||
$session = SessionManager::fetch((int) $session_id_value);
|
||||
if (!empty($session)) {
|
||||
return intval($session_id_value);
|
||||
} else {
|
||||
return new WSCMError(300, "Session not found");
|
||||
}
|
||||
} else {
|
||||
$session_id = SessionManager::getSessionIdFromOriginalId(
|
||||
$session_id_value,
|
||||
$session_id_field_name
|
||||
);
|
||||
if ($session_id == 0) {
|
||||
return new WSCMError(300, "Session not found");
|
||||
} else {
|
||||
return $session_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles an error by calling the WSError error handler.
|
||||
*
|
||||
* @param WSError $error Error
|
||||
*/
|
||||
protected function handleError($error)
|
||||
{
|
||||
$handler = WSCMError::getErrorHandler();
|
||||
$handler->handle($error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a successful result.
|
||||
*
|
||||
* @return array Array with a code of 0 and a message 'Operation was successful'
|
||||
*/
|
||||
protected function getSuccessfulResult()
|
||||
{
|
||||
return ['code' => 0, 'message' => 'Operation was successful'];
|
||||
}
|
||||
}
|
||||
201
main/webservices/cm_webservice_announcements.php
Normal file
201
main/webservices/cm_webservice_announcements.php
Normal file
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/cm_webservice.php';
|
||||
|
||||
/**
|
||||
* Description of cm_soap_inbox.
|
||||
*
|
||||
* @author marcosousa
|
||||
*/
|
||||
class WSCMAnnouncements extends WSCM
|
||||
{
|
||||
public function get_announcements_id($username, $password, $course_code)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$result = self::get_announcements($username, $course_code);
|
||||
|
||||
$announcements = "#";
|
||||
while ($announcement = Database::fetch_array($result)) {
|
||||
$announcements .= $announcement['id']."#";
|
||||
}
|
||||
|
||||
return $announcements;
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function get_announcement_data(
|
||||
$username,
|
||||
$password,
|
||||
$course_code,
|
||||
$announcement_id,
|
||||
$field
|
||||
) {
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$htmlcode = false;
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
|
||||
$result = self::get_announcements(
|
||||
$username,
|
||||
$course_code,
|
||||
$announcement_id
|
||||
);
|
||||
while ($announcement = Database::fetch_array($result)) {
|
||||
$announcements[] = $announcement;
|
||||
}
|
||||
|
||||
switch ($field) {
|
||||
case 'sender':
|
||||
$field_table = "insert_user_id";
|
||||
$sender = api_get_user_info(
|
||||
$announcements[0][$field_table]
|
||||
);
|
||||
$announcements[0][$field_table] = $sender['firstname']." ".$sender['lastname'];
|
||||
break;
|
||||
case 'title':
|
||||
$htmlcode = true;
|
||||
$field_table = "title";
|
||||
break;
|
||||
case 'date':
|
||||
$field_table = "end_date";
|
||||
break;
|
||||
case 'content':
|
||||
$htmlcode = true;
|
||||
$field_table = "content";
|
||||
$announcements[0][$field_table] = nl2br_revert(
|
||||
$announcements[0][$field_table]
|
||||
);
|
||||
break;
|
||||
default:
|
||||
$field_table = "title";
|
||||
}
|
||||
|
||||
return (htmlcode) ? html_entity_decode(
|
||||
$announcements[0][$field_table]
|
||||
) : $announcements[0][$field_table];
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
private function get_announcements(
|
||||
$username,
|
||||
$course_code,
|
||||
$announcement_id = 0
|
||||
) {
|
||||
$session_id = api_get_session_id();
|
||||
$condition_session = api_get_session_condition($session_id);
|
||||
|
||||
$announcement_id = ($announcement_id == 0) ? "" : "AND announcement.id=".$announcement_id;
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
$course_info = CourseManager::get_course_information($course_code);
|
||||
$tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
|
||||
$tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
|
||||
$maximum = '12';
|
||||
|
||||
$group_memberships = GroupManager::get_group_ids(
|
||||
$course_info['real_id'],
|
||||
$user_id
|
||||
);
|
||||
|
||||
if (api_get_group_id() == 0) {
|
||||
$cond_user_id = " AND (
|
||||
ip.to_user_id='".$user_id."' OR
|
||||
ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR
|
||||
ip.to_group_id IS NULL
|
||||
) ";
|
||||
} else {
|
||||
$cond_user_id = " AND (
|
||||
ip.to_user_id='".$user_id."' OR
|
||||
ip.to_group_id IN (0, ".api_get_group_id().") OR
|
||||
ip.to_group_id IS NULL
|
||||
) ";
|
||||
}
|
||||
|
||||
// the user is member of several groups => display personal
|
||||
// announcements AND his group announcements AND the general announcements
|
||||
if (is_array($group_memberships) && count($group_memberships) > 0) {
|
||||
$sql = "SELECT
|
||||
announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
|
||||
FROM $tbl_announcement announcement, $tbl_item_property ip
|
||||
WHERE
|
||||
announcement.id = ip.ref AND
|
||||
ip.tool='announcement' AND
|
||||
ip.visibility='1'
|
||||
$announcement_id
|
||||
$cond_user_id
|
||||
$condition_session
|
||||
GROUP BY ip.ref
|
||||
ORDER BY display_order DESC
|
||||
LIMIT 0,$maximum";
|
||||
} else {
|
||||
// the user is not member of any group
|
||||
// this is an identified user => show the general announcements AND his personal announcements
|
||||
if ($user_id) {
|
||||
if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
|
||||
$cond_user_id = " AND (
|
||||
ip.lastedit_user_id = '".api_get_user_id()."' OR
|
||||
( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
|
||||
) ";
|
||||
} else {
|
||||
$cond_user_id = " AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) ";
|
||||
}
|
||||
|
||||
$sql = "SELECT
|
||||
announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
|
||||
FROM $tbl_announcement announcement, $tbl_item_property ip
|
||||
WHERE
|
||||
announcement.id = ip.ref AND
|
||||
ip.tool='announcement' AND
|
||||
ip.visibility='1'
|
||||
$announcement_id
|
||||
$cond_user_id
|
||||
$condition_session
|
||||
GROUP BY ip.ref
|
||||
ORDER BY display_order DESC
|
||||
LIMIT 0,$maximum";
|
||||
} else {
|
||||
if (api_get_course_setting('allow_user_edit_announcement')) {
|
||||
$cond_user_id = " AND (
|
||||
ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL
|
||||
) ";
|
||||
} else {
|
||||
$cond_user_id = " AND ip.to_group_id='0' ";
|
||||
}
|
||||
|
||||
// the user is not identiefied => show only the general announcements
|
||||
$sql = "SELECT
|
||||
announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
|
||||
FROM $tbl_announcement announcement, $tbl_item_property ip
|
||||
WHERE announcement.id = ip.ref
|
||||
AND ip.tool='announcement'
|
||||
AND ip.visibility='1'
|
||||
AND ip.to_group_id='0'
|
||||
$announcement_id
|
||||
$condition_session
|
||||
GROUP BY ip.ref
|
||||
ORDER BY display_order DESC
|
||||
LIMIT 0,$maximum";
|
||||
}
|
||||
}
|
||||
|
||||
$result = Database::query($sql);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
echo "aqui: ";
|
||||
$aqui = new WSCMAnnouncements();
|
||||
echo "<pre>";
|
||||
//print_r($aqui->unreadMessage("aluno", "e695f51fe3dd6b7cf2be3188a614f10f"));
|
||||
print_r($aqui->get_announcement_data("aluno", "c4ca4238a0b923820dcc509a6f75849b", "P0204", "17", "title"));
|
||||
echo "</pre>";
|
||||
*/
|
||||
708
main/webservices/cm_webservice_course.php
Normal file
708
main/webservices/cm_webservice_course.php
Normal file
@@ -0,0 +1,708 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/cm_webservice.php';
|
||||
|
||||
/**
|
||||
* Web services available for the Course module. This class extends the WS class.
|
||||
*/
|
||||
class WSCMCourse extends WSCM
|
||||
{
|
||||
/**
|
||||
* Deletes a course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*/
|
||||
public function DeleteCourse(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->deleteCourseHelper(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes multiple courses.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param array Array of courses with elements of the form
|
||||
* array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value')
|
||||
*
|
||||
* @return array Array with elements like
|
||||
* array('course_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
|
||||
* Note that if the result array contains a code different
|
||||
* than 0, an error occured
|
||||
*/
|
||||
public function DeleteCourses($secret_key, $courses)
|
||||
{
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$results = [];
|
||||
foreach ($courses as $course) {
|
||||
$result_tmp = [];
|
||||
$result_op = $this->deleteCourseHelper($course['course_id_field_name'], $course['course_id_value']);
|
||||
$result_tmp['course_id_value'] = $course['course_id_value'];
|
||||
if ($result_op instanceof WSCMError) {
|
||||
// Return the error in the results
|
||||
$result_tmp['result'] = $result_op->toArray();
|
||||
} else {
|
||||
$result_tmp['result'] = $this->getSuccessfulResult();
|
||||
}
|
||||
$results[] = $result_tmp;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Title
|
||||
* @param string Category code
|
||||
* @param string Wanted code. If it's not defined, it will be generated automatically
|
||||
* @param string Tutor name
|
||||
* @param string Course admin user id field name
|
||||
* @param string Course admin user id value
|
||||
* @param string Course language
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param array Course extra fields
|
||||
*
|
||||
* @return int Course id generated
|
||||
*/
|
||||
public function CreateCourse(
|
||||
$secret_key,
|
||||
$title,
|
||||
$category_code,
|
||||
$wanted_code,
|
||||
$tutor_name,
|
||||
$course_admin_user_id_field_name,
|
||||
$course_admin_user_id_value,
|
||||
$language,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$extras
|
||||
) {
|
||||
// First, verify the secret key
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->createCourseHelper(
|
||||
$title,
|
||||
$category_code,
|
||||
$wanted_code,
|
||||
$tutor_name,
|
||||
$course_admin_user_id_field_name,
|
||||
$course_admin_user_id_value,
|
||||
$language,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$extras
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
} else {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create multiple courses.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param array Courses to be created, with elements following the structure presented in CreateCourse
|
||||
*
|
||||
* @return array Array with elements of the form
|
||||
* array('course_id_value' => 'original value sent', 'course_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful'))
|
||||
*/
|
||||
public function CreateCourses($secret_key, $courses)
|
||||
{
|
||||
// First, verify the secret key
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSCMError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$results = [];
|
||||
foreach ($courses as $course) {
|
||||
$result_tmp = [];
|
||||
//reinitialize variables just in case
|
||||
$title = $category_code = $wanted_code = $tutor_name = $course_admin_user_id_field_name = $course_admin_user_id_value = $language = $course_id_field_name = $course_id_value = $extras = null;
|
||||
extract($course);
|
||||
$result = $this->createCourseHelper(
|
||||
$title,
|
||||
$category_code,
|
||||
$wanted_code,
|
||||
$tutor_name,
|
||||
$course_admin_user_id_field_name,
|
||||
$course_admin_user_id_value,
|
||||
$language,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$extras
|
||||
);
|
||||
if ($result instanceof WSCMError) {
|
||||
$result_tmp['result'] = $result->toArray();
|
||||
$result_tmp['course_id_value'] = $course_id_value;
|
||||
$result_tmp['course_id_generated'] = 0;
|
||||
} else {
|
||||
$result_tmp['result'] = $this->getSuccessfulResult();
|
||||
$result_tmp['course_id_value'] = $course_id_value;
|
||||
$result_tmp['course_id_generated'] = $result;
|
||||
}
|
||||
$results[] = $result_tmp;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits a course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param string Title
|
||||
* @param string Category code
|
||||
* @param string Department name
|
||||
* @param string Department url
|
||||
* @param string Course language
|
||||
* @param int Visibility
|
||||
* @param int Subscribe (0 = denied, 1 = allowed)
|
||||
* @param int Unsubscribe (0 = denied, 1 = allowed)
|
||||
* @param string Visual code
|
||||
* @param array Course extra fields
|
||||
*/
|
||||
public function EditCourse(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$title,
|
||||
$category_code,
|
||||
$department_name,
|
||||
$department_url,
|
||||
$language,
|
||||
$visibility,
|
||||
$subscribe,
|
||||
$unsubscribe,
|
||||
$visual_code,
|
||||
$extras
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSCMError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->editCourseHelper(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$title,
|
||||
$category_code,
|
||||
$department_name,
|
||||
$department_url,
|
||||
$language,
|
||||
$visibility,
|
||||
$subscribe,
|
||||
$unsubscribe,
|
||||
$visual_code,
|
||||
$extras
|
||||
);
|
||||
if ($result instanceof WSCMError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List courses.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name. Use "chamilo_course_id" to use internal id
|
||||
*
|
||||
* @return array An array with elements of the form
|
||||
* ('id' => 'Course internal id', 'code' => 'Course code', 'title' => 'Course title', 'language' => 'Course language', 'visibility' => 'Course visibility',
|
||||
* 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id')
|
||||
*/
|
||||
public function ListCourses($secret_key, $course_id_field_name)
|
||||
{
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$courses_result = [];
|
||||
$category_names = [];
|
||||
|
||||
$courses = CourseManager::get_courses_list();
|
||||
foreach ($courses as $course) {
|
||||
$course_tmp = [];
|
||||
$course_tmp['id'] = $course['id'];
|
||||
$course_tmp['code'] = $course['code'];
|
||||
$course_tmp['title'] = $course['title'];
|
||||
$course_tmp['language'] = $course['course_language'];
|
||||
$course_tmp['visibility'] = $course['visibility'];
|
||||
|
||||
// Determining category name
|
||||
if ($category_names[$course['category_code']]) {
|
||||
$course_tmp['category_name'] = $category_names[$course['category_code']];
|
||||
} else {
|
||||
$category = CourseManager::get_course_category($course['category_code']);
|
||||
$category_names[$course['category_code']] = $category['name'];
|
||||
$course_tmp['category_name'] = $category['name'];
|
||||
}
|
||||
|
||||
// Determining number of students registered in course
|
||||
$user_list = CourseManager::get_user_list_from_course_code($course['code']);
|
||||
$course_tmp['number_students'] = count($user_list);
|
||||
|
||||
// Determining external course id
|
||||
$course_tmp['external_course_id'] = CourseManager::get_course_extra_field_value($course_id_field_name, $course['code']);
|
||||
$courses_result[] = $course_tmp;
|
||||
}
|
||||
|
||||
return $courses_result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe user to a course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name. Use "chamilo_course_id" to use internal id
|
||||
* @param string course id value
|
||||
* @param string User id field name. Use "chamilo_user_id" to use internal id
|
||||
* @param string User id value
|
||||
* @param int Status (1 = Teacher, 5 = Student)
|
||||
*/
|
||||
public function SubscribeUserToCourse(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$status
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeUserSubscription(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
1,
|
||||
$status
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsusbscribe user from course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name. Use "chamilo_course_id" to use internal id
|
||||
* @param string course id value
|
||||
* @param string User id field name. Use "chamilo_user_id" to use internal id
|
||||
* @param string User id value
|
||||
*/
|
||||
public function UnsubscribeUserFromCourse(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$user_id_field_name,
|
||||
$user_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeUserSubscription(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
0
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the descriptions of a course, along with their id.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*
|
||||
* @return array Returns an array with elements of the form
|
||||
* array('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content')
|
||||
*/
|
||||
public function GetCourseDescriptions(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$course_id = $this->getCourseId($course_id_field_name, $course_id_value);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
// Course exists, get its descriptions
|
||||
$descriptions = CourseDescription::get_descriptions($course_id);
|
||||
$results = [];
|
||||
foreach ($descriptions as $description) {
|
||||
$results[] = ['course_desc_id' => $description->get_description_type(),
|
||||
'course_desc_title' => $description->get_title(),
|
||||
'course_desc_content' => $description->get_content(), ];
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit course description.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param int Category id from course description
|
||||
* @param string Description title
|
||||
* @param string Course description content
|
||||
*/
|
||||
public function EditCourseDescription(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$course_desc_id,
|
||||
$course_desc_title,
|
||||
$course_desc_content
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$course_id = $this->getCourseId($course_id_field_name, $course_id_value);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
// Create the new course description
|
||||
$cd = new CourseDescription();
|
||||
$cd->set_description_type($course_desc_id);
|
||||
$cd->set_title($course_desc_title);
|
||||
$cd->set_content($course_desc_content);
|
||||
$cd->set_session_id(0);
|
||||
|
||||
// Get course info
|
||||
$course_info = CourseManager::get_course_information(
|
||||
CourseManager::get_course_code_from_course_id($course_id)
|
||||
);
|
||||
// Check if this course description exists
|
||||
$descriptions = CourseDescription::get_descriptions($course_id);
|
||||
$exists = false;
|
||||
foreach ($descriptions as $description) {
|
||||
if ($description->get_description_type() == $course_desc_id) {
|
||||
$exists = true;
|
||||
}
|
||||
}
|
||||
$cd->set_course_id($course_info['real_id']);
|
||||
if (!$exists) {
|
||||
$cd->set_progress(0);
|
||||
$cd->insert();
|
||||
} else {
|
||||
$cd->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function unreadMessage($username, $password)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$table_message = Database::get_main_table(TABLE_MESSAGE);
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
$condition_msg_status = ' msg_status = 1 '; // define('MESSAGE_STATUS_UNREAD', '1');
|
||||
|
||||
$sql_query = "SELECT COUNT(*) as number_messages
|
||||
FROM $table_message
|
||||
WHERE $condition_msg_status AND user_receiver_id=".$user_id;
|
||||
|
||||
$sql_result = Database::query($sql_query);
|
||||
$result = Database::fetch_array($sql_result);
|
||||
|
||||
return $result['number_messages'];
|
||||
}
|
||||
|
||||
return "0";
|
||||
}
|
||||
|
||||
public function get_message_data($username, $password)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$user_id = get_user_id_from_username($username);
|
||||
}
|
||||
}
|
||||
|
||||
public function nada($username, $password)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
return $username.$password;
|
||||
}
|
||||
|
||||
return $username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a course (helper method).
|
||||
*
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*
|
||||
* @return mixed True if the course was successfully deleted, WSError otherwise
|
||||
*/
|
||||
protected function deleteCourseHelper(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
) {
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSCMError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$course_code = CourseManager::get_course_code_from_course_id(
|
||||
$course_id
|
||||
);
|
||||
CourseManager::delete_course($course_code);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a course (helper method).
|
||||
*
|
||||
* @param string Title
|
||||
* @param string Category code
|
||||
* @param string Wanted code. If it's not defined, it will be generated automatically
|
||||
* @param string Tutor name
|
||||
* @param string Course admin user id field name
|
||||
* @param string Course admin user id value
|
||||
* @param string Course language
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param array Course extra fields
|
||||
*
|
||||
* @return mixed Generated id if creation was successful, WSError otherwise
|
||||
*/
|
||||
protected function createCourseHelper(
|
||||
$title,
|
||||
$category_code,
|
||||
$wanted_code,
|
||||
$tutor_name,
|
||||
$course_admin_user_id_field_name,
|
||||
$course_admin_user_id_value,
|
||||
$language,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$extras
|
||||
) {
|
||||
// Add the original course id field name and value to the extra fields if needed
|
||||
$extras_associative = [];
|
||||
if ($course_id_field_name != "chamilo_course_id") {
|
||||
$extras_associative[$course_id_field_name] = $course_id_value;
|
||||
}
|
||||
foreach ($extras as $extra) {
|
||||
$extras_associative[$extra['field_name']] = $extra['field_value'];
|
||||
}
|
||||
$course_admin_id = $this->getUserId($course_admin_user_id_field_name, $course_admin_user_id_value);
|
||||
if ($course_admin_id instanceof WSError) {
|
||||
return $course_admin_id;
|
||||
}
|
||||
if ($wanted_code == '') {
|
||||
$wanted_code = CourseManager::generate_course_code($title);
|
||||
}
|
||||
$result = create_course(
|
||||
$wanted_code,
|
||||
$title,
|
||||
$tutor_name,
|
||||
$category_code,
|
||||
$language,
|
||||
$course_admin_id,
|
||||
$this->_configuration['db_prefix'],
|
||||
0
|
||||
);
|
||||
if (!$result) {
|
||||
return new WSError(202, 'There was an error creating the course');
|
||||
} else {
|
||||
// Update extra fields
|
||||
foreach ($extras_associative as $fname => $fvalue) {
|
||||
CourseManager::update_course_extra_field_value($result, $fname, $fvalue);
|
||||
}
|
||||
// Get course id
|
||||
$course_info = CourseManager::get_course_information($result);
|
||||
|
||||
return $course_info['real_id'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits a course (helper method).
|
||||
*
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param string Title
|
||||
* @param string Category code
|
||||
* @param string Department name
|
||||
* @param string Department url
|
||||
* @param string Course language
|
||||
* @param int Visibility
|
||||
* @param int Subscribe (0 = denied, 1 = allowed)
|
||||
* @param int Unsubscribe (0 = denied, 1 = allowed)
|
||||
* @param string Visual code
|
||||
* @param array Course extra fields
|
||||
*
|
||||
* @return mixed True in case of success, WSError otherwise
|
||||
*/
|
||||
protected function editCourseHelper(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$title,
|
||||
$category_code,
|
||||
$department_name,
|
||||
$department_url,
|
||||
$language,
|
||||
$visibility,
|
||||
$subscribe,
|
||||
$unsubscribe,
|
||||
$visual_code,
|
||||
$extras
|
||||
) {
|
||||
$course_id = $this->getCourseId($course_id_field_name, $course_id_value);
|
||||
if ($course_id instanceof WSCMError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$attributes = [];
|
||||
if (!empty($title)) {
|
||||
$attributes['title'] = $title;
|
||||
}
|
||||
if (!empty($category_code)) {
|
||||
$attributes['category_code'] = $category_code;
|
||||
}
|
||||
if (!empty($department_name)) {
|
||||
$attributes['department_name'] = $department_name;
|
||||
}
|
||||
if (!empty($department_url)) {
|
||||
$attributes['department_url'] = $department_url;
|
||||
}
|
||||
if (!empty($language)) {
|
||||
$attributes['course_language'] = $language;
|
||||
}
|
||||
if ($visibility != '') {
|
||||
$attributes['visibility'] = (int) $visibility;
|
||||
}
|
||||
if ($subscribe != '') {
|
||||
$attributes['subscribe'] = (int) $subscribe;
|
||||
}
|
||||
if ($unsubscribe != '') {
|
||||
$attributes['unsubscribe'] = (int) $unsubscribe;
|
||||
}
|
||||
if (!empty($visual_code)) {
|
||||
$attributes['visual_code'] = $visual_code;
|
||||
}
|
||||
if (!empty($attributes)) {
|
||||
CourseManager::update_attributes($course_id, $attributes);
|
||||
}
|
||||
if (!empty($extras)) {
|
||||
$course_code = CourseManager::get_course_code_from_course_id($course_id);
|
||||
$extras_associative = [];
|
||||
foreach ($extras as $extra) {
|
||||
$extras_associative[$extra['field_name']] = $extra['field_value'];
|
||||
}
|
||||
foreach ($extras_associative as $fname => $fvalue) {
|
||||
CourseManager::update_extra_field_value($course_code, $fname, $fvalue);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe or unsubscribe user to a course (helper method).
|
||||
*
|
||||
* @param string Course id field name. Use "chamilo_course_id" to use internal id
|
||||
* @param string course id value
|
||||
* @param string User id field name. Use "chamilo_user_id" to use internal id
|
||||
* @param string User id value
|
||||
* @param int Set to 1 to subscribe, 0 to unsubscribe
|
||||
* @param int Status (STUDENT or TEACHER) Used for subscription only
|
||||
*
|
||||
* @return mixed True if subscription or unsubscription was successful, false otherwise
|
||||
*/
|
||||
protected function changeUserSubscription(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$state,
|
||||
$status = STUDENT
|
||||
) {
|
||||
$course_id = $this->getCourseId($course_id_field_name, $course_id_value);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
} else {
|
||||
$course_code = CourseManager::get_course_code_from_course_id($course_id);
|
||||
if ($state == 0) {
|
||||
// Unsubscribe user
|
||||
CourseManager::unsubscribe_user($user_id, $course_code);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
// Subscribe user
|
||||
if (CourseManager::subscribeUser($user_id, $course_code, $status)) {
|
||||
return true;
|
||||
} else {
|
||||
return new WSError(203, 'An error occured subscribing to this course');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
44
main/webservices/cm_webservice_courses.php
Normal file
44
main/webservices/cm_webservice_courses.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/cm_webservice.php';
|
||||
|
||||
/**
|
||||
* Description of cm_soap_inbox.
|
||||
*
|
||||
* @author marcosousa
|
||||
*/
|
||||
class WSCMCourses extends WSCM
|
||||
{
|
||||
public function get_courses_code($username, $password)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
$listOfCourses = UserManager::get_personal_session_course_list($user_id);
|
||||
|
||||
$courses_id = "#";
|
||||
foreach ($listOfCourses as $course) {
|
||||
$courses_id .= $course['code']."#";
|
||||
}
|
||||
|
||||
return $courses_id;
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function get_course_title($username, $password, $course_code)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$course_info = CourseManager::get_course_information($course_code);
|
||||
|
||||
return $course_info['title'];
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
}
|
||||
306
main/webservices/cm_webservice_forum.php
Normal file
306
main/webservices/cm_webservice_forum.php
Normal file
@@ -0,0 +1,306 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/../forum/forumfunction.inc.php';
|
||||
require_once __DIR__.'/cm_webservice.php';
|
||||
|
||||
/**
|
||||
* Description of cm_soap_inbox.
|
||||
*
|
||||
* @author marcosousa
|
||||
*/
|
||||
class WSCMForum extends WSCM
|
||||
{
|
||||
public function get_foruns_id($username, $password, $course_code)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$course_db = api_get_course_info($course_code);
|
||||
$foruns_info = get_forums($id = '', $course_db['code']);
|
||||
$foruns_id = '#';
|
||||
foreach ($foruns_info as $forum) {
|
||||
if (isset($forum['forum_id'])) {
|
||||
$foruns_id .= $forum['forum_id']."#";
|
||||
}
|
||||
}
|
||||
|
||||
return $foruns_id;
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function get_forum_title(
|
||||
$username,
|
||||
$password,
|
||||
$course_code,
|
||||
$forum_id
|
||||
) {
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$course_db = api_get_course_info($course_code);
|
||||
$table_forums = Database::get_course_table(TABLE_FORUM, $course_db['db_name']);
|
||||
$table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db['db_name']);
|
||||
|
||||
$sql = "SELECT * FROM ".$table_forums." forums, ".$table_item_property." item_properties
|
||||
WHERE item_properties.tool='".TOOL_FORUM."'
|
||||
AND item_properties.ref='".Database::escape_string($forum_id)."'
|
||||
AND forums.forum_id='".Database::escape_string($forum_id)."'";
|
||||
$result = Database::query($sql);
|
||||
$forum_info = Database::fetch_array($result);
|
||||
$forum_info['approval_direct_post'] = 0; // we can't anymore change this option, so it should always be activated
|
||||
|
||||
$forum_title = utf8_decode($forum_info['forum_title']);
|
||||
|
||||
return $forum_title;
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function get_forum_threads_id(
|
||||
$username,
|
||||
$password,
|
||||
$course_code,
|
||||
$forum_id
|
||||
) {
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$threads_info = get_threads($forum_id);
|
||||
$threads_id = '#';
|
||||
foreach ($threads_info as $thread) {
|
||||
if (isset($thread['thread_id'])) {
|
||||
$threads_id .= $thread['thread_id']."#";
|
||||
}
|
||||
}
|
||||
|
||||
return $threads_id;
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function get_forum_thread_data(
|
||||
$username,
|
||||
$password,
|
||||
$course_code,
|
||||
$thread_id,
|
||||
$field
|
||||
) {
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$course_db = api_get_course_info($course_code);
|
||||
$table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db['db_name']);
|
||||
$table_threads = Database::get_course_table(TABLE_FORUM_THREAD, $course_db['db_name']);
|
||||
|
||||
$sql = "SELECT * FROM ".$table_threads." threads, ".$table_item_property." item_properties
|
||||
WHERE item_properties.tool='".TOOL_FORUM_THREAD."'
|
||||
AND item_properties.ref='".Database::escape_string($thread_id)."'
|
||||
AND threads.thread_id='".Database::escape_string($thread_id)."'";
|
||||
$result = Database::query($sql);
|
||||
$thread_info = Database::fetch_array($result);
|
||||
|
||||
switch ($field) {
|
||||
case 'title':
|
||||
$htmlcode = true;
|
||||
$field_table = "thread_title";
|
||||
break;
|
||||
case 'date':
|
||||
$field_table = "thread_date";
|
||||
break;
|
||||
case 'sender':
|
||||
$field_table = "insert_user_id";
|
||||
break;
|
||||
case 'sender_name':
|
||||
$user_id = $thread_info['insert_user_id'];
|
||||
$user_info = api_get_user_info($user_id);
|
||||
|
||||
return $user_info['firstname'];
|
||||
break;
|
||||
default:
|
||||
$field_table = "title";
|
||||
}
|
||||
|
||||
return $thread_info[$field_table];
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function get_forum_thread_title(
|
||||
$username,
|
||||
$password,
|
||||
$course_code,
|
||||
$thread_id
|
||||
) {
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$course_db = api_get_course_info($course_code);
|
||||
$table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db['db_name']);
|
||||
$table_threads = Database::get_course_table(TABLE_FORUM_THREAD, $course_db['db_name']);
|
||||
|
||||
$sql = "SELECT * FROM ".$table_threads." threads, ".$table_item_property." item_properties
|
||||
WHERE item_properties.tool='".TOOL_FORUM_THREAD."'
|
||||
AND item_properties.ref='".Database::escape_string($thread_id)."'
|
||||
AND threads.thread_id='".Database::escape_string($thread_id)."'";
|
||||
$result = Database::query($sql);
|
||||
$thread_info = Database::fetch_array($result);
|
||||
|
||||
$htmlcode = true;
|
||||
$field_table = "thread_title";
|
||||
|
||||
return $thread_info[$field_table];
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function get_posts_id($username, $password, $course_code, $thread_id)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$course_db = api_get_course_info($course_code);
|
||||
|
||||
$table_users = Database::get_main_table(TABLE_MAIN_USER);
|
||||
$table_posts = Database::get_course_table(TABLE_FORUM_POST, $course_db['db_name']);
|
||||
|
||||
// note: change these SQL so that only the relevant fields of the user table are used
|
||||
if (api_is_allowed_to_edit(null, true)) {
|
||||
$sql = "SELECT * FROM $table_posts posts
|
||||
LEFT JOIN $table_users users
|
||||
ON posts.poster_id=users.user_id
|
||||
WHERE posts.thread_id='".Database::escape_string($thread_id)."'
|
||||
ORDER BY posts.post_id ASC";
|
||||
} else {
|
||||
// students can only se the posts that are approved (posts.visible='1')
|
||||
$sql = "SELECT * FROM $table_posts posts
|
||||
LEFT JOIN $table_users users
|
||||
ON posts.poster_id=users.user_id
|
||||
WHERE posts.thread_id='".Database::escape_string($thread_id)."'
|
||||
AND posts.visible='1'
|
||||
ORDER BY posts.post_id ASC";
|
||||
}
|
||||
$result = Database::query($sql);
|
||||
while ($row = Database::fetch_array($result)) {
|
||||
$posts_info[] = $row;
|
||||
}
|
||||
|
||||
$posts_id = '#';
|
||||
|
||||
foreach ($posts_info as $post) {
|
||||
if (isset($post['post_id'])) {
|
||||
$posts_id .= $post['post_id']."#";
|
||||
}
|
||||
}
|
||||
|
||||
return $posts_id;
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function get_post_data(
|
||||
$username,
|
||||
$password,
|
||||
$course_code,
|
||||
$post_id,
|
||||
$field
|
||||
) {
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$table_posts = Database::get_course_table(TABLE_FORUM_POST);
|
||||
$table_users = Database::get_main_table(TABLE_MAIN_USER);
|
||||
|
||||
$sql = "SELECT * FROM ".$table_posts."posts, ".$table_users." users
|
||||
WHERE posts.poster_id=users.user_id AND posts.post_id='".Database::escape_string($post_id)."'";
|
||||
$result = Database::query($sql);
|
||||
$post_info = Database::fetch_array($result);
|
||||
|
||||
$htmlcode = false;
|
||||
switch ($field) {
|
||||
case 'title':
|
||||
$htmlcode = true;
|
||||
$field_table = "post_title";
|
||||
break;
|
||||
case 'text':
|
||||
$htmlcode = true;
|
||||
$field_table = "post_text";
|
||||
break;
|
||||
case 'date':
|
||||
$field_table = "post_date";
|
||||
break;
|
||||
case 'sender':
|
||||
$field_table = "user_id";
|
||||
break;
|
||||
case 'sender_name':
|
||||
$field_table = "firstname";
|
||||
break;
|
||||
default:
|
||||
$htmlcode = true;
|
||||
$field_table = "title";
|
||||
}
|
||||
|
||||
return ($htmlcode) ? html_entity_decode($post_info[$field_table]) : $post_info[$field_table];
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function send_post(
|
||||
$username,
|
||||
$password,
|
||||
$course_code,
|
||||
$forum_id,
|
||||
$thread_id,
|
||||
$title,
|
||||
$content
|
||||
) {
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$em = Database::getManager();
|
||||
$course_db = api_get_course_info($course_code);
|
||||
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
$table_threads = Database::get_course_table(TABLE_FORUM_THREAD, $course_db['db_name']);
|
||||
$forum_table_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT, $course_db['db_name']);
|
||||
$table_posts = Database::get_course_table(TABLE_FORUM_POST, $course_db['db_name']);
|
||||
$post_date = date('Y-m-d H:i:s');
|
||||
$visible = 1;
|
||||
$has_attachment = false;
|
||||
$my_post = '';
|
||||
$post_notification = '';
|
||||
|
||||
$content = nl2br($content);
|
||||
|
||||
$title = htmlentities($title);
|
||||
$content = htmlentities($content);
|
||||
|
||||
$postDate = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC'));
|
||||
$post = new \Chamilo\CourseBundle\Entity\CForumPost();
|
||||
$post
|
||||
->setPostTitle($title)
|
||||
->setPostText(isset($content) ? (api_html_entity_decode($content)) : null)
|
||||
->setThreadId($thread_id)
|
||||
->setForumId($forum_id)
|
||||
->setPosterId($user_id)
|
||||
->setPostDate($postDate)
|
||||
->setPostNotification(isset($post_notification) ? $post_notification : null)
|
||||
->setPostParentId(isset($my_post) ? $my_post : null)
|
||||
->setVisible($visible);
|
||||
|
||||
$em->persist($post);
|
||||
$em->flush();
|
||||
|
||||
return "Post enviado!";
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
echo "aqui: ";
|
||||
$aqui = new WSCMForum();
|
||||
echo "<pre>";
|
||||
|
||||
//print_r($aqui->unreadMessage("aluno", "e695f51fe3dd6b7cf2be3188a614f10f"));
|
||||
//print_r($aqui->get_post_data("aluno", "c4ca4238a0b923820dcc509a6f75849b", "95", "sender_name"));
|
||||
|
||||
print_r($aqui->send_post("aluno", "c4ca4238a0b923820dcc509a6f75849b", "P0304", "3", "15", "títle", "conteúdo222222"));
|
||||
echo "</pre>";
|
||||
*/
|
||||
225
main/webservices/cm_webservice_inbox.php
Normal file
225
main/webservices/cm_webservice_inbox.php
Normal file
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/cm_webservice.php';
|
||||
|
||||
/**
|
||||
* Description of cm_soap_inbox.
|
||||
*
|
||||
* @author marcosousa
|
||||
*/
|
||||
class WSCMInbox extends WSCM
|
||||
{
|
||||
public function unreadMessage($username, $password)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$table_message = Database::get_main_table(TABLE_MESSAGE);
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
$condition_msg_status = ' msg_status = 1 '; // define('MESSAGE_STATUS_UNREAD', '1');
|
||||
|
||||
$sql_query = "SELECT COUNT(*) as number_messages
|
||||
FROM $table_message
|
||||
WHERE $condition_msg_status AND user_receiver_id=".$user_id;
|
||||
|
||||
$sql_result = Database::query($sql_query);
|
||||
$result = Database::fetch_array($sql_result);
|
||||
|
||||
return $result['number_messages'];
|
||||
}
|
||||
|
||||
return "0";
|
||||
}
|
||||
|
||||
public function get_message_id(
|
||||
$username,
|
||||
$password,
|
||||
$from,
|
||||
$number_of_items
|
||||
) {
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
$table_message = Database::get_main_table(TABLE_MESSAGE);
|
||||
|
||||
$sql_query = "SELECT id FROM $table_message ".
|
||||
" WHERE user_receiver_id=".$user_id." AND msg_status IN (0,1)".
|
||||
" ORDER BY send_date LIMIT $from,$number_of_items";
|
||||
|
||||
$sql_result = Database::query($sql_query);
|
||||
$message = "#";
|
||||
while ($result = Database::fetch_row($sql_result)) {
|
||||
$message .= $result[0]."#";
|
||||
}
|
||||
|
||||
return $message;
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function get_message_data($username, $password, $message_id, $field)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$htmlcode = false;
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
switch ($field) {
|
||||
case 'sender':
|
||||
$field_table = "user_sender_id";
|
||||
break;
|
||||
case 'title':
|
||||
$htmlcode = true;
|
||||
$field_table = "title";
|
||||
break;
|
||||
case 'date':
|
||||
$field_table = "send_date";
|
||||
break;
|
||||
case 'status':
|
||||
$field_table = "msg_status";
|
||||
break;
|
||||
case 'content':
|
||||
$this->set_message_as_read($user_id, $message_id);
|
||||
$htmlcode = true;
|
||||
$field_table = "content";
|
||||
break;
|
||||
default:
|
||||
$field_table = "title";
|
||||
}
|
||||
|
||||
$table_message = Database::get_main_table(TABLE_MESSAGE);
|
||||
|
||||
$sql_query = "SELECT ".$field_table." FROM $table_message ".
|
||||
" WHERE user_receiver_id=".$user_id." AND id=".$message_id;
|
||||
|
||||
$sql_result = Database::query($sql_query);
|
||||
$result = Database::fetch_row($sql_result);
|
||||
|
||||
return $htmlcode ? html_entity_decode($result[0]) : $result[0];
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function get_message_id_sent(
|
||||
$username,
|
||||
$password,
|
||||
$from,
|
||||
$number_of_items
|
||||
) {
|
||||
$from = (int) $from;
|
||||
$number_of_items = (int) $number_of_items;
|
||||
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
|
||||
$table_message = Database::get_main_table(TABLE_MESSAGE);
|
||||
$sql_query = "SELECT id FROM $table_message
|
||||
WHERE user_sender_id=".$user_id." AND msg_status=".MESSAGE_STATUS_OUTBOX."
|
||||
ORDER BY send_date
|
||||
LIMIT $from,$number_of_items";
|
||||
|
||||
$sql_result = Database::query($sql_query);
|
||||
$message = "#";
|
||||
while ($result = Database::fetch_row($sql_result)) {
|
||||
$message .= $result[0]."#";
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
|
||||
public function get_message_data_sent($username, $password, $id, $field)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$htmlcode = false;
|
||||
switch ($field) {
|
||||
case 'sender':
|
||||
$field_table = "user_sender_id";
|
||||
break;
|
||||
case 'title':
|
||||
$htmlcode = true;
|
||||
$field_table = "title";
|
||||
break;
|
||||
case 'date':
|
||||
$field_table = "send_date";
|
||||
break;
|
||||
case 'status':
|
||||
$field_table = "msg_status";
|
||||
break;
|
||||
case 'content':
|
||||
$htmlcode = true;
|
||||
$field_table = "content";
|
||||
break;
|
||||
default:
|
||||
$field_table = "title";
|
||||
}
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
$table_message = Database::get_main_table(TABLE_MESSAGE);
|
||||
$sql_query = "SELECT ".$field_table." FROM $table_message ".
|
||||
" WHERE user_sender_id=".$user_id." AND id=".$id;
|
||||
$sql_result = Database::query($sql_query);
|
||||
$result = Database::fetch_row($sql_result);
|
||||
|
||||
return $htmlcode ? html_entity_decode($result[0]) : $result[0];
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
public function message_send(
|
||||
$username,
|
||||
$password,
|
||||
$receiver_user_id,
|
||||
$subject,
|
||||
$content
|
||||
) {
|
||||
//TODO: verificar data de envio. Esta divergindo de data!
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$group_id = intval(0);
|
||||
$parent_id = intval(0);
|
||||
$edit_message_id = intval(0);
|
||||
$sent_email = false;
|
||||
$user_sender_id = UserManager::get_user_id_from_username($username);
|
||||
|
||||
$subject = htmlentities($subject);
|
||||
$content = htmlentities($content);
|
||||
|
||||
$table_message = Database::get_main_table(TABLE_MESSAGE);
|
||||
|
||||
$query = "INSERT INTO $table_message(user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) ".
|
||||
" VALUES ('$user_sender_id', '$receiver_user_id', '1', '".api_get_utc_datetime()."','$subject','$content','$group_id','$parent_id', '".api_get_utc_datetime()."')";
|
||||
Database::query($query);
|
||||
|
||||
$query = "INSERT INTO $table_message(user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) ".
|
||||
" VALUES ('$user_sender_id', '$receiver_user_id', '4', '".api_get_utc_datetime()."','$subject','$content','$group_id','$parent_id', '".api_get_utc_datetime()."')";
|
||||
Database::query($query);
|
||||
|
||||
$inbox_last_id = Database::insert_id();
|
||||
|
||||
return $inbox_last_id;
|
||||
} else {
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
}
|
||||
|
||||
protected function set_message_as_read($user_id, $message_id)
|
||||
{
|
||||
$table_message = Database::get_main_table(TABLE_MESSAGE);
|
||||
$query = "UPDATE $table_message SET msg_status = '".MESSAGE_STATUS_NEW."'
|
||||
WHERE user_receiver_id=".$user_id." AND id='".$message_id."';";
|
||||
Database::query($query);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
echo "aqui: ";
|
||||
$aqui = new WSCMInbox();
|
||||
|
||||
//print_r($aqui->unreadMessage("aluno", "e695f51fe3dd6b7cf2be3188a614f10f"));
|
||||
print_r($aqui->message_send("aluno", "356a192b7913b04c54574d18c28d46e6395428ab", "1", "Título da mensagem", "Conteúdo da mensagem com ç ã"));
|
||||
|
||||
|
||||
*/
|
||||
213
main/webservices/cm_webservice_user.php
Normal file
213
main/webservices/cm_webservice_user.php
Normal file
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/cm_webservice.php';
|
||||
|
||||
/**
|
||||
* Description of cm_soap_user.
|
||||
*
|
||||
* @author marcosousa
|
||||
*/
|
||||
class WSCMUser extends WSCM
|
||||
{
|
||||
public function find_id_user($username, $password, $name)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$listResult = "#";
|
||||
|
||||
$listArrayResult = [];
|
||||
$listArray = [];
|
||||
|
||||
$list = $this->get_user_list_like_start(
|
||||
['firstname' => $name],
|
||||
['firstname']
|
||||
);
|
||||
foreach ($list as $userData) {
|
||||
$listArray[] = $userData['user_id'];
|
||||
}
|
||||
|
||||
$list = $this->get_user_list_like_start(
|
||||
['lastname' => $name],
|
||||
['firstname']
|
||||
);
|
||||
foreach ($list as $userData) {
|
||||
$listArray[] = $userData['user_id'];
|
||||
}
|
||||
|
||||
$list = $this->get_user_list_like_start(
|
||||
['email' => $name],
|
||||
['firstname']
|
||||
);
|
||||
foreach ($list as $userData) {
|
||||
$listArray[] = $userData['user_id'];
|
||||
}
|
||||
|
||||
$listArrayResult = array_unique($listArray);
|
||||
foreach ($listArrayResult as $result) {
|
||||
$listResult .= $result."#";
|
||||
}
|
||||
|
||||
return $listResult;
|
||||
}
|
||||
|
||||
return "0";
|
||||
}
|
||||
|
||||
public function get_link_user_picture($username, $password, $id)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$userPic = UserManager::getUserPicture($id);
|
||||
if (empty($userPic)) {
|
||||
return "0";
|
||||
}
|
||||
|
||||
return $userPic;
|
||||
}
|
||||
|
||||
return "0";
|
||||
}
|
||||
|
||||
public function get_user_name($username, $password, $id, $field)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$userInfo = api_get_user_info($id);
|
||||
switch ($field) {
|
||||
case 'firstname':
|
||||
return $userInfo['firstname'];
|
||||
break;
|
||||
case 'lastname':
|
||||
return $userInfo['lastname'];
|
||||
break;
|
||||
case 'bothfl':
|
||||
return $userInfo['firstname']." ".$userInfo['lastname'];
|
||||
break;
|
||||
case 'bothlf':
|
||||
return $userInfo['lastname']." ".$userInfo['firstname'];
|
||||
break;
|
||||
default:
|
||||
return $userInfo['firstname'];
|
||||
}
|
||||
|
||||
return "0";
|
||||
}
|
||||
|
||||
return "0";
|
||||
}
|
||||
|
||||
public function send_invitation(
|
||||
$username,
|
||||
$password,
|
||||
$userfriend_id,
|
||||
$content_message = ''
|
||||
) {
|
||||
global $charset;
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
$message_title = get_lang('Invitation');
|
||||
$count_is_true = SocialManager::send_invitation_friend(
|
||||
$user_id,
|
||||
$userfriend_id,
|
||||
$message_title,
|
||||
$content_message
|
||||
);
|
||||
|
||||
if ($count_is_true) {
|
||||
return Display::return_message(
|
||||
api_htmlentities(
|
||||
get_lang('InvitationHasBeenSent'),
|
||||
ENT_QUOTES,
|
||||
$charset
|
||||
),
|
||||
'normal',
|
||||
false
|
||||
);
|
||||
} else {
|
||||
return Display::return_message(
|
||||
api_htmlentities(
|
||||
get_lang('YouAlreadySentAnInvitation'),
|
||||
ENT_QUOTES,
|
||||
$charset
|
||||
),
|
||||
'error',
|
||||
false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
|
||||
public function accept_friend($username, $password, $userfriend_id)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
UserManager::relate_users(
|
||||
$userfriend_id,
|
||||
$user_id,
|
||||
USER_RELATION_TYPE_FRIEND
|
||||
);
|
||||
SocialManager::invitation_accepted($userfriend_id, $user_id);
|
||||
|
||||
return get_lang('AddedContactToList');
|
||||
}
|
||||
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
|
||||
public function denied_invitation($username, $password, $userfriend_id)
|
||||
{
|
||||
if ($this->verifyUserPass($username, $password) == "valid") {
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
SocialManager::invitation_denied($userfriend_id, $user_id);
|
||||
|
||||
return get_lang('InvitationDenied');
|
||||
}
|
||||
|
||||
return get_lang('InvalidId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of users of which the given conditions match with a LIKE '%cond%'.
|
||||
*
|
||||
* @param array $conditions a list of condition (exemple : status=>STUDENT)
|
||||
* @param array $order_by a list of fields on which sort
|
||||
*
|
||||
* @return array an array with all users of the platform
|
||||
*
|
||||
* @todo optional course code parameter, optional sorting parameters...
|
||||
*@todo Use the UserManager class
|
||||
* @todo security filter order by
|
||||
*/
|
||||
private static function get_user_list_like_start($conditions = [], $order_by = [])
|
||||
{
|
||||
$user_table = Database::get_main_table(TABLE_MAIN_USER);
|
||||
$return_array = [];
|
||||
$sql_query = "SELECT * FROM $user_table";
|
||||
if (count($conditions) > 0) {
|
||||
$sql_query .= ' WHERE ';
|
||||
foreach ($conditions as $field => $value) {
|
||||
$field = Database::escape_string($field);
|
||||
$value = Database::escape_string($value);
|
||||
$sql_query .= $field.' LIKE \''.$value.'%\'';
|
||||
}
|
||||
}
|
||||
$order = '';
|
||||
foreach ($order_by as $orderByItem) {
|
||||
$order .= Database::escape_string($orderByItem).', ';
|
||||
}
|
||||
$order = substr($order, 0, -2);
|
||||
if (count($order_by) > 0) {
|
||||
$sql_query .= ' ORDER BY '.$order;
|
||||
}
|
||||
|
||||
$sql_result = Database::query($sql_query);
|
||||
while ($result = Database::fetch_array($sql_result)) {
|
||||
$return_array[] = $result;
|
||||
}
|
||||
|
||||
return $return_array;
|
||||
}
|
||||
}
|
||||
96
main/webservices/courses_list.rest.php
Normal file
96
main/webservices/courses_list.rest.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* This script provides the caller service with a list
|
||||
* of courses that have a certain level of visibility
|
||||
* on this chamilo portal.
|
||||
* It is set to work with the Chamilo module for Drupal:
|
||||
* http://drupal.org/project/chamilo.
|
||||
*
|
||||
* @author Yannick Warnier <yannick.warnier@beeznest.com>
|
||||
*
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
|
||||
api_protect_webservices();
|
||||
|
||||
/**
|
||||
* Get a list of courses (code, url, title, teacher, language) and return to caller
|
||||
* Function registered as service. Returns strings in UTF-8.
|
||||
*
|
||||
* @param string Security key (the Dokeos install's API key)
|
||||
* @param mixed Array or string. Type of visibility of course (public, public-registered, private, closed)
|
||||
*
|
||||
* @return array Courses list (code=>[title=>'title',url='http://...',teacher=>'...',language=>''],code=>[...],...)
|
||||
*/
|
||||
function courses_list($security_key, $visibilities = 'public')
|
||||
{
|
||||
global $_configuration;
|
||||
|
||||
// Check if this script is launch by server and if security key is ok.
|
||||
if ($security_key != $_configuration['security_key']) {
|
||||
return ['error_msg' => 'Security check failed'];
|
||||
}
|
||||
|
||||
$vis = [
|
||||
'public' => '3',
|
||||
'public-registered' => '2',
|
||||
'private' => '1',
|
||||
'closed' => '0',
|
||||
];
|
||||
|
||||
$courses_list = [];
|
||||
|
||||
if (!is_array($visibilities)) {
|
||||
$tmp = $visibilities;
|
||||
$visibilities = [$tmp];
|
||||
}
|
||||
foreach ($visibilities as $visibility) {
|
||||
if (!in_array($visibility, array_keys($vis))) {
|
||||
return ['error_msg' => 'Security check failed'];
|
||||
}
|
||||
$courses_list_tmp = CourseManager::get_courses_list(
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
$vis[$visibility]
|
||||
);
|
||||
foreach ($courses_list_tmp as $index => $course) {
|
||||
$course_info = CourseManager::get_course_information(
|
||||
$course['code']
|
||||
);
|
||||
$courses_list[$course['code']] = [
|
||||
'title' => api_utf8_encode(
|
||||
$course_info['title']
|
||||
),
|
||||
'url' => api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/',
|
||||
'teacher' => api_utf8_encode($course_info['tutor_name']),
|
||||
'language' => $course_info['course_language'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $courses_list;
|
||||
}
|
||||
|
||||
header('Content-Type: text/xml; charset=utf-8');
|
||||
echo '<?xml version="1.0"?>';
|
||||
echo '<courseslist>';
|
||||
|
||||
if (empty($_POST['security-key']) || empty($_POST['visibility'])) {
|
||||
echo '<errormsg>Invalid parameters, this script expects a security-key and a visibility parameters</errormsg>';
|
||||
} else {
|
||||
$courses_list = courses_list($_POST['security-key'], $_POST['visibility']);
|
||||
foreach ($courses_list as $code => $cd) {
|
||||
echo '<course>';
|
||||
echo '<code>', $code, '</code>';
|
||||
echo '<title>', $cd['title'], '</title>';
|
||||
echo '<url>', $cd['url'], '</url>';
|
||||
echo '<teacher>', $cd['teacher'], '</teacher>';
|
||||
echo '<language>', $cd['language'], '</language>';
|
||||
echo '</course>';
|
||||
}
|
||||
}
|
||||
echo '</courseslist>';
|
||||
149
main/webservices/courses_list.soap.php
Normal file
149
main/webservices/courses_list.soap.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* This script provides the caller service with a list
|
||||
* of courses that have a certain level of visibility
|
||||
* on this chamilo portal.
|
||||
* It is set to work with the Chamilo module for Drupal:
|
||||
* http://drupal.org/project/chamilo.
|
||||
*
|
||||
* @author Yannick Warnier <yannick.warnier@beeznest.com>
|
||||
*
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
|
||||
api_protect_webservices();
|
||||
|
||||
// Create the server instance
|
||||
$server = new soap_server();
|
||||
// Initialize WSDL support
|
||||
$server->configureWSDL('WSCourseList', 'urn:WSCourseList');
|
||||
|
||||
/* Register WSCourseList function */
|
||||
// Register the data structures used by the service
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'courseDetails',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'name' => 'code',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'title',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'url',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'teacher',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'language',
|
||||
'type' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'courseList',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[
|
||||
['ref' => 'SOAP-ENC:arrayType',
|
||||
'wsdl:arrayType' => 'tns:courseDetails[]', ],
|
||||
],
|
||||
'tns:courseDetails'
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSCourseList', // method name
|
||||
['username' => 'xsd:string',
|
||||
'signature' => 'xsd:string',
|
||||
'visibilities' => 'xsd:string', ], // input parameters
|
||||
['return' => 'xsd:Array'], // output parameters
|
||||
'urn:WSCourseList', // namespace
|
||||
'urn:WSCourseList#WSCourseList', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service returns a list of courses' // documentation
|
||||
);
|
||||
|
||||
/**
|
||||
* Get a list of courses (code, url, title, teacher, language) and return to caller
|
||||
* Function registered as service. Returns strings in UTF-8.
|
||||
*
|
||||
* @param string User name in Chamilo
|
||||
* @param string Signature (composed of the sha1(username+apikey)
|
||||
* @param mixed Array or string. Type of visibility of course (public, public-registered, private, closed)
|
||||
*
|
||||
* @return array Courses list (code=>[title=>'title',url='http://...',teacher=>'...',language=>''],code=>[...],...)
|
||||
*/
|
||||
function WSCourseList($username, $signature, $visibilities = 'public')
|
||||
{
|
||||
if (empty($username) or empty($signature)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
global $_configuration;
|
||||
|
||||
$info = api_get_user_info_from_username($username);
|
||||
$user_id = $info['user_id'];
|
||||
if (!UserManager::is_admin($user_id)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
$list = UserManager::get_api_keys($user_id, 'dokeos');
|
||||
$key = '';
|
||||
foreach ($list as $key) {
|
||||
break;
|
||||
}
|
||||
|
||||
$local_key = $username.$key;
|
||||
|
||||
if (!api_is_valid_secret_key($signature, $local_key) &&
|
||||
!api_is_valid_secret_key($signature, $username.$_configuration['security_key'])
|
||||
) {
|
||||
return -1; // The secret key is incorrect.
|
||||
}
|
||||
//public-registered = open
|
||||
$vis = ['public' => '3', 'public-registered' => '2', 'private' => '1', 'closed' => '0'];
|
||||
|
||||
$courses_list = [];
|
||||
|
||||
if (!is_array($visibilities)) {
|
||||
$visibilities = split(',', $visibilities);
|
||||
}
|
||||
foreach ($visibilities as $visibility) {
|
||||
if (!in_array($visibility, array_keys($vis))) {
|
||||
return ['error_msg' => 'Security check failed'];
|
||||
}
|
||||
$courses_list_tmp = CourseManager::get_courses_list(
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
$vis[$visibility]
|
||||
);
|
||||
foreach ($courses_list_tmp as $index => $course) {
|
||||
$course_info = CourseManager::get_course_information($course['code']);
|
||||
$courses_list[] = [
|
||||
'code' => $course['code'],
|
||||
'title' => api_utf8_encode($course_info['title']),
|
||||
'url' => api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/',
|
||||
'teacher' => api_utf8_encode($course_info['tutor_name']),
|
||||
'language' => $course_info['course_language'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $courses_list;
|
||||
}
|
||||
|
||||
// Use the request to (try to) invoke the service.
|
||||
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
|
||||
$server->service($HTTP_RAW_POST_DATA);
|
||||
29
main/webservices/example_fill_users_fields.php
Normal file
29
main/webservices/example_fill_users_fields.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* This script populates the user_extra_fields_value table with a new field which
|
||||
* contains the username for each user. This allows you to use web
|
||||
* services to update users based on their username (which is assumed
|
||||
* to be the same as in the application which calls the webservice).
|
||||
* This script should be called any time a new user (or a large group of new
|
||||
* users) is added to the database.
|
||||
*
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
//remove the next line to enable the script (this can harm your database so
|
||||
// don't enable unless you know what you're doing and you have a backup)
|
||||
exit();
|
||||
// update this ID after you create the corresponding field through the Chamilo
|
||||
// profile fields manager (admin page, users section) as text field.
|
||||
// Give this field a name you will later use in original_field_id_name, while
|
||||
// you will use the normal username of Chamilo users.
|
||||
$extra_field_id = 9;
|
||||
require_once '../inc/global.inc.php';
|
||||
$tuser = Database::get_main_table(TABLE_MAIN_USER);
|
||||
$tuserfv = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
|
||||
$sql = "SELECT user_id, username FROM $tuser ORDER BY user_id";
|
||||
$res = Database::query($sql);
|
||||
while ($row = Database::fetch_array($res)) {
|
||||
$sql2 = "INSERT INTO $tuserfv (item_id, field_id, value)
|
||||
VALUES (".$row['user_id'].", 11,'".$row['username']."')";
|
||||
$res2 = Database::query($sql2);
|
||||
}
|
||||
533
main/webservices/gradebook.php
Normal file
533
main/webservices/gradebook.php
Normal file
@@ -0,0 +1,533 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
use Skill as SkillManager;
|
||||
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
|
||||
api_protect_webservices();
|
||||
|
||||
ini_set('memory_limit', -1);
|
||||
|
||||
/*
|
||||
ini_set('upload_max_filesize', '4000M');
|
||||
ini_set('post_max_size', '4000M');
|
||||
ini_set('max_execution_time', '80000');
|
||||
ini_set('max_input_time', '80000');
|
||||
*/
|
||||
|
||||
$debug = true;
|
||||
|
||||
define('WS_ERROR_SECRET_KEY', 1);
|
||||
|
||||
function return_error($code)
|
||||
{
|
||||
$fault = null;
|
||||
switch ($code) {
|
||||
case WS_ERROR_SECRET_KEY:
|
||||
$fault = new soap_fault('Server', '', 'Secret key is not correct or params are not correctly set');
|
||||
break;
|
||||
}
|
||||
|
||||
return $fault;
|
||||
}
|
||||
|
||||
function WSHelperVerifyKey($params)
|
||||
{
|
||||
global $_configuration, $debug;
|
||||
if (is_array($params)) {
|
||||
$secret_key = $params['secret_key'];
|
||||
} else {
|
||||
$secret_key = $params;
|
||||
}
|
||||
//error_log(print_r($params,1));
|
||||
$check_ip = false;
|
||||
$ip_matches = false;
|
||||
$ip = trim($_SERVER['REMOTE_ADDR']);
|
||||
// if we are behind a reverse proxy, assume it will send the
|
||||
// HTTP_X_FORWARDED_FOR header and use this IP instead
|
||||
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||
list($ip1) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
||||
$ip = trim($ip1);
|
||||
}
|
||||
if ($debug) {
|
||||
error_log("ip: $ip");
|
||||
}
|
||||
// Check if a file that limits access from webservices exists and contains
|
||||
// the restraining check
|
||||
if (is_file('webservice-auth-ip.conf.php')) {
|
||||
include 'webservice-auth-ip.conf.php';
|
||||
if ($debug) {
|
||||
error_log("webservice-auth-ip.conf.php file included");
|
||||
}
|
||||
if (!empty($ws_auth_ip)) {
|
||||
$check_ip = true;
|
||||
$ip_matches = api_check_ip_in_range($ip, $ws_auth_ip);
|
||||
if ($debug) {
|
||||
error_log("ip_matches: $ip_matches");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($debug) {
|
||||
error_log("checkip ".intval($check_ip));
|
||||
}
|
||||
|
||||
if ($check_ip) {
|
||||
$security_key = $_configuration['security_key'];
|
||||
} else {
|
||||
$security_key = $ip.$_configuration['security_key'];
|
||||
//error_log($secret_key.'-'.$security_key);
|
||||
}
|
||||
$result = api_is_valid_secret_key($secret_key, $security_key);
|
||||
//error_log($secret_key.'-'.$security_key);
|
||||
if ($debug) {
|
||||
error_log('WSHelperVerifyKey result: '.intval($result));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Create the server instance
|
||||
$server = new soap_server();
|
||||
//$server->soap_defencoding = 'UTF-8';
|
||||
|
||||
// Initialize WSDL support
|
||||
$server->configureWSDL('WSGradebook', 'urn:WSGradebook');
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'WSGradebookScoreParams',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'item_id' => [
|
||||
'name' => 'item_id',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'item_type' => [
|
||||
'name' => 'item_type',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'email' => [
|
||||
'name' => 'email',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'returnItemScore',
|
||||
'complexType',
|
||||
'struct',
|
||||
'sequence',
|
||||
'',
|
||||
[
|
||||
'score' => ['name' => 'score', 'type' => 'xsd:string'],
|
||||
'date' => ['name' => 'date', 'type' => 'xsd:string'],
|
||||
'counter' => ['name' => 'counter', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSGetGradebookUserItemScore', // method name
|
||||
['params' => 'tns:WSGradebookScoreParams'], // input parameters
|
||||
['return' => 'tns:returnItemScore'], // output parameters
|
||||
'urn:WSGradebook', // namespace
|
||||
'urn:WSGradebook#WSGetGradebookUserItemScore', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'get gradebook item user result'
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
function WSGetGradebookUserItemScore($params)
|
||||
{
|
||||
if (!WSHelperVerifyKey($params)) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
$itemId = $params['item_id'];
|
||||
$itemType = $params['item_type'];
|
||||
$email = $params['email'];
|
||||
$userInfo = api_get_user_info_from_email($email);
|
||||
|
||||
if (empty($userInfo)) {
|
||||
return new soap_fault('Server', '', 'User not found');
|
||||
}
|
||||
|
||||
$em = Database::getManager();
|
||||
|
||||
$score = [];
|
||||
switch ($itemType) {
|
||||
case 'link':
|
||||
/** @var \Chamilo\CoreBundle\Entity\GradebookLink $link */
|
||||
$link = $em->getRepository('ChamiloCoreBundle:GradebookLink')->find($itemId);
|
||||
if (empty($link)) {
|
||||
return new soap_fault('Server', '', 'gradebook link not found');
|
||||
}
|
||||
|
||||
$links = AbstractLink::load($link->getId());
|
||||
switch ($link->getType()) {
|
||||
case LINK_EXERCISE:
|
||||
/** @var ExerciseLink $link */
|
||||
foreach ($links as $link) {
|
||||
$link->set_session_id($link->getCategory()->get_session_id());
|
||||
$score = $link->calc_score($userInfo['user_id']);
|
||||
break;
|
||||
}
|
||||
|
||||
if (empty($score)) {
|
||||
// If no score found then try exercises from base course.
|
||||
/** @var ExerciseLink $link */
|
||||
foreach ($links as $link) {
|
||||
$link->checkBaseExercises = true;
|
||||
$link->set_session_id($link->getCategory()->get_session_id());
|
||||
$score = $link->calc_score($userInfo['user_id']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case LINK_STUDENTPUBLICATION:
|
||||
/** @var StudentPublicationLink $link */
|
||||
foreach ($links as $link) {
|
||||
$link->set_session_id($link->getCategory()->get_session_id());
|
||||
$score = $link->calc_score($userInfo['user_id']);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'evaluation':
|
||||
//$evaluation = $em->getRepository('ChamiloCoreBundle:GradebookEvaluation')->find($itemId);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!empty($score)) {
|
||||
$result = ExerciseLib::show_score($score[0], $score[1], false);
|
||||
$result = strip_tags($result);
|
||||
|
||||
return ['score' => $result, 'date' => $score[2], 'counter' => $score[3]];
|
||||
}
|
||||
|
||||
return new soap_fault('Server', '', 'Score not found');
|
||||
}
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'WSGradebookCategoryScoreParams',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_code' => [
|
||||
'name' => 'course_code',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'session_id' => [
|
||||
'name' => 'session_id',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'email' => [
|
||||
'name' => 'email',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSGetGradebookCategoryUserScore', // method name
|
||||
['params' => 'tns:WSGradebookCategoryScoreParams'], // input parameters
|
||||
['return' => 'xsd:string'], // output parameters
|
||||
'urn:WSGradebook', // namespace
|
||||
'urn:WSGradebook#WSGetGradebookCategoryUserScore', // soapaction
|
||||
'rpc', // style
|
||||
'encoded'
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
function WSGetGradebookCategoryUserScore($params)
|
||||
{
|
||||
if (!WSHelperVerifyKey($params)) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
$courseCode = $params['course_code'];
|
||||
$sessionId = (int) $params['session_id'];
|
||||
if (!empty($sessionId)) {
|
||||
$sessionInfo = api_get_session_info($sessionId);
|
||||
if (empty($sessionInfo)) {
|
||||
return new soap_fault('Server', '', 'Session not found');
|
||||
}
|
||||
}
|
||||
|
||||
$email = $params['email'];
|
||||
$userInfo = api_get_user_info_from_email($email);
|
||||
|
||||
if (empty($userInfo)) {
|
||||
return new soap_fault('Server', '', 'User not found');
|
||||
}
|
||||
$userId = $userInfo['user_id'];
|
||||
$courseInfo = api_get_course_info($courseCode);
|
||||
if (empty($courseInfo)) {
|
||||
return new soap_fault('Server', '', 'Course not found');
|
||||
}
|
||||
|
||||
$cats = Category::load(null,
|
||||
null,
|
||||
$courseCode,
|
||||
null,
|
||||
null,
|
||||
$sessionId
|
||||
);
|
||||
|
||||
/** @var Category $category */
|
||||
$category = isset($cats[0]) ? $cats[0] : null;
|
||||
$scorecourse_display = null;
|
||||
|
||||
if (!empty($category)) {
|
||||
$categoryCourse = Category::load($category->get_id());
|
||||
$category = isset($categoryCourse[0]) ? $categoryCourse[0] : null;
|
||||
$allevals = $category->get_evaluations($userId, true);
|
||||
$alllinks = $category->get_links($userId, true);
|
||||
|
||||
$allEvalsLinks = array_merge($allevals, $alllinks);
|
||||
$main_weight = $category->get_weight();
|
||||
$scoredisplay = ScoreDisplay::instance();
|
||||
$item_value_total = 0;
|
||||
/** @var AbstractLink $item */
|
||||
foreach ($allEvalsLinks as $item) {
|
||||
$item->set_session_id($sessionId);
|
||||
$item->set_course_code($courseCode);
|
||||
$score = $item->calc_score($userId);
|
||||
if (!empty($score)) {
|
||||
$divide = $score[1] == 0 ? 1 : $score[1];
|
||||
$item_value = $score[0] / $divide * $item->get_weight();
|
||||
$item_value_total += $item_value;
|
||||
}
|
||||
}
|
||||
|
||||
$item_total = $main_weight;
|
||||
$total_score = [$item_value_total, $item_total];
|
||||
$score = $scoredisplay->display_score($total_score, SCORE_DIV_PERCENT);
|
||||
$score = strip_tags($score);
|
||||
|
||||
return $score;
|
||||
}
|
||||
|
||||
if (empty($category)) {
|
||||
return new soap_fault('Server', '', 'Gradebook category not found');
|
||||
}
|
||||
|
||||
return new soap_fault('Server', '', 'Score not found');
|
||||
}
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'WSLpProgressParams',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_code' => [
|
||||
'name' => 'course_code',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'session_id' => [
|
||||
'name' => 'session_id',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'lp_id' => [
|
||||
'name' => 'lp_id',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'email' => [
|
||||
'name' => 'email',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSGetLpProgress', // method name
|
||||
['params' => 'tns:WSLpProgressParams'], // input parameters
|
||||
['return' => 'xsd:string'], // output parameters
|
||||
'urn:WSGradebook', // namespace
|
||||
'urn:WSGradebook#WSGetLpProgress', // soapaction
|
||||
'rpc', // style
|
||||
'encoded'
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
function WSGetLpProgress($params)
|
||||
{
|
||||
if (!WSHelperVerifyKey($params)) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
$courseCode = $params['course_code'];
|
||||
$courseInfo = api_get_course_info($courseCode);
|
||||
if (empty($courseInfo)) {
|
||||
return new soap_fault('Server', '', 'Course not found');
|
||||
}
|
||||
|
||||
$sessionId = (int) $params['session_id'];
|
||||
if (!empty($sessionId)) {
|
||||
$sessionInfo = api_get_session_info($sessionId);
|
||||
if (empty($sessionInfo)) {
|
||||
return new soap_fault('Server', '', 'Session not found');
|
||||
}
|
||||
}
|
||||
|
||||
$email = $params['email'];
|
||||
$userInfo = api_get_user_info_from_email($email);
|
||||
$userId = $userInfo['user_id'];
|
||||
|
||||
if (empty($userInfo)) {
|
||||
return new soap_fault('Server', '', 'User not found');
|
||||
}
|
||||
|
||||
$lpId = $params['lp_id'];
|
||||
$lp = new learnpath($courseCode, $lpId, $userId);
|
||||
|
||||
if (empty($lp)) {
|
||||
return new soap_fault('Server', '', 'LP not found');
|
||||
}
|
||||
|
||||
return $lp->progress_db;
|
||||
}
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'WSAssignSkillParams',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'skill_id' => [
|
||||
'name' => 'skill_id',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'level' => [
|
||||
'name' => 'level',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'justification' => [
|
||||
'name' => 'justification',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'email' => [
|
||||
'name' => 'email',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'author_email' => [
|
||||
'name' => 'author_email',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSAssignSkill', // method name
|
||||
['params' => 'tns:WSAssignSkillParams'], // input parameters
|
||||
['return' => 'xsd:string'], // output parameters
|
||||
'urn:WSGradebook', // namespace
|
||||
'urn:WSGradebook:WSAssignSkill', // soapaction
|
||||
'rpc', // style
|
||||
'encoded'
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
function WSAssignSkill($params)
|
||||
{
|
||||
if (!WSHelperVerifyKey($params)) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
$em = Database::getManager();
|
||||
$skillManager = new SkillManager();
|
||||
|
||||
$skillId = isset($params['skill_id']) ? $params['skill_id'] : 0;
|
||||
$skillRepo = $em->getRepository('ChamiloCoreBundle:Skill');
|
||||
$skill = $skillRepo->find($skillId);
|
||||
|
||||
if (empty($skill)) {
|
||||
return new soap_fault('Server', '', 'Skill not found');
|
||||
}
|
||||
|
||||
$justification = $params['justification'];
|
||||
|
||||
if (strlen($justification) < 10) {
|
||||
return new soap_fault('Server', '', 'Justification smaller than 10 chars');
|
||||
}
|
||||
|
||||
$level = (int) $params['level'];
|
||||
|
||||
$email = $params['email'];
|
||||
$userInfo = api_get_user_info_from_email($email);
|
||||
|
||||
if (empty($userInfo)) {
|
||||
return new soap_fault('Server', '', 'User not found');
|
||||
}
|
||||
|
||||
$email = $params['author_email'];
|
||||
$authorInfo = api_get_user_info_from_email($email);
|
||||
|
||||
if (empty($authorInfo)) {
|
||||
return new soap_fault('Server', '', 'Author not found');
|
||||
}
|
||||
|
||||
$userId = $userInfo['user_id'];
|
||||
$user = api_get_user_entity($userId);
|
||||
$skillUser = $skillManager->addSkillToUserBadge(
|
||||
$user,
|
||||
$skill,
|
||||
$level,
|
||||
$justification,
|
||||
$authorInfo['id']
|
||||
);
|
||||
|
||||
if (!empty($skillUser)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Use the request to (try to) invoke the service
|
||||
$GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents('php://input');
|
||||
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
|
||||
|
||||
// If you send your data in utf8 then this value must be false.
|
||||
$decodeUTF8 = api_get_setting('registration.soap.php.decode_utf8');
|
||||
if ($decodeUTF8 === 'true') {
|
||||
$server->decode_utf8 = true;
|
||||
} else {
|
||||
$server->decode_utf8 = false;
|
||||
}
|
||||
$server->service($HTTP_RAW_POST_DATA);
|
||||
83
main/webservices/http-auth.php
Normal file
83
main/webservices/http-auth.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
$realm = 'The batcave';
|
||||
|
||||
// Just a random id
|
||||
$nonce = uniqid();
|
||||
|
||||
// Get the digest from the http header
|
||||
$digest = getDigest();
|
||||
|
||||
// If there was no digest, show login
|
||||
if (is_null($digest)) {
|
||||
requireLogin($realm, $nonce);
|
||||
}
|
||||
|
||||
$digestParts = digestParse($digest);
|
||||
|
||||
$validUser = 'admin';
|
||||
$validPass = 'admin';
|
||||
|
||||
// Based on all the info we gathered we can figure out what the response should be
|
||||
$A1 = md5("{$digestParts['username']}:{$realm}:{$validPass}");
|
||||
$A2 = md5("{$_SERVER['REQUEST_METHOD']}:{$digestParts['uri']}");
|
||||
|
||||
$validResponse = md5("{$A1}:{$digestParts['nonce']}:{$digestParts['nc']}:{$digestParts['cnonce']}:{$digestParts['qop']}:{$A2}");
|
||||
|
||||
if ($digestParts['response'] != $validResponse) {
|
||||
requireLogin($realm, $nonce);
|
||||
} else {
|
||||
// We're in!
|
||||
echo 'a7532ae474e5e66a0c16eddab02e02a7';
|
||||
exit();
|
||||
}
|
||||
|
||||
// This function returns the digest string
|
||||
function getDigest()
|
||||
{
|
||||
// mod_php
|
||||
if (isset($_SERVER['PHP_AUTH_DIGEST'])) {
|
||||
$digest = $_SERVER['PHP_AUTH_DIGEST'];
|
||||
// most other servers
|
||||
} elseif (isset($_SERVER['HTTP_AUTHENTICATION'])) {
|
||||
if (strpos(
|
||||
strtolower($_SERVER['HTTP_AUTHENTICATION']),
|
||||
'digest'
|
||||
) === 0) {
|
||||
$digest = substr($_SERVER['HTTP_AUTHORIZATION'], 7);
|
||||
}
|
||||
} elseif (isset($_SERVER['HTTP_WWW_AUTHENTICATE'])) {
|
||||
$digest = $_SERVER['HTTP_WWW_AUTHENTICATE'];
|
||||
}
|
||||
|
||||
return $digest;
|
||||
}
|
||||
|
||||
// This function forces a login prompt
|
||||
function requireLogin($realm, $nonce)
|
||||
{
|
||||
header('WWW-Authenticate: Digest realm="'.$realm.'",qop="auth",nonce="'.$nonce.'",opaque="'.md5($realm).'"');
|
||||
header('HTTP/1.1 401');
|
||||
echo 'Authentication Canceled';
|
||||
exit();
|
||||
}
|
||||
|
||||
// This function extracts the separate values from the digest string
|
||||
function digestParse($digest)
|
||||
{
|
||||
// protect against missing data
|
||||
$needed_parts = ['nonce' => 1, 'nc' => 1, 'cnonce' => 1, 'qop' => 1, 'username' => 1, 'uri' => 1, 'response' => 1];
|
||||
$data = [];
|
||||
|
||||
preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $digest, $matches, PREG_SET_ORDER);
|
||||
|
||||
foreach ($matches as $m) {
|
||||
$data[$m[1]] = $m[2] ? $m[2] : $m[3];
|
||||
unset($needed_parts[$m[1]]);
|
||||
}
|
||||
|
||||
return $needed_parts ? false : $data;
|
||||
}
|
||||
6
main/webservices/index.html
Normal file
6
main/webservices/index.html
Normal file
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
746
main/webservices/lp.php
Normal file
746
main/webservices/lp.php
Normal file
@@ -0,0 +1,746 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
|
||||
api_protect_webservices();
|
||||
|
||||
ini_set('memory_limit', -1);
|
||||
/*
|
||||
ini_set('upload_max_filesize', '4000M');
|
||||
ini_set('post_max_size', '4000M');
|
||||
ini_set('max_execution_time', '80000');
|
||||
ini_set('max_input_time', '80000');
|
||||
*/
|
||||
|
||||
$debug = true;
|
||||
|
||||
define('WS_ERROR_SECRET_KEY', 1);
|
||||
|
||||
function return_error($code)
|
||||
{
|
||||
$fault = null;
|
||||
switch ($code) {
|
||||
case WS_ERROR_SECRET_KEY:
|
||||
$fault = new soap_fault('Server', '', 'Secret key is not correct or params are not correctly set');
|
||||
break;
|
||||
}
|
||||
|
||||
return $fault;
|
||||
}
|
||||
|
||||
function WSHelperVerifyKey($params)
|
||||
{
|
||||
global $_configuration, $debug;
|
||||
if (is_array($params)) {
|
||||
$secret_key = $params['secret_key'];
|
||||
} else {
|
||||
$secret_key = $params;
|
||||
}
|
||||
//error_log(print_r($params,1));
|
||||
$check_ip = false;
|
||||
$ip_matches = false;
|
||||
$ip = trim($_SERVER['REMOTE_ADDR']);
|
||||
// if we are behind a reverse proxy, assume it will send the
|
||||
// HTTP_X_FORWARDED_FOR header and use this IP instead
|
||||
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||
list($ip1) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
||||
$ip = trim($ip1);
|
||||
}
|
||||
if ($debug) {
|
||||
error_log("ip: $ip");
|
||||
}
|
||||
// Check if a file that limits access from webservices exists and contains
|
||||
// the restraining check
|
||||
if (is_file('webservice-auth-ip.conf.php')) {
|
||||
include 'webservice-auth-ip.conf.php';
|
||||
if ($debug) {
|
||||
error_log("webservice-auth-ip.conf.php file included");
|
||||
}
|
||||
if (!empty($ws_auth_ip)) {
|
||||
$check_ip = true;
|
||||
$ip_matches = api_check_ip_in_range($ip, $ws_auth_ip);
|
||||
if ($debug) {
|
||||
error_log("ip_matches: $ip_matches");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($debug) {
|
||||
error_log("checkip ".intval($check_ip));
|
||||
}
|
||||
|
||||
if ($check_ip) {
|
||||
$security_key = $_configuration['security_key'];
|
||||
} else {
|
||||
$security_key = $ip.$_configuration['security_key'];
|
||||
//error_log($secret_key.'-'.$security_key);
|
||||
}
|
||||
$result = api_is_valid_secret_key($secret_key, $security_key);
|
||||
//error_log($secret_key.'-'.$security_key);
|
||||
if ($debug) {
|
||||
error_log('WSHelperVerifyKey result: '.intval($result));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Create the server instance
|
||||
$server = new soap_server();
|
||||
|
||||
//$server->soap_defencoding = 'UTF-8';
|
||||
|
||||
// Initialize WSDL support
|
||||
$server->configureWSDL('WSLP', 'urn:WSLP');
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'params',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_id_name' => [
|
||||
'name' => 'course_id_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'course_id_value' => [
|
||||
'name' => 'course_id_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'session_id_name' => [
|
||||
'name' => 'session_id_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'session_id_value' => [
|
||||
'name' => 'session_id_value',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'file_data' => ['name' => 'file', 'type' => 'xsd:string'],
|
||||
'filename' => ['name' => 'filename', 'type' => 'xsd:string'],
|
||||
'lp_name' => ['name' => 'lp_name', 'type' => 'xsd:string'],
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSImportLP', // method name
|
||||
['params' => 'tns:params'], // input parameters
|
||||
['return' => 'xsd:string'], // output parameters
|
||||
'urn:WSLP', // namespace
|
||||
'urn:WSLP#WSImportLP', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service adds users' // documentation
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
function WSImportLP($params)
|
||||
{
|
||||
global $debug;
|
||||
if (!WSHelperVerifyKey($params)) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
if ($debug) {
|
||||
error_log('WSImportLP');
|
||||
}
|
||||
|
||||
$courseIdName = $params['course_id_name'];
|
||||
$courseIdValue = $params['course_id_value'];
|
||||
$sessionIdName = isset($params['session_id_name']) ? $params['session_id_name'] : null;
|
||||
$sessionIdValue = isset($params['session_id_value']) ? $params['session_id_value'] : null;
|
||||
|
||||
$lpName = $params['lp_name'];
|
||||
|
||||
$courseInfo = CourseManager::getCourseInfoFromOriginalId(
|
||||
$courseIdValue,
|
||||
$courseIdName
|
||||
);
|
||||
$courseId = $courseInfo['real_id'];
|
||||
|
||||
if (empty($courseInfo)) {
|
||||
if ($debug) {
|
||||
error_log('Course not found');
|
||||
}
|
||||
|
||||
return 'Course not found';
|
||||
}
|
||||
|
||||
$sessionId = 0;
|
||||
if (!empty($sessionIdName) && !empty($sessionIdValue)) {
|
||||
$sessionId = SessionManager::getSessionIdFromOriginalId(
|
||||
$sessionIdValue,
|
||||
$sessionIdName
|
||||
);
|
||||
|
||||
if (empty($sessionId)) {
|
||||
if ($debug) {
|
||||
error_log('Session not found');
|
||||
}
|
||||
|
||||
return 'Session not found';
|
||||
}
|
||||
}
|
||||
|
||||
$proximity = 'local';
|
||||
$maker = 'Scorm';
|
||||
$maxScore = ''; //$_REQUEST['use_max_score']
|
||||
|
||||
$oScorm = new scorm($courseInfo['code']);
|
||||
$fileData = base64_decode($params['file_data']);
|
||||
|
||||
$uniqueFile = uniqid();
|
||||
$userId = 1; // admin
|
||||
$filePath = api_get_path(SYS_ARCHIVE_PATH).$uniqueFile;
|
||||
file_put_contents($filePath, $fileData);
|
||||
|
||||
$fileName = $params['filename'];
|
||||
|
||||
$fileInfo = [
|
||||
'tmp_name' => $filePath,
|
||||
'name' => $fileName,
|
||||
];
|
||||
|
||||
$manifest = $oScorm->import_package($fileInfo, '', $courseInfo);
|
||||
|
||||
if (!$manifest) {
|
||||
if ($debug) {
|
||||
error_log('manifest.xml file not found');
|
||||
}
|
||||
|
||||
return 'manifest.xml file not found';
|
||||
}
|
||||
|
||||
$manifestData = $oScorm->parse_manifest($manifest);
|
||||
|
||||
if (!empty($manifestData)) {
|
||||
$oScorm->import_manifest(
|
||||
$courseInfo['code'],
|
||||
$maxScore,
|
||||
$sessionId,
|
||||
$userId
|
||||
);
|
||||
$oScorm->set_name($lpName);
|
||||
$oScorm->set_proximity($proximity, $courseId);
|
||||
$oScorm->set_maker($maker, $courseId);
|
||||
//$oScorm->set_jslib('scorm_api.php');
|
||||
|
||||
if ($debug) {
|
||||
error_log('scorm was added');
|
||||
}
|
||||
|
||||
return 1;
|
||||
} else {
|
||||
if ($debug) {
|
||||
error_log('manifest data empty');
|
||||
}
|
||||
|
||||
return 'manifest data empty';
|
||||
}
|
||||
}
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'paramsGetLpList',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_id_name' => [
|
||||
'name' => 'course_id_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'course_id_value' => [
|
||||
'name' => 'course_id_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'session_id_name' => [
|
||||
'name' => 'session_id_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'session_id_value' => [
|
||||
'name' => 'session_id_value',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'lpListItem',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'id' => ['name' => 'id', 'type' => 'xsd:string'],
|
||||
'name' => ['name' => 'name', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'lpList',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:lpListItem[]']],
|
||||
'tns:lpListItem'
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSGetLpList', // method name
|
||||
['params' => 'tns:paramsGetLpList'], // input parameters
|
||||
['return' => 'tns:lpList'], // output parameters
|
||||
'urn:WSLP', // namespace
|
||||
'urn:WSLP#WSGetLpList', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service adds users' // documentation
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
function WSGetLpList($params)
|
||||
{
|
||||
global $debug;
|
||||
if (!WSHelperVerifyKey($params)) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
$courseIdName = $params['course_id_name'];
|
||||
$courseIdValue = $params['course_id_value'];
|
||||
|
||||
$sessionIdName = isset($params['session_id_name']) ? $params['session_id_name'] : null;
|
||||
$sessionIdValue = isset($params['session_id_value']) ? $params['session_id_value'] : null;
|
||||
|
||||
$courseInfo = CourseManager::getCourseInfoFromOriginalId(
|
||||
$courseIdValue,
|
||||
$courseIdName
|
||||
);
|
||||
|
||||
if (empty($courseInfo)) {
|
||||
if ($debug) {
|
||||
error_log("Course not found: $courseIdName : $courseIdValue");
|
||||
}
|
||||
|
||||
return 'Course not found';
|
||||
}
|
||||
|
||||
$courseId = $courseInfo['real_id'];
|
||||
|
||||
$sessionId = 0;
|
||||
if (!empty($sessionIdName) && !empty($sessionIdValue)) {
|
||||
$sessionId = SessionManager::get_session_id_from_original_id(
|
||||
$sessionIdValue,
|
||||
$sessionIdName
|
||||
);
|
||||
|
||||
if (empty($sessionId)) {
|
||||
if ($debug) {
|
||||
error_log('Session not found');
|
||||
}
|
||||
|
||||
return 'Session not found';
|
||||
}
|
||||
}
|
||||
|
||||
$list = new LearnpathList(null, $courseInfo, $sessionId);
|
||||
$flatList = $list->get_flat_list();
|
||||
$result = [];
|
||||
foreach ($flatList as $id => $lp) {
|
||||
$result[] = [
|
||||
'id' => $id,
|
||||
'name' => $lp['lp_name'],
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'paramsDeleteLp',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_id_name' => [
|
||||
'name' => 'course_id_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'course_id_value' => [
|
||||
'name' => 'course_id_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'lp_id' => [
|
||||
'name' => 'lp_id',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSDeleteLp', // method name
|
||||
['params' => 'tns:paramsDeleteLp'], // input parameters
|
||||
['return' => 'xsd:string'], // output parameters
|
||||
'urn:WSLP', // namespace
|
||||
'urn:WSLP#WSDeleteLp', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service deletes a LP' // documentation
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
function WSDeleteLp($params)
|
||||
{
|
||||
global $debug;
|
||||
if (!WSHelperVerifyKey($params)) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
$courseIdName = $params['course_id_name'];
|
||||
$courseIdValue = $params['course_id_value'];
|
||||
$lpId = $params['lp_id'];
|
||||
|
||||
$sessionIdName = isset($params['session_id_name']) ? $params['session_id_name'] : null;
|
||||
$sessionIdValue = isset($params['session_id_value']) ? $params['session_id_value'] : null;
|
||||
|
||||
$courseInfo = CourseManager::getCourseInfoFromOriginalId(
|
||||
$courseIdValue,
|
||||
$courseIdName
|
||||
);
|
||||
|
||||
if (empty($courseInfo)) {
|
||||
if ($debug) {
|
||||
error_log("Course not found: $courseIdName : $courseIdValue");
|
||||
}
|
||||
|
||||
return 'Course not found';
|
||||
}
|
||||
$courseId = $courseInfo['real_id'];
|
||||
$courseCode = $courseInfo['code'];
|
||||
|
||||
$sessionId = 0;
|
||||
|
||||
/*
|
||||
if (!empty($sessionIdName) && !empty($sessionIdValue)) {
|
||||
$sessionId = SessionManager::get_session_id_from_original_id(
|
||||
$sessionIdValue,
|
||||
$sessionIdName
|
||||
);
|
||||
|
||||
if (empty($sessionId)) {
|
||||
|
||||
if ($debug) error_log('Session not found');
|
||||
return 'Session not found';
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
$lp = new learnpath($courseCode, $lpId, null);
|
||||
if ($lp) {
|
||||
if ($debug) {
|
||||
error_log("LP deleted $lpId");
|
||||
}
|
||||
|
||||
$course_dir = $courseInfo['directory'].'/document';
|
||||
$sys_course_path = api_get_path(SYS_COURSE_PATH);
|
||||
$base_work_dir = $sys_course_path.$course_dir;
|
||||
|
||||
$items = $lp->get_flat_ordered_items_list($lpId, 0, $courseId);
|
||||
|
||||
if (!empty($items)) {
|
||||
/** @var $item learnpathItem */
|
||||
foreach ($items as $itemId) {
|
||||
$item = new learnpathItem($itemId, null, $courseId);
|
||||
|
||||
if ($item) {
|
||||
$documentId = $item->get_path();
|
||||
|
||||
if ($debug) {
|
||||
error_log("lp item id found #$itemId");
|
||||
}
|
||||
|
||||
$documentInfo = DocumentManager::get_document_data_by_id(
|
||||
$documentId,
|
||||
$courseInfo['code'],
|
||||
false,
|
||||
$sessionId
|
||||
);
|
||||
|
||||
if (!empty($documentInfo)) {
|
||||
if ($debug) {
|
||||
error_log("Document id deleted #$documentId");
|
||||
}
|
||||
DocumentManager::delete_document(
|
||||
$courseInfo,
|
||||
null,
|
||||
$base_work_dir,
|
||||
$sessionId,
|
||||
$documentId
|
||||
);
|
||||
} else {
|
||||
if ($debug) {
|
||||
error_log("No document found for id #$documentId");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($debug) {
|
||||
error_log("Document not found #$itemId");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$lp->delete($courseInfo, $lpId, 'remove');
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'lpItem',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'data' => ['name' => 'data', 'type' => 'xsd:string'],
|
||||
'title' => ['name' => 'title', 'type' => 'xsd:string'],
|
||||
'filename' => ['name' => 'filename', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'lpItemList',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:lpItem[]']],
|
||||
'tns:lpItem'
|
||||
);
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'paramsCreateLp',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_id_name' => [
|
||||
'name' => 'course_id_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'course_id_value' => [
|
||||
'name' => 'course_id_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
/*'session_id_name' => array(
|
||||
'name' => 'session_id_name',
|
||||
'type' => 'xsd:string',
|
||||
),
|
||||
'session_id_value' => array(
|
||||
'name' => 'session_id_value',
|
||||
'type' => 'xsd:string',
|
||||
),*/
|
||||
'lp_name' => [
|
||||
'name' => 'lp_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'lp_item_list' => [
|
||||
'name' => 'lp_item_list',
|
||||
'type' => 'tns:lpItemList',
|
||||
],
|
||||
'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSCreateLp', // method name
|
||||
['params' => 'tns:paramsCreateLp'], // input parameters
|
||||
['return' => 'xsd:string'], // output parameters
|
||||
'urn:WSLP', // namespace
|
||||
'urn:WSLP#WSCreateLp', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service creates a LP' // documentation
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return soap_fault|null
|
||||
*/
|
||||
function WSCreateLp($params)
|
||||
{
|
||||
global $debug;
|
||||
if (!WSHelperVerifyKey($params)) {
|
||||
return return_error(WS_ERROR_SECRET_KEY);
|
||||
}
|
||||
|
||||
if ($debug) {
|
||||
error_log('WSCreateLp');
|
||||
}
|
||||
|
||||
$courseIdName = $params['course_id_name'];
|
||||
$courseIdValue = $params['course_id_value'];
|
||||
$lpName = $params['lp_name'];
|
||||
$lpItemList = $params['lp_item_list'];
|
||||
|
||||
/*$sessionIdName = isset($params['session_id_name']) ? $params['session_id_name'] : null;
|
||||
$sessionIdValue = isset($params['session_id_value']) ? $params['session_id_value'] : null;*/
|
||||
|
||||
$courseInfo = CourseManager::getCourseInfoFromOriginalId(
|
||||
$courseIdValue,
|
||||
$courseIdName
|
||||
);
|
||||
|
||||
if (empty($courseInfo)) {
|
||||
if ($debug) {
|
||||
error_log('Course not found');
|
||||
}
|
||||
}
|
||||
|
||||
$userId = 1;
|
||||
$courseId = $courseInfo['real_id'];
|
||||
$courseCode = $courseInfo['code'];
|
||||
|
||||
/*$sessionId = 0;
|
||||
if (!empty($sessionIdName) && !empty($sessionIdValue)) {
|
||||
$sessionId = SessionManager::get_session_id_from_original_id(
|
||||
$sessionIdValue,
|
||||
$sessionIdName
|
||||
);
|
||||
|
||||
if (empty($sessionId)) {
|
||||
|
||||
if ($debug) {
|
||||
error_log('Session not found');
|
||||
}
|
||||
|
||||
return 'Session not found';
|
||||
}
|
||||
}*/
|
||||
if ($debug) {
|
||||
error_log('add_lp');
|
||||
}
|
||||
$lpId = learnpath::add_lp(
|
||||
$courseCode,
|
||||
$lpName,
|
||||
'',
|
||||
'chamilo',
|
||||
'manual',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
0,
|
||||
$userId
|
||||
);
|
||||
|
||||
if ($lpId) {
|
||||
if ($debug) {
|
||||
error_log('LP created');
|
||||
}
|
||||
|
||||
$lp = new learnpath($courseCode, $lpId, null);
|
||||
|
||||
$previousId = 0;
|
||||
foreach ($lpItemList as $lpItem) {
|
||||
$info = pathinfo($lpItem['filename']);
|
||||
$extension = $info['extension'];
|
||||
$data = base64_decode($lpItem['data']);
|
||||
|
||||
if ($debug) {
|
||||
error_log('create_document: '.$info['filename']);
|
||||
}
|
||||
|
||||
$documentId = $lp->create_document(
|
||||
$courseInfo,
|
||||
$data,
|
||||
$info['filename'],
|
||||
$extension,
|
||||
0,
|
||||
$userId
|
||||
);
|
||||
|
||||
if ($documentId) {
|
||||
if ($debug) {
|
||||
error_log("Document created $documentId");
|
||||
|
||||
$itemId = $lp->add_item(
|
||||
null,
|
||||
$previousId,
|
||||
'document',
|
||||
$documentId,
|
||||
$lpItem['title'],
|
||||
'',
|
||||
'',
|
||||
0,
|
||||
$userId
|
||||
);
|
||||
|
||||
$previousId = $itemId;
|
||||
|
||||
if ($itemId) {
|
||||
if ($debug) {
|
||||
error_log("Item added");
|
||||
}
|
||||
} else {
|
||||
if ($debug) {
|
||||
error_log("Item not added");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($debug) {
|
||||
error_log("Document NOT created");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
} else {
|
||||
if ($debug) {
|
||||
error_log('LP not created');
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Use the request to (try to) invoke the service
|
||||
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
|
||||
// If you send your data in utf8 then this value must be false.
|
||||
if (isset($_configuration['registration.soap.php.decode_utf8'])) {
|
||||
if ($_configuration['registration.soap.php.decode_utf8']) {
|
||||
$server->decode_utf8 = true;
|
||||
} else {
|
||||
$server->decode_utf8 = false;
|
||||
}
|
||||
}
|
||||
$server->service($HTTP_RAW_POST_DATA);
|
||||
7417
main/webservices/registration.soap.php
Normal file
7417
main/webservices/registration.soap.php
Normal file
File diff suppressed because it is too large
Load Diff
130
main/webservices/soap.php
Normal file
130
main/webservices/soap.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/webservice.php';
|
||||
|
||||
/**
|
||||
* SOAP error handler. Handles an error sending a SOAP fault.
|
||||
*/
|
||||
class WSSoapErrorHandler implements WSErrorHandler
|
||||
{
|
||||
/**
|
||||
* Handles the error by sending a SOAP fault through the server.
|
||||
*
|
||||
* @param WSError Error to handle
|
||||
*/
|
||||
public function handle($error)
|
||||
{
|
||||
$server = WSSoapServer::singleton();
|
||||
$server->fault(strval($error->code), $error->message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SOAP server wrapper implementing a Singleton.
|
||||
*/
|
||||
class WSSoapServer
|
||||
{
|
||||
/**
|
||||
* SOAP server instance.
|
||||
*
|
||||
* @var soap_server
|
||||
*/
|
||||
private static $_instance;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Singleton method.
|
||||
*/
|
||||
public static function singleton()
|
||||
{
|
||||
if (!isset(self::$_instance)) {
|
||||
self::$_instance = new soap_server();
|
||||
// Set the error handler
|
||||
WSError::setErrorHandler(new WSSoapErrorHandler());
|
||||
// Configure the service
|
||||
self::$_instance->configureWSDL('WSService', 'urn:WSService');
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
}
|
||||
|
||||
$s = WSSoapServer::singleton();
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'code' => ['name' => 'code', 'type' => 'xsd:int'],
|
||||
'message' => ['name' => 'message', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'extras',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'field_name' => ['name' => 'field_name', 'type' => 'xsd:string'],
|
||||
'field_value' => ['name' => 'field_value', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'extra_field',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[
|
||||
[
|
||||
'ref' => 'SOAP-ENC:arrayType',
|
||||
'wsdl:arrayType' => 'tns:extras[]',
|
||||
],
|
||||
],
|
||||
'tns:extras'
|
||||
);
|
||||
|
||||
/*
|
||||
$s->wsdl->addComplexType(
|
||||
'extra_field',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
array(
|
||||
'field_name' => array('name' => 'field_name', 'type' => 'xsd:string'),
|
||||
'field_value' => array('name' => 'field_value', 'type' => 'xsd:string')
|
||||
)
|
||||
);
|
||||
*/
|
||||
|
||||
$s->register(
|
||||
'WS.test',
|
||||
[],
|
||||
['return' => 'xsd:string']
|
||||
);
|
||||
|
||||
require_once __DIR__.'/soap_user.php';
|
||||
require_once __DIR__.'/soap_course.php';
|
||||
require_once __DIR__.'/soap_session.php';
|
||||
require_once __DIR__.'/soap_report.php';
|
||||
|
||||
// Use the request to (try to) invoke the service
|
||||
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
|
||||
$s->service($HTTP_RAW_POST_DATA);
|
||||
48
main/webservices/soap.test.php
Normal file
48
main/webservices/soap.test.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Test script for soap.php.
|
||||
*
|
||||
* @author Yannick Warnier <yannick.warnier@beeznest.com>
|
||||
*
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
exit; //remove to enable
|
||||
// Include the necessary files, assuming this script is located in main/lp/ or something like that
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
global $_configuration;
|
||||
|
||||
// First build the signature to use with the webservice. We assume
|
||||
// we are calling the webservice from the same server, so getting
|
||||
// the IP (part of the signature) can be done through $_SERVER['REMOTE_ADDR']
|
||||
$ip = trim($_SERVER['REMOTE_ADDR']);
|
||||
$signature = sha1($ip.$_configuration['security_key']);
|
||||
|
||||
// Prepare the arguments to the webservice, based on the user ID (int), the course ID (int), the learnpath_id and the learnpath_item_id:
|
||||
$uid = 1; // set to your user ID
|
||||
$cid = 1; // set to your course ID
|
||||
$lpid = 1; // set to your learnpath ID
|
||||
$lpiid = 1; // set to your learnpath item ID
|
||||
|
||||
// Build the server's SOAP script address
|
||||
$server = api_get_path(WEB_CODE_PATH).'webservices/registration.soap.php?wsdl';
|
||||
|
||||
/**
|
||||
* Call the webservice.
|
||||
*/
|
||||
|
||||
// Init the SOAP connection
|
||||
$client = new SoapClient($server, ['cache_wsdl' => WSDL_CACHE_NONE]);
|
||||
|
||||
// Call the function we want with the right params...
|
||||
try {
|
||||
$response = $client->{'WSSearchSession'}(['term' => 'a', 'extrafields' => [], 'secret_key' => $signature]);
|
||||
} catch (Exception $e) {
|
||||
error_log(print_r($e->getMessage(), 1));
|
||||
}
|
||||
//$response = $client->{'WSReport.GetLearnpathStatusSingleItem'}($signature, 'chamilo_user_id', $uid, 'chamilo_course_id', $cid, $lpid, $lpiid);
|
||||
//$response = $client->{'WSReport.GetLearnpathProgress'}($signature, 'chamilo_user_id', $uid, 'chamilo_course_id', $cid, $lpid);
|
||||
//$response = $client->{'WSReport.GetLearnpathHighestLessonLocation'}($signature, 'chamilo_user_id', $uid, 'chamilo_course_id', $cid, $lpid);
|
||||
// Print the output, or do whatever you like with it (it's the status for this item):
|
||||
echo '<pre>'.print_r($response, 1).'</pre>';
|
||||
// This should print "complete", "incomplete" or any other active status.
|
||||
264
main/webservices/soap_course.php
Normal file
264
main/webservices/soap_course.php
Normal file
@@ -0,0 +1,264 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Configures the WSCourse SOAP service.
|
||||
*
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/webservice_course.php';
|
||||
require_once __DIR__.'/soap.php';
|
||||
|
||||
/**
|
||||
* Configures the WSCourse SOAP service.
|
||||
*/
|
||||
$s = WSSoapServer::singleton();
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_id',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_id_field_name' => ['name' => 'course_id_field_name', 'type' => 'xsd:string'],
|
||||
'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'],
|
||||
'result' => ['name' => 'result', 'type' => 'tns:result'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_result_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course_result[]']],
|
||||
'tns:course_result'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCourse.DeleteCourse',
|
||||
['secret_key' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string'],
|
||||
[],
|
||||
'urn:WSService', // namespace
|
||||
'urn:WSService#WSCourse.DeleteCourse', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'Delete a course in chamilo' // documentation
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCourse.DeleteCourses',
|
||||
['secret_key' => 'xsd:string', 'courses' => 'tns:course_id[]'],
|
||||
['return' => 'tns:course_result_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCourse.CreateCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'title' => 'xsd:string',
|
||||
'category_code' => 'xsd:string',
|
||||
'wanted_code' => 'xsd:string',
|
||||
'tutor_name' => 'xsd:string',
|
||||
'course_admin_user_id_field_name' => 'xsd:string',
|
||||
'course_admin_user_id_value' => 'xsd:string',
|
||||
'language' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'extras' => 'tns:extra_field',
|
||||
],
|
||||
['return' => 'xsd:int']
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_create',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'title' => ['name' => 'title', 'type' => 'xsd:string'],
|
||||
'category_code' => ['name' => 'category_code', 'type' => 'xsd:string'],
|
||||
'wanted_code' => ['name' => 'wanted_code', 'type' => 'xsd:int'],
|
||||
'tutor_name' => ['name' => 'tutor_name', 'type' => 'xsd:string'],
|
||||
'course_admin_user_id_field_name' => ['name' => 'course_admin_user_id_field_name', 'type' => 'xsd:string'],
|
||||
'course_admin_user_id_value' => ['name' => 'course_admin_user_id_value', 'type' => 'xsd:string'],
|
||||
'language' => ['name' => 'language', 'type' => 'xsd:string'],
|
||||
'course_id_field_name' => ['name' => 'course_id_field_name', 'type' => 'xsd:string'],
|
||||
'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'],
|
||||
'extras' => ['name' => 'extras', 'type' => 'tns:extra_field'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_create_result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'],
|
||||
'course_id_generated' => ['name' => 'course_id_generated', 'type' => 'xsd:int'],
|
||||
'result' => ['name' => 'result', 'type' => 'tns:result'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_create_result_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course_create_result[]']],
|
||||
'tns:course_create_result'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCourse.CreateCourses',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'courses' => 'tns:course_create[]',
|
||||
],
|
||||
['return' => 'tns:course_create_result_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCourse.EditCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'title' => 'xsd:string',
|
||||
'category_code' => 'xsd:string',
|
||||
'department_name' => 'xsd:string',
|
||||
'department_url' => 'xsd:string',
|
||||
'language' => 'xsd:string',
|
||||
'visibility' => 'xsd:int',
|
||||
'subscribe' => 'xsd:int',
|
||||
'unsubscribe' => 'xsd:int',
|
||||
'visual_code' => 'xsd:string',
|
||||
'extras' => 'tns:extra_field',
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'id' => ['name' => 'id', 'type' => 'xsd:int'],
|
||||
'code' => ['name' => 'code', 'type' => 'xsd:string'],
|
||||
'title' => ['name' => 'title', 'type' => 'xsd:string'],
|
||||
'language' => ['name' => 'language', 'type' => 'xsd:string'],
|
||||
'visibility' => ['name' => 'visibility', 'type' => 'xsd:int'],
|
||||
'category_name' => ['name' => 'category_name', 'type' => 'xsd:string'],
|
||||
'number_students' => ['name' => 'number_students', 'type' => 'xsd:int'],
|
||||
'external_course_id' => ['name' => 'external_course_id', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course[]']],
|
||||
'tns:course'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCourse.ListCourses',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'visibilities' => 'xsd:string',
|
||||
],
|
||||
['return' => 'tns:course_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCourse.SubscribeUserToCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'status' => 'xsd:int',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCourse.UnsubscribeUserFromCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_description',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_desc_id' => ['name' => 'course_desc_id', 'type' => 'xsd:int'],
|
||||
'course_desc_title' => ['name' => 'course_desc_title', 'type' => 'xsd:string'],
|
||||
'course_desc_content' => ['name' => 'course_desc_content', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_description_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course_description[]']],
|
||||
'tns:course_description'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCourse.GetCourseDescriptions',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
],
|
||||
['return' => 'tns:course_description_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSCourse.EditCourseDescription',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'course_desc_id' => 'xsd:int',
|
||||
'course_desc_title' => 'xsd:string',
|
||||
'course_desc_content' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
261
main/webservices/soap_report.php
Normal file
261
main/webservices/soap_report.php
Normal file
@@ -0,0 +1,261 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Configures the WSReport SOAP service.
|
||||
*
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/webservice_report.php';
|
||||
require_once __DIR__.'/soap.php';
|
||||
$s = WSSoapServer::singleton();
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_id',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'user_id_field_name' => [
|
||||
'name' => 'user_id_field_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'user_id_value' => [
|
||||
'name' => 'user_id_value',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'course_id',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'course_id_field_name' => [
|
||||
'name' => 'course_id_field_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'course_id_value' => [
|
||||
'name' => 'course_id_value',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'session_id',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'session_id_field_name' => [
|
||||
'name' => 'session_id_field_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'session_id_value' => [
|
||||
'name' => 'session_id_value',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
/*
|
||||
$s->wsdl->addComplexType(
|
||||
'user_result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
array(
|
||||
'user_id_value' => array('name' => 'user_id_value', 'type' => 'xsd:string'),
|
||||
'result' => array('name' => 'result', 'type' => 'tns:result')
|
||||
)
|
||||
);*/
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'id' => ['name' => 'id', 'type' => 'xsd:string'],
|
||||
'title' => ['name' => 'title', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'progress_result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'progress_bar_mode' => [
|
||||
'name' => 'progress_bar_mode',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'progress_db' => ['name' => 'progress_db', 'type' => 'xsd:string'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'score_result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'min_score' => ['name' => 'min_score', 'type' => 'xsd:string'],
|
||||
'max_score' => ['name' => 'max_score', 'type' => 'xsd:string'],
|
||||
'mastery_score' => [
|
||||
'name' => 'mastery_score',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'current_score' => [
|
||||
'name' => 'current_score',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_result_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[
|
||||
[
|
||||
'ref' => 'SOAP-ENC:arrayType',
|
||||
'wsdl:arrayType' => 'tns:user_result[]',
|
||||
],
|
||||
],
|
||||
'tns:user_result'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSReport.GetTimeSpentOnPlatform',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSReport.GetTimeSpentOnCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSReport.GetTimeSpentOnCourseInSession',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'session_id_field_name' => 'xsd:string',
|
||||
'session_id_value' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSReport.GetTimeSpentOnLearnpathInCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'learnpath_id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSReport.GetLearnpathsByCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
],
|
||||
['return' => 'tns:user_result_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSReport.GetLearnpathProgress',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'learnpath_id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'tns:progress_result']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSReport.GetLearnpathHighestLessonLocation',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'learnpath_id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSReport.GetLearnpathScoreSingleItem',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'learnpath_id' => 'xsd:string',
|
||||
'learnpath_item_id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'tns:score_result']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSReport.GetLearnpathStatusSingleItem',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'learnpath_id' => 'xsd:string',
|
||||
'learnpath_item_id' => 'xsd:string',
|
||||
],
|
||||
['return' => 'xsd:string']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSReport.test',
|
||||
[],
|
||||
['return' => 'xsd:string']
|
||||
);
|
||||
129
main/webservices/soap_session.php
Normal file
129
main/webservices/soap_session.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Configures the WSSession SOAP service.
|
||||
*
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/webservice_session.php';
|
||||
require_once __DIR__.'/soap.php';
|
||||
|
||||
$s = WSSoapServer::singleton();
|
||||
|
||||
$s->register(
|
||||
'WSSession.CreateSession',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'name' => 'xsd:string',
|
||||
'start_date' => 'xsd:string',
|
||||
'end_date' => 'xsd:string',
|
||||
'nb_days_access_before' => 'xsd:int',
|
||||
'nb_days_access_after' => 'xsd:int',
|
||||
'nolimit' => 'xsd:int',
|
||||
'visibility' => 'xsd:int',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'session_id_field_name' => 'xsd:string',
|
||||
'session_id_value' => 'xsd:string',
|
||||
'extras' => 'tns:extra_field',
|
||||
],
|
||||
['return' => 'xsd:int']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSSession.DeleteSession',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'session_id_field_name' => 'xsd:string',
|
||||
'session_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSSession.EditSession',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'name' => 'xsd:string',
|
||||
'start_date' => 'xsd:string',
|
||||
'end_date' => 'xsd:string',
|
||||
'nb_days_access_before' => 'xsd:int',
|
||||
'nb_days_access_after' => 'xsd:int',
|
||||
'nolimit' => 'xsd:int',
|
||||
'visibility' => 'xsd:int',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'session_id_field_name' => 'xsd:string',
|
||||
'session_id_value' => 'xsd:string',
|
||||
'extras' => 'tns:extra_field',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSSession.SubscribeUserToSession',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'session_id_field_name' => 'xsd:string',
|
||||
'session_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSSession.UnsubscribeUserFromSession',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'session_id_field_name' => 'xsd:string',
|
||||
'session_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSSession.SubscribeTeacherToSessionCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'session_id_field_name' => 'xsd:string',
|
||||
'session_id_value' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSSession.UnsubscribeTeacherFromSessionCourse',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'session_id_field_name' => 'xsd:string',
|
||||
'session_id_value' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSSession.SubscribeCourseToSession',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'session_id_field_name' => 'xsd:string',
|
||||
'session_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSSession.UnsubscribeCourseFromSession',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'course_id_field_name' => 'xsd:string',
|
||||
'course_id_value' => 'xsd:string',
|
||||
'session_id_field_name' => 'xsd:string',
|
||||
'session_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
307
main/webservices/soap_user.php
Normal file
307
main/webservices/soap_user.php
Normal file
@@ -0,0 +1,307 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Configures the WSUser SOAP service.
|
||||
*
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/webservice_user.php';
|
||||
require_once __DIR__.'/soap.php';
|
||||
|
||||
/**
|
||||
* Configures the WSUser SOAP service.
|
||||
*
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
$s = WSSoapServer::singleton();
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_id',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'user_id_field_name' => [
|
||||
'name' => 'user_id_field_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'user_id_value' => [
|
||||
'name' => 'user_id_value',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'user_id_value' => [
|
||||
'name' => 'user_id_value',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'result' => ['name' => 'result', 'type' => 'tns:result'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_result_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[
|
||||
[
|
||||
'ref' => 'SOAP-ENC:arrayType',
|
||||
'wsdl:arrayType' => 'tns:user_result[]',
|
||||
],
|
||||
],
|
||||
'tns:user_result'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSUser.DisableUser',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSUser.DisableUsers',
|
||||
['secret_key' => 'xsd:string', 'users' => 'tns:user_id[]'],
|
||||
['return' => 'tns:user_result_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSUser.EnableUser',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSUser.EnableUsers',
|
||||
['secret_key' => 'xsd:string', 'users' => 'tns:user_id[]'],
|
||||
['return' => 'tns:user_result_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSUser.DeleteUser',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSUser.DeleteUsers',
|
||||
['secret_key' => 'xsd:string', 'users' => 'tns:user_id[]'],
|
||||
['return' => 'tns:user_result_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSUser.CreateUser',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'firstname' => 'xsd:string',
|
||||
'lastname' => 'xsd:string',
|
||||
'status' => 'xsd:int',
|
||||
'loginname' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'encrypt_method' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'visibility' => 'xsd:int',
|
||||
'email' => 'xsd:string',
|
||||
'language' => 'xsd:string',
|
||||
'phone' => 'xsd:string',
|
||||
'expiration_date' => 'xsd:string',
|
||||
'extras' => 'tns:extra_field',
|
||||
],
|
||||
['return' => 'xsd:int']
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_create',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
|
||||
'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
|
||||
'status' => ['name' => 'status', 'type' => 'xsd:int'],
|
||||
'loginname' => ['name' => 'loginname', 'type' => 'xsd:string'],
|
||||
'password' => ['name' => 'password', 'type' => 'xsd:string'],
|
||||
'encrypt_method' => [
|
||||
'name' => 'encrypt_method',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'user_id_field_name' => [
|
||||
'name' => 'user_id_field_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'user_id_value' => [
|
||||
'name' => 'user_id_value',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'visibility' => ['name' => 'visibility', 'type' => 'xsd:int'],
|
||||
'email' => ['name' => 'email', 'type' => 'xsd:string'],
|
||||
'language' => ['name' => 'language', 'type' => 'xsd:string'],
|
||||
'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
|
||||
'expiration_date' => [
|
||||
'name' => 'expiration_date',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'extras' => ['name' => 'extras', 'type' => 'tns:extra_field'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_create_result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'user_id_value' => [
|
||||
'name' => 'user_id_value',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'user_id_generated' => [
|
||||
'name' => 'user_id_generated',
|
||||
'type' => 'xsd:int',
|
||||
],
|
||||
'result' => ['name' => 'result', 'type' => 'tns:result'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_create_result_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[
|
||||
[
|
||||
'ref' => 'SOAP-ENC:arrayType',
|
||||
'wsdl:arrayType' => 'tns:user_create_result[]',
|
||||
],
|
||||
],
|
||||
'tns:user_create_result'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSUser.CreateUsers',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'users' => 'tns:user_create[]',
|
||||
],
|
||||
['return' => 'tns:user_create_result_array']
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSUser.EditUser',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'user_id_field_name' => 'xsd:string',
|
||||
'user_id_value' => 'xsd:string',
|
||||
'firstname' => 'xsd:string',
|
||||
'lastname' => 'xsd:string',
|
||||
'status' => 'xsd:int',
|
||||
'loginname' => 'xsd:string',
|
||||
'password' => 'xsd:string',
|
||||
'encrypt_method' => 'xsd:string',
|
||||
'email' => 'xsd:string',
|
||||
'language' => 'xsd:string',
|
||||
'phone' => 'xsd:string',
|
||||
'expiration_date' => 'xsd:string',
|
||||
'extras' => 'tns:extra_field',
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_edit',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'user_id_field_name' => [
|
||||
'name' => 'user_id_field_name',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'user_id_value' => [
|
||||
'name' => 'user_id_value',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
|
||||
'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
|
||||
'status' => ['name' => 'status', 'type' => 'xsd:int'],
|
||||
'loginname' => ['name' => 'loginname', 'type' => 'xsd:string'],
|
||||
'password' => ['name' => 'password', 'type' => 'xsd:string'],
|
||||
'encrypt_method' => [
|
||||
'name' => 'encrypt_method',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'email' => ['name' => 'email', 'type' => 'xsd:string'],
|
||||
'language' => ['name' => 'language', 'type' => 'xsd:string'],
|
||||
'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
|
||||
'expiration_date' => [
|
||||
'name' => 'expiration_date',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'extras' => ['name' => 'extras', 'type' => 'tns:extra_field'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_edit_result',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'user_id_value' => [
|
||||
'name' => 'user_id_value',
|
||||
'type' => 'xsd:string',
|
||||
],
|
||||
'result' => ['name' => 'result', 'type' => 'tns:result'],
|
||||
]
|
||||
);
|
||||
|
||||
$s->wsdl->addComplexType(
|
||||
'user_edit_result_array',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[
|
||||
[
|
||||
'ref' => 'SOAP-ENC:arrayType',
|
||||
'wsdl:arrayType' => 'tns:user_edit_result[]',
|
||||
],
|
||||
],
|
||||
'tns:user_edit_result'
|
||||
);
|
||||
|
||||
$s->register(
|
||||
'WSUser.EditUsers',
|
||||
[
|
||||
'secret_key' => 'xsd:string',
|
||||
'users' => 'tns:user_edit[]',
|
||||
],
|
||||
['return' => 'tns:user_edit_result_array']
|
||||
);
|
||||
64
main/webservices/test.php
Normal file
64
main/webservices/test.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
exit;
|
||||
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once '../inc/conf/configuration.php';
|
||||
|
||||
?>
|
||||
<html>
|
||||
<body>
|
||||
<div class="results">
|
||||
<?php
|
||||
$server = api_get_path(WEB_CODE_PATH).'webservices/';
|
||||
$serversys = api_get_path(SYS_CODE_PATH).'webservices/';
|
||||
//$script = 'registration.soap.php';
|
||||
$script = isset($_POST['script']) ? $_POST['script'] : false;
|
||||
$function = isset($_POST['function']) ? $_POST['function'] : false;
|
||||
|
||||
$contact = $server.$script.'?wsdl';
|
||||
|
||||
$client = new nusoap_client($contact);
|
||||
$err = $client->getError();
|
||||
if ($err) {
|
||||
// Display the error
|
||||
echo '<h2>Constructor error</h2><pre>'.$err.'</pre>';
|
||||
// At this point, you know the call that follows will fail
|
||||
}
|
||||
$response = [];
|
||||
if (!empty($function)) {
|
||||
$response = $client->call($function);
|
||||
echo '<pre>';
|
||||
print_r($response);
|
||||
echo '#</pre>';
|
||||
} else {
|
||||
echo "empty function $function";
|
||||
}
|
||||
|
||||
$list = scandir($serversys);
|
||||
$scripts = [];
|
||||
foreach ($list as $item) {
|
||||
if (substr($item, 0, 1) == '.') {
|
||||
continue;
|
||||
}
|
||||
if (substr($item, -8) == 'soap.php') {
|
||||
$scripts[] = $item;
|
||||
}
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
|
||||
<form method="POST" action="">
|
||||
<label for="script">Script</label>
|
||||
<select name="script">
|
||||
<?php
|
||||
foreach ($scripts as $script) {
|
||||
echo '<option value="'.$script.'">'.$script.'</script>';
|
||||
}
|
||||
?>
|
||||
</select><br />
|
||||
<label for="function">Function</label>
|
||||
<input type="text" name="function" value="<?php echo $function; ?>"></input><br />
|
||||
<label for="param[0]">Param 0</label>
|
||||
<input type="text" name="param[0]" ></input><br />
|
||||
<input type="submit" name="submit" value="Send"/>
|
||||
</form>
|
||||
18
main/webservices/testip.php
Normal file
18
main/webservices/testip.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
$ip = '';
|
||||
if (!empty($_SERVER['REMOTE_ADDR'])) {
|
||||
$ip = trim($_SERVER['REMOTE_ADDR']);
|
||||
}
|
||||
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||
if (filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6) == $_SERVER['HTTP_X_FORWARDED_FOR']) {
|
||||
list($ip1, $ip2) = preg_split('/,/', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
||||
$ip = trim($ip1);
|
||||
}
|
||||
}
|
||||
if (!empty($ip) && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
|
||||
echo htmlentities($ip);
|
||||
}
|
||||
170
main/webservices/user_import/import.lib.php
Normal file
170
main/webservices/user_import/import.lib.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt*/
|
||||
|
||||
/**
|
||||
* Validates imported data.
|
||||
*/
|
||||
function validate_data($users)
|
||||
{
|
||||
global $defined_auth_sources;
|
||||
$errors = [];
|
||||
$usernames = [];
|
||||
if (is_array($users)) {
|
||||
foreach ($users as $index => $user) {
|
||||
// 1. Check whether mandatory fields have been set.
|
||||
$mandatory_fields = ['LastName', 'FirstName'];
|
||||
if (api_get_setting('registration', 'email') == 'true') {
|
||||
$mandatory_fields[] = 'Email';
|
||||
}
|
||||
foreach ($mandatory_fields as $key => $field) {
|
||||
if (!isset($user[$field]) || strlen($user[$field]) == 0) {
|
||||
$user['error'] = get_lang($field.'Mandatory');
|
||||
$errors[] = $user;
|
||||
}
|
||||
}
|
||||
// 2. Check username.
|
||||
if (!UserManager::is_username_empty($user['UserName'])) {
|
||||
// 2.1. Check whether username was used twice in the import file.
|
||||
if (isset($usernames[$user['UserName']])) {
|
||||
$user['error'] = get_lang('UserNameUsedTwice');
|
||||
$errors[] = $user;
|
||||
}
|
||||
$usernames[$user['UserName']] = 1;
|
||||
// 2.2. Check whether username is allready in use in database.
|
||||
if (!UserManager::is_username_available($user['UserName'])) {
|
||||
$user['error'] = get_lang('UserNameNotAvailable');
|
||||
$errors[] = $user;
|
||||
}
|
||||
// 2.3. Check whether username is too long.
|
||||
if (UserManager::is_username_too_long($user['UserName'])) {
|
||||
$user['error'] = get_lang('UserNameTooLong');
|
||||
$errors[] = $user;
|
||||
}
|
||||
}
|
||||
// 3. Check status.
|
||||
if (isset($user['Status']) && !api_status_exists($user['Status'])) {
|
||||
$user['error'] = get_lang('WrongStatus');
|
||||
$errors[] = $user;
|
||||
}
|
||||
// 5. Check authentication source.
|
||||
if (isset($user['AuthSource']) && strlen($user['AuthSource']) != 0) {
|
||||
if (!in_array($user['AuthSource'], $defined_auth_sources)) {
|
||||
$user['error'] = get_lang('AuthSourceNotAvailable');
|
||||
$errors[] = $user;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds missing user-information (which isn't required, like password, username, etc).
|
||||
*/
|
||||
function complete_missing_data($user)
|
||||
{
|
||||
// 1. Create a username if necessary.
|
||||
if (UserManager::is_username_empty($user['UserName'])) {
|
||||
$user['UserName'] = UserManager::create_unique_username(
|
||||
$user['FirstName'],
|
||||
$user['LastName']
|
||||
);
|
||||
}
|
||||
// 2. Generate a password if necessary.
|
||||
if (!isset($user['Password']) || strlen($user['Password']) == 0) {
|
||||
$user['Password'] = api_generate_password();
|
||||
}
|
||||
// 3. set status if not allready set.
|
||||
if (!isset($user['Status']) || strlen($user['Status']) == 0) {
|
||||
$user['Status'] = 'user';
|
||||
}
|
||||
// 4. Set authsource if not allready set.
|
||||
if (!isset($user['AuthSource']) || strlen($user['AuthSource']) == 0) {
|
||||
$user['AuthSource'] = PLATFORM_AUTH_SOURCE;
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the imported data.
|
||||
*
|
||||
* @param array
|
||||
*/
|
||||
function save_data($users)
|
||||
{
|
||||
if (is_array($users)) {
|
||||
foreach ($users as $index => $user) {
|
||||
$user = complete_missing_data($user);
|
||||
$user['Status'] = api_status_key($user['Status']);
|
||||
$user_id = UserManager::create_user(
|
||||
$user['FirstName'],
|
||||
$user['LastName'],
|
||||
$user['Status'],
|
||||
$user['Email'],
|
||||
$user['UserName'],
|
||||
$user['Password'],
|
||||
$user['OfficialCode'],
|
||||
api_get_setting('PlatformLanguage'),
|
||||
$user['PhoneNumber'],
|
||||
'',
|
||||
$user['AuthSource']
|
||||
);
|
||||
|
||||
if (!empty($user['Courses'])) {
|
||||
foreach ($user['Courses'] as $course) {
|
||||
if (CourseManager::course_exists($course)) {
|
||||
CourseManager::subscribeUser(
|
||||
$user_id,
|
||||
$course,
|
||||
$user['Status']
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Hard-coded French texts.
|
||||
|
||||
// Qualite
|
||||
if (!empty($user['Qualite'])) {
|
||||
UserManager::update_extra_field_value($user_id, 'qualite', $user['Qualite']);
|
||||
}
|
||||
|
||||
// Categorie
|
||||
if (!empty($user['Categorie'])) {
|
||||
UserManager::update_extra_field_value($user_id, 'categorie', $user['Categorie']);
|
||||
}
|
||||
|
||||
// Etat
|
||||
if (!empty($user['Etat'])) {
|
||||
UserManager::update_extra_field_value($user_id, 'etat', $user['Etat']);
|
||||
}
|
||||
|
||||
// Niveau
|
||||
if (!empty($user['Niveau'])) {
|
||||
UserManager::update_extra_field_value($user_id, 'niveau', $user['Niveau']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the CSV-file.
|
||||
*
|
||||
* @param string $file Path to the CSV-file
|
||||
*
|
||||
* @return array All user information read from the file
|
||||
*/
|
||||
function parse_csv_data($file)
|
||||
{
|
||||
$users = Import::csvToArray($file);
|
||||
foreach ($users as $index => $user) {
|
||||
if (isset($user['Courses'])) {
|
||||
$user['Courses'] = explode('|', trim($user['Courses']));
|
||||
}
|
||||
$users[$index] = $user;
|
||||
}
|
||||
|
||||
return $users;
|
||||
}
|
||||
7
main/webservices/user_import/index.html
Normal file
7
main/webservices/user_import/index.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0; url=../gradebook.php">
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
66
main/webservices/user_import/service.php
Normal file
66
main/webservices/user_import/service.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* See license terms in /license.txt.
|
||||
*
|
||||
* @author Eric Marguin <eric.marguin@dokeos.com>
|
||||
*/
|
||||
require_once __DIR__.'/../../inc/global.inc.php';
|
||||
|
||||
api_protect_webservices();
|
||||
|
||||
/**
|
||||
* Import users into database from a file located on the server.
|
||||
* Function registered as service.
|
||||
*
|
||||
* @param string The csv (only csv) file containing users tom import
|
||||
* @param string Security key (as found in configuration file)
|
||||
*
|
||||
* @return string Error message
|
||||
*/
|
||||
function import_users_from_file($filepath, $security_key)
|
||||
{
|
||||
$errors_returned = [
|
||||
0 => 'success',
|
||||
1 => 'file import does not exist',
|
||||
2 => 'no users to import',
|
||||
3 => 'wrong datas in file',
|
||||
4 => 'security error',
|
||||
];
|
||||
|
||||
$key = api_get_configuration_value('security_key');
|
||||
|
||||
// Check whether this script is launch by server and security key is ok.
|
||||
if (empty($_SERVER['REMOTE_ADDR']) || $_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR'] || $security_key != $key) {
|
||||
return $errors_returned[4];
|
||||
}
|
||||
|
||||
// Libraries
|
||||
require_once 'import.lib.php';
|
||||
|
||||
// Check is users file exists.
|
||||
if (!is_file($filepath)) {
|
||||
return $errors_returned[1];
|
||||
}
|
||||
|
||||
// Get list of users
|
||||
$users = parse_csv_data($filepath);
|
||||
if (count($users) == 0) {
|
||||
return $errors_returned[2];
|
||||
}
|
||||
|
||||
// Check the datas for each user
|
||||
$errors = validate_data($users);
|
||||
if (count($errors) > 0) {
|
||||
return $errors_returned[3];
|
||||
}
|
||||
|
||||
// Apply modifications in database
|
||||
save_data($users);
|
||||
|
||||
return $errors_returned[0]; // Import successfull
|
||||
}
|
||||
|
||||
$server = new soap_server();
|
||||
$server->register('import_users_from_file');
|
||||
$http_request = (isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '');
|
||||
$server->service($http_request);
|
||||
231
main/webservices/user_info.soap.php
Normal file
231
main/webservices/user_info.soap.php
Normal file
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* This script provides the caller service with user details.
|
||||
* It is set to work with the Chamilo module for Drupal:
|
||||
* http://drupal.org/project/chamilo.
|
||||
*
|
||||
* @author Yannick Warnier <yannick.warnier@dokeos.com>
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
|
||||
api_protect_webservices();
|
||||
|
||||
// Create the server instance
|
||||
$server = new soap_server();
|
||||
// Initialize WSDL support
|
||||
$server->configureWSDL('WSUserInfo', 'urn:WSUserInfo');
|
||||
|
||||
/* Register WSCourseList function */
|
||||
// Register the data structures used by the service
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'courseDetails',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'name' => 'code',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'title',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'url',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'teacher',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'language',
|
||||
'type' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'courseList',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[
|
||||
[
|
||||
'ref' => 'SOAP-ENC:arrayType',
|
||||
'wsdl:arrayType' => 'tns:courseDetails[]',
|
||||
],
|
||||
],
|
||||
'tns:courseDetails'
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSCourseListOfUser', // method name
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'signature' => 'xsd:string',
|
||||
], // input parameters
|
||||
['return' => 'xsd:Array'], // output parameters
|
||||
'urn:WSUserInfo', // namespace
|
||||
'urn:WSUserInfo#WSUserInfo', // soapaction
|
||||
'rpc', // style
|
||||
'encoded', // use
|
||||
'This service returns a list of courses' // documentation
|
||||
);
|
||||
|
||||
/**
|
||||
* Get a list of courses (code, url, title, teacher, language) for a specific
|
||||
* user and return to caller
|
||||
* Function registered as service. Returns strings in UTF-8.
|
||||
*
|
||||
* @param string User name in Chamilo
|
||||
* @param string Signature (composed of the sha1(username+apikey)
|
||||
*
|
||||
* @return array Courses list (code=>[title=>'title',url='http://...',teacher=>'...',language=>''],code=>[...],...)
|
||||
*/
|
||||
function WSCourseListOfUser($username, $signature)
|
||||
{
|
||||
if (empty($username) or empty($signature)) {
|
||||
return -1;
|
||||
}
|
||||
$info = api_get_user_info_from_username($username);
|
||||
$user_id = $info['user_id'];
|
||||
$list = UserManager::get_api_keys($user_id, 'dokeos');
|
||||
$key = '';
|
||||
foreach ($list as $key) {
|
||||
break;
|
||||
}
|
||||
|
||||
$local_key = $username.$key;
|
||||
|
||||
if (!api_is_valid_secret_key($signature, $local_key)) {
|
||||
return -1; // The secret key is incorrect.
|
||||
}
|
||||
|
||||
$courses_list = [];
|
||||
$courses_list_tmp = CourseManager::get_courses_list_by_user_id($user_id);
|
||||
foreach ($courses_list_tmp as $index => $course) {
|
||||
$course_info = CourseManager::get_course_information($course['code']);
|
||||
$courses_list[] = [
|
||||
'code' => $course['code'],
|
||||
'title' => api_utf8_encode($course_info['title']),
|
||||
'url' => api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/',
|
||||
'teacher' => api_utf8_encode($course_info['tutor_name']),
|
||||
'language' => $course_info['course_language'],
|
||||
];
|
||||
}
|
||||
|
||||
return $courses_list;
|
||||
}
|
||||
|
||||
/* Register WSEventsList function */
|
||||
// Register the data structures used by the service
|
||||
$server->wsdl->addComplexType(
|
||||
'eventDetails',
|
||||
'complexType',
|
||||
'struct',
|
||||
'all',
|
||||
'',
|
||||
[
|
||||
'name' => 'datestart',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'dateend',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'title',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'link',
|
||||
'type' => 'xsd:string',
|
||||
'name' => 'coursetitle',
|
||||
'type' => 'xsd:string',
|
||||
]
|
||||
);
|
||||
|
||||
$server->wsdl->addComplexType(
|
||||
'eventsList',
|
||||
'complexType',
|
||||
'array',
|
||||
'',
|
||||
'SOAP-ENC:Array',
|
||||
[],
|
||||
[
|
||||
[
|
||||
'ref' => 'SOAP-ENC:arrayType',
|
||||
'wsdl:arrayType' => 'tns:eventDetails[]',
|
||||
],
|
||||
],
|
||||
'tns:eventDetails'
|
||||
);
|
||||
|
||||
// Register the method to expose
|
||||
$server->register(
|
||||
'WSEventsList',
|
||||
// method name
|
||||
[
|
||||
'username' => 'xsd:string',
|
||||
'signature' => 'xsd:string',
|
||||
'datestart' => 'xsd:int',
|
||||
'dateend' => 'xsd:int',
|
||||
],
|
||||
// input parameters
|
||||
['return' => 'xsd:Array'],
|
||||
// output parameters
|
||||
'urn:WSUserInfo',
|
||||
// namespace
|
||||
'urn:WSUserInfo#WSEventsList',
|
||||
// soapaction
|
||||
'rpc',
|
||||
// style
|
||||
'encoded',
|
||||
// use
|
||||
'This service returns a list of events of the courses the given user is subscribed to' // documentation
|
||||
);
|
||||
|
||||
/**
|
||||
* Get a list of events between two dates for the given username
|
||||
* Function registered as service. Returns strings in UTF-8.
|
||||
*
|
||||
* @param string Username
|
||||
* @param string User's API key (the user's API key)
|
||||
* @param int Start date, in YYYYMMDD format
|
||||
* @param int End date, in YYYYMMDD format
|
||||
*
|
||||
* @return array Events list
|
||||
*/
|
||||
function WSEventsList($username, $signature, $datestart = 0, $dateend = 0)
|
||||
{
|
||||
if (empty($username) or empty($signature)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
$info = api_get_user_info_from_username($username);
|
||||
$user_id = $info['user_id'];
|
||||
$list = UserManager::get_api_keys($user_id, 'dokeos');
|
||||
$key = '';
|
||||
foreach ($list as $key) {
|
||||
break;
|
||||
}
|
||||
|
||||
$local_key = $username.$key;
|
||||
|
||||
if (!api_is_valid_secret_key($signature, $local_key)) {
|
||||
return -1; // The secret key is incorrect.
|
||||
}
|
||||
$events_list = [];
|
||||
|
||||
$user_id = UserManager::get_user_id_from_username($username);
|
||||
if ($user_id === false) {
|
||||
return $events_list;
|
||||
} // Error in user id recovery.
|
||||
$ds = substr($datestart, 0, 4).'-'.substr($datestart, 4, 2).'-'.substr($datestart, 6, 2).' 00:00:00';
|
||||
$de = substr($dateend, 0, 4).'-'.substr($dateend, 4, 2).'-'.substr($dateend, 6, 2).' 00:00:00';
|
||||
$events_list = Agenda::get_personal_agenda_items_between_dates(
|
||||
$user_id,
|
||||
$ds,
|
||||
$de
|
||||
);
|
||||
|
||||
return $events_list;
|
||||
}
|
||||
|
||||
// Use the request to (try to) invoke the service.
|
||||
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
|
||||
$server->service($HTTP_RAW_POST_DATA);
|
||||
28
main/webservices/webservice-auth-ip.conf.dist.php
Normal file
28
main/webservices/webservice-auth-ip.conf.dist.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* This file allows a Chamilo portal admin to authorize access from specific
|
||||
* IPs or ranges of IPs.
|
||||
*/
|
||||
/**
|
||||
* Check no direct access to file using a constant defined in the calling script.
|
||||
*/
|
||||
if (!defined('WS_ERROR_SECRET_KEY')) {
|
||||
exit();
|
||||
}
|
||||
/**
|
||||
* Define here the IPs or ranges that will be authorized to access the
|
||||
* webservice. When this is in place, the security key check will be made on
|
||||
* the string given here in $ws_auth_ip, and not anymore on
|
||||
* $_SERVER['REMOTE_ADDR'], but $_SERVER['REMOTE_ADDR'] will still be checked
|
||||
* against the IP or range provided. It doesn't support IPv6 yet.
|
||||
* If $ws_auth_ip is not defined, this file will be ignored. If $ws_auth_ip *is*
|
||||
* defined, then the only security key expected from the client is the
|
||||
* $_configuration['security_key'] encrypted through SHA1.
|
||||
*
|
||||
* @example
|
||||
* <pre>
|
||||
* $ws_auth_ip = '192.168.1.1/22';
|
||||
* $ws_auth_ip = '192.168.1.5';
|
||||
* $ws_auth_ip = '192.168.1.5,192.168.1.9';
|
||||
* </pre>
|
||||
*/
|
||||
268
main/webservices/webservice.php
Normal file
268
main/webservices/webservice.php
Normal file
@@ -0,0 +1,268 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
|
||||
api_protect_webservices();
|
||||
|
||||
/**
|
||||
* Error returned by one of the methods of the web service.
|
||||
* Contains an error code and an error message.
|
||||
*/
|
||||
class WSError
|
||||
{
|
||||
/**
|
||||
* Error code.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $code;
|
||||
|
||||
/**
|
||||
* Error message.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $message;
|
||||
/**
|
||||
* Error handler. This needs to be a class that implements the interface WSErrorHandler.
|
||||
*
|
||||
* @var WSErrorHandler
|
||||
*/
|
||||
protected static $_handler;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int Error code
|
||||
* @param string Error message
|
||||
*/
|
||||
public function __construct($code, $message)
|
||||
{
|
||||
$this->code = $code;
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the error handler.
|
||||
*
|
||||
* @param WSErrorHandler Error handler
|
||||
*/
|
||||
public static function setErrorHandler($handler)
|
||||
{
|
||||
if ($handler instanceof WSErrorHandler) {
|
||||
self::$_handler = $handler;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the error handler.
|
||||
*
|
||||
* @return WSErrorHandler Error handler
|
||||
*/
|
||||
public static function getErrorHandler()
|
||||
{
|
||||
return self::$_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the error into an array.
|
||||
*
|
||||
* @return array Associative array with code and message
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return ['code' => $this->code, 'message' => $this->message];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface that must be implemented by any error handler.
|
||||
*/
|
||||
interface WSErrorHandler
|
||||
{
|
||||
/**
|
||||
* Handle method.
|
||||
*
|
||||
* @param WSError Error
|
||||
*/
|
||||
public function handle($error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main class of the webservice. Webservice classes extend this class.
|
||||
*/
|
||||
class WS
|
||||
{
|
||||
/**
|
||||
* Chamilo configuration.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_configuration;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->_configuration = $GLOBALS['_configuration'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function. Returns the string success.
|
||||
*
|
||||
* @return string Success
|
||||
*/
|
||||
public function test()
|
||||
{
|
||||
return "success";
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the API key.
|
||||
*
|
||||
* @param string Secret key
|
||||
*
|
||||
* @return mixed WSError in case of failure, null in case of success
|
||||
*/
|
||||
protected function verifyKey($secret_key)
|
||||
{
|
||||
$ip = trim($_SERVER['REMOTE_ADDR']);
|
||||
// if we are behind a reverse proxy, assume it will send the
|
||||
// HTTP_X_FORWARDED_FOR header and use this IP instead
|
||||
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||
list($ip1, $ip2) = preg_split(
|
||||
'/,/',
|
||||
$_SERVER['HTTP_X_FORWARDED_FOR']
|
||||
);
|
||||
$ip = trim($ip1);
|
||||
}
|
||||
$security_key = $ip.$this->_configuration['security_key'];
|
||||
|
||||
if (!api_is_valid_secret_key($secret_key, $security_key)) {
|
||||
return new WSError(1, "API key is invalid");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the real user id based on the user id field name and value.
|
||||
* Note that if the user id field name is "chamilo_user_id", it will use the user id
|
||||
* in the system database.
|
||||
*
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
*
|
||||
* @return mixed System user id if the user was found, WSError otherwise
|
||||
*/
|
||||
protected function getUserId($user_id_field_name, $user_id_value)
|
||||
{
|
||||
if ($user_id_field_name == "chamilo_user_id") {
|
||||
if (UserManager::is_user_id_valid(intval($user_id_value))) {
|
||||
return intval($user_id_value);
|
||||
} else {
|
||||
return new WSError(100, "User not found");
|
||||
}
|
||||
} else {
|
||||
$user_id = UserManager::get_user_id_from_original_id(
|
||||
$user_id_value,
|
||||
$user_id_field_name
|
||||
);
|
||||
if ($user_id == 0) {
|
||||
return new WSError(100, "User not found");
|
||||
} else {
|
||||
return $user_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the real course id based on the course id field name and value.
|
||||
* Note that if the course id field name is "chamilo_course_id", it will use the course id
|
||||
* in the system database.
|
||||
*
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*
|
||||
* @return mixed System course id if the course was found, WSError otherwise
|
||||
*/
|
||||
protected function getCourseId($course_id_field_name, $course_id_value)
|
||||
{
|
||||
if ($course_id_field_name == "chamilo_course_id") {
|
||||
if (CourseManager::get_course_code_from_course_id(
|
||||
intval($course_id_value)
|
||||
) != null
|
||||
) {
|
||||
return intval($course_id_value);
|
||||
} else {
|
||||
return new WSError(200, "Course not found");
|
||||
}
|
||||
} else {
|
||||
$courseId = CourseManager::get_course_code_from_original_id(
|
||||
$course_id_value,
|
||||
$course_id_field_name
|
||||
);
|
||||
if (!empty($courseId)) {
|
||||
return $courseId;
|
||||
} else {
|
||||
return new WSError(200, "Course not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the real session id based on the session id field name and value.
|
||||
* Note that if the session id field name is "chamilo_session_id", it will use the session id
|
||||
* in the system database.
|
||||
*
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
*
|
||||
* @return mixed System session id if the session was found, WSError otherwise
|
||||
*/
|
||||
protected function getSessionId($session_id_field_name, $session_id_value)
|
||||
{
|
||||
if ($session_id_field_name == "chamilo_session_id") {
|
||||
$session = SessionManager::fetch((int) $session_id_value);
|
||||
if (!empty($session)) {
|
||||
return intval($session_id_value);
|
||||
} else {
|
||||
return new WSError(300, "Session not found");
|
||||
}
|
||||
} else {
|
||||
$session_id = SessionManager::getSessionIdFromOriginalId(
|
||||
$session_id_value,
|
||||
$session_id_field_name
|
||||
);
|
||||
if ($session_id == 0) {
|
||||
return new WSError(300, "Session not found");
|
||||
} else {
|
||||
return $session_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles an error by calling the WSError error handler.
|
||||
*
|
||||
* @param WSError Error
|
||||
*/
|
||||
protected function handleError($error)
|
||||
{
|
||||
$handler = WSError::getErrorHandler();
|
||||
$handler->handle($error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a successful result.
|
||||
*
|
||||
* @return array Array with a code of 0 and a message 'Operation was successful'
|
||||
*/
|
||||
protected function getSuccessfulResult()
|
||||
{
|
||||
return ['code' => 0, 'message' => 'Operation was successful'];
|
||||
}
|
||||
}
|
||||
733
main/webservices/webservice_course.php
Normal file
733
main/webservices/webservice_course.php
Normal file
@@ -0,0 +1,733 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/webservice.php';
|
||||
|
||||
/**
|
||||
* Web services available for the Course module. This class extends the WS class.
|
||||
*/
|
||||
class WSCourse extends WS
|
||||
{
|
||||
/**
|
||||
* Deletes a course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*/
|
||||
public function DeleteCourse(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->deleteCourseHelper(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes multiple courses.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param array Array of courses with elements of the form
|
||||
* array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value')
|
||||
*
|
||||
* @return array Array with elements like
|
||||
* array('course_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
|
||||
* Note that if the result array contains a code different
|
||||
* than 0, an error occured
|
||||
*/
|
||||
public function DeleteCourses($secret_key, $courses)
|
||||
{
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$results = [];
|
||||
foreach ($courses as $course) {
|
||||
$result_tmp = [];
|
||||
$result_op = $this->deleteCourseHelper(
|
||||
$course['course_id_field_name'],
|
||||
$course['course_id_value']
|
||||
);
|
||||
$result_tmp['course_id_value'] = $course['course_id_value'];
|
||||
if ($result_op instanceof WSError) {
|
||||
// Return the error in the results
|
||||
$result_tmp['result'] = $result_op->toArray();
|
||||
} else {
|
||||
$result_tmp['result'] = $this->getSuccessfulResult();
|
||||
}
|
||||
$results[] = $result_tmp;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Title
|
||||
* @param string Category code
|
||||
* @param string Wanted code. If it's not defined, it will be generated automatically
|
||||
* @param string Tutor name
|
||||
* @param string Course admin user id field name
|
||||
* @param string Course admin user id value
|
||||
* @param string Course language
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param array Course extra fields
|
||||
*
|
||||
* @return int Course id generated
|
||||
*/
|
||||
public function CreateCourse(
|
||||
$secret_key,
|
||||
$title,
|
||||
$category_code,
|
||||
$wanted_code,
|
||||
$tutor_name,
|
||||
$course_admin_user_id_field_name,
|
||||
$course_admin_user_id_value,
|
||||
$language,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$extras
|
||||
) {
|
||||
// First, verify the secret key
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->createCourseHelper(
|
||||
$title,
|
||||
$category_code,
|
||||
$wanted_code,
|
||||
$tutor_name,
|
||||
$course_admin_user_id_field_name,
|
||||
$course_admin_user_id_value,
|
||||
$language,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$extras
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
} else {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create multiple courses.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param array Courses to be created, with elements following the structure presented in CreateCourse
|
||||
*
|
||||
* @return array Array with elements of the form
|
||||
* array('course_id_value' => 'original value sent', 'course_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful'))
|
||||
*/
|
||||
public function CreateCourses($secret_key, $courses)
|
||||
{
|
||||
// First, verify the secret key
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$results = [];
|
||||
foreach ($courses as $course) {
|
||||
$result_tmp = [];
|
||||
// re-initialize variables just in case
|
||||
$title = $category_code = $wanted_code = $tutor_name = $course_admin_user_id_field_name = $course_admin_user_id_value = $language = $course_id_field_name = $course_id_value = $extras = 0;
|
||||
extract($course);
|
||||
$result = $this->createCourseHelper(
|
||||
$title,
|
||||
$category_code,
|
||||
$wanted_code,
|
||||
$tutor_name,
|
||||
$course_admin_user_id_field_name,
|
||||
$course_admin_user_id_value,
|
||||
$language,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$extras
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$result_tmp['result'] = $result->toArray();
|
||||
$result_tmp['course_id_value'] = $course_id_value;
|
||||
$result_tmp['course_id_generated'] = 0;
|
||||
} else {
|
||||
$result_tmp['result'] = $this->getSuccessfulResult();
|
||||
$result_tmp['course_id_value'] = $course_id_value;
|
||||
$result_tmp['course_id_generated'] = $result;
|
||||
}
|
||||
$results[] = $result_tmp;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits a course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param string Title
|
||||
* @param string Category code
|
||||
* @param string Department name
|
||||
* @param string Department url
|
||||
* @param string Course language
|
||||
* @param int Visibility
|
||||
* @param int Subscribe (0 = denied, 1 = allowed)
|
||||
* @param int Unsubscribe (0 = denied, 1 = allowed)
|
||||
* @param string Visual code
|
||||
* @param array Course extra fields
|
||||
*/
|
||||
public function EditCourse(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$title,
|
||||
$category_code,
|
||||
$department_name,
|
||||
$department_url,
|
||||
$language,
|
||||
$visibility,
|
||||
$subscribe,
|
||||
$unsubscribe,
|
||||
$visual_code,
|
||||
$extras
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->editCourseHelper(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$title,
|
||||
$category_code,
|
||||
$department_name,
|
||||
$department_url,
|
||||
$language,
|
||||
$visibility,
|
||||
$subscribe,
|
||||
$unsubscribe,
|
||||
$visual_code,
|
||||
$extras
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List courses.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string A list of visibility filter we want to apply
|
||||
*
|
||||
* @return array An array with elements of the form
|
||||
* ('id' => 'Course internal id', 'code' => 'Course code', 'title' => 'Course title', 'language' => 'Course language', 'visibility' => 'Course visibility',
|
||||
* 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id')
|
||||
*/
|
||||
public function ListCourses(
|
||||
$secret_key,
|
||||
$visibility = 'public,public-registered,private,closed'
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$visibilities = split(',', $visibility);
|
||||
$vis = [
|
||||
'public' => '3',
|
||||
'public-registered' => '2',
|
||||
'private' => '1',
|
||||
'closed' => '0',
|
||||
];
|
||||
foreach ($visibilities as $p => $visibility) {
|
||||
$visibilities[$p] = $vis[$visibility];
|
||||
}
|
||||
$courses_result = [];
|
||||
$category_names = [];
|
||||
|
||||
$courses = CourseManager::get_courses_list();
|
||||
foreach ($courses as $course) {
|
||||
//skip elements that do not match required visibility
|
||||
if (!in_array($course['visibility'], $visibilities)) {
|
||||
continue;
|
||||
}
|
||||
$course_tmp = [];
|
||||
$course_tmp['id'] = $course['id'];
|
||||
$course_tmp['code'] = $course['code'];
|
||||
$course_tmp['title'] = $course['title'];
|
||||
$course_tmp['language'] = $course['course_language'];
|
||||
$course_tmp['visibility'] = $course['visibility'];
|
||||
|
||||
// Determining category name
|
||||
if ($category_names[$course['category_code']]) {
|
||||
$course_tmp['category_name'] = $category_names[$course['category_code']];
|
||||
} else {
|
||||
$category = CourseManager::get_course_category(
|
||||
$course['category_code']
|
||||
);
|
||||
$category_names[$course['category_code']] = $category['name'];
|
||||
$course_tmp['category_name'] = $category['name'];
|
||||
}
|
||||
|
||||
// Determining number of students registered in course
|
||||
$user_list = CourseManager::get_user_list_from_course_code(
|
||||
$course['code'],
|
||||
0
|
||||
);
|
||||
$course_tmp['number_students'] = count($user_list);
|
||||
|
||||
// Determining external course id - this code misses the external course id field name
|
||||
// $course_tmp['external_course_id'] = CourseManager::get_course_extra_field_value($course_field_name, $course['code']);
|
||||
|
||||
$courses_result[] = $course_tmp;
|
||||
}
|
||||
|
||||
return $courses_result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe user to a course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name. Use "chamilo_course_id" to use internal id
|
||||
* @param string course id value
|
||||
* @param string User id field name. Use "chamilo_user_id" to use internal id
|
||||
* @param string User id value
|
||||
* @param int Status (1 = Teacher, 5 = Student)
|
||||
*/
|
||||
public function SubscribeUserToCourse(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$status
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeUserSubscription(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
1,
|
||||
$status
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsusbscribe user from course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name. Use "chamilo_course_id" to use internal id
|
||||
* @param string course id value
|
||||
* @param string User id field name. Use "chamilo_user_id" to use internal id
|
||||
* @param string User id value
|
||||
*/
|
||||
public function UnsubscribeUserFromCourse(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$user_id_field_name,
|
||||
$user_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeUserSubscription(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
0
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the descriptions of a course, along with their id.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*
|
||||
* @return array Returns an array with elements of the form
|
||||
* array('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content')
|
||||
*/
|
||||
public function GetCourseDescriptions(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
// Course exists, get its descriptions
|
||||
$descriptions = CourseDescription::get_descriptions($course_id);
|
||||
$results = [];
|
||||
foreach ($descriptions as $description) {
|
||||
$results[] = [
|
||||
'course_desc_id' => $description->get_description_type(
|
||||
),
|
||||
'course_desc_title' => $description->get_title(),
|
||||
'course_desc_content' => $description->get_content(),
|
||||
];
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit course description.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param int Category id from course description
|
||||
* @param string Description title
|
||||
* @param string Course description content
|
||||
*/
|
||||
public function EditCourseDescription(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$course_desc_id,
|
||||
$course_desc_title,
|
||||
$course_desc_content
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
// Create the new course description
|
||||
$cd = new CourseDescription();
|
||||
$cd->set_description_type($course_desc_id);
|
||||
$cd->set_title($course_desc_title);
|
||||
$cd->set_content($course_desc_content);
|
||||
$cd->set_session_id(0);
|
||||
// Get course info
|
||||
$course_info = CourseManager::get_course_information(
|
||||
CourseManager::get_course_code_from_course_id($course_id)
|
||||
);
|
||||
$cd->set_course_id($course_info['real_id']);
|
||||
// Check if this course description exists
|
||||
$descriptions = CourseDescription::get_descriptions($course_id);
|
||||
$exists = false;
|
||||
foreach ($descriptions as $description) {
|
||||
if ($description->get_description_type() == $course_desc_id) {
|
||||
$exists = true;
|
||||
}
|
||||
}
|
||||
if (!$exists) {
|
||||
$cd->set_progress(0);
|
||||
$cd->insert();
|
||||
} else {
|
||||
$cd->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a course (helper method).
|
||||
*
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*
|
||||
* @return mixed True if the course was successfully deleted, WSError otherwise
|
||||
*/
|
||||
protected function deleteCourseHelper(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
) {
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$course_code = CourseManager::get_course_code_from_course_id(
|
||||
$course_id
|
||||
);
|
||||
CourseManager::delete_course($course_code);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a course (helper method).
|
||||
*
|
||||
* @param string Title
|
||||
* @param string Category code
|
||||
* @param string Wanted code. If it's not defined, it will be generated automatically
|
||||
* @param string Tutor name
|
||||
* @param string Course admin user id field name
|
||||
* @param string Course admin user id value
|
||||
* @param string Course language
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param array Course extra fields
|
||||
*
|
||||
* @return mixed Generated id if creation was successful, WSError otherwise
|
||||
*/
|
||||
protected function createCourseHelper(
|
||||
$title,
|
||||
$category_code,
|
||||
$wanted_code,
|
||||
$tutor_name,
|
||||
$course_admin_user_id_field_name,
|
||||
$course_admin_user_id_value,
|
||||
$language,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$extras
|
||||
) {
|
||||
// Add the original course id field name and value to the extra fields if needed
|
||||
$extras_associative = [];
|
||||
if ($course_id_field_name != "chamilo_course_id") {
|
||||
$extras_associative[$course_id_field_name] = $course_id_value;
|
||||
}
|
||||
foreach ($extras as $extra) {
|
||||
$extras_associative[$extra['field_name']] = $extra['field_value'];
|
||||
}
|
||||
$course_admin_id = $this->getUserId(
|
||||
$course_admin_user_id_field_name,
|
||||
$course_admin_user_id_value
|
||||
);
|
||||
if ($course_admin_id instanceof WSError) {
|
||||
return $course_admin_id;
|
||||
}
|
||||
if ($wanted_code == '') {
|
||||
$wanted_code = CourseManager::generate_course_code($title);
|
||||
}
|
||||
$result = create_course(
|
||||
$wanted_code,
|
||||
$title,
|
||||
$tutor_name,
|
||||
$category_code,
|
||||
$language,
|
||||
$course_admin_id,
|
||||
$this->_configuration['db_prefix'],
|
||||
0
|
||||
);
|
||||
if (!$result) {
|
||||
return new WSError(202, 'There was an error creating the course');
|
||||
} else {
|
||||
// Update extra fields
|
||||
foreach ($extras_associative as $fname => $fvalue) {
|
||||
CourseManager::update_course_extra_field_value(
|
||||
$result,
|
||||
$fname,
|
||||
$fvalue
|
||||
);
|
||||
}
|
||||
// Get course id
|
||||
$course_info = CourseManager::get_course_information($result);
|
||||
|
||||
return $course_info['real_id'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits a course (helper method).
|
||||
*
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param string Title
|
||||
* @param string Category code
|
||||
* @param string Department name
|
||||
* @param string Department url
|
||||
* @param string Course language
|
||||
* @param int Visibility
|
||||
* @param int Subscribe (0 = denied, 1 = allowed)
|
||||
* @param int Unsubscribe (0 = denied, 1 = allowed)
|
||||
* @param string Visual code
|
||||
* @param array Course extra fields
|
||||
*
|
||||
* @return mixed True in case of success, WSError otherwise
|
||||
*/
|
||||
protected function editCourseHelper(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$title,
|
||||
$category_code,
|
||||
$department_name,
|
||||
$department_url,
|
||||
$language,
|
||||
$visibility,
|
||||
$subscribe,
|
||||
$unsubscribe,
|
||||
$visual_code,
|
||||
$extras
|
||||
) {
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$attributes = [];
|
||||
if (!empty($title)) {
|
||||
$attributes['title'] = $title;
|
||||
}
|
||||
if (!empty($category_code)) {
|
||||
$attributes['category_code'] = $category_code;
|
||||
}
|
||||
if (!empty($department_name)) {
|
||||
$attributes['department_name'] = $department_name;
|
||||
}
|
||||
if (!empty($department_url)) {
|
||||
$attributes['department_url'] = $department_url;
|
||||
}
|
||||
if (!empty($language)) {
|
||||
$attributes['course_language'] = $language;
|
||||
}
|
||||
if ($visibility != '') {
|
||||
$attributes['visibility'] = (int) $visibility;
|
||||
}
|
||||
if ($subscribe != '') {
|
||||
$attributes['subscribe'] = (int) $subscribe;
|
||||
}
|
||||
if ($unsubscribe != '') {
|
||||
$attributes['unsubscribe'] = (int) $unsubscribe;
|
||||
}
|
||||
if (!empty($visual_code)) {
|
||||
$attributes['visual_code'] = $visual_code;
|
||||
}
|
||||
if (!empty($attributes)) {
|
||||
CourseManager::update_attributes($course_id, $attributes);
|
||||
}
|
||||
if (!empty($extras)) {
|
||||
$course_code = CourseManager::get_course_code_from_course_id(
|
||||
$course_id
|
||||
);
|
||||
$extras_associative = [];
|
||||
foreach ($extras as $extra) {
|
||||
$extras_associative[$extra['field_name']] = $extra['field_value'];
|
||||
}
|
||||
foreach ($extras_associative as $fname => $fvalue) {
|
||||
CourseManager::update_extra_field_value(
|
||||
$course_code,
|
||||
$fname,
|
||||
$fvalue
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe or unsubscribe user to a course (helper method).
|
||||
*
|
||||
* @param string Course id field name. Use "chamilo_course_id" to use internal id
|
||||
* @param string course id value
|
||||
* @param string User id field name. Use "chamilo_user_id" to use internal id
|
||||
* @param string User id value
|
||||
* @param int Set to 1 to subscribe, 0 to unsubscribe
|
||||
* @param int Status (STUDENT or TEACHER) Used for subscription only
|
||||
*
|
||||
* @return mixed True if subscription or unsubscription was successful, false otherwise
|
||||
*/
|
||||
protected function changeUserSubscription(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$state,
|
||||
$status = STUDENT
|
||||
) {
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
} else {
|
||||
$course_code = CourseManager::get_course_code_from_course_id(
|
||||
$course_id
|
||||
);
|
||||
if ($state == 0) {
|
||||
// Unsubscribe user
|
||||
CourseManager::unsubscribe_user($user_id, $course_code);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
// Subscribe user
|
||||
if (CourseManager::subscribeUser(
|
||||
$user_id,
|
||||
$course_code,
|
||||
$status
|
||||
)
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return new WSError(
|
||||
203,
|
||||
'An error occured subscribing to this course'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
347
main/webservices/webservice_report.php
Normal file
347
main/webservices/webservice_report.php
Normal file
@@ -0,0 +1,347 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/webservice.php';
|
||||
|
||||
/**
|
||||
* Web services available for the User module. This class extends the WS class.
|
||||
*/
|
||||
class WSReport extends WS
|
||||
{
|
||||
/**
|
||||
* Gets the time spent on the platform by a given user.
|
||||
*
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
*
|
||||
* @return array Array of results
|
||||
*/
|
||||
public function GetTimeSpentOnPlatform($user_id_field_name, $user_id_value)
|
||||
{
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
} else {
|
||||
return Tracking::get_time_spent_on_the_platform($user_id, 'ever');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the time spent in a course by a given user.
|
||||
*
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*
|
||||
* @return array Array of results
|
||||
*/
|
||||
public function GetTimeSpentOnCourse(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
) {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
}
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$course_code = CourseManager::get_course_code_from_course_id(
|
||||
$course_id
|
||||
);
|
||||
}
|
||||
|
||||
return Tracking::get_time_spent_on_the_course($user_id, $course_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the time spent in a course by a given user.
|
||||
*
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*
|
||||
* @return array Array of results
|
||||
*/
|
||||
public function GetTimeSpentOnCourseInSession(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
) {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
}
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$course_code = CourseManager::get_course_code_from_course_id(
|
||||
$course_id
|
||||
);
|
||||
}
|
||||
$session_id = $this->getSessionId(
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
);
|
||||
if ($session_id instanceof WSError) {
|
||||
return $session_id;
|
||||
}
|
||||
|
||||
return Tracking::get_time_spent_on_the_course(
|
||||
$user_id,
|
||||
$course_id,
|
||||
$session_id
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of learning paths by course.
|
||||
*
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*
|
||||
* @return array Array of id=>title of learning paths
|
||||
*/
|
||||
public function GetLearnpathsByCourse(
|
||||
$secret_key,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
) {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
}
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$course_code = CourseManager::get_course_code_from_course_id(
|
||||
$course_id
|
||||
);
|
||||
}
|
||||
|
||||
$lp = new LearnpathList($user_id, api_get_course_info($course_code));
|
||||
$list = $lp->list;
|
||||
$return = [];
|
||||
foreach ($list as $id => $item) {
|
||||
$return[] = ['id' => $id, 'title' => $item['lp_name']];
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets progress attained in the given learning path by the given user.
|
||||
*
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param string Learnpath ID
|
||||
*
|
||||
* @return float Between 0 and 100 (% of progress)
|
||||
*/
|
||||
public function GetLearnpathProgress(
|
||||
$secret_key,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$learnpath_id
|
||||
) {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
}
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$course_code = CourseManager::get_course_code_from_course_id(
|
||||
$course_id
|
||||
);
|
||||
}
|
||||
$lp = new learnpath($course_code, $learnpath_id, $user_id);
|
||||
$return = [
|
||||
'progress_bar_mode' => $lp->progress_bar_mode,
|
||||
'progress_db' => $lp->progress_db,
|
||||
];
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the highest element seen (lesson_location) in the given learning
|
||||
* path by the given user. If the user saw the learning path several times,
|
||||
* the last time (lp_view) is assumed. If there are several items in the lp,
|
||||
* the last item seen (lp_view.last_item) is considered as the relevant one
|
||||
* to get the lesson_location from.
|
||||
*
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param string Learnpath ID
|
||||
*
|
||||
* @return string The last item's lesson_location value
|
||||
*/
|
||||
public function GetLearnpathHighestLessonLocation(
|
||||
$secret_key,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$learnpath_id
|
||||
) {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
}
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$course_code = CourseManager::get_course_code_from_course_id(
|
||||
$course_id
|
||||
);
|
||||
}
|
||||
$lp = new learnpath($course_code, $learnpath_id, $user_id);
|
||||
$item = $lp->last_item_seen;
|
||||
$return = $lp->items[$item]->get_lesson_location();
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets score obtained in the given learning path by the given user,
|
||||
* assuming there is only one item (SCO) in the learning path.
|
||||
*
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param int Learnpath ID
|
||||
* @param int Learnpath *ITEM* ID
|
||||
*
|
||||
* @return float Generally between 0 and 100
|
||||
*/
|
||||
public function GetLearnpathScoreSingleItem(
|
||||
$secret_key,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$learnpath_id,
|
||||
$learnpath_item_id
|
||||
) {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
}
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$course_code = CourseManager::get_course_code_from_course_id(
|
||||
$course_id
|
||||
);
|
||||
}
|
||||
|
||||
$lp = new learnpath($course_code, $learnpath_id, $user_id);
|
||||
$return = [
|
||||
'min_score' => $lp->items[$learnpath_item_id]->min_score,
|
||||
'max_score' => $lp->items[$learnpath_item_id]->max_score,
|
||||
'mastery_score' => $lp->items[$learnpath_item_id]->mastery_score,
|
||||
'current_score' => $lp->items[$learnpath_item_id]->current_score,
|
||||
];
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets status obtained in the given learning path by the given user,
|
||||
* assuming there is only one item (SCO) in the learning path.
|
||||
*
|
||||
* @param string Secret key
|
||||
* @param string User id field name (use chamilo_user_id if none)
|
||||
* @param string User id value
|
||||
* @param string Course id field name (use chamilo_course_id if none)
|
||||
* @param string Course id value
|
||||
* @param int Learnpath ID
|
||||
* @param int Learnpath *ITEM* ID
|
||||
*
|
||||
* @return string "not attempted", "passed", "completed", "failed", "incomplete"
|
||||
*/
|
||||
public function GetLearnpathStatusSingleItem(
|
||||
$secret_key,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$learnpath_id,
|
||||
$learnpath_item_id
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
}
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
$course_code = CourseManager::get_course_code_from_course_id(
|
||||
$course_id
|
||||
);
|
||||
}
|
||||
$lp = new learnpath($course_code, $learnpath_id, $user_id);
|
||||
|
||||
return $lp->items[$learnpath_item_id]->status;
|
||||
}
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
return 'Hello world!';
|
||||
}
|
||||
}
|
||||
764
main/webservices/webservice_session.php
Normal file
764
main/webservices/webservice_session.php
Normal file
@@ -0,0 +1,764 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/webservice.php';
|
||||
|
||||
/**
|
||||
* Web services available for the Session module. This class extends the WS class.
|
||||
*/
|
||||
class WSSession extends WS
|
||||
{
|
||||
/**
|
||||
* Creates a session.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Name of the session
|
||||
* @param string Start date, use the 'YYYY-MM-DD' format
|
||||
* @param string End date, use the 'YYYY-MM-DD' format
|
||||
* @param int Access delays of the coach (days before)
|
||||
* @param int Access delays of the coach (days after)
|
||||
* @param int Nolimit (0 = no limit of time, 1 = limit of time)
|
||||
* @param int Visibility
|
||||
* @param string User id field name for the coach
|
||||
* @param string User id value for the coach
|
||||
* @param string Original session id field name (use "chamilo_session_id" to use internal id)
|
||||
* @param string Original session id value
|
||||
* @param array Array of extra fields
|
||||
*
|
||||
* @return int Session id generated
|
||||
*/
|
||||
public function CreateSession(
|
||||
$secret_key,
|
||||
$name,
|
||||
$start_date,
|
||||
$end_date,
|
||||
$nb_days_access_before,
|
||||
$nb_days_access_after,
|
||||
$nolimit,
|
||||
$visibility,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$extras
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$session_id = $this->createSessionHelper(
|
||||
$name,
|
||||
$start_date,
|
||||
$end_date,
|
||||
$nb_days_access_before,
|
||||
$nb_days_access_after,
|
||||
$nolimit,
|
||||
$visibility,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$extras
|
||||
);
|
||||
if ($session_id instanceof WSError) {
|
||||
$this->handleError($session_id);
|
||||
} else {
|
||||
return $session_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a session.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
*/
|
||||
public function DeleteSession(
|
||||
$secret_key,
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->deleteSessionHelper(
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits a session.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Name of the session
|
||||
* @param string Start date, use the 'YYYY-MM-DD' format
|
||||
* @param string End date, use the 'YYYY-MM-DD' format
|
||||
* @param int Access delays of the coach (days before)
|
||||
* @param int Access delays of the coach (days after)
|
||||
* @param int Nolimit (0 = no limit of time, 1 = limit of time)
|
||||
* @param int Visibility
|
||||
* @param string User id field name for the coach
|
||||
* @param string User id value for the coach
|
||||
* @param string Original session id field name (use "chamilo_session_id" to use internal id)
|
||||
* @param string Original session id value
|
||||
* @param array Array of extra fields
|
||||
*/
|
||||
public function EditSession(
|
||||
$secret_key,
|
||||
$name,
|
||||
$start_date,
|
||||
$end_date,
|
||||
$nb_days_access_before,
|
||||
$nb_days_access_after,
|
||||
$nolimit,
|
||||
$visibility,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$extras
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->editSessionHelper(
|
||||
$name,
|
||||
$start_date,
|
||||
$end_date,
|
||||
$nb_days_access_before,
|
||||
$nb_days_access_after,
|
||||
$nolimit,
|
||||
$visibility,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$extras
|
||||
);
|
||||
if ($session_id_value instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe user to a session.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
*/
|
||||
public function SubscribeUserToSession(
|
||||
$secret_key,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeUserSubscription(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
1
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe user to a session.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
*/
|
||||
public function UnsubscribeUserFromSession(
|
||||
$secret_key,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeUserSubscription(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
0
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe teacher to a session course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*/
|
||||
public function SubscribeTeacherToSessionCourse(
|
||||
$secret_key,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeUserSubscription(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
1
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe teacher to a session course.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
*/
|
||||
public function UnsubscribeTeacherFromSessionCourse(
|
||||
$secret_key,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeUserSubscription(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
0
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe course to session.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
*/
|
||||
public function SubscribeCourseToSession(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeCourseSubscription(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
1
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribe course from session.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
*/
|
||||
public function UnsubscribeCourseFromSession(
|
||||
$secret_key,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeCourseSubscription(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
0
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a session (helper method).
|
||||
*
|
||||
* @param string Name of the session
|
||||
* @param string Start date, use the 'YYYY-MM-DD' format
|
||||
* @param string End date, use the 'YYYY-MM-DD' format
|
||||
* @param int Access delays of the coach (days before)
|
||||
* @param int Access delays of the coach (days after)
|
||||
* @param int Nolimit (0 = no limit of time, 1 = limit of time)
|
||||
* @param int Visibility
|
||||
* @param string User id field name for the coach
|
||||
* @param string User id value for the coach
|
||||
* @param string Original session id field name (use "chamilo_session_id" to use internal id)
|
||||
* @param string Original session id value
|
||||
* @param array Array of extra fields
|
||||
*
|
||||
* @return mixed Generated id in case of success, WSError otherwise
|
||||
*/
|
||||
protected function createSessionHelper(
|
||||
$name,
|
||||
$start_date,
|
||||
$end_date,
|
||||
$nb_days_access_before,
|
||||
$nb_days_access_after,
|
||||
$nolimit,
|
||||
$visibility,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$extras
|
||||
) {
|
||||
// Verify that coach exists and get its id
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
}
|
||||
|
||||
$coachStartDate = null;
|
||||
if (!empty($nb_days_access_before)) {
|
||||
$day = intval($nb_days_access_before);
|
||||
$coachStartDate = date(
|
||||
'Y-m-d ',
|
||||
strtotime($start_date.' + '.$day.' days')
|
||||
);
|
||||
}
|
||||
|
||||
$coachEndDate = null;
|
||||
if (!empty($nb_days_access_after)) {
|
||||
$day = intval($nb_days_access_after);
|
||||
$coachEndDate = date(
|
||||
'Y-m-d ',
|
||||
strtotime($end_date.' + '.$day.' days')
|
||||
);
|
||||
}
|
||||
|
||||
// Try to create the session
|
||||
$session_id = SessionManager::create_session(
|
||||
$name,
|
||||
$start_date,
|
||||
$end_date,
|
||||
$start_date,
|
||||
$end_date,
|
||||
$coachStartDate,
|
||||
$coachEndDate,
|
||||
$user_id,
|
||||
0,
|
||||
$visibility
|
||||
);
|
||||
if (!is_int($session_id)) {
|
||||
return new WSError(301, 'Could not create the session');
|
||||
} else {
|
||||
// Add the Original session id to the extra fields
|
||||
$extras_associative = [];
|
||||
if ($session_id_field_name != "chamilo_session_id") {
|
||||
$extras_associative[$session_id_field_name] = $session_id_value;
|
||||
}
|
||||
foreach ($extras as $extra) {
|
||||
$extras_associative[$extra['field_name']] = $extra['field_value'];
|
||||
}
|
||||
// Create the extra fields
|
||||
foreach ($extras_associative as $fname => $fvalue) {
|
||||
SessionManager::create_session_extra_field($fname, 1, $fname);
|
||||
SessionManager::update_session_extra_field_value(
|
||||
$session_id,
|
||||
$fname,
|
||||
$fvalue
|
||||
);
|
||||
}
|
||||
|
||||
return $session_id;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a session (helper method).
|
||||
*
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
*
|
||||
* @return mixed True in case of success, WSError otherwise
|
||||
*/
|
||||
protected function deleteSessionHelper(
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
) {
|
||||
$session_id = $this->getSessionId(
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
);
|
||||
if ($session_id instanceof WSError) {
|
||||
return $session_id;
|
||||
} else {
|
||||
SessionManager::delete($session_id, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits a session (helper method).
|
||||
*
|
||||
* @param string Name of the session
|
||||
* @param string Start date, use the 'YYYY-MM-DD' format
|
||||
* @param string End date, use the 'YYYY-MM-DD' format
|
||||
* @param int Access delays of the coach (days before)
|
||||
* @param int Access delays of the coach (days after)
|
||||
* @param int Nolimit (0 = no limit of time, 1 = limit of time)
|
||||
* @param int Visibility
|
||||
* @param string User id field name for the coach
|
||||
* @param string User id value for the coach
|
||||
* @param string Original session id field name (use "chamilo_session_id" to use internal id)
|
||||
* @param string Original session id value
|
||||
* @param array Array of extra fields
|
||||
*
|
||||
* @return mixed True on success, WSError otherwise
|
||||
*/
|
||||
protected function editSessionHelper(
|
||||
$name,
|
||||
$start_date,
|
||||
$end_date,
|
||||
$nb_days_access_before,
|
||||
$nb_days_access_after,
|
||||
$nolimit,
|
||||
$visibility,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$extras
|
||||
) {
|
||||
$session_id = $this->getSessionId(
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
);
|
||||
if ($session_id instanceof WSError) {
|
||||
return $session_id;
|
||||
} else {
|
||||
// Verify that coach exists and get its id
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
}
|
||||
|
||||
$coachStartDate = null;
|
||||
if (!empty($nb_days_access_before)) {
|
||||
$day = intval($nb_days_access_before);
|
||||
$coachStartDate = date(
|
||||
'Y-m-d ',
|
||||
strtotime($start_date.' + '.$day.' days')
|
||||
);
|
||||
}
|
||||
|
||||
$coachEndDate = null;
|
||||
if (!empty($nb_days_access_after)) {
|
||||
$day = intval($nb_days_access_after);
|
||||
$coachEndDate = date(
|
||||
'Y-m-d ',
|
||||
strtotime($end_date.' + '.$day.' days')
|
||||
);
|
||||
}
|
||||
|
||||
$result_id = SessionManager::edit_session(
|
||||
$session_id,
|
||||
$name,
|
||||
$start_date,
|
||||
$end_date,
|
||||
$start_date,
|
||||
$end_date,
|
||||
$coachStartDate,
|
||||
$coachEndDate,
|
||||
$user_id,
|
||||
0,
|
||||
(int) $visibility
|
||||
);
|
||||
if (!is_int($result_id)) {
|
||||
return new WSError(302, 'Could not edit the session');
|
||||
} else {
|
||||
if (!empty($extras)) {
|
||||
$extras_associative = [];
|
||||
foreach ($extras as $extra) {
|
||||
$extras_associative[$extra['field_name']] = $extra['field_value'];
|
||||
}
|
||||
// Create the extra fields
|
||||
foreach ($extras_associative as $fname => $fvalue) {
|
||||
SessionManager::create_session_extra_field(
|
||||
$fname,
|
||||
1,
|
||||
$fname
|
||||
);
|
||||
SessionManager::update_session_extra_field_value(
|
||||
$session_id,
|
||||
$fname,
|
||||
$fvalue
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change user subscription (helper method).
|
||||
*
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
* @param int State (1 to subscribe, 0 to unsubscribe)
|
||||
*
|
||||
* @return mixed True on success, WSError otherwise
|
||||
*/
|
||||
protected function changeUserSubscription(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$state
|
||||
) {
|
||||
$session_id = $this->getSessionId(
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
);
|
||||
if ($session_id instanceof WSError) {
|
||||
return $session_id;
|
||||
} else {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
} else {
|
||||
if ($state == 1) {
|
||||
SessionManager::subscribeUsersToSession(
|
||||
$session_id,
|
||||
[$user_id]
|
||||
);
|
||||
} else {
|
||||
$result = SessionManager::unsubscribe_user_from_session(
|
||||
$session_id,
|
||||
$user_id
|
||||
);
|
||||
if (!$result) {
|
||||
return new WSError(
|
||||
303,
|
||||
'There was an error unsubscribing this user from the session'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change Teacher subscription (helper method).
|
||||
*
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param int State (1 to subscribe, 0 to unsubscribe)
|
||||
*
|
||||
* @return mixed True on success, WSError otherwise
|
||||
*/
|
||||
protected function changeTeacherSubscription(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$state
|
||||
) {
|
||||
$session_id = $this->getSessionId(
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
);
|
||||
if ($session_id instanceof WSError) {
|
||||
return $session_id;
|
||||
} else {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
} else {
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
if ($state == 1) {
|
||||
SessionManager::set_coach_to_course_session(
|
||||
$user_id,
|
||||
$session_id,
|
||||
$course_id
|
||||
);
|
||||
} else {
|
||||
$user_id = [0 => $user_id];
|
||||
$result = SessionManager::removeUsersFromCourseSession(
|
||||
$user_id,
|
||||
$session_id,
|
||||
$course_id
|
||||
);
|
||||
if (!$result) {
|
||||
return new WSError(
|
||||
303,
|
||||
'There was an error unsubscribing this Teacher from the session'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change course subscription.
|
||||
*
|
||||
* @param string Course id field name
|
||||
* @param string Course id value
|
||||
* @param string Session id field name
|
||||
* @param string Session id value
|
||||
* @param int State (1 to subscribe, 0 to unsubscribe)
|
||||
*
|
||||
* @return mixed True on success, WSError otherwise
|
||||
*/
|
||||
protected function changeCourseSubscription(
|
||||
$course_id_field_name,
|
||||
$course_id_value,
|
||||
$session_id_field_name,
|
||||
$session_id_value,
|
||||
$state
|
||||
) {
|
||||
$session_id = $this->getSessionId(
|
||||
$session_id_field_name,
|
||||
$session_id_value
|
||||
);
|
||||
if ($session_id instanceof WSError) {
|
||||
return $session_id;
|
||||
} else {
|
||||
$course_id = $this->getCourseId(
|
||||
$course_id_field_name,
|
||||
$course_id_value
|
||||
);
|
||||
if ($course_id instanceof WSError) {
|
||||
return $course_id;
|
||||
} else {
|
||||
if ($state == 1) {
|
||||
SessionManager::add_courses_to_session(
|
||||
$session_id,
|
||||
[$course_id]
|
||||
);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
$result = SessionManager::unsubscribe_course_from_session(
|
||||
$session_id,
|
||||
$course_id
|
||||
);
|
||||
if ($result) {
|
||||
return true;
|
||||
} else {
|
||||
return new WSError(
|
||||
304,
|
||||
'Error unsubscribing course from session'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
649
main/webservices/webservice_user.php
Normal file
649
main/webservices/webservice_user.php
Normal file
@@ -0,0 +1,649 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @package chamilo.webservices
|
||||
*/
|
||||
require_once __DIR__.'/../inc/global.inc.php';
|
||||
require_once __DIR__.'/webservice.php';
|
||||
|
||||
/**
|
||||
* Web services available for the User module. This class extends the WS class.
|
||||
*/
|
||||
class WSUser extends WS
|
||||
{
|
||||
/**
|
||||
* Disables a user.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
|
||||
* @param string User id value
|
||||
*/
|
||||
public function DisableUser(
|
||||
$secret_key,
|
||||
$user_id_field_name,
|
||||
$user_id_value
|
||||
) {
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
// Let the implementation handle it
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeUserActiveState(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
0
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables multiple users.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param array Array of users with elements of the form
|
||||
* array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
|
||||
*
|
||||
* @return array Array with elements like
|
||||
* array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
|
||||
* Note that if the result array contains a code different
|
||||
* than 0, an error occured
|
||||
*/
|
||||
public function DisableUsers($secret_key, $users)
|
||||
{
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
// Let the implementation handle it
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
return $this->changeUsersActiveState($users, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables a user.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
|
||||
* @param string User id value
|
||||
*/
|
||||
public function EnableUser($secret_key, $user_id_field_name, $user_id_value)
|
||||
{
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->changeUserActiveState(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
1
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables multiple users.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param array Array of users with elements of the form
|
||||
* array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
|
||||
*
|
||||
* @return array Array with elements like
|
||||
* array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
|
||||
* Note that if the result array contains a code different
|
||||
* than 0, an error occured
|
||||
*/
|
||||
public function EnableUsers($secret_key, $users)
|
||||
{
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
// Let the implementation handle it
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
return $this->changeUsersActiveState($users, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a user.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
|
||||
* @param string User id value
|
||||
*/
|
||||
public function DeleteUser($secret_key, $user_id_field_name, $user_id_value)
|
||||
{
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->deleteUserHelper(
|
||||
$user_id_field_name,
|
||||
$user_id_value
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes multiple users.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param array Array of users with elements of the form
|
||||
* array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
|
||||
*
|
||||
* @return array Array with elements like
|
||||
* array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
|
||||
* Note that if the result array contains a code different
|
||||
* than 0, an error occured
|
||||
*/
|
||||
public function DeleteUsers($secret_key, $users)
|
||||
{
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$results = [];
|
||||
foreach ($users as $user) {
|
||||
$result_tmp = [];
|
||||
$result_op = $this->deleteUserHelper(
|
||||
$user['user_id_field_name'],
|
||||
$user['user_id_value']
|
||||
);
|
||||
$result_tmp['user_id_value'] = $user['user_id_value'];
|
||||
if ($result_op instanceof WSError) {
|
||||
// Return the error in the results
|
||||
$result_tmp['result'] = $result_op->toArray();
|
||||
} else {
|
||||
$result_tmp['result'] = $this->getSuccessfulResult();
|
||||
}
|
||||
$results[] = $result_tmp;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a user.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string User first name
|
||||
* @param string User last name
|
||||
* @param int User status
|
||||
* @param string Login name
|
||||
* @param string Password (encrypted or not)
|
||||
* @param string Encrypt method. Leave blank if you are passing the password in clear text,
|
||||
* set to the encrypt method used to encrypt the password otherwise. Remember
|
||||
* to include the salt in the extra fields if you are encrypting the password
|
||||
* @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
|
||||
* @param string User id value. Leave blank if you are using the internal user_id
|
||||
* @param int Visibility. Set by default to 1
|
||||
* @param string User email. Set by default to an empty string
|
||||
* @param string Language. Set by default to english
|
||||
* @param string Phone. Set by default to an empty string
|
||||
* @param string Expiration date. Set to null by default
|
||||
* @param array Extra fields. An array with elements of the form
|
||||
* array('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Set to an empty array by default
|
||||
*
|
||||
* @return int New user id generated by the system
|
||||
*/
|
||||
public function CreateUser(
|
||||
$secret_key,
|
||||
$firstname,
|
||||
$lastname,
|
||||
$status,
|
||||
$login,
|
||||
$password,
|
||||
$encrypt_method,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$visibility = 1,
|
||||
$email = '',
|
||||
$language = 'english',
|
||||
$phone = '',
|
||||
$expiration_date = '0000-00-00 00:00:00',
|
||||
$extras = []
|
||||
) {
|
||||
// First, verify the secret key
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$result = $this->createUserHelper(
|
||||
$firstname,
|
||||
$lastname,
|
||||
$status,
|
||||
$login,
|
||||
$password,
|
||||
$encrypt_method,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$visibility,
|
||||
$email,
|
||||
$language,
|
||||
$phone,
|
||||
$expiration_date,
|
||||
$extras
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
} else {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates multiple users.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method
|
||||
*
|
||||
* @return array Array with elements of the form
|
||||
* array('user_id_value' => 'original value sent', 'user_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful'))
|
||||
*/
|
||||
public function CreateUsers($secret_key, $users)
|
||||
{
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$results = [];
|
||||
foreach ($users as $user) {
|
||||
$result_tmp = [];
|
||||
// re-initialize variables just in case
|
||||
$firstname = $lastname = $status = $login = $password = $encrypt_method = $user_id_field_name = $user_id_value = $visibility = $email = $language = $phone = $expiration_date = $extras = null;
|
||||
extract($user);
|
||||
$result = $this->createUserHelper(
|
||||
$firstname,
|
||||
$lastname,
|
||||
$status,
|
||||
$login,
|
||||
$password,
|
||||
$encrypt_method,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$visibility,
|
||||
$email,
|
||||
$language,
|
||||
$phone,
|
||||
$expiration_date,
|
||||
$extras
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$result_tmp['result'] = $result->toArray();
|
||||
$result_tmp['user_id_value'] = $user_id_value;
|
||||
$result_tmp['user_id_generated'] = 0;
|
||||
} else {
|
||||
$result_tmp['result'] = $this->getSuccessfulResult();
|
||||
$result_tmp['user_id_value'] = $user_id_value;
|
||||
$result_tmp['user_id_generated'] = $result;
|
||||
}
|
||||
$results[] = $result_tmp;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits user info.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param string User id field name. Use "chamilo_user_id" in order to use internal system id
|
||||
* @param string User id value
|
||||
* @param string First name
|
||||
* @param string Last name
|
||||
* @param int User status
|
||||
* @param string Login name
|
||||
* @param string Password. Leave blank if you don't want to update it
|
||||
* @param string Encrypt method
|
||||
* @param string User email
|
||||
* @param string Language. Set by default to english
|
||||
* @param string Phone. Set by default to an empty string
|
||||
* @param string Expiration date. Set to null by default
|
||||
* @param array Extra fields. An array with elements of the form
|
||||
* ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update
|
||||
*/
|
||||
public function EditUser(
|
||||
$secret_key,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$firstname,
|
||||
$lastname,
|
||||
$status,
|
||||
$loginname,
|
||||
$password,
|
||||
$encrypt_method,
|
||||
$email,
|
||||
$language,
|
||||
$phone,
|
||||
$expiration_date,
|
||||
$extras
|
||||
) {
|
||||
// First, verify the secret key
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$extras_associative = [];
|
||||
if (!empty($extras)) {
|
||||
foreach ($extras as $extra) {
|
||||
$extras_associative[$extra['field_name']] = $extra['field_value'];
|
||||
}
|
||||
}
|
||||
|
||||
$result = $this->editUserHelper(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$firstname,
|
||||
$lastname,
|
||||
$status,
|
||||
$loginname,
|
||||
$password,
|
||||
$encrypt_method,
|
||||
$email,
|
||||
$language,
|
||||
$phone,
|
||||
$expiration_date,
|
||||
$extras_associative
|
||||
);
|
||||
if ($result instanceof WSError) {
|
||||
$this->handleError($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits multiple users.
|
||||
*
|
||||
* @param string API secret key
|
||||
* @param array Users array. Each member of this array must follow the structure imposed by the EditUser method
|
||||
*
|
||||
* @return array Array with elements like
|
||||
* array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
|
||||
* Note that if the result array contains a code different
|
||||
* than 0, an error occured
|
||||
*/
|
||||
public function EditUsers($secret_key, $users)
|
||||
{
|
||||
$verifKey = $this->verifyKey($secret_key);
|
||||
if ($verifKey instanceof WSError) {
|
||||
$this->handleError($verifKey);
|
||||
} else {
|
||||
$results = [];
|
||||
foreach ($users as $user) {
|
||||
$result_tmp = [];
|
||||
// re-initialize variables just in case
|
||||
$user_id_field_name = $user_id_value = $firstname = $lastname = $status = $loginname = $password = $encrypt_method = $email = $language = $phone = $expiration_date = $extras = null;
|
||||
extract($user);
|
||||
$result_op = $this->editUserHelper(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$firstname,
|
||||
$lastname,
|
||||
$status,
|
||||
$loginname,
|
||||
$password,
|
||||
$encrypt_method,
|
||||
$email,
|
||||
$language,
|
||||
$phone,
|
||||
$expiration_date,
|
||||
$extras
|
||||
);
|
||||
$result_tmp['user_id_value'] = $user['user_id_value'];
|
||||
if ($result_op instanceof WSError) {
|
||||
// Return the error in the results
|
||||
$result_tmp['result'] = $result_op->toArray();
|
||||
} else {
|
||||
$result_tmp['result'] = $this->getSuccessfulResult();
|
||||
}
|
||||
$results[] = $result_tmp;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables a user.
|
||||
*
|
||||
* @param string User id field name
|
||||
* @param string User id value
|
||||
* @param int Set to 1 to enable and to 0 to disable
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function changeUserActiveState(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$state
|
||||
) {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
} else {
|
||||
if ($state == 0) {
|
||||
UserManager::disable($user_id);
|
||||
} else {
|
||||
if ($state == 1) {
|
||||
UserManager::enable($user_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables multiple users.
|
||||
*
|
||||
* @param array Users
|
||||
* @param int Set to 1 to enable and to 0 to disable
|
||||
*
|
||||
* @return array Array of results
|
||||
*/
|
||||
protected function changeUsersActiveState($users, $state)
|
||||
{
|
||||
$results = [];
|
||||
foreach ($users as $user) {
|
||||
$result_tmp = [];
|
||||
$result_op = $this->changeUserActiveState(
|
||||
$user['user_id_field_name'],
|
||||
$user['user_id_value'],
|
||||
$state
|
||||
);
|
||||
$result_tmp['user_id_value'] = $user['user_id_value'];
|
||||
if ($result_op instanceof WSError) {
|
||||
// Return the error in the results
|
||||
$result_tmp['result'] = $result_op->toArray();
|
||||
} else {
|
||||
$result_tmp['result'] = $this->getSuccessfulResult();
|
||||
}
|
||||
$results[] = $result_tmp;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a user (helper method).
|
||||
*
|
||||
* @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
|
||||
* @param string User id value
|
||||
*
|
||||
* @return mixed True if user was successfully deleted, WSError otherwise
|
||||
*/
|
||||
protected function deleteUserHelper($user_id_field_name, $user_id_value)
|
||||
{
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
} else {
|
||||
if (!UserManager::delete_user($user_id)) {
|
||||
return new WSError(
|
||||
101,
|
||||
"There was a problem while deleting this user"
|
||||
);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a user (helper method).
|
||||
*
|
||||
* @param string User first name
|
||||
* @param string User last name
|
||||
* @param int User status
|
||||
* @param string Login name
|
||||
* @param string Password (encrypted or not)
|
||||
* @param string Encrypt method. Leave blank if you are passing the password in clear text,
|
||||
* set to the encrypt method used to encrypt the password otherwise. Remember
|
||||
* to include the salt in the extra fields if you are encrypting the password
|
||||
* @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
|
||||
* @param string User id value. Leave blank if you are using the internal user_id
|
||||
* @param int visibility
|
||||
* @param string user email
|
||||
* @param string language
|
||||
* @param string phone
|
||||
* @param string Expiration date
|
||||
* @param array Extra fields. An array with elements of the form
|
||||
* array('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field').
|
||||
*
|
||||
* @return mixed New user id generated by the system, WSError otherwise
|
||||
*/
|
||||
protected function createUserHelper(
|
||||
$firstname,
|
||||
$lastname,
|
||||
$status,
|
||||
$login,
|
||||
$password,
|
||||
$encrypt_method,
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$visibility,
|
||||
$email,
|
||||
$language,
|
||||
$phone,
|
||||
$expiration_date,
|
||||
$extras = []
|
||||
) {
|
||||
// Add the original user id field name and value to the extra fields if needed
|
||||
$extras_associative = [];
|
||||
if ($user_id_field_name != "chamilo_user_id") {
|
||||
$extras_associative[$user_id_field_name] = $user_id_value;
|
||||
}
|
||||
if (!empty($extras)) {
|
||||
foreach ($extras as $extra) {
|
||||
$extras_associative[$extra['field_name']] = $extra['field_value'];
|
||||
}
|
||||
}
|
||||
$result = UserManager::create_user(
|
||||
$firstname,
|
||||
$lastname,
|
||||
$status,
|
||||
$email,
|
||||
$login,
|
||||
$password,
|
||||
'',
|
||||
$language,
|
||||
$phone,
|
||||
'',
|
||||
PLATFORM_AUTH_SOURCE,
|
||||
$expiration_date,
|
||||
$visibility,
|
||||
0,
|
||||
$extras_associative,
|
||||
$encrypt_method
|
||||
);
|
||||
if (!$result) {
|
||||
return new WSError(104, 'There was an error creating the user');
|
||||
} else {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits user info (helper method).
|
||||
*
|
||||
* @param string User id field name. Use "chamilo_user_id" in order to use internal system id
|
||||
* @param string User id value
|
||||
* @param string First name
|
||||
* @param string Last name
|
||||
* @param int User status
|
||||
* @param string Login name
|
||||
* @param string Password. Leave blank if you don't want to update it
|
||||
* @param string Encrypt method
|
||||
* @param string User email
|
||||
* @param string Language. Set by default to english
|
||||
* @param string Phone. Set by default to an empty string
|
||||
* @param string Expiration date. Set to null by default
|
||||
* @param array Extra fields. An array with elements of the form
|
||||
* ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field').
|
||||
* Leave empty if you don't want to update
|
||||
*
|
||||
* @return mixed True if user was successfully updated, WSError otherwise
|
||||
*/
|
||||
protected function editUserHelper(
|
||||
$user_id_field_name,
|
||||
$user_id_value,
|
||||
$firstname,
|
||||
$lastname,
|
||||
$status,
|
||||
$loginname,
|
||||
$password,
|
||||
$encrypt_method,
|
||||
$email,
|
||||
$language,
|
||||
$phone,
|
||||
$expiration_date,
|
||||
$extras
|
||||
) {
|
||||
$user_id = $this->getUserId($user_id_field_name, $user_id_value);
|
||||
if ($user_id instanceof WSError) {
|
||||
return $user_id;
|
||||
} else {
|
||||
if ($password == '') {
|
||||
$password = null;
|
||||
}
|
||||
$user_info = api_get_user_info($user_id);
|
||||
if (count($extras) == 0) {
|
||||
$extras = null;
|
||||
}
|
||||
|
||||
$result = UserManager::update_user(
|
||||
$user_id,
|
||||
$firstname,
|
||||
$lastname,
|
||||
$loginname,
|
||||
$password,
|
||||
PLATFORM_AUTH_SOURCE,
|
||||
$email,
|
||||
$status,
|
||||
'',
|
||||
$phone,
|
||||
$user_info['picture_uri'],
|
||||
$expiration_date,
|
||||
$user_info['active'],
|
||||
null,
|
||||
$user_info['hr_dept_id'],
|
||||
$extras,
|
||||
$encrypt_method
|
||||
);
|
||||
if (!$result) {
|
||||
return new WSError(105, 'There was an error updating the user');
|
||||
} else {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user