Minor - Adding docs, removing unused code, code formatting.

1.9.x
Julio Montoya 12 years ago
parent 057ebe686e
commit a3fa07796c
  1. 205
      main/inc/lib/sessionmanager.lib.php

@ -28,10 +28,14 @@ class SessionManager
public static function fetch($id) public static function fetch($id)
{ {
$t = Database::get_main_table(TABLE_MAIN_SESSION); $t = Database::get_main_table(TABLE_MAIN_SESSION);
if ($id != strval(intval($id))) { return array(); } if ($id != strval(intval($id))) {
return array();
}
$s = "SELECT * FROM $t WHERE id = $id"; $s = "SELECT * FROM $t WHERE id = $id";
$r = Database::query($s); $r = Database::query($s);
if (Database::num_rows($r) != 1) { return array(); } if (Database::num_rows($r) != 1) {
return array();
}
return Database::fetch_array($r,'ASSOC'); return Database::fetch_array($r,'ASSOC');
} }
@ -175,7 +179,11 @@ class SessionManager
} }
} }
function session_name_exists($session_name) { /**
* @param string $session_name
* @return bool
*/
function session_name_exists($session_name) {
$session_name = Database::escape_string($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' ")); $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; return $result['count'] > 0;
@ -187,7 +195,6 @@ class SessionManager
*/ */
static function get_count_admin($where_condition = null) static function get_count_admin($where_condition = null)
{ {
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
$tbl_user = Database::get_main_table(TABLE_MAIN_USER); $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
@ -445,7 +452,8 @@ class SessionManager
* </code> * </code>
* @return string wanted unused code * @return string wanted unused code
*/ */
function generate_nice_next_session_name($session_name) { function generate_nice_next_session_name($session_name)
{
$session_name_ok = !self::session_name_exists($session_name); $session_name_ok = !self::session_name_exists($session_name);
if (!$session_name_ok) { if (!$session_name_ok) {
$table = Database::get_main_table(TABLE_MAIN_SESSION); $table = Database::get_main_table(TABLE_MAIN_SESSION);
@ -563,8 +571,9 @@ class SessionManager
$rs = Database::query("SELECT id FROM $tbl_session WHERE name='".Database::escape_string($name)."'"); $rs = Database::query("SELECT id FROM $tbl_session WHERE name='".Database::escape_string($name)."'");
$exists = false; $exists = false;
while ($row = Database::fetch_array($rs)) { while ($row = Database::fetch_array($rs)) {
if ($row['id'] != $id) if ($row['id'] != $id) {
$exists = true; $exists = true;
}
} }
if ($exists) { if ($exists) {
@ -647,7 +656,12 @@ class SessionManager
event_system(LOG_SESSION_DELETE, LOG_SESSION_ID, $id_checked, api_get_utc_datetime(), $userId); event_system(LOG_SESSION_DELETE, LOG_SESSION_ID, $id_checked, api_get_utc_datetime(), $userId);
} }
public static function clear_session_ref_promotion($id_promotion) { /**
* @param int $id_promotion
* @return bool
*/
public static function clear_session_ref_promotion($id_promotion)
{
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$id_promotion = intval($id_promotion); $id_promotion = intval($id_promotion);
$update_sql = "UPDATE $tbl_session SET promotion_id=0 WHERE promotion_id='$id_promotion'"; $update_sql = "UPDATE $tbl_session SET promotion_id=0 WHERE promotion_id='$id_promotion'";
@ -656,16 +670,16 @@ class SessionManager
} else { } else {
return false; return false;
} }
} }
/**
* Subscribes users (students) to the given session and optionally (default) unsubscribes previous users /**
* @author Carlos Vargas from existing code * Subscribes users (students) to the given session and optionally (default) unsubscribes previous users
* @param integer Session ID * @author Carlos Vargas from existing code
* @param array List of user IDs * @param integer Session ID
* @param bool Whether to unsubscribe existing users (true, default) or not (false) * @param array List of user IDs
* @return void Nothing, or false on error * @param bool Whether to unsubscribe existing users (true, default) or not (false)
**/ * @return void Nothing, or false on error
**/
public static function suscribe_users_to_session($id_session, $user_list, $session_visibility = SESSION_VISIBLE_READ_ONLY, $empty_users = true, $send_email = false) { public static function suscribe_users_to_session($id_session, $user_list, $session_visibility = SESSION_VISIBLE_READ_ONLY, $empty_users = true, $send_email = false) {
if ($id_session!= strval(intval($id_session))) return false; if ($id_session!= strval(intval($id_session))) return false;
@ -797,8 +811,7 @@ class SessionManager
$session_id, $session_id,
$course_code, $course_code,
$session_visibility = SESSION_VISIBLE_READ_ONLY $session_visibility = SESSION_VISIBLE_READ_ONLY
) ) {
{
$tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
$tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
@ -898,13 +911,12 @@ class SessionManager
* @param bool Whether to unsubscribe existing users (true, default) or not (false) * @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) { public static function add_courses_to_session ($id_session, $course_list, $empty_courses = true) {
// security checks // security checks
if ($id_session!= strval(intval($id_session))) { return false; } if ($id_session!= strval(intval($id_session))) {
//foreach($course_list as $intCourse){ return false;
// if ($intCourse!= strval(intval($intCourse))) { return false; } }
//}
// initialisation
$tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); $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 = Database::get_main_table(TABLE_MAIN_SESSION);
$tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
@ -1007,12 +1019,12 @@ class SessionManager
} }
/** /**
* Creates a new extra field for a given session * Creates a new extra field for a given session
* @param string Field's internal variable name * @param string Field's internal variable name
* @param int Field's type * @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 * @return int new extra field id
*/ */
public static function create_session_extra_field ($fieldvarname, $fieldtype, $fieldtitle) { public static function create_session_extra_field ($fieldvarname, $fieldtype, $fieldtitle) {
// database table definition // database table definition
$t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD); $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
@ -1052,12 +1064,12 @@ class SessionManager
} }
/** /**
* Update an extra field value for a given session * Update an extra field value for a given session
* @param integer Course ID * @param integer Course ID
* @param string Field variable name * @param string Field variable name
* @param string Field value * @param string Field value
* @return boolean true if field updated, false otherwise * @return boolean true if field updated, false otherwise
*/ */
public static function update_session_extra_field_value ($session_id,$fname,$fvalue='') { 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);
@ -1065,23 +1077,21 @@ class SessionManager
$fname = Database::escape_string($fname); $fname = Database::escape_string($fname);
$session_id = (int)$session_id; $session_id = (int)$session_id;
$fvalues = ''; $fvalues = '';
if(is_array($fvalue)) { if (is_array($fvalue)) {
foreach($fvalue as $val) { foreach ($fvalue as $val) {
$fvalues .= Database::escape_string($val).';'; $fvalues .= Database::escape_string($val).';';
} }
if(!empty($fvalues)) { if (!empty($fvalues)) {
$fvalues = substr($fvalues,0,-1); $fvalues = substr($fvalues,0,-1);
} }
} } else {
else
{
$fvalues = Database::escape_string($fvalue); $fvalues = Database::escape_string($fvalue);
} }
$sqlsf = "SELECT * FROM $t_sf WHERE field_variable='$fname'"; $sqlsf = "SELECT * FROM $t_sf WHERE field_variable='$fname'";
$ressf = Database::query($sqlsf); $ressf = Database::query($sqlsf);
if(Database::num_rows($ressf)==1) if (Database::num_rows($ressf)==1) {
{ //ok, the field exists //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); $rowsf = Database::fetch_array($ressf);
@ -1091,17 +1101,14 @@ class SessionManager
$n = Database::num_rows($ressfv); $n = Database::num_rows($ressfv);
if ($n>1) { if ($n>1) {
//problem, we already have to values for this field and user combination - keep last one //problem, we already have to values for this field and user combination - keep last one
while($rowsfv = Database::fetch_array($ressfv)) while($rowsfv = Database::fetch_array($ressfv)) {
{ if($n > 1) {
if($n > 1)
{
$sqld = "DELETE FROM $t_sfv WHERE id = ".$rowsfv['id']; $sqld = "DELETE FROM $t_sfv WHERE id = ".$rowsfv['id'];
$resd = Database::query($sqld); $resd = Database::query($sqld);
$n--; $n--;
} }
$rowsfv = Database::fetch_array($ressfv); $rowsfv = Database::fetch_array($ressfv);
if($rowsfv['field_value'] != $fvalues) if($rowsfv['field_value'] != $fvalues) {
{
$sqlu = "UPDATE $t_sfv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms) WHERE id = ".$rowsfv['id']; $sqlu = "UPDATE $t_sfv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms) WHERE id = ".$rowsfv['id'];
$resu = Database::query($sqlu); $resu = Database::query($sqlu);
return($resu?true:false); return($resu?true:false);
@ -1135,7 +1142,7 @@ class SessionManager
* Checks the relationship between a session and a course. * Checks the relationship between a session and a course.
* @param int $session_id * @param int $session_id
* @param int $course_id * @param int $course_id
* @return bool Returns TRUE if the session and the course are related, FALSE otherwise. * @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); $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
@ -1268,7 +1275,6 @@ class SessionManager
} }
$result = Database::query($sql); $result = Database::query($sql);
return ($result? true:false); return ($result? true:false);
} }
/** /**
@ -1306,15 +1312,13 @@ class SessionManager
} }
} }
$sql = "DELETE FROM $tbl_session_category WHERE id IN (".$id_checked.")"; $sql = "DELETE FROM $tbl_session_category WHERE id IN (".$id_checked.")";
$rs = @Database::query($sql); $rs = Database::query($sql);
$result = Database::affected_rows(); $result = Database::affected_rows();
// Add event to system log // Add event to system log
$user_id = api_get_user_id(); $user_id = api_get_user_id();
event_system(LOG_SESSION_CATEGORY_DELETE, LOG_SESSION_CATEGORY_ID, $id_checked, api_get_utc_datetime(), $user_id); event_system(LOG_SESSION_CATEGORY_DELETE, LOG_SESSION_CATEGORY_ID, $id_checked, api_get_utc_datetime(), $user_id);
// delete extra session fields where field variable is "PERIODO" // delete extra session fields where field variable is "PERIODO"
$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); $t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
@ -1327,7 +1331,7 @@ class SessionManager
$row_field = Database::fetch_row($rs_field); $row_field = Database::fetch_row($rs_field);
$field_id = $row_field[0]; $field_id = $row_field[0];
$sql_delete_sfv = "DELETE FROM $t_sfv WHERE session_id = '$id_checked' AND field_id = '$field_id'"; $sql_delete_sfv = "DELETE FROM $t_sfv WHERE session_id = '$id_checked' AND field_id = '$field_id'";
$rs_delete_sfv = Database::query($sql_delete_sfv); Database::query($sql_delete_sfv);
} }
$sql = "SELECT * FROM $t_sfv WHERE field_id = '$field_id' "; $sql = "SELECT * FROM $t_sfv WHERE field_id = '$field_id' ";
@ -1335,7 +1339,7 @@ class SessionManager
if (Database::num_rows($rs_field_id) == 0) { if (Database::num_rows($rs_field_id) == 0) {
$sql_delete_sf = "DELETE FROM $t_sf WHERE id = '$field_id'"; $sql_delete_sf = "DELETE FROM $t_sf WHERE id = '$field_id'";
$rs_delete_sf = Database::query($sql_delete_sf); Database::query($sql_delete_sf);
} }
return true; return true;
@ -1447,7 +1451,7 @@ class SessionManager
$course_code = api_get_course_id(); $course_code = api_get_course_id();
} }
// definitios of tables // Table definition
$tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); $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_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
$tbl_user = Database::get_main_table(TABLE_MAIN_USER); $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
@ -1500,12 +1504,12 @@ class SessionManager
} }
} }
/** /**
* Subscribes sessions to human resource manager (Dashboard feature) * Subscribes sessions to human resource manager (Dashboard feature)
* @param int Human Resource Manager id * @param int Human Resource Manager id
* @param array Sessions id * @param array Sessions id
* @param int Relation type * @param int Relation type
**/ **/
public static function suscribe_sessions_to_hr_manager($hr_manager_id,$sessions_list) { public static function suscribe_sessions_to_hr_manager($hr_manager_id,$sessions_list) {
global $_configuration; global $_configuration;
// Database Table Definitions // Database Table Definitions
@ -1531,18 +1535,6 @@ class SessionManager
Database::query($sql); Database::query($sql);
} }
} }
/*
//Deleting assigned courses in sessions to hrm_id
$sql = "SELECT * FROM $tbl_session_rel_course_user WHERE id_user = $hr_manager_id ";
$result = Database::query($sql);
if (Database::num_rows($result) > 0) {
$sql = "DELETE FROM $tbl_session_rel_course_user WHERE id_user = $hr_manager_id ";
Database::query($sql);
}
*/
// inserting new sessions list // inserting new sessions list
if (is_array($sessions_list)) { if (is_array($sessions_list)) {
foreach ($sessions_list as $session_id) { foreach ($sessions_list as $session_id) {
@ -1747,11 +1739,20 @@ class SessionManager
return $status; return $status;
} }
static function get_all_sessions_by_promotion($id) { /**
* @param int $id
* @return array
*/
static function get_all_sessions_by_promotion($id)
{
$t = Database::get_main_table(TABLE_MAIN_SESSION); $t = Database::get_main_table(TABLE_MAIN_SESSION);
return Database::select('*', $t, array('where'=>array('promotion_id = ?'=>$id))); return Database::select('*', $t, array('where'=>array('promotion_id = ?'=>$id)));
} }
/**
* @param int $promotion_id
* @param array $list
*/
static function suscribe_sessions_to_promotion($promotion_id, $list) { static function suscribe_sessions_to_promotion($promotion_id, $list) {
$t = Database::get_main_table(TABLE_MAIN_SESSION); $t = Database::get_main_table(TABLE_MAIN_SESSION);
$params = array(); $params = array();
@ -1788,15 +1789,8 @@ class SessionManager
* @return int The new session ID on success, 0 otherwise * @return int The new session ID on success, 0 otherwise
* @todo make sure the extra session fields are copied too * @todo make sure the extra session fields are copied too
*/ */
/** public function copy_session($id, $copy_courses = true, $copy_users = true, $create_new_courses = false, $set_exercises_lp_invisible = false)
* @param $id {
* @param $copy_courses
* @param $copy_users
* @param $create_new_courses
* @param $set_exercises_lp_invisible
* @return int
*/
public function copy_session($id, $copy_courses = true, $copy_users = true, $create_new_courses = false, $set_exercises_lp_invisible = false) {
$id = intval($id); $id = intval($id);
$s = self::fetch($id); $s = self::fetch($id);
$s['year_start'] = substr($s['date_start'],0,4); $s['year_start'] = substr($s['date_start'],0,4);
@ -1834,8 +1828,8 @@ class SessionManager
foreach ($courses as $course) { foreach ($courses as $course) {
$short_courses[] = $course; $short_courses[] = $course;
} }
} }
$courses = null; $courses = null;
//We will copy the current courses of the session to new courses //We will copy the current courses of the session to new courses
@ -1874,7 +1868,7 @@ class SessionManager
} }
} else { } else {
foreach($short_courses as $course_data) { foreach($short_courses as $course_data) {
$new_short_courses[] = $course_data['code']; $new_short_courses[] = $course_data['code'];
} }
} }
@ -1900,6 +1894,11 @@ class SessionManager
return $sid; return $sid;
} }
/**
* @param int $user_id
* @param int $session_id
* @return bool
*/
static function user_is_general_coach($user_id, $session_id) { static function user_is_general_coach($user_id, $session_id) {
$session_id = intval($session_id); $session_id = intval($session_id);
$user_id = intval($user_id); $user_id = intval($user_id);
@ -1919,7 +1918,8 @@ class SessionManager
* @param int ID of the URL we want to filter on (optional) * @param int ID of the URL we want to filter on (optional)
* @return int Number of sessions * @return int Number of sessions
*/ */
public static function count_sessions($access_url_id=null) { public static function count_sessions($access_url_id = null)
{
$session_table = Database::get_main_table(TABLE_MAIN_SESSION); $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
$access_url_rel_session_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); $access_url_rel_session_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
$sql = "SELECT count(id) FROM $session_table s"; $sql = "SELECT count(id) FROM $session_table s";
@ -1932,7 +1932,11 @@ class SessionManager
return $row[0]; return $row[0];
} }
static function protect_session_edit($id) { /**
* @param int $id
*/
static function protect_session_edit($id)
{
api_protect_admin_script(true); api_protect_admin_script(true);
$session_info = self::fetch($id); $session_info = self::fetch($id);
if (!api_is_platform_admin() && api_get_setting('allow_session_admins_to_manage_all_sessions') != 'true') { if (!api_is_platform_admin() && api_get_setting('allow_session_admins_to_manage_all_sessions') != 'true') {
@ -1943,14 +1947,16 @@ class SessionManager
} }
/** /**
* @param $course_code * @param string $course_code
* @return array * @return array
*/ */
public static function get_session_by_course($course_code) { public static function get_session_by_course($course_code)
{
$table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); $table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
$table_session = Database::get_main_table(TABLE_MAIN_SESSION); $table_session = Database::get_main_table(TABLE_MAIN_SESSION);
$course_code = Database::escape_string($course_code); $course_code = Database::escape_string($course_code);
$sql = "SELECT name, s.id FROM $table_session_course sc INNER JOIN $table_session s ON (sc.id_session = s.id) $sql = "SELECT name, s.id
FROM $table_session_course sc INNER JOIN $table_session s ON (sc.id_session = s.id)
WHERE sc.course_code = '$course_code' "; WHERE sc.course_code = '$course_code' ";
$result = Database::query($sql); $result = Database::query($sql);
return Database::store_result($result); return Database::store_result($result);
@ -2272,7 +2278,6 @@ class SessionManager
} }
$course_counter++; $course_counter++;
$pattern = "/\[(.*?)\]/"; $pattern = "/\[(.*?)\]/";
preg_match_all($pattern, $course, $matches); preg_match_all($pattern, $course, $matches);
@ -2521,11 +2526,11 @@ class SessionManager
*/ */
public static function updateCoaches($sessionId, $courseCode, $coachList, $deleteCoachesNotInList = false) public static function updateCoaches($sessionId, $courseCode, $coachList, $deleteCoachesNotInList = false)
{ {
$currentCoaches = SessionManager::getCoachesByCourseSession($sessionId, $courseCode); $currentCoaches = self::getCoachesByCourseSession($sessionId, $courseCode);
if (!empty($coachList)) { if (!empty($coachList)) {
foreach ($coachList as $userId) { foreach ($coachList as $userId) {
SessionManager::set_coach_to_course_session($userId, $sessionId, $courseCode); self::set_coach_to_course_session($userId, $sessionId, $courseCode);
} }
} }
@ -2538,7 +2543,7 @@ class SessionManager
if (!empty($coachesToDelete)) { if (!empty($coachesToDelete)) {
foreach ($coachesToDelete as $userId) { foreach ($coachesToDelete as $userId) {
SessionManager::set_coach_to_course_session($userId, $sessionId, $courseCode, true); self::set_coach_to_course_session($userId, $sessionId, $courseCode, true);
} }
} }
} }

Loading…
Cancel
Save