');
$form->addSelect('session_category', get_lang('SessionCategory'), $categoriesOptions, array(
'id' => 'session_category',
'class' => 'chzn-select',
'style' => 'width:370px;',
));
$form->addHtmlEditor(
'description',
get_lang('Description'),
false,
false,
array(
'ToolbarSet' => 'Minimal',
)
);
$form->addElement('checkbox', 'show_description', null, get_lang('ShowDescription'));
$visibilityGroup = array();
$visibilityGroup[] = $form->createElement('select', 'session_visibility', null, array(
SESSION_VISIBLE_READ_ONLY => get_lang('SessionReadOnly'),
SESSION_VISIBLE => get_lang('SessionAccessible'),
SESSION_INVISIBLE => api_ucfirst(get_lang('SessionNotAccessible')),
));
$form->addGroup($visibilityGroup, 'visibility_group', get_lang('SessionVisibility'), null, false);
$options = [
0 => get_lang('ByDuration'),
1 => get_lang('ByDates'),
];
$form->addSelect('access', get_lang('Access'), $options, array(
'onchange' => 'accessSwitcher()',
'id' => 'access',
));
$form->addElement('html', '
');
$form->addElement(
'number',
'duration',
array(
get_lang('SessionDurationTitle'),
get_lang('SessionDurationDescription'),
),
array(
'maxlength' => 50,
)
);
$form->addElement('html', '
');
$form->addElement('html', '
');
// Dates
$form->addDateTimePicker(
'access_start_date',
array(get_lang('SessionStartDate'), get_lang('SessionStartDateComment')),
array('id' => 'access_start_date')
);
$form->addDateTimePicker(
'access_end_date',
array(get_lang('SessionEndDate'), get_lang('SessionEndDateComment')),
array('id' => 'access_end_date')
);
$form->addRule(
array('access_start_date', 'access_end_date'),
get_lang('StartDateMustBeBeforeTheEndDate'),
'compare_datetime_text',
'< allow_empty'
);
$form->addDateTimePicker(
'display_start_date',
array(
get_lang('SessionDisplayStartDate'),
get_lang('SessionDisplayStartDateComment'),
),
array('id' => 'display_start_date')
);
$form->addDateTimePicker(
'display_end_date',
array(
get_lang('SessionDisplayEndDate'),
get_lang('SessionDisplayEndDateComment'),
),
array('id' => 'display_end_date')
);
$form->addRule(
array('display_start_date', 'display_end_date'),
get_lang('StartDateMustBeBeforeTheEndDate'),
'compare_datetime_text',
'< allow_empty'
);
$form->addDateTimePicker(
'coach_access_start_date',
array(
get_lang('SessionCoachStartDate'),
get_lang('SessionCoachStartDateComment'),
),
array('id' => 'coach_access_start_date')
);
$form->addDateTimePicker(
'coach_access_end_date',
array(
get_lang('SessionCoachEndDate'),
get_lang('SessionCoachEndDateComment'),
),
array('id' => 'coach_access_end_date')
);
$form->addRule(
array('coach_access_start_date', 'coach_access_end_date'),
get_lang('StartDateMustBeBeforeTheEndDate'),
'compare_datetime_text',
'< allow_empty'
);
$form->addElement('html', '
');
$form->addCheckBox(
'send_subscription_notification',
[
get_lang('SendSubscriptionNotification'),
get_lang('SendAnEmailWhenAUserBeingSubscribed')
]
);
// Extra fields
$extra_field = new ExtraField('session');
$extra = $extra_field->addElements($form, $sessionId);
$form->addElement('html','
');
$js = $extra['jquery_ready_content'];
return ['js' => $js];
}
/**
* 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);
$sessionCourseUserTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
$courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
$where = 'WHERE 1 = 1 ';
$user_id = api_get_user_id();
if (api_is_session_admin() &&
api_get_setting('allow_session_admins_to_see_all_sessions') == 'false'
) {
$where.=" WHERE s.session_admin_id = $user_id ";
}
if (!empty($options['where'])) {
$options['where'] = str_replace('course_title', 'c.title', $options['where']);
$options['where'] = str_replace("( session_active = '0' )", '1=1', $options['where']);
$options['where'] = str_replace(
array("AND session_active = '1' )", " AND ( session_active = '1' )"),
array(') GROUP BY s.name HAVING session_active = 1 ', " GROUP BY s.name HAVING session_active = 1 " )
, $options['where']
);
$options['where'] = str_replace(
array("AND session_active = '0' )", " AND ( session_active = '0' )"),
array(') GROUP BY s.name HAVING session_active = 0 ', " GROUP BY s.name HAVING session_active = '0' "),
$options['where']
);
if (!empty($options['extra'])) {
$options['where'] = str_replace(' 1 = 1 AND', '', $options['where']);
$options['where'] = str_replace('AND', 'OR', $options['where']);
foreach ($options['extra'] as $extra) {
$options['where'] = str_replace($extra['field'], 'fv.field_id = '.$extra['id'].' AND fvo.option_value', $options['where']);
}
}
$where .= ' AND '.$options['where'];
}
$today = api_get_utc_datetime();
$query_rows = "SELECT count(*) as total_rows, c.title as course_title, s.name,
IF (
(s.access_start_date <= '$today' AND '$today' < s.access_end_date) OR
(s.access_start_date = '0000-00-00 00:00:00' AND s.access_end_date = '0000-00-00 00:00:00' ) OR
(s.access_start_date IS NULL AND s.access_end_date IS NULL) OR
(s.access_start_date <= '$today' AND ('0000-00-00 00:00:00' = s.access_end_date OR s.access_end_date IS NULL )) OR
('$today' < s.access_end_date AND ('0000-00-00 00:00:00' = s.access_start_date OR s.access_start_date IS NULL) )
, 1, 0) as session_active
FROM $tbl_session s
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 $sessionCourseUserTable scu
ON s.id = scu.session_id
INNER JOIN $courseTable c
ON c.id = scu.c_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_rows = "SELECT count(*) as total_rows
FROM $tbl_session s
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 $where ";
}
}
$result = Database::query($query_rows);
$num = 0;
if (Database::num_rows($result)) {
$rows = Database::fetch_array($result);
$num = $rows['total_rows'];
}
return $num;
}
/**
* @param string $list_type
* @return array
*/
public static function getGridColumns($list_type = 'simple')
{
// Column config
$operators = array('cn', 'nc');
$date_operators = array('gt', 'ge', 'lt', 'le');
switch ($list_type) {
case 'simple':
$columns = array(
get_lang('Name'),
get_lang('Category'),
get_lang('SessionDisplayStartDate'),
get_lang('SessionDisplayEndDate'),
//get_lang('Coach'),
//get_lang('Status'),
//get_lang('CourseTitle'),
get_lang('Visibility'),
);
$column_model = array (
array('name'=>'name', 'index'=>'s.name', 'width'=>'160', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('sopt' => $operators)),
array('name'=>'category_name', 'index'=>'category_name', 'width'=>'40', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('sopt' => $operators)),
array('name'=>'display_start_date', 'index'=>'display_start_date', 'width'=>'50', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('dataInit' => 'date_pick_today', 'sopt' => $date_operators)),
array('name'=>'display_end_date', 'index'=>'display_end_date', 'width'=>'50', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('dataInit' => 'date_pick_one_month', 'sopt' => $date_operators)),
array('name'=>'visibility', 'index'=>'visibility', 'width'=>'40', 'align'=>'left', 'search' => 'false'),
);
break;
case 'complete':
$columns = array(
get_lang('Name'),
get_lang('SessionDisplayStartDate'),
get_lang('SessionDisplayEndDate'),
get_lang('Coach'),
get_lang('Status'),
get_lang('Visibility'),
get_lang('CourseTitle'),
);
$column_model = array (
array('name'=>'name', 'index'=>'s.name', 'width'=>'200', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('sopt' => $operators)),
array('name'=>'display_start_date', 'index'=>'display_start_date', 'width'=>'70', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('dataInit' => 'date_pick_today', 'sopt' => $date_operators)),
array('name'=>'display_end_date', 'index'=>'display_end_date', 'width'=>'70', 'align'=>'left', 'search' => 'true', 'searchoptions' => array('dataInit' => 'date_pick_one_month', 'sopt' => $date_operators)),
array('name'=>'coach_name', 'index'=>'coach_name', 'width'=>'70', 'align'=>'left', 'search' => 'false', 'searchoptions' => array('sopt' => $operators)),
array('name'=>'session_active', 'index'=>'session_active', 'width'=>'25', 'align'=>'left', 'search' => 'true', 'stype'=>'select',
// for the bottom bar
'searchoptions' => array(
'defaultValue' => '1',
'value' => '1:'.get_lang('Active').';0:'.get_lang('Inactive')),
// for the top bar
'editoptions' => array('value' => '" ":'.get_lang('All').';1:'.get_lang('Active').';0:'.get_lang('Inactive')),
),
array('name'=>'visibility', 'index'=>'visibility', 'width'=>'40', 'align'=>'left', 'search' => 'false'),
array('name'=>'course_title', 'index'=>'course_title', 'width'=>'50', 'hidden' => 'true', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true','sopt' => $operators)),
);
break;
}
// Inject extra session fields
$session_field = new ExtraField('session');
$rules = $session_field->getRules($columns, $column_model);
$column_model[] = array('name'=>'actions', 'index'=>'actions', 'width'=>'80', 'align'=>'left','formatter'=>'action_formatter','sortable'=>'false', 'search' => 'false');
$columns[] = get_lang('Actions');
foreach ($column_model as $col_model) {
$simple_column_name[] = $col_model['name'];
}
$return_array = array(
'columns' => $columns,
'column_model' => $column_model,
'rules' => $rules,
'simple_column_name' => $simple_column_name,
);
return $return_array;
}
/**
* Converts all dates sent through the param array (given form) to correct dates with timezones
* @param array The dates The same array, with times converted
* @param boolean $applyFormat Whether apply the DATE_TIME_FORMAT_SHORT format for sessions
* @return array The same array, with times converted
*/
static function convert_dates_to_local($params, $applyFormat = false)
{
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);
$params['access_start_date'] = api_get_local_time($params['access_start_date'], null, null, true);
$params['access_end_date'] = api_get_local_time($params['access_end_date'], null, null, true);
$params['coach_access_start_date'] = isset($params['coach_access_start_date']) ? api_get_local_time($params['coach_access_start_date'], null, null, true) : null;
$params['coach_access_end_date'] = isset($params['coach_access_end_date']) ? api_get_local_time($params['coach_access_end_date'], null, null, true) : null;
if ($applyFormat) {
if (isset($params['display_start_date'])) {
$params['display_start_date'] = api_format_date($params['display_start_date'], DATE_TIME_FORMAT_SHORT);
}
if (isset($params['display_end_date'])) {
$params['display_end_date'] = api_format_date($params['display_end_date'], DATE_TIME_FORMAT_SHORT);
}
if (isset($params['access_start_date'])) {
$params[''] = api_format_date($params['access_start_date'], DATE_TIME_FORMAT_SHORT);
}
if (isset($params['access_end_date'])) {
$params['access_end_date'] = api_format_date($params['access_end_date'], DATE_TIME_FORMAT_SHORT);
}
if (isset($params['coach_access_start_date'])) {
$params['coach_access_start_date'] = api_format_date($params['coach_access_start_date'], DATE_TIME_FORMAT_SHORT);
}
if (isset($params['coach_access_end_date'])) {
$params['coach_access_end_date'] = api_format_date($params['coach_access_end_date'], DATE_TIME_FORMAT_SHORT);
}
}
return $params;
}
/**
* Gets the admin session list callback of the session/session_list.php
* page with all user/details in the right fomat
* @param array
* @result array Array of rows results
* @asset ('a') === false
*/
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);
$extraFieldTable = Database::get_main_table(TABLE_EXTRA_FIELD);
$tbl_session_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
$tbl_session_field_options = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
$where = 'WHERE 1 = 1 ';
$user_id = api_get_user_id();
if (!api_is_platform_admin()) {
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_western_name_order()) {
$coach_name = " CONCAT(u.firstname, ' ', u.lastname) as coach_name ";
}
$today = api_get_utc_datetime();
$inject_extra_fields = null;
$extra_fields = array();
$extra_fields_info = array();
//for now only sessions
$extra_field = new ExtraField('session');
$double_fields = array();
$extra_field_option = new ExtraFieldOption('session');
if (isset($options['extra'])) {
$extra_fields = $options['extra'];
if (!empty($extra_fields)) {
foreach ($extra_fields as $extra) {
$inject_extra_fields .= " IF (fv.field_id = {$extra['id']}, fvo.option_display_text, NULL ) as {$extra['field']} , ";
if (isset($extra_fields_info[$extra['id']])) {
$info = $extra_fields_info[$extra['id']];
} else {
$info = $extra_field->get($extra['id']);
$extra_fields_info[$extra['id']] = $info;
}
if ($info['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
$double_fields[$info['id']] = $info;
}
}
}
}
$options_by_double = array();
foreach ($double_fields as $double) {
$my_options = $extra_field_option->get_field_options_by_field(
$double['id'],
true
);
$options_by_double['extra_'.$double['field_variable']] = $my_options;
}
//sc.name as category_name,
$select = "
SELECT * FROM (
SELECT DISTINCT
IF (
(s.access_start_date <= '$today' AND '$today' < s.access_end_date) OR
(s.access_start_date = '0000-00-00 00:00:00' AND s.access_end_date = '0000-00-00 00:00:00' ) OR
(s.access_start_date IS NULL AND s.access_end_date IS NULL) OR
(s.access_start_date <= '$today' AND ('0000-00-00 00:00:00' = s.access_end_date OR s.access_end_date IS NULL )) OR
('$today' < s.access_end_date AND ('0000-00-00 00:00:00' = s.access_start_date OR s.access_start_date IS NULL) )
, 1, 0) as session_active,
s.name,
s.nbr_courses,
s.nbr_users,
s.display_start_date,
s.display_end_date,
$coach_name,
access_start_date,
access_end_date,
s.visibility,
u.user_id,
$inject_extra_fields
c.title as course_title,
s.id ";
if (!empty($options['where'])) {
if (!empty($options['extra'])) {
$options['where'] = str_replace(' 1 = 1 AND', '', $options['where']);
$options['where'] = str_replace('AND', 'OR', $options['where']);
foreach ($options['extra'] as $extra) {
$options['where'] = str_replace($extra['field'], 'fv.field_id = '.$extra['id'].' AND fvo.option_value', $options['where']);
}
}
$options['where'] = str_replace('course_title', 'c.title', $options['where']);
$options['where'] = str_replace("( session_active = '0' )", '1=1', $options['where']);
$options['where'] = str_replace(
array("AND session_active = '1' )", " AND ( session_active = '1' )"),
array(') GROUP BY s.name HAVING session_active = 1 ', " GROUP BY s.name HAVING session_active = 1 " )
, $options['where']
);
$options['where'] = str_replace(
array("AND session_active = '0' )", " AND ( session_active = '0' )"),
array(') GROUP BY s.name HAVING session_active = 0 ', " GROUP BY s.name HAVING session_active = '0' "),
$options['where']
);
$where .= ' AND '.$options['where'];
}
if (!empty($options['limit'])) {
$where .= " LIMIT ".$options['limit'];
}
$query = "$select FROM $tbl_session s
LEFT JOIN $tbl_session_field_values fv
ON (fv.item_id = s.id)
LEFT JOIN $extraFieldTable f
ON f.id = fv.field_id
LEFT JOIN $tbl_session_field_options fvo
ON (fv.field_id = fvo.field_id)
LEFT JOIN $tbl_session_rel_course src
ON (src.session_id = s.id)
LEFT JOIN $tbl_course c
ON (src.c_id = c.id)
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) ".
$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
FROM $tbl_session s
LEFT JOIN $tbl_session_field_values fv ON (fv.session_id = s.id)
LEFT 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)
LEFT JOIN $tbl_course c ON (src.c_id = c.id)
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)
$where";
}
}
$query .= ") AS session_table";
if (!empty($options['order'])) {
$query .= " ORDER BY ".$options['order'];
}
//error_log($query);
//echo $query;
$result = Database::query($query);
$formatted_sessions = array();
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 = self::convert_dates_to_local($session);
switch ($session['visibility']) {
case SESSION_VISIBLE_READ_ONLY: //1
$session['visibility'] = get_lang('ReadOnly');
break;
case SESSION_VISIBLE: //2
case SESSION_AVAILABLE: //4
$session['visibility'] = get_lang('Visible');
break;
case SESSION_INVISIBLE: //3
$session['visibility'] = api_ucfirst(get_lang('Invisible'));
break;
}
// Cleaning double selects
foreach ($session as $key => &$value) {
if (isset($options_by_double[$key]) || isset($options_by_double[$key.'_second'])) {
$options = explode('::', $value);
}
$original_key = $key;
if (strpos($key, '_second') === false) {
} else {
$key = str_replace('_second', '', $key);
}
if (isset($options_by_double[$key])) {
if (isset($options[0])) {
if (isset($options_by_double[$key][$options[0]])) {
if (strpos($original_key, '_second') === false) {
$value = $options_by_double[$key][$options[0]]['option_display_text'];
} else {
$value = $options_by_double[$key][$options[1]]['option_display_text'];
}
}
}
}
}
// Magic filter
if (isset($formatted_sessions[$session_id])) {
$formatted_sessions[$session_id] = self::compareArraysToMerge($formatted_sessions[$session_id], $session);
} else {
$formatted_sessions[$session_id] = $session;
}
}
}
return $formatted_sessions;
}
/**
* Compare two arrays
* @param array $array1
* @param array $array2
*
* @return array
*/
static function compareArraysToMerge($array1, $array2)
{
if (empty($array2)) {
return $array1;
}
foreach ($array1 as $key => $item) {
if (!isset($array1[$key])) {
//My string is empty try the other one
if (isset($array2[$key]) && !empty($array2[$key])) {
$array1[$key] = $array2[$key];
}
}
}
return $array1;
}
/**
* Get link to the admin page for this session
* @param int $id Session ID
* @return mixed URL to the admin page to manage the session, or false on error
*/
public static function getAdminPath($id)
{
$id = intval($id);
$session = self::fetch($id);
if (empty($session)) {
return false;
}
return api_get_path(WEB_CODE_PATH) . 'session/resume_session.php?id_session=' . $id;
}
/**
* Get link to the user page for this session.
* If a course is provided, build the link to the course
* @param int $id Session ID
* @param int $courseId Course ID (optional) in case the link has to send straight to the course
* @return mixed URL to the page to use the session, or false on error
*/
public static function getPath($id, $courseId = 0)
{
$id = intval($id);
$session = self::fetch($id);
if (empty($session)) {
return false;
}
if (empty($courseId)) {
return api_get_path(WEB_CODE_PATH) . 'session/index.php?session_id=' . $id;
} else {
$courseInfo = api_get_course_info_by_id($courseId);
if ($courseInfo) {
return $courseInfo['course_public_url'].'?id_session='.$id;
}
}
return false;
}
/**
* Return an associative array 'id_course' => [id_session1, id_session2...]
* where course id_course is in sessions id_session1, id_session2
* for course where user is coach
* i.e. coach for the course or
* main coach for a session the course is in
* for a session category (or woth no session category if empty)
*
* @param $userId
*
* @return array
*/
public static function getSessionCourseForUser($userId)
{
// list of COURSES where user is COURSE session coach
$listCourseCourseCoachSession = self::getCoursesForCourseSessionCoach($userId);
// list of courses where user is MAIN session coach
$listCourseMainCoachSession = self::getCoursesForMainSessionCoach($userId);
// merge these 2 array
$listResCourseSession = $listCourseCourseCoachSession;
foreach ($listCourseMainCoachSession as $courseId2 => $listSessionId2) {
if (isset($listResCourseSession[$courseId2])) {
// if sessionId array exists for this course
// same courseId, merge the list of session
foreach ($listCourseMainCoachSession[$courseId2] as $i => $sessionId2) {
if (!in_array($sessionId2, $listResCourseSession[$courseId2])) {
$listResCourseSession[$courseId2][] = $sessionId2;
}
}
} else {
$listResCourseSession[$courseId2] = $listSessionId2;
}
}
return $listResCourseSession;
}
/**
* Return an associative array 'id_course' => [id_session1, id_session2...]
* where course id_course is in sessions id_session1, id_session2
* @param $userId
*
* @return array
*/
public static function getCoursesForCourseSessionCoach($userId)
{
$listResCourseSession = array();
$tblCourse = Database::get_main_table(TABLE_MAIN_COURSE);
$tblSessionRelCourseRelUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
$sql = "SELECT session_id, c_id, c.id
FROM $tblSessionRelCourseRelUser srcru
LEFT JOIN $tblCourse c
ON c.id = srcru.c_id
WHERE
srcru.user_id =".intval($userId)." AND
srcru.status = 2";
$res = Database::query($sql);
while ($data = Database::fetch_assoc($res)) {
if (self::isSessionDateOkForCoach($data['session_id'])) {
if (!isset($listResCourseSession[$data['id']])) {
$listResCourseSession[$data['id']] = array();
}
$listResCourseSession[$data['id']][] = $data['session_id'];
}
}
return $listResCourseSession;
}
/**
* Return true if coach is allowed to access this session
* @param int $sessionId
* @return bool
*/
public static function isSessionDateOkForCoach($sessionId)
{
return api_get_session_visibility($sessionId);
$listSessionInfo = api_get_session_info($sessionId);
$dateStart = $listSessionInfo['date_start'];
$dateEnd = $listSessionInfo['date_end'];
$nbDaysAccessBeforeBeginning = $listSessionInfo['nb_days_access_before_beginning'];
$nbDaysAccessAfterEnd = $listSessionInfo['nb_days_access_after_end'];
// no start date
if ($dateStart == '0000-00-00') {
return true;
}
$now = time();
$dateStartForCoach = api_strtotime($dateStart.' 00:00:00') - ($nbDaysAccessBeforeBeginning * 86400);
$dateEndForCoach = api_strtotime($dateEnd.' 00:00:00') + ($nbDaysAccessAfterEnd * 86400);
if ($dateEnd == '0000-00-00') {
// start date but no end date
if ($dateStartForCoach <= $now) {
return true;
}
} else {
// start date and end date
if ($dateStartForCoach <= $now && $now <= $dateEndForCoach) {
return true;
}
}
return false;
}
/**
* Return an associative array 'id_course' => [id_session1, id_session2...]
* where course id_course is in sessions id_session1, id_session2
* @param $userId
*
* @return array
*/
public static function getCoursesForMainSessionCoach($userId)
{
$listResCourseSession = array();
$tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
// list of SESSION where user is session coach
$sql = "SELECT id FROM $tblSession
WHERE id_coach = ".intval($userId);
$res = Database::query($sql);
while ($data = Database::fetch_assoc($res)) {
$sessionId = $data['id'];
$listCoursesInSession = self::getCoursesInSession($sessionId);
foreach ($listCoursesInSession as $i => $courseId) {
if (self::isSessionDateOkForCoach($sessionId)) {
if (!isset($listResCourseSession[$courseId])) {
$listResCourseSession[$courseId] = array();
}
$listResCourseSession[$courseId][] = $sessionId;
}
}
}
return $listResCourseSession;
}
/**
* Return an array of course_id used in session $sessionId
* @param $sessionId
*
* @return array
*/
public static function getCoursesInSession($sessionId)
{
$listResultsCourseId = array();
$tblSessionRelCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
$tblCourse = Database::get_main_table(TABLE_MAIN_COURSE);
// list of course in this session
$sql = "SELECT session_id, c.id
FROM $tblSessionRelCourse src
LEFT JOIN $tblCourse c
ON c.id = src.c_id
WHERE session_id = ".intval($sessionId);
$res = Database::query($sql);
while ($data = Database::fetch_assoc($res)) {
$listResultsCourseId[] = $data['id'];
}
return $listResultsCourseId;
}
/**
* Return an array of courses in session for user
* and for each courses the list of session that use this course for user
*
* [0] => array
* userCatId
* userCatTitle
* courseInUserCatList
* [0] => array
* courseId
* title
* courseCode
* sessionCatList
* [0] => array
* catSessionId
* catSessionName
* sessionList
* [0] => array
* sessionId
* sessionName
*
* @param $userId
* @return array
*
*/
public static function getNamedSessionCourseForCoach($userId)
{
$listResults = array();
$listCourseSession = self::getSessionCourseForUser($userId);
foreach ($listCourseSession as $courseId => $listSessionId) {
// Course info
$courseInfo = api_get_course_info_by_id($courseId);
$listOneCourse = array();
$listOneCourse['courseId'] = $courseId;
$listOneCourse['title'] = $courseInfo['title'];
//$listOneCourse['courseCode'] = $courseInfo['code'];
$listOneCourse['course'] = $courseInfo;
$listOneCourse['sessionCatList'] = array();
$listCat = array();
foreach ($listSessionId as $i => $sessionId) {
// here we got all session for this course
// lets check there session categories
$sessionInfo = SessionManager::fetch($sessionId);
$catId = $sessionInfo['session_category_id'];
if (!isset($listCat[$catId])) {
$listCatInfo = self::get_session_category($catId);
$listCat[$catId] = array();
$listCat[$catId]['catSessionId'] = $catId;
$listCat[$catId]['catSessionName'] = $listCatInfo['name'];
$listCat[$catId]['sessionList'] = array();
}
$listSessionInfo = SessionManager::fetch($sessionId);
$listSessionIdName = array(
"sessionId" => $sessionId,
"sessionName" => $listSessionInfo['name'],
);
$listCat[$catId]['sessionList'][] = $listSessionIdName;
}
// sort $listCat by catSessionName
usort($listCat, 'self::compareBySessionName');
// in each catSession sort sessionList by sessionName
foreach($listCat as $i => $listCatSessionInfo) {
$listSessionList = $listCatSessionInfo['sessionList'];
usort($listSessionList, 'self::compareCatSessionInfo');
$listCat[$i]['sessionList'] = $listSessionList;
}
$listOneCourse['sessionCatList'] = $listCat;
// user course category
list($userCatId, $userCatTitle) = CourseManager::getUserCourseCategoryForCourse(
$userId,
$courseId
);
$userCatId = intval($userCatId);
$listResults[$userCatId]['courseInUserCategoryId'] = $userCatId;
$listResults[$userCatId]['courseInUserCategoryTitle'] = $userCatTitle;
$listResults[$userCatId]['courseInUserCatList'][] = $listOneCourse;
}
// sort by user course cat
uasort($listResults, 'self::compareByUserCourseCat');
// sort by course title
foreach ($listResults as $userCourseCatId => $tabCoursesInCat) {
$courseInUserCatList = $tabCoursesInCat['courseInUserCatList'];
uasort($courseInUserCatList, 'self::compareByCourse');
$listResults[$userCourseCatId]['courseInUserCatList'] = $courseInUserCatList;
}
return $listResults;
}
/**
* @param array $listA
* @param array $listB
* @return int
*/
private static function compareCatSessionInfo($listA, $listB)
{
if ($listA['sessionName'] == $listB['sessionName']) {
return 0;
} else if($listA['sessionName'] > $listB['sessionName']) {
return 1;
} else {
return -1;
}
}
/**
* @param array $listA
* @param array $listB
* @return int
*/
private static function compareBySessionName($listA, $listB)
{
if ($listB['catSessionName'] == '') {
return -1;
} else if ($listA['catSessionName'] == '') {
return 1;
} else if ($listA['catSessionName'] == $listB['catSessionName']) {
return 0;
} else if($listA['catSessionName'] > $listB['catSessionName']) {
return 1;
} else {
return -1;
}
}
/**
* @param array $listA
* @param array $listB
* @return int
*/
private static function compareByUserCourseCat($listA, $listB)
{
if ($listA['courseInUserCategoryTitle'] == $listB['courseInUserCategoryTitle']) {
return 0;
} else if($listA['courseInUserCategoryTitle'] > $listB['courseInUserCategoryTitle']) {
return 1;
} else {
return -1;
}
}
/**
* @param array $listA
* @param array $listB
* @return int
*/
private static function compareByCourse($listA, $listB)
{
if ($listA['title'] == $listB['title']) {
return 0;
} else if($listA['title'] > $listB['title']) {
return 1;
} else {
return -1;
}
}
/**
* Return HTML code for displaying session_course_for_coach
* @param $userId
* @return string
*/
public static function getHtmlNamedSessionCourseForCoach($userId)
{
$htmlRes = '';
$listInfo = self::getNamedSessionCourseForCoach($userId);
foreach ($listInfo as $i => $listCoursesInfo) {
$courseInfo = $listCoursesInfo['course'];
$courseCode = $listCoursesInfo['course']['code'];
$listParamsCourse = array();
$listParamsCourse['icon'] = ''.
CourseManager::course_item_html($listParamsCourse, true);
// for each category of session
$htmlCatSessions = '';
foreach ($listCoursesInfo['sessionCatList'] as $j => $listCatSessionsInfo) {
// we got an array of session categories
$catSessionId = $listCoursesInfo['sessionCatList'][$j]['catSessionId'];
$catSessionName = $listCoursesInfo['sessionCatList'][$j]['catSessionName'];
$listParamsCatSession['icon'] = Display::return_icon('folder_blue.png', $catSessionName, array(), ICON_SIZE_LARGE);
$listParamsCatSession['link'] = '';
$listParamsCatSession['title'] = $catSessionName;
$marginShift = 20;
if ($catSessionName != '') {
$htmlCatSessions .= '
' .
CourseManager::course_item_html($listParamsCatSession, true) . '
';
$marginShift = 40;
}
// for each sessions
$listCatSessionSessionList = $listCoursesInfo['sessionCatList'][$j]['sessionList'];
$htmlSession = '';
foreach ($listCatSessionSessionList as $k => $listSessionInfo) {
// we got an array of session info
$sessionId = $listSessionInfo['sessionId'];
$sessionName = $listSessionInfo['sessionName'];
$listParamsSession['icon'] = Display::return_icon('blackboard_blue.png', $sessionName, array(), ICON_SIZE_LARGE);
$listParamsSession['link'] = '';
$linkToCourseSession = $courseInfo['course_public_url'].'?id_session='.$sessionId;
$listParamsSession['title'] =
$sessionName.'
';
$htmlSession .= '
'.
CourseManager::course_item_html($listParamsSession, true).'
';
}
$htmlCatSessions .= $htmlSession;
}
$htmlRes .= $htmlCourse.'
'.$htmlCatSessions.'
';
}
return $htmlRes;
}
}