debugOn();
$xajax -> registerFunction ('search_courses');
// setting the section (for the tabs)
$this_section = SECTION_PLATFORM_ADMIN;
// Access restrictions
api_protect_admin_script(true);
// setting breadcrumbs
$interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
$interbreadcrumb[] = array('url' => 'session_list.php','name' => get_lang('SessionList'));
// Database Table Definitions
$tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
$tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
$tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
// setting the name of the tool
$tool_name= get_lang('SubscribeCoursesToSession');
$id_session=intval($_GET['id_session']);
$add_type = 'multiple';
if(isset($_GET['add_type']) && $_GET['add_type']!=''){
$add_type = Security::remove_XSS($_REQUEST['add_type']);
}
if (!api_is_platform_admin()) {
$sql = 'SELECT session_admin_id FROM '.Database :: get_main_table(TABLE_MAIN_SESSION).' WHERE id='.$id_session;
$rs = api_sql_query($sql,__FILE__,__LINE__);
if (Database::result($rs,0,0)!=$_user['user_id']) {
api_not_allowed(true);
}
}
function search_courses($needle)
{
global $tbl_course, $tbl_session_rel_course, $id_session;
$xajax_response = new XajaxResponse();
$return = '';
if(!empty($needle)) {
// xajax send utf8 datas... datas in db can be non-utf8 datas
$charset = api_get_setting('platform_charset');
$needle = mb_convert_encoding($needle, $charset, 'utf-8');
// search users where username or firstname or lastname begins likes $needle
$sql = 'SELECT course.code, course.visual_code, course.title, session_rel_course.id_session
FROM '.$tbl_course.' course
LEFT JOIN '.$tbl_session_rel_course.' session_rel_course
ON course.code = session_rel_course.course_code
AND session_rel_course.id_session = '.intval($id_session).'
WHERE course.visual_code LIKE "'.$needle.'%"
OR course.title LIKE "'.$needle.'%"';
global $_configuration;
if ($_configuration['multiple_access_urls']==true) {
$tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
$access_url_id = api_get_current_access_url_id();
if ($access_url_id != -1){
$sql = 'SELECT course.code, course.visual_code, course.title, session_rel_course.id_session
FROM '.$tbl_course.' course
LEFT JOIN '.$tbl_session_rel_course.' session_rel_course
ON course.code = session_rel_course.course_code
AND session_rel_course.id_session = '.intval($id_session).'
INNER JOIN '.$tbl_course_rel_access_url.' url_course ON (url_course.course_code=course.code)
WHERE access_url_id = '.$access_url_id.' AND (course.visual_code LIKE "'.$needle.'%"
OR course.title LIKE "'.$needle.'%" )';
}
}
$rs = api_sql_query($sql, __FILE__, __LINE__);
$course_list = array();
while($course = Database :: fetch_array($rs)) {
$course_list[] = $course['code'];
$return .= ''.$course['title'].' ('.$course['visual_code'].')
';
}
}
$_SESSION['course_list'] = $course_list;
$xajax_response -> addAssign('ajax_list_courses','innerHTML',utf8_encode($return));
return $xajax_response;
}
$xajax -> processRequests();
$htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
$htmlHeadXtra[] = '
';
$formSent=0;
$errorMsg=$firstLetterCourse=$firstLetterSession='';
$CourseList=$SessionList=array();
$courses=$sessions=array();
$noPHP_SELF=true;
if ($_POST['formSent']) {
$formSent=$_POST['formSent'];
$firstLetterCourse=$_POST['firstLetterCourse'];
$firstLetterSession=$_POST['firstLetterSession'];
$CourseList=$_POST['SessionCoursesList'];
if (!is_array($CourseList)) {
$CourseList=array();
}
$nbr_courses=0;
$id_coach = api_sql_query("SELECT id_coach FROM $tbl_session WHERE id=$id_session");
$id_coach = Database::fetch_array($id_coach);
$id_coach = $id_coach[0];
$rs = api_sql_query("SELECT course_code FROM $tbl_session_rel_course WHERE id_session=$id_session");
$existingCourses = api_store_result($rs);
$sql="SELECT id_user
FROM $tbl_session_rel_user
WHERE id_session = $id_session";
$result=api_sql_query($sql,__FILE__,__LINE__);
$UserList=api_store_result($result);
foreach($CourseList as $enreg_course) {
$enreg_course = Database::escape_string($enreg_course);
$exists = false;
foreach($existingCourses as $existingCourse) {
if($enreg_course == $existingCourse['course_code']) {
$exists=true;
}
}
if(!$exists) {
$sql_insert_rel_course= "INSERT INTO $tbl_session_rel_course(id_session,course_code, id_coach) VALUES('$id_session','$enreg_course','$id_coach')";
api_sql_query($sql_insert_rel_course ,__FILE__,__LINE__);
//We add in the existing courses table the current course, to not try to add another time the current course
$existingCourses[]=array('course_code'=>$enreg_course);
$nbr_users=0;
foreach ($UserList as $enreg_user) {
$enreg_user = Database::escape_string($enreg_user['id_user']);
$sql_insert = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$enreg_course','$enreg_user')";
api_sql_query($sql_insert,__FILE__,__LINE__);
if(Database::affected_rows()) {
$nbr_users++;
}
}
api_sql_query("UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'",__FILE__,__LINE__);
}
}
foreach($existingCourses as $existingCourse) {
if(!in_array($existingCourse['course_code'], $CourseList)){
api_sql_query("DELETE FROM $tbl_session_rel_course WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
api_sql_query("DELETE FROM $tbl_session_rel_course_rel_user WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
}
}
$nbr_courses=count($CourseList);
api_sql_query("UPDATE $tbl_session SET nbr_courses=$nbr_courses WHERE id='$id_session'",__FILE__,__LINE__);
if(isset($_GET['add']))
header('Location: add_users_to_session.php?id_session='.$id_session.'&add=true');
else
header('Location: resume_session.php?id_session='.$id_session);
//header('Location: '.$_GET['page'].'?id_session='.$id_session);
}
Display::display_header($tool_name);
api_display_tool_title($tool_name);
/*$sql = 'SELECT COUNT(1) FROM '.$tbl_course;
$rs = api_sql_query($sql, __FILE__, __LINE__);
$count_courses = mysql_result($rs, 0, 0);*/
$ajax_search = $add_type == 'unique' ? true : false;
$nosessionCourses = $sessionCourses = array();
if ($ajax_search) {
$sql="SELECT code, title, visual_code, id_session
FROM $tbl_course course
INNER JOIN $tbl_session_rel_course session_rel_course
ON course.code = session_rel_course.course_code
AND session_rel_course.id_session = ".intval($id_session)."
ORDER BY ".(sizeof($courses)?"(code IN(".implode(',',$courses).")) DESC,":"")." title";
global $_configuration;
if ($_configuration['multiple_access_urls']==true) {
$tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
$access_url_id = api_get_current_access_url_id();
if ($access_url_id != -1){
$sql="SELECT code, title, visual_code, id_session
FROM $tbl_course course
INNER JOIN $tbl_session_rel_course session_rel_course
ON course.code = session_rel_course.course_code
AND session_rel_course.id_session = ".intval($id_session)."
INNER JOIN $tbl_course_rel_access_url url_course ON (url_course.course_code=course.code)
WHERE access_url_id = $access_url_id
ORDER BY ".(sizeof($courses)?"(code IN(".implode(',',$courses).")) DESC,":"")." title";
}
}
$result=api_sql_query($sql,__FILE__,__LINE__);
$Courses=api_store_result($result);
foreach($Courses as $course) {
$sessionCourses[$course['code']] = $course ;
}
} else {
$sql="SELECT code, title, visual_code, id_session
FROM $tbl_course course
LEFT JOIN $tbl_session_rel_course session_rel_course
ON course.code = session_rel_course.course_code
AND session_rel_course.id_session = ".intval($id_session)."
ORDER BY ".(sizeof($courses)?"(code IN(".implode(',',$courses).")) DESC,":"")." title";
global $_configuration;
if ($_configuration['multiple_access_urls']==true) {
$tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
$access_url_id = api_get_current_access_url_id();
if ($access_url_id != -1){
$sql="SELECT code, title, visual_code, id_session
FROM $tbl_course course
LEFT JOIN $tbl_session_rel_course session_rel_course
ON course.code = session_rel_course.course_code
AND session_rel_course.id_session = ".intval($id_session)."
INNER JOIN $tbl_course_rel_access_url url_course ON (url_course.course_code=course.code)
WHERE access_url_id = $access_url_id
ORDER BY ".(sizeof($courses)?"(code IN(".implode(',',$courses).")) DESC,":"")." title";
}
}
$result=api_sql_query($sql,__FILE__,__LINE__);
$Courses=api_store_result($result);
foreach($Courses as $course) {
if ($course['id_session'] == $id_session) {
$sessionCourses[$course['code']] = $course ;
} else {
$nosessionCourses[$course['code']] = $course ;
}
}
}
unset($Courses);
if($add_type == 'multiple') {
$link_add_type_unique = ''.get_lang('SessionAddTypeUnique').'';
$link_add_type_multiple = get_lang('SessionAddTypeMultiple');
} else {
$link_add_type_unique = get_lang('SessionAddTypeUnique');
$link_add_type_multiple = ''.get_lang('SessionAddTypeMultiple').'';
}
?>