Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customise RCITI branch #778

Merged
merged 8 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion www/js/control/general-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ angular.module('emission.main.control',['emission.services',
'ionic-datepicker.provider',
'emission.splash.startprefs',
'emission.splash.updatecheck',
'emission.survey.launch',
'emission.main.metrics.factory',
'emission.stats.clientstats',
'emission.plugin.kvstore',
Expand All @@ -23,7 +24,7 @@ angular.module('emission.main.control',['emission.services',
ControlTransitionNotifyHelper,
CarbonDatasetHelper,
UpdateCheck, i18nUtils,
CalorieCal, ClientStats, CommHelper, Logger,
CalorieCal, ClientStats, CommHelper, Logger, SurveyLaunch,
$translate) {

var datepickerObject = {
Expand Down Expand Up @@ -446,6 +447,19 @@ angular.module('emission.main.control',['emission.services',
return ionic.Platform.isIOS();
}

$scope.editUserProfile = function() {
SurveyLaunch.startSurveyPrefilled('https://up.byamarin.com')
};

$scope.launchEndSurvey = function() {
SurveyLaunch.startSurveyPrefilled(
'https://pe.byamarin.com',
undefined,
'https://www.taharashidi.com/endsurvey'
)
$scope.endForceSync();
};

$ionicPopover.fromTemplateUrl('templates/control/main-sync-settings.html', {
scope: $scope
}).then(function(popover) {
Expand Down
77 changes: 69 additions & 8 deletions www/js/intro.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

angular.module('emission.intro', ['emission.splash.startprefs',
'emission.splash.updatecheck',
'emission.survey.launch',
'emission.i18n.utils',
'ionic-toast'])

Expand All @@ -22,7 +23,7 @@ angular.module('emission.intro', ['emission.splash.startprefs',

.controller('IntroCtrl', function($scope, $state, $window,
$ionicPlatform, $ionicSlideBoxDelegate,
$ionicPopup, $ionicHistory, ionicToast, $timeout, CommHelper, StartPrefs, UpdateCheck, $translate, i18nUtils) {
$ionicPopup, $ionicHistory, ionicToast, $timeout, CommHelper, StartPrefs, SurveyLaunch, UpdateCheck, $translate, i18nUtils) {

$scope.setupPermissionText = function() {
$scope.platform = $window.device.platform;
Expand Down Expand Up @@ -99,12 +100,25 @@ angular.module('emission.intro', ['emission.splash.startprefs',
});
};

// Adapted from https://stackoverflow.com/a/63363662/4040267
// made available under a CC BY-SA 4.0 license

$scope.generateRandomToken = function(length) {
var randomInts = window.crypto.getRandomValues(new Uint8Array(length * 2));
var randomChars = Array.from(randomInts).map((b) => String.fromCharCode(b));
var randomString = randomChars.join("");
var validRandomString = window.btoa(randomString).replace(/[+/]/g, "");
return validRandomString.substring(0, length);
}

$scope.disagree = function() {
$state.go('root.main.heatmap');
};

$scope.agree = function() {
StartPrefs.markConsented().then(function(response) {
$scope.randomToken = $scope.generateRandomToken(8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would bump this up since it is autogenerated and you are not getting the user to fill it out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is a good number to use here? I'm still deciding whether to use a list of pre-generated tokens or not. One of the benefits is so that I can link my screening questionnaire responses to the trip database.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is not a huge security concern.. Let's leave it like this for now.

window.Logger.log("Signing in with random token "+$scope.randomToken);
$ionicHistory.clearHistory();
if ($state.is('root.intro')) {
$scope.next();
Expand Down Expand Up @@ -134,20 +148,67 @@ angular.module('emission.intro', ['emission.splash.startprefs',
});
}

$scope.login = function() {
window.cordova.plugins.BEMJWTAuth.signIn().then(function(userEmail) {
$scope.startSurvey = function () {
SurveyLaunch.startSurveyPrefilled(
'https://up.byamarin.com/' // redirect link
);
}

$scope.tokenToClipboard = function() {
navigator.clipboard.writeText($scope.randomToken);
};

$scope.loginNew = function() {
$scope.login($scope.randomToken);
};

$scope.loginExisting = function() {
$scope.data = {};
const tokenPopup = $ionicPopup.show({
template: '<input type="String" ng-model="data.existing_token">',
title: 'Enter the existing token that you have',
scope: $scope,
buttons: [
{
text: '<b>OK</b>',
type: 'button-positive',
onTap: function(e) {
if (!$scope.data.existing_token) {
//don't allow the user to close unless he enters a username

e.preventDefault();
} else {
return $scope.data.existing_token;
}
}
},{
text: '<b>Cancel</b>',
type: 'button-stable',
onTap: function(e) {
return null;
}
}
]
});
tokenPopup.then(function(token) {
if (token != null) {
$scope.login(token);
}
}).catch(function(err) {
$scope.alertError(err);
});
};

$scope.login = function(token) {
window.cordova.plugins.BEMJWTAuth.setPromptedAuthToken(token).then(function(userEmail) {
// ionicToast.show(message, position, stick, time);
// $scope.next();
ionicToast.show(userEmail, 'middle', false, 2500);
if (userEmail == "null" || userEmail == "") {
$scope.alertError("Invalid login "+userEmail);
} else {
CommHelper.registerUser(function(successResult) {
UpdateCheck.getChannel().then(function(retVal) {
CommHelper.updateUser({
client: retVal
});
});
$scope.startSurvey();
$scope.finish();
}, function(errorResult) {
$scope.alertError('User registration error', errorResult);
Expand Down
19 changes: 15 additions & 4 deletions www/js/survey/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,26 @@ angular.module('emission.survey.launch', ['emission.services',
startSurveyCommon(url, elementXPath, "js/survey/uuid_insert_xpath.js");
}

surveylaunch.startSurveyPrefilled = function (url, uuidSearchParam) {
surveylaunch.startSurveyPrefilled = function (url, uuidSearchParam, returnUrl) {
CommHelper.getUser().then(function(userProfile) {
// alert("finished loading script");
let uuid = userProfile.user_id['$uuid']
Logger.log("inserting user id into survey. userId = "+ uuid
+" base url = "+url);
let urlObj = new URL(url);
urlObj.searchParams.append(uuidSearchParam, uuid);
let modifiedURL = urlObj.href;
let modifiedURL;
if (!uuidSearchParam) {
modifiedURL = url + '/' + uuid
} else {
let urlObj = new URL(url);
urlObj.searchParams.append(uuidSearchParam, uuid);
modifiedURL = urlObj.href;
}
if (returnUrl) {
// NOTE: We manually append with & to make it compatible when we embed uuid as path param
// i.e. http://pe.byamarin.com/${uuid}&return_url=https://www.google.com
// However, if uuid is appended normally `return_url` will correctly be appended with & anyways.
modifiedURL = `${modifiedURL}&return_url=${encodeURIComponent(returnUrl)}`;
}
Logger.log("modified URL = "+modifiedURL);
let iab = $window.cordova.InAppBrowser.open(modifiedURL, '_blank', surveylaunch.options);
iab.addEventListener('loaderror', function(event) {
Expand Down
2 changes: 1 addition & 1 deletion www/json/emailConfig.json.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"address": "shankari@eecs.berkeley.edu"
"address": "a.siripanich@unsw.edu.au"
}
12 changes: 12 additions & 0 deletions www/templates/control/main-control.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
<div class="control-list-text">{{settings.auth.email}}</div>
<div ng-click="returnToIntro()" id ="switch-user" class="control-icon-button"><i class="icon ion-log-out"></i></div>
</div>
<div class="control-list-item">
<div class="control-list-text">Edit User Profile</div>
<div ng-click="editUserProfile()" class="control-icon-button control-icon-user-profile">
<i class="ion-person"></i>
</div>
</div>
<div class="control-list-item">
<div class="control-list-text">End Survey</div>
<div ng-click="launchEndSurvey()" class="control-icon-button control-icon-end-survey">
<i class="ion-document-text"></i>
</div>
</div>
<div class="control-list-item">
<div class="control-list-text" translate>{{'.view-privacy'}}</div>
<div ng-click="viewPrivacyPolicy($event)" class="control-icon-button"><i class="ion-eye"></i></div>
Expand Down
Loading