1434 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* local_moofactory_notification plugin
|
|
|
19 |
*
|
|
|
20 |
* @package local_moofactory_notification
|
|
|
21 |
* @copyright 2020 Patrick ROCHET <patrick.r@lmsfactory.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
require_once("../../config.php");
|
|
|
26 |
require_once($CFG->libdir . '/adminlib.php');
|
|
|
27 |
require_once('lib.php');
|
|
|
28 |
|
|
|
29 |
require_once('managenotif_form.php');
|
|
|
30 |
|
|
|
31 |
global $PAGE;
|
|
|
32 |
|
|
|
33 |
admin_externalpage_setup('local_moofactory_notification_managenotif');
|
|
|
34 |
|
|
|
35 |
$id = optional_param('id', 0, PARAM_INT); // Id de la notification.
|
|
|
36 |
|
|
|
37 |
$returnurl = new moodle_url($CFG->wwwroot . '/admin/category.php?category=moofactory_notification');
|
|
|
38 |
|
|
|
39 |
if (!empty($id)) {
|
|
|
40 |
$mform = new managenotif_form(null, array('id' => $id), 'post', '', array('id' => 'notificationsform'));
|
|
|
41 |
} else {
|
|
|
42 |
$mform = new managenotif_form(null, null, 'post', '', array('id' => 'notificationsform'));
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
if ($mform->is_cancelled()) {
|
|
|
46 |
redirect($returnurl);
|
|
|
47 |
} else if ($fromform = $mform->get_data()) {
|
|
|
48 |
$typeinitial = $fromform->typeinitial;
|
|
|
49 |
$type = $fromform->notificationtype;
|
|
|
50 |
|
|
|
51 |
// Les cours
|
|
|
52 |
$sql = "SELECT id, fullname FROM {course} WHERE id <> 1";
|
|
|
53 |
$courses = $DB->get_records_sql($sql, array());
|
|
|
54 |
|
|
|
55 |
// Changement de type de notification
|
|
|
56 |
$customfieldname = '';
|
|
|
57 |
if($type <> $typeinitial){
|
|
|
58 |
switch($typeinitial){
|
|
|
59 |
case "courseenroll":
|
|
|
60 |
$customfieldinitialname = 'courseenrollmentsnotification';
|
|
|
61 |
break;
|
|
|
62 |
case "courseaccess":
|
|
|
63 |
$customfieldinitialname = 'courseaccessnotification';
|
|
|
64 |
break;
|
|
|
65 |
case "courseevent":
|
|
|
66 |
$customfieldinitialname = 'courseeventsnotification';
|
|
|
67 |
break;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
// Notification par défaut et notification déplacée
|
|
|
71 |
$records = $DB->get_records('local_mf_notification', array('type'=>$typeinitial), 'base DESC, name ASC');
|
|
|
72 |
$index = 0;
|
|
|
73 |
foreach($records as $record) {
|
|
|
74 |
$index++;
|
|
|
75 |
if($record->base == 1){
|
|
|
76 |
$basenotif = $index;
|
|
|
77 |
}
|
|
|
78 |
if($record->id == $fromform->selectnotifications){
|
|
|
79 |
$movednotif = $index;
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
// Pour tous les cours
|
|
|
84 |
foreach ($courses as $course) {
|
|
|
85 |
$courseid = $course->id;
|
|
|
86 |
// La notification n'est plus dans le customfield initial, il faut réorganiser
|
|
|
87 |
$coursenotif = (int)local_moofactory_notification_getCustomfield($courseid, $customfieldinitialname, 'select');
|
|
|
88 |
|
|
|
89 |
// Si la notif du cours est celle qui change de type, il faut la remplacer par la notif de base
|
|
|
90 |
if($coursenotif == $movednotif){
|
|
|
91 |
local_moofactory_notification_setCustomfield($courseid, $customfieldinitialname, 'select', $basenotif);
|
|
|
92 |
}
|
|
|
93 |
// Sinon, il faut mettre le nouvel index si la notif qui change de type a un index inférieur à la notif du cours
|
|
|
94 |
elseif($coursenotif > $movednotif){
|
|
|
95 |
local_moofactory_notification_setCustomfield($courseid, $customfieldinitialname, 'select', $coursenotif - 1);
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// Il faut réorganiser le customfield de destination
|
|
|
100 |
switch($type){
|
|
|
101 |
case "courseenroll":
|
|
|
102 |
$customfieldname = 'courseenrollmentsnotification';
|
|
|
103 |
break;
|
|
|
104 |
case "courseaccess":
|
|
|
105 |
$customfieldname = 'courseaccessnotification';
|
|
|
106 |
break;
|
|
|
107 |
case "courseevent":
|
|
|
108 |
$customfieldname = 'courseeventsnotification';
|
|
|
109 |
break;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
if (!empty($fromform->submitbutton)) {
|
|
|
113 |
// Update de la notification.
|
|
|
114 |
$data = new stdClass;
|
|
|
115 |
$data->id = $fromform->selectnotifications;
|
|
|
116 |
$data->type = $type;
|
|
|
117 |
$data->name = $fromform->notificationname;
|
|
|
118 |
$data->subject = $fromform->notificationsubject;
|
|
|
119 |
$data->bodyhtml = $fromform->notificationbodyhtml['text'];
|
|
|
120 |
$DB->update_record('local_mf_notification', $data);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
// La nouvelle notification
|
|
|
124 |
$records = $DB->get_records('local_mf_notification', array('type'=>$type), 'base DESC, name ASC');
|
|
|
125 |
$newnotif = 0;
|
|
|
126 |
$index = 0;
|
|
|
127 |
foreach($records as $record) {
|
|
|
128 |
$index++;
|
|
|
129 |
if($record->id == $fromform->selectnotifications){
|
|
|
130 |
$newnotif = $index;
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
// Pour tous les cours
|
|
|
135 |
foreach ($courses as $course) {
|
|
|
136 |
$courseid = $course->id;
|
|
|
137 |
$coursenotif = (int)local_moofactory_notification_getCustomfield($courseid, $customfieldname, 'select');
|
|
|
138 |
|
|
|
139 |
if($coursenotif >= $newnotif){
|
|
|
140 |
local_moofactory_notification_setCustomfield($courseid, $customfieldname, 'select', $coursenotif + 1);
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
// Pas de changement de type
|
|
|
146 |
else{
|
|
|
147 |
switch($type){
|
|
|
148 |
case "courseenroll":
|
|
|
149 |
$customfieldname = 'courseenrollmentsnotification';
|
|
|
150 |
break;
|
|
|
151 |
case "courseaccess":
|
|
|
152 |
$customfieldname = 'courseaccessnotification';
|
|
|
153 |
break;
|
|
|
154 |
case "courseevent":
|
|
|
155 |
$customfieldname = 'courseeventsnotification';
|
|
|
156 |
break;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
// Position avant update
|
|
|
160 |
$indexbefore = 0;
|
|
|
161 |
$records = $DB->get_records('local_mf_notification', array('type'=>$type), 'base DESC, name ASC');
|
|
|
162 |
$index = 0;
|
|
|
163 |
foreach($records as $record) {
|
|
|
164 |
$index++;
|
|
|
165 |
if($record->id == $fromform->selectnotifications){
|
|
|
166 |
$indexbefore = $index;
|
|
|
167 |
}
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
if (!empty($fromform->submitbutton)) {
|
|
|
171 |
// Update de la notification.
|
|
|
172 |
$data = new stdClass;
|
|
|
173 |
$data->id = $fromform->selectnotifications;
|
|
|
174 |
$data->type = $fromform->notificationtype;
|
|
|
175 |
$data->name = $fromform->notificationname;
|
|
|
176 |
$data->subject = $fromform->notificationsubject;
|
|
|
177 |
$data->bodyhtml = $fromform->notificationbodyhtml['text'];
|
|
|
178 |
$DB->update_record('local_mf_notification', $data);
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
// Position après update
|
|
|
182 |
$indexafter = 0;
|
|
|
183 |
$records = $DB->get_records('local_mf_notification', array('type'=>$type), 'base DESC, name ASC');
|
|
|
184 |
$index = 0;
|
|
|
185 |
foreach($records as $record) {
|
|
|
186 |
$index++;
|
|
|
187 |
if($record->id == $fromform->selectnotifications){
|
|
|
188 |
$indexafter = $index;
|
|
|
189 |
}
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
foreach ($courses as $course) {
|
|
|
193 |
$courseid = $course->id;
|
|
|
194 |
$coursenotif = (int)local_moofactory_notification_getCustomfield($courseid, $customfieldname, 'select');
|
|
|
195 |
|
|
|
196 |
if($indexafter != $indexbefore){
|
|
|
197 |
if($coursenotif < $indexbefore && $coursenotif >= $indexafter){
|
|
|
198 |
local_moofactory_notification_setCustomfield($courseid, $customfieldname, 'select', $coursenotif + 1);
|
|
|
199 |
}
|
|
|
200 |
if($coursenotif > $indexbefore && $coursenotif <= $indexafter){
|
|
|
201 |
local_moofactory_notification_setCustomfield($courseid, $customfieldname, 'select', $coursenotif - 1);
|
|
|
202 |
}
|
|
|
203 |
if($coursenotif == $indexbefore){
|
|
|
204 |
$offset = $indexafter - $indexbefore;
|
|
|
205 |
local_moofactory_notification_setCustomfield($courseid, $customfieldname, 'select', $coursenotif + $offset);
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
$nexturl = new moodle_url($CFG->wwwroot . '/local/moofactory_notification/managenotif.php', array('id' => $fromform->selectnotifications));
|
|
|
212 |
// Typically you finish up by redirecting to somewhere where the user
|
|
|
213 |
// can see what they did.
|
|
|
214 |
redirect($nexturl);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
echo $OUTPUT->header();
|
|
|
218 |
echo $OUTPUT->heading(get_string('managenotif', 'local_moofactory_notification'), 2);
|
|
|
219 |
|
|
|
220 |
$mform->display();
|
|
|
221 |
|
|
|
222 |
$PAGE->requires->string_for_js('copied', 'local_moofactory_notification');
|
|
|
223 |
$PAGE->requires->js('/local/moofactory_notification/util.js');
|
|
|
224 |
echo $OUTPUT->footer();
|