Passed
Push — 1.11.x ( 1a2284...d20441 )
by Angel Fernando Quiroz
11:23
created

CourseHomeNotifyPlugin::install()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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