Skip to content

Commit

Permalink
Bug #5, DB upgrade for multi-survey URL capability [iet:8853003]
Browse files Browse the repository at this point in the history
* 5000 student-limit on BOS surveys
  • Loading branch information
nfreear committed May 3, 2017
1 parent ece48c7 commit 0a39dc0
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"semistandard": "node_modules/.bin/semistandard",
"test": [
"vendor/bin/parallel-lint --exclude vendor .",
"vendor/bin/phpcs --standard=PSR2 -n db/*.php",
"vendor/bin/phpcs --standard=PSR2 -n db/User.php",
"composer cn",
"composer semistandard",
"composer eslint",
Expand Down
21 changes: 19 additions & 2 deletions db/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class User

const CSV_OUCU = 0; // CSV file column offsets.
const CSV_TEAM = 4;
const CSV_BATCH = 8; // TODO: bug #5.

const PREFIX = 'ouop_';
const UNDEF_INSTRUMENT = 'tpt'; //'kd';
Expand Down Expand Up @@ -120,6 +121,7 @@ public static function insertFromCsv($filename = '../example.csv', $ignore_headi
'lastname' => self::row($row, self::CSV_TEAM + 2), # 6,
'email' => self::row($row, self::CSV_TEAM + 3), # 7,
'timecreated' => time(),
'batch' => self::row($row, self::CSV_BATCH, 0),
];
$user_id = $DB->insert_record(self::USER_TABLE, $user_record, $returnid = true);

Expand Down Expand Up @@ -199,6 +201,7 @@ public static function getMoodleProfile($mdl_user)
'profile' => (object) [],
'body_class' => null,
'redirect_url' => null,
'survey_urls' => null,
];
}

Expand Down Expand Up @@ -229,9 +232,23 @@ public static function getMoodleProfile($mdl_user)
'profile' => (object) $profile,
'body_class' => self::bodyClasses($profile),
'redirect_url' => self::getRedirectUrl($mdl_profile),
'survey_urls' => self::getSurveyUrls($profile),
];
}

public static function getSurveyUrls($profile)
{
global $CFG; // Moodle global;

$survey_urls = $CFG->auth_ouopenid_survey_urls;

$batch = $profile->batch;
if (! isset($survey_urls[ $batch ])) {
self::debug([ __FUNCTION__, 'error', ]);
}
return isset($survey_urls[ $batch ]) ? $survey_urls[ $batch ] : $survey_urls[ 0 ];
}

/** Get Moodle roles for currently logged in user.
* @return object
*/
Expand Down Expand Up @@ -325,9 +342,9 @@ public static function debugLevel()
return $level;
}

protected static function row($row, $offset)
protected static function row($row, $offset, $default = null)
{
return isset($row[ $offset ]) ? $row[ $offset ] : null;
return isset($row[ $offset ]) ? $row[ $offset ] : $default;
}

protected static function bodyClasses($fields)
Expand Down
7 changes: 5 additions & 2 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20170314" COMMENT="XMLDB file for OU-OpenId authentication."
<XMLDB PATH="auth/openid/db" VERSION="20170428" COMMENT="XMLDB file for OU-OpenId authentication."
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd">
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd">
<!--
@package auth_ouopenid
@copyright (c) 2017 The Open University.
Expand All @@ -19,6 +19,9 @@
<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."/>

<!-- New fields. -->
<FIELD NAME="batch" TYPE="int" LENGTH="5" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT="Optional; a batch or tranche number within the student sample."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
61 changes: 61 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* OU-OpenID database upgrade (update) function(s).
*
* @package auth_ouopenid
* @author Nick Freear, 01-May-2017.
* @copyright (c) 2017 The Open University.
* @license proprietary
*/

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

function xmldb_auth_ouopenid_upgrade($oldversion) {
global $DB; // Moodle global.

$TABLE_NAME = 'auth_ouopenid_users';
$SAVE_POINT_1 = 2017042800;

__auth_ouopenid_debug(__FUNCTION__);

$dbman = $DB->get_manager();

if ($oldversion < 2017042800) {

// Create table for agreement.
$table = new xmldb_table('auth_ouopenid_users');

// Adding fields to table ..users.
// $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
// $table->add_field('version', XMLDB_TYPE_CHAR, '5', null, null, null, null);

// Adding keys to table local_tesla_agreement.
// $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));

// Add activity completed field
// https://github.com/moodle/moodle/blob/master/lib/xmldb/xmldb_field.php#L95
// _($name, $type=null, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $default=null, $previous=null)
// <FIELD NAME="batch" TYPE="int" LENGTH="5" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT=".."/>
$field = new xmldb_field('batch', XMLDB_TYPE_INTEGER, '5', null, XMLDB_NOTNULL, null, '0', null);

if (! $dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);

__auth_ouopenid_debug('add_field');
}

// update the data
// $DB->execute("UPDATE {local_tesla_enrollment} SET done = '1' WHERE data = 'yes'");

// Plugin savepoint reached.
upgrade_plugin_savepoint(true, 2017042800, 'auth', 'ouopenid');

__auth_ouopenid_debug('savepoint');
}
}

function _auth_ouopenid_debug($obj) {
static $count = 0;
header(sprintf('X-auth-ou-openid-upgrade-%02d: %s', $count, json_encode($obj)));
$count++;
}
1 change: 1 addition & 0 deletions user/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'user' => $user,
'profile' => $prof->profile,
'body_class' => $prof->body_class,
'survey_urls'=> $prof->survey_urls,
'user_roles' => OuUser::getRoles(),
'redirect_url' => USER_LOGGED_IN ? $prof->redirect_url : null,
'strings' => USER_LOGGED_IN ? OuUser::getStringsAjax() : [],
Expand Down
15 changes: 10 additions & 5 deletions user/ouop-local-fixes.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,20 @@
};

OUOP.fix_pilot_survey_links = function ($, resp) {
var $links = $('#region-main a[ href *= OUCU ]');
var $links = $('a[ href= "#!-pre-survey-link" ], a[ href= "#!-post-survey-link" ]');
// var $links = $('#region-main a[ href *= OUCU ]');

$links.each(function (idx, el) {
var $link = $(el);
var url = $link.attr('href');

$link
.attr('href', url.replace(/\{?OUCU\}?/, resp.profile.ouop_oucu))
.addClass('ouop-pilot-survey-link').addClass('a' + idx);
var survey_urls = resp.survey_urls; // TODO: bug #5.

if (url.match(/#!-pre-survey-/)) {
$link.attr('href', survey_urls.pre.replace(/\{?OUCU\}?/, resp.profile.ouop_oucu));
} else {
$link.attr('href', survey_urls.post.replace(/\{?OUCU\}?/, resp.profile.ouop_oucu));
}
$link.addClass('ouop-pilot-survey-link').addClass('a' + idx);

C.warn('ouop: pilot-survey-links', idx, $link);
});
Expand Down
10 changes: 7 additions & 3 deletions version.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?php
/**
* OU-OpenID module version information
* OU-OpenID module version information.
*
* @package auth_ouopenid
* @author Nick Freear, 07-March-2017.
* @copyright (c) 2017 The Open University.
* @license proprietary
*/

//defined('MOODLE_INTERNAL') || die();
// Was: defined('MOODLE_INTERNAL') || die(); !

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

$plugin->dependencies = [
'auth_openid' => '2017030600' // TODO: check!
];

0 comments on commit 0a39dc0

Please sign in to comment.