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
 * MailingLog 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 core_h5p\core;
29
use dml_exception;
30
use stdClass;
31
 
32
defined('MOODLE_INTERNAL') || die();
33
 
34
global $CFG;
35
 
36
require_once $CFG->dirroot . '/mod/custommailing/lib.php';
37
 
38
/**
39
 * Class Mailing
40
 * @package mod_custommailing
41
 */
42
class MailingLog
43
{
44
    /**
45
     * @param stdClass $record
46
     * @return bool|int
47
     * @throws dml_exception
48
     */
49
    public static function create(stdClass $record) {
50
        global $DB;
51
 
52
        $record->timecreated = $record->timemodified = time();
53
 
54
        return $DB->insert_record('custommailing_logs', $record);
55
    }
56
 
57
    /**
58
     * @param null|array $conditions
59
     * @return array
60
     * @throws dml_exception
61
     */
62
    public static function getAll($conditions = null) {
63
        global $DB;
64
 
65
        $records = [];
66
        $rs = $DB->get_recordset('custommailing_logs', $conditions, 'ORDER BY id');
67
        foreach ($rs as $record) {
68
            $record = MailingLog::format($record);
69
            $records[$record->id] = $record;
70
        }
71
        $rs->close();
72
 
73
        return $records;
74
    }
75
 
76
    /**
77
     * @param int $custommailingid
78
     * @return array
79
     * @throws dml_exception
80
     */
81
    public static function getAllForTable(int $custommailingid) {
82
        global $DB;
83
 
84
        $user_name_fields_raw = \core_user\fields::get_name_fields();
85
        foreach ($user_name_fields_raw as $field) {
86
            $field = 'u.' . $field . ' as user_' . $field;
87
        }
88
        $user_name_fields = implode(', ', $user_name_fields_raw);
89
        $records = [];
90
        $sql = "SELECT cl.id, cm.mailingname, u.id AS user_id, $user_name_fields, u.email AS user_email, cl.timecreated, cl.emailstatus
91
                FROM {custommailing_logs} cl
92
                JOIN {custommailing_mailing} cm ON cm.id = cl.custommailingmailingid
93
                JOIN {user} u ON u.id = cl.emailtouserid
94
                WHERE cm.custommailingid = :custommailingid
95
                ORDER BY cl.id DESC";
96
        $rs = $DB->get_recordset_sql($sql, ['custommailingid' => $custommailingid]);
97
        foreach ($rs as $record) {
98
            $record = (array) $record;
99
            $user = new stdClass();
100
            $log = new stdClass();
101
            foreach ($record as $property => $value) {
102
                if (preg_match('/^user\_/', $property)) {
103
                    $property = preg_replace('/^user\_/','',$property);
104
                    $user->$property = $value;
105
                }
106
                else {
107
                    $log->$property = $value;
108
                }
109
            }
110
            $log->user = fullname($user) . ' - '. $user->email;
111
            $records[$log->id] = $log;
112
        }
113
        $rs->close();
114
 
115
        return $records;
116
    }
117
 
118
    /**
119
     * @param stdClass $record
120
     * @return bool
121
     * @throws dml_exception
122
     */
123
    public static function update(stdClass $record) {
124
        global $DB;
125
 
126
        $record->timemodified = time();
127
 
128
        return $DB->update_record('custommailing_logs', $record);
129
    }
130
 
131
    /**
132
     * @param int $userid
133
     * @throws dml_exception
134
     */
135
    public static function deleteByUser(int $userid) {
136
        global $DB;
137
 
138
        $DB->delete_records('custommailing_logs', ['emailtouserid' => $userid]);
139
    }
140
 
141
    /**
142
     * @param stdClass $record
143
     * @return stdClass
144
     */
145
    protected static function format(stdClass $record) {
146
        $record->id = (int) $record->id;
147
        $record->custommailingmailingid = (int) $record->custommailingmailingid;
148
        $record->emailtouserid = (int) $record->emailtouserid;
149
        $record->emailstatus = (int) $record->emailstatus;
150
        $record->timecreated = (int) $record->timecreated;
151
        $record->timemodified = (int) $record->timemodified;
152
 
153
        return $record;
154
    }
155
}