-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4f13da2173529d3e0dc3.js
184 lines (169 loc) · 5.69 KB
/
4f13da2173529d3e0dc3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const btnDownload = document.querySelector('.btn-report');
const fetchQuestionInfo = async () => {
const response = await fetch(
`${window.location.origin}/assets/data/story/story.json`,
);
const jsonData = await response.json();
return jsonData;
};
const getCode = (id) => {
const codes = JSON.parse(localStorage.getItem(`${id}_code`));
let codeText = '';
// code를 순회하면서 요소를 추가
codes.forEach((code) => {
const trimedCode = code.trim();
if (trimedCode) {
codeText += '```py\n' + trimedCode + '\n```\n';
}
});
return codeText ? codeText : '```py\n\n```';
};
// const getChart = (chartData) => {
// return new Promise((resolve) => {
// const canvas = document.createElement('canvas');
// canvas.setAttribute('id', 'chart');
// const ctx = canvas.getContext('2d');
// ctx.canvas.width = 800;
// const myChart = new Chart(ctx, {
// type: 'bar',
// data: {
// labels: Object.keys(chartData),
// datasets: [
// {
// label: '점수',
// data: Object.values(chartData),
// borderWidth: 1,
// },
// ],
// },
// options: {
// scales: {
// y: {
// beginAtZero: true,
// min: 0,
// max: 100,
// },
// },
// responsive: false,
// },
// });
// setTimeout(() => {
// const imgLink = canvas.toDataURL('image/png');
// canvas.remove();
// resolve(imgLink);
// }, 500);
// document.body.appendChild(canvas);
// });
// };
const getTable = (evalData, chartData) => {
let result = '|항목|진행도|성취도|\n|:---:|:---|:---:|\n';
for (const key of Object.keys(chartData)) {
const fulfilled = Math.floor((chartData[key] / evalData[key]) * 10);
const unfulfilled = Math.floor(
((evalData[key] - chartData[key]) / evalData[key]) * 10,
);
result += `|${key} |${
'◼︎'.repeat(fulfilled) + '◻︎'.repeat(unfulfilled)
}|${fulfilled * 10}%|\n`;
}
return result;
};
const downloadFile = async ({ data, fileName, fileType }) => {
const blob = new Blob([data], { type: fileType });
const link = document.createElement('a');
link.download = fileName;
link.href = await URL.createObjectURL(blob);
const clickEvt = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
});
link.dispatchEvent(clickEvt);
link.remove();
};
btnDownload.addEventListener('click', (e) => {
const score = {
입문: {
'변수와 자료형': 0,
연산: 0,
'반복문과 조건문': 0,
함수: 0,
},
기초: {
'변수와 자료형': 0,
연산: 0,
'반복문과 조건문': 0,
함수: 0,
클래스: 0,
},
};
const evaluate_score = {
입문: {
'변수와 자료형': 5,
연산: 8,
'반복문과 조건문': 3,
함수: 3,
},
기초: {
'변수와 자료형': 4,
연산: 4,
'반복문과 조건문': 1,
함수: 5,
클래스: 3,
},
};
const questionData = fetchQuestionInfo();
questionData.then((data) => {
let reportData = '';
Object.keys(storyChapter).forEach((chap) => {
let chapterData = '';
const storyList = storyChapter[chap];
// storyList를 순회
storyList.forEach((id) => {
if (localStorage.getItem(`${id}_code`)) {
const result =
localStorage.getItem(`${id}_check`) === '정답'
? 'Y'
: 'N';
if (result == 'Y') {
const evaluation = data.find(
(el) => el.id === id,
).evaluation;
for (const evl of evaluation) {
score[chap][evl] += 1;
}
}
const storyData = `## 문제 ${id}번\n\n* 제출 시간 : ${
localStorage.getItem(`${id}_time`) || '-'
}\n* 통과 여부 : ${result}\n\n${getCode(id)}\n\n`;
chapterData += storyData;
}
});
// 표로 가져오기
reportData +=
`# ${chap} 학습 성취도\n\n ${getTable(
evaluate_score[chap],
score[chap],
)}\n\n` +
chapterData +
'---\n\n';
});
// TODO: 학번과 이름을 입력받아 파일명을 만들어준다.
const userName =
JSON.parse(localStorage.getItem('profile'))?.name || '[이름]';
if (!!reportData) {
const fileName = `보고서`;
const today = new Date();
downloadFile({
data: reportData,
fileName: `${today
.toISOString()
.slice(2, 10)
.replace(/-/g, '')}_${fileName}_${userName}.md`,
fileType: 'text/json',
});
} else {
window.alert('다운로드 할 데이터가 없습니다.');
}
});
});