-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…249] * [iet:8269283]
- Loading branch information
Showing
7 changed files
with
174 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,8 @@ | |
*.css | ||
*.csv | ||
|
||
/bin | ||
/user | ||
/js | ||
#/bin | ||
|
||
#.travis.yml | ||
phpcs.xml | ||
|
||
composer.lock | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters