1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Object-based submissions (ends up as static file)
6 *
7 * @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
8 *
9 * @ilCtrl_Calls ilExSubmissionTextGUI:
10 * @ingroup ModulesExercise
11 */
12class ilExSubmissionTextGUI extends ilExSubmissionBaseGUI
13{
14    /**
15     * @var ilObjUser
16     */
17    protected $user;
18
19    /**
20     * @var ilHelpGUI
21     */
22    protected $help;
23
24
25    /**
26     * Constructor
27     */
28    public function __construct(ilObjExercise $a_exercise, ilExSubmission $a_submission)
29    {
30        global $DIC;
31
32        parent::__construct($a_exercise, $a_submission);
33        $this->user = $DIC->user();
34        $this->help = $DIC["ilHelp"];
35    }
36
37    public function executeCommand()
38    {
39        $ilCtrl = $this->ctrl;
40
41        if (!$this->assignment ||
42            $this->assignment->getType() != ilExAssignment::TYPE_TEXT ||
43            !$this->submission->canView()) {
44            return;
45        }
46
47        $class = $ilCtrl->getNextClass($this);
48        $cmd = $ilCtrl->getCmd("showassignmenttext");
49
50        switch ($class) {
51            default:
52                $this->{$cmd . "Object"}();
53                break;
54        }
55    }
56
57    public static function getOverviewContent(ilInfoScreenGUI $a_info, ilExSubmission $a_submission)
58    {
59        global $DIC;
60
61        $lng = $DIC->language();
62        $ilCtrl = $DIC->ctrl();
63
64        if ($a_submission->canSubmit()) {
65            $button = ilLinkButton::getInstance();
66            $button->setPrimary(true);
67            $button->setCaption("exc_text_assignment_edit");
68            $button->setUrl($ilCtrl->getLinkTargetByClass(array("ilExSubmissionGUI", "ilExSubmissionTextGUI"), "editAssignmentText"));
69            $files_str = $button->render();
70        } else {
71            $button = ilLinkButton::getInstance();
72            $button->setCaption("exc_text_assignment_show");
73            $button->setUrl($ilCtrl->getLinkTargetByClass(array("ilExSubmissionGUI", "ilExSubmissionTextGUI"), "showAssignmentText"));
74            $files_str = $button->render();
75        }
76
77        $a_info->addProperty($lng->txt("exc_files_returned_text"), $files_str);
78    }
79
80
81    //
82    // TEXT ASSIGNMENT (EDIT)
83    //
84
85    protected function initAssignmentTextForm($a_read_only = false)
86    {
87        $ilCtrl = $this->ctrl;
88        $lng = $this->lng;
89
90        include_once "Services/Form/classes/class.ilPropertyFormGUI.php";
91        $form = new ilPropertyFormGUI();
92        $form->setTitle($this->lng->txt("exc_assignment") . " \"" . $this->assignment->getTitle() . "\"");
93
94        if (!$a_read_only) {
95            $text = new ilTextAreaInputGUI($this->lng->txt("exc_your_text"), "atxt");
96            $text->setRequired((bool) $this->submission->getAssignment()->getMandatory());
97            $text->setRows(40);
98            $text->setMaxNumOfChars($this->assignment->getMaxCharLimit());
99            $text->setMinNumOfChars($this->assignment->getMinCharLimit());
100
101            if ($text->isCharLimited()) {
102                $char_msg = "";
103                if ($this->assignment->getMinCharLimit()) {
104                    $char_msg .= $lng->txt("exc_min_char_limit") . ": " . $this->assignment->getMinCharLimit();
105                }
106                if ($this->assignment->getMaxCharLimit()) {
107                    $char_msg .= " " . $lng->txt("exc_max_char_limit") . ": " . $this->assignment->getMaxCharLimit();
108                }
109                $text->setInfo($char_msg);
110            }
111
112            $form->addItem($text);
113
114            // custom rte tags
115            $text->setUseRte(true);
116            $text->setRTESupport($this->submission->getUserId(), "exca~", "exc_ass");
117
118            // see ilObjForumGUI
119            $text->disableButtons(array(
120                'charmap',
121                'undo',
122                'redo',
123                'justifyleft',
124                'justifycenter',
125                'justifyright',
126                'justifyfull',
127                'anchor',
128                'fullscreen',
129                'cut',
130                'copy',
131                'paste',
132                'pastetext',
133                'code',
134                // 'formatselect' #13234
135            ));
136
137            $form->setFormAction($ilCtrl->getFormAction($this, "updateAssignmentText"));
138            $form->addCommandButton("updateAssignmentTextAndReturn", $this->lng->txt("save_return"));
139            $form->addCommandButton("updateAssignmentText", $this->lng->txt("save"));
140            $form->addCommandButton("returnToParent", $this->lng->txt("cancel"));
141        } else {
142            $form->setFormAction($ilCtrl->getFormAction($this, "returnToParent"));
143            $text = new ilNonEditableValueGUI($this->lng->txt("exc_files_returned_text"), "atxt", true);
144            $form->addItem($text);
145        }
146
147        return $form;
148    }
149
150    public function editAssignmentTextObject(ilPropertyFormGUI $a_form = null)
151    {
152        $ilCtrl = $this->ctrl;
153        $ilUser = $this->user;
154
155        if (!$this->submission->canSubmit()) {
156            ilUtil::sendFailure($this->lng->txt("exercise_time_over"), true);
157            $ilCtrl->redirect($this, "returnToParent");
158        }
159
160        $deadline = max($this->assignment->getDeadline(), $this->assignment->getExtendedDeadline());
161        if ($deadline) {
162            $deadline = $this->assignment->getPersonalDeadline($ilUser->getId());
163
164            // extended deadline date should not be presented anywhere
165            // see ilExAssignmentGUI::addSchedule()
166            $dl_info = ilDatePresentation::formatDate(new ilDateTime($deadline, IL_CAL_UNIX));
167
168            // #16151 - extended deadline warning (only after deadline passed)
169            if ($deadline < time()) {
170                $dl = ilDatePresentation::formatDate(new ilDateTime($deadline, IL_CAL_UNIX));
171                $dl = '<br /><span class="warning">' . sprintf($this->lng->txt("exc_late_submission_warning"), $dl) . '</span>';
172                $dl_info .= $dl;
173            }
174
175            ilUtil::sendInfo($this->lng->txt("exc_edit_until") . ": " . $dl_info);
176        }
177
178        $this->handleTabs();
179
180        $ilHelp = $this->help;
181        $ilHelp->setScreenIdComponent("exc");
182        $ilHelp->setScreenId("text_submission");
183
184        if (!$a_form) {
185            $a_form = $this->initAssignmentTextForm();
186
187            $files = $this->submission->getFiles();
188            if ($files) {
189                $files = array_shift($files);
190                if (trim($files["atext"])) {
191                    $text = $a_form->getItemByPostVar("atxt");
192                    // mob id to mob src
193                    $text->setValue(ilRTE::_replaceMediaObjectImageSrc($files["atext"], 1));
194                }
195            }
196        }
197
198        $this->tpl->setContent($a_form->getHTML());
199    }
200
201    public function updateAssignmentTextAndReturnObject()
202    {
203        $this->updateAssignmentTextObject(true);
204    }
205
206    public function updateAssignmentTextObject($a_return = false)
207    {
208        $ilCtrl = $this->ctrl;
209
210        if (!$this->submission->canSubmit()) {
211            ilUtil::sendFailure($this->lng->txt("exercise_time_over"), true);
212            $ilCtrl->redirect($this, "returnToParent");
213        }
214
215        $form = $this->initAssignmentTextForm();
216
217        // we are not using a purifier, so we have to set the valid RTE tags
218        // :TODO:
219        include_once("./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php");
220        $rte = $form->getItemByPostVar("atxt");
221        $rte->setRteTags(ilObjAdvancedEditing::_getUsedHTMLTags("exc_ass"));
222
223        if ($form->checkInput()) {
224            $text = trim($form->getInput("atxt"));
225
226            $existing = $this->submission->getFiles();
227
228            $returned_id = $this->submission->updateTextSubmission(
229                // mob src to mob id
230                ilRTE::_replaceMediaObjectImageSrc($text, 0)
231            );
232
233            // no empty text
234            if ($returned_id) {
235                // #16532 - always send notifications
236                $this->handleNewUpload();
237
238                // mob usage
239                include_once "Services/MediaObjects/classes/class.ilObjMediaObject.php";
240                $mobs = ilRTE::_getMediaObjects($text, 0);
241                foreach ($mobs as $mob) {
242                    if (ilObjMediaObject::_exists($mob)) {
243                        ilObjMediaObject::_removeUsage($mob, 'exca~:html', $this->submission->getUserId());
244                        ilObjMediaObject::_saveUsage($mob, 'exca:html', $returned_id);
245                    }
246                }
247            } else {
248                $this->handleRemovedUpload();
249            }
250
251            ilUtil::sendSuccess($this->lng->txt("exc_text_saved"), true);
252            if ($a_return) {
253                $ilCtrl->redirect($this, "returnToParent");
254            } else {
255                $ilCtrl->redirect($this, "editAssignmentText");
256            }
257        }
258
259        $form->setValuesByPost();
260        $this->editAssignmentTextObject($form);
261    }
262
263    public function showAssignmentTextObject()
264    {
265        if (!$this->submission->isTutor()) {
266            $this->handleTabs();
267        }
268
269        $a_form = $this->initAssignmentTextForm(true);
270
271        $files = $this->submission->getFiles();
272        if ($files) {
273            $files = array_shift($files);
274            if (trim($files["atext"])) {
275                if ($files["late"] &&
276                    !$this->submission->hasPeerReviewAccess()) {
277                    ilUtil::sendFailure($this->lng->txt("exc_late_submission"));
278                }
279
280                $text = $a_form->getItemByPostVar("atxt");
281                // mob id to mob src
282                $text->setValue(nl2br(ilRTE::_replaceMediaObjectImageSrc($files["atext"], 1)));
283            }
284        }
285
286        $this->tpl->setContent($a_form->getHTML());
287    }
288}
289