From cef3f032c3f701d3dda90cb11662a5e59c8a717e Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 30 Nov 2017 22:13:45 +0400 Subject: The class that creates the main table is defined --- classes/managertokens_table.php | 195 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 classes/managertokens_table.php (limited to 'classes') diff --git a/classes/managertokens_table.php b/classes/managertokens_table.php new file mode 100644 index 0000000..2930a97 --- /dev/null +++ b/classes/managertokens_table.php @@ -0,0 +1,195 @@ +. + +/** + * Describes the plugin tables. + * + * @package tool_managertokens + * @copyright 2017 "Valentin Popov" + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined("MOODLE_INTERNAL") || die(); + +require_once($CFG->libdir . "/tablelib.php"); + +/** + * Describes the main table of the plugin. + * + * @copyright 2017 "Valentin Popov" + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tool_managertokens_table extends table_sql { + /** + * Manager address. + * + * @var string $managertokens + */ + protected static $managertokens = "/admin/tool/managertokens/index.php"; + + /** + * Editor's address. + * + * @var string $editor + */ + protected static $editor = "/admin/tool/managertokens/editor.php"; + + /** + * Constructor. + * + * @param string $uniqueid The unique identifier of the table. + */ + public function __construct($uniqueid) { + parent::__construct($uniqueid); + $this->define_table_columns(); + $this->define_table_configs(); + } + + /** + * Defines the basic settings of the table. + */ + public function define_table_configs() { + $this->set_sql("*", "{tool_managertokens_tokens}", "1"); + $this->collapsible(false); + $this->is_downloadable(false); + $this->no_sorting("actions"); + $this->pageable(true); + } + + /** + * Defines the main columns and table headers. + */ + public function define_table_columns() { + $columns = array( + "targetid", + "token", + "scope", + "timelastuse", + "timecreated", + "actions" + ); + + $headers = array( + new lang_string("role", "moodle"), + new lang_string("password", "moodle"), + new lang_string("used", "moodle"), + new lang_string("lastmodified", "moodle"), + new lang_string("create", "moodle"), + new lang_string("actions", "moodle") + ); + + $this->define_columns($columns); + $this->define_headers($headers); + } + + /** + * Specifies the display of a column with actions. + * + * @param object $row Data from the database. + * @return string Displayed data. + */ + public function col_actions($row) { + global $OUTPUT; + + /* Sets the switch icon */ + $hideshowicon = "t/lock"; + $hideshowstring = new lang_string("enable", "moodle"); + if (boolval($row->enabled)) { + $hideshowicon = "t/unlock"; + $hideshowstring = new lang_string("disable", "moodle"); + } + + /* Link to enable / disable the token */ + $hideshowlink = new moodle_url(self::$managertokens, array("hideshowid" => $row->id, "sesskey" => sesskey())); + $hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($hideshowicon, $hideshowstring)); + + /* Link to edit the token */ + $editlink = new moodle_url(self::$editor, array("tokenid" => $row->id)); + $edititem = $OUTPUT->action_icon($editlink, new pix_icon("t/edit", new lang_string("edit", "moodle"))); + + /* Link to delete the token */ + $deletelink = new moodle_url(self::$managertokens, array("deleteid" => $row->id, "sesskey" => sesskey())); + $deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon("t/delete", new lang_string("delete", "moodle"))); + + $html = $hideshowitem . $edititem . $deleteitem; + return $html; + } + + /** + * Specifies the display of the column with the role. + * + * @param object $row Data from the database. + * @return string Displayed data. + */ + public function col_targetid($row) { + $html = $row->targetid; + + if ($row->targettype == "user") { + /* Specifies the user name */ + $linktext = new lang_string("user", "moodle"); + if ($user = core_user::get_user($row->targetid)) { + $linktext = "$user->firstname $user->lastname"; + } + + /* Creates a reference */ + $linkurl = new moodle_url("/user/profile.php", array("id" => $row->targetid)); + $html = html_writer::link($linkurl, $linktext); + } + + return $html; + } + + /** + * Specifies the display of a column from the time of creation. + * + * @param object $row Data from the database. + * @return string Displayed data. + */ + public function col_timecreated($row) { + $date = userdate($row->timecreated, new lang_string("strftimerecent", "langconfig")); + return $date; + } + + /** + * Specifies the display of the last used column. + * + * @param object $row Data from the database. + * @return string Displayed data. + */ + public function col_timelastuse($row) { + $date = $row->timelastuse; + + if (!empty($row->timelastuse)) { + $date = userdate($row->timelastuse, new lang_string("strftimerecent", "langconfig")); + } + + return $date; + } + + /** + * Specifies the display of a column with a token. + * + * @param object $row Data from the database. + * @return string Displayed data. + */ + public function col_token($row) { + $linktext = $row->token; + $linkurl = new moodle_url(self::$editor, array("tokenid" => $row->id)); + + $html = html_writer::link($linkurl, $linktext); + return $html; + } +} \ No newline at end of file -- cgit v1.2.3