Issues (2160)

plugin/createdrupaluser/src/CreateDrupalUser.php (2 issues)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Create a user in Drupal website when a user is registered in Chamilo LMS.
6
 *
7
 * @author Angel Fernando Quiroz Campos <[email protected]>
8
 *
9
 * @package chamilo.plugin.createDrupalUser
10
 */
11
class CreateDrupalUser extends Plugin implements HookPluginInterface
12
{
13
    public const EXTRAFIELD_VARIABLE_NAME = 'drupal_user_id';
14
15
    /**
16
     * Class constructor.
17
     */
18
    protected function __construct()
19
    {
20
        $parameters = [
21
            'drupal_domain' => 'text',
22
        ];
23
24
        parent::__construct('1.0', 'Angel Fernando Quiroz Campos', $parameters);
25
    }
26
27
    /**
28
     * Instance the plugin.
29
     *
30
     * @staticvar null $result
31
     *
32
     * @return CreateDrupalUser
33
     */
34
    public static function create()
35
    {
36
        static $result = null;
37
38
        return $result ? $result : $result = new self();
39
    }
40
41
    /**
42
     * Install the plugin.
43
     */
44
    public function install()
45
    {
46
        $this->createExtraField();
47
        $this->installHook();
48
    }
49
50
    /**
51
     * Uninstall the plugin.
52
     */
53
    public function uninstall()
54
    {
55
        $this->uninstallHook();
56
        $this->deleteExtraField();
57
    }
58
59
    /**
60
     * Install the Create User hook.
61
     */
62
    public function installHook()
63
    {
64
        /** @var HookCreateDrupalUser $observer */
65
        $observer = HookCreateDrupalUser::create();
66
        HookCreateUser::create()->attach($observer);
67
    }
68
69
    /**
70
     * Uninstall the Create User hook.
71
     */
72
    public function uninstallHook()
73
    {
74
        /** @var HookCreateDrupalUser $observer */
75
        $observer = HookCreateDrupalUser::create();
76
        $event = HookCreateUser::create();
77
78
        if ($event) {
0 ignored issues
show
$event is of type HookCreateUser, thus it always evaluated to true.
Loading history...
79
            $event->detach($observer);
80
        }
81
    }
82
83
    /**
84
     * Get the drupal_user_id extra field information.
85
     *
86
     * @return array The info
87
     */
88
    private function getExtraFieldInfo()
89
    {
90
        $extraField = new ExtraField('user');
91
        $extraFieldHandler = $extraField->get_handler_field_info_by_field_variable(
92
            self::EXTRAFIELD_VARIABLE_NAME
93
        );
94
95
        return $extraFieldHandler;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $extraFieldHandler also could return the type boolean which is incompatible with the documented return type array.
Loading history...
96
    }
97
98
    /**
99
     * Create the drupal_user_id when it not exists.
100
     */
101
    private function createExtraField()
102
    {
103
        $extraFieldExists = $this->getExtraFieldInfo() !== false;
104
105
        if (!$extraFieldExists) {
106
            $extraField = new ExtraField('user');
107
            $extraField->save(
108
                [
109
                    'field_type' => ExtraField::FIELD_TYPE_INTEGER,
110
                    'variable' => self::EXTRAFIELD_VARIABLE_NAME,
111
                    'display_text' => get_plugin_lang('DrupalUserId', 'CreateDrupalUser'),
112
                    'default_value' => null,
113
                    'field_order' => null,
114
                    'visible_to_self' => false,
115
                    'changeable' => false,
116
                    'filter' => null,
117
                ]
118
            );
119
        }
120
    }
121
122
    /**
123
     * Delete the drupal_user_id and values.
124
     */
125
    private function deleteExtraField()
126
    {
127
        $extraFieldInfo = $this->getExtraFieldInfo();
128
        $extraFieldExists = $extraFieldInfo !== false;
129
130
        if ($extraFieldExists) {
131
            $extraField = new ExtraField('user');
132
            $extraField->delete($extraFieldInfo['id']);
133
        }
134
    }
135
136
    /*
137
    public function notifyDocumentAction(HookDocumentActionEventInterface $hook)
138
    {
139
        $data = $hook->getEventData();
140
        if ($data['type'] === HOOK_EVENT_TYPE_PRE) {
141
            $data['actions'][] = Display::return_icon('edit.png');
142
        }
143
144
        return $data;
145
    }
146
147
    public function notifyDocumentItemAction(HookDocumentItemActionEventInterface $hook)
148
    {
149
        $data = $hook->getEventData();
150
        if ($data['type'] === HOOK_EVENT_TYPE_PRE) {
151
            $data['actions'][] = $data['id'].' - '.$data['title'];
152
        }
153
154
        return $data;
155
    }*/
156
}
157