Passed
Pull Request — master (#6229)
by
unknown
11:01 queued 02:20
created

SurveyLink::needs_max()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Framework\Container;
6
use Chamilo\CourseBundle\Entity\CSurvey;
7
8
/**
9
 * Gradebook link to a survey item.
10
 *
11
 * @author Ivan Tcholakov <[email protected]>, 2010
12
 */
13
class SurveyLink extends AbstractLink
14
{
15
    /** @var CSurvey */
16
    private $survey_data;
17
18
    /**
19
     * Constructor.
20
     */
21
    public function __construct()
22
    {
23
        parent::__construct();
24
        $this->set_type(LINK_SURVEY);
25
    }
26
27
    public function get_name(): string
28
    {
29
        $survey = $this->get_survey_data();
30
31
        if (!$survey instanceof CSurvey) {
32
            return get_lang('Untitled Survey');
33
        }
34
35
        return $survey->getCode() . ': ' . self::html_to_text($survey->getTitle());
36
    }
37
38
    public function get_description(): string
39
    {
40
        $survey = $this->get_survey_data();
41
42
        if (!$survey instanceof CSurvey) {
43
            return '';
44
        }
45
46
        return $survey->getSubtitle();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $survey->getSubtitle() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
47
    }
48
49
    public function get_type_name(): string
50
    {
51
        return get_lang('Survey');
52
    }
53
54
    public function is_allowed_to_change_name(): bool
55
    {
56
        return false;
57
    }
58
59
    public function needs_name_and_description(): bool
60
    {
61
        return false;
62
    }
63
64
    public function needs_max(): bool
65
    {
66
        return false;
67
    }
68
69
    public function needs_results(): bool
70
    {
71
        return false;
72
    }
73
74
    /**
75
     * Generates an array of all surveys available.
76
     *
77
     * @return array 2-dimensional array - every element contains 2 subelements (id, name)
78
     */
79
    public function get_all_links(): array
80
    {
81
        if (empty($this->course_id)) {
82
            return [];
83
        }
84
85
        $session = api_get_session_entity($this->get_session_id());
86
        $course = api_get_course_entity($this->getCourseId());
87
        $repo = Container::getSurveyRepository();
88
89
        $qb = $repo->getResourcesByCourse($course, $session);
90
        $surveys = $qb->getQuery()->getResult();
91
        $links = [];
92
        /** @var CSurvey $survey */
93
        foreach ($surveys as $survey) {
94
            $links[] = [
95
                $survey->getIid(),
96
                api_trunc_str(
97
                    $survey->getCode() . ': ' . self::html_to_text($survey->getTitle()),
98
                    80
99
                ),
100
            ];
101
        }
102
103
        return $links;
104
    }
105
106
    /**
107
     * Has anyone done this survey yet?
108
     * Implementation of the AbstractLink class, mainly used dynamically in gradebook/lib/fe.
109
     */
110
    public function has_results(): bool
111
    {
112
        $survey = $this->get_survey_data();
113
        if (!$survey) {
114
            return false;
115
        }
116
117
        $repo = Container::getSurveyInvitationRepository();
118
        $course = api_get_course_entity($this->course_id);
119
        $session = api_get_session_entity($this->get_session_id());
120
121
        $results = $repo->getAnsweredInvitations($survey, $course, $session);
122
123
        return count($results) > 0;
124
    }
125
126
    /**
127
     * Calculate score for a student (to show in the gradebook).
128
     *
129
     * @param int    $studentId
130
     * @param string $type      Type of result we want (best|average|ranking)
131
     *
132
     * @return array|null
133
     */
134
    public function calc_score($studentId = null, $type = null): ?array
135
    {
136
        $survey = $this->get_survey_data();
137
        if (!$survey) {
138
            return [null, null];
139
        }
140
141
        $course = api_get_course_entity($this->course_id);
142
        $session = api_get_session_entity($this->get_session_id());
143
        $repo = Container::getSurveyInvitationRepository();
144
        $max_score = 1;
145
146
        if ($studentId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $studentId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
147
            $user = api_get_user_entity($studentId);
148
            $answered = $repo->hasUserAnswered($survey, $course, $user, $session);
149
150
            return [$answered ? $max_score : 0, $max_score];
151
        }
152
153
        $results = $repo->getAnsweredInvitations($survey, $course, $session);
154
        $rescount = count($results);
155
156
        if ($rescount === 0) {
157
            return [null, null];
158
        }
159
160
        switch ($type) {
161
            case 'best':
162
            case 'average':
163
            default:
164
                return [$rescount, $rescount];
165
        }
166
    }
167
168
    /**
169
     * Check if this still links to a survey.
170
     */
171
    public function is_valid_link(): bool
172
    {
173
        return null !== $this->get_survey_data();
174
    }
175
176
    public function get_link(): ?string
177
    {
178
        if ('true' === api_get_setting('survey.hide_survey_reporting_button')) {
179
            return null;
180
        }
181
182
        if (api_is_allowed_to_edit()) {
183
            $survey = $this->get_survey_data();
184
            $sessionId = $this->get_session_id();
185
186
            if ($survey) {
187
                return api_get_path(WEB_CODE_PATH) . 'survey/reporting.php?' .
188
                    api_get_cidreq_params($this->getCourseId(), $sessionId) .
189
                    '&survey_id=' . $survey->getIid();
190
            }
191
        }
192
193
        return null;
194
    }
195
196
    /**
197
     * Get the name of the icon for this tool.
198
     */
199
    public function get_icon_name(): string
200
    {
201
        return 'survey';
202
    }
203
204
    /**
205
     * Get the survey data from the c_survey table with the current object id.
206
     */
207
    private function get_survey_data(): ?CSurvey
208
    {
209
        if (empty($this->survey_data)) {
210
            $repo = Container::getSurveyRepository();
211
            $survey = $repo->find($this->get_ref_id());
212
213
            if (!$survey instanceof CSurvey) {
214
                return null;
215
            }
216
217
            $this->survey_data = $survey;
218
        }
219
220
        return $this->survey_data;
221
    }
222
223
    /**
224
     * @param string $string
225
     */
226
    private static function html_to_text($string): string
227
    {
228
        return strip_tags($string);
229
    }
230
}
231