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
 * Mailing Class
19
 *
20
 * @package    mod_custommailing
21
 * @author     olivier@cblue.be
22
 * @copyright  2021 CBlue SPRL
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_custommailing;
27
 
28
use dml_exception;
29
use stdClass;
30
 
31
defined('MOODLE_INTERNAL') || die();
32
 
33
global $CFG;
34
 
35
require_once $CFG->dirroot . '/mod/custommailing/lib.php';
36
 
37
/**
38
 * Class Mailing
39
 * @package mod_custommailing
40
 *
41
 * @property int id
42
 * @property int custommailingid
43
 * @property string mailingname
44
 * @property string mailinglang
45
 * @property string mailingsubject
46
 * @property string mailingcontent
47
 * @property int mailingcontentformat
48
 * @property int mailingmodle
49
 * @property int mailingdelay
50
 * @property int mailingstatus
51
 * @property int targetmoduleid
52
 * @property int customcertmoduleid
53
 * @property int starttime
54
 * @property int timecreated
55
 * @property int timemodified
56
 */
57
class Mailing {
58
 
59
    /**
60
     * @param stdClass $record
61
     * @return bool|int
62
     * @throws dml_exception
63
     */
64
    public static function create(stdClass $record) {
65
        global $DB;
66
 
67
        $record->timecreated = $record->timemodified = time();
68
 
69
        return $DB->insert_record('custommailing_mailing', $record);
70
    }
71
 
72
    /**
73
     * @param int $id
74
     * @return stdClass
75
     * @throws dml_exception
76
     */
77
    public static function get(int $id) {
78
        global $DB;
79
 
80
        $record = $DB->get_record('custommailing_mailing', ['id' => $id], '*', MUST_EXIST);
81
        $record = Mailing::format($record);
82
 
83
 
84
        return $record;
85
    }
86
 
87
    /**
88
     * @param int $id
89
     * @return stdClass
90
     * @throws dml_exception
91
     */
92
    public static function getWithCourse(int $id) {
93
        global $DB;
94
 
95
        $sql = "SELECT cm.*, c.course as courseid
96
                FROM {custommailing_mailing} cm
97
                JOIN {custommailing} c ON c.id = cm.custommailingid
98
                ";
99
        $record = $DB->get_record_sql($sql, ['id' => $id]);
100
        $record = Mailing::format($record);
101
 
102
        return $record;
103
    }
104
 
105
 
106
    /**
107
     * @param int $custommailing_id
108
     * @return stdClass[]
109
     * @throws dml_exception
110
     */
111
    public static function getAll(int $custommailing_id) {
112
        global $DB;
113
 
114
        $records = [];
115
        $rs = $DB->get_recordset('custommailing_mailing', ['custommailingid' => $custommailing_id], 'id ASC');
116
        foreach ($rs as $record) {
117
            $record = Mailing::format($record);
118
            $records[$record->id] = $record;
119
        }
120
        $rs->close();
121
 
122
        return $records;
123
    }
124
 
125
    /**
126
     * @param stdClass $record
127
     * @return bool
128
     * @throws dml_exception
129
     */
130
    public static function update(stdClass $record) {
131
        global $DB;
132
 
133
        $record->timemodified = time();
134
 
135
        return $DB->update_record('custommailing_mailing', $record);
136
    }
137
 
138
    /**
139
     * @param int $id
140
     * @throws dml_exception
141
     */
142
    public static function delete(int $id) {
143
        global $DB;
144
 
145
        $DB->delete_records('custommailing_logs', ['custommailingmailingid' => $id]);
146
        $DB->delete_records('custommailing_mailing', ['id' => $id]);
147
    }
148
 
149
    /**
150
     * @param int $custommailing_id
151
     * @throws dml_exception
152
     */
153
    public static function deleteAll(int $custommailing_id) {
154
        foreach (Mailing::getAll($custommailing_id) as $mailing) {
155
            Mailing::delete($mailing->id);
156
        }
157
    }
158
 
159
    /**
160
     * @return array
161
     * @throws dml_exception
162
     */
163
    public static function getAllToSend() {
164
        global $DB;
165
 
166
        $records = [];
167
        $sql = "SELECT cm.*, c.course as courseid
168
                FROM {custommailing_mailing} cm
169
                JOIN {custommailing} c ON c.id = cm.custommailingid
170
                ";
171
        $rs = $DB->get_recordset_sql($sql, ['mailingstatus' => MAILING_STATUS_ENABLED]);
172
        foreach ($rs as $record) {
173
            $record = Mailing::format($record);
174
            $records[$record->id] = $record;
175
        }
176
        $rs->close();
177
 
178
        return $records;
179
    }
180
 
181
    /**
182
     * @param stdClass $record
183
     * @return stdClass
184
     */
185
    protected static function format(stdClass $record) {
186
        $record->id = (int) $record->id;
187
        $record->custommailingid = (int) $record->custommailingid;
188
        $record->mailingcontentformat = (int) $record->mailingcontentformat;
189
        $record->mailingmode = (int) $record->mailingmode;
190
        $record->mailingdelay = (int) $record->mailingdelay;
191
        $record->mailingstatus = (bool) $record->mailingstatus;
192
        $record->retroactive = (bool) $record->retroactive;
193
        $record->targetmoduleid = (int) $record->targetmoduleid;
194
        $record->targetmodulestatus = (int) $record->targetmodulestatus;
195
        $record->customcertmoduleid = empty($record->customcertmoduleid) ? null : (int) $record->customcertmoduleid;
196
        $record->starttime = (int) $record->starttime;
197
        $record->timecreated = (int) $record->timecreated;
198
        $record->timemodified = (int) $record->timemodified;
199
 
200
        return $record;
201
    }
202
}