Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal: Add session replication, reinscription logic, and child session hierarchy for course expiration - refs BT#22057 #5831

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
ba4e25b
Internal: Add session replication, reinscription logic, and child ses…
christianbeeznest Sep 30, 2024
c3312db
Merge remote-tracking branch 'upstream/master' into matra-22057
christianbeeznest Oct 9, 2024
7f6e7ae
Session: Add settings and interface fields for session and lesson fun…
christianbeeznest Oct 9, 2024
3f353ef
Resolved merge conflicts with upstream/master
christianbeeznest Oct 9, 2024
2bf9adf
Internal: Add missing settings in migration - refs BT#22057
christianbeeznest Nov 26, 2024
d507e64
Resolved merge conflicts with upstream/master
christianbeeznest Nov 27, 2024
66a227d
Internal: Fix error type for validity_id_days field
christianbeeznest Nov 27, 2024
42f207b
Merge remote-tracking branch 'upstream/master' into matra-22057
christianbeeznest Nov 27, 2024
353c19a
Internal: Fix session duplication and reinscription logic improvement…
christianbeeznest Nov 27, 2024
238cbdb
Merge remote-tracking branch 'upstream/master' into matra-22057
christianbeeznest Nov 28, 2024
e9cca0b
Internal: Improve session reinscription logic using direct CLpView va…
christianbeeznest Nov 28, 2024
9cbcca1
Merge remote-tracking branch 'upstream/master' into matra-22057
christianbeeznest Nov 29, 2024
ba09140
Internal: Fix session duplication and reinscription logic - refs BT#2…
christianbeeznest Nov 29, 2024
8f8fa4f
Adding changes from upstream/master
christianbeeznest Dec 4, 2024
7b66599
Internal: Implement automatic session repetition and coach notificati…
christianbeeznest Dec 4, 2024
ad82e0b
Merge remote-tracking branch 'upstream/master' into matra-22057
christianbeeznest Dec 18, 2024
3b34dc1
Internal: Reinscription logic updated to use sessions' validity and g…
christianbeeznest Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions public/main/inc/ajax/model.ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ function getWhereClause($col, $oper, $val)
$count = ExerciseLib::get_count_exam_results(
$exerciseId,
$whereCondition,
'',
$courseId,
false,
true,
$status
Expand Down Expand Up @@ -839,6 +839,7 @@ function getWhereClause($col, $oper, $val)
['where' => $whereCondition, 'extra' => $extra_fields]
);
break;
case 'replication':
case 'custom':
case 'simple':
$count = SessionManager::getSessionsForAdmin(
Expand Down Expand Up @@ -1981,7 +1982,7 @@ function getWhereClause($col, $oper, $val)
break;
case 'custom':
case 'simple':
case 'all':
case 'replication':
$result = SessionManager::getSessionsForAdmin(
api_get_user_id(),
[
Expand All @@ -2000,6 +2001,7 @@ function getWhereClause($col, $oper, $val)
break;
case 'active':
case 'close':
case 'all':
$result = SessionManager::formatSessionsAdminForGrid(
[
'where' => $whereCondition,
Expand Down
213 changes: 172 additions & 41 deletions public/main/inc/lib/sessionmanager.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ public static function create_session(
$sendSubscriptionNotification = false,
$accessUrlId = 0,
$status = 0,
$notifyBoss = false
$notifyBoss = false,
$parentId = null,
$daysBeforeFinishingForReinscription = null,
$lastRepetition = false,
$daysBeforeFinishingToCreateNewRepetition = null,
$validityInDays = null
) {
global $_configuration;

Expand Down Expand Up @@ -188,42 +193,30 @@ public static function create_session(
$endDate = Database::escape_string($endDate);

if (empty($name)) {
$msg = get_lang('A title is required for the session');

return $msg;
return get_lang('A title is required for the session');
} elseif (!empty($startDate) && !api_is_valid_date($startDate, 'Y-m-d H:i') &&
!api_is_valid_date($startDate, 'Y-m-d H:i:s')
) {
$msg = get_lang('Invalid start date was given.');

return $msg;
return get_lang('Invalid start date was given.');
} elseif (!empty($endDate) && !api_is_valid_date($endDate, 'Y-m-d H:i') &&
!api_is_valid_date($endDate, 'Y-m-d H:i:s')
) {
$msg = get_lang('Invalid end date was given.');

return $msg;
return get_lang('Invalid end date was given.');
} elseif (!empty($startDate) && !empty($endDate) && $startDate >= $endDate) {
$msg = get_lang('The first date should be before the end date');

return $msg;
return get_lang('The first date should be before the end date');
} else {
$ready_to_create = false;
if ($fixSessionNameIfExists) {
$name = self::generateNextSessionName($name);
if ($name) {
$ready_to_create = true;
} else {
$msg = get_lang('Session title already exists');

return $msg;
return get_lang('Session title already exists');
}
} else {
$rs = Database::query("SELECT 1 FROM $tbl_session WHERE title='".$name."'");
if (Database::num_rows($rs)) {
$msg = get_lang('Session title already exists');

return $msg;
return get_lang('Session title already exists');
}
$ready_to_create = true;
}
Expand All @@ -239,7 +232,11 @@ public static function create_session(
->setShowDescription(1 === $showDescription)
->setSendSubscriptionNotification((bool) $sendSubscriptionNotification)
->setNotifyBoss((bool) $notifyBoss)
;
->setParentId($parentId)
->setDaysToReinscription((int) $daysBeforeFinishingForReinscription)
->setLastRepetition($lastRepetition)
->setDaysToNewRepetition((int) $daysBeforeFinishingToCreateNewRepetition)
->setValidityInDays((int) $validityInDays);

foreach ($coachesId as $coachId) {
$session->addGeneralCoach(api_get_user_entity($coachId));
Expand Down Expand Up @@ -288,18 +285,6 @@ public static function create_session(
$extraFields['item_id'] = $session_id;
$sessionFieldValue = new ExtraFieldValue('session');
$sessionFieldValue->saveFieldValues($extraFields);
/*
Sends a message to the user_id = 1

$user_info = api_get_user_info(1);
$complete_name = $user_info['firstname'].' '.$user_info['lastname'];
$subject = api_get_setting('siteName').' - '.get_lang('A new session has been created');
$message = get_lang('A new session has been created')." <br /> ".get_lang('Session name').' : '.$name;
api_mail_html($complete_name, $user_info['email'], $subject, $message);
*
*/
// Adding to the correct URL
//UrlManager::add_session_to_url($session_id, $accessUrlId);

// add event to system log
$user_id = api_get_user_id();
Expand Down Expand Up @@ -523,6 +508,10 @@ public static function getSessionsForAdmin(
}
$select .= ', status';

if ('replication' === $listType) {
$select .= ', parent_id';
}

if (isset($options['order'])) {
$isMakingOrder = 0 === strpos($options['order'], 'category_name');
}
Expand Down Expand Up @@ -654,6 +643,11 @@ public static function getSessionsForAdmin(
)
)";
break;
case 'replication':
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CASE statements must be defined using a colon

$formatted = false;
$query .= "AND s.days_to_new_repetition IS NOT NULL
AND (SELECT COUNT(id) FROM session AS child WHERE child.parent_id = s.id) <= 1";
break;
}

$query .= $order;
Expand All @@ -668,6 +662,23 @@ public static function getSessionsForAdmin(
$session['users'] = Database::fetch_assoc($result)['nbr'];
}
}

if ('replication' === $listType) {
$formattedSessions = [];
foreach ($sessions as $session) {
$formattedSessions[] = $session;
if (isset($session['id'])) {
$childSessions = array_filter($sessions, fn($s) => isset($s['parent_id']) && $s['parent_id'] === $session['id']);
foreach ($childSessions as $childSession) {
$childSession['title'] = '-- ' . $childSession['title'];
$formattedSessions[] = $childSession;
}
}
}

return $formattedSessions;
}

if ('all' === $listType) {
if ($getCount) {
return $sessions[0]['total_rows'];
Expand Down Expand Up @@ -1793,7 +1804,12 @@ public static function edit_session(
$sessionAdminId = 0,
$sendSubscriptionNotification = false,
$status = 0,
$notifyBoss = 0
$notifyBoss = 0,
$parentId = 0,
$daysBeforeFinishingForReinscription = null,
$daysBeforeFinishingToCreateNewRepetition = null,
$lastRepetition = false,
$validityInDays = null
) {
$id = (int) $id;
$status = (int) $status;
Expand Down Expand Up @@ -1863,6 +1879,11 @@ public static function edit_session(
->setVisibility($visibility)
->setSendSubscriptionNotification((bool) $sendSubscriptionNotification)
->setNotifyBoss((bool) $notifyBoss)
->setParentId($parentId)
->setDaysToReinscription((int) $daysBeforeFinishingForReinscription)
->setLastRepetition($lastRepetition)
->setDaysToNewRepetition((int) $daysBeforeFinishingToCreateNewRepetition)
->setValidityInDays((int) $validityInDays)
->setAccessStartDate(null)
->setAccessStartDate(null)
->setDisplayStartDate(null)
Expand All @@ -1871,6 +1892,16 @@ public static function edit_session(
->setCoachAccessEndDate(null)
;

if ($parentId) {
$sessionEntity->setParentId($parentId);
} else {
$sessionEntity->setParentId(null);
}

$sessionEntity->setDaysToReinscription($daysBeforeFinishingForReinscription);
$sessionEntity->setLastRepetition($lastRepetition);
$sessionEntity->setDaysToNewRepetition($daysBeforeFinishingToCreateNewRepetition);

$newGeneralCoaches = array_map(
fn($coachId) => api_get_user_entity($coachId),
$coachesId
Expand Down Expand Up @@ -8271,6 +8302,85 @@ public static function setForm(FormValidator $form, Session $session = null, $fr
$extra_field = new ExtraFieldModel('session');
$extra = $extra_field->addElements($form, $session ? $session->getId() : 0, ['image']);

if ('true' === api_get_setting('session.enable_auto_reinscription')) {
$form->addElement(
'text',
'days_before_finishing_for_reinscription',
get_lang('Days before finishing for reinscription'),
['maxlength' => 5]
);
$form->addRule(
'days_before_finishing_for_reinscription',
get_lang('Days must be a positive number or empty'),
'regex',
'/^\d*$/'
);
}

if ('true' === api_get_setting('session.enable_session_replication')) {
$form->addElement(
'text',
'days_before_finishing_to_create_new_repetition',
get_lang('Days before finishing to create new repetition'),
['maxlength' => 5]
);
$form->addRule(
'days_before_finishing_to_create_new_repetition',
get_lang('Days must be a positive number or empty'),
'regex',
'/^\d*$/'
);
}

if ('true' === api_get_setting('session.enable_auto_reinscription') || 'true' === api_get_setting('session.enable_session_replication')) {
$form->addElement(
'checkbox',
'last_repetition',
get_lang('Last repetition')
);

$form->addElement(
'number',
'validity_in_days',
get_lang('Validity in days'),
[
'min' => 0,
'max' => 365,
'step' => 1,
'placeholder' => get_lang('Enter the number of days'),
]
);

$form->addRule(
'validity_in_days',
get_lang('The field must be a positive number'),
'numeric',
null,
'client'
);
}

/** @var HTML_QuickForm_select $element */
$element = $form->createElement(
'select',
'parent_id',
get_lang('Parent session'),
[],
['class' => 'form-control']
);

$element->addOption(get_lang('None'), 0, []);
$sessions = SessionManager::getListOfParentSessions();
$currentSessionId = $session?->getId();
foreach ($sessions as $id => $title) {
if ($id !== $currentSessionId) {
$attributes = [];
$element->addOption($title, $id, $attributes);
}
}

$form->addElement($element);

$form->addElement('html', '</div>');

$js = $extra['jquery_ready_content'];
Expand Down Expand Up @@ -8791,7 +8901,7 @@ public static function getGridColumns(
];

break;

case 'replication':
case 'custom':
$columns = [
'#',
Expand All @@ -8810,6 +8920,7 @@ public static function getGridColumns(
[
'name' => 'title',
'index' => 's.title',
'width' => '260px',
'width' => '300',
'align' => 'left',
'search' => 'true',
Expand Down Expand Up @@ -9805,9 +9916,9 @@ public static function getDefaultSessionTab()
}

/**
* @return array
* @return string
*/
public static function getSessionListTabs($listType)
public static function getSessionListTabs($listType): string
{
$tabs = [
[
Expand All @@ -9826,10 +9937,10 @@ public static function getSessionListTabs($listType)
'content' => get_lang('Custom list'),
'url' => api_get_path(WEB_CODE_PATH).'session/session_list.php?list_type=custom',
],
/*[
'content' => get_lang('Complete'),
'url' => api_get_path(WEB_CODE_PATH).'session/session_list_simple.php?list_type=complete',
],*/
[
'content' => get_lang('Replication'),
'url' => api_get_path(WEB_CODE_PATH).'session/session_list.php?list_type=replication',
],
];
$default = null;
switch ($listType) {
Expand All @@ -9845,6 +9956,9 @@ public static function getSessionListTabs($listType)
case 'custom':
$default = 4;
break;
case 'replication':
$default = 5;
break;
}

return Display::tabsOnlyLink($tabs, $default);
Expand Down Expand Up @@ -10227,6 +10341,24 @@ public static function getAllUserIdsInSession(int $sessionId): array
return $users;
}

/**
* Retrieves a list of parent sessions.
*/
public static function getListOfParentSessions(): array
{
$sessions = [];
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$sql = "SELECT id, title FROM $tbl_session WHERE parent_id IS NULL ORDER BY title";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable "tbl_session" is not in valid camel caps format

$result = Database::query($sql);

while ($row = Database::fetch_array($result)) {
$sessions[$row['id']] = $row['title'];
}

return $sessions;
}


/**
* Method to export sessions data as CSV
*/
Expand Down Expand Up @@ -10413,5 +10545,4 @@ private static function generateSessionCourseReportData($sessionId, $courseId, $

return [$csvHeaders, $csvContent];
}

}
Loading
Loading