Skip to content

Commit

Permalink
适配最新量表文件结构
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Sep 22, 2024
1 parent 63066f3 commit 0f511f3
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 150 deletions.
137 changes: 57 additions & 80 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ListItem } from 'mdui/components/list-item.js';

export type Colors = 'green' | 'yellow' | 'orange' | 'red';
export type Languages = 'zh' | 'en';
export type Method = 'plus' | 'average' | 'multiply';
export type Method = 'plus' | 'average' | 'multiply' | 'division';

/**
* 导航栏元素接口
Expand Down Expand Up @@ -121,6 +121,49 @@ export interface QuestionnairesList {

// 量表文件类型接口

/**
* 量表题目文件
*/
export interface QuestionnaireFile {
/**
* 量表名称
*/
name: string;
/**
* 量表介绍
*/
description: string;
/**
* 作答提示
*/
answerTips: string;
/**
* 结果提示&解析,支持 `HTML` 标签,用 `innerHTML` 上屏
*/
resultTips: string;
/**
* 题目来源(引用)
* 比如从哪儿找到题目,从哪儿找的评分标准之类的
* @type {string[]}
*/
references: string[];
/**
* 题目选项
* @type {OptionItem[]}
*/
options: OptionItem[];
/**
* 题目
* @type {QuestionItem[]}
*/
questions: QuestionItem[];
/**
* 评分标准
* @type {Scoring[]}
*/
scoring: Scoring[];
}

/**
* 单个选项
*/
Expand Down Expand Up @@ -159,23 +202,30 @@ export interface QuestionItem {
}

/**
* 单个评分标准
* 评分标准组
*/
export interface Criterion {
export interface Scoring {
/**
* 评分组组id
* 需与题目的题目组id完全对应
* @see QuestionItem.groupId
* 组Id
*/
groupId: number;
groupId: number
/**
* 该范围对外现实的名称
*/
name: string
/**
* 计算方法
* * `plus` 全部求和
* * `average` 求和后取平均分(只返回平均分)
* * `multiply` 求和后将结果 × 2(只返回计算后的结果)
*/
method: Method | string;
/**
* 分值范围
*/
ranges: Range[]
}

/**
* 单个分值范围
*/
Expand Down Expand Up @@ -203,79 +253,6 @@ export interface Range {
max: number;
}

/**
* 分值范围(带分组)
*/
export interface Ranges {
/**
* 范围组id
* 需与题目的题目组id完全对应
* @see QuestionItem.groupId
*/
groupId: number;
/**
* 组名称
*/
name: string;
/**
* 单个评分范围
* @type {Range[]}
*/
ranges: Range[];
}
/**
* 评分标准组
*/
export interface Scoring {
/**
* 单个评分标准
*/
criteria: Criterion[];
ranges: Ranges[];
}

/**
* 量表题目文件
*/
export interface QuestionnaireFile {
/**
* 量表名称
*/
name: string;
/**
* 量表介绍
*/
description: string;
/**
* 作答提示
*/
answerTips: string;
/**
* 结果提示&解析,支持 `HTML` 标签,用 `innerHTML` 上屏
*/
resultTips: string;
/**
* 题目来源(引用)
* 比如从哪儿找到题目,从哪儿找的评分标准之类的
* @type {string[]}
*/
references: string[];
/**
* 题目选项
* @type {OptionItem[]}
*/
options: OptionItem[];
/**
* 题目
* @type {QuestionItem[]}
*/
questions: QuestionItem[];
/**
* 评分标准
* @type {Scoring}
*/
scoring: Scoring;
}

/**
* 计算完成后评分接口
Expand Down
2 changes: 1 addition & 1 deletion src/list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ document.addEventListener('listPageLoaded', async () => {
const loadingTip: HTMLDivElement = document.querySelector('#loadingTip')!;
const searchBar: TextField = document.querySelector('#searchBar')!;

getFile(`https://cdn.jsdelivr.net/gh/Super12138/AY-Questionnaires-DB@main/list.json?${new Date().getTime()}`)
getFile(`https://cdn.jsdelivr.net/gh/Super12138/AY-Questionnaires-DB@minify/list.json?${new Date().getTime()}`)
.then((response: string) => {
const json: QuestionnairesList = JSON.parse(response);
const catogories: Category[] = json.categories; // 获取分组
Expand Down
2 changes: 1 addition & 1 deletion src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ document.addEventListener('settingsPageLoaded', async () => {
const listVersion: HTMLParagraphElement = document.querySelector('#listVersion')!;
appVersion.innerHTML = `网站版本:${VERSION_NAME}-${VARIANT}-${COMMIT_HASH} (${VERSION_CODE})`;

getFile(`https://cdn.jsdelivr.net/gh/Super12138/AY-Questionnaires-DB@main/list.json?${new Date().getTime()}`)
getFile(`https://cdn.jsdelivr.net/gh/Super12138/AY-Questionnaires-DB@minify/list.json?${new Date().getTime()}`)
.then((response: string) => {
const json: QuestionnairesList = JSON.parse(response);

Expand Down
27 changes: 4 additions & 23 deletions src/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import '@mdui/icons/check--outlined.js';
import '@mdui/icons/tips-and-updates--outlined.js';

import { RadioGroup } from 'mdui/components/radio-group.js';
import { BasicScoreResult, ButtonType, Criterion, GroupedData, QuestionnaireFile, QuestionResult, Ranges, ScoreResult, Scoring } from '../interfaces';
import { BasicScoreResult, ButtonType, GroupedData, QuestionnaireFile, QuestionResult, ScoreResult, Scoring } from '../interfaces';
import { hide, show } from '../utils/element';
import { LogHelper } from '../utils/LogHelper';
import { getFile } from '../utils/network';
import { showKeyboardNotice } from '../utils/notices';
import { Question } from './question';
Expand All @@ -28,7 +27,6 @@ const appTitle: TopAppBarTitle = document.querySelector('#appTitle')!;
const url: URL = new URL(window.location.href);
const questionnaire: string | null = url.searchParams.get("name");
const backBtn: ButtonIcon = document.querySelector('#backBtn')!;
const logHelper = LogHelper.getInstance();

enum NextButtonType {
START,
Expand Down Expand Up @@ -82,7 +80,7 @@ document.addEventListener('testPageLoaded', async () => {
setUpNextButton(nextBtn, NextButtonType.START);

// 获取量表json
getFile(`https://cdn.jsdelivr.net/gh/Super12138/AY-Questionnaires-DB@main/questionnaires/${questionnaire}.json?${new Date().getTime()}`)
getFile(`https://cdn.jsdelivr.net/gh/Super12138/AY-Questionnaires-DB@minify/questionnaires/${questionnaire}.json?${new Date().getTime()}`)
.then((response: string) => {
// 加载完了显示答题页面,隐藏加载提示
loadingTip.style.opacity = "0";
Expand Down Expand Up @@ -160,7 +158,6 @@ document.addEventListener('testPageLoaded', async () => {
switch (currentNextBtnType) {
case NextButtonType.NEXT: // “下一题”按钮
currentQuestion += 1;
logHelper.log(currentQuestion);
show(questions[currentQuestion].html);
hide(questions[currentQuestion - 1].html);
if (currentQuestion === questionnaireTotal - 1) {
Expand Down Expand Up @@ -212,23 +209,8 @@ document.addEventListener('testPageLoaded', async () => {
acc[name].push(current.value); // 将用户分数存入数组
return acc;
}, {});

// 获取评分组
const scoring: Scoring = json.scoring;
// 获取评分标准
const criteria: Criterion[] = scoring.criteria;
// 获取分值范围
const ranges: Ranges[] = scoring.ranges;

// 获取每个评分标准,并按照groupId对其分组
const groupedCriteria: GroupedData = criteria.reduce((acc: GroupedData, current: Criterion) => {
const name: number = current.groupId;
if (!acc[name]) {
acc[name] = [];
}
acc[name].push(current.method);
return acc;
}, {});
const scoring: Scoring[] = json.scoring;

const resultTbody: HTMLTableSectionElement = document.querySelector('#resultTbody')!;

Expand All @@ -250,7 +232,7 @@ document.addEventListener('testPageLoaded', async () => {
}

// 将分组后的题目和分组后的评分标准匹配起来,并计算得分
const score: ScoreResult[] = getScore(groupedQuestions, groupedCriteria, ranges);
const score: ScoreResult[] = getScore(groupedQuestions, scoring);
// 将得分上屏
score.forEach((item: ScoreResult) => {
const itemContainer: HTMLTableRowElement = document.createElement('tr');
Expand Down Expand Up @@ -296,7 +278,6 @@ document.addEventListener('testPageLoaded', async () => {
currentQuestion -= 1;
show(questions[currentQuestion].html);
hide(questions[currentQuestion + 1].html);
logHelper.log(currentQuestion);
setUpNextButton(nextBtn, NextButtonType.NEXT);
previousBtn.disabled = currentQuestion === 0;
updateProgress();
Expand Down
4 changes: 2 additions & 2 deletions src/test/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class Question {
const radio: Option = new Option(option.name, currentScoreList[index]);
radioGroup.appendChild(radio.html);
});
// 测试用 生成随机数(0-3)
// const randomNumber = Math.floor(Math.random() * 5);
// 测试用 生成随机数
// const randomNumber = Math.floor(Math.random() * 4);
// radioGroup.value = randomNumber.toString();

div.appendChild(questionContent);
Expand Down
Loading

0 comments on commit 0f511f3

Please sign in to comment.