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
 * MoodleNet share progress table.
19
 *
20
 * @package    core
21
 * @copyright  2023 David Woloszyn <david.woloszyn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core\moodlenet;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
require_once($CFG->libdir . '/tablelib.php');
30
 
31
use html_writer;
32
use moodle_url;
33
use stdClass;
34
use table_sql;
35
 
36
/**
37
 * MoodleNet share progress table.
38
 *
39
 * @package    core
40
 * @copyright  2023 David Woloszyn <david.woloszyn@moodle.com>
41
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
43
class share_progress_table extends table_sql {
44
 
45
    /** @var int The user id records will be displayed for. */
46
    protected $userid;
47
 
48
    /**
49
     * Set up the table.
50
     *
51
     * @param string $uniqueid Unique id of table.
52
     * @param moodle_url $url The base URL.
53
     * @param int $userid The user id.
54
     */
55
    public function __construct($uniqueid, $url, $userid) {
56
        parent::__construct($uniqueid);
57
        $this->userid = $userid;
58
        $this->define_table_columns();
59
        $this->define_baseurl($url);
60
        $this->define_table_configs();
61
    }
62
 
63
    /**
64
     * Define table configs.
65
     */
66
    protected function define_table_configs() {
67
        $this->collapsible(false);
68
        $this->sortable(false);
69
        $this->pageable(true);
70
        $this->set_default_per_page(25);
71
    }
72
 
73
    /**
74
     * Set up the columns and headers.
75
     */
76
    protected function define_table_columns() {
77
        // Define headers and columns.
78
        $cols = [
79
            'name' => get_string('name'),
80
            'type' => get_string('moodlenet:columntype'),
81
            'timecreated' => get_string('moodlenet:columnsenddate'),
82
            'status' => get_string('moodlenet:columnsendstatus'),
83
        ];
84
 
85
        $this->define_columns(array_keys($cols));
86
        $this->define_headers(array_values($cols));
87
        $this->column_class('status', 'text-center');
88
    }
89
 
90
    /**
91
     * Name column.
92
     *
93
     * @param stdClass $row Row data.
94
     * @return string
95
     */
96
    protected function col_name(stdClass $row): string {
97
        global $OUTPUT;
98
 
99
        $class = '';
100
        // Track deletion of resources on Moodle.
101
        $deleted = false;
102
        // Courses.
103
        if ($row->type == share_recorder::TYPE_COURSE) {
104
            if ($row->fullname) {
105
                $name = $row->fullname;
106
            } else {
107
                $name = get_string('moodlenet:deletedcourse');
108
                $deleted = true;
109
            }
110
        // Activities.
111
        } else if ($row->type == share_recorder::TYPE_ACTIVITY) {
112
            if ($cm = get_coursemodule_from_id('', $row->cmid)) {
113
                $name = $cm->name;
114
            } else {
115
                $name = get_string('moodlenet:deletedactivity');
116
                $deleted = true;
117
            }
118
        }
119
        if ($deleted) {
120
            $class = 'font-italic';
121
        }
122
        // Add a link to the resource if it was recorded.
123
        if (!empty($row->resourceurl)) {
124
            // Apply bold to resource links that aren't deleted.
125
            if (!$deleted) {
126
                $class = 'font-weight-bold';
127
            }
128
            $icon = $OUTPUT->pix_icon('i/externallink', get_string('opensinnewwindow'), 'moodle', ['class' => 'ml-1']);
129
            $text = $name . $icon;
130
            $attributes = [
131
                'target' => '_blank',
132
                'rel' => 'noopener noreferrer',
133
            ];
134
            $name = html_writer::link($row->resourceurl, $text, $attributes);
135
        }
136
 
137
        return html_writer::span($name, $class);
138
    }
139
 
140
    /**
141
     * Type column.
142
     *
143
     * @param stdClass $row Row data.
144
     * @return string
145
     */
146
    protected function col_type(stdClass $row): string {
147
        // Courses.
148
        if ($row->type == share_recorder::TYPE_COURSE) {
149
            $type = get_string('course');
150
        // Activities.
151
        } else if ($row->type == share_recorder::TYPE_ACTIVITY) {
152
            if ($row->modname) {
153
                $type = get_string('modulename', $row->modname);
154
            } else {
155
                // Alternatively, default to 'activity'.
156
                $type = get_string('activity');
157
            }
158
        }
159
 
160
        return $type;
161
    }
162
 
163
    /**
164
     * Time created column (Send date).
165
     *
166
     * @param stdClass $row Row data.
167
     * @return string
168
     */
169
    protected function col_timecreated(stdClass $row): string {
170
        $format = get_string('strftimedatefullshort', 'core_langconfig');
171
        return userdate($row->timecreated, $format);
172
    }
173
 
174
    /**
175
     * Status column (Send status).
176
     *
177
     * @param stdClass $row Row data.
178
     * @return string
179
     */
180
    protected function col_status(stdClass $row): string {
181
        // Display a badge indicating the status of the share.
182
        if ($row->status == share_recorder::STATUS_IN_PROGRESS) {
183
            $status = html_writer::span(get_string('inprogress'), 'badge bg-warning text-dark');
184
        } else if ($row->status == share_recorder::STATUS_SENT) {
185
            $status = html_writer::span(get_string('sent'), 'badge bg-success text-white');
186
        } else if ($row->status == share_recorder::STATUS_ERROR) {
187
            $status = html_writer::span(get_string('error'), 'badge bg-danger text-white');
188
        }
189
 
190
        return $status;
191
    }
192
 
193
    /**
194
     * Builds the SQL query.
195
     *
196
     * @param bool $count When true, return the count SQL.
197
     * @return array containing sql to use and an array of params.
198
     */
199
    protected function get_sql_and_params($count = false) {
200
        if ($count) {
201
            $select = "COUNT(1)";
202
        } else {
203
            $select = "msp.id, msp.type, msp.courseid, msp.cmid, msp.timecreated, " .
204
                "msp.resourceurl, msp.status, c.fullname, md.name AS modname";
205
        }
206
 
207
        $sql = "SELECT $select
208
                  FROM {moodlenet_share_progress} msp
209
             LEFT JOIN {course} c ON c.id = msp.courseid
210
             LEFT JOIN {course_modules} cm ON cm.course = msp.courseid
211
                       AND cm.id = msp.cmid
212
             LEFT JOIN {modules} md ON md.id = cm.module
213
                 WHERE msp.userid = :userid";
214
 
215
        $params = ['userid' => $this->userid];
216
 
217
        if (!$count) {
218
            $sql .= " ORDER BY msp.status DESC, msp.timecreated DESC";
219
        }
220
 
221
        return [$sql, $params];
222
    }
223
 
224
    /**
225
     * Query the DB.
226
     *
227
     * @param int $pagesize size of page for paginated displayed table.
228
     * @param bool $useinitialsbar do you want to use the initials bar.
229
     */
230
    public function query_db($pagesize, $useinitialsbar = true) {
231
        global $DB;
232
 
233
        list($countsql, $countparams) = $this->get_sql_and_params(true);
234
        list($sql, $params) = $this->get_sql_and_params();
235
        $total = $DB->count_records_sql($countsql, $countparams);
236
        $this->pagesize($pagesize, $total);
237
        $this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size());
238
 
239
        // Set initial bars.
240
        if ($useinitialsbar) {
241
            $this->initialbars($total > $pagesize);
242
        }
243
    }
244
 
245
    /**
246
     * Notification to display when there are no results.
247
     */
248
    public function print_nothing_to_display() {
249
        global $OUTPUT;
250
        echo $OUTPUT->notification(get_string('moodlenet:nosharedresources'), 'info');
251
    }
252
}