Issues (2128)

main/admin/gradebook_dependency.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\GradebookCategory;
5
6
require_once __DIR__.'/../inc/global.inc.php';
7
8
api_protect_admin_script();
9
10
$allow = api_get_configuration_value('gradebook_dependency');
11
if (false == $allow) {
12
    api_not_allowed(true);
13
}
14
15
$categoryId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 1;
16
17
$em = Database::getManager();
18
$repo = $em->getRepository('ChamiloCoreBundle:GradebookCategory');
19
/** @var GradebookCategory $category */
20
$category = $repo->find($categoryId);
21
if (!$category) {
22
    api_not_allowed(true);
23
}
24
25
$categoryObj = Category::load($categoryId);
26
/** @var Category $categoryObj */
27
$categoryObj = $categoryObj[0];
28
$dependencies = $categoryObj->getCourseListDependency();
29
30
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
31
32
$currentUrl = api_get_self().'?';
33
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
34
35
$interbreadcrumb[] = [
36
    'url' => api_get_path(WEB_CODE_PATH).'admin/gradebook_list.php',
37
    'name' => get_lang('Gradebook'),
38
];
39
40
$tpl = new Template(get_lang('CourseList'));
41
$toolbar = Display::url(
42
    Display::return_icon('back.png', get_lang('Add'), [], ICON_SIZE_MEDIUM),
43
    api_get_path(WEB_CODE_PATH).'admin/gradebook_list.php'
44
);
45
46
if (empty($dependencies)) {
47
    Display::addFlash(
48
        Display::return_message(get_lang('ThisGradebookDoesntHaveDependencies'))
49
    );
50
}
51
52
$content = '';
53
$courseList = [];
54
$mandatoryList = api_get_configuration_value('gradebook_dependency_mandatory_courses');
55
$mandatoryList = isset($mandatoryList['courses']) ? $mandatoryList['courses'] : [];
56
$mandatoryListCompleteList = [];
57
foreach ($mandatoryList as $courseMandatoryId) {
58
    $mandatoryListCompleteList[] = api_get_course_info_by_id($courseMandatoryId);
59
}
60
$totalDependencies = count($dependencies);
61
$min = $categoryObj->getMinimumToValidate();
62
$gradeBooksToValidateInDependence = $categoryObj->getGradeBooksToValidateInDependence();
63
$userResult = [];
64
65
$dependencyList = [];
66
foreach ($dependencies as $courseId) {
67
    $dependencyList[$courseId] = api_get_course_info_by_id($courseId);
68
}
69
$courseUserLoaded = [];
70
71
foreach ($dependencyList as $courseId => $courseInfo) {
0 ignored issues
show
Comprehensibility Bug introduced by
$courseId is overwriting a variable from outer foreach loop.
Loading history...
72
    $courseCode = $courseInfo['code'];
73
    $subCategory = Category::load(null, null, $courseCode);
74
    /** @var Category $subCategory */
75
    $subCategory = $subCategory ? $subCategory[0] : [];
76
    if (empty($subCategory)) {
77
        continue;
78
    }
79
80
    $userList = CourseManager::get_student_list_from_course_code($courseCode);
81
    foreach ($userList as $user) {
82
        $userId = $user['user_id'];
83
        $userInfo = api_get_user_info($userId);
84
        $courseId = $courseInfo['real_id'];
85
86
        $userCourseList = CourseManager::get_courses_list_by_user_id(
87
            $userId,
88
            false,
89
            false,
90
            true,
91
            [],
92
            false
93
        );
94
        $userCourseListCode = array_column($userCourseList, 'code');
95
96
        if (!isset($userResult[$userId]['result_mandatory_20'])) {
97
            $userResult[$userId]['result_mandatory_20'] = 0;
98
        }
99
        if (!isset($userResult[$userId]['result_not_mandatory_80'])) {
100
            $userResult[$userId]['result_not_mandatory_80'] = 0;
101
        }
102
103
        foreach ($userCourseList as $courseItem) {
104
            $myCourseCode = $courseItem['code'];
105
            $myCourseId = $courseItem['real_id'];
106
            if (in_array($myCourseId, $dependencies)) {
107
                continue;
108
            }
109
110
            if (isset($courseUserLoaded[$userId][$myCourseId])) {
111
                continue;
112
            } else {
113
                $courseUserLoaded[$userId][$myCourseId] = true;
114
            }
115
116
            $courseCategory = Category::load(
117
                null,
118
                null,
119
                $myCourseCode
120
            );
121
            $courseCategory = isset($courseCategory[0]) ? $courseCategory[0] : [];
122
            $userResult[$userId]['result_out_dependencies'][$myCourseCode] = false;
123
            if (!empty($courseCategory)) {
124
                $result = Category::userFinishedCourse(
125
                    $userId,
126
                    $courseCategory,
127
                    true
128
                );
129
                $userResult[$userId]['result_out_dependencies'][$myCourseCode] = $result;
130
131
                if (in_array($myCourseId, $mandatoryList)) {
132
                    if ($userResult[$userId]['result_mandatory_20'] < 20 && $result) {
133
                        $userResult[$userId]['result_mandatory_20'] += 10;
134
                    }
135
                } else {
136
                    if ($userResult[$userId]['result_not_mandatory_80'] < 80 && $result) {
137
                        $userResult[$userId]['result_not_mandatory_80'] += 10;
138
                        //  var_dump($userResult[$userId]['result_80'] );
139
                    }
140
                }
141
            }
142
        }
143
144
        $result = Category::userFinishedCourse(
145
            $userId,
146
            $subCategory,
147
            true
148
        );
149
150
        $userResult[$userId]['result_dependencies'][$courseCode] = $result;
151
        $userResult[$userId]['user_info'] = $userInfo;
152
153
        if (in_array($courseId, $mandatoryList)) {
154
            if ($userResult[$userId]['result_mandatory_20'] < 20 && $result) {
155
                $userResult[$userId]['result_mandatory_20'] += 10;
156
            }
157
        } else {
158
            if ($userResult[$userId]['result_not_mandatory_80'] < 80 && $result) {
159
                $userResult[$userId]['result_not_mandatory_80'] += 10;
160
            }
161
        }
162
    }
163
    $courseList[] = $courseInfo;
164
}
165
166
foreach ($userResult as $userId => &$userData) {
167
    $courseListPassedOutDependency = count(array_filter($userData['result_out_dependencies']));
168
    $courseListPassedDependency = count(array_filter($userData['result_dependencies']));
169
    $total = $courseListPassedDependency + $courseListPassedOutDependency;
170
    $userData['course_list_passed_out_dependency'] = $courseListPassedOutDependency;
171
    $userData['course_list_passed_out_dependency_count'] = count($userData['result_out_dependencies']);
172
    // Min req must apply + mandatory should be 20
173
    //$userData['final_result'] = $total >= $min && $userData['result_mandatory_20'] == 20;
174
    //$userData['final_result'] = $total >= $min && $courseListPassedDependency == $totalDependencies;
175
    $userData['final_result'] = $total >= $min && $courseListPassedDependency >= $gradeBooksToValidateInDependence;
176
}
177
178
$tpl->assign('current_url', $currentUrl);
179
$tpl->assign(
180
    'actions',
181
    Display::toolbarAction(
182
        'toolbar',
183
        [$toolbar],
184
        [1, 4]
185
    )
186
);
187
188
$tpl->assign('mandatory_courses', $mandatoryListCompleteList);
189
$tpl->assign('min_to_validate', $min);
190
$tpl->assign('gradebook_category', $category);
191
$tpl->assign('courses', $courseList);
192
$tpl->assign('users', $userResult);
193
$layout = $tpl->get_template('admin/gradebook_dependency.tpl');
194
$tpl->display($layout);
195