Skip to content

Commit

Permalink
Bug #1, db/install XML, User model, csv-import script [iet:8336…
Browse files Browse the repository at this point in the history
…249]

* [iet:8269283]
  • Loading branch information
nfreear committed Mar 13, 2017
1 parent 211fc82 commit 6aa69c1
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 10 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
*.css
*.csv

/bin
/user
/js
#/bin

#.travis.yml
phpcs.xml

composer.lock
Expand Down
2 changes: 1 addition & 1 deletion bin/csv-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

define( 'FLAG_BIG', '--big' );
define( 'CSV_HEADINGS', 'oucu,course_present,tesla_instrument,notes' );
define( 'CSV_HEADINGS', 'oucu,course_present,tesla_instrument,notes,is_team,firstname,lastname,email' );
define( 'CSV_BIG_TIMES', 100 ); // 4000 x 5 = 20,000.
define( 'CSV_FILENAME', './example.csv' );
define( 'CSV_BIG_FILENAME', './example-big.csv' );
Expand Down
32 changes: 32 additions & 0 deletions bin/csv-import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Simple CSV to database import script.
*
* @author Nick Freear, 13-March-2017.
*/
require_once __DIR__ . '/../../../config.php';
require_once __DIR__ . '/../../../vendor/autoload.php';
//require_once __DIR__ . '/../vendor/autoload.php';

use IET_OU\Moodle\Auth\Ouopenid\Db\User as OuUser;

define( 'CSV_FILE', __DIR__ . '/../example.csv' );


$csv_file = CSV_FILE;

echo "OU-OpenID importer. Filename: $csv_file\n";

if (argc > 1 && $argv[ $argc - 1 ] === '--delete') {
OuUser::delete();
echo "User table emptied.\n";
}

$count = OuUser::insertFromCsv($csv_file, true, function ($idx, $user_id) {
echo '.';
});

echo "\nUsers inserted: $count\n";


//End.
14 changes: 11 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
{
"name": "iet-ou/moodle-auth-ouopenid",
"description": "This plugin is a wrapper around the OpenID authentication plugin for Moodle. ©2017 The Open University.",
"description": "This plugin is a wrapper around the OpenID authentication plugin for Moodle. ©The Open University.",
"homepage": "https://github.com/IET-OU",
"type": "moodle-auth",
"license": "proprietary",
"time": "2017-03-06",
"support": {
"source": "https://github.com/IET-OU/moodle-auth_ouopenid"
},
"authors": [ { "name": "Nick Freear" } ],
"autoload": {
"psr-4": {
"IET_OU\\Moodle\\Auth\\Ouopenid\\Db\\": "db/"
}
},
"require": {
"php": ">=5.5.9",
"composer/installers": "^1.2"
},
"require-dev": {
"jakub-onderka/php-parallel-lint": "^0.9.2",
"squizlabs/php_codesniffer": "2.5.1"
"squizlabs/php_codesniffer": "2.8.1"
},
"scripts": {
"npm-install": "npm install eslint",
Expand All @@ -29,7 +35,9 @@
],
"test": [
"vendor/bin/parallel-lint --exclude vendor .",
"composer cn"
"vendor/bin/phpcs --standard=PSR2 -n db/*.php",
"composer cn",
"php -r 'simplexml_load_file(\"db/install.xml\");' && echo install.xml OK!"
],
"phpcs": "vendor/bin/phpcs --standard=./phpcs.xml --ignore=vendor --ignore=.git --extensions=php .",
"cn": "vendor/bin/phpcs --standard=PHPCS --sniffs=Generic.NamingConventions.ConstructorName --extensions=php --ignore=vendor ."
Expand Down
100 changes: 100 additions & 0 deletions db/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php namespace IET_OU\Moodle\Auth\Ouopenid\Db;

/**
* DB model for an OU-OpenID 'User' or potential pilot participant.
*
* (Note: follows PSR-2, not Moodle coding style.)
*
* @author Nick Freear, 13-March-2017.
* @copyright (c) 2017 The Open University.
*
* @link https://docs.moodle.org/dev/Data_manipulation_API
* @link https://github.com/goodby/csv#import-to-database-via-pdo
* @link http://csv.thephpleague.com/8.0/examples/#importing-a-csv-into-a-database-table
*/

//require_once __DIR__ . '/../vendor/autoload.php';

use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;

global $DB;

class User
{

const USER_TABLE = 'auth_ouopenid_users';
const CSV_OUCU = 0; // CSV file column offsets.
const CSV_TEAM = 4;

public static function getUser($oucu)
{
$user = $DB->get_record(self::USER_TABLE, [ 'oucu' => $oucu ], $fields = '*', $strictness = IGNORE_MISSING);
return $user;
}

public static function delete()
{
return $DB->delete_records(self::USER_TABLE);
}

public static function insertUser($user)
{
$user_record = (object) [
'oucu' => $user->oucu,
'course_presentation' => $user->presentation,
'teslainstrument' => $user->teslaintrument,
'is_team' => $user->is_team,
'firstname' => $user->firstname,
'lastname' => $user->lastname,
'email' => $user->email,
'timecreated' => time(),
];
$user_id = $DB->insert_record(self::USER_TABLE, $user_record, $returnid = true);
return $user_id;
}

public static function insertFromCsv($filename = '../example.csv', $ignore_heading = true, $callback = null)
{
$config = new LexerConfig();
$lexer = new Lexer($config);

$interpreter = new Interpreter();
//$interpreter->unstrict(); // Ignore row column count consistency

$count = 0;

$interpreter->addObserver(function (array $row) use ($DB, &$count, $ignore_heading, $callback) {
$count++;
if ($count <= 1 && $ignore_heading) {
echo "Ignore CSV heading row.\n";
return;
}
//$stmt = $pdo->prepare('INSERT INTO user (id, name, email) VALUES (?, ?, ?)');
//$stmt->execute($columns);
$user_record = (object) [
'oucu' => $row[ self::CSV_OUCU ], # 0,
'course_presentation' => $row[ self::CSV_OUCU + 1 ], # 1,
'teslainstrument' => $row[ self::CSV_OUCU + 2 ],
//'notes' => $row[ self::CSV_OUCU + 3 ], # 3,
'is_team' => $row[ self::CSV_TEAM ], # 4,
'firstname' => $row[ self::CSV_TEAM + 1 ], # 5,
'lastname' => $row[ self::CSV_TEAM + 2 ], # 6,
'email' => $row[ self::CSV_TEAM + 3 ], # 7,
'timecreated' => time(),
];
$user_id = $DB->insert_record(self::USER_TABLE, $user_record, $returnid = true);

if ($callback) {
$callback ($count, $user_id);
}
});

$lexer->parse($filename, $interpreter);

return $count;
}
}

//End.
27 changes: 27 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20170313" COMMENT="XMLDB file for OU-OpenId authentication."
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd">
<TABLES>
<TABLE NAME="auth_ouopenid_users" COMMENT="List of potential pilot students, and TeSLA team members (CSV).">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="oucu" TYPE="char" LENGTH="32" NOTNULL="true" SEQUENCE="false" COMMENT="Open University login, eg. 'ab12', 'xyz4321'"/>
<FIELD NAME="course_presentation" TYPE="char" LENGTH="32" NOTNULL="true" SEQUENCE="false" COMMENT="Open University course module presentation code, eg. 'AA100-J'"/>
<FIELD NAME="teslainstrument" TYPE="char" LENGTH="16" NOTNULL="true" SEQUENCE="false" COMMENT="Initial setting for a TeSLA istrument for the user, eg. 'kd'"/>
<FIELD NAME="notes" TYPE="char" LENGTH="150" NOTNULL="false" SEQUENCE="false" COMMENT="Optional free-form text ??"/>
<FIELD NAME="is_team" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT="Is the user a member of the TeSLA team? Boolean."/>
<FIELD NAME="firstname" TYPE="char" LENGTH="64" NOTNULL="false" SEQUENCE="false" COMMENT="Empty for a student."/>
<FIELD NAME="lastname" TYPE="char" LENGTH="64" NOTNULL="false" SEQUENCE="false" COMMENT="Empty for a student."/>
<FIELD NAME="email" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="Empty for a student."/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Time-date."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="oucu" UNIQUE="false" FIELDS="oucu"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* @package ouopenid
*/

defined('MOODLE_INTERNAL') || die();
//defined('MOODLE_INTERNAL') || die();

$plugin->version = 2017030600; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2017031300; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2015101600; // Requires this Moodle version
$plugin->component = 'auth_ouopenid';

0 comments on commit 6aa69c1

Please sign in to comment.