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
 * Sql table implementation for report_failedemails.
19
 *
20
 * @package   report_failedemails
21
 * @copyright 2023 Krishna Mohan Prasad <kmp.moodle@gmail.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace report_failedemails;
26
 
27
defined('MOODLE_INTERNAL') || die;
28
use moodle_url;
29
use table_sql;
30
use stdClass;
31
 
32
require_once($CFG->libdir . "/tablelib.php");
33
 
34
/**
35
 * Table failed_emails class for displaying email failures.
36
 */
37
class failed_emails_table extends table_sql {
38
 
39
    /** @var string download parameter name */
40
    protected $downloadparamname = 'download';
41
 
42
    /** @var integer to count current row no */
43
    private $scounter = 1;
44
 
45
    /**
46
     * Sets up the failed_emails_table parameters
47
     * @param string $uniqueid     unique id of table
48
     * @param string|moodle_url $baseurl      base url of the table
49
     */
50
    public function __construct($uniqueid, $baseurl) {
51
        parent::__construct($uniqueid);
52
        $this->define_baseurl($baseurl);
53
 
54
        $headcols = [
55
            's_no' => 'S.No.',
56
            'affected_user' => get_string('affected_user', 'report_failedemails'),
57
            'subject' => get_string('email_subject', 'report_failedemails'),
58
            'message' => get_string('email_message', 'report_failedemails'),
59
            'timecreated' => get_string('date'),
60
        ];
61
 
62
        $this->define_columns(array_keys($headcols));
63
        $this->define_headers(array_values($headcols));
64
 
65
        // Allow pagination.
66
        $this->pageable(true);
67
        // Allow downloading.
68
        $name = format_string(get_string('failed_emails_report', 'report_failedemails'));
69
        $this->is_downloadable(true);
70
        $this->is_downloading(
71
            optional_param($this->downloadparamname, 0, PARAM_ALPHA),
72
            $name,
73
            get_string('pluginname', 'report_failedemails')
74
        );
75
 
76
        // Allow sorting.
77
        $this->sortable(true);
78
        $this->no_sorting('s_no');
79
        $this->no_sorting('subject');
80
        $this->no_sorting('message');
81
 
82
        list($fields, $from, $where, $whereparams) = $this->get_sql_fragments();
83
 
84
        list($countsql, $countsqlparams) = $this->get_count_sql();
85
 
86
        $this->set_sql($fields, $from, $where, $whereparams);
87
 
88
        $this->set_count_sql($countsql, $countsqlparams);
89
    }
90
 
91
    /**
92
     * Generate serial no column
93
     * @param  stdClass $row row data object
94
     * @return int      returns row no
95
     */
96
    public function col_s_no($row): int {
97
        $count = ($this->currpage * $this->pagesize) + $this->scounter;
98
        $this->scounter = $this->scounter + 1;
99
        return $count;
100
    }
101
 
102
    /**
103
     * Generates affected user column
104
     * @param  stdClass $row row data object
105
     * @return string|action_link user profile name or link
106
     */
107
    public function col_affected_user($row) {
108
        global $OUTPUT;
109
        $affecteduser = \core_user::get_user($row->relateduserid);
110
        $userfullname = $row->affected_user;
111
        if (!$this->is_downloading()) {
112
            if (user_can_view_profile($affecteduser)) {
113
                $url = new moodle_url('/user/profile.php', ['id' => $row->relateduserid]);
114
                $profilelink = $OUTPUT->action_link($url, $userfullname, null, ['target' => '_blank']);
115
                return $profilelink;
116
            }
117
        }
118
 
119
        return $userfullname;
120
    }
121
 
122
    /**
123
     * Generates subject column
124
     * @param  stdClass $row row data object
125
     * @return string mail subject
126
     */
127
    public function col_subject($row) {
128
        $otherinfo = $row->other;
129
        $otherinfoarr = json_decode($otherinfo);
130
 
131
        return $otherinfoarr->subject;
132
    }
133
 
134
    /**
135
     * Generated message colum
136
     * @param  stdClass $row row data object
137
     * @return string mail message
138
     */
139
    public function col_message($row) {
140
        $otherinfo = $row->other;
141
        $otherinfoarr = json_decode($otherinfo);
142
 
143
        return $otherinfoarr->message;
144
    }
145
 
146
    /**
147
     * Generates timecreated column
148
     * @param  stdClass $row row data object
149
     * @return string the formatted date/time.
150
     */
151
    public function col_timecreated($row) {
152
        return userdate($row->timecreated);
153
    }
154
 
155
    private function get_sql_fragments() {
156
        global $DB;
157
 
158
        list($filtercondition, $filterparams) = $this->make_filter_condition_and_params($filterparams);
159
 
160
        $fields = 'lssl.id, lssl.relateduserid, lssl.other, lssl.timecreated,'. $DB->sql_concat('u.firstname', "'  '", 'u.lastname') .'AS affected_user';
161
        $from = '{logstore_standard_log} AS lssl
162
        JOIN {user} u ON u.id = lssl.relateduserid';
163
        return [$fields, $from, $filtercondition, $filterparams];
164
    }
165
 
166
    private function get_count_sql() {
167
        list($filtercondition, $filterparams) = $this->make_filter_condition_and_params($filterparams);
168
        $fields = 'SELECT COUNT(lssl.id) ';
169
        $from = ' FROM {logstore_standard_log} lssl
170
        JOIN {user} u ON u.id = lssl.relateduserid ';
171
        return  [$fields. $from. " WHERE ".$filtercondition, $filterparams];
172
    }
173
 
174
    /**
175
     * To make a filter for events of email failure
176
     * @return array containing where condtion and parameters
177
     */
178
    private function make_filter_condition_and_params() {
179
        global $DB;
180
        $params = [];
181
        $where = $DB->sql_equal('eventname', ':eventname');
182
        $params['eventname'] = '\core\event\email_failed';
183
        return [$where, $params];
184
    }
185
 
186
    /**
187
     * Generates total count of failed emails
188
     * @return int count of failed emails
189
     */
190
    private function get_total_count() {
191
        global $DB;
192
        list($countsql, $countsqlparams) = $this->get_count_sql();
193
        $totalcount = $DB->count_records_sql($countsql, $countsqlparams);
194
        return $totalcount;
195
    }
196
 
197
    /**
198
     * [download description]
199
     * @return [type] [description]
200
     */
201
    public function download() {
202
        \core\session\manager::write_close();
203
        $this->out($this->get_total_count(), false);
204
        exit;
205
    }
206
}