From 744c06b8fcfc3a2d1540eeef44c84522b58475af Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 23 Nov 2012 10:46:44 +0100 Subject: sqlbox: Move db_table_from_query to locallib.php --- sqlbox/locallib.php | 17 +++++++++++++++++ sqlbox/view.php | 20 ++------------------ 2 files changed, 19 insertions(+), 18 deletions(-) (limited to 'sqlbox') diff --git a/sqlbox/locallib.php b/sqlbox/locallib.php index 3820cec..f335b9a 100644 --- a/sqlbox/locallib.php +++ b/sqlbox/locallib.php @@ -38,3 +38,20 @@ defined('MOODLE_INTERNAL') || die(); //function sqlbox_do_something_useful(array $things) { // return new stdClass(); //} + +function db_table_from_query($db, $query) { + $table = new html_table; + $result = pg_query($db, $query); + if (!$result) + error("Query failed: ".pg_last_error($db)); + + $table = new html_table; + $n = pg_num_fields($result); + for ($i = 0; $i < $n; $i++) + $table->head[] = pg_field_name($result, $i); + $table->align = array_fill(0, $n, 'left'); + while ($row = pg_fetch_row($result)) + $table->data[] = $row; + + return $table; +} diff --git a/sqlbox/view.php b/sqlbox/view.php index 0c7c841..8d6f29a 100644 --- a/sqlbox/view.php +++ b/sqlbox/view.php @@ -31,6 +31,7 @@ require_once(dirname(dirname(dirname(__FILE__))).'/config.php'); require_once(dirname(__FILE__).'/lib.php'); +require_once(dirname(__FILE__).'/locallib.php'); require_once(dirname(__FILE__).'/sqlbox_form.php'); $id = optional_param('id', 0, PARAM_INT); // course_module ID, or @@ -86,7 +87,7 @@ if (isset($data->sqlbox_query)) { if (!db) error("Failed to connect to sqlbox db"); - $table = db_get_table_from_query($db, $data->sqlbox_query); + $table = db_table_from_query($db, $data->sqlbox_query); if (strlen($sqlbox->query) > 0) { $table_orig = db_get_table_from_query($db, $sqlbox->query); if ($table->data != $table_orig->data || @@ -107,20 +108,3 @@ if (isset($data->sqlbox_query)) { echo $OUTPUT->footer(); -function db_get_table_from_query($db, $query) { - $table = new html_table; - $result = pg_query($db, $query); - if (!$result) - error("Query failed: ".pg_last_error($db)); - - $table = new html_table; - $n = pg_num_fields($result); - for ($i = 0; $i < $n; $i++) - $table->head[] = pg_field_name($result, $i); - $table->align = array_fill(0, $n, 'left'); - while ($row = pg_fetch_row($result)) - $table->data[] = $row; - - return $table; -} - -- cgit