Skip to content

Commit 13f92fe

Browse files
authored
Merge pull request #6604 from christianbeeznest/ofaj-22849
User: Add T&C gate (show_terms_if_profile_completed + termactivated) and recompute profile_completed - refs BT#22849
2 parents cb1098c + 5c2fc7d commit 13f92fe

File tree

3 files changed

+123
-51
lines changed

3 files changed

+123
-51
lines changed

public/main/auth/tc.php

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,63 @@
88

99
$return = $_POST['return'] ?? $_GET['return'] ?? '/home';
1010

11-
if ($_SERVER['REQUEST_METHOD'] === 'POST'
11+
$canAccept = true;
12+
$infoMessage = '';
13+
14+
$userId = 0;
15+
$termData = Session::read('term_and_condition');
16+
if (!empty($termData['user_id'])) {
17+
$userId = (int) $termData['user_id'];
18+
} else {
19+
$userId = api_get_user_id();
20+
}
21+
22+
$isPlatformAdmin = api_is_platform_admin();
23+
if ('true' === api_get_setting('registration.allow_terms_conditions') && !$isPlatformAdmin) {
24+
if ('true' === api_get_setting('ticket.show_terms_if_profile_completed')) {
25+
if (!empty($userId)) {
26+
$userInfo = api_get_user_info($userId);
27+
if ($userInfo && ANONYMOUS != $userInfo['status']) {
28+
$extraFieldValue = new ExtraFieldValue('user');
29+
$value = $extraFieldValue->get_values_by_handler_and_field_variable(
30+
$userId,
31+
'termactivated'
32+
);
33+
34+
$termActivated = false;
35+
if (isset($value['value'])) {
36+
$termActivated = !empty($value['value']) && 1 === (int) $value['value'];
37+
}
38+
39+
if (false === $termActivated) {
40+
$canAccept = false;
41+
$infoMessage = Display::return_message(
42+
get_lang('The terms and conditions have not yet been validated by your tutor'),
43+
'warning',
44+
false
45+
);
46+
}
47+
48+
if ($canAccept && 1 !== (int) $userInfo['profile_completed']) {
49+
$canAccept = false;
50+
$infoMessage .= Display::return_message(
51+
get_lang('You must first fill your profile to enable the terms and conditions validation.'),
52+
'warning',
53+
true
54+
);
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
if (
62+
$_SERVER['REQUEST_METHOD'] === 'POST'
1263
&& !empty($_POST['legal_accept_type'])
13-
&& (
14-
isset($_POST['legal_accept'])
15-
|| api_get_setting('registration.hide_legal_accept_checkbox') === 'true'
16-
)
64+
&& (isset($_POST['legal_accept']) || api_get_setting('registration.hide_legal_accept_checkbox') === 'true')
1765
) {
18-
$userId = 0;
19-
$termData = Session::read('term_and_condition');
20-
if (!empty($termData['user_id'])) {
21-
$userId = (int)$termData['user_id'];
22-
} else {
23-
$userId = api_get_user_id();
66+
if (!$canAccept) {
67+
ChamiloHelper::displayLegalTermsPage($return, false, $infoMessage);
2468
}
2569

2670
if ($userId > 0) {
@@ -53,4 +97,4 @@
5397
}
5498
}
5599

56-
ChamiloHelper::displayLegalTermsPage($return);
100+
ChamiloHelper::displayLegalTermsPage($return, $canAccept, $infoMessage);

public/main/inc/lib/legal.lib.php

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,10 @@ public static function sendLegal(int $userId, int $coachId)
385385
{
386386
$subject = get_lang('Your terms and conditions are ready to be signed');
387387
$studentDetails = api_get_user_info($userId);
388-
$coachDetails = api_get_user_info($coachId);
389-
$link = trim(
390-
api_get_setting('course_validation_terms_and_conditions_url')
391-
);
392-
$completeLink = '<a href="'.$link.'">'.$link.'</a>';
393-
// Note: Translated string has 3 replacement markers, not just one as the original string suggests.
388+
$coachDetails = api_get_user_info($coachId);
389+
$link = trim(api_get_setting('course_validation_terms_and_conditions_url'));
390+
$completeLink = '<a href="'.$link.'">'.$link.'</a>';
391+
394392
$content = sprintf(
395393
get_lang('Hello,<br />Your tutor sent you your terms and conditions. You can sign it following this URL: %s'),
396394
$studentDetails['firstname'],
@@ -402,17 +400,42 @@ public static function sendLegal(int $userId, int $coachId)
402400

403401
$extraFieldValue = new ExtraFieldValue('user');
404402
$value = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'termactivated');
405-
if (false === $value || $value['value'] != 1) {
403+
if ($value === false || (int)($value['value'] ?? 0) !== 1) {
406404
$extraFieldInfo = $extraFieldValue->getExtraField()->get_handler_field_info_by_field_variable('termactivated');
407405
if ($extraFieldInfo) {
408-
$newParams = [
409-
'item_id' => $userId,
410-
'field_id' => $extraFieldInfo['id'],
406+
$extraFieldValue->save([
407+
'item_id' => $userId,
408+
'field_id' => $extraFieldInfo['id'],
411409
'field_value' => 1,
412-
'comment' => '',
413-
];
414-
$extraFieldValue->save($newParams);
410+
'comment' => '',
411+
]);
412+
}
413+
}
414+
415+
$enforceProfileCompleted =
416+
api_get_setting('ticket.show_terms_if_profile_completed') === 'true'
417+
|| api_get_setting('registration.show_terms_if_profile_completed') === 'true';
418+
419+
if ($enforceProfileCompleted) {
420+
$allValues = $extraFieldValue->getAllValuesByItem($userId);
421+
$justTermResults = [];
422+
foreach ($allValues as $row) {
423+
$var = $row['field_variable'] ?? $row['variable'] ?? null;
424+
if ($var !== null && str_starts_with($var, 'terms_')) {
425+
$val = $row['value'] ?? null;
426+
$justTermResults[$var] = !empty($val) && $val !== '0';
427+
}
428+
}
429+
430+
$profileCompleted = 1;
431+
if (in_array(false, $justTermResults, true)) {
432+
$profileCompleted = 0;
415433
}
434+
435+
$table = Database::get_main_table(TABLE_MAIN_USER);
436+
Database::query("UPDATE $table SET profile_completed = ".$profileCompleted." WHERE id = ".$userId);
437+
438+
ChamiloSession::write('profile_completed_result', $justTermResults);
416439
}
417440
}
418441

src/CoreBundle/Helpers/ChamiloHelper.php

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,8 @@ public static function saveUserTermsAcceptance(int $userId, string $legalAcceptT
599599

600600
/**
601601
* Displays the Terms and Conditions page.
602-
*
603-
* @param string $returnUrl The URL to redirect back to after acceptance
604602
*/
605-
public static function displayLegalTermsPage(string $returnUrl = '/home'): void
603+
public static function displayLegalTermsPage(string $returnUrl = '/home', bool $canAccept = true, string $infoMessage = ''): void
606604
{
607605
$iso = api_get_language_isocode();
608606
$langId = api_get_language_id($iso);
@@ -612,56 +610,63 @@ public static function displayLegalTermsPage(string $returnUrl = '/home'): void
612610
// No T&C for current language → show a message
613611
Display::display_header(get_lang('Terms and Conditions'));
614612
echo '<div class="max-w-3xl mx-auto text-gray-90 text-lg text-center">'
615-
.get_lang('No terms and conditions available for this language.')
616-
.'</div>';
613+
. get_lang('No terms and conditions available for this language.')
614+
. '</div>';
617615
Display::display_footer();
618-
619616
exit;
620617
}
621618

622619
Display::display_header(get_lang('Terms and Conditions'));
623620

624621
if (!empty($term['content'])) {
625622
echo '<div class="max-w-3xl mx-auto bg-white shadow p-8 rounded">';
626-
echo '<h1 class="text-2xl font-bold text-primary mb-6">'.get_lang('Terms and Conditions').'</h1>';
627-
echo '<div class="prose prose-sm max-w-none mb-6">'.$term['content'].'</div>';
623+
echo '<h1 class="text-2xl font-bold text-primary mb-6">' . get_lang('Terms and Conditions') . '</h1>';
624+
625+
if (!empty($infoMessage)) {
626+
echo '<div class="mb-4">'.$infoMessage.'</div>';
627+
}
628+
629+
echo '<div class="prose prose-sm max-w-none mb-6">' . $term['content'] . '</div>';
628630

629631
$extra = new ExtraFieldValue('terms_and_condition');
630632
foreach ($extra->getAllValuesByItem($term['id']) as $field) {
631633
if (!empty($field['field_value'])) {
632634
echo '<div class="mb-4">';
633-
echo '<h3 class="text-lg font-semibold text-primary">'.$field['display_text'].'</h3>';
634-
echo '<p class="text-gray-90 mt-1">'.$field['field_value'].'</p>';
635+
echo '<h3 class="text-lg font-semibold text-primary">' . $field['display_text'] . '</h3>';
636+
echo '<p class="text-gray-90 mt-1">' . $field['field_value'] . '</p>';
635637
echo '</div>';
636638
}
637639
}
638640

639-
$hide = 'true' === api_get_setting('registration.hide_legal_accept_checkbox');
640-
641-
echo '<form method="post" action="tc.php?return='.urlencode($returnUrl).'" class="space-y-6">';
642-
echo '<input type="hidden" name="legal_accept_type" value="'.$term['version'].':'.$term['language_id'].'">';
643-
echo '<input type="hidden" name="return" value="'.htmlspecialchars($returnUrl).'">';
641+
echo '<form method="post" action="tc.php?return=' . urlencode($returnUrl) . '" class="space-y-6">';
642+
echo '<input type="hidden" name="legal_accept_type" value="' . $term['version'] . ':' . $term['language_id'] . '">';
643+
echo '<input type="hidden" name="return" value="' . htmlspecialchars($returnUrl) . '">';
644+
645+
if ($canAccept) {
646+
$hide = 'true' === api_get_setting('registration.hide_legal_accept_checkbox');
647+
if ($hide) {
648+
echo '<input type="hidden" name="legal_accept" value="1">';
649+
} else {
650+
echo '<label class="flex items-start space-x-2">';
651+
echo '<input type="checkbox" name="legal_accept" value="1" required class="rounded border-gray-300 text-primary focus:ring-primary">';
652+
echo '<span class="text-gray-90 text-sm">' . get_lang('I have read and agree to the') . ' ';
653+
echo '<a href="tc.php?preview=1" target="_blank" class="text-primary hover:underline">' . get_lang('Terms and Conditions') . '</a>';
654+
echo '</span>';
655+
echo '</label>';
656+
}
644657

645-
if ($hide) {
646-
echo '<input type="hidden" name="legal_accept" value="1">';
658+
echo '<div><button type="submit" class="inline-block bg-primary text-white font-semibold px-6 py-3 rounded hover:opacity-90 transition">' . get_lang('Accept Terms and Conditions') . '</button></div>';
647659
} else {
648-
echo '<label class="flex items-start space-x-2">';
649-
echo '<input type="checkbox" name="legal_accept" value="1" required class="rounded border-gray-300 text-primary focus:ring-primary">';
650-
echo '<span class="text-gray-90 text-sm">'.get_lang('I have read and agree to the').' ';
651-
echo '<a href="tc.php?preview=1" target="_blank" class="text-primary hover:underline">'.get_lang('Terms and Conditions').'</a>';
652-
echo '</span>';
653-
echo '</label>';
660+
echo '<div><button type="button" class="inline-block bg-gray-400 text-white font-semibold px-6 py-3 rounded cursor-not-allowed" disabled>' . get_lang('Accept Terms and Conditions') . '</button></div>';
654661
}
655662

656-
echo '<div><button type="submit" class="inline-block bg-primary text-white font-semibold px-6 py-3 rounded hover:opacity-90 transition">'.get_lang('Accept Terms and Conditions').'</button></div>';
657663
echo '</form>';
658664
echo '</div>';
659665
} else {
660-
echo '<div class="text-center text-gray-90 text-lg">'.get_lang('Coming soon...').'</div>';
666+
echo '<div class="text-center text-gray-90 text-lg">' . get_lang('Coming soon...') . '</div>';
661667
}
662668

663669
Display::display_footer();
664-
665670
exit;
666671
}
667672
}

0 commit comments

Comments
 (0)