ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.RunManager.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26
35{
36 protected \ilLanguage $lng;
37 protected \ILIAS\Survey\Code\CodeManager $code_manager;
40 protected int $survey_id;
43 protected \ilObjSurvey $survey;
44 protected int $current_user_id;
45 protected int $appraisee_id;
46
47 public function __construct(
48 InternalRepoService $repo_service,
52 int $appraisee_id = 0
53 ) {
54 $this->repo = $repo_service->execution()->run();
55
56 $this->survey_id = $survey->getSurveyId();
57 $this->survey = $survey;
58 $this->feature_config = $domain_service->modeFeatureConfig($survey->getMode());
59 $this->domain_service = $domain_service;
60 $this->current_user_id = $current_user_id;
61 $this->appraisee_id = $appraisee_id;
62 $this->session_repo = $repo_service->execution()->runSession();
63 $this->code_manager = $domain_service->code($survey, $current_user_id);
64 $this->lng = $domain_service->lng();
65 }
66
67 protected function codeNeeded(): bool
68 {
69 return !$this->survey->isAccessibleWithoutCode();
70 }
71
72 public function getCurrentRunId(): int
73 {
77 $code = $this->getCode();
78
79 $this->checkUserParameters($user_id, $code);
80
81 // code needed, no code given -> no run
82 if ($code === "" && $this->codeNeeded()) {
83 return 0;
84 }
85
86 $run_id = $repo->getCurrentRunId($survey_id, $user_id, $code, $this->appraisee_id);
87 return (int) $run_id;
88 }
89
95 protected function checkUserParameters(
96 int $user_id,
97 string $code = ""
98 ): void {
99 if ($this->feature_config->usesAppraisees() && $this->appraisee_id === 0) {
100 throw new \ilSurveyException("No appraisee specified");
101 }
102
103 if (!$this->feature_config->usesAppraisees() && $this->appraisee_id > 0) {
104 // self eval currently uses current user as appraisee id
105 if ($this->survey->getMode() !== \ilObjSurvey::MODE_SELF_EVAL || $user_id !== $this->appraisee_id) {
106 throw new \ilSurveyException("Appraisee ID given, but appraisees not supported");
107 }
108 }
109
110 /* this fails on the info screen
111 if ($user_id === ANONYMOUS_USER_ID && $code === "") {
112 throw new \ilSurveyException("Code missing for anonymous user.");
113 }*/
114 }
115
116
117 // Get state of current run
118 protected function getCurrentState(): int
119 {
120 $repo = $this->repo;
121 $run_id = $this->getCurrentRunId();
122 return $repo->getState($run_id);
123 }
124
125 public function hasStarted(int $appraisee = 0): bool
126 {
127 return in_array(
128 $this->getCurrentState(),
130 true
131 );
132 }
133
134 public function hasFinished(): bool
135 {
136 return ($this->getCurrentState() ===
138 }
139
145 public function belongsToFinishedRun(
146 string $code
147 ): bool {
148 $repo = $this->repo;
149 $code_manager = $this->domain_service->code($this->survey, $this->current_user_id);
150
151 if ($code_manager->exists($code)) {
152 $run_id = $repo->getCurrentRunId(
153 $this->survey_id,
154 $this->current_user_id,
155 $code,
156 $this->appraisee_id
157 );
158 if ($run_id > 0) {
159 $state = $repo->getState($run_id);
160 if ($state === RunDBRepository::FINISHED) {
161 return true;
162 }
163 }
164 }
165 return false;
166 }
167
171 public function getRunsForUser(
172 int $user_id,
173 string $code = ""
174 ): array {
175 return $this->repo->getRunsForUser($this->survey->getSurveyId(), $user_id, $code);
176 }
177
178 public function getById(int $run_id): ?Run
179 {
180 $run = $this->repo->getById($run_id);
181 if (!is_null($run) && $run->getSurveyId() !== $this->survey->getSurveyId()) {
182 throw new \ilSurveyException("Run survey id mismatch.");
183 }
184 return $run;
185 }
186
190 public function start(
191 int $appraisee_id = 0
192 ): void {
193 $appraisee_id = ($appraisee_id > 0)
194 ? $appraisee_id
195 : $this->appraisee_id;
196 $code = $this->getCode();
197 $user_id = $this->current_user_id;
198 $survey = $this->survey;
199
200 if ($survey->getAnonymize() && $code === '') {
201 return;
202 }
203 $this->repo->add($this->survey->getSurveyId(), $user_id, $code, $appraisee_id);
204 }
205
206 public function initSession(
207 string $requested_code = ""
208 ): void {
209 $user_id = $this->current_user_id;
210 $survey = $this->survey;
211 $session_repo = $this->session_repo;
212 // validate incoming
213 $code_input = false;
214 // ->requested_code
215 $anonymous_code = $requested_code;
216 if ($anonymous_code !== "") {
217 $code_input = true;
218 if ($this->belongsToFinishedRun($anonymous_code)) { // #15031 - valid as long survey is not finished
219 $anonymous_code = "";
220 } else {
221 // #15860
222 // a user has used a valid code, we store this in table
223 // svy_anonymous
224 $this->code_manager->bindUser($anonymous_code, $user_id);
225 $session_repo->setCode($survey->getId(), $anonymous_code);
226 }
227 }
228 // now we try to get the code from the session
229 if (!$anonymous_code) {
230 $anonymous_code = $session_repo->getCode($survey->getId());
231 if ($anonymous_code) {
232 $code_input = true; // ??
233 }
234 }
235
236 // if the survey is anonymous, codes are stored for logged
237 // in users in svy_finished. Here we get this code, if already stored
238 if ($survey->getAnonymize() && !$anonymous_code) {
239 $anonymous_code = $survey->findCodeForUser($user_id);
240 }
241
242 // get existing runs for current user, might generate code
243 $execution_status = $survey->getUserSurveyExecutionStatus($anonymous_code);
244 if ($execution_status) {
245 $anonymous_code = (string) $execution_status["code"];
246 $execution_status = $execution_status["runs"];
247 }
248
249 // (final) check for proper anonymous code
250 if (!$survey->isAccessibleWithoutCode() &&
251// !$is_appraisee &&
252 $code_input && // #11346
253 (!$anonymous_code || !$this->code_manager->exists($anonymous_code))) {
254 $anonymous_code = "";
255 $this->clearCode();
256 throw new \ilWrongSurveyCodeException($this->lng->txt("svy_wrong_or_expired_code"));
257 }
258 $this->session_repo->setCode($survey->getId(), $anonymous_code);
259 }
260
264 public function getCode(): string
265 {
266 return $this->session_repo->getCode($this->survey->getId());
267 }
268
269 public function clearCode(): void
270 {
271 $this->session_repo->clearCode($this->survey->getId());
272 }
273
277 public function setStartTime(
278 int $first_question
279 ): void {
280 $run_id = $this->getCurrentRunId();
281 $time = time();
282 $this->session_repo->setPageEnter($time);
283 $this->repo->addTime($run_id, $time, $first_question);
284 }
285
286 public function setEndTime(): void
287 {
288 $run_id = $this->getCurrentRunId();
289 $time = time();
290 $this->repo->updateTime($run_id, $time, $this->session_repo->getPageEnter());
291 $this->session_repo->clearPageEnter();
292 }
293
294 public function getPageEnter(): int
295 {
296 return $this->session_repo->getPageEnter();
297 }
298
299 public function setPreviewData(int $question_id, array $data): void
300 {
301 $this->session_repo->setPreviewData($this->survey_id, $question_id, $data);
302 }
303
304 public function getPreviewData(int $question_id): array
305 {
306 return $this->session_repo->getPreviewData($this->survey_id, $question_id);
307 }
308
309 public function clearPreviewData(int $question_id): void
310 {
311 $this->session_repo->clearPreviewData($this->survey_id, $question_id);
312 }
313
314 public function clearAllPreviewData(): void
315 {
316 $this->session_repo->clearAllPreviewData($this->survey_id);
317 }
318
319 public function setErrors(array $errors): void
320 {
321 $this->session_repo->setErrors($errors);
322 }
323
324 public function getErrors(): array
325 {
326 return $this->session_repo->getErrors();
327 }
328
329 public function clearErrors(): void
330 {
331 $this->session_repo->clearErrors();
332 }
333
334 public function setPostData(array $data): void
335 {
336 $this->session_repo->setPostData($data);
337 }
338
339 public function getPostData(): array
340 {
341 return $this->session_repo->getPostData();
342 }
343
344 public function clearPostData(): void
345 {
346 $this->session_repo->clearPostData();
347 }
348}
getCurrentRunId(int $survey_id, int $user_id, string $code="", int $appr_id=0)
Survey Run Note: The manager should get the current user id passed.
setPreviewData(int $question_id, array $data)
start(int $appraisee_id=0)
Starts the survey creating a new run.
setStartTime(int $first_question)
Set start time of run.
initSession(string $requested_code="")
checkUserParameters(int $user_id, string $code="")
Check user parameters.
getCode()
Get current valid code.
belongsToFinishedRun(string $code)
Does code belong to current anonymous started, but not finished run? Note: this method acts on the cu...
getRunsForUser(int $user_id, string $code="")
ILIAS Survey Code CodeManager $code_manager
InternalDomainService $domain_service
__construct(InternalRepoService $repo_service, InternalDomainService $domain_service, \ilObjSurvey $survey, int $current_user_id, int $appraisee_id=0)
Stores access codes of anonymous session.
setCode(int $survey_id, string $code)
Code data class.
Definition: class.Run.php:28
code(\ilObjSurvey $survey, int $user_id)
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!file_exists('../ilias.ini.php'))