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(); |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|