Skip to content

Commit

Permalink
Merge pull request #5574 from christianbeeznest/ofaj-21759-2
Browse files Browse the repository at this point in the history
Internal: Add restrictions to terms validation process - refs BT#21759
  • Loading branch information
christianbeeznest authored Jun 5, 2024
2 parents ce623d6 + 024342a commit 011e3af
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 24 deletions.
10 changes: 10 additions & 0 deletions assets/vue/services/socialService.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ export default {
}
},

async checkTermsRestrictions(userId) {
try {

return await baseService.get(`${API_URL}/terms-restrictions/${userId}`);
} catch (error) {
console.error("Error checking terms restrictions:", error)
throw error
}
},

async fetchInvitations(userId) {
try {
const response = await axios.get(`${API_URL}/invitations/${userId}`)
Expand Down
67 changes: 43 additions & 24 deletions assets/vue/views/terms/Terms.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
<template>
<div class="terms">
<div v-for="(item, index) in term" :key="index">
<h3>{{ item.title }}</h3>
<div v-html="item.content"></div>
</div>
<p class="small mt-4 mb-4">{{ term.date_text }}</p>
<div v-if="!accepted">
<BaseButton
:label="$t('Accept Terms and Conditions')"
type="primary"
icon="pi pi-check"
@click="acceptTerms"
/>
</div>
<div v-else>
<p>{{ t('You accepted these terms on') }} {{ acceptanceDate }}</p>
<BaseButton
:label="$t('Revoke Acceptance')"
type="danger"
icon="pi pi-times"
@click="revokeAcceptance"
/>
<div v-if="!isLoading">
<div v-for="(item, index) in term" :key="index">
<h3>{{ item.title }}</h3>
<div v-html="item.content"></div>
</div>
<p class="small mt-4 mb-4">{{ term.date_text }}</p>
<div v-if="!accepted && !blockButton">
<BaseButton
:label="$t('Accept Terms and Conditions')"
type="primary"
icon="pi pi-check"
@click="acceptTerms"
/>
</div>
<div v-else-if="accepted">
<p>{{ t('You accepted these terms on') }} {{ acceptanceDate }}</p>
<BaseButton
:label="$t('Revoke Acceptance')"
type="danger"
icon="pi pi-times"
@click="revokeAcceptance"
/>
</div>
<div v-if="blockButton" class="alert alert-warning" v-html="infoMessage"></div>
</div>
</div>
</template>
Expand All @@ -37,6 +40,9 @@ const { t } = useI18n()
const term = ref({})
const accepted = ref(false)
const acceptanceDate = ref(null)
const blockButton = ref(false)
const infoMessage = ref('')
const isLoading = ref(true)
const securityStore = useSecurityStore()
const fetchTerms = async () => {
Expand All @@ -59,6 +65,17 @@ const checkAcceptance = async () => {
}
}
const checkRestrictions = async () => {
try {
const userId = securityStore.user.id
const response = await socialService.checkTermsRestrictions(userId)
blockButton.value = response.blockButton
infoMessage.value = response.infoMessage
} catch (error) {
console.error('Error checking restrictions:', error)
}
}
const acceptTerms = async () => {
try {
const userId = securityStore.user.id
Expand All @@ -81,8 +98,10 @@ const revokeAcceptance = async () => {
}
}
onMounted(() => {
fetchTerms()
checkAcceptance()
onMounted(async () => {
await fetchTerms()
await checkAcceptance()
await checkRestrictions()
isLoading.value = false
})
</script>
48 changes: 48 additions & 0 deletions src/CoreBundle/Controller/SocialController.php
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,54 @@ public function uploadGroupPicture(
return new JsonResponse(['success' => 'Group and image saved successfully'], Response::HTTP_OK);
}

#[Route('/terms-restrictions/{userId}', name: 'chamilo_core_social_terms_restrictions')]
public function checkTermsRestrictions(
int $userId,
UserRepository $userRepo,
ExtraFieldRepository $extraFieldRepository,
TranslatorInterface $translator,
SettingsManager $settingsManager
): JsonResponse {

/** @var User $user */
$user = $userRepo->find($userId);

if (!$user) {
return $this->json(['error' => 'User not found'], Response::HTTP_NOT_FOUND);
}

$isAdmin = $user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_SUPER_ADMIN');

$termActivated = false;
$blockButton = false;
$infoMessage = '';

if (!$isAdmin) {
if ('true' === $settingsManager->getSetting('ticket.show_terms_if_profile_completed')) {
$extraFieldValue = new ExtraFieldValue('user');
$value = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'termactivated');
if (isset($value['value'])) {
$termActivated = !empty($value['value']) && 1 === (int) $value['value'];
}

if (false === $termActivated) {
$blockButton = true;
$infoMessage .= $translator->trans('The terms and conditions have not yet been validated by your tutor.').'&nbsp;';
}

if (!$user->isProfileCompleted()) {
$blockButton = true;
$infoMessage .= $translator->trans('You must first fill your profile to enable the terms and conditions validation.');
}
}
}

return $this->json([
'blockButton' => $blockButton,
'infoMessage' => $infoMessage,
]);
}

/**
* Formats a hierarchical structure of messages for display.
*
Expand Down

0 comments on commit 011e3af

Please sign in to comment.