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
 * this file contains the status table class to display configured exclusions.
19
 *
20
 * File         exclusiontable.php
21
 * Encoding     UTF-8
22
 * @copyright   Sebsoft.nl
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace tool_usersuspension;
27
 
28
defined('MOODLE_INTERNAL') || die;
29
require_once($CFG->libdir . '/tablelib.php');
30
 
31
/**
32
 * tool_usersuspension\exclusiontable
33
 *
34
 * @package     tool_usersuspension
35
 *
36
 * @copyright   Sebsoft.nl
37
 * @author      R.J. van Dongen <rogier@sebsoft.nl>
38
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class exclusiontable extends \flexible_table {
41
 
42
    /**
43
     * Raw data array
44
     * @var array
45
     */
46
    protected $rawdata;
47
 
48
    /**
49
     * item deletion string
50
     * @var string
51
     */
52
    protected $strdelete;
53
 
54
    /**
55
     * table type string
56
     * @var string
57
     */
58
    protected $tabletype = 'all';
59
 
60
    /**
61
     * Create a new instance of the exclusiontable
62
     */
63
    public function __construct() {
64
        global $USER;
65
        parent::__construct(__CLASS__. '-' . $USER->id . '-' . $this->tabletype);
66
        $this->rawdata = array();
67
        $this->strdelete = get_string('action:delete-exclusion', 'tool_usersuspension');
68
        $this->no_sorting('action');
69
    }
70
 
71
    /**
72
     * Renders the table
73
     *
74
     * @param int $pagesize
75
     * @param bool $useinitialsbar
76
     */
77
    public function render($pagesize, $useinitialsbar = true) {
78
        $this->define_columns(array('type', 'name', 'timecreated', 'action'));
79
        $this->define_headers(array(
80
            get_string('thead:type', 'tool_usersuspension'),
81
            get_string('thead:name', 'tool_usersuspension'),
82
            get_string('thead:timecreated', 'tool_usersuspension'),
83
            get_string('thead:action', 'tool_usersuspension'))
84
        );
85
 
86
        $this->setup();
87
 
88
        switch ($this->tabletype)
89
        {
90
            case 'user':
91
                $this->load_type_users($pagesize);
92
                break;
93
            case 'cohort':
94
                $this->load_type_cohorts($pagesize);
95
                break;
96
            default:
97
                $this->load_all($pagesize);
98
                break;
99
        }
100
 
101
        $this->build_table();
102
        $this->finish_output();
103
    }
104
 
105
    /**
106
     * Take the data returned from the db_query and go through all the rows
107
     * processing each col using either col_{columnname} method or other_cols
108
     * method or if other_cols returns NULL then put the data straight into the
109
     * table.
110
     */
111
    public function build_table() {
112
        if ($this->rawdata) {
113
            foreach ($this->rawdata as $row) {
114
                $formattedrow = $this->format_row($row);
115
                $this->add_data_keyed($formattedrow, $this->get_row_class($row));
116
            }
117
        }
118
    }
119
 
120
    /**
121
     *
122
     * Get any extra classes names to add to this row in the HTML.
123
     * @param \stdClass $row array the data for this row.
124
     * @return string added to the class="" attribute of the tr.
125
     */
126
    public function get_row_class($row) {
127
        return '';
128
    }
129
 
130
    /**
131
     * Return the SQL and parameters for the 'user' type part of the exclusion table
132
     * @return array
133
     */
134
    protected function get_type_users_sql() {
135
        global $DB;
136
        $pfx = util::get_prefix();
137
        $fields = 'e.id,e.type,u.id as refid,' . $DB->sql_fullname('u.firstname', 'u.lastname') .
138
                ' AS name,e.timecreated,null AS action';
139
        $from = '{tool_usersuspension_excl} e JOIN {user} u ON e.refid=u.id';
140
        $where = "type = :{$pfx}type";
141
        $sql = "SELECT $fields FROM $from WHERE $where";
142
        return array($sql, array("{$pfx}type" => 'user'));
143
    }
144
 
145
    /**
146
     * Load the data for the 'user' type part of the exclusion table
147
     * @param int $pagesize
148
     */
149
    protected function load_type_users($pagesize) {
150
        global $DB;
151
        // Table size.
152
        $count = $DB->count_records('tool_usersuspension_excl', array('type' => 'user'));
153
        $this->pagesize($pagesize, $count);
154
 
155
        list($sql, $params) = $this->get_type_users_sql();
156
        $sort = $this->get_sql_sort();
157
        if ($sort) {
158
            $sql .= " ORDER BY $sort";
159
        }
160
        $records = $DB->get_records_sql($sql, $params,
161
                $this->get_page_start(), $this->get_page_size());
162
        $this->rawdata = array_merge($this->rawdata, $records);
163
    }
164
 
165
    /**
166
     * Return the SQL and parameters for the 'cohort' type part of the exclusion table
167
     * @return array
168
     */
169
    protected function get_type_cohorts_sql() {
170
        $pfx = util::get_prefix();
171
        $fields = 'e.id,e.type,c.id as refid,c.name,e.timecreated,null AS action';
172
        $from = '{tool_usersuspension_excl} e JOIN {cohort} c ON e.refid=c.id';
173
        $where = "type = :{$pfx}type";
174
        $sql = "SELECT $fields FROM $from WHERE $where";
175
        return array($sql, array("{$pfx}type" => 'cohort'));
176
    }
177
 
178
    /**
179
     * Load the data for the 'cohort' type part of the exclusion table
180
     * @param int $pagesize
181
     */
182
    protected function load_type_cohorts($pagesize) {
183
        global $DB;
184
        // Table size.
185
        $count = $DB->count_records('tool_usersuspension_excl', array('type' => 'user'));
186
        $this->pagesize($pagesize, $count);
187
 
188
        list($sql, $params) = $this->get_type_cohorts_sql();
189
        $sort = $this->get_sql_sort();
190
        if ($sort) {
191
            $sql .= " ORDER BY $sort";
192
        }
193
        $records = $DB->get_records_sql($sql . $sort, $params,
194
                $this->get_page_start(), $this->get_page_size());
195
        $this->rawdata = array_merge($this->rawdata, $records);
196
    }
197
 
198
    /**
199
     * Load all data for the exclusion table
200
     * @param int $pagesize
201
     */
202
    protected function load_all($pagesize) {
203
        global $DB;
204
        // Table size.
205
        $count = $DB->count_records('tool_usersuspension_excl');
206
        $this->pagesize($pagesize, $count);
207
 
208
        list($sql, $params) = $this->get_type_users_sql();
209
        list($sql2, $params2) = $this->get_type_cohorts_sql();
210
 
211
        $params = array_merge($params, $params2);
212
        $sql .= ' UNION ' . $sql2;
213
        $sort = $this->get_sql_sort();
214
        if ($sort) {
215
            $sql .= " ORDER BY $sort";
216
        }
217
        $records = $DB->get_records_sql($sql, $params,
218
                $this->get_page_start(), $this->get_page_size());
219
        $this->rawdata = $records;
220
    }
221
 
222
    /**
223
     * Render visual representation of the 'username' column for use in the table
224
     *
225
     * @param \stdClass $row
226
     * @return string time string
227
     */
228
    public function col_name($row) {
229
        global $CFG;
230
        if ($row->type === 'user') {
231
            $link = new \moodle_url($CFG->wwwroot . '/user/profile.php', array('id' => $row->id));
232
            return '<a href="' . $link->out() . '">' . $row->name . '</a>';
233
        } else {
234
            $link = new \moodle_url($CFG->wwwroot . '/cohort/index.php');
235
            return '<a href="' . $link->out() . '">' . $row->name . '</a>';
236
        }
237
    }
238
 
239
    /**
240
     * Render visual representation of the 'timemodified' column for use in the table
241
     *
242
     * @param \stdClass $row
243
     * @return string time string
244
     */
245
    public function col_timecreated($row) {
246
        return userdate($row->timecreated);
247
    }
248
 
249
    /**
250
     * Render visual representation of the 'action' column for use in the table
251
     *
252
     * @param \stdClass $row
253
     * @return string actions
254
     */
255
    public function col_action($row) {
256
        $actions = array();
257
        $actions[] = $this->get_action($row, 'delete', true);
258
        return implode('', $actions);
259
    }
260
 
261
    /**
262
     * Return the image tag representing an action image
263
     *
264
     * @param string $action
265
     * @return string HTML image tag
266
     */
267
    protected function get_action_image($action) {
268
        global $OUTPUT;
269
        $actionstr = 'str' . $action;
270
        return '<img src="' . $OUTPUT->image_url($action, 'tool_usersuspension') .
271
                '" title="' . $this->{$actionstr} . '"/>';
272
    }
273
 
274
    /**
275
     * Return a string containing the link to an action
276
     *
277
     * @param \stdClass $row
278
     * @param string $action
279
     * @param bool $confirm whether or not to render a javascript confirm box
280
     * @return string link representing the action with an image
281
     */
282
    protected function get_action($row, $action, $confirm = false) {
283
        $actionstr = 'str' . $action;
284
        $onclick = '';
285
        if ($confirm) {
286
            $onclick = ' onclick="return confirm(\'' .
287
                    get_string('action:confirm-'.$action.'-exclusion', 'tool_usersuspension') .
288
                    '\');"';
289
        }
290
        return '<a ' . $onclick . 'href="' . new \moodle_url($this->baseurl,
291
                array('action' => $action, 'id' => $row->id, 'sesskey' => sesskey())) .
292
                '" alt="' . $this->{$actionstr} .
293
                '">' . $this->get_action_image($action) . '</a>';
294
    }
295
 
296
}