/* Sets up the daily quiz, making all the io requests in parallel. */ var TheDailyQuiz = Class.create({ appUrl : null, addUrl : null, dailyQuizUrl : null, appParams : null, currentUser : null, userInfo : null, currentQuiz : null, currentState : null, errorFlag : null, readyInterval : null, readyCount : 0, measure : null, initialize : function(_appUrl, _addUrl, _dailyQuizUrl) { this.appUrl = _appUrl; this.addUrl = _addUrl; this.dailyQuizUrl = _dailyQuizUrl; this.measure = new Metrics(_dailyQuizUrl); this.appParams = this.getAppParams(); // Analytics var userFrom = 'unknown'; if (this.appParams != null && this.appParams.from != null) userFrom = this.appParams.from; // Ask new users to add app var osEnv = gadgets.views.getParams(); if (osEnv.installState == '0' || osEnv.installState == 0) { this.measure.google('newuser', userFrom); window.top.location = this.addUrl; return; } this.measure.google('returninguser', userFrom); this.readyInterval = window.setInterval(this.goReady.bind(this), 100); this.requestCurrentUser(); this.requestUserInfo(); this.requestCurrentQuiz(null); }, goReady : function() { if (this.errorFlag == true) { window.clearInterval(this.readyInterval); this.measure.google('load-error', 'loading-canvas'); Debug.error('Error flag raised!'); $('loadNotice').hide(); $('loadError').show(); } else if (this.readyCount == 0) { window.clearInterval(this.readyInterval); var userState = 'newuser-load-success'; if (this.currentUser.isAppUser) userState = 'appuser-load-success'; // this.measure.google(userState, 'loading-canvas'); Debug.info('The Daily Quiz is loaded and ready!'); $('loadNotice').hide(); $('canvas').show(); this.goEditor(); } }, requestCurrentQuiz : function(handle) { Debug.log('DailyQuiz.requestCurrentQuiz'); // Setup call var params = {}; params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON; params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.NONE; params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET; var serviceUrl = this.dailyQuizUrl + 'quiz_json.php'; if (handle != null && handle.length > 0) serviceUrl += '?quiz_handle=' + handle; ++this.readyCount; Debug.info('Sending current quiz req... ' + serviceUrl); gadgets.io.makeRequest(serviceUrl, this.handleCurrentQuizRequest.bind(this), params); }, handleCurrentQuizRequest : function(response) { Debug.log('TheDailyQuiz.handleCurrentQuizRequest'); // this.measure.google('quiz-edit', 'thedailyquiz'); if (response.errorCode) { Debug.error('TheDailyQuiz.handleCurrentQuizRequest: ' + response.errorCode); this.errorFlag = true; return; } this.currentQuiz = gadgets.json.parse(response.text); Debug.group('Got current quiz.'); Debug.dump(this.currentQuiz); Debug.groupEnd(); --this.readyCount; }, requestUserInfo : function() { Debug.log('DailyQuiz.requestUserInfo'); // Setup call var params = {}; params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON; params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED; params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET; var serviceUrl = this.dailyQuizUrl + 'load.php'; // this is a non-blocking load :) // ++this.readyCount; Debug.info('Sending user info req... ' + serviceUrl); gadgets.io.makeRequest(serviceUrl, this.handleUserInfoRequest.bind(this), params); }, handleUserInfoRequest : function(response) { Debug.log('TheDailyQuiz.handleUserInfoRequest'); if (response.errorCode) { Debug.error('TheDailyQuiz.handleUserInfoRequest: ' + response.errorCode); this.errorFlag = true; return; } this.userInfo = gadgets.json.parse(response.text); Debug.group('Got user info.'); Debug.dump(this.userInfo); Debug.groupEnd(); // this is a non-blocking load :) // --this.readyCount; }, requestCurrentUser : function() { Debug.log('DailyQuiz.requestCurrentUser'); // Get viewer, viewer friends, viewer friends with app var req = opensocial.newDataRequest(); req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER), 'viewer'); // get all friends, and friends with app var friendIds = opensocial.newIdSpec({ userId: 'VIEWER', groupId: 'FRIENDS' }); var friendParams = { }; friendParams[opensocial.DataRequest.PeopleRequestFields.FILTER] = opensocial.DataRequest.FilterType.HAS_APP; req.add(req.newFetchPeopleRequest(friendIds), 'viewer_friends'); req.add(req.newFetchPeopleRequest(friendIds, friendParams), 'viewer_friends_with_app'); ++this.readyCount; Debug.info('Sending current user req...'); req.send(this.handleCurrentUserRequest.bind(this)); }, handleCurrentUserRequest : function(response) { Debug.log('DailyQuiz.handleCurrentUserRequest'); var viewer_data = null; var viewer_friends_data = null; var viewer_friends_with_app_data = null; // Get friend data var viewer_friends = response.get('viewer_friends'); if (viewer_friends.hadError()) { Debug.error('DailyQuiz.handleCurrentUserRequest: ' + viewer_friends.getErrorMessage()); this.measure.google('error', 'canvas-handleCurrentUserRequest'); this.errorFlag = true; return; } else { viewer_friends_data = viewer_friends.getData().asArray(); Debug.info('Got current user friends: ' + viewer_friends_data.length); } // Get friends with app data var viewer_friends_with_app = response.get('viewer_friends_with_app'); if (viewer_friends_with_app.hadError()) { Debug.error('DailyQuiz.handleCurrentUserRequest: ' + viewer_friends_with_app.getErrorMessage()); this.errorFlag = true; return; } else { viewer_friends_with_app_data = viewer_friends_with_app.getData().asArray(); Debug.info('Got current user friends with app: ' + viewer_friends_with_app_data.length); } // Get current user data var viewer = response.get('viewer'); if (viewer.hadError()) { Debug.error('DailyQuiz.handleCurrentUserRequest: ' + viewer.getErrorMessage()); this.errorFlag = true; return; } else { Debug.info('Got current user.'); viewer_data = viewer.getData(); var isAppUser = true; var osEnv = gadgets.views.getParams(); if (osEnv.installState == '0' || osEnv.installState == 0) isAppUser = false; this.currentUser = new User(viewer_data, isAppUser); this.currentUser.addFriends(viewer_friends_data, viewer_friends_with_app_data); } Debug.group('Got current user: ' + this.currentUser.msid); Debug.dump(this.currentUser); Debug.groupEnd(); --this.readyCount; }, getAppParams : function() { if (this.appParams != null) return this.appParams; Debug.group('Getting params.'); var locationParams = document.location.toString(); locationParams = locationParams.split('?'); if (locationParams.length >= 2) { locationParams = locationParams[1].split('&'); for (var i = 0; i < locationParams.length; ++i) { Debug.dump(locationParams[1]); if (locationParams[i].substr(0,2) == 'p=') { var pParam = unescape(decodeURI(locationParams[i].substr(2))); this.appParams = gadgets.json.parse(pParam); Debug.info('Found app params: '); Debug.dump(this.appParams); return this.appParams; } } } Debug.warn('No app params.'); Debug.groupEnd(); }, /* State transitions start here. */ goEditor : function() { Debug.log('goEditor'); this.currentState = new QuizEditorState(this.currentUser, this.currentQuiz, $('canvas'), this.goSaveQuiz.bind(this)); }, goSaveQuiz : function(quiz) { Debug.log('goSaveQuiz'); // this.measure.invite('edit'); this.currentQuiz = quiz; this.goAskBulletin(); }, goAskProfile : function(status) { Debug.log('goAskProfile'); this.measure.google('profile-ask', 'edit'); this.currentState = new DialogState(this.currentUser, $('canvas'), 'Put your quiz on your profile?', ' Yes ', this.goPostToProfile.bind(this), ' No ', this.goAskBulletin.bind(this)); }, goPostToProfile : function () { Debug.log('goPostToProfile'); this.measure.google('profile-post', 'profile-ask'); var os_token = MyOpenSpace.MySpaceContainer.OSToken; var osContainer = opensocial.Container.get(); var supported = osContainer.getMySpaceEnvironment().getSupportedPostToTargets(); var skip = true; for (var i = 0; i < supported.length; ++i) { if (supported[i] == MyOpenSpace.PostTo.Targets.PROFILE) skip = false; } if (skip) { this.goAskBulletin(); } else { var caption = '
