1<?php
2/* Copyright (c) 2020 Daniel Weise <daniel.weise@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3
4declare(strict_types=1);
5
6use \ILIAS\UI\Component\Input\Field;
7use \ILIAS\Refinery\Factory as Refinery;
8
9class ilIndividualAssessmentUserGrading
10{
11    /**
12     * @var string
13     */
14    protected $name;
15
16    /**
17     * @var string
18     */
19    protected $record;
20
21    /**
22     * @var string
23     */
24    protected $internal_note;
25
26    /**
27     * @var ?string
28     */
29    protected $file;
30
31    /**
32     * @var bool
33     */
34    protected $is_file_visible;
35
36    /**
37     * @var int
38     */
39    protected $learning_progress;
40
41    /**
42     * @var string
43     */
44    protected $place;
45
46    /**
47     * @var DateTimeImmutable|null
48     */
49    protected $event_time;
50
51    /**
52     * @var bool
53     */
54    protected $notify;
55
56    /**
57     * @var bool
58     */
59    protected $finalized;
60
61    public function __construct(
62        string $name,
63        string $record,
64        string $internal_note,
65        ?string $file,
66        bool $is_file_visible,
67        int $learning_progress,
68        string $place,
69        ?DateTimeImmutable $event_time,
70        bool $notify,
71        bool $finalized = false
72    ) {
73        $this->name = $name;
74        $this->record = $record;
75        $this->internal_note = $internal_note;
76        $this->file = $file;
77        $this->is_file_visible = $is_file_visible;
78        $this->learning_progress = $learning_progress;
79        $this->place = $place;
80        $this->event_time = $event_time;
81        $this->notify = $notify;
82        $this->finalized = $finalized;
83    }
84
85    public function getName() : string
86    {
87        return $this->name;
88    }
89
90    public function getRecord() : string
91    {
92        return $this->record;
93    }
94
95    public function getInternalNote() : string
96    {
97        return $this->internal_note;
98    }
99
100    public function getFile() : ?string
101    {
102        return $this->file;
103    }
104
105    public function isFileVisible() : bool
106    {
107        return $this->is_file_visible;
108    }
109
110    public function getLearningProgress() : int
111    {
112        return $this->learning_progress;
113    }
114
115    public function getPlace() : string
116    {
117        return $this->place;
118    }
119
120    public function getEventTime() : ?DateTimeImmutable
121    {
122        return $this->event_time;
123    }
124
125    public function isNotify() : bool
126    {
127        return $this->notify;
128    }
129
130    public function isFinalized() : bool
131    {
132        return $this->finalized;
133    }
134
135    public function withFinalized(bool $finalize) : ilIndividualAssessmentUserGrading
136    {
137        $clone = clone $this;
138        $clone->finalized = $finalize;
139        return $clone;
140    }
141
142    public function withFile(?string $file) : ilIndividualAssessmentUserGrading
143    {
144        $clone = clone $this;
145        $clone->file = $file;
146        return $clone;
147    }
148
149    public function toFormInput(
150        Field\Factory $input,
151        \ilLanguage $lng,
152        Refinery $refinery,
153        array $grading_options,
154        bool $may_be_edited = true,
155        bool $place_required = false,
156        bool $amend = false,
157        ilIndividualAssessmentMemberGUI $file_handler
158    ) : Field\Input {
159        $name = $input
160            ->text($lng->txt('name'), '')
161            ->withDisabled(true)
162            ->withValue($this->getName())
163        ;
164
165        $record = $input
166            ->textarea($lng->txt('iass_record'), $lng->txt('iass_record_info'))
167            ->withValue($this->getRecord())
168            ->withDisabled(!$may_be_edited)
169        ;
170
171        $internal_note = $input
172            ->textarea($lng->txt('iass_internal_note'), $lng->txt('iass_internal_note_info'))
173            ->withValue($this->getInternalNote())
174            ->withDisabled(!$may_be_edited)
175        ;
176
177        $file = $input
178            ->file($file_handler, $lng->txt('iass_upload_file'), $lng->txt('iass_file_dropzone'))
179            ->withValue([$this->getFile()])
180        ;
181
182        $file_visible = $input
183            ->checkbox($lng->txt('iass_file_visible_examinee'))
184            ->withValue($this->isFileVisible())
185            ->withDisabled(!$may_be_edited)
186        ;
187
188        $learning_progress = $input
189            ->select($lng->txt('grading'), $grading_options)
190            ->withValue($this->getLearningProgress() ? $this->getLearningProgress() : ilIndividualAssessmentMembers::LP_IN_PROGRESS)
191            ->withDisabled(!$may_be_edited)
192            ->withRequired(true)
193        ;
194
195        $place = $input
196            ->text($lng->txt('iass_place'))
197            ->withValue($this->getPlace())
198            ->withRequired($place_required)
199            ->withDisabled(!$may_be_edited)
200        ;
201
202        $event_time = $input
203            ->dateTime($lng->txt('iass_event_time'))
204            ->withRequired($place_required)
205            ->withDisabled(!$may_be_edited)
206        ;
207
208        if (!is_null($this->getEventTime())) {
209            $event_time = $event_time->withValue($this->getEventTime()->format('d-m-Y'));
210        }
211
212        $notify = $input
213            ->checkbox($lng->txt('iass_notify'), $lng->txt('iass_notify_explanation'))
214            ->withValue($this->isNotify())
215            ->withDisabled(!$may_be_edited)
216        ;
217
218        $fields = [
219            'name' => $name,
220            'record' => $record,
221            'internal_note' => $internal_note,
222            'file' => $file,
223            'file_visible' => $file_visible,
224            'learning_progress' => $learning_progress,
225            'place' => $place,
226            'event_time' => $event_time,
227            'notify' => $notify
228        ];
229
230        if (!$amend) {
231            $finalized = $input
232                ->checkbox($lng->txt('iass_finalize'), $lng->txt('iass_finalize_info'))
233                ->withValue($this->isFinalized())
234                ->withDisabled(!$may_be_edited)
235            ;
236
237            $fields['finalized'] = $finalized;
238        }
239
240        return $input->section(
241            $fields,
242            $lng->txt('iass_edit_record')
243        )->withAdditionalTransformation(
244            $refinery->custom()->transformation(function ($values) use ($amend) {
245                $finalized = $this->isFinalized();
246                if (!$amend) {
247                    $finalized = $values['finalized'];
248                }
249
250                $file = $this->getFile();
251                if (
252                    isset($values['file'][0]) &&
253                    trim($values['file'][0]) != ''
254                ) {
255                    $file = $values['file'][0];
256                }
257
258                return new ilIndividualAssessmentUserGrading(
259                    $values['name'],
260                    $values['record'],
261                    $values['internal_note'],
262                    $file,
263                    $values['file_visible'],
264                    (int) $values['learning_progress'],
265                    $values['place'],
266                    $values['event_time'],
267                    $values['notify'],
268                    $finalized
269                );
270            })
271        );
272    }
273}
274