WSListSessionDetailsByCategory from BT9090 - refs BT#9092

Conflicts:
	main/inc/lib/sessionmanager.lib.php
	main/webservices/registration.soap.php
1.10.x
Daniel Barreto 11 years ago
parent 89ee9656e3
commit 80a036ef54
  1. 158
      main/inc/lib/sessionmanager.lib.php
  2. 247
      plugin/advancedsubscription/src/HookAdvancedSubscription.class.php

@ -5233,8 +5233,8 @@ class SessionManager
$sessionId = intval($sessionId); $sessionId = intval($sessionId);
$userId = intval($userId); $userId = intval($userId);
$sql = "SELECT COUNT(1) AS qty FROM $sessionRelUserTable $sql = "SELECT COUNT(1) AS qty FROM $sessionRelUserTable "
WHERE id_session = $sessionId AND id_user = $userId AND relation_type = 0"; . "WHERE id_session = $sessionId AND id_user = $userId AND relation_type = 0";
$result = Database::fetch_assoc(Database::query($sql)); $result = Database::fetch_assoc(Database::query($sql));
@ -5314,7 +5314,6 @@ class SessionManager
* Check if the course belongs to the session * Check if the course belongs to the session
* @param int $sessionId The session id * @param int $sessionId The session id
* @param string $courseCode The course code * @param string $courseCode The course code
*
* @return bool * @return bool
*/ */
public static function sessionHasCourse($sessionId, $courseCode) public static function sessionHasCourse($sessionId, $courseCode)
@ -5498,4 +5497,157 @@ class SessionManager
return $sessionList; return $sessionList;
} }
/**
* Return the Session Category id searched by name
* @param string $categoryName Name attribute of session category used for search query
* @param bool $force boolean used to get even if something is wrong (e.g not unique name)
* @return int|array If success, return category id (int), else it will return an array
* with the next structure:
* array('error' => true, 'errorMessage' => ERROR_MESSAGE)
*/
public static function getSessionCategoryIdByName($categoryName, $force = false)
{
// Start error result
$errorResult = array('error' => true, 'errorMessage' => 'There was an error');
$categoryName = Database::escape_string($categoryName);
// Check if is not empty category name
if (!empty($categoryName)) {
$sessionCategoryTable = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
// Get all session category with same name
$result = Database::select(
'id',
$sessionCategoryTable,
array(
'where' => array(
'name = ?' => $categoryName,
)
)
);
// Check the result
if ($result < 1) {
// If not found any result, update error message
$errorResult['errorMessage'] = 'Not found any session category name ' . $categoryName;
} elseif (count($result) > 1 && !$force) {
// If found more than one result and force is disabled, update error message
$errorResult['errorMessage'] = 'Found many session categories';
} elseif (count($result) == 1 || $force) {
// If found just one session category or force option is enabled
return key($result);
}
} else {
// category name is empty, update error message
$errorResult['errorMessage'] = 'Not valid category name';
}
return $errorResult;
}
/**
* Return all data from sessions (plus extra field, course and coach data) by category id
* @param int $sessionCategoryId session category id used to search sessions
* @return array
*/
public static function getSessionListAndExtraByCategoryId($sessionCategoryId)
{
// Start error result
$errorResult = array('error' => true, 'errorMessage' => 'There was an error');
$sessionCategoryId = intval($sessionCategoryId);
// Check if sesssion category id is valid
if ($sessionCategoryId > 0) {
// Get table names
$sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
$sessionFieldTable = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
$sessionFieldValueTable = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
$sessionCourseUserTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
$userTable = Database::get_main_table(TABLE_MAIN_USER);
$courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
// Get all data from all sessions whit the session category specified
$sessionList = Database::select(
'*',
$sessionTable,
array(
'where' => array(
'session_category_id = ?' => $sessionCategoryId
)
)
);
// Check if session list query had result
if (!empty($sessionList)) {
// implode all session id
$sessionIdsString = '(' . implode(', ', array_keys($sessionList)) . ')';
// Get all field variables
$sessionFieldList = Database::select('id, field_variable', $sessionFieldTable);
// Get all field values
$sessionFieldValueList = Database::select(
'session_id, field_id, field_value',
$sessionFieldValueTable,
array('where' => array('session_id IN ?' => $sessionIdsString))
);
// Check if session field values had result
if (!empty($sessionFieldValueList)) {
$sessionFieldValueListBySession = array();
foreach ($sessionFieldValueList as $key => $sessionFieldValue) {
// Create an array to index ids to session id
$sessionFieldValueListBySession[$sessionFieldValue['session_id']][] = $key;
}
}
// Query used to find course-coaches from sessions
$sql = "SELECT scu.id_session AS session_id, c.id AS course_id, c.code AS course_code," .
" c.title AS course_title, u.username AS coach_username, u.firstname AS coach_firstname, " .
" u.lastname AS coach_lastname " .
"FROM $courseTable c " .
"INNER JOIN $sessionCourseUserTable scu ON c.code = scu.course_code " .
"INNER JOIN $userTable u ON scu.id_user = u.user_id " .
"WHERE scu.status = 2 AND scu.id_session IN $sessionIdsString " .
"ORDER BY scu.id_session ASC ";
$res = Database::query($sql);
$sessionCourseList = Database::store_result($res, 'ASSOC');
// Check if course list had result
if (!empty($sessionCourseList)) {
$sessionCourseListBySession = array();
foreach ($sessionCourseList as $key => $sessionCourse) {
// Create an array to index ids to session_id
$sessionCourseListBySession[$sessionCourse['session_id']][] = $key;
}
}
// Join lists
if (is_array($sessionList)) {
foreach ($sessionList as $id => &$row) {
if (is_array($sessionFieldValueListBySession[$id])) {
// If have an index array for session extra fields, use it to join arrays
foreach ($sessionFieldValueListBySession[$id] as $key) {
$row['extra'][$key] = array(
'field_name' => $sessionFieldList[$sessionFieldValueList[$key]['field_id']]['field_variable'],
'field_value' => $sessionFieldValueList[$key]['field_value'],
);
}
}
if (is_array($sessionCourseListBySession[$id])) {
// If have an index array for session course coach, use it to join arrays
foreach ($sessionCourseListBySession[$id] as $key) {
$row['course'][$key] = array(
'course_id' => $sessionCourseList[$key]['course_id'],
'course_code' => $sessionCourseList[$key]['course_code'],
'course_title' => $sessionCourseList[$key]['course_title'],
'coach_username' => $sessionCourseList[$key]['coach_username'],
'coach_firstname' => $sessionCourseList[$key]['coach_firstname'],
'coach_lastname' => $sessionCourseList[$key]['coach_lastname'],
);
}
}
}
}
return $sessionList;
} else {
// Not found result, update error message
$errorResult['errorMessage'] = 'Not found any session for session category id ' . $sessionCategoryId;
}
return $errorResult;
}
}
} }

@ -146,6 +146,175 @@ class HookAdvancedSubscription extends HookObserver implements
) )
); );
/** WSListSessionsDetailsByCategory **/
$server->wsdl->addComplexType(
'listSessionsDetailsByCategory',
'complexType',
'struct',
'all',
'',
array(
'id' => array('name' => 'id', 'type' => 'xsd:string'), // session_category.id
'category_name' => array('name' => 'category_name', 'type' => 'xsd:string'), // session_category.name
'access_url_id' => array('name' => 'access_url_id', 'type' => 'xsd:int'), // session_category.access_url_id
'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string') // secret key
),
array(),
'tns:listSessionsDetailsByCategory'
);
$server->wsdl->addComplexType(
'sessionDetailsCourse',
'complexType',
'struct',
'all',
'',
array(
'course_id' => array('name' => 'course_id', 'type' => 'xsd:int'), // course.id
'course_code' => array('name' => 'course_code', 'type' => 'xsd:string'), // course.code
'course_title' => array('name' => 'course_title', 'type' => 'xsd:string'), // course.title
'coach_username' => array('name' => 'coach_username', 'type' => 'xsd:string'), // user.username
'coach_firstname' => array('name' => 'coach_firstname', 'type' => 'xsd:string'), // user.firstname
'coach_lastname' => array('name' => 'coach_lastname', 'type' => 'xsd:string'), // user.lastname
)
);
$server->wsdl->addComplexType(
'sessionDetailsCourseList',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array(
'ref' => 'SOAP-ENC:arrayType',
'wsdl:arrayType' => 'tns:sessionDetailsCourse[]',
)
),
'tns:sessionDetailsCourse'
);
$server->wsdl->addComplexType(
'sessionDetails',
'complexType',
'struct',
'all',
'',
array(
// session.id
'id' => array(
'name' => 'id',
'type' => 'xsd:int'
),
// session.id_coach
'coach_id' => array(
'name' => 'coach_id',
'type' => 'xsd:int'
),
// session.name
'name' => array(
'name' => 'name',
'type' => 'xsd:string'
),
// session.nbr_courses
'courses_num' => array(
'name' => 'courses_num',
'type' => 'xsd:int'
),
// session.nbr_users
'users_num' => array(
'name' => 'users_num',
'type' => 'xsd:int'
),
// session.nbr_classes
'classes_num' => array(
'name' => 'classes_num',
'type' => 'xsd:int'
),
// session.date_start
'date_start' => array(
'name' => 'date_start',
'type' => 'xsd:string'
),
// session.date_end
'date_end' => array(
'name' => 'date_end',
'type' => 'xsd:string'
),
// session.nb_days_access_before_beginning
'access_days_before_num' => array(
'name' => 'access_days_before_num',
'type' => 'xsd:int'
),
// session.nb_days_access_after_end
'access_days_after_num' => array(
'name' => 'access_days_after_num',
'type' => 'xsd:int'
),
// session.session_admin_id
'session_admin_id' => array(
'name' => 'session_admin_id',
'type' => 'xsd:int'
),
// session.visibility
'visibility' => array(
'name' => 'visibility',
'type' => 'xsd:int'
),
// session.session_category_id
'session_category_id' => array(
'name' => 'session_category_id',
'type' => 'xsd:int'
),
// session.promotion_id
'promotion_id' => array(
'name' => 'promotion_id',
'type' => 'xsd:int'
),
// session.number of registered users validated
'validated_user_num' => array(
'name' => 'validated_user_num',
'type' => 'xsd:int'
),
// session.number of registered users from waiting queue
'waiting_user_num' => array(
'name' => 'waiting_user_num',
'type' => 'xsd:int'
),
// extra fields
// Array(field_name, field_value)
'extra' => array(
'name' => 'extra',
'type' => 'tns:extrasList'
),
// course and coaches data
// Array(course_id, course_code, course_title, coach_username, coach_firstname, coach_lastname)
'course' => array(
'name' => 'courses',
'type' => 'tns:sessionDetailsCourseList'
),
)
);
$server->wsdl->addComplexType(
'sessionDetailsList',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array(
'ref' => 'SOAP-ENC:arrayType',
'wsdl:arrayType' => 'tns:sessionDetails[]',
)
),
'tns:sessionDetails'
);
// Register the method to expose // Register the method to expose
$server->register('HookAdvancedSubscription..WSSessionListInCategory', // method name $server->register('HookAdvancedSubscription..WSSessionListInCategory', // method name
array('sessionCategoryInput' => 'tns:sessionCategoryInput'), // input parameters array('sessionCategoryInput' => 'tns:sessionCategoryInput'), // input parameters
@ -177,6 +346,17 @@ class HookAdvancedSubscription extends HookObserver implements
'This service encrypt data to be used later in urls' // documentation 'This service encrypt data to be used later in urls' // documentation
); );
$server->register(
'HookAdvancedSubscription..WSListSessionsDetailsByCategory', // method name
array('name' => 'tns:listSessionsDetailsByCategory'), // input parameters
array('return' => 'tns:sessionDetailsList'), // output parameters
'urn:WSRegistration', // namespace
'urn:WSRegistration#WSListSessionsDetailsByCategory', // soapaction
'rpc', // style
'encoded', // use
'This service returns a list of detailed sessions by a category' // documentation
);
return $data; return $data;
} else { } else {
// Nothing to do // Nothing to do
@ -334,4 +514,71 @@ class HookAdvancedSubscription extends HookObserver implements
return $result; return $result;
} }
/**
* Get a list of sessions (id, coach_id, name, courses_num, users_num, classes_num,
* date_start, date_end, access_days_before_num, session_admin_id, visibility,
* session_category_id, promotion_id,
* validated_user_num, waiting_user_num,
* extra, course) the validated_usernum and waiting_user_num are
* used when have the plugin for advance incsription enables.
* The extra data (field_name, field_value)
* The course data (course_id, course_code, course_title,
* coach_username, coach_firstname, coach_lastname)
* @param array List of parameters (id, category_name, access_url_id, secret_key)
* @return array|soap_fault Sessions list (id=>[title=>'title',url='http://...',date_start=>'...',date_end=>''])
*/
function WSListSessionsDetailsByCategory($params)
{
global $debug;
if ($debug) error_log('WSListSessionsDetailsByCategory');
if ($debug) error_log('Params '. print_r($params, 1));
$secretKey = $params['secret_key'];
// Check if secret key is valid
if(!WSHelperVerifyKey($secretKey)) {
//return return_error(WS_ERROR_SECRET_KEY);
}
// Check if category ID is set
if (!empty($params['id']) && empty($params['category_name'])) {
$sessionCategoryId = $params['id'];
} elseif (!empty($params['category_name'])) {
// Check if category name is set
$sessionCategoryId = SessionManager::getSessionCategoryIdByName($params['category_name']);
if (is_array($sessionCategoryId)) {
$sessionCategoryId = current($sessionCategoryId);
}
} else {
// Return soap fault Not valid input params
return return_error(WS_ERROR_INVALID_INPUT);
}
// Get the session List by category
$sessionList = SessionManager::getSessionListAndExtraByCategoryId($sessionCategoryId);
if (empty($sessionList)) {
// If not found any session, return error
return return_error(WS_ERROR_NOT_FOUND_RESULT);
}
//@TODO: Not implemented yet, see BT#9092
// Check if advanced inscription plugin is enabled
$isAdvancedInscriptionEnabled = false;
if ($isAdvancedInscriptionEnabled) {
// Get validated and waiting queue users count for each session
foreach ($sessionList as &$session) {;
// Add validated and queue users count
$session['validated_user_num'] = 0;
$session['waiting_user_num'] = 0;
}
} else {
// Set -1 to validated and waiting queue users count
}
return $sessionList;
}
} }
Loading…
Cancel
Save