Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Notificationeabc enrolment plugin.
19
 *
20
 * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment)
21
 *
22
 * @package    enrol_notificationeabc
23
 * @copyright  2017 e-ABC Learning
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @author     Osvaldo Arriola <osvaldo@e-abclearning.com>
26
 */
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
require_once($CFG->dirroot . '/lib/moodlelib.php');
31
 
32
/**
33
 * Lib class
34
 *
35
 * @package    enrol_notificationeabc
36
 * @copyright  2017 e-ABC Learning
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @author     Osvaldo Arriola <osvaldo@e-abclearning.com>
39
 */
40
class enrol_notificationeabc_plugin extends enrol_plugin
41
{
42
    /** @var log.*/
43
    private $log = '';
44
 
45
    // Funcion que envia las notificaciones a los usuarios.
46
 
47
    /**
48
     *  Send mail method
49
     * @param stdClass $user instancia usuario
50
     * @param stdClass $course instancia curso
51
     * @param int $type aviso matriculacion, actualizacion o desmatriculacion
52
     * @return bool
53
     */
54
    public function enviarmail(stdClass $user, stdClass $course, $type) {
55
        global $CFG, $DB, $COURSE;
56
 
57
        $course->url = $CFG->wwwroot . '/course/view.php?id=' . $course->id;
58
 
59
        $enrol = $DB->get_record('enrol', array('enrol' => 'notificationeabc', 'courseid' => $course->id, 'status' => 0));
60
        $activeglobalenrol = $this->get_config('activarglobal');
61
        $activarglobalunenrolalert = $this->get_config('activarglobalunenrolalert');
62
        $activarglobalenrolupdated = $this->get_config('activarglobalenrolupdated');
63
        $mensajeenrol = $this->get_config('location');
64
        $plainmensajeenrol = strip_tags($mensajeenrol);
65
        $mensajeunenrol = $this->get_config('unenrolmessage');
66
        $plainmensajeunenrol = strip_tags($mensajeunenrol);
67
        $mensajeupdateenrol = $this->get_config('updatedenrolmessage');
68
        $plainmensajeupdateenrol = strip_tags($mensajeupdateenrol);
69
 
70
        switch ((int)$type) {
71
            case 1:
72
                // Si no se configuro un mensaje personalizado se envia uno por defecto basico.
73
                if (!empty($enrol) && !empty($enrol->customtext1)) {
74
                    $texto = strip_tags($enrol->customtext1);
75
                    if (!empty($texto)) {
76
                        $mensaje = $this->process_mensaje($enrol->customtext1, $user, $course);
77
                    } else {
78
                        $mensaje = get_string("filelockedmail", "enrol_notificationeabc", $course);
79
                    }
80
 
81
                } else if (!empty($activeglobalenrol) && !empty($plainmensajeenrol)) {
82
                    $mensaje = $this->process_mensaje($mensajeenrol, $user, $course);
83
                } else {
84
                    $mensaje = get_string("filelockedmail", "enrol_notificationeabc", $course);
85
                }
86
                break;
87
            case 2:
88
 
89
                if (!empty($enrol) && !empty($enrol->customtext2)) {
90
                    $texto = strip_tags($enrol->customtext2);
91
                    if (!empty($texto)) {
92
                        $mensaje = $this->process_mensaje($enrol->customtext2, $user, $course);
93
                    } else {
94
                        $mensaje = get_string("unenrolmessagedefault", "enrol_notificationeabc", $course);
95
                    }
96
 
97
                } else if (!empty($activarglobalunenrolalert) && !empty($plainmensajeunenrol)) {
98
                    $mensaje = $this->process_mensaje($mensajeunenrol, $user, $course);
99
                } else {
100
                    $mensaje = get_string("unenrolmessagedefault", "enrol_notificationeabc", $course);
101
                }
102
                break;
103
            case 3:
104
 
105
                if (!empty($enrol) && !empty($enrol->customtext3)) {
106
                    $texto = strip_tags($enrol->customtext3);
107
                    if (!empty($texto)) {
108
                        $mensaje = $this->process_mensaje($enrol->customtext3, $user, $course);
109
                    } else {
110
                        $mensaje = get_string("updatedenrolmessagedefault", "enrol_notificationeabc", $course);
111
                    }
112
 
113
                } else if (!empty($activarglobalenrolupdated) && !empty($plainmensajeupdateenrol)) {
114
                    $mensaje = $this->process_mensaje($mensajeupdateenrol, $user, $course);
115
                } else {
116
                    $mensaje = get_string("updatedenrolmessagedefault", "enrol_notificationeabc", $course);
117
                }
118
                break;
119
 
120
            default:
121
                break;
122
        }
123
 
124
        $soporte = core_user::get_support_user();
125
 
126
        $sender = get_admin();
127
 
128
        if (!empty($enrol) && !empty($enrol->customchar1)) {
129
            $sender->email = $enrol->customchar1;
130
        } else {
131
            $sender->email = $soporte->email;
132
        }
133
 
134
        if (!empty($enrol) && !empty($enrol->customchar2)) {
135
            $sender->firstname = $enrol->customchar2;
136
        } else {
137
            $sender->firstname = $soporte->firstname;
138
        }
139
 
140
        $sender->lastname = $soporte->lastname;
141
 
142
        $eventdata = new \core\message\message();
143
        $eventdata->courseid = $course->id;
144
        $eventdata->modulename = 'moodle';
145
        $eventdata->component = 'enrol_notificationeabc';
146
        $eventdata->name = 'notificationeabc_enrolment';
147
        $eventdata->userfrom = $sender;
148
        $eventdata->userto = $user->id;
149
        $eventdata->subject = get_string('subject', 'enrol_notificationeabc');
150
        $eventdata->fullmessage = '';
151
        $eventdata->fullmessageformat = FORMAT_HTML;
152
        $eventdata->fullmessagehtml = $mensaje;
153
        $eventdata->smallmessage = '';
154
        $strdata = new stdClass();
155
        $strdata->username = $user->username;
156
        $strdata->coursename = $course->fullname;
157
        if (message_send($eventdata)) {
158
            $this->log .= get_string('succefullsend', 'enrol_notificationeabc', $strdata);
159
            return true;
160
        } else {
161
            $this->log .= get_string('failsend', 'enrol_notificationeabc', $strdata);
162
            return false;
163
        }
164
    } // End of function.
165
 
166
    // Procesa el mensaje para aceptar marcadores.
167
    /**
168
     * Proccess message method
169
     * @param String $mensaje el mensaje en bruto
170
     * @param stdClass $user instancia usuario
171
     * @param stdClass $course instancia curso
172
     * @return String el mensaje procesado
173
     */
174
    public function process_mensaje($mensaje, stdClass $user, stdClass $course) {
175
        global $CFG;
176
        $m = $mensaje;
177
        $url = new moodle_url($CFG->wwwroot . '/course/view.php', array('id' => $course->id));
178
        $m = str_replace('{COURSENAME}', $course->fullname, $m);
179
        $m = str_replace('{USERNAME}', $user->username, $m);
180
        $m = str_replace('{NOMBRE}', $user->firstname, $m);
181
        $m = str_replace('{APELLIDO}', $user->lastname, $m);
182
        $m = str_replace('{URL}', $url, $m);
183
        return $m;
184
    }
185
 
186
 
187
    /**
188
     * Returns link to page which may be used to add new instance of enrolment plugin in course.
189
     * @param int $courseid
190
     * @return moodle_url page url
191
     */
192
    public function get_newinstance_link($courseid) {
193
        global $DB;
194
        $numenrol = $DB->count_records('enrol', array('courseid' => $courseid, 'enrol' => 'notificationeabc'));
195
        $context = context_course::instance($courseid, MUST_EXIST);
196
 
197
        if (!has_capability('enrol/notificationeabc:manage', $context) or $numenrol >= 1) {
198
            return null;
199
        }
200
        return new moodle_url('/enrol/notificationeabc/edit.php', array('courseid' => $courseid));
201
    }
202
 
203
 
204
    /**
205
     * Returns defaults for new instances.
206
     * @return array
207
     */
208
    public function get_instance_defaults() {
209
 
210
        $fields = array();
211
        $fields['customtext1'] = $this->get_config('location');
212
        $fields['customtext2'] = $this->get_config('unenrolmessage');
213
        $fields['customtext3'] = $this->get_config('updatedenrolmessage');
214
        $fields['status'] = 1;
215
        $fields['customint1'] = 0;
216
        $fields['customint2'] = 0;
217
        $fields['customint3'] = $this->get_config('activeenrolalert');
218
        $fields['customint4'] = $this->get_config('activeunenrolalert');
219
        $fields['customint5'] = $this->get_config('activeenrolupdatedalert');
220
        $fields['customchar1'] = $this->get_config('emailsender');
221
        $fields['customchar2'] = $this->get_config('namesender');
222
 
223
        return $fields;
224
    }
225
 
226
    /**
227
     * Get icons
228
     * @param stdClass $instance
229
     * @return array
230
     * @throws coding_exception
231
     */
232
    public function get_action_icons(stdClass $instance) {
233
        global $OUTPUT;
234
 
235
        if ($instance->enrol !== 'notificationeabc') {
236
            throw new coding_exception('invalid enrol instance!');
237
        }
238
        $context = context_course::instance($instance->courseid);
239
 
240
        $icons = array();
241
 
242
        if (has_capability('enrol/notificationeabc:manage', $context)) {
243
            $editlink = new moodle_url(
244
                "/enrol/notificationeabc/edit.php",
245
                array('courseid' => $instance->courseid, 'id' => $instance->id)
246
            );
247
            $icons[] = $OUTPUT->action_icon(
248
                $editlink,
249
                new pix_icon(
250
                    'i/edit',
251
                    get_string('edit'),
252
                    'core',
253
                    array('class' => 'icon')
254
                )
255
            );
256
        }
257
 
258
        return $icons;
259
    }
260
 
261
 
262
    /**
263
     * get info icons method
264
     * @param array $instances
265
     * @return array
266
     */
267
    public function get_info_icons(array $instances) {
268
        $icons = array();
269
        $icons[] = new pix_icon('email', get_string('pluginname', 'enrol_notificationeabc'), 'enrol_notificationeabc');
270
        return $icons;
271
    }
272
 
273
} // End of class.
274