Passed
Push — master ( 13109f...2670e4 )
by Angel Fernando Quiroz
09:35
created

CourseHomeNotifyPlugin   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 78
c 0
b 0
f 0
dl 0
loc 198
rs 10
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_name() 0 3 1
A install() 0 13 2
A __construct() 0 11 1
B renderRegion() 0 85 8
A setCourseSettings() 0 19 2
A create() 0 5 2
A uninstall() 0 13 2
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
    public 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
     * @throws \Doctrine\DBAL\Exception
47
     */
48
    public function install()
49
    {
50
        $em = Database::getManager();
51
52
        if ($em->getConnection()->createSchemaManager()->tablesExist(['course_home_notify_notification'])) {
53
            return;
54
        }
55
56
        $schemaTool = new SchemaTool($em);
57
        $schemaTool->createSchema(
58
            [
59
                $em->getClassMetadata(Notification::class),
60
                $em->getClassMetadata(NotificationRelUser::class),
61
            ]
62
        );
63
    }
64
65
    /**
66
     * Uninstall process.
67
     * Remove Doctrine entity. And drop table in database.
68
     *
69
     * @throws \Doctrine\DBAL\Exception
70
     */
71
    public function uninstall()
72
    {
73
        $em = Database::getManager();
74
75
        if (!$em->getConnection()->createSchemaManager()->tablesExist(['course_home_notify_notification'])) {
76
            return;
77
        }
78
79
        $schemaTool = new SchemaTool($em);
80
        $schemaTool->dropSchema(
81
            [
82
                $em->getClassMetadata(Notification::class),
83
                $em->getClassMetadata(NotificationRelUser::class),
84
            ]
85
        );
86
    }
87
88
    /**
89
     * @param string $region
90
     *
91
     * @return string
92
     */
93
    public function renderRegion($region)
94
    {
95
        if (
96
            'main_bottom' !== $region
97
            || strpos($_SERVER['SCRIPT_NAME'], 'course_home/course_home.php') === false
98
        ) {
99
            return '';
100
        }
101
102
        $courseId = api_get_course_int_id();
103
        $userId = api_get_user_id();
104
105
        if (empty($courseId) || empty($userId)) {
106
            return '';
107
        }
108
109
        $course = api_get_course_entity($courseId);
110
        $user = api_get_user_entity($userId);
111
112
        $em = Database::getManager();
113
        /** @var Notification $notification */
114
        $notification = $em
115
            ->getRepository('ChamiloPluginBundle:CourseHomeNotify\Notification')
116
            ->findOneBy(['course' => $course]);
117
118
        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...
119
            return '';
120
        }
121
122
        $modalFooter = '';
123
        $modalConfig = ['show' => true];
124
125
        if ($notification->getExpirationLink()) {
126
            /** @var NotificationRelUser $notificationUser */
127
            $notificationUser = $em
128
                ->getRepository('ChamiloPluginBundle:CourseHomeNotify\NotificationRelUser')
129
                ->findOneBy(['notification' => $notification, 'user' => $user]);
130
131
            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...
132
                return '';
133
            }
134
135
            $contentUrl = api_get_path(WEB_PLUGIN_PATH).$this->get_name().'/content.php?hash='.$notification->getHash();
136
            $link = Display::toolbarButton(
137
                $this->get_lang('PleaseFollowThisLink'),
138
                $contentUrl,
139
                'external-link',
140
                'link',
141
                ['id' => 'course-home-notify-link', 'target' => '_blank']
142
            );
143
144
            $modalConfig['keyboard'] = false;
145
            $modalConfig['backdrop'] = 'static';
146
147
            $modalFooter = '<div class="modal-footer">'.$link.'</div>';
148
        }
149
150
        $modal = '<div id="course-home-notify-modal" class="modal" tabindex="-1" role="dialog">
151
            <div class="modal-dialog" role="document">
152
                <div class="modal-content">
153
                    <div class="modal-header">
154
                        <button type="button" class="close" data-dismiss="modal" aria-label="'.get_lang('Close').'">
155
                            <span aria-hidden="true">&times;</span>
156
                        </button>
157
                        <h4 class="modal-title">'.$this->get_lang('CourseNotice').'</h4>
158
                    </div>
159
                    <div class="modal-body">
160
                        '.$notification->getContent().'
161
                    </div>
162
                    '.$modalFooter.'
163
                </div>
164
            </div>
165
        </div>';
166
167
        $modal .= "<script>
168
            $(document).ready(function () {
169
                \$('#course-home-notify-modal').modal(".json_encode($modalConfig).");
170
                
171
                \$('#course-home-notify-link').on('click', function () {
172
                    $('#course-home-notify-modal').modal('hide');
173
                });
174
            });
175
        </script>";
176
177
        return $modal;
178
    }
179
180
    /**
181
     * Set the course settings.
182
     */
183
    private function setCourseSettings()
184
    {
185
        if ('true' !== $this->get(self::SETTING_ENABLED)) {
186
            return;
187
        }
188
189
        $name = $this->get_name();
190
191
        $button = Display::toolbarButton(
192
            $this->get_lang('SetNotification'),
193
            api_get_path(WEB_PLUGIN_PATH).$name.'/configure.php?'.api_get_cidreq(),
194
            'cog',
195
            'primary'
196
        );
197
198
        $this->course_settings = [
199
            [
200
                'name' => '<p>'.$this->get_comment().'</p>'.$button.'<hr>',
201
                'type' => 'html',
202
            ],
203
        ];
204
    }
205
206
    public function get_name()
207
    {
208
        return 'CourseHomeNotify';
209
    }
210
}
211