diff --git a/public/main/admin/statistics/index.php b/public/main/admin/statistics/index.php index a1980d0eb1..9a1eda51d7 100644 --- a/public/main/admin/statistics/index.php +++ b/public/main/admin/statistics/index.php @@ -7,6 +7,9 @@ use Chamilo\CoreBundle\Framework\Container; /** * This tool show global Statistics on general platform events. */ + +use Chamilo\CoreBundle\Entity\Session; + $cidReset = true; require_once __DIR__.'/../../inc/global.inc.php'; @@ -386,6 +389,7 @@ switch ($report) { // User count $tableSession = Database::get_main_table(TABLE_MAIN_SESSION); + $tableSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER); $sql = "SELECT * FROM $tableSession WHERE (display_start_date BETWEEN '$start' AND '$end' OR @@ -405,10 +409,12 @@ switch ($report) { $content .= Display::page_subheader2(get_lang('GeneralStats')); // Coach // Coach - $sql = "SELECT count(DISTINCT(id_coach)) count FROM $tableSession + $sql = "SELECT COUNT(DISTINCT(sru.user_id)) count FROM $tableSession s + INNER JOIN $tableSessionRelUser sru ON s.id = sru.session_id WHERE - (display_start_date BETWEEN '$start' AND '$end' OR - display_end_date BETWEEN '$start' AND '$end') + (s.display_start_date BETWEEN '$start' AND '$end' OR + s.display_end_date BETWEEN '$start' AND '$end') + AND sru.relation_type = ".Session::SESSION_COACH." $statusCondition "; $result = Database::query($sql); diff --git a/public/main/cron/import_csv.php b/public/main/cron/import_csv.php index cd263e40d7..33d7eae66c 100644 --- a/public/main/cron/import_csv.php +++ b/public/main/cron/import_csv.php @@ -1135,7 +1135,8 @@ class ImportCsv $teacher = current($teachers); $teacherId = $teacher['user_id']; } else { - $teacherId = $sessionInfo['id_coach']; + $generalCoachesId = SessionManager::getGeneralCoachesIdForSession($sessionId); + $teacherId = $generalCoachesId[0] ?? 0; } } } else { diff --git a/public/main/forum/forumfunction.inc.php b/public/main/forum/forumfunction.inc.php index 099a430585..218c403925 100644 --- a/public/main/forum/forumfunction.inc.php +++ b/public/main/forum/forumfunction.inc.php @@ -1491,9 +1491,12 @@ function get_thread_users_details($thread_id) $orderby = 'ORDER BY user.lastname, user.firstname'; } - if (api_get_session_id()) { - $session_info = api_get_session_info(api_get_session_id()); - $user_to_avoid = "'".$session_info['id_coach']."', '".$session_info['session_admin_id']."'"; + $session = api_get_session_entity(); + + if ($session) { + $coachesId = $session->getGeneralCoaches()->map(fn(User $coach) => $coach->getId())->getValues(); + $coachesId[] = $session->getSessionAdmin()->getId(); + $user_to_avoid = implode(', ', $coachesId); //not showing coaches $sql = "SELECT DISTINCT user.id, user.lastname, user.firstname, thread_id FROM $t_posts p, $t_users user, $t_session_rel_user session_rel_user_rel_course @@ -1552,9 +1555,12 @@ function get_thread_users_qualify($thread_id) $orderby = 'ORDER BY user.lastname, user.firstname'; } - if ($sessionId) { - $session_info = api_get_session_info($sessionId); - $user_to_avoid = "'".$session_info['id_coach']."', '".$session_info['session_admin_id']."'"; + $session = api_get_session_entity(); + + if ($session) { + $coachesId = $session->getGeneralCoaches()->map(fn(User $coach) => $coach->getId())->getValues(); + $coachesId[] = $session->getSessionAdmin()->getId(); + $user_to_avoid = implode(', ', $coachesId); //not showing coaches $sql = "SELECT DISTINCT post.poster_id, user.lastname, user.firstname, post.thread_id,user.id,qualify.qualify FROM $t_posts post , $t_users user, $t_session_rel_user scu, $t_qualify qualify @@ -1635,9 +1641,12 @@ function get_thread_users_not_qualify($thread_id) $cad = substr($cad, 0, strlen($cad) - 1); } - if (api_get_session_id()) { - $session_info = api_get_session_info(api_get_session_id()); - $user_to_avoid = "'".$session_info['id_coach']."', '".$session_info['session_admin_id']."'"; + $session = api_get_session_entity(); + + if ($session) { + $coachesId = $session->getGeneralCoaches()->map(fn(User $coach) => $coach->getId())->getValues(); + $coachesId[] = $session->getSessionAdmin()->getId(); + $user_to_avoid = implode(', ', $coachesId); //not showing coaches $sql = "SELECT DISTINCT user.id, user.lastname, user.firstname, post.thread_id FROM $t_posts post , $t_users user, $t_session_rel_user session_rel_user_rel_course diff --git a/public/main/inc/lib/AnnouncementManager.php b/public/main/inc/lib/AnnouncementManager.php index b48ed70e64..be7db4b358 100644 --- a/public/main/inc/lib/AnnouncementManager.php +++ b/public/main/inc/lib/AnnouncementManager.php @@ -5,6 +5,7 @@ use Chamilo\CoreBundle\Entity\AbstractResource; use Chamilo\CoreBundle\Entity\ExtraField as ExtraFieldEntity; use Chamilo\CoreBundle\Entity\ExtraFieldValues; +use Chamilo\CoreBundle\Entity\User; use Chamilo\CoreBundle\Framework\Container; use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter; use Chamilo\CourseBundle\Entity\CAnnouncement; @@ -82,15 +83,20 @@ class AnnouncementManager $generalCoachEmail = ''; $coaches = ''; if (!empty($sessionId)) { - $sessionInfo = api_get_session_info($sessionId); + $session = api_get_session_entity($sessionId); + $coaches = CourseManager::get_coachs_from_course_to_string( $sessionId, $courseInfo['real_id'] ); - $generalCoach = api_get_user_info($sessionInfo['id_coach']); - $generalCoachName = $generalCoach['complete_name']; - $generalCoachEmail = $generalCoach['email']; + $session->getGeneralCoaches() + ->forAll(function (int $key, User $user) use (&$generalCoachName, &$generalCoachEmail) { + $generalCoachName .= $user->getFullname().PHP_EOL; + $generalCoachEmail .= $user->getEmail().PHP_EOL; + + return true; + }); } $data = []; diff --git a/public/main/inc/lib/ScheduledAnnouncement.php b/public/main/inc/lib/ScheduledAnnouncement.php index c981ab537d..44b9d7ee95 100644 --- a/public/main/inc/lib/ScheduledAnnouncement.php +++ b/public/main/inc/lib/ScheduledAnnouncement.php @@ -2,6 +2,9 @@ /* For licensing terms, see /license.txt */ +use Chamilo\CoreBundle\Entity\Session; +use Chamilo\CoreBundle\Entity\User; + /** * Config setting: * $_configuration['allow_scheduled_announcements'] = true;. @@ -300,22 +303,24 @@ class ScheduledAnnouncement extends Model if (!empty($result['date']) && $result['date'] < $now) { $sessionId = $result['session_id']; $sessionInfo = api_get_session_info($sessionId); + $session = api_get_session_entity($sessionId); if (empty($sessionInfo)) { continue; } $users = SessionManager::get_users_by_session( $sessionId, - 0, + Session::STUDENT, false, $urlId ); + $generalCoaches = $session->getGeneralCoaches(); - $coachId = $sessionInfo['id_coach']; - - if (empty($users) || empty($coachId)) { + if (empty($users) || 0 === $generalCoaches->count()) { continue; } + $coachId = $generalCoaches->first()->getId(); + $coachList = []; if ($users) { $sendToCoaches = $extraFieldValue->get_values_by_handler_and_field_variable( @@ -379,21 +384,19 @@ class ScheduledAnnouncement extends Model true ); - $generalCoach = ''; - $generalCoachEmail = ''; - if (!empty($sessionInfo['id_coach'])) { - $coachInfo = api_get_user_info($sessionInfo['id_coach']); - if (!empty($coachInfo)) { - $generalCoach = $coachInfo['complete_name']; - $generalCoachEmail = $coachInfo['email']; - } + $generalCoachName = []; + $generalCoachEmail = []; + /** @var User $generalCoach */ + foreach ($generalCoaches as $generalCoach) { + $generalCoachName[] = $generalCoach->getFullname(); + $generalCoachEmail[] = $generalCoach->getEmail(); } $tags = [ '((session_name))' => $sessionInfo['name'], '((session_start_date))' => $startTime, - '((general_coach))' => $generalCoach, - '((general_coach_email))' => $generalCoachEmail, + '((general_coach))' => implode(' - ', $generalCoachName), + '((general_coach_email))' => implode(' - ', $generalCoachEmail), '((session_end_date))' => $endTime, '((user_complete_name))' => $userInfo['complete_name'], '((user_firstname))' => $userInfo['firstname'], diff --git a/public/main/inc/lib/api.lib.php b/public/main/inc/lib/api.lib.php index 9ed3024471..b983050d9a 100644 --- a/public/main/inc/lib/api.lib.php +++ b/public/main/inc/lib/api.lib.php @@ -2860,7 +2860,7 @@ function api_is_course_admin() /** * Checks whether the current user is a course coach - * Based on the presence of user in session.id_coach (session general coach). + * Based on the presence of user in session_rel_user.relation_type (as session general coach, value 3). * * @return bool True if current user is a course coach */ @@ -2943,6 +2943,7 @@ function api_is_coach($session_id = 0, $courseId = null, $check_student_view = t $session_table = Database::get_main_table(TABLE_MAIN_SESSION); $session_rel_course_rel_user_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); + $tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER); $sessionIsCoach = []; if (!empty($courseId)) { @@ -2959,10 +2960,11 @@ function api_is_coach($session_id = 0, $courseId = null, $check_student_view = t } if (!empty($session_id)) { - $sql = "SELECT DISTINCT id, name, access_start_date, access_end_date - FROM $session_table - WHERE session.id_coach = $userId AND id = $session_id - ORDER BY access_start_date, access_end_date, name"; + $sql = "SELECT DISTINCT s.id + FROM $session_table AS s + INNER JOIN $tblSessionRelUser sru ON s.id = sru.session_id + WHERE sru.user_id = $userId AND s.id = $session_id AND sru.relation_type = ".SessionEntity::SESSION_COACH + ." ORDER BY s.access_start_date, s.access_end_date, s.name"; $result = Database::query($sql); if (!empty($sessionIsCoach)) { $sessionIsCoach = array_merge( @@ -4826,24 +4828,25 @@ function api_is_course_visible_for_user($userid = null, $cid = null) // Is it the session coach or the session admin? $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); + $tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER); $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); - $sql = "SELECT - session.id_coach, session_admin_id, session.id - FROM - $tbl_session as session - INNER JOIN $tbl_session_course - ON session_rel_course.session_id = session.id - AND session_rel_course.c_id = '$courseId' - LIMIT 1"; + $sql = "SELECT s.session_admin_id, sru.user_id AS session_coach_id + FROM $tbl_session AS s + LEFT JOIN $tblSessionRelUser sru + ON (sru.session_id = s.id AND sru.relation_type = ".SessionEntity::SESSION_COACH.") + INNER JOIN $tbl_session_course src + ON (src.session_id = s.id AND src.c_id = $courseId)"; $result = Database::query($sql); $row = Database::store_result($result); + $sessionAdminsId = array_column($row, 'session_admin_id'); + $sessionCoachesId = array_column($row, 'session_coach_id'); - if ($row[0]['id_coach'] == $userid) { + if (in_array($userid, $sessionCoachesId)) { $is_courseMember = true; $is_courseAdmin = false; - } elseif ($row[0]['session_admin_id'] == $userid) { + } elseif (in_array($userid, $sessionAdminsId)) { $is_courseMember = false; $is_courseAdmin = false; } else { diff --git a/public/main/inc/lib/course.lib.php b/public/main/inc/lib/course.lib.php index b38150d39a..03cd9738a6 100644 --- a/public/main/inc/lib/course.lib.php +++ b/public/main/inc/lib/course.lib.php @@ -1194,8 +1194,10 @@ class CourseManager return true; } - $sql = 'SELECT 1 FROM '.Database::get_main_table(TABLE_MAIN_SESSION). - " WHERE id = $session_id AND id_coach = $userId"; + $sql = "SELECT s.id FROM ".Database::get_main_table(TABLE_MAIN_SESSION)." s + INNER JOIN ".Database::get_main_table(TABLE_MAIN_SESSION_USER)." sru + ON (sru.session_id = s.id AND sru.relation_type = ".SessionEntity::SESSION_COACH.") + WHERE sru.user_id = $userId AND s.id = $session_id"; if (Database::num_rows(Database::query($sql)) > 0) { return true; @@ -1804,12 +1806,9 @@ class CourseManager } if ($addGeneralCoach) { - $table = Database::get_main_table(TABLE_MAIN_SESSION); - // We get the session coach. - $sql = "SELECT id_coach FROM $table WHERE id = $session_id"; - $rs = Database::query($sql); - $session_id_coach = Database::result($rs, 0, 'id_coach'); - if (is_int($session_id_coach)) { + $generalCoachesId = SessionManager::getGeneralCoachesIdForSession($session_id); + + foreach ($generalCoachesId as $session_id_coach) { $userInfo = api_get_user_info($session_id_coach); if ($userInfo) { $users[$session_id_coach] = $userInfo; @@ -4054,11 +4053,10 @@ class CourseManager $session_category_id = null; $active = false; if (!empty($session_id)) { - $sessionCoachName = ''; - if (!empty($sessionInfo['id_coach'])) { - $coachInfo = api_get_user_info($sessionInfo['id_coach']); - $sessionCoachName = $coachInfo['complete_name']; - } + $sessionCoachName = implode( + ' - ', + SessionManager::getGeneralCoachesNamesForSession($session_id) + ); $session_category_id = self::get_session_category_id_by_session_id($course_info['id_session']); diff --git a/public/main/inc/lib/course_home.lib.php b/public/main/inc/lib/course_home.lib.php index 5db05ef828..4cc1364c1d 100644 --- a/public/main/inc/lib/course_home.lib.php +++ b/public/main/inc/lib/course_home.lib.php @@ -145,7 +145,10 @@ class CourseHome $session_category = $rows_session_category['name']; } - $coachInfo = api_get_user_info($sessionInfo['id_coach']); + $sessionCoachName = implode( + ' - ', + SessionManager::getGeneralCoachesNamesForSession($id_session) + ); $output = ''; if (!empty($session_category)) { @@ -156,7 +159,7 @@ class CourseHome $msgDate = $dateInfo['access']; $output .= ' '.get_lang('Session name').': '.''.$sessionInfo['name'].' - '.get_lang('General coach').': '.''.$coachInfo['complete_name'].''; + '.get_lang('General coach').': '.''.$sessionCoachName.''; $output .= ' '.get_lang('Identifier of session').': '. Display::return_icon('star.png', ' ', ['align' => 'absmiddle']).' diff --git a/public/main/inc/lib/display.lib.php b/public/main/inc/lib/display.lib.php index ac11c2ceb3..986d9296de 100644 --- a/public/main/inc/lib/display.lib.php +++ b/public/main/inc/lib/display.lib.php @@ -1413,19 +1413,18 @@ class Display public static function getSessionTitleBox($session_id) { $session_info = api_get_session_info($session_id); - $coachInfo = []; - if (!empty($session_info['id_coach'])) { - $coachInfo = api_get_user_info($session_info['id_coach']); - } + $generalCoachesNames = implode( + ' - ', + SessionManager::getGeneralCoachesNamesForSession($session_id) + ); $session = []; $session['category_id'] = $session_info['session_category_id']; $session['title'] = $session_info['name']; - $session['coach_id'] = $session['id_coach'] = $session_info['id_coach']; $session['dates'] = ''; $session['coach'] = ''; - if ('true' === api_get_setting('show_session_coach') && isset($coachInfo['complete_name'])) { - $session['coach'] = get_lang('General coach').': '.$coachInfo['complete_name']; + if ('true' === api_get_setting('show_session_coach') && $generalCoachesNames) { + $session['coach'] = get_lang('General coach').': '.$generalCoachesNames; } $active = false; if (('0000-00-00 00:00:00' === $session_info['access_end_date'] && @@ -1442,9 +1441,6 @@ class Display } else { $dates = SessionManager::parseSessionDates($session_info, true); $session['dates'] = $dates['access']; - if ('true' === api_get_setting('show_session_coach') && isset($coachInfo['complete_name'])) { - $session['coach'] = $coachInfo['complete_name']; - } //$active = $date_start <= $now && $date_end >= $now; } $session['active'] = $active; diff --git a/public/main/inc/lib/sessionmanager.lib.php b/public/main/inc/lib/sessionmanager.lib.php index c1394c81bb..d437e1793c 100644 --- a/public/main/inc/lib/sessionmanager.lib.php +++ b/public/main/inc/lib/sessionmanager.lib.php @@ -61,7 +61,6 @@ class SessionManager $result = [ 'id' => $session->getId(), - 'id_coach' => null, //$session->getGeneralCoach() ? $session->getGeneralCoach()->getId() : null, 'session_category_id' => $session->getCategory() ? $session->getCategory()->getId() : null, 'name' => $session->getName(), 'description' => $session->getDescription(), @@ -3168,12 +3167,9 @@ class SessionManager s.nbr_courses, s.access_start_date, s.access_end_date, - u.firstname, - u.lastname, sc.name as category_name, s.promotion_id FROM $session_table s - INNER JOIN $user_table u ON s.id_coach = u.id INNER JOIN $table_access_url_rel_session ar ON ar.session_id = s.id LEFT JOIN $session_category_table sc ON s.session_category_id = sc.id LEFT JOIN $session_course_table sco ON (sco.session_id = s.id) @@ -3284,13 +3280,10 @@ class SessionManager $sql = "SELECT s.id, s.name, - s.id_coach, - u.firstname, - u.lastname, s.session_category_id, c.name as category_name, s.description, - (SELECT COUNT(*) FROM $tbl_session_user WHERE session_id = s.id) as users, + s.nbr_users as users, (SELECT COUNT(*) FROM $tbl_lp WHERE session_id = s.id) as lessons "; if (false !== $field) { $fieldId = $field['id']; @@ -3299,9 +3292,7 @@ class SessionManager $sql .= " FROM $tbl_session s LEFT JOIN $tbl_session_category c ON s.session_category_id = c.id - INNER JOIN $tbl_users u - ON s.id_coach = u.id - ORDER BY 9 DESC + ORDER BY 8 DESC LIMIT 8"; $result = Database::query($sql); @@ -4225,7 +4216,7 @@ class SessionManager } /** - * The general coach (field: session.id_coach). + * The general coach (session_rel_user.relation_type = 3). * * @param int $user_id user id * @param bool $asPlatformAdmin The user is platform admin, return everything @@ -4234,34 +4225,28 @@ class SessionManager */ public static function get_sessions_by_general_coach($user_id, $asPlatformAdmin = false) { - $session_table = Database::get_main_table(TABLE_MAIN_SESSION); + $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION); $user_id = (int) $user_id; - // Session where we are general coach - $sql = "SELECT DISTINCT * - FROM $session_table"; + $innerJoin = ''; + $whereConditions = ''; if (!$asPlatformAdmin) { - $sql .= " WHERE id_coach = $user_id"; + $innerJoin = " INNER JOIN session_rel_user AS sru ON (s.id = sru.session_id) "; + $whereConditions = "sru.user_id = $user_id AND sru.relation_type = ".Session::SESSION_COACH; } if (api_is_multiple_url_enabled()) { - $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); + $tblSessionRelAccessUrl = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); $access_url_id = api_get_current_access_url_id(); - $sqlCoach = ''; - if (!$asPlatformAdmin) { - $sqlCoach = " id_coach = $user_id AND "; - } - if (-1 != $access_url_id) { - $sql = 'SELECT DISTINCT session.* - FROM '.$session_table.' session INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url - ON (session.id = session_rel_url.session_id) - WHERE '.$sqlCoach.' access_url_id = '.$access_url_id; + $innerJoin .= " INNER JOIN $tblSessionRelAccessUrl session_rel_url + ON (s.id = session_rel_url.session_id)"; + $whereConditions .= " AND session_rel_url.access_url_id = $access_url_id"; } } - $sql .= ' ORDER by name'; + $sql = "SELECT s.* FROM $sessionTable AS s $innerJoin WHERE $whereConditions ORDER BY s.name"; $result = Database::query($sql); return Database::store_result($result, 'ASSOC'); @@ -4458,6 +4443,8 @@ class SessionManager ]; }*/ + $generalCoaches = self::getGeneralCoachesIdForSession($id); + // Now try to create the session $sid = self::create_session( $s['name'].' '.get_lang('Copy'), @@ -4467,7 +4454,7 @@ class SessionManager $s['display_end_date'], $s['coach_access_start_date'], $s['coach_access_end_date'], - [(int) $s['id_coach']], + $generalCoaches, $s['session_category_id'], (int) $s['visibility'], true, @@ -5032,7 +5019,6 @@ class SessionManager $sessionParams = [ 'name' => $session_name, - 'id_coach' => $coach_id, 'access_start_date' => $dateStart, 'access_end_date' => $dateEnd, 'display_start_date' => $displayAccessStartDate, @@ -5048,15 +5034,27 @@ class SessionManager } // Creating the session. $session_id = Database::insert($tbl_session, $sessionParams); - if ($debug) { - if ($session_id) { - foreach ($enreg as $key => $value) { - if ('extra_' === substr($key, 0, 6)) { //an extra field - self::update_session_extra_field_value($session_id, substr($key, 6), $value); - } + if ($session_id) { + Database::insert( + $tbl_session_user, + [ + 'relation_type' => Session::SESSION_COACH, + 'duration' => 0, + 'registered_at' => api_get_utc_datetime(), + 'user_id' => $coach_id, + 'session_id' => $session_id, + ] + ); + foreach ($enreg as $key => $value) { + if ('extra_' === substr($key, 0, 6)) { //an extra field + self::update_session_extra_field_value($session_id, substr($key, 6), $value); } + } + if ($debug) { $logger->debug("Session created: #$session_id - $session_name"); - } else { + } + } else { + if ($debug) { $message = "Sessions - Session NOT created: $session_name"; $logger->debug($message); $report[] = $message; @@ -5090,7 +5088,6 @@ class SessionManager $sessionParams = [ 'name' => $session_name, - 'id_coach' => $coach_id, 'access_start_date' => $dateStart, 'access_end_date' => $dateEnd, 'display_start_date' => $displayAccessStartDate, @@ -5104,13 +5101,19 @@ class SessionManager if (!empty($extraParams)) { $sessionParams = array_merge($sessionParams, $extraParams); } - Database::insert($tbl_session, $sessionParams); - - // We get the last insert id. - $my_session_result = self::get_session_by_name($session_name); - $session_id = $my_session_result['id']; + $session_id = Database::insert($tbl_session, $sessionParams); if ($session_id) { + Database::insert( + $tbl_session_user, + [ + 'relation_type' => Session::SESSION_COACH, + 'duration' => 0, + 'registered_at' => api_get_utc_datetime(), + 'user_id' => $coach_id, + 'session_id' => $session_id, + ] + ); foreach ($enreg as $key => $value) { if ('extra_' == substr($key, 0, 6)) { //an extra field self::update_session_extra_field_value($session_id, substr($key, 6), $value); @@ -5148,7 +5151,6 @@ class SessionManager } else { // Updating the session. $params = [ - 'id_coach' => $coach_id, 'access_start_date' => $dateStart, 'access_end_date' => $dateEnd, 'display_start_date' => $displayAccessStartDate, @@ -5231,6 +5233,21 @@ class SessionManager } Database::update($tbl_session, $params, ['id = ?' => $session_id]); + Database::delete( + $tbl_session_user, + ['session_id = ? AND relation_type = ?' => [$session_id, Session::SESSION_COACH]] + ); + Database::insert( + $tbl_session_user, + [ + 'relation_type' => Session::SESSION_COACH, + 'duration' => 0, + 'registered_at' => api_get_utc_datetime(), + 'user_id' => $coach_id, + 'session_id' => $session_id, + ] + ); + foreach ($enreg as $key => $value) { if ('extra_' == substr($key, 0, 6)) { //an extra field self::update_session_extra_field_value($session_id, substr($key, 6), $value); @@ -9082,16 +9099,13 @@ class SessionManager public static function getCoursesForMainSessionCoach($userId) { $userId = (int) $userId; + $user = api_get_user_entity($userId); $listResCourseSession = []; - $tblSession = Database::get_main_table(TABLE_MAIN_SESSION); - // list of SESSION where user is session coach - $sql = "SELECT id FROM $tblSession - WHERE id_coach = ".$userId; - $res = Database::query($sql); + $sessions = $user->getSessionsAsGeneralCoach(); - while ($data = Database::fetch_assoc($res)) { - $sessionId = $data['id']; + foreach ($sessions as $session) { + $sessionId = $session->getId(); $listCoursesInSession = self::getCoursesInSession($sessionId); foreach ($listCoursesInSession as $i => $courseId) { if (api_get_session_visibility($sessionId)) { @@ -9829,4 +9843,20 @@ class SessionManager return -1; } } + + public static function getGeneralCoachesIdForSession(int $sessionId): array + { + return api_get_session_entity($sessionId) + ->getGeneralCoaches() + ->map(fn(User $user) => $user->getId()) + ->getValues(); + } + + public static function getGeneralCoachesNamesForSession(int $sessionId): array + { + return api_get_session_entity($sessionId) + ->getGeneralCoaches() + ->map(fn(User $user) => $user->getFullname()) + ->getValues(); + } } diff --git a/public/main/inc/lib/tracking.lib.php b/public/main/inc/lib/tracking.lib.php index 902dbaa948..645b0dbbad 100644 --- a/public/main/inc/lib/tracking.lib.php +++ b/public/main/inc/lib/tracking.lib.php @@ -3673,6 +3673,7 @@ class Tracking $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); + $tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER); // At first, courses where $coach_id is coach of the course. $sql = 'SELECT DISTINCT c.code @@ -3708,29 +3709,35 @@ class Tracking } // Then, courses where $coach_id is coach of the session - $sql = 'SELECT DISTINCT course.code - FROM '.$tbl_session_course.' as session_course - INNER JOIN '.$tbl_session.' as session - ON session.id = session_course.session_id - AND session.id_coach = '.$coach_id.' - INNER JOIN '.$tbl_course.' as course - ON course.id = session_course.c_id'; + $sql = "'SELECT DISTINCT course.code + FROM $tbl_session_course as session_course + INNER JOIN $tbl_session as session + ON (session.id = session_course.session_id) + INNER JOIN $tblSessionRelUser session_user + ON (session.id = session_user.session_id + AND session_user.user_id = $coach_id + AND session_user.relation_type = ".SessionEntity::SESSION_COACH.") + INNER JOIN $tbl_course as course + ON course.id = session_course.c_id'"; if (api_is_multiple_url_enabled()) { $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 (-1 != $access_url_id) { - $sql = 'SELECT DISTINCT c.code - FROM '.$tbl_session_course.' as session_course - INNER JOIN '.$tbl_course.' c + $sql = "'SELECT DISTINCT c.code + FROM $tbl_session_course as session_course + INNER JOIN $tbl_course c ON (c.id = session_course.c_id) - INNER JOIN '.$tbl_session.' as session + INNER JOIN $tbl_session as session ON session.id = session_course.session_id - AND session.id_coach = '.$coach_id.' - INNER JOIN '.$tbl_course.' as course + INNER JOIN $tblSessionRelUser session_user + ON (session.id = session_user.session_id + AND session_user.user_id = $coach_id + AND session_user.relation_type = ".SessionEntity::SESSION_COACH.") + INNER JOIN $tbl_course as course ON course.id = session_course.c_id - INNER JOIN '.$tbl_course_rel_access_url.' course_rel_url - ON (course_rel_url.c_id = c.id)'; + INNER JOIN $tbl_course_rel_access_url course_rel_url + ON (course_rel_url.c_id = c.id)'"; } } @@ -8136,13 +8143,14 @@ class TrackingCourseLog $recorset = Database::query($sql); } } else { - $sql = "SELECT session.id, session.name, user.username - FROM $table_tool tool, $table_session session, $table_user user + $sql = "SELECT session.id s.id, s.name u.username + FROM c_tool t, session s, user u, session_rel_user sru WHERE - tool.c_id = $course_id AND - tool.session_id = session.id AND - session.id_coach = user.user_id AND - tool.$id = $ref"; + t.c_id = $course_id AND + t.session_id = s.id AND + sru.session_id = s.id AND + sru.user_id = u.id AND + t.$id = $ref"; $recorset = Database::query($sql); } diff --git a/public/main/inc/lib/usermanager.lib.php b/public/main/inc/lib/usermanager.lib.php index d8c855a67a..329f1ebc21 100644 --- a/public/main/inc/lib/usermanager.lib.php +++ b/public/main/inc/lib/usermanager.lib.php @@ -692,10 +692,11 @@ class UserManager $sql = "DELETE FROM $table_session_course_user WHERE user_id = '".$user_id."'"; Database::query($sql); - // If the user was added as a id_coach then set the current admin as coach see BT# + // If the user was added as a general coach then set the current admin as general coach see BT# $currentUserId = api_get_user_id(); - $sql = "UPDATE $table_session SET id_coach = $currentUserId - WHERE id_coach = '".$user_id."'"; + $sql = "UPDATE session_rel_user + SET user_id = $currentUserId + WHERE user_id = $user_id AND relation_type = ". SessionEntity::SESSION_COACH; Database::query($sql); $sql = "UPDATE $table_session SET id_coach = $currentUserId @@ -2871,13 +2872,14 @@ class UserManager } $sql = "SELECT DISTINCT - id, name, access_start_date, access_end_date + s.id, s.name, s.access_start_date, s.access_end_date FROM $tbl_session s + INNER JOIN $tbl_session_user sru ON sru.session_id = s.id WHERE ( - id_coach = $user_id + sru.user_id = $user_id AND sru.relation_type = ".SessionEntity::SESSION_COACH." ) $coachCourseConditions - ORDER BY access_start_date, access_end_date, name"; + ORDER BY s.access_start_date, s.access_end_date, s.name"; $result = Database::query($sql); if (Database::num_rows($result) > 0) { @@ -3108,8 +3110,9 @@ class UserManager } } else { //check if user is general coach for this session - $sessionInfo = api_get_session_info($session_id); - if ($sessionInfo['id_coach'] == $user_id) { + $session = api_get_session_entity($session_id); + $user = api_get_user_entity($user_id); + if ($session && $session->hasUserAsGeneralCoach($user)) { $courseList = SessionManager::get_course_list_by_session_id($session_id); if (!empty($courseList)) { foreach ($courseList as $course) { diff --git a/public/main/inc/lib/userportal.lib.php b/public/main/inc/lib/userportal.lib.php index cffa57e257..e065da9cfd 100644 --- a/public/main/inc/lib/userportal.lib.php +++ b/public/main/inc/lib/userportal.lib.php @@ -1330,7 +1330,6 @@ class IndexManager 'id' => $session_id, ]; $session_box = Display::getSessionTitleBox($session_id); - $coachId = $session_box['id_coach']; $imageField = $extraFieldValue->get_values_by_handler_and_field_variable( $session_id, 'image' @@ -1338,14 +1337,7 @@ class IndexManager $params['category_id'] = $session_box['category_id']; $params['title'] = $session_box['title']; - $params['id_coach'] = $coachId; - $params['coach_url'] = api_get_path(WEB_AJAX_PATH). - 'user_manager.ajax.php?a=get_user_popup&user_id='.$coachId; $params['coach_name'] = !empty($session_box['coach']) ? $session_box['coach'] : null; - $params['coach_avatar'] = UserManager::getUserPicture( - $coachId, - USER_IMAGE_SIZE_SMALL - ); $params['date'] = $session_box['dates']; $params['image'] = isset($imageField['value']) ? $imageField['value'] : null; $params['duration'] = isset($session_box['duration']) ? ' '.$session_box['duration'] : null; diff --git a/public/main/mySpace/ti_report.php b/public/main/mySpace/ti_report.php index 25fbdabc6a..930b869805 100644 --- a/public/main/mySpace/ti_report.php +++ b/public/main/mySpace/ti_report.php @@ -2,6 +2,8 @@ /* For licensing terms, see /license.txt */ +use Chamilo\CoreBundle\Entity\Session; + $cidReset = true; require_once __DIR__.'/../inc/global.inc.php'; @@ -77,15 +79,16 @@ if ($form->validate()) { $second = DateTime::createFromFormat('Y-m-d', $endDate); $numberOfWeeks = floor($first->diff($second)->days / 7); - $sql = " SELECT id_coach, name, id as session_id, display_start_date, display_end_date - FROM session - WHERE display_start_date BETWEEN '$startDate' AND '$endDate' - ORDER BY id_coach"; + $sql = "SELECT sru.user_id, s.name, s.id as session_id, s.display_start_date, s.display_end_date + FROM session + LEFT JOIN session_rel_user sru ON (sru.session_id = s.id AND sru.relation_type = ". Session::SESSION_COACH.") + WHERE s.display_start_date BETWEEN '$startDate' AND '$endDate' + ORDER BY sru.user_id"; $result = Database::query($sql); $coachList = []; while ($row = Database::fetch_array($result, 'ASSOC')) { - $coachId = $row['id_coach']; + $coachId = $row['user_id']; if (!isset($coachList[$coachId])) { $userInfo = api_get_user_info($coachId); $coachList[$coachId]['complete_name'] = $userInfo['complete_name']; diff --git a/public/main/mySpace/user_import.php b/public/main/mySpace/user_import.php index 6e43facdc1..f726325a3e 100644 --- a/public/main/mySpace/user_import.php +++ b/public/main/mySpace/user_import.php @@ -30,10 +30,9 @@ if ('true' === api_get_setting('add_users_by_coach')) { if (!api_is_platform_admin()) { if (isset($_REQUEST['id_session'])) { $id_session = intval($_REQUEST['id_session']); - $sql = 'SELECT id_coach FROM '.Database::get_main_table(TABLE_MAIN_SESSION).' - WHERE id='.$id_session; - $rs = Database::query($sql); - if (Database::result($rs, 0, 0) != api_get_user_id()) { + $session = api_get_session_entity($id_session); + $user = api_get_user_entity(); + if (!$session->hasUserAsGeneralCoach($user)) { api_not_allowed(true); } } else { diff --git a/public/main/session/session_export.php b/public/main/session/session_export.php index 899123de79..744ea096c6 100644 --- a/public/main/session/session_export.php +++ b/public/main/session/session_export.php @@ -40,38 +40,43 @@ if (isset($_POST['formSent'])) { if (empty($session_id)) { $sql = "SELECT s.id, - name, - id_coach, - username, - access_start_date, - access_end_date, - visibility, - session_category_id + s.name, + u.username, + s.access_start_date, + s.access_end_date, + s.visibility, + s.session_category_id FROM $tblSession s - INNER JOIN $tblUser - ON $tblUser.user_id = s.id_coach - ORDER BY id"; + INNER JOIN $tblSessionRelUser sru + ON (s.id = sru.session_id AND sru.relation_type = ".Session::SESSION_COACH.") + INNER JOIN $tblUser u + ON u.id = sru.user_id + ORDER BY s.id"; if (api_is_multiple_url_enabled()) { $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); $access_url_id = api_get_current_access_url_id(); if (-1 != $access_url_id) { - $sql = "SELECT s.id, name,id_coach,username,access_start_date,access_end_date,visibility,session_category_id + $sql = "SELECT s.id, s.name,u.username,s.access_start_date,s.access_end_date,s.visibility,s.session_category_id FROM $tblSession s INNER JOIN $tbl_session_rel_access_url as session_rel_url ON (s.id= session_rel_url.session_id) - INNER JOIN $tblUser u ON (u.user_id = s.id_coach) - WHERE access_url_id = $access_url_id - ORDER BY id"; + INNER JOIN $tblSessionRelUser sru + ON (s.id = sru.session_id AND sru.relation_type = ".Session::SESSION_COACH.") + INNER JOIN $tblUser u ON (u.id = sru.user_id) + WHERE session_rel_url.access_url_id = $access_url_id + ORDER BY s.id"; } } $result = Database::query($sql); } else { - $sql = "SELECT s.id,name,username,access_start_date,access_end_date,visibility,session_category_id + $sql = "SELECT s.id,s.name,u.username,s.access_start_date,s.access_end_date,s.visibility,s.session_category_id FROM $tblSession s - INNER JOIN $tblUser - ON $tblUser.user_id = s.id_coach + INNER JOIN $tblSessionRelUser sru + ON (s.id = sru.session_id AND sru.relation_type = ".Session::SESSION_COACH.") + INNER JOIN $tblUser u + ON u.id = sru.user_id WHERE s.id='$session_id'"; $result = Database::query($sql); } diff --git a/public/main/session/session_import.php b/public/main/session/session_import.php index 2b5149e43c..d38be2fa7f 100644 --- a/public/main/session/session_import.php +++ b/public/main/session/session_import.php @@ -259,7 +259,6 @@ if (isset($_POST['formSent']) && $_POST['formSent']) { // Creating the session. $sql_session = "INSERT IGNORE INTO $tbl_session SET name = '".Database::escape_string($session_name)."', - id_coach = '$coach_id', access_start_date = '$date_start', access_end_date = '$date_end', visibility = '$visibility', @@ -267,6 +266,16 @@ if (isset($_POST['formSent']) && $_POST['formSent']) { session_admin_id=".intval($_user['user_id']); $rs_session = Database::query($sql_session); $session_id = Database::insert_id(); + Database::insert( + $tbl_session_user, + [ + 'relation_type' => Session::SESSION_COACH, + 'duration' => 0, + 'registered_at' => api_get_utc_datetime(), + 'user_id' => $coach_id, + 'session_id' => $session_id, + ] + ); $session_counter++; } else { // Update the session if it is needed. @@ -275,7 +284,6 @@ if (isset($_POST['formSent']) && $_POST['formSent']) { // Creating the session. $sql_session = "INSERT IGNORE INTO $tbl_session SET name = '".Database::escape_string($session_name)."', - id_coach = '$coach_id', access_start_date = '$date_start', access_end_date = '$date_end', visibility = '$visibility', @@ -283,11 +291,20 @@ if (isset($_POST['formSent']) && $_POST['formSent']) { session_admin_id=".intval($_user['user_id']); $rs_session = Database::query($sql_session); $session_id = Database::insert_id(); + Database::insert( + $tbl_session_user, + [ + 'relation_type' => Session::SESSION_COACH, + 'duration' => 0, + 'registered_at' => api_get_utc_datetime(), + 'user_id' => $coach_id, + 'session_id' => $session_id, + ] + ); $session_counter++; } else { // if the session already exists - update it. $sql_session = "UPDATE $tbl_session SET - id_coach = '$coach_id', access_start_date = '$date_start', access_end_date = '$date_end', visibility = '$visibility', @@ -299,6 +316,16 @@ if (isset($_POST['formSent']) && $_POST['formSent']) { Database::query("DELETE FROM $tbl_session_user WHERE session_id ='$session_id'"); Database::query("DELETE FROM $tbl_session_course WHERE session_id='$session_id'"); Database::query("DELETE FROM $tbl_session_course_user WHERE session_id='$session_id'"); + Database::insert( + $tbl_session_user, + [ + 'relation_type' => Session::SESSION_COACH, + 'duration' => 0, + 'registered_at' => api_get_utc_datetime(), + 'user_id' => $coach_id, + 'session_id' => $session_id, + ] + ); } }