# See how to use this class

1. <?php
2. /**
3. * Table controller
4. *
5. * @author Mark Nielsen
6. * @version $Id$
7. * @package blocks/helloworld
8. */
10. defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
12. class block_helloworld_controller_table extends mr_controller_block {
13. /**
14. * Default screen
15. *
16. * Demo of mr_html_table, mr_html_paging, mr_file_export and mr_preferences
17. */
18. public function view_action() {
19. global $DB, $COURSE;
21. #### DEMO CODE ####
22. // Table stores sorting info in mr_preferences
23. // Paging store perpage info in mr_preferences
24. $preferences = new mr_preferences($COURSE->id, 'blocks/helloworld');
26. // Setup the paging bar
27. $paging = new mr_html_paging($preferences, $this->url);
28. $paging->set_total($DB->count_records('user'));
29. $paging->set_perpageopts(array('all', 1, 5, 50, 100));
31. // Setup a new table
32. $table = new mr_html_table($preferences, $this->url, 'username');
34. // Add columns and column formatting
35. $table->add_column('username', get_string('username'))
36. ->add_column('firstname', get_string('firstname'))
37. ->add_column('lastname', get_string('lastname'))
38. ->add_column('email', get_string('email'))
39. ->add_column('lastaccess', get_string('lastaccess'))
40. ->add_format('lastaccess', 'date')
41. ->add_format(array('username', 'firstname', 'lastname', 'email'), 'string');
43. // Export handler - autodetects if we are exporting
44. $export = new mr_file_export('**', false, $this->url);
46. // When exporting, sets the limitfrom and limitnum appropriately
47. $paging->set_export($export);
49. // When exporting, send column headers to the export
50. // And then route all rows to the export as well
51. $table->set_export($export);
53. // Fetch rows to add to the table
54. $rows = $DB->get_records('user', NULL, $table->get_sql_sort(), $table->get_sql_select(),
55. $paging->get_limitfrom(), $paging->get_limitnum());
57. foreach ($rows as $row) {
58. $table->add_row($row);
59. }
60. // If exporting, sends file, if not, does nothing
61. $export->send();
63. // Render the result
64. $output = $this->mroutput->render($paging);
65. $output .= $this->mroutput->render($table);
66. $output .= $this->mroutput->render($paging);
67. $output .= $this->mroutput->render($export);
68. #### DEMO CODE ####
70. return $this->output->heading('Demo of mr_table_*').
71. $this->helper->highlight(__CLASS__, __FUNCTION__, true).
72. $this->output->box($output, 'generalbox boxaligncenter boxwidthnormal');
73. }
74. }

Documentation generated on Thu, 28 Jun 2012 16:33:52 -0700 by phpDocumentor 1.4.3