Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Delete notification.
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('deletenotif_form.php');
28
require_once('lib.php');
29
 
30
admin_externalpage_setup('local_moofactory_notification_managenotif');
31
 
32
$id = optional_param('id', 0, PARAM_INT); // Id de la notification à supprimer.
33
 
34
$returnurl = new moodle_url($CFG->wwwroot . '/local/moofactory_notification/managenotif.php', array('id' => $id));
35
$nexturl = new moodle_url($CFG->wwwroot . '/local/moofactory_notification/managenotif.php');
36
 
37
if (!empty($id)) {
38
    $mform = new deletenotif_form( null, array('id' => $id));
39
} else {
40
    redirect($nexturl);
41
}
42
 
43
if ($mform->is_cancelled()) {
44
    redirect($returnurl);
45
 
46
} else if ($fromform = $mform->get_data()) {
47
    // On recherche la notif définie au niveau des paramètres du plugin correspondant au type de la notif supprimée.
48
    $record = $DB->get_record('local_mf_notification', array('id'=>$fromform->id));
49
    $type = $record->type;
50
    switch($type){
51
        case "courseenroll":
52
            $notifvalue = get_config('local_moofactory_notification', 'coursesenrollmentsnotification');
53
            $customfieldname = 'courseenrollmentsnotification';
54
            break;
55
        case "courseaccess":
56
            $notifvalue = get_config('local_moofactory_notification', 'coursesaccessnotification');
57
            $customfieldname = 'courseaccessnotification';
58
            break;
59
        case "courseevent":
60
            $notifvalue = get_config('local_moofactory_notification', 'courseseventsnotification');
61
            $customfieldname = 'courseeventsnotification';
62
            break;
63
    }
64
    // Si c'est la même, il faut remplacer par la notif de base dans les paramètres du plugin.
65
    if($notifvalue == $fromform->id){
66
        $record = $DB->get_record('local_mf_notification', array('type'=>$type, 'base'=>1));
67
        switch($type){
68
            case "courseenroll":
69
                set_config('coursesenrollmentsnotification', $record->id, 'local_moofactory_notification');
70
                break;
71
            case "courseaccess":
72
                set_config('coursesaccessnotification', $record->id, 'local_moofactory_notification');
73
                break;
74
            case "courseevent":
75
                set_config('courseseventsnotification', $record->id, 'local_moofactory_notification');
76
                break;
77
        }
78
    }
79
 
80
    // Notification par défaut et notification supprimée.
81
    $records = $DB->get_records('local_mf_notification', array('type'=>$type), 'base DESC, name ASC');
82
    $index = 0;
83
    foreach($records as $record) {
84
        $index++;
85
        if($record->base == 1){
86
            $basenotifid = $record->id;
87
            $basenotif = $index;
88
        }
89
        if($record->id == $fromform->id){
90
            $deletednotif = $index;
91
        }
92
    }
93
 
94
    // Pour tous les cours.
95
    $sql = "SELECT id, fullname FROM {course} WHERE id <> 1";
96
    $courses = $DB->get_records_sql($sql, array());
97
    foreach ($courses as $course) {
98
        $courseid = $course->id;
99
        $coursenotif = (int)local_moofactory_notification_getCustomfield($courseid, $customfieldname, 'select');
100
 
101
        // Si la notif du cours est celle qui est supprimée, il faut la remplacer par la notif de base.
102
        if($coursenotif == $deletednotif){
103
            local_moofactory_notification_setCustomfield($courseid, $customfieldname, 'select', $basenotif);
104
        }
105
        // Sinon, il faut mettre le nouvel index si la notif supprimée a un index inférieur à la notif du cours.
106
        elseif($coursenotif > $deletednotif){
107
            local_moofactory_notification_setCustomfield($courseid, $customfieldname, 'select', $coursenotif - 1);
108
        }
109
 
110
        // Pour les activités.
111
        $activities = local_moofactory_notification_get_all_activities($courseid);
112
 
113
        foreach($activities as $activity){
114
            $moduleid = $activity["id"];
115
            $name = 'modulenotification_'.$courseid.'_'.$moduleid;
116
            $notifid = get_config('local_moofactory_notification', $name);
117
 
118
            // Si la notif de l'activité est celle qui est supprimée, il faut la remplacer par la notif de base.
119
            if($fromform->id == $notifid){
120
                set_config($name, $basenotifid, 'local_moofactory_notification');
121
            }
122
        }
123
    }
124
 
125
    $DB->delete_records('local_mf_notification', array('id' => $fromform->id));
126
 
127
    // Typically you finish up by redirecting to somewhere where the user can see what they did.
128
    redirect($nexturl);
129
}
130
 
131
echo $OUTPUT->header();
132
 
133
echo $OUTPUT->heading(get_string('deletenotification', 'local_moofactory_notification'), 2);
134
$mform->display();
135
 
136
echo $OUTPUT->footer();