@ -8,9 +8,10 @@
* @package chamilo.library
*/
/**
* Code
* The SessionManager class manages all the Chamilo sessions (as in course
* groups).
* @package chamilo.library.session
*/
class SessionManager {
//See BT#4871
@ -22,8 +23,9 @@ class SessionManager {
/**
* Fetches a session from the database
* @param int Session ID
* @return array Session details
* @param int Session ID
* @return array Session details
* @assert (-1) === array()
*/
public static function fetch($id) {
$t = Database::get_main_table(TABLE_MAIN_SESSION);
@ -33,8 +35,13 @@ class SessionManager {
if (Database::num_rows($r) != 1) { return array(); }
return Database::fetch_array($r,'ASSOC');
}
public static function add($params) {
/**
* Creates a session
* @param array Fields to use in the creation of the session
* @param boolean Whether to allow for same-name sessions or not.
* @assert (array()) === false
*/
public static function add($params, $allow_homonyms=false) {
global $_configuration;
//just in case
@ -54,14 +61,20 @@ class SessionManager {
return get_lang('PortalSessionsLimitReached');
}
}
$my_session_result = SessionManager::get_session_by_name($params['name']);
$session_id = null;
if (!$allow_homonyms) {
$my_session_result = SessionManager::get_session_by_name($params['name']);
$session_id = null;
if ($my_session_result == false) {
if ($my_session_result == false) {
$session_model = new SessionModel();
$session_id = $session_model->save($params);
} else {
error_log('Session already exists with name: '.$params['name']." session_id: ".$my_session_result['id']);
}
} else {
//with the allow_homonyms option, two sessions can share names
$session_model = new SessionModel();
$session_id = $session_model->save($params);
} else {
error_log('Session already exits with name: '.$params['name']." session_id: ".$my_session_result['id']);
}
if (!empty($session_id)) {
@ -129,8 +142,16 @@ class SessionManager {
}
return $session_id;
}
/**
* Updates a session with the given array of field values
* @param array An array of fields values
* @return void
* @assert (null) === false
*/
public static function update($params) {
if (empty($params) || count($params)< 1 ) {
return false;
}
$session_model = new SessionModel();
$session_model->update($params);
@ -141,16 +162,23 @@ class SessionManager {
$session_field_value->save_field_values($params);
}
}
/**
* Checks whether a session already exists with the given name (used in
* add() to avoid homonym sessions)
* @param string A session name
* @assert ('') === false
*/
function session_name_exists($session_name) {
$session_name = Database::escape_string($session_name);
$result = Database::fetch_array(Database::query("SELECT COUNT(*) as count FROM ".Database::get_main_table(TABLE_MAIN_SESSION)." WHERE name = '$session_name' "));
return $result['count'] > 0;
}
/**
* Gets the admin session list callback of the admin/session_list.php page
* @param array order and limit keys
* @param boolean Whether to get all the results or only the count
* @return mixed Integer for number of rows, or array of results
* @assert (array(),true) !== false
*/
public static function get_sessions_admin($options = array(), $get_count = false) {
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
@ -332,8 +360,17 @@ class SessionManager {
return $formatted_sessions;
}
/**
* Gets the number of rows in the session table filtered through the given
* array of parameters
* @param array Array of options/filters/keys
* @return integer The number of rows, or false on wrong param
* @assert ('a') === false
*/
static function get_count_admin_complete($options = array()) {
if (!is_array($options)) {
return false;
}
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
$tbl_user = Database::get_main_table(TABLE_MAIN_USER);
@ -380,35 +417,38 @@ class SessionManager {
$num = $recorset['total_rows'];
return $num;
}
/**
* Gets the admin session list callback of the admin/session_list.php page with all user/details
* Gets the admin session list callback of the admin/session_list.php
* page with all user/details in the right fomat
* @param array order and limit keys
* @result array Array of rows results
* @asset ('a') === false
*/
public static function get_sessions_admin_complete($options = array()) {
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
$tbl_user = Database::get_main_table(TABLE_MAIN_USER);
public static function get_sessions_admin_complete($options = array()) {
if (!is_array($options)) {
return false;
}
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
$tbl_user = Database::get_main_table(TABLE_MAIN_USER);
$tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
$tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
$tbl_session_field_values = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
$tbl_session_field_options = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS);
$where = 'WHERE 1 = 1 ';
$user_id = api_get_user_id();
$where = 'WHERE 1 = 1 ';
$user_id = api_get_user_id();
if (api_is_session_admin() & & api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false') {
$where.=" AND s.session_admin_id = $user_id ";
}
$coach_name = " CONCAT (u.lastname , ' ', u.firstname) as coach_name ";
if (api_is_session_admin() & & api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false') {
$where.=" AND s.session_admin_id = $user_id ";
}
if (api_is_western_name_order()) {
$coach_name = " CONCAT (u.firstname, ' ', u.lastname) as coach_name ";
}
$coach_name = " CONCAT (u.lastname , ' ', u.firstname) as coach_name ";
if (api_is_western_name_order()) {
$coach_name = " CONCAT (u.firstname, ' ', u.lastname) as coach_name ";
}
$today = api_get_utc_datetime();
$today = api_get_utc_datetime();
$inject_extra_fields = null;
@ -448,7 +488,7 @@ class SessionManager {
//var_dump($options_by_double);
//sc.name as category_name,
$select = "
$select = "
SELECT * FROM (
SELECT DISTINCT
IF (
@ -482,12 +522,12 @@ class SessionManager {
$options['where'] = str_replace('course_title', 'c.title', $options['where']);
$where .= ' AND '.$options['where'];
}
}
if (!empty($options['limit'])) {
$where .= " LIMIT ".$options['limit'];
}
$query = "$select FROM $tbl_session s
$query = "$select FROM $tbl_session s
LEFT JOIN $tbl_session_field_values fv ON (fv.session_id = s.id)
INNER JOIN $tbl_session_field_options fvo ON (fv.field_id = fvo.field_id)
LEFT JOIN $tbl_session_rel_course src ON (src.id_session = s.id)
@ -496,61 +536,59 @@ class SessionManager {
INNER JOIN $tbl_user u ON (s.id_coach = u.user_id) ".
$where;
if (api_is_multiple_url_enabled()) {
$table_access_url_rel_session= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
$access_url_id = api_get_current_access_url_id();
if ($access_url_id != -1) {
$where.= " AND ar.access_url_id = $access_url_id ";
$query = "$select
if (api_is_multiple_url_enabled()) {
$table_access_url_rel_session= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
$access_url_id = api_get_current_access_url_id();
if ($access_url_id != -1) {
$where.= " AND ar.access_url_id = $access_url_id ";
$query = "$select
FROM $tbl_session s
LEFT JOIN $tbl_session_field_values fv ON (fv.session_id = s.id)
LEFT JOIN $tbl_session_rel_course src ON (src.id_session = s.id)
LEFT JOIN $tbl_session_rel_course src ON (src.id_session = s.id)
LEFT JOIN $tbl_course c ON (src.course_code = c.code)
LEFT JOIN $tbl_session_category sc ON (s.session_category_id = sc.id)
INNER JOIN $tbl_user u ON (s.id_coach = u.user_id)
INNER JOIN $table_access_url_rel_session ar ON (ar.session_id = s.id)
INNER JOIN $tbl_user u ON (s.id_coach = u.user_id)
INNER JOIN $table_access_url_rel_session ar ON (ar.session_id = s.id)
$where";
}
}
}
}
$query .= ") AS session_table";
$query .= ") AS session_table";
if (!empty($options['order'])) {
$query .= " ORDER BY ".$options['order'];
}
//error_log($query);
// echo $query;
// echo $query;
$result = Database::query($query);
$formatted_sessions = array();
$result = Database::query($query);
$formatted_sessions = array();
if (Database::num_rows($result)) {
$sessions = Database::store_result($result, 'ASSOC');
foreach ($sessions as $session) {
if (Database::num_rows($result)) {
$sessions = Database::store_result($result, 'ASSOC');
foreach ($sessions as $session) {
$session_id = $session['id'];
$session['name'] = Display::url($session['name'], "resume_session.php?id_session=".$session['id']);
$session['coach_name'] = Display::url($session['coach_name'], "user_information.php?user_id=".$session['user_id']);
if ($session['session_active'] == 1) {
$session['session_active'] = Display::return_icon('accept.png', get_lang('Active'), array(), ICON_SIZE_SMALL);
} else {
$session['session_active'] = Display::return_icon('error.png', get_lang('Inactive'), array(), ICON_SIZE_SMALL);
}
$session['name'] = Display::url($session['name'], "resume_session.php?id_session=".$session['id']);
$session['coach_name'] = Display::url($session['coach_name'], "user_information.php?user_id=".$session['user_id']);
if ($session['session_active'] == 1) {
$session['session_active'] = Display::return_icon('accept.png', get_lang('Active'), array(), ICON_SIZE_SMALL);
} else {
$session['session_active'] = Display::return_icon('error.png', get_lang('Inactive'), array(), ICON_SIZE_SMALL);
}
$session = self::convert_dates_to_local($session);
switch ($session['visibility']) {
case SESSION_VISIBLE_READ_ONLY: //1
$session['visibility'] = get_lang('ReadOnly');
switch ($session['visibility']) {
case SESSION_VISIBLE_READ_ONLY: //1
$session['visibility'] = get_lang('ReadOnly');
break;
case SESSION_VISIBLE: //2
case SESSION_VISIBLE: //2
case SESSION_AVAILABLE: //4
$session['visibility'] = get_lang('Visible');
$session['visibility'] = get_lang('Visible');
break;
case SESSION_INVISIBLE: //3
$session['visibility'] = api_ucfirst(get_lang('Invisible'));
case SESSION_INVISIBLE: //3
$session['visibility'] = api_ucfirst(get_lang('Invisible'));
break;
}
@ -586,12 +624,14 @@ class SessionManager {
} else {
$formatted_sessions[$session_id] = $session;
}
}
}
}
}
return $formatted_sessions;
}
return $formatted_sessions;
}
/**
*
*/
static function compare_arrays_to_merge($array1, $array2) {
if (empty($array2)) {
return $array1;
@ -606,8 +646,17 @@ class SessionManager {
}
return $array1;
}
/**
* Converts all dates sent through the param array (given form) to correct
* dates with timezones
* @param array The dates
* @return array The same array, with times converted
* @assert ('a') === false
*/
static function convert_dates_to_local($params) {
if (!is_array($params)) {
return false;
}
$params['display_start_date'] = api_get_local_time($params['display_start_date'], null, null, true);
$params['display_end_date'] = api_get_local_time($params['display_end_date'], null, null, true);
@ -624,17 +673,18 @@ class SessionManager {
*
* @param string wanted code
* < code >
* $wanted_code = 'curse' if there are in the DB codes like curse1 curse2 the function will return: course3
* if the course code doest not exist in the DB the same course code will be returned
* $wanted_code = 'sess' if there are names like sess, sess1, sess2 in the
* database already, the function will return: sess3
* if the session name doesn't exist in the DB the same name is returned
* < / code >
* @return string wanted unused code
*/
function generate_nice_next_session_name($session_name) {
$session_name_ok = !self::session_name_exists($session_name);
if (!$session_name_ok) {
function generate_nice_next_session_name($session_name) {
$session_name_ok = !self::session_name_exists($session_name);
if (!$session_name_ok) {
$table = Database::get_main_table(TABLE_MAIN_SESSION);
$session_name = Database::escape_string($session_name);
$sql = "SELECT count(*) as count FROM $table WHERE name LIKE '$session_name%'";
$sql = "SELECT count(*) as count FROM $table WHERE name LIKE '$session_name%'";
$result = Database::query($sql);
if (Database::num_rows($result) > 0 ) {
$row = Database::fetch_array($result);
@ -643,13 +693,12 @@ class SessionManager {
$result = self::session_name_exists($session_name);
if (!$result) {
return $session_name;
}
}
}
return false;
}
return false;
}
return $session_name;
}
/**
* Edit a session
* @author Carlos Vargas from existing code
@ -2687,4 +2736,4 @@ class SessionManager {
);
return $return_array;
}
}
}