Passed
Pull Request — 1.11.x (#3859)
by Angel Fernando Quiroz
09:26
created

StudentFollowPage::getLpSubscription()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
c 0
b 0
f 0
nc 4
nop 4
dl 0
loc 41
rs 9.552
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
                api_get_user_entity($studentId),
29
                api_get_course_entity($courseId),
30
                api_get_session_entity($sessionId)
31
            );
32
33
            if (null === $itemProperty) {
34
                return '-';
35
            }
36
37
            return "{$itemProperty->getInsertUser()->getCompleteName()}<br>"
38
                .Display::tag(
39
                    'small',
40
                    api_convert_and_format_date($itemProperty->getInsertDate(), DATE_TIME_FORMAT_LONG)
41
                );
42
        }
43
44
        $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

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