Passed
Push — master ( 3001d0...50d443 )
by Angel Fernando Quiroz
08:01 queued 14s
created

IsAllowedToEditHelper::canCreateCourse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 14
rs 10
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\ServiceHelper;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\Session;
11
use Chamilo\CoreBundle\Entity\User;
12
use Chamilo\CoreBundle\Settings\SettingsManager;
13
use ExtraFieldValue;
14
use Symfony\Bundle\SecurityBundle\Security;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
17
readonly class IsAllowedToEditHelper
18
{
19
    public function __construct(
20
        private SettingsManager $settingsManager,
21
        private Security $security,
22
        private RequestStack $requestStack,
23
        private CidReqHelper $cidReqHelper,
24
    ) {}
25
26
    public function check(
27
        bool $tutor = false,
28
        bool $coach = false,
29
        bool $sessionCoach = false,
30
        bool $checkStudentView = true,
31
        ?Course $course = null,
32
        ?Session $session = null,
33
    ): bool {
34
        /** @var User $user */
35
        $user = $this->security->getUser();
36
37
        $studentViewIsActive = 'studentview' === $this->requestStack->getSession()->get('studentview');
38
39
        $isSessionAdminAllowedToEdit = 'true' === $this->settingsManager->getSetting('session.session_admins_edit_courses_content');
40
41
        if ($user->isAdmin() || ($user->isSessionAdmin() && $isSessionAdminAllowedToEdit)) {
42
            if ($checkStudentView && $studentViewIsActive) {
43
                return false;
44
            }
45
46
            return true;
47
        }
48
49
        $session = $session ?: $this->cidReqHelper->getSessionEntity();
50
        $course = $course ?: $this->cidReqHelper->getCourseEntity();
51
52
        if ($session && $course && 'true' === $this->settingsManager->getSetting('session.session_courses_read_only_mode')) {
53
            $lockExrafieldField = (new ExtraFieldValue('course'))
54
                ->get_values_by_handler_and_field_variable(
55
                    $course->getId(),
56
                    'session_courses_read_only_mode'
57
                )
58
            ;
59
60
            if (!empty($lockExrafieldField['value'])) {
61
                return false;
62
            }
63
        }
64
65
        $isCoachAllowedToEdit = $session?->hasCoach($user) && !$studentViewIsActive;
66
        $sessionVisibility = $session?->setAccessVisibilityByUser($user);
67
        $isCourseAdmin = $user->hasRole('ROLE_CURRENT_COURSE_TEACHER') || $user->hasRole('ROLE_CURRENT_COURSE_SESSION_TEACHER');
68
69
        if (!$isCourseAdmin && $tutor) {
70
            $isCourseAdmin = $user->isCourseTutor($course, $session);
71
        }
72
73
        if (!$isCourseAdmin && $coach) {
74
            if (Session::READ_ONLY === $sessionVisibility) {
75
                $isCoachAllowedToEdit = false;
76
            }
77
78
            if ('true' === $this->settingsManager->getSetting('session.allow_coach_to_edit_course_session')) {
79
                $isCourseAdmin = $isCoachAllowedToEdit;
80
            }
81
        }
82
83
        if (!$isCourseAdmin && $sessionCoach) {
84
            $isCourseAdmin = $isCoachAllowedToEdit;
85
        }
86
87
        if ('true' !== $this->settingsManager->getSetting('course.student_view_enabled')) {
88
            return $isCourseAdmin;
89
        }
90
91
        if ($session) {
92
            if (Session::READ_ONLY === $sessionVisibility) {
93
                $isCoachAllowedToEdit = false;
94
            }
95
96
            $isAllowed = 'true' === $this->settingsManager->getSetting('session.allow_coach_to_edit_course_session') && $isCoachAllowedToEdit;
97
98
            if ($checkStudentView) {
99
                $isAllowed = $isAllowed && !$studentViewIsActive;
100
            }
101
        } elseif ($checkStudentView) {
102
            $isAllowed = $isCourseAdmin && !$studentViewIsActive;
103
        } else {
104
            $isAllowed = $isCourseAdmin;
105
        }
106
107
        return $isAllowed;
108
    }
109
110
    /**
111
     * Checks whether current user is allowed to create courses.
112
     */
113
    public function canCreateCourse(): bool
114
    {
115
        /** @var User $user */
116
        $user = $this->security->getUser();
117
118
        if ($user->isAdmin()) {
119
            return true;
120
        }
121
122
        if ($user->isTeacher()) {
123
            return 'true' === $this->settingsManager->getSetting('course.allow_users_to_create_courses');
124
        }
125
126
        return $this->requestStack->getSession()->get('is_allowedCreateCourse');
127
    }
128
}
129