Passed
Push — 1.11.x ( b90206...a9dc20 )
by Angel Fernando Quiroz
11:15
created

StudentFollowPage::getLpVisibleField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 15
rs 9.9666
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CourseBundle\Entity\CItemProperty;
6
7
/**
8
 * Class StudentFollowPage.
9
 */
10
class StudentFollowPage
11
{
12
    const VARIABLE_ACQUISITION = 'acquisition';
13
    const VARIABLE_INVISIBLE = 'invisible';
14
15
    public static function getLpSubscription(
16
        array $lpInfo,
17
        int $studentId,
18
        int $courseId,
19
        int $sessionId = 0
20
    ): string {
21
        $em = Database::getManager();
22
23
        if ($lpInfo['subscribe_users']) {
24
            $itemRepo = $em->getRepository(CItemProperty::class);
25
            $itemProperty = $itemRepo->findByUserSuscribedToItem(
26
                'learnpath',
27
                $lpInfo['iid'],
28
                $studentId,
29
                $courseId,
30
                $sessionId
31
            );
32
33
            if (null === $itemProperty) {
34
                $userGroups = GroupManager::getAllGroupPerUserSubscription($studentId, $courseId);
35
36
                foreach ($userGroups as $groupInfo) {
37
                    $itemProperty = $itemRepo->findByGroupSuscribedToLp(
38
                        'learnpath',
39
                        $lpInfo['iid'],
40
                        $groupInfo['iid'],
41
                        $courseId,
42
                        $sessionId
43
                    );
44
45
                    if (null !== $itemProperty) {
46
                        break;
47
                    }
48
                }
49
            }
50
51
            if (null === $itemProperty) {
52
                return '-';
53
            }
54
55
            return "{$itemProperty->getInsertUser()->getCompleteName()}<br>"
56
                .Display::tag(
57
                    'small',
58
                    api_convert_and_format_date($itemProperty->getInsertDate(), DATE_TIME_FORMAT_LONG)
59
                );
60
        }
61
62
        $subscriptionEvent = Event::findUserSubscriptionToCourse($studentId, $courseId, $sessionId);
0 ignored issues
show
Bug introduced by
The method findUserSubscriptionToCourse() does not exist on Event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        /** @scrutinizer ignore-call */ 
63
        $subscriptionEvent = Event::findUserSubscriptionToCourse($studentId, $courseId, $sessionId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
64
        if (empty($subscriptionEvent)) {
65
            return '-';
66
        }
67
68
        $creator = api_get_user_entity($subscriptionEvent['default_user_id']);
69
70
        return "{$creator->getCompleteName()}<br>"
71
            .Display::tag(
72
                'small',
73
                api_convert_and_format_date($subscriptionEvent['default_date'], DATE_TIME_FORMAT_LONG)
74
            );
75
    }
76
77
    public static function getLpAcquisition(
78
        array $lpInfo,
79
        int $studentId,
80
        int $courseId,
81
        int $sessionId = 0,
82
        bool $allowEdit = false
83
    ): string {
84
        $lpView = learnpath::findLastView($lpInfo['iid'], $studentId, $courseId, $sessionId);
85
86
        if (empty($lpView)) {
87
            return '-';
88
        }
89
90
        $extraField = new ExtraField('lp_view');
91
        $field = $extraField->get_handler_field_info_by_field_variable(self::VARIABLE_ACQUISITION);
92
93
        $extraFieldValue = new ExtraFieldValue('lp_view');
94
        $value = $extraFieldValue->get_values_by_handler_and_field_variable($lpView['iid'], self::VARIABLE_ACQUISITION);
95
96
        $return = '';
97
98
        if (empty($value)) {
99
            $return .= '-';
100
        } else {
101
            $optionSelected = array_filter(
102
                $field['options'],
103
                function (array $option) use ($value) {
104
                    return $option['option_value'] == $value['value'];
105
                }
106
            );
107
108
            if (empty($optionSelected)) {
109
                $return .= '-';
110
            } else {
111
                $optionSelected = current($optionSelected);
112
                $valueComment = json_decode($value['comment'], true);
113
114
                $register = api_get_user_entity($valueComment['user']);
115
116
                $return .= $optionSelected['display_text'].'<br>'
117
                    .Display::tag('small', $register->getCompleteName()).'<br>'
118
                    .Display::tag(
119
                        'small',
120
                        api_convert_and_format_date($valueComment['datetime'], DATE_TIME_FORMAT_LONG)
121
                    ).'<br>';
122
            }
123
        }
124
125
        $editUrl = api_get_path(WEB_AJAX_PATH).'student_follow_page.ajax.php?'
126
            .http_build_query(['lp_view' => $lpView['iid'], 'a' => 'form_adquisition']);
127
128
        if ($allowEdit) {
129
            $return .= Display::url(
130
                Display::return_icon('edit.png', get_lang('Edit'), [], ICON_SIZE_TINY),
131
                $editUrl,
132
                ['class' => 'ajax', 'data-title' => $lpInfo['lp_name']]
133
            );
134
        }
135
136
        return '<div id="acquisition-'.$lpView['iid'].'">'.$return.'</div>';
137
    }
138
139
    public static function getLpVisibleScript()
140
    {
141
        $url = api_get_path(WEB_AJAX_PATH).'student_follow_page.ajax.php?'.http_build_query(['a' => 'views_invisible']);
142
143
        return "<script>$(function () {
144
            var chkbView = $('[name=\"chkb_view[]\"]');
145
146
            function doRequest(element, state) {
147
                element.prop('disabled', true);
148
149
                var views = $.makeArray(element).map(function (input) { return input.value; });
150
151
                return $.post('$url', { 'chkb_view[]': views, 'state': state }, function () { element.prop('disabled', false); });
152
            }
153
154
            $('[name=\"chkb_category[]\"]').on('change', function () {
155
                var checked = this.checked;
156
                var chkbs = $(this).parents('table').find('td :checkbox').each(function () { this.checked = checked; });
157
158
                doRequest(chkbs, checked);
159
            }).prop('checked', true);
160
161
            chkbView.on('change', function () {
162
                doRequest($(this), this.checked);
163
164
                $(this).parents('table').find('th :checkbox').prop(
165
                    'checked',
166
                    $.makeArray($(this).parents('table').find('td :checkbox'))
167
                        .map(function (input) { return input.checked; })
168
                        .reduce(function (acc, cur) { return acc && cur; })
169
                );
170
            }).each(function () {
171
                if (!this.checked) {
172
                    $(this).parents('table').find('th :checkbox').prop('checked', false);
173
                }
174
            });
175
        });</script>";
176
    }
177
178
    public static function getLpVisibleField(array $lpInfo, int $studentId, int $courseId, int $sessionId = 0)
179
    {
180
        $attrs = [];
181
182
        $isVisible = self::isViewVisible($lpInfo['iid'], $studentId, $courseId, $sessionId);
183
184
        if (!$isVisible) {
185
            $attrs['checked'] = 'checked';
186
        }
187
188
        return Display::input(
189
            'checkbox',
190
            'chkb_view[]',
191
            implode('_', [$lpInfo['iid'], $studentId, $courseId, $sessionId]),
192
            $attrs
193
        );
194
    }
195
196
    public static function isViewVisible(int $lpId, int $studentId, int $courseId, int $sessionId): bool
197
    {
198
        $lpView = learnpath::findLastView($lpId, $studentId, $courseId, $sessionId);
199
200
        if (empty($lpView)) {
201
            return true;
202
        }
203
204
        $extraFieldValue = new ExtraFieldValue('lp_view');
205
        $value = $extraFieldValue->get_values_by_handler_and_field_variable($lpView['iid'], self::VARIABLE_INVISIBLE);
206
207
        return empty($value) || empty($value['value']);
208
    }
209
}
210