From 7b88e1340bec7f4042b5418f5c6c491caae6f222 Mon Sep 17 00:00:00 2001 From: Nick Freear Date: Thu, 11 May 2017 12:13:42 +0100 Subject: [PATCH] Bug #5, Add `ouop-query` commandline PHP script [iet:8974552] --- bin/csv-import.php | 4 ++-- bin/ouop-query.php | 47 ++++++++++++++++++++++++++++++++++++++++++++++ composer.json | 1 + db/User.php | 22 +++++++++++++++++----- 4 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 bin/ouop-query.php diff --git a/bin/csv-import.php b/bin/csv-import.php index b801b75..02e08a5 100644 --- a/bin/csv-import.php +++ b/bin/csv-import.php @@ -50,8 +50,8 @@ }); -cli_writeln("\nWarnings:"); -print_r(OuUser::getWarnings()); +cli_write(sprintf( "\nWarnings (%d): ", count(OuUser::getWarnings()) )); +cli_writeln(json_encode( OuUser::getWarnings(), JSON_PRETTY_PRINT )); cli_writeln("\nUsers inserted: $count"); diff --git a/bin/ouop-query.php b/bin/ouop-query.php new file mode 100644 index 0000000..1a388d8 --- /dev/null +++ b/bin/ouop-query.php @@ -0,0 +1,47 @@ +libdir . '/clilib.php'); + +use IET_OU\Moodle\Auth\Ouopenid\Db\User as OuUser; + +cli_heading('OU-OpenID DB queries'); + +$counts = [ + 'Total users' => OuUser::count(), + 'Tranche 1' => OuUser::count([ 'batch' => 0 ]), + 'Tranche 2' => OuUser::count([ 'batch' => 2 ]), + 'Tranche 3' => OuUser::count([ 'batch' => 3 ]), + 'Keystroke preset' => OuUser::count([ 'teslainstrument' => 'kd' ]), + 'Plagiarism preset' => OuUser::count([ 'teslainstrument' => 'tpt' ]), + 'No preset' => OuUser::count([ 'teslainstrument' => null ]), // Should always be '0'! +]; + +cli_write('Counts: '); +cli_writeln(json_encode( $counts , JSON_PRETTY_PRINT )); + +exit; // Work-in-progress! + + +$conditions = [ 'oucu' => '{ EDIT ME }' ]; + +$results = [ + OuUser::query($conditions), +]; + +cli_write('Results: '); +print_r( $results ); + +// End. diff --git a/composer.json b/composer.json index 1f2e5e0..3908b6c 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "squizlabs/php_codesniffer": "2.8.1" }, "bin": [ + "bin/ouop-query.php", "bin/csv-example.php", "bin/csv-import.php" ], diff --git a/db/User.php b/db/User.php index 3511726..952f227 100644 --- a/db/User.php +++ b/db/User.php @@ -51,10 +51,22 @@ public static function getUser($username, $strictness = IGNORE_MISSING | IGNORE_ return $user; } - public static function countUsers() + /** Count DB records, with or without conditions. + * @return int + */ + public static function count($conditions = null) { global $DB; // Moodle global. - return $DB->count_records(self::USER_TABLE); + return $DB->count_records(self::USER_TABLE, $conditions); + } + + /** Select DB records, based on conditions. + * @return array Array of objects. + */ + public static function query($conditions, $limitnum = 4) + { + global $DB; // Moodle global. + return $DB->get_records(self::USER_TABLE, $conditions, $sort = '', $fields = '*', $from = 0, $limitnum); } /** Delete all records from plugin DB table. @@ -62,7 +74,6 @@ public static function countUsers() public static function delete() { global $DB; // Moodle global. - return $DB->delete_records(self::USER_TABLE); } @@ -125,7 +136,7 @@ public static function insertFromCsv($filename = '../example.csv', $ignore_headi ]; $user_id = $DB->insert_record(self::USER_TABLE, $user_record, $returnid = true); - if ($callback) { + if ($callback && is_callable($callback)) { $callback ($count, $user_id); } }); @@ -287,7 +298,8 @@ public static function getRedirectUrl($profile, $action = null) $url = $redirects[ $instrument ]->url; - return $CFG->wwwroot . sprintf($url, $action); + return $CFG->wwwroot . str_replace('%s', $action, $url); + // Was: return $CFG->wwwroot . sprintf($url, $action); } /** Get Google Doc. embed URL [ MOVE ]