Add webservices to add or remove visibility rules between users and courses in the courses catalogue

pull/2487/head
David Nos 9 years ago
parent 43ad39178e
commit 0106766c70
  1. 102
      main/inc/lib/course.lib.php
  2. 220
      main/webservices/registration.soap.php

@ -777,6 +777,108 @@ class CourseManager
return $insertId;
}
/**
* Add the user $userId visibility to the course $courseCode in the catalogue.
* @author David Nos
*
* @param int $userId the id of the user
* @param string $courseCode the course code
* @param int $visible (optional) The course visibility in the catalogue to the user (1=visible, 0=invisible)
*
* @return boolean true if added succesfully, false otherwise.
*/
public static function addUserVisibilityToCourseInCatalogue($userId, $courseCode, $visible = 1)
{
$debug = false;
$userTable = Database::get_main_table(TABLE_MAIN_USER);
$courseUserTable = Database::get_main_table(TABLE_MAIN_COURSE_CATALOGUE_USER);
if (empty($userId) || empty($courseCode) || ($userId != strval(intval($userId)))) {
return false;
}
$courseCode = Database::escape_string($courseCode);
$courseInfo = api_get_course_info($courseCode);
$courseId = $courseInfo['real_id'];
// Check in advance whether the user has already been registered on the platform.
$sql = "SELECT status FROM " . $userTable . " WHERE user_id = $userId ";
if (Database::num_rows(Database::query($sql)) == 0) {
if ($debug) {
error_log('The user has not been registered to the platform');
}
return false; // The user has not been registered to the platform.
}
// Check whether the user has already been registered to the course visibility in the catalogue.
$sql = "SELECT * FROM $courseUserTable
WHERE
user_id = $userId AND
visible = " . $visible . " AND
c_id = $courseId";
if (Database::num_rows(Database::query($sql)) > 0) {
if ($debug) {
error_log('The user has been already registered to the course visibility in the catalogue');
}
return true; // The visibility of the user to the course in the catalogue does already exist.
}
// Register the user visibility to course in catalogue.
$params = [
'user_id' => $userId,
'c_id' => $courseId,
'visible' => $visible
];
$insertId = Database::insert($courseUserTable, $params);
return $insertId;
}
/**
* Remove the user $userId visibility to the course $courseCode in the catalogue.
* @author David Nos
*
* @param int $userId the id of the user
* @param string $courseCode the course code
* @param int $visible (optional) The course visibility in the catalogue to the user (1=visible, 0=invisible)
*
* @return boolean true if removed succesfully or register not found, false otherwise.
*/
public static function removeUserVisibilityToCourseInCatalogue($userId, $courseCode, $visible = 1)
{
$courseUserTable = Database::get_main_table(TABLE_MAIN_COURSE_CATALOGUE_USER);
if (empty($userId) || empty($courseCode) || ($userId != strval(intval($userId)))) {
return false;
}
$courseCode = Database::escape_string($courseCode);
$courseInfo = api_get_course_info($courseCode);
$courseId = $courseInfo['real_id'];
// Check whether the user has already been registered to the course visibility in the catalogue.
$sql = "SELECT * FROM $courseUserTable
WHERE
user_id = $userId AND
visible = " . $visible . " AND
c_id = $courseId";
if (Database::num_rows(Database::query($sql)) > 0) {
$cond = array(
'user_id = ? AND c_id = ? AND visible = ? ' => array(
$userId,
$courseId,
$visible
)
);
return Database::delete($courseUserTable, $cond);
}
else {
return true; // Register does not exist
}
}
/**
* Checks wether a parameter exists.
* If it doesn't, the function displays an error message.

@ -6830,6 +6830,226 @@ function WSDeleteUserFromGroup($params)
/* Delete user from group Web Service end */
/** WSRegisterUserVisibilityToCourseCatalogue **/
// Register the data structures used by the service
$server->wsdl->addComplexType(
'user_course_visibility',
'complexType',
'struct',
'all',
'',
array (
'course_id' => array('name' => 'course_id', 'type' => 'tns:course_id'),
'user_id' => array('name' => 'user_id', 'type' => 'tns:user_id'),
'visible' => array('name' => 'status', 'type' => 'xsd:int')
)
);
$server->wsdl->addComplexType(
'user_course_visibility_array',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:user_course_visibility[]')
),
'tns:user_course_visibility'
);
$server->wsdl->addComplexType(
'registerUserToCourseCatalogue_arg',
'complexType',
'struct',
'all',
'',
array (
'userscourses' => array('name' => 'userscourses', 'type' => 'tns:user_course_visibility_array'),
'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string')
)
);
$server->wsdl->addComplexType(
'registerUserToCourseCatalogue_return',
'complexType',
'struct',
'all',
'',
array(
'original_user_id_value' => array('name' => 'original_user_id_value', 'type' => 'xsd:string'),
'original_course_id_value' => array('name' => 'original_course_id_value', 'type' => 'xsd:string'),
'visible' => array('name' => 'visible', 'type' => 'xsd:int'),
'result' => array('name' => 'result', 'type' => 'xsd:int')
)
);
$server->wsdl->addComplexType(
'registerUserToCourseCatalogue_return_global',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:registerUserToCourseCatalogue_return[]')),
'tns:registerUserToCourseCatalogue_return'
);
// Register the method to expose
$server->register('WSAddUserVisibilityToCourseInCatalogue', // method name
array('registerUserToCourseCatalogue' => 'tns:registerUserToCourseCatalogue_arg'), // input parameters
array('return' => 'tns:registerUserToCourseCatalogue_return_global'),
'urn:WSRegistration', // namespace
'urn:WSRegistration#WSRegisterUserVisibilityToCourseCatalogue', // soapaction
'rpc', // style
'encoded', // use
'This service registers the visibility of users to course in catalogue' // documentation
);
// define the method WSRegisterUserVisibilityToCourseInCatalogue
function WSAddUserVisibilityToCourseInCatalogue($params) {
global $debug;
if (!WSHelperVerifyKey($params)) {
return returnError(WS_ERROR_SECRET_KEY);
}
if ($debug) error_log('WSAddUserVisibilityToCourseCatalogue params: '.print_r($params, 1));
$results = array();
$userscourses = $params['userscourses'];
foreach ($userscourses as $usercourse) {
$original_course_id = $usercourse['course_id'];
$original_user_id = $usercourse['user_id'];
$visible = $usercourse['visible'];
$resultValue = 0;
// Get user id
$userId = UserManager::get_user_id_from_original_id(
$original_user_id['original_user_id_value'],
$original_user_id['original_user_id_name']
);
if ($debug) error_log('WSAddUserVisibilityToCourseCatalogue userId: '.$userId);
if ($userId == 0) {
// If user was not found, there was a problem
$resultValue = 0;
} else {
// User was found
$courseInfo = CourseManager::getCourseInfoFromOriginalId(
$original_course_id['original_course_id_value'],
$original_course_id['original_course_id_name']
);
$courseCode = $courseInfo['code'];
if (empty($courseCode)) {
// Course was not found
$resultValue = 0;
} else {
if ($debug) error_log('WSAddUserVisibilityToCourseCatalogue courseCode: '.$courseCode);
$result = CourseManager::addUserVisibilityToCourseInCatalogue($userId, $courseCode, $visible);
if ($result) {
$resultValue = 1;
if ($debug) error_log('WSAddUserVisibilityToCourseCatalogue registered');
} else {
if ($debug) error_log('WSAddUserVisibilityToCourseCatalogue NOT registered: ');
}
}
}
$results[] = array(
'original_user_id_value' => $original_user_id['original_user_id_value'],
'original_course_id_value' => $original_course_id['original_course_id_value'],
'visible' => $visible,
'result' => $resultValue
);
}
return $results;
}
// Register the method to expose
$server->register('WSRemoveUserVisibilityToCourseInCatalogue', // method name
array('registerUserToCourseCatalogue' => 'tns:registerUserToCourseCatalogue_arg'), // input parameters
array('return' => 'tns:registerUserToCourseCatalogue_return_global'),
'urn:WSRegistration', // namespace
'urn:WSRegistration#WSRegisterUserVisibilityToCourseCatalogue', // soapaction
'rpc', // style
'encoded', // use
'This service removes the visibility of users to course in catalogue' // documentation
);
// define the method WSRemoveUserVisibilityToCourseInCatalogue
function WSRemoveUserVisibilityToCourseInCatalogue($params) {
global $debug;
if (!WSHelperVerifyKey($params)) {
return returnError(WS_ERROR_SECRET_KEY);
}
if ($debug) error_log('WSRemoveUserVisibilityToCourseInCatalogue params: '.print_r($params, 1));
$results = array();
$userscourses = $params['userscourses'];
foreach ($userscourses as $usercourse) {
$original_course_id = $usercourse['course_id'];
$original_user_id = $usercourse['user_id'];
$visible = $usercourse['visible'];
$resultValue = 0;
// Get user id
$userId = UserManager::get_user_id_from_original_id(
$original_user_id['original_user_id_value'],
$original_user_id['original_user_id_name']
);
if ($debug) error_log('WSRemoveUserVisibilityToCourseInCatalogue user_id: '.$userId);
if ($userId == 0) {
// If user was not found, there was a problem
$resultValue = 0;
} else {
// User was found
$courseInfo = CourseManager::getCourseInfoFromOriginalId(
$original_course_id['original_course_id_value'],
$original_course_id['original_course_id_name']
);
$courseCode = $courseInfo['code'];
if (empty($courseCode)) {
// Course was not found
$resultValue = 0;
} else {
if ($debug) error_log('WSRemoveUserVisibilityToCourseInCatalogue courseCode: '.$courseCode);
$result = CourseManager::removeUserVisibilityToCourseInCatalogue($userId, $courseCode, $visible);
if ($result) {
$resultValue = 1;
if ($debug) error_log('WSRemoveUserVisibilityToCourseInCatalogue removed');
} else {
if ($debug) error_log('WSRemoveUserVisibilityToCourseInCatalogue NOT removed: ');
}
}
}
$results[] = array(
'original_user_id_value' => $original_user_id['original_user_id_value'],
'original_course_id_value' => $original_course_id['original_course_id_value'],
'visible' => $visible,
'result' => $resultValue
);
}
return $results;
}
// Add more webservices through hooks from plugins
if (!empty($hook)) {
$hook->setEventData(array('server' => $server));

Loading…
Cancel
Save