Issue #306 - Platform admin: Minor corrections in the script for session import.

skala
Ivan Tcholakov 16 years ago
parent b20726c95d
commit b44cc46864
  1. 14
      main/admin/session_import.php
  2. 186
      main/inc/lib/sessionmanager.lib.php

@ -90,7 +90,7 @@ if ($_POST['formSent']) {
$firstname = api_utf8_decode($node_user->Firstname);
$password = api_utf8_decode($node_user->Password);
if (empty($password)) {
$password = base64_encode(rand(1000, 10000));
$password = api_generate_password();
}
$email = api_utf8_decode($node_user->Email);
$official_code = api_utf8_decode($node_user->OfficialCode);
@ -260,7 +260,7 @@ if ($_POST['formSent']) {
$coach_id = api_get_user_id();
}
$date_start = $node_session->DateStart;
$date_start = api_utf8_decode($node_session->DateStart); // Just in case - encoding conversion.
if (!empty($date_start)) {
list($year_start, $month_start, $day_start) = explode('-', $date_start);
@ -271,7 +271,7 @@ if ($_POST['formSent']) {
$time_start = mktime(0, 0, 0, $month_start, $day_start, $year_start);
}
$date_end = $node_session->DateEnd;
$date_end = api_utf8_decode($node_session->DateEnd);
if (!empty($date_start)) {
list($year_end, $month_end, $day_end) = explode('-', $date_end);
if (empty($year_end) || empty($month_end) || empty($day_end)) {
@ -318,7 +318,7 @@ if ($_POST['formSent']) {
} else {
// Update the session if it is needed.
$my_session_result = SessionManager::get_session_by_name($session_name);
if ($my_session_result == false) {
if ($my_session_result === false) {
// Creating the session.
$sql_session = "INSERT IGNORE INTO $tbl_session SET
name = '".Database::escape_string($session_name)."',
@ -372,7 +372,7 @@ if ($_POST['formSent']) {
// Adding courses to a session.
foreach ($node_session->Course as $node_course) {
$course_code = Database::escape_string($node_course->CourseCode);
$course_code = Database::escape_string(api_utf8_decode($node_course->CourseCode));
// Verify that the course pointed by the course code node exists.
if (CourseManager::course_exists($course_code)) {
// If the course exists we continue.
@ -389,7 +389,7 @@ if ($_POST['formSent']) {
$coach_id = '';
}
$session_course_relation = SessionManager::relation_session_course_exist($session_id, $course_code);
if ($session_course_relation == false) {
if (!$session_course_relation) {
$sql_course = "INSERT INTO $tbl_session_course SET
course_code = '$course_code',
id_coach='$coach_id',
@ -576,7 +576,7 @@ if ($_POST['formSent']) {
$session_counter++;
} else {
$my_session_result = SessionManager::get_session_by_name($session_name);
if ($my_session_result == false) {
if ($my_session_result === false) {
// Creating a session.
$sql_session = "INSERT IGNORE INTO $tbl_session SET

@ -11,7 +11,7 @@
require_once('display.lib.php');
class SessionManager {
private function __construct() {
}
/**
* Fetches a session from the database
@ -26,8 +26,8 @@ class SessionManager {
if (Database::num_rows($r) != 1) { return array(); }
return Database::fetch_array($r,'ASSOC');
}
/**
* Create a session
/**
* Create a session
* @author Carlos Vargas <carlos.vargas@dokeos.com>,from existing code
* @param string name
* @param integer year_start
@ -45,22 +45,22 @@ class SessionManager {
public static function create_session ($sname,$syear_start,$smonth_start,$sday_start,$syear_end,$smonth_end,$sday_end,$snb_days_acess_before,$snb_days_acess_after,$nolimit,$coach_username) {
global $_user;
$name= trim($sname);
$year_start= intval($syear_start);
$month_start=intval($smonth_start);
$day_start=intval($sday_start);
$year_end=intval($syear_end);
$month_end=intval($smonth_end);
$day_end=intval($sday_end);
$nb_days_acess_before = intval($snb_days_acess_before);
$nb_days_acess_after = intval($snb_days_acess_after);
$year_start= intval($syear_start);
$month_start=intval($smonth_start);
$day_start=intval($sday_start);
$year_end=intval($syear_end);
$month_end=intval($smonth_end);
$day_end=intval($sday_end);
$nb_days_acess_before = intval($snb_days_acess_before);
$nb_days_acess_after = intval($snb_days_acess_after);
$tbl_user = Database::get_main_table(TABLE_MAIN_USER);
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$sql = 'SELECT user_id FROM '.$tbl_user.' WHERE username="'.Database::escape_string($coach_username).'"';
$rs = api_sql_query($sql, __FILE__, __LINE__);
$id_coach = Database::result($rs,0,'user_id');
if (empty($nolimit)) {
$date_start="$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
$date_end="$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
@ -79,7 +79,7 @@ class SessionManager {
return $msg;
} elseif (empty($nolimit) && (!$month_end || !$day_end || !$year_end || !checkdate($month_end,$day_end,$year_end))) {
$msg=get_lang('InvalidEndDate');
return $msg;
return $msg;
} elseif(empty($nolimit) && $date_start >= $date_end) {
$msg=get_lang('StartDateShouldBeBeforeEndDate');
return $msg;
@ -90,19 +90,19 @@ class SessionManager {
return $msg;
} else {
api_sql_query("INSERT INTO $tbl_session(name,date_start,date_end,id_coach,session_admin_id, nb_days_access_before_beginning, nb_days_access_after_end) VALUES('".Database::escape_string($name)."','$date_start','$date_end','$id_coach',".intval($_user['user_id']).",".$nb_days_acess_before.", ".$nb_days_acess_after.")",__FILE__,__LINE__);
$id_session=Database::get_last_insert_id();
// add event to system log
$id_session=Database::get_last_insert_id();
// add event to system log
$time = time();
$user_id = api_get_user_id();
$user_id = api_get_user_id();
event_system(LOG_SESSION_CREATE, LOG_SESSION_ID, $id_session, $time, $user_id);
return $id_session;
return $id_session;
}
}
}
/**
* Edit a session
}
/**
* Edit a session
* @author Carlos Vargas <carlos.vargas@dokeos.com>,from existing code
* @param integer id
* @param string name
@ -131,12 +131,12 @@ class SessionManager {
$id_coach= intval($id_coach);
$nb_days_acess_before= intval($nb_days_acess_before);
$nb_days_acess_after = intval($nb_days_acess_after);
$tbl_user = Database::get_main_table(TABLE_MAIN_USER);
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
if (empty($nolimit)) {
$date_start="$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
$date_end="$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
@ -159,7 +159,7 @@ class SessionManager {
} elseif (empty($nolimit) && $date_start >= $date_end) {
$msg=get_lang('StartDateShouldBeBeforeEndDate');
return $msg;
} else {
} else {
$rs = api_sql_query("SELECT id FROM $tbl_session WHERE name='".Database::escape_string($name)."'");
$exists = false;
while ($row = Database::fetch_array($rs)) {
@ -176,7 +176,7 @@ class SessionManager {
date_end='".$date_end."',
id_coach='".$id_coach."',
nb_days_access_before_beginning = ".$nb_days_acess_before.",
nb_days_access_after_end = ".$nb_days_acess_after."
nb_days_access_after_end = ".$nb_days_acess_after."
WHERE id='$id'";
api_sql_query($sql,__FILE__,__LINE__);
/*$sqlu = "UPDATE $tbl_session_rel_course " .
@ -187,12 +187,12 @@ class SessionManager {
}
}
}
/**
* Delete session
/**
* Delete session
* @author Carlos Vargas <carlos.vargas@dokeos.com>, from existing code
* @param array id_checked
* @return void Nothing, or false on error
* The parameters is a array to delete sessions
* @return void Nothing, or false on error
* The parameters is a array to delete sessions
**/
public static function delete_session ($id_checked) {
$tbl_session= Database::get_main_table(TABLE_MAIN_SESSION);
@ -206,7 +206,7 @@ class SessionManager {
} else {
$id_checked=intval($id_checked);
}
if (!api_is_platform_admin()) {
$sql = 'SELECT session_admin_id FROM '.Database :: get_main_table(TABLE_MAIN_SESSION).' WHERE id='.$id_checked;
$rs = api_sql_query($sql,__FILE__,__LINE__);
@ -216,9 +216,9 @@ class SessionManager {
}
api_sql_query("DELETE FROM $tbl_session WHERE id IN($id_checked)",__FILE__,__LINE__);
api_sql_query("DELETE FROM $tbl_session_rel_course WHERE id_session IN($id_checked)",__FILE__,__LINE__);
api_sql_query("DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session IN($id_checked)",__FILE__,__LINE__);
api_sql_query("DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session IN($id_checked)",__FILE__,__LINE__);
api_sql_query("DELETE FROM $tbl_session_rel_user WHERE id_session IN($id_checked)",__FILE__,__LINE__);
// delete extra session fields
$t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
$t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
@ -244,24 +244,24 @@ class SessionManager {
if (count($field_ids) > 0 && count($field_all_ids) > 0) {
foreach($field_ids as $field_id) {
// check if field id is used into table field value
// check if field id is used into table field value
if (in_array($field_id,$field_all_ids)) {
continue;
} else {
$sql_session_field = "DELETE FROM $t_sf WHERE id = '$field_id'";
api_sql_query($sql_session_field,__FILE__,__LINE__);
}
}
}
}
}
// add event to system log
// add event to system log
$time = time();
$user_id = api_get_user_id();
$user_id = api_get_user_id();
event_system(LOG_SESSION_DELETE, LOG_SESSION_ID, $id_checked, $time, $user_id);
}
}
/**
* Subscribes users to the given session and optionally (default) unsubscribes previous users
* @author Carlos Vargas <carlos.vargas@dokeos.com>,from existing code
@ -271,7 +271,7 @@ class SessionManager {
* @return void Nothing, or false on error
**/
public static function suscribe_users_to_session ($id_session,$user_list,$empty_users=true) {
if ($id_session!= strval(intval($id_session))) return false;
foreach($user_list as $intUser){
if ($intUser!= strval(intval($intUser))) return false;
@ -280,7 +280,7 @@ class SessionManager {
$tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
$tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$sql = "SELECT id_user FROM $tbl_session_rel_user WHERE id_session='$id_session'";
$result = api_sql_query($sql,__FILE__,__LINE__);
$existingUsers = array();
@ -290,11 +290,11 @@ class SessionManager {
$sql = "SELECT course_code FROM $tbl_session_rel_course WHERE id_session='$id_session'";
$result=api_sql_query($sql,__FILE__,__LINE__);
$course_list=array();
while($row=Database::fetch_array($result)) {
$course_list[]=$row['course_code'];
}
foreach ($course_list as $enreg_course) {
// for each course in the session
$nbr_users=0;
@ -305,7 +305,7 @@ class SessionManager {
if(!in_array($existing_user, $user_list)) {
$sql = "DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course' AND id_user='$existing_user'";
api_sql_query($sql,__FILE__,__LINE__);
if(Database::affected_rows()) {
$nbr_users--;
}
@ -353,7 +353,7 @@ class SessionManager {
* @param int Session ID
* @param array List of courses IDs
* @param bool Whether to unsubscribe existing users (true, default) or not (false)
* @return void Nothing, or false on error
* @return void Nothing, or false on error
**/
public static function add_courses_to_session ($id_session, $course_list, $empty_courses=true) {
// security checks
@ -388,33 +388,33 @@ class SessionManager {
if (!in_array($existingCourse['course_code'], $course_list)){
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=0;
}
// Pass through the courses list we want to add to the session
foreach ($course_list as $enreg_course) {
$enreg_course = Database::escape_string($enreg_course);
$exists = false;
// check if the course we want to add is already subscribed
// check if the course we want to add is already subscribed
foreach ($existingCourses as $existingCourse) {
if ($enreg_course == $existingCourse['course_code']) {
$exists=true;
}
}
if (!$exists) {
//if the course isn't subscribed yet
//if the course isn't subscribed yet
$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__);
api_sql_query($sql_insert_rel_course ,__FILE__,__LINE__);
//We add the current course in the existing courses array, to avoid adding another time the current course
$existingCourses[]=array('course_code'=>$enreg_course);
$nbr_courses++;
// subscribe all the users from the session to this course inside the session
// subscribe all the users from the session to this course inside the session
$nbr_users=0;
foreach ($user_list as $enreg_user) {
foreach ($user_list as $enreg_user) {
$enreg_user_id = 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_id')";
api_sql_query($sql_insert,__FILE__,__LINE__);
@ -427,48 +427,48 @@ class SessionManager {
}
api_sql_query("UPDATE $tbl_session SET nbr_courses=$nbr_courses WHERE id='$id_session'",__FILE__,__LINE__);
}
/**
* Creates a new extra field for a given session
* @param string Field's internal variable name
* @param int Field's type
* @param string Field's language var name
* @param string Field's language var name
* @return int new extra field id
*/
public static function create_session_extra_field ($fieldvarname, $fieldtype, $fieldtitle) {
// database table definition
$t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
public static function create_session_extra_field ($fieldvarname, $fieldtype, $fieldtitle) {
// database table definition
$t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
$fieldvarname = Database::escape_string($fieldvarname);
$fieldtitle = Database::escape_string($fieldtitle);
$fieldtype = (int)$fieldtype;
$fieldtitle = Database::escape_string($fieldtitle);
$fieldtype = (int)$fieldtype;
$time = time();
$sql_field = "SELECT id FROM $t_sf WHERE field_variable = '$fieldvarname'";
$res_field = api_sql_query($sql_field,__FILE__,__LINE__);
$r_field = Database::fetch_row($res_field);
if (Database::num_rows($res_field)>0) {
if (Database::num_rows($res_field)>0) {
$field_id = $r_field[0];
} else {
// save new fieldlabel into course_field table
// save new fieldlabel into course_field table
$sql = "SELECT MAX(field_order) FROM $t_sf";
$res = api_sql_query($sql,__FILE__,__LINE__);
$order = 0;
if (Database::num_rows($res)>0) {
$row = Database::fetch_row($res);
$order = $row[0]+1;
}
$sql = "INSERT INTO $t_sf
SET field_type = '$fieldtype',
field_variable = '$fieldvarname',
field_display_text = '$fieldtitle',
field_order = '$order',
field_order = '$order',
tms = FROM_UNIXTIME($time)";
$result = api_sql_query($sql,__FILE__,__LINE__);
$field_id=Database::get_last_insert_id();
$field_id=Database::get_last_insert_id();
}
return $field_id;
}
@ -481,8 +481,8 @@ class SessionManager {
* @return boolean true if field updated, false otherwise
*/
public static function update_session_extra_field_value ($session_id,$fname,$fvalue='') {
$t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
$t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
$t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
$fname = Database::escape_string($fname);
$session_id = (int)$session_id;
@ -502,14 +502,14 @@ class SessionManager {
{
$fvalues = Database::escape_string($fvalue);
}
$sqlsf = "SELECT * FROM $t_sf WHERE field_variable='$fname'";
$ressf = api_sql_query($sqlsf,__FILE__,__LINE__);
if(Database::num_rows($ressf)==1)
{ //ok, the field exists
// Check if enumerated field, if the option is available
// Check if enumerated field, if the option is available
$rowsf = Database::fetch_array($ressf);
$tms = time();
$sqlsfv = "SELECT * FROM $t_sfv WHERE session_id = '$session_id' AND field_id = '".$rowsf['id']."' ORDER BY id";
$ressfv = api_sql_query($sqlsfv,__FILE__,__LINE__);
@ -526,10 +526,10 @@ class SessionManager {
}
$rowsfv = Database::fetch_array($ressfv);
if($rowsfv['field_value'] != $fvalues)
{
{
$sqlu = "UPDATE $t_sfv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms) WHERE id = ".$rowsfv['id'];
$resu = api_sql_query($sqlu,__FILE__,__LINE__);
return($resu?true:false);
return($resu?true:false);
}
return true;
}
@ -555,16 +555,16 @@ class SessionManager {
return false; //field not found
}
}
/**
* Checks the relationship between an Session and a Course (return the num_rows)
* @param int user id
* @param int url id
* @return int num_rows or false if there are not fields
* Checks the relationship between a session and a course.
* @param int $session_id
* @param int $course_id
* @return bool Returns TRUE if the session and the course are related, FALSE otherwise.
* */
public static function relation_session_course_exist ($session_id, $course_id) {
public static function relation_session_course_exist ($session_id, $course_id) {
$tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
$return_value = false;
$return_value = false;
$sql= "SELECT course_code FROM $tbl_session_course WHERE id_session = ".Database::escape_string($session_id)." AND course_code = '".Database::escape_string($course_id)."'";
$result = api_sql_query($sql, __FILE__, __LINE__);
$num = Database::num_rows($result);
@ -573,19 +573,19 @@ class SessionManager {
}
return $return_value;
}
/**
* Get the session information by name
* Get the session information by name
* @param string session name
* @return mixed false if the session does not exist, array if the session exist
* */
* @return mixed false if the session does not exist, array if the session exist
* */
public static function get_session_by_name ($session_name) {
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$sql = 'SELECT id, id_coach, date_start, date_end FROM '.$tbl_session.' WHERE name="'.Database::escape_string($session_name).'"';
$result = api_sql_query($sql, __FILE__, __LINE__);
$num = Database::num_rows($result);
if ($num>0){
return Database::fetch_array($result);
return Database::fetch_array($result);
} else {
return false;
}

Loading…
Cancel
Save