CourseHomeNotifyPlugin::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Framework\Container;
5
use Chamilo\PluginBundle\CourseHomeNotify\Entity\Notification;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Notification. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Chamilo\PluginBundle\CourseHomeNotify\Entity\NotificationRelUser;
7
use Doctrine\ORM\Tools\SchemaTool;
8
9
/**
10
 * Class CourseHomeNotifyPlugin.
11
 */
12
class CourseHomeNotifyPlugin extends Plugin
13
{
14
    public const SETTING_ENABLED = 'enabled';
15
16
    /**
17
     * CourseHomeNotifyPlugin constructor.
18
     */
19
    protected function __construct()
20
    {
21
        $settings = [];
22
23
        parent::__construct('0.1', 'Angel Fernando Quiroz Campos', $settings);
24
25
        $this->isCoursePlugin = true;
26
        $this->addCourseTool = false;
27
        $this->setCourseSettings();
28
    }
29
30
    /**
31
     * @return CourseHomeNotifyPlugin|null
32
     */
33
    public static function create()
34
    {
35
        static $result = null;
36
37
        return $result ? $result : $result = new self();
38
    }
39
40
    /**
41
     * Install process.
42
     * Create table in database. And setup Doctrine entity.
43
     *
44
     * @throws \Doctrine\ORM\Tools\ToolsException
45
     * @throws \Doctrine\DBAL\Exception
46
     */
47
    public function install()
48
    {
49
        $em = Database::getManager();
50
51
        if ($em->getConnection()->createSchemaManager()->tablesExist(['course_home_notify_notification'])) {
52
            return;
53
        }
54
55
        $schemaTool = new SchemaTool($em);
56
        $schemaTool->createSchema(
57
            [
58
                $em->getClassMetadata(Notification::class),
59
                $em->getClassMetadata(NotificationRelUser::class),
60
            ]
61
        );
62
    }
63
64
    /**
65
     * Uninstall process.
66
     * Remove Doctrine entity. And drop table in database.
67
     *
68
     * @throws \Doctrine\DBAL\Exception
69
     */
70
    public function uninstall()
71
    {
72
        $em = Database::getManager();
73
74
        if (!$em->getConnection()->createSchemaManager()->tablesExist(['course_home_notify_notification'])) {
75
            return;
76
        }
77
78
        $schemaTool = new SchemaTool($em);
79
        $schemaTool->dropSchema(
80
            [
81
                $em->getClassMetadata(Notification::class),
82
                $em->getClassMetadata(NotificationRelUser::class),
83
            ]
84
        );
85
    }
86
87
    /**
88
     * @param string $region
89
     *
90
     * @return string
91
     */
92
    public function renderRegion($region)
93
    {
94
        if (
95
            'main_bottom' !== $region
96
            || strpos($_SERVER['SCRIPT_NAME'], 'course_home/course_home.php') === false
97
        ) {
98
            return '';
99
        }
100
101
        $courseId = api_get_course_int_id();
102
        $userId = api_get_user_id();
103
104
        if (empty($courseId) || empty($userId)) {
105
            return '';
106
        }
107
108
        $course = api_get_course_entity($courseId);
109
        $user = api_get_user_entity($userId);
110
111
        $em = Database::getManager();
112
        /** @var Notification $notification */
113
        $notification = $em
114
            ->getRepository('ChamiloPluginBundle:CourseHomeNotify\Notification')
115
            ->findOneBy(['course' => $course]);
116
117
        if (!$notification) {
0 ignored issues
show
introduced by
$notification is of type Chamilo\PluginBundle\Cou...ify\Entity\Notification, thus it always evaluated to true.
Loading history...
118
            return '';
119
        }
120
121
        $modalFooter = '';
122
        $modalConfig = ['show' => true];
123
124
        if ($notification->getExpirationLink()) {
125
            /** @var NotificationRelUser $notificationUser */
126
            $notificationUser = $em
127
                ->getRepository('ChamiloPluginBundle:CourseHomeNotify\NotificationRelUser')
128
                ->findOneBy(['notification' => $notification, 'user' => $user]);
129
130
            if ($notificationUser) {
0 ignored issues
show
introduced by
$notificationUser is of type Chamilo\PluginBundle\Cou...ity\NotificationRelUser, thus it always evaluated to true.
Loading history...
131
                return '';
132
            }
133
134
            $contentUrl = api_get_path(WEB_PLUGIN_PATH).$this->get_name().'/content.php?hash='.$notification->getHash();
135
            $link = Display::toolbarButton(
136
                $this->get_lang('PleaseFollowThisLink'),
137
                $contentUrl,
138
                'external-link',
139
                'link',
140
                ['id' => 'course-home-notify-link', 'target' => '_blank']
141
            );
142
143
            $modalConfig['keyboard'] = false;
144
            $modalConfig['backdrop'] = 'static';
145
146
            $modalFooter = '<div class="modal-footer">'.$link.'</div>';
147
        }
148
149
        $modal = '<div id="course-home-notify-modal" class="modal" tabindex="-1" role="dialog">
150
            <div class="modal-dialog" role="document">
151
                <div class="modal-content">
152
                    <div class="modal-header">
153
                        <button type="button" class="close" data-dismiss="modal" aria-label="'.get_lang('Close').'">
154
                            <span aria-hidden="true">&times;</span>
155
                        </button>
156
                        <h4 class="modal-title">'.$this->get_lang('CourseNotice').'</h4>
157
                    </div>
158
                    <div class="modal-body">
159
                        '.$notification->getContent().'
160
                    </div>
161
                    '.$modalFooter.'
162
                </div>
163
            </div>
164
        </div>';
165
166
        $modal .= "<script>
167
            $(document).ready(function () {
168
                \$('#course-home-notify-modal').modal(".json_encode($modalConfig).");
169
170
                \$('#course-home-notify-link').on('click', function () {
171
                    $('#course-home-notify-modal').modal('hide');
172
                });
173
            });
174
        </script>";
175
176
        return $modal;
177
    }
178
179
    /**
180
     * Set the course settings.
181
     */
182
    private function setCourseSettings()
183
    {
184
        if (!Container::getPluginHelper()->isPluginEnabled($this->get_name())) {
185
            return;
186
        }
187
188
        $name = $this->get_name();
189
190
        $button = Display::toolbarButton(
191
            $this->get_lang('SetNotification'),
192
            api_get_path(WEB_PLUGIN_PATH).$name.'/configure.php?'.api_get_cidreq(),
193
            'cog',
194
            'primary'
195
        );
196
197
        $this->course_settings = [
198
            [
199
                'name' => '<p>'.$this->get_comment().'</p>'.$button.'<hr>',
200
                'type' => 'html',
201
            ],
202
        ];
203
    }
204
}
205