ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilIndividualAssessmentMemberGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 2020 Daniel Weise <daniel.weise@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3 
4 declare(strict_types=1);
5 
17 use ILIAS\Data;
18 use ILIAS\Refinery;
19 
21 {
22  const CMD_VIEW = 'view';
23  const CMD_EDIT = 'edit';
24  const CMD_UPDATE = 'update';
25  const CMD_FINALIZE = 'finalize';
26  const CMD_FINALIZE_CONFIRMATION = 'finalizeConfirmation';
27  const CMD_AMEND = 'amend';
28  const CMD_SAVE_AMEND = "saveAmend";
29  const CMD_DOWNLOAD_FILE = "downloadFile";
30 
34  protected $ctrl;
35 
39  protected $lng;
40 
44  protected $tpl;
45 
49  protected $user;
50 
54  protected $input_factory;
55 
60 
64  protected $button_factory;
65 
69  protected $refinery_factory;
70 
74  protected $data_factory;
75 
79  protected $renderer;
80 
84  protected $request;
85 
89  protected $object;
90 
94  protected $parent_gui;
95 
99  protected $iass_access;
100 
104  protected $notificator;
105 
109  protected $toolbar;
110 
114  protected $error_object;
115 
116  public function __construct(
117  ilCtrl $ctrl,
125  Data\Factory $data_factory,
127  ServerRequest $request,
132  ) {
134 
135  $this->ctrl = $ctrl;
136  $this->lng = $lng;
137  $this->tpl = $tpl;
138  $this->user = $user;
139  $this->input_factory = $input_factory;
140  $this->messagebox_factory = $messagebox_factory;
141  $this->button_factory = $button_factory;
142  $this->refinery_factory = $refinery_factory;
143  $this->data_factory = $data_factory;
144  $this->renderer = $renderer;
145  $this->request = $request;
146  $this->notificator = $notificator;
147  $this->toolbar = $toolbar;
148  $this->object = $object;
149  $this->error_object = $error_object;
150  }
151 
152  public function executeCommand() : void
153  {
154  $cmd = $this->ctrl->getCmd();
155 
156  switch ($cmd) {
157  case self::CMD_VIEW:
158  case self::CMD_UPDATE:
159  case self::CMD_EDIT:
160  case self::CMD_FINALIZE:
161  case self::CMD_FINALIZE_CONFIRMATION:
162  case self::CMD_AMEND:
163  case self::CMD_SAVE_AMEND:
164  case self::CMD_DOWNLOAD_FILE:
165  $this->$cmd();
166  break;
167  case AbstractCtrlAwareUploadHandler::CMD_UPLOAD:
168  case AbstractCtrlAwareUploadHandler::CMD_REMOVE:
169  case AbstractCtrlAwareUploadHandler::CMD_INFO:
170  parent::executeCommand();
171  break;
172  default:
173  throw new LogicException("Unknown command $cmd");
174  }
175  }
176 
177  protected function view()
178  {
179  if (!$this->mayBeViewed()) {
180  $this->handleAccessViolation();
181  return;
182  }
183  $form = $this->buildForm('', false);
184  $this->tpl->setContent($this->renderer->render($form));
185  }
186 
187  protected function edit()
188  {
189  if (!$this->mayBeEdited()) {
190  $this->handleAccessViolation();
191  return;
192  }
193 
194  $this->ctrl->setParameterByClass(self::class, 'usr_id', $this->getExaminee()->getId());
195  $action = $this->ctrl->getFormAction($this, 'update');
196  $this->ctrl->clearParameterByClass(self::class, 'usr_id');
197 
198  $this->setToolbar();
199  $form = $this->buildForm($action, true);
200  $this->tpl->setContent($this->renderer->render($form));
201  }
202 
203  protected function update()
204  {
205  $form = $this
206  ->buildForm($this->ctrl->getFormAction($this, self::CMD_UPDATE), true)
207  ->withRequest($this->request)
208  ;
209 
211  $grading = $form->getData();
212  if (!is_null($grading)) {
213  if ($grading->getFile() == '') {
214  $storage = $this->getUserFileStorage();
215  $storage->deleteCurrentFile();
216  }
217 
218  if ($grading->isFinalized()) {
219  $not_finalized_grading = $grading->withFinalized(false);
220  $this->saveMember($not_finalized_grading);
221  $this->finalizeConfirmation();
222  return;
223  }
224 
225  $this->saveMember($grading);
226 
227  if ($this->getObject()->isActiveLP()) {
229  }
230 
231  ilUtil::sendSuccess($this->lng->txt('iass_membership_saved'), true);
232  $this->redirect(self::CMD_EDIT);
233  }
234  }
235 
236  protected function amend()
237  {
238  if (!$this->mayBeAmended()) {
239  $this->handleAccessViolation();
240  return;
241  }
242 
243  $this->ctrl->setParameterByClass(self::class, 'usr_id', $this->getExaminee()->getId());
244  $action = $this->ctrl->getFormAction($this, self::CMD_SAVE_AMEND);
245  $this->ctrl->clearParameterByClass(self::class, 'usr_id');
246 
247  $this->setToolbar();
248  $form = $this->buildForm($action, true, true);
249  $this->tpl->setContent($this->renderer->render($form));
250  }
251 
252  protected function downloadFile()
253  {
254  $path = $this->getUserFileStorage()->getFilePath();
255  $file_name = $this->getMember()->fileName();
256  ilUtil::deliverFile($path, $file_name);
257  }
258 
259  protected function saveAmend()
260  {
261  if (!$this->mayBeAmended()) {
262  $this->handleAccessViolation();
263  return;
264  }
265 
266  $form = $this
267  ->buildForm($this->ctrl->getFormAction($this, self::CMD_AMEND), true, true)
268  ->withRequest($this->request)
269  ;
270 
271  $grading = $form->getData();
272 
273  if (!is_null($grading)) {
274  if ($grading->getFile() == '') {
275  $storage = $this->getUserFileStorage();
276  $storage->deleteCurrentFile();
277  }
278 
279  $this->saveMember($grading, true, true);
280 
281  if ($this->getObject()->isActiveLP()) {
283  }
284 
285  ilUtil::sendSuccess($this->lng->txt('iass_amend_saved'), true);
286  $this->redirect(self::CMD_AMEND);
287  }
288  }
289 
290  protected function buildForm(
291  string $form_action,
292  bool $may_be_edited,
293  bool $amend = false
294  ) : ILIAS\UI\Component\Input\Container\Form\Form {
295  $section = $this->getMember()->getGrading()->toFormInput(
296  $this->input_factory->field(),
297  $this->lng,
299  $this->getPossibleLPStates(),
300  $may_be_edited,
301  (bool) $this->getObject()->getSettings()->isEventTimePlaceRequired(),
302  $amend,
303  $this
304  );
305 
306  $form = $this->input_factory->container()->form()->standard($form_action, [$section]);
307  $form = $form->withAdditionalTransformation(
308  $this->refinery_factory->custom()->transformation(
309  function ($values) use ($amend) {
310  return array_shift($values);
311  }
312  )
313  );
314  return $form;
315  }
316 
317  protected function finalize() : void
318  {
319  if (!$this->mayBeEdited()) {
320  $this->handleAccessViolation();
321  return;
322  }
323 
324  $member = $this->getMember();
325  if (!$member->mayBeFinalized()) {
326  ilUtil::sendFailure($this->lng->txt('iass_may_not_finalize'), true);
327  $this->redirect('edit');
328  return;
329  }
330 
331  try {
332  $grading = $member->getGrading()->withFinalized(true);
333  $member = $member->withGrading($grading);
334  $this->getObject()->membersStorage()->updateMember($member);
336  ilUtil::sendFailure($e->getMessage(), true);
337  $this->redirect('edit');
338  return;
339  }
340 
341  if ($this->object->isActiveLP()) {
343  }
344 
345  try {
346  $member->maybeSendNotification($this->notificator);
347  } catch (ilIndividualAssessmentException $e) {
348  ilUtil::sendFailure($e->getMessage(), true);
349  $this->redirect('edit');
350  return;
351  }
352 
353  ilUtil::sendSuccess($this->lng->txt('iass_membership_finalized'), true);
354  $this->redirect('view');
355  }
356 
357  protected function finalizeConfirmation()
358  {
359  if (!$this->mayBeEdited()) {
360  $this->handleAccessViolation();
361  return;
362  }
363 
364  $message = $this->lng->txt('iass_finalize_user_qst');
365  $this->ctrl->setParameterByClass(self::class, 'usr_id', $this->getExaminee()->getId());
366  $finalize = $this->ctrl->getFormActionByClass(self::class, self::CMD_FINALIZE);
367  $cancel = $this->ctrl->getFormActionByClass(self::class, self::CMD_EDIT);
368  $this->ctrl->clearParameterByClass(self::class, 'usr_id');
369 
370  $buttons = [
371  $this->button_factory->standard($this->lng->txt('iass_confirm_finalize'), $finalize),
372  $this->button_factory->standard($this->lng->txt('iass_cancel'), $cancel)
373  ];
374 
375  $message_box = $this->messagebox_factory->confirmation($message)->withButtons($buttons);
376 
377  $this->tpl->setContent($this->renderer->render($message_box));
378  }
379 
380  protected function saveMember(
382  bool $keep_examiner = false,
383  bool $amend = false
384  ) : void {
385  $member = $this->getMember()
386  ->withGrading($grading)
387  ;
388 
389  if ($amend) {
390  $member = $member->withChangerId($this->user->getId());
391  }
392 
393  if (!$keep_examiner) {
394  $member = $member->withExaminerId($this->user->getId());
395  }
396  $this->getObject()->membersStorage()->updateMember($member);
397  }
398 
399  protected function getPossibleLPStates() : array
400  {
401  return [
402  ilIndividualAssessmentMembers::LP_IN_PROGRESS => $this->lng->txt('iass_status_pending'),
403  ilIndividualAssessmentMembers::LP_COMPLETED => $this->lng->txt('iass_status_completed'),
404  ilIndividualAssessmentMembers::LP_FAILED => $this->lng->txt('iass_status_failed')
405  ];
406  }
407 
408  protected function getUploadResult() : HandlerResult
409  {
410  $this->upload->process();
411  $array = $this->upload->getResults();
412  $result = end($array);
413 
414  if ($result instanceof UploadResult && $result->isOK()) {
415  $identifier = $this->uploadFile($result);
416  $status = HandlerResult::STATUS_OK;
417  $message = 'Upload ok';
418  } else {
419  $status = HandlerResult::STATUS_FAILED;
420  $identifier = '';
421  $message = $result->getStatus()->getMessage();
422  }
423 
424  return new BasicHandlerResult($this->getFileIdentifierParameterName(), $status, $identifier, $message);
425  }
426 
427  protected function getRemoveResult(string $identifier) : HandlerResult
428  {
429  $status = HandlerResult::STATUS_FAILED;
430  $message = $this->lng->txt('iass_file_deleted');
431 
432  if ($this->getFileName() == $identifier) {
433  $this->deleteFile();
434  $member = $this->getMember();
435  $grading = $member->getGrading()->withFile(null);
436  $member = $member->withGrading($grading);
437  $this->getObject()->membersStorage()->updateMember($member);
438  $status = HandlerResult::STATUS_OK;
439  $message = 'File Deleted';
440  }
441 
442  return new BasicHandlerResult($this->getFileIdentifierParameterName(), $status, $identifier, $message);
443  }
444 
448  protected function getInfoResult(string $identifier) : FileInfoResult
449  {
450  $filename = $this->getFileName();
451  if ($filename != $identifier) {
452  throw new LogicException("Wrong filename $identifier");
453  }
454 
455  $file_size = filesize($this->getFilePath());
456  return new BasicFileInfoResult(
458  $identifier,
459  $filename,
460  $file_size,
461  pathinfo($filename, PATHINFO_EXTENSION)
462  );
463  }
464 
468  public function getInfoForExistingFiles(array $file_ids) : array
469  {
470  $name = $this->getFileName();
471 
472  $ids = array_filter($file_ids, function ($id) {
473  if ($id == "") {
474  return false;
475  }
476  return true;
477  });
478 
479  if (is_null($name) || count($ids) === 0) {
480  return [];
481  }
482 
483  if (!in_array($name, $file_ids)) {
484  throw new LogicException("Wrong filename " . $this->getFileName());
485  }
486 
487  return [
490  "identifier",
491  $name,
492  64,
493  ''
494  )
495  ];
496  }
497 
501  public function getFileIdentifierParameterName() : string
502  {
503  return 'iass';
504  }
505 
509  public function getUploadURL() : string
510  {
511  $this->ctrl->setParameter($this, 'usr_id', $this->getExaminee()->getId());
512  $link = $this->ctrl->getLinkTarget($this, self::CMD_UPLOAD);
513  $this->ctrl->setParameter($this, 'usr_id', null);
514 
515  return $link;
516  }
517 
521  public function getExistingFileInfoURL() : string
522  {
523  $this->ctrl->setParameter($this, 'usr_id', $this->getExaminee()->getId());
524  $link = $this->ctrl->getLinkTarget($this, self::CMD_INFO);
525  $this->ctrl->setParameter($this, 'usr_id', null);
526 
527  return $link;
528  }
529 
533  public function getFileRemovalURL() : string
534  {
535  $this->ctrl->setParameter($this, 'usr_id', $this->getExaminee()->getId());
536  $this->ctrl->setParameter($this, $this->getFileIdentifierParameterName(), $this->getFileName());
537  $link = $this->ctrl->getLinkTarget($this, self::CMD_REMOVE);
538  $this->ctrl->setParameter($this, 'usr_id', null);
539  $this->ctrl->setParameter($this, $this->getFileIdentifierParameterName(), null);
540 
541  return $link;
542  }
543 
544  protected function uploadFile(UploadResult $result) : string
545  {
546  $storage = $this->getUserFileStorage();
547  $storage->create();
548  $storage->deleteCurrentFile();
549  $storage->uploadFile($result);
550 
551  return $result->getName();
552  }
553 
554  protected function deleteFile()
555  {
556  $storage = $this->getUserFileStorage();
557  $storage->deleteCurrentFile();
558  }
559 
560  protected function getFileName() : ?string
561  {
562  $path = $this->getFilePath();
563  if (is_null($path)) {
564  return null;
565  }
566 
567  return end(explode('/', $path));
568  }
569 
570  protected function getFilePath() : ?string
571  {
572  $storage = $this->getUserFileStorage();
573  if ($storage->isEmpty()) {
574  return null;
575  }
576 
577  return $storage->getFilePath();
578  }
579 
580  protected function redirect(string $cmd) : void
581  {
582  $this->ctrl->setParameterByClass(self::class, 'usr_id', $this->getExaminee()->getId());
583  $this->ctrl->redirect($this, $cmd);
584  }
585 
587  {
588  $this->object = $object;
589  }
590 
591  protected function getObject() : ilObjIndividualAssessment
592  {
593  return $this->object;
594  }
595 
597  {
598  $this->parent_gui = $parent_gui;
599  }
600 
602  {
603  return $this->parent_gui;
604  }
605 
607  {
608  if (is_null($this->iass_access)) {
609  $this->iass_access = $this->getObject()->accessHandler();
610  }
611  return $this->iass_access;
612  }
613 
614  protected function getExaminee()
615  {
616  return new ilObjUser($_GET['usr_id']);
617  }
618 
620  {
621  $storage = $this->getObject()->getFileStorage();
622  $storage->setUserId($this->getExaminee()->getId());
623  return $storage;
624  }
625 
627  {
628  return $this->getObject()->membersStorage()->loadMember(
629  $this->getObject(),
630  $this->getExaminee()
631  );
632  }
633 
634  protected function setToolbar()
635  {
636  $member = $this->getMember();
637  if ($member->fileName() != '') {
638  $btn = ilLinkButton::getInstance();
639  $btn->setCaption('download_assessment_paper');
640  $this->ctrl->setParameter($this, 'usr_id', $this->getExaminee()->getId());
641  $btn->setUrl($this->ctrl->getLinkTarget($this, self::CMD_DOWNLOAD_FILE, false, true));
642  $this->ctrl->setParameter($this, 'usr_id', null);
643  $this->toolbar->addButtonInstance($btn);
644  }
645  }
646 
647  protected function mayBeEdited() : bool
648  {
649  return $this->getAccessHandler()->isSystemAdmin() || (!$this->isFinalized() && $this->userMayGrade());
650  }
651 
652  protected function mayBeViewed() : bool
653  {
654  return
655  $this->getAccessHandler()->isSystemAdmin() ||
656  ($this->isFinalized() && ($this->userMayGrade() || $this->userMayView()))
657  ;
658  }
659 
660  protected function mayBeAmended() : bool
661  {
662  return $this->getAccessHandler()->isSystemAdmin() || ($this->isFinalized() && $this->userMayAmend());
663  }
664 
665  protected function userMayGrade() : bool
666  {
667  return
668  $this->getAccessHandler()->isSystemAdmin() ||
669  (!$this->targetWasEditedByOtherUser($this->getMember()) && $this->getAccessHandler()->mayGradeUser())
670  ;
671  }
672 
673  protected function userMayView() : bool
674  {
675  return $this->getAccessHandler()->isSystemAdmin() || $this->getAccessHandler()->mayViewUser();
676  }
677 
678  protected function userMayAmend() : bool
679  {
680  return $this->getAccessHandler()->isSystemAdmin() || $this->getAccessHandler()->mayAmendGradeUser();
681  }
682 
683  protected function targetWasEditedByOtherUser(ilIndividualAssessmentMember $member) : bool
684  {
685  return
686  (int) $member->examinerId() !== (int) $this->user->getId() &&
687  0 !== (int) $member->examinerId()
688  ;
689  }
690 
691  protected function isFinalized() : bool
692  {
693  return $this->getMember()->finalized();
694  }
695 
696  public function handleAccessViolation()
697  {
698  $this->error_object->raiseError($this->txt("msg_no_perm_read"), $this->error_object->WARNING);
699  }
700 }
An entity that renders components to a string output.
Definition: Renderer.php:14
This class provides processing control methods.
Class Factory.
For the purpose of streamlining the grading and learning-process status definition outside of tests...
$result
$_GET["client_id"]
buildForm(string $form_action, bool $may_be_edited, bool $amend=false)
Class ChatMainBarProvider .
static updateLPStatusOfMember(ilIndividualAssessmentMember $member)
__construct(ilCtrl $ctrl, ilLanguage $lng, ilGlobalPageTemplate $tpl, ilObjUser $user, Input\Factory $input_factory, MessageBox\Factory $messagebox_factory, Button\Factory $button_factory, Refinery\Factory $refinery_factory, Data\Factory $data_factory, Renderer $renderer, ServerRequest $request, ilIndividualAssessmentPrimitiveInternalNotificator $notificator, ilToolbarGUI $toolbar, ilObjIndividualAssessment $object, ilErrorHandling $error_object)
user()
Definition: user.php:4
setParentGUI(ilIndividualAssessmentMembersGUI $parent_gui)
$section
Definition: Utf8Test.php:83
if($format !==null) $name
Definition: metadata.php:230
Class ilGlobalPageTemplate.
targetWasEditedByOtherUser(ilIndividualAssessmentMember $member)
Builds data types.
Definition: Factory.php:19
saveMember(ilIndividualAssessmentUserGrading $grading, bool $keep_examiner=false, bool $amend=false)
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
$filename
Definition: buildRTE.php:89
Edit the record of a user, set LP.
__construct(Container $dic, ilPlugin $plugin)
Handles the fileupload and folder creation for files uploaded in grading form.
$message
Definition: xapiexit.php:14
language handling
executeCommand()
Since this is a ilCtrl aware UploadHandler executeCommand MUST be implemented.
For the purpose of streamlining the grading and learning-process status definition outside of tests...
static deliverFile( $a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.