This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcandela-lti-table.php
133 lines (107 loc) · 3.91 KB
/
candela-lti-table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
// Class to display LTI maps.
// If file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) exit;
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
class Candela_LTI_Table extends WP_List_Table {
function __construct() {
global $status, $page;
parent::__construct(array(
'singular' => 'LTI map',
'plural' => 'LTI maps',
));
}
function column_default($item, $column_name) {
return $item[$column_name];
}
function column_cb($item) {
return sprintf('<input type="checkbox" name=ID[]" value="%s" />', $item['ID']);
}
function column_target_action($item) {
return sprintf('<a href="%s">%s</a>', esc_attr($item['target_action']), $item['target_action']);
}
function column_user($item) {
return sprintf('<a href="%s/wp-admin/user-edit.php?user_id=%d">%s</a>', get_site_url(), esc_attr($item['user_id']), $item['user_nicename']);
}
function column_blog_id($item) {
if ( ! empty( $item['blog_id'] ) ) {
$blog = get_blog_details( $item['blog_id']);
return sprintf('<a href="%s">%s</a>', get_site_url($item['blog_id']), $blog->blogname);
}
else {
return 'N/A';
}
}
function get_columns() {
return array(
'cb' => '<input type="checkbox" />',
'resource_link_id' => __('Link ID', 'candela_lti'),
'target_action' => __('Action', 'candela_lti'),
'user' => __('User', 'candela_lti'),
'blog_id' => __('Blog ID', 'candela_lti' ),
);
}
function get_sortable_columns() {
return array(
'resource_link_id' => array('resource_link_id', TRUE),
'target_action' => array('target_action', FALSE),
'user' => array('user_nicename', FALSE),
'blog_id' => array('blog_id', FALSE),
);
}
function get_bulk_actions() {
return array(
'delete' => 'Delete',
);
}
function process_bulk_action() {
global $wpdb;
$table_name = CANDELA_LTI_TABLE;
if ('delete' === $this->current_action() ) {
$ids = isset($_REQUEST['ID']) ? $_REQUEST['ID'] : array();
if ( is_array($ids) ) {
$ids = implode(',', $ids);
}
if ( ! empty($ids) ) {
$wpdb->query("DELETE FROM $table_name WHERE id IN( $ids )");
}
}
}
function prepare_items() {
global $wpdb;
$table_name = CANDELA_LTI_TABLE;
$per_page = 20;
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
$this->process_bulk_action();
$total_items = $wpdb->get_var("SELECT count(ID) FROM $table_name");
$paged = isset($_REQUEST['paged']) ? max(0, intval($_REQUEST['paged']) - 1) : 0;
$orderby = (isset($_REQUEST['orderby']) && in_array($_REQUEST['orderby'], array_keys($this->get_sortable_columns()))) ? $_REQUEST['orderby'] : 'resource_link_id';
$order = (isset($_REQUEST['order']) && in_array($_REQUEST['order'], array('asc', 'desc'))) ? $_REQUEST['order'] : 'asc';
if ( get_current_blog_id() != 1 ) {
$sql = "SELECT l.*, u.user_nicename FROM $table_name l
LEFT JOIN wp_users u ON u.ID = l.user_id
WHERE blog_id = %d
ORDER BY $orderby $order
LIMIT %d OFFSET %d";
$prepared = $wpdb->prepare($sql, get_current_blog_id(), $per_page, $paged);
}
else {
$sql = "SELECT l.*, u.user_nicename FROM $table_name l
LEFT JOIN wp_users u ON u.ID = l.user_id
ORDER BY $orderby $order
LIMIT %d OFFSET %d";
$prepared = $wpdb->prepare($sql, $per_page, $paged);
}
$this->items = $wpdb->get_results($prepared, ARRAY_A);
$this->set_pagination_args(array(
'total_items' => $total_items, // total items defined above
'per_page' => $per_page, // per page constant defined at top of method
'total_pages' => ceil($total_items / $per_page) // calculate pages count
));
}
}