1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
6/**
7 * Class ilCmiXapiRegistrationGUI
8 *
9 * @author      Uwe Kohnle <kohnle@internetlehrer-gmbh.de>
10 * @author      Björn Heyser <info@bjoernheyser.de>
11 * @author      Stefan Schneider <info@eqsoft.de>
12 *
13 * @package     Module/CmiXapi
14 */
15class ilCmiXapiRegistrationGUI
16{
17    const CMD_SHOW_FORM = 'showForm';
18    const CMD_SAVE_FORM = 'saveForm';
19    const CMD_CANCEL = 'cancel';
20
21    const DEFAULT_CMD = self::CMD_SHOW_FORM;
22
23    /**
24     * @var ilObjCmiXapi
25     */
26    protected $object;
27
28    /**
29     * @var ilCmiXapiUser
30     */
31    protected $cmixUser;
32
33    /**
34     * ilCmiXapiRegistrationGUI constructor.
35     * @param ilObjCmiXapi $object
36     */
37    public function __construct(ilObjCmiXapi $object)
38    {
39        global $DIC; /* @var \ILIAS\DI\Container $DIC */
40
41        $this->object = $object;
42
43        $this->cmixUser = new ilCmiXapiUser($object->getId(), $DIC->user()->getId(), $object->getPrivacyIdent());
44    }
45
46    public function executeCommand()
47    {
48        global $DIC; /* @var \ILIAS\DI\Container $DIC */
49
50        switch ($DIC->ctrl()->getNextClass()) {
51            default:
52                $command = $DIC->ctrl()->getCmd(self::DEFAULT_CMD) . 'Cmd';
53                $this->{$command}();
54        }
55    }
56
57    protected function cancelCmd()
58    {
59        global $DIC; /* @var \ILIAS\DI\Container $DIC */
60
61        $DIC->ctrl()->redirectByClass(ilObjCmiXapiGUI::class, ilObjCmiXapiGUI::CMD_INFO_SCREEN);
62    }
63
64    protected function showFormCmd(ilPropertyFormGUI $form = null)
65    {
66        global $DIC; /* @var \ILIAS\DI\Container $DIC */
67
68        if ($form === null) {
69            $form = $this->buildForm();
70        }
71
72        $DIC->ui()->mainTemplate()->setContent($form->getHTML());
73    }
74
75    protected function saveFormCmd()
76    {
77        global $DIC; /* @var \ILIAS\DI\Container $DIC */
78
79        $form = $this->buildForm();
80
81        if (!$form->checkInput()) {
82            $form->setValuesByPost();
83            $this->showFormCmd($form);
84            return;
85        }
86
87        $this->saveRegistration($form);
88
89        ilUtil::sendSuccess($DIC->language()->txt('registration_saved_successfully'), true);
90        $DIC->ctrl()->redirectByClass(ilObjCmiXapiGUI::class, ilObjCmiXapiGUI::CMD_INFO_SCREEN);
91    }
92
93    /**
94     * @return ilPropertyFormGUI
95     */
96    protected function buildForm()
97    {
98        global $DIC; /* @var \ILIAS\DI\Container $DIC */
99
100        $form = new ilPropertyFormGUI();
101
102        $form->setFormAction($DIC->ctrl()->getFormAction($this, self::CMD_SHOW_FORM));
103
104        if (!$this->hasRegistration()) {
105            $form->setTitle($DIC->language()->txt('form_create_registration'));
106            $form->addCommandButton(self::CMD_SAVE_FORM, $DIC->language()->txt('btn_create_registration'));
107        } else {
108            $form->setTitle($DIC->language()->txt('form_change_registration'));
109            $form->addCommandButton(self::CMD_SAVE_FORM, $DIC->language()->txt('btn_change_registration'));
110        }
111
112        $form->addCommandButton(self::CMD_CANCEL, $DIC->language()->txt('cancel'));
113
114        $userIdent = new ilEMailInputGUI($DIC->language()->txt('field_user_ident'), 'user_ident');
115        $userIdent->setInfo($DIC->language()->txt('field_user_ident_info'));
116        $userIdent->setRequired(true);
117        $userIdent->setValue($this->cmixUser->getUsrIdent());
118        $form->addItem($userIdent);
119
120        return $form;
121    }
122
123    protected function hasRegistration()
124    {
125        return strlen($this->cmixUser->getUsrIdent());
126    }
127
128    protected function saveRegistration(ilPropertyFormGUI $form)
129    {
130        $this->cmixUser->setUsrIdent($form->getInput('user_ident'));
131        $this->cmixUser->save();
132    }
133}
134