Skip to content

Commit

Permalink
Bug #5, Add ouop-query commandline PHP script [iet:8974552]
Browse files Browse the repository at this point in the history
  • Loading branch information
nfreear committed May 11, 2017
1 parent 0c2b7ce commit 7b88e13
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
4 changes: 2 additions & 2 deletions bin/csv-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
47 changes: 47 additions & 0 deletions bin/ouop-query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* CLI. Run database queries to check data integrity.
*
* @package auth_ouopenid
* @author Nick Freear, 10-May-2017.
* @copyright (c) 2017 The Open University.
*/
define( 'CLI_SCRIPT', true );

require_once(__DIR__ . '/bootstrap.php');

auth_ouopenid\bootstrap::vendor_autoload();
auth_ouopenid\bootstrap::moodle_config();

require_once($CFG->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.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"squizlabs/php_codesniffer": "2.8.1"
},
"bin": [
"bin/ouop-query.php",
"bin/csv-example.php",
"bin/csv-import.php"
],
Expand Down
22 changes: 17 additions & 5 deletions db/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,29 @@ 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.
*/
public static function delete()
{
global $DB; // Moodle global.

return $DB->delete_records(self::USER_TABLE);
}

Expand Down Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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 ]
Expand Down

0 comments on commit 7b88e13

Please sign in to comment.