From a779e69b4987628deecc483b8b87e988947e16c0 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 10 Sep 2019 09:39:15 +0200 Subject: [PATCH] MOBILE-3109 survey: Add return types to survey --- .../index/addon-mod-survey-index.html | 12 +-- .../mod/survey/components/index/index.ts | 16 ++-- src/addon/mod/survey/providers/helper.ts | 29 +++++-- src/addon/mod/survey/providers/survey.ts | 87 ++++++++++++++++--- 4 files changed, 111 insertions(+), 33 deletions(-) diff --git a/src/addon/mod/survey/components/index/addon-mod-survey-index.html b/src/addon/mod/survey/components/index/addon-mod-survey-index.html index da07713d4..3f5eec9f1 100644 --- a/src/addon/mod/survey/components/index/addon-mod-survey-index.html +++ b/src/addon/mod/survey/components/index/addon-mod-survey-index.html @@ -36,14 +36,14 @@ -
+

{{ question.text }}

{{ 'addon.mod_survey.responses' | translate }}
- +
{{ option }}
@@ -61,20 +61,20 @@ - + {{ 'core.choose' | translate }} - {{option}} + {{option}}
- + @@ -82,7 +82,7 @@ - {{option}} + {{option}} diff --git a/src/addon/mod/survey/components/index/index.ts b/src/addon/mod/survey/components/index/index.ts index 23f479553..e1715f02a 100644 --- a/src/addon/mod/survey/components/index/index.ts +++ b/src/addon/mod/survey/components/index/index.ts @@ -15,8 +15,8 @@ import { Component, Optional, Injector } from '@angular/core'; import { Content } from 'ionic-angular'; import { CoreCourseModuleMainActivityComponent } from '@core/course/classes/main-activity-component'; -import { AddonModSurveyProvider } from '../../providers/survey'; -import { AddonModSurveyHelperProvider } from '../../providers/helper'; +import { AddonModSurveyProvider, AddonModSurveySurvey } from '../../providers/survey'; +import { AddonModSurveyHelperProvider, AddonModSurveyQuestionFormatted } from '../../providers/helper'; import { AddonModSurveyOfflineProvider } from '../../providers/offline'; import { AddonModSurveySyncProvider } from '../../providers/sync'; @@ -31,8 +31,8 @@ export class AddonModSurveyIndexComponent extends CoreCourseModuleMainActivityCo component = AddonModSurveyProvider.COMPONENT; moduleName = 'survey'; - survey: any; - questions: any; + survey: AddonModSurveySurvey; + questions: AddonModSurveyQuestionFormatted[]; answers = {}; protected userId: number; @@ -103,7 +103,7 @@ export class AddonModSurveyIndexComponent extends CoreCourseModuleMainActivityCo return this.surveyProvider.getSurvey(this.courseId, this.module.id).then((survey) => { this.survey = survey; - this.description = survey.intro || survey.description; + this.description = survey.intro; this.dataRetrieved.emit(survey); if (sync) { @@ -144,13 +144,13 @@ export class AddonModSurveyIndexComponent extends CoreCourseModuleMainActivityCo // Init answers object. this.questions.forEach((q) => { if (q.name) { - const isTextArea = q.multi && q.multi.length === 0 && q.type === 0; + const isTextArea = q.multiArray && q.multiArray.length === 0 && q.type === 0; this.answers[q.name] = q.required ? -1 : (isTextArea ? '' : '0'); } - if (q.multi && !q.multi.length && q.parent === 0 && q.type > 0) { + if (q.multiArray && !q.multiArray.length && q.parent === 0 && q.type > 0) { // Options shown in a select. Remove all HTML. - q.options = q.options.map((option) => { + q.optionsArray = q.optionsArray.map((option) => { return this.textUtils.cleanTags(option); }); } diff --git a/src/addon/mod/survey/providers/helper.ts b/src/addon/mod/survey/providers/helper.ts index 23d7dc9b4..1553bd9d4 100644 --- a/src/addon/mod/survey/providers/helper.ts +++ b/src/addon/mod/survey/providers/helper.ts @@ -14,6 +14,7 @@ import { Injectable } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; +import { AddonModSurveyQuestion } from './survey'; /** * Service that provides helper functions for surveys. @@ -29,7 +30,7 @@ export class AddonModSurveyHelperProvider { * @param value Value to convert. * @return Array. */ - protected commaStringToArray(value: any): string[] { + protected commaStringToArray(value: string | string[]): string[] { if (typeof value == 'string') { if (value.length > 0) { return value.split(','); @@ -47,7 +48,7 @@ export class AddonModSurveyHelperProvider { * @param questions Questions. * @return Object with parent questions. */ - protected getParentQuestions(questions: any[]): any { + protected getParentQuestions(questions: AddonModSurveyQuestion[]): {[id: number]: AddonModSurveyQuestion} { const parents = {}; questions.forEach((question) => { @@ -66,24 +67,24 @@ export class AddonModSurveyHelperProvider { * @param questions Questions. * @return Promise resolved with the formatted questions. */ - formatQuestions(questions: any[]): any[] { + formatQuestions(questions: AddonModSurveyQuestion[]): AddonModSurveyQuestionFormatted[] { const strIPreferThat = this.translate.instant('addon.mod_survey.ipreferthat'), strIFoundThat = this.translate.instant('addon.mod_survey.ifoundthat'), strChoose = this.translate.instant('core.choose'), - formatted = [], + formatted: AddonModSurveyQuestionFormatted[] = [], parents = this.getParentQuestions(questions); let num = 1; questions.forEach((question) => { // Copy the object to prevent modifying the original. - const q1 = Object.assign({}, question), + const q1: AddonModSurveyQuestionFormatted = Object.assign({}, question), parent = parents[q1.parent]; // Turn multi and options into arrays. - q1.multi = this.commaStringToArray(q1.multi); - q1.options = this.commaStringToArray(q1.options); + q1.multiArray = this.commaStringToArray(q1.multi); + q1.optionsArray = this.commaStringToArray(q1.options); if (parent) { // It's a sub-question. @@ -114,7 +115,7 @@ export class AddonModSurveyHelperProvider { q1.name = 'q' + q1.id; q1.num = num++; if (q1.type > 0) { // Add "choose" option since this question is not required. - q1.options.unshift(strChoose); + q1.optionsArray.unshift(strChoose); } } @@ -123,5 +124,15 @@ export class AddonModSurveyHelperProvider { return formatted; } - } + +/** + * Survey question with some calculated data. + */ +export type AddonModSurveyQuestionFormatted = AddonModSurveyQuestion & { + required?: boolean; // Calculated in the app. Whether the question is required. + name?: string; // Calculated in the app. The name of the question. + num?: number; // Calculated in the app. Number of the question. + multiArray?: string[]; // Subquestions ids, converted to an array. + optionsArray?: string[]; // Question options, converted to an array. +}; diff --git a/src/addon/mod/survey/providers/survey.ts b/src/addon/mod/survey/providers/survey.ts index 078fae02c..428a3db39 100644 --- a/src/addon/mod/survey/providers/survey.ts +++ b/src/addon/mod/survey/providers/survey.ts @@ -21,6 +21,7 @@ import { CoreFilepoolProvider } from '@providers/filepool'; import { CoreCourseLogHelperProvider } from '@core/course/providers/log-helper'; import { AddonModSurveyOfflineProvider } from './offline'; import { CoreSite, CoreSiteWSPreSets } from '@classes/site'; +import { CoreWSExternalWarning, CoreWSExternalFile } from '@providers/ws'; /** * Service that provides some features for surveys. @@ -46,7 +47,7 @@ export class AddonModSurveyProvider { * @param siteId Site ID. If not defined, current site. * @return Promise resolved when the questions are retrieved. */ - getQuestions(surveyId: number, ignoreCache?: boolean, siteId?: string): Promise { + getQuestions(surveyId: number, ignoreCache?: boolean, siteId?: string): Promise { return this.sitesProvider.getSite(siteId).then((site) => { const params = { surveyid: surveyId @@ -61,7 +62,9 @@ export class AddonModSurveyProvider { preSets.emergencyCache = false; } - return site.read('mod_survey_get_questions', params, preSets).then((response) => { + return site.read('mod_survey_get_questions', params, preSets) + .then((response: AddonModSurveyGetQuestionsResult): any => { + if (response.questions) { return response.questions; } @@ -101,7 +104,9 @@ export class AddonModSurveyProvider { * @param siteId Site ID. If not defined, current site. * @return Promise resolved when the survey is retrieved. */ - protected getSurveyDataByKey(courseId: number, key: string, value: any, ignoreCache?: boolean, siteId?: string): Promise { + protected getSurveyDataByKey(courseId: number, key: string, value: any, ignoreCache?: boolean, siteId?: string) + : Promise { + return this.sitesProvider.getSite(siteId).then((site) => { const params = { courseids: [courseId] @@ -116,7 +121,9 @@ export class AddonModSurveyProvider { preSets.emergencyCache = false; } - return site.read('mod_survey_get_surveys_by_courses', params, preSets).then((response) => { + return site.read('mod_survey_get_surveys_by_courses', params, preSets) + .then((response: AddonModSurveyGetSurveysByCoursesResult): any => { + if (response && response.surveys) { const currentSurvey = response.surveys.find((survey) => { return survey[key] == value; @@ -140,7 +147,7 @@ export class AddonModSurveyProvider { * @param siteId Site ID. If not defined, current site. * @return Promise resolved when the survey is retrieved. */ - getSurvey(courseId: number, cmId: number, ignoreCache?: boolean, siteId?: string): Promise { + getSurvey(courseId: number, cmId: number, ignoreCache?: boolean, siteId?: string): Promise { return this.getSurveyDataByKey(courseId, 'coursemodule', cmId, ignoreCache, siteId); } @@ -153,7 +160,7 @@ export class AddonModSurveyProvider { * @param siteId Site ID. If not defined, current site. * @return Promise resolved when the survey is retrieved. */ - getSurveyById(courseId: number, id: number, ignoreCache?: boolean, siteId?: string): Promise { + getSurveyById(courseId: number, id: number, ignoreCache?: boolean, siteId?: string): Promise { return this.getSurveyDataByKey(courseId, 'id', id, ignoreCache, siteId); } @@ -277,17 +284,16 @@ export class AddonModSurveyProvider { * @param surveyId Survey ID. * @param answers Answers. * @param siteId Site ID. If not defined, current site. - * @return Promise resolved when answers are successfully submitted. Rejected with object containing - * the error message (if any) and a boolean indicating if the error was returned by WS. + * @return Promise resolved when answers are successfully submitted. */ - submitAnswersOnline(surveyId: number, answers: any[], siteId?: string): Promise { + submitAnswersOnline(surveyId: number, answers: any[], siteId?: string): Promise { return this.sitesProvider.getSite(siteId).then((site) => { const params = { surveyid: surveyId, answers: answers }; - return site.write('mod_survey_submit_answers', params).then((response) => { + return site.write('mod_survey_submit_answers', params).then((response: AddonModSurveySubmitAnswersResult) => { if (!response.status) { return Promise.reject(this.utils.createFakeWSError('')); } @@ -295,3 +301,64 @@ export class AddonModSurveyProvider { }); } } + +/** + * Survey returned by WS mod_survey_get_surveys_by_courses. + */ +export type AddonModSurveySurvey = { + id: number; // Survey id. + coursemodule: number; // Course module id. + course: number; // Course id. + name: string; // Survey name. + intro?: string; // The Survey intro. + introformat?: number; // Intro format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN). + introfiles?: CoreWSExternalFile[]; // @since 3.2. + template?: number; // Survey type. + days?: number; // Days. + questions?: string; // Question ids. + surveydone?: number; // Did I finish the survey?. + timecreated?: number; // Time of creation. + timemodified?: number; // Time of last modification. + section?: number; // Course section id. + visible?: number; // Visible. + groupmode?: number; // Group mode. + groupingid?: number; // Group id. +}; + +/** + * Survey question. + */ +export type AddonModSurveyQuestion = { + id: number; // Question id. + text: string; // Question text. + shorttext: string; // Question short text. + multi: string; // Subquestions ids. + intro: string; // The question intro. + type: number; // Question type. + options: string; // Question options. + parent: number; // Parent question (for subquestions). +}; + +/** + * Result of WS mod_survey_get_questions. + */ +export type AddonModSurveyGetQuestionsResult = { + questions: AddonModSurveyQuestion[]; + warnings?: CoreWSExternalWarning[]; +}; + +/** + * Result of WS mod_survey_get_surveys_by_courses. + */ +export type AddonModSurveyGetSurveysByCoursesResult = { + surveys: AddonModSurveySurvey[]; + warnings?: CoreWSExternalWarning[]; +}; + +/** + * Result of WS mod_survey_submit_answers. + */ +export type AddonModSurveySubmitAnswersResult = { + status: boolean; // Status: true if success. + warnings?: CoreWSExternalWarning[]; +};