1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Class ilObjectActivationGUI
6 *
7 * @author Stefan Meyer <meyer@leifos.com>
8 *
9 * @ilCtrl_Calls ilObjectActivationGUI: ilConditionHandlerGUI
10 */
11class ilObjectActivationGUI
12{
13    /**
14     * @var ilErrorHandling
15     */
16    protected $error;
17
18    /**
19     * @var ilTabsGUI
20     */
21    protected $tabs_gui;
22
23    /**
24     * @var ilAccessHandler
25     */
26    protected $access;
27
28    /**
29     * @var ilTree
30     */
31    protected $tree;
32
33    /**
34     * @var ilObjUser
35     */
36    protected $user;
37
38    /**
39     * @var ilHelpGUI
40     */
41    protected $help;
42
43    /**
44     * @var int
45     */
46    protected $parent_ref_id;
47
48    /**
49     * @var int
50     */
51    protected $item_id;
52
53    /**
54     * @var \ilTemplate
55     */
56    protected $tpl;
57
58    /**
59     * @var ilCtrl
60     */
61    protected $ctrl;
62
63    /**
64     * @var \ilLanguage
65     */
66    protected $lng;
67
68    /**
69     * @var int|null
70     */
71    protected $timing_mode = null;
72
73    /**
74     * @var int|null
75     */
76    protected $activation = null;
77
78    /**
79     * ilObjectActivationGUI constructor.
80     * @param $a_ref_id
81     * @param $a_item_id
82     */
83    public function __construct($a_ref_id, $a_item_id)
84    {
85        global $DIC;
86
87        $this->tpl = $DIC->ui()->mainTemplate();
88        $this->ctrl = $DIC->ctrl();
89        $this->lng = $DIC->language();
90        $this->lng->loadLanguageModule('crs');
91
92        $this->error = $DIC['ilErr'];
93        $this->tabs_gui = $DIC->tabs();
94        $this->access = $DIC->access();
95        $this->tree = $DIC->repositoryTree();
96        $this->user = $DIC->user();
97        $this->help = $DIC["ilHelp"];
98
99
100        $this->parent_ref_id = $a_ref_id;
101        $this->item_id = $a_item_id;
102
103        $this->ctrl->saveParameter($this, 'item_id');
104    }
105
106    /**
107     * Execute command
108     * @throws ilCtrlException
109     */
110    public function executeCommand()
111    {
112        $tpl = $this->tpl;
113
114        $this->__setTabs();
115
116        $cmd = $this->ctrl->getCmd();
117
118        // Check if item id is given and valid
119        if (!$this->item_id) {
120            ilUtil::sendFailure($this->lng->txt("crs_no_item_id_given"), true);
121            $this->ctrl->returnToParent($this);
122        }
123
124        $tpl->getStandardTemplate();
125
126        switch ($this->ctrl->getNextClass($this)) {
127            case 'ilconditionhandlergui':
128                // preconditions for single course items
129                $this->ctrl->saveParameter($this, 'item_id', $_GET['item_id']);
130                $new_gui = new ilConditionHandlerGUI($this, (int) $_GET['item_id']);
131                $this->ctrl->forwardCommand($new_gui);
132                $this->tabs_gui->setTabActive('preconditions');
133                break;
134
135            default:
136                $this->initTimingMode();
137                $this->initItem();
138                $this->tabs_gui->setTabActive('timings');
139                if (!$cmd) {
140                    $cmd = 'edit';
141                }
142                $this->$cmd();
143                $this->tabs_gui->setTabActive('timings');
144                break;
145        }
146
147        $tpl->show();
148    }
149
150    /**
151     * @return int
152     */
153    public function getItemId()
154    {
155        return $this->item_id;
156    }
157
158    public function getTimingMode()
159    {
160        return $this->timing_mode;
161    }
162
163    /**
164     * Get parent ref_id
165     * @return int
166     */
167    public function getParentId()
168    {
169        return $this->parent_ref_id;
170    }
171
172    /**
173     * Get item object
174     * @return ilObjectActivation
175     */
176    public function getActivation()
177    {
178        return $this->activation;
179    }
180
181
182    /**
183     * cancel action handler
184     */
185    public function cancel()
186    {
187        $this->ctrl->setParameterByClass('ilrepositorygui', 'ref_id', $this->parent_ref_id);
188        $this->ctrl->redirectByClass('ilrepositorygui');
189    }
190
191    /**
192     * edit timings
193     *
194     * @access public
195     * @return
196     */
197    public function edit(ilPropertyFormGUI $form = null)
198    {
199        $ilErr = $this->error;
200        $ilAccess = $this->access;
201        $tpl = $this->tpl;
202
203        // #19997 - see ilObjectListGUI::insertTimingsCommand()
204        if (
205            !$ilAccess->checkAccess('write', '', $this->parent_ref_id) &&
206            !$ilAccess->checkAccess('write', '', $this->getItemId())) {
207            $ilErr->raiseError($this->lng->txt('permission_denied'), $ilErr->MESSAGE);
208        }
209
210        if (!$form instanceof ilPropertyFormGUI) {
211            // show edit warning if timings are on
212            if ($GLOBALS['tree']->checkForParentType($this->getParentId(), 'crs')) {
213                if ($this->getActivation()->getTimingType() == ilObjectActivation::TIMINGS_PRESETTING) {
214                    ilUtil::sendInfo($this->lng->txt('crs_timings_warning_timing_exists'));
215                }
216            }
217
218            $form = $this->initFormEdit();
219        }
220        $tpl->setContent($form->getHTML());
221    }
222
223    /**
224     * init form edit
225     *
226     * @access protected
227     * @return
228     */
229    protected function initFormEdit()
230    {
231        $tree = $this->tree;
232
233        $form = new ilPropertyFormGUI();
234        $form->setFormAction($this->ctrl->getFormAction($this));
235
236        $title = ilObject::_lookupTitle(ilObject::_lookupObjId($this->getItemId()));
237        $form->setTitle($title . ': ' . $this->lng->txt('crs_edit_timings'));
238
239
240        $availability = new ilCheckboxInputGUI($this->lng->txt('crs_timings_availability_enabled'), 'availability');
241        $availability->setValue(1);
242        $availability->setChecked($this->getActivation()->getTimingType() == ilObjectActivation::TIMINGS_ACTIVATION);
243
244        $start = new ilDateTimeInputGUI($this->lng->txt('crs_timings_start'), 'timing_start');
245        $start->setDate(new ilDateTime($this->getActivation()->getTimingStart(), IL_CAL_UNIX));
246        $start->setShowTime(true);
247        $availability->addSubItem($start);
248
249        $end = new ilDateTimeInputGUI($this->lng->txt('crs_timings_end'), 'timing_end');
250        $end->setDate(new ilDateTime($this->getActivation()->getTimingEnd(), IL_CAL_UNIX));
251        $end->setShowTime(true);
252        $availability->addSubItem($end);
253
254        $isv = new ilCheckboxInputGUI($this->lng->txt('crs_timings_visibility_short'), 'visible');
255        $isv->setInfo($this->lng->txt('crs_timings_visibility'));
256        $isv->setValue(1);
257        $isv->setChecked((bool) $this->getActivation()->enabledVisible());
258        $availability->addSubItem($isv);
259
260
261        $form->addItem($availability);
262
263        $form->addCommandButton('update', $this->lng->txt('save'));
264        $form->addCommandButton('cancel', $this->lng->txt('cancel'));
265
266        return $form;
267    }
268
269    /**
270     * update
271     *
272     * @access public
273     * @return
274     */
275    public function update()
276    {
277        $ilErr = $this->error;
278        $ilAccess = $this->access;
279        $tpl = $this->tpl;
280        $ilUser = $this->user;
281
282        // #19997 - see ilObjectListGUI::insertTimingsCommand()
283        if (
284            !$ilAccess->checkAccess('write', '', $this->parent_ref_id) &&
285            !$ilAccess->checkAccess('write', '', $this->getItemId())) {
286            $ilErr->raiseError($this->lng->txt('permission_denied'), $ilErr->MESSAGE);
287        }
288
289        $form = $this->initFormEdit();
290        if ($form->checkInput()) {
291            $valid = true;
292            $activation = new ilObjectActivation();
293            $activation->read($this->getItemId());
294
295            if ($form->getInput('availability')) {
296                $this->getActivation()->setTimingType(ilObjectActivation::TIMINGS_ACTIVATION);
297
298                $timing_start = $form->getItemByPostVar('timing_start')->getDate();
299                $timing_end = $form->getItemByPostVar('timing_end')->getDate();
300
301                if ($timing_start && $timing_end && ilDateTime::_after($timing_start, $timing_end)) {
302                    $form->getItemByPostVar('timing_start')->setAlert($this->lng->txt('crs_timing_err_start_end'));
303                    $form->getItemByPostVar('timing_end')->setAlert($this->lng->txt('crs_timing_err_start_end'));
304                    $valid = false;
305                }
306
307                $this->getActivation()->setTimingStart($timing_start ? $timing_start->get(IL_CAL_UNIX) : null);
308                $this->getActivation()->setTimingEnd($timing_end ? $timing_end->get(IL_CAL_UNIX) : null);
309
310                $this->getActivation()->toggleVisible((bool) $form->getInput('visible'));
311            } elseif ($this->getActivation()->getTimingType() != ilObjectActivation::TIMINGS_PRESETTING) {
312                $this->getActivation()->setTimingType(ilObjectActivation::TIMINGS_DEACTIVATED);
313            }
314
315            if ($valid) {
316                $this->getActivation()->update($this->getItemId(), $this->getParentId());
317                ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
318                $this->ctrl->redirect($this, "edit");
319            } else {
320                ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
321            }
322        }
323
324        $form->setValuesByPost();
325        $this->edit($form);
326    }
327
328    /**
329     * @return bool
330     */
331    protected function __setTabs()
332    {
333        $ilCtrl = $this->ctrl;
334        $ilHelp = $this->help;
335
336        $this->tabs_gui->clearTargets();
337
338        $ilHelp->setScreenIdComponent("obj");
339
340        $ilCtrl->setParameterByClass("ilrepositorygui", "ref_id", $this->parent_ref_id);
341        $back_link = $ilCtrl->getLinkTargetByClass("ilrepositorygui", "");
342        $ilCtrl->setParameterByClass("ilrepositorygui", "ref_id", $_GET["ref_id"]);
343        $this->tabs_gui->setBackTarget($this->lng->txt('btn_back'), $back_link);
344
345        $this->tabs_gui->addTarget(
346            "timings",
347            $this->ctrl->getLinkTarget($this, 'edit'),
348            "edit",
349            get_class($this)
350        );
351
352        $this->ctrl->setParameterByClass('ilconditionhandlergui', 'item_id', $this->item_id);
353        $this->tabs_gui->addTarget(
354            "preconditions",
355            $this->ctrl->getLinkTargetByClass('ilConditionHandlerGUI', 'listConditions'),
356            "",
357            "ilConditionHandlerGUI"
358        );
359        return true;
360    }
361
362    /**
363     * Init type of timing mode
364     */
365    protected function initTimingMode()
366    {
367        // Check for parent course and if available read timing mode (abs | rel)
368        $crs_ref_id = $GLOBALS['tree']->checkForParentType(
369            $this->parent_ref_id,
370            'crs'
371        );
372        $crs_obj_id = ilObject::_lookupObjId($crs_ref_id);
373
374        if ($crs_obj_id) {
375            $this->timing_mode = ilObjCourse::lookupTimingMode($crs_obj_id);
376        } else {
377            $this->timing_mode = ilCourseConstants::IL_CRS_VIEW_TIMING_ABSOLUTE;
378        }
379    }
380
381    /**
382     * Init item
383     */
384    protected function initItem()
385    {
386        $this->activation = new ilObjectActivation();
387        $this->getActivation()->read($this->item_id, $this->getParentId());
388    }
389}
390