Commit b47df134 authored by limingzhe's avatar limingzhe

feat: 增加成绩上报统计

parent 88828761
import { middleLayerBase } from "./middleLayerBase"; import { middleLayerBase } from "./middleLayerBase";
import { initAir } from './air'; import { initAir } from './air';
import StudyRecord from './studyRecord';
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
...@@ -78,7 +79,8 @@ export default class NewClass extends middleLayerBase { ...@@ -78,7 +79,8 @@ export default class NewClass extends middleLayerBase {
const study_record = JSON.parse(bundleInfo.study_record); const study_record = JSON.parse(bundleInfo.study_record);
console.log('study_record: ', study_record); console.log('study_record: ', study_record);
this.initStudyRecord(study_record);
} }
...@@ -557,4 +559,79 @@ export default class NewClass extends middleLayerBase { ...@@ -557,4 +559,79 @@ export default class NewClass extends middleLayerBase {
} }
onHomeworkFinishStudyRecord(d1 = null, d2 = null) {
console.log('in onHomeworkFinishStudyRecord');
let data = d1;
let callback = null;
if (typeof(d1) == 'function') {
data = d2;
callback = d1;
}
if (this.curHomeworkId == null || this.curSyllabusId == null) {
callback && callback();
return;
}
this.callNetworkApiPostNew(`api/app_source/v1/student/homework/finished`, {
syllabus_id: this.curSyllabusId,
homework_id: this.curHomeworkId,
result: JSON.stringify(data || {}),
}, res => {
this.curSyllabusId = null;
this.curHomeworkId = null;
callback && callback(res);
});
}
studyRecord: StudyRecord;
studyRecordModel;
initStudyRecord(study_record_str) {
this.studyRecordModel = JSON.parse(study_record_str);
this.studyRecord = new StudyRecord();
}
setSRRecord(key, value) {
if (this.studyRecord) {
this.studyRecord.recordAudioScore(key, value);
}
}
addSRAudioDuration(duration) {
if (this.studyRecord) {
this.studyRecord.addAudioDuration(duration);
}
}
showSRResultByRecord() {
if (this.studyRecord) {
this.studyRecordModel.recordData = this.studyRecord.getAllAudioKeyData();
const score = this.studyRecord.getTotalAverageAudioScore();
this.showSRResult(score)
}
}
showSRResult(score) {
if (this.studyRecord) {
console.log('showSRResult score: ', score);
// this.studyRecord.showResult();
}
}
reportData() {
if (this.studyRecord) {
this.studyRecord.reportData(this.studyRecordModel);
}
}
} }
class StudyRecord {
constructor() {
this.study_record = null;
// 录音统计相关
this.audioScores = {}; // 按key存储录音分数 {key: [score1, score2, ...]}
// 视频时长统计
this.totalVideoDuration = 0; // 总视频时长(秒)
// 音频时长统计
this.totalAudioDuration = 0; // 总音频时长(秒)
// 做题对错统计
this.totalQuestions = 0; // 总题数
this.questionResults = {}; // 题目结果 {index: score} 默认-1表示未答
}
// ==================== 录音统计方法 ====================
/**
* 记录录音分数
* @param {string} key - 录音标识
* @param {number} score - 录音分数
*/
recordAudioScore(key, score) {
if (!this.audioScores[key]) {
this.audioScores[key] = [];
}
this.audioScores[key].push(score);
}
/**
* 获取指定key的平均分数
* @param {string} key - 录音标识
* @returns {number} 平均分数,如果没有记录返回0
*/
getAverageAudioScore(key) {
if (!this.audioScores[key] || this.audioScores[key].length === 0) {
return 0;
}
const sum = this.audioScores[key].reduce((acc, score) => acc + score, 0);
return sum / this.audioScores[key].length;
}
/**
* 获取指定key的录音次数
* @param {string} key - 录音标识
* @returns {number} 录音次数
*/
getAudioRecordCount(key) {
return this.audioScores[key] ? this.audioScores[key].length : 0;
}
/**
* 获取总录音次数
* @returns {number} 总录音次数
*/
getTotalAudioRecordCount() {
let total = 0;
for (const key in this.audioScores) {
total += this.audioScores[key].length;
}
return total;
}
/**
* 获取所有录音key
* @returns {string[]} 所有录音key数组
*/
getAllAudioKeys() {
return Object.keys(this.audioScores);
}
/**
* 计算多个key的总平均分
* @param {string[]} keys - 要计算平均分的key数组,如果不传则计算所有key
* @returns {number} 总平均分,如果没有记录返回0
*/
getTotalAverageAudioScore(keys = null) {
const targetKeys = keys || this.getAllAudioKeys();
if (targetKeys.length === 0) {
return 0;
}
let totalScore = 0;
let totalCount = 0;
for (const key of targetKeys) {
if (this.audioScores[key] && this.audioScores[key].length > 0) {
const keySum = this.audioScores[key].reduce((acc, score) => acc + score, 0);
totalScore += keySum;
totalCount += this.audioScores[key].length;
}
}
return totalCount > 0 ? totalScore / totalCount : 0;
}
/**
* 获取所有key及其对应数据
* @returns {Object} 包含所有key和对应数据的对象 {key: {score: averageScore, count: count}}
*/
getAllAudioKeyData() {
const result = {};
for (const key in this.audioScores) {
if (this.audioScores[key] && this.audioScores[key].length > 0) {
result[key] = {
'score': this.getAverageAudioScore(key),
'count': this.getAudioRecordCount(key)
};
}
}
return result;
}
// ==================== 视频时长统计方法 ====================
/**
* 设置总视频时长
* @param {number} duration - 视频时长(秒)
*/
setTotalVideoDuration(duration) {
this.totalVideoDuration = duration;
}
/**
* 获取总视频时长
* @returns {number} 总视频时长(秒)
*/
getTotalVideoDuration() {
return this.totalVideoDuration;
}
// ==================== 音频时长统计方法 ====================
/**
* 设置总音频时长
* @param {number} duration - 音频时长(秒)
*/
setTotalAudioDuration(duration) {
this.totalAudioDuration = duration;
}
/**
* 增加音频时长
* @param {number} duration - 要增加的音频时长(秒)
*/
addAudioDuration(duration) {
this.totalAudioDuration += duration;
}
/**
* 获取总音频时长
* @returns {number} 总音频时长(秒)
*/
getTotalAudioDuration() {
return this.totalAudioDuration;
}
// ==================== 做题对错统计方法 ====================
/**
* 设置总题数
* @param {number} total - 总题数
*/
setTotalQuestions(total) {
this.totalQuestions = total;
// 初始化所有题目的默认分数为-1
for (let i = 0; i < total; i++) {
if (!(i in this.questionResults)) {
this.questionResults[i] = -1;
}
}
}
/**
* 设置某题的对错
* @param {number} index - 题目索引
* @param {number} score - 题目分数(1为正确,0为错误,-1为未答)
*/
setQuestionResult(index, score) {
if (index >= 0 && index < this.totalQuestions) {
this.questionResults[index] = score;
}
}
/**
* 获取某题的对错
* @param {number} index - 题目索引
* @returns {number} 题目分数
*/
getQuestionResult(index) {
return this.questionResults[index] !== undefined ? this.questionResults[index] : -1;
}
/**
* 获取所有题的对错
* @returns {Object} 所有题目结果 {index: score}
*/
getAllQuestionResults() {
return { ...this.questionResults };
}
/**
* 获取已答题的题目数量
* @returns {number} 已答题数量
*/
getAnsweredQuestionCount() {
let count = 0;
for (const index in this.questionResults) {
if (this.questionResults[index] !== -1) {
count++;
}
}
return count;
}
/**
* 计算平均分
* @returns {number} 平均分,如果没有答题返回0
*/
getAverageScore() {
const answeredCount = this.getAnsweredQuestionCount();
if (answeredCount === 0) {
return 0;
}
let totalScore = 0;
for (const index in this.questionResults) {
if (this.questionResults[index] !== -1) {
totalScore += this.questionResults[index];
}
}
return totalScore / answeredCount;
}
/**
* 获取正确题目数量
* @returns {number} 正确题目数量
*/
getCorrectQuestionCount() {
let count = 0;
for (const index in this.questionResults) {
if (this.questionResults[index] === 1) {
count++;
}
}
return count;
}
/**
* 获取错误题目数量
* @returns {number} 错误题目数量
*/
getWrongQuestionCount() {
let count = 0;
for (const index in this.questionResults) {
if (this.questionResults[index] === 0) {
count++;
}
}
return count;
}
/**
* 获取未答题目数量
* @returns {number} 未答题目数量
*/
getUnansweredQuestionCount() {
let count = 0;
for (const index in this.questionResults) {
if (this.questionResults[index] === -1) {
count++;
}
}
return count;
}
// ==================== 数据导出和导入方法 ====================
/**
* 导出所有统计数据
* @returns {Object} 所有统计数据
*/
exportData() {
return {
audioScores: this.audioScores,
totalVideoDuration: this.totalVideoDuration,
totalAudioDuration: this.totalAudioDuration,
totalQuestions: this.totalQuestions,
questionResults: this.questionResults
};
}
/**
* 导入统计数据
* @param {Object} data - 统计数据
*/
importData(data) {
if (data.audioScores) this.audioScores = data.audioScores;
if (data.totalVideoDuration !== undefined) this.totalVideoDuration = data.totalVideoDuration;
if (data.totalAudioDuration !== undefined) this.totalAudioDuration = data.totalAudioDuration;
if (data.totalQuestions !== undefined) this.totalQuestions = data.totalQuestions;
if (data.questionResults) this.questionResults = data.questionResults;
}
/**
* 重置所有统计数据
*/
reset() {
this.audioScores = {};
this.totalVideoDuration = 0;
this.totalAudioDuration = 0;
this.totalQuestions = 0;
this.questionResults = {};
}
// ==================== 数据上报方法 ====================
reportData(studyRecordModel) {
this.callNetworkApiPostNew(`api/insights/v1/study-analysis`, studyRecordModel, res => {
console.log('reportData => res:', res);
// callback && callback(res);
});
// this.studyRecordModel.recordData = jsonEncode({'questions': _questionScores});
// console.log(this.studyRecordModel.toJson());
// const score = getAverageQuestionScore().toInt();
// StudyRecordScore.showScoreDismissible(context, score);
// DioResponse result = await DioUtil().request(Api.studyAnalysis,
// method: DioMethod.post,
// service: DioService.insights,
// cancelToken: null,
// data: this.studyRecordModel.toJson());
// if (result.statusCode == 200) {
// console.log('上传成功');
// // GetStorage().write('canvasReportData', jsonString);
// } else {
// console.log('上传失败');
// }
}
}
export default StudyRecord;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment