This is the last session of (moodle plugin build) i.e. create a table view according to Moodle standards. We do not create tables with html or use datatable assistance. we're going to make our own version and it's pretty easy.
1. Make a file "table.php" continuing from the previous discussion.
myplugin/ ├─ db/ ├─ form/ ├─ lang/ ├─ templates/ ├─ form.php ├─ index.php ├─ lib.php ├─ table.php ├─ template.php ├─ version.php
What we need to know is the elements in the table:
1. head
2. queries
3. row data
4. pagination
2. Before we create the pagination we must first determine the data limit to limit what will be displayed then we will set the data page to be displayed first.
//SET START AND LIMIT PAGINATION
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 5, PARAM_INT);
3.Before creating an element table, you must declare a table object.
$table = new html_table();
4. After that we just need to make the first part of the table is the head
$table->head = array ();
$table->head[] = 'ID';
$table->head[] = get_string('name');
$table->head[] = get_string('email');
We have three columns of data here to display.
5. Create a query that will be displayed in the table and associated with a pagination, so what we need is to calculate the entire amount of data to divide into the number of pages and set the limit of data to be displayed.
//SET QUERY GET USER
$queryuser = "SELECT * FROM {user}";
//COUNT ALL DATA
$alluser = $DB->get_records_sql($queryuser);
//QUERY FOR LIMIT AND PAGE
$queryuser .= " LIMIT ".$perpage." OFFSET ".intval($page)*$perpage;
//SET ALL QUERYS
$datauser = $DB->get_records_sql($queryuser);
6. The last thing in setting the appearance of the data is to set the data row.
//LOOP DATA AND SET ROW DATA
foreach ($datauser as $key => $value) {
$row = array ();
$row[] = $value->id;
$row[] = $value->firstname;
$row[] = $value->email;
$table->data[] = $row;
}
7. The last part of making this table is displaying the table that we have set, and displaying the pagination.
//SET BASE URL PAGE
$baseurl = new moodle_url('/local/myplugin/table.php', array('perpage' => $perpage));
//DISPLAY TABLE
echo html_writer::start_tag('div', array('class'=>'no-overflow'));
echo html_writer::table($table);
echo html_writer::end_tag('div');
//PAGINATION
echo $OUTPUT->paging_bar(count($alluser), $page, $perpage, $baseurl);
8. For the entire source code.
<?php
/**
* @package local_myplugin
* @author Deril Lab
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
//CALL CONFIG.PHP
require_once(__DIR__ . '/../../config.php');
$PAGE->set_url(new moodle_url('/local/myplugin/index.php'));
//SET TITLE PAGE
$PAGE->set_title(get_string("pluginname", "local_myplugin"));
$PAGE->set_heading(get_string("pluginname", "local_myplugin"));
//SET START AND LIMIT PAGINATION
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 5, PARAM_INT);
GLOBAL $DB;
GLOBAL $CFG;
GLOBAL $USER;
//PRIVATE PAGE FOR PUBLIC
require_login();
//SET BREADCRHUM
$PAGE->navbar->add(get_string("pluginname", "local_myplugin"));
//GET HEADER
echo $OUTPUT->header();
/** TABLE EXAMPLE **/
$table = new html_table();
$table->head = array ();
$table->head[] = 'ID';
$table->head[] = get_string('name');
$table->head[] = get_string('email');
//SET QUERY GET USER
$queryuser = "SELECT * FROM {user}";
//COUNT ALL DATA
$alluser = $DB->get_records_sql($queryuser);
//QUERY FOR LIMIT AND PAGE
$queryuser .= " LIMIT ".$perpage." OFFSET ".intval($page)*$perpage;
//SET ALL QUERYS
$datauser = $DB->get_records_sql($queryuser);
//LOOP DATA AND SET ROW DATA
foreach ($datauser as $key => $value) {
$row = array ();
$row[] = $value->id;
$row[] = $value->firstname;
$row[] = $value->email;
$table->data[] = $row;
}
//SET BASE URL PAGE
$baseurl = new moodle_url('/local/myplugin/table.php', array('perpage' => $perpage));
//DISPLAY TABLE
echo html_writer::start_tag('div', array('class'=>'no-overflow'));
echo html_writer::table($table);
echo html_writer::end_tag('div');
/** END TABLE **/
//PAGINATION
echo $OUTPUT->paging_bar(count($alluser), $page, $perpage, $baseurl);
//GET FOOTER
echo $OUTPUT->footer();
?>
0 Comments