-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
208 lines (178 loc) · 6.83 KB
/
script.js
File metadata and controls
208 lines (178 loc) · 6.83 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
function calculateLevels() {
const studentName = document.getElementById('studentName').value // 生徒名
const startDate = new Date(document.getElementById('startDate').value) // 入塾日
const startGrade = document.getElementById('startGrade').value // 初期学年
const startLevel = document.getElementById('startLevel').value // 開始レベル
const isHalfLesson =
document.querySelector('input[name="isHalfLesson"]:checked').value === 'yes'
const lessonFrequency = parseInt(
document.getElementById('lessonFrequency').value
)
const levels = [
'Pre1',
'Pre2',
'Pre3',
'Pre4',
'BP1',
'BP2',
'BP3',
'BP4',
'BP5',
'BP6',
'BP7',
'BP8',
'BP9',
'BP10',
'BP11',
'BP12'
]
const lessonCounts = [
0, 9, 9, 18, 9, 21, 25, 22, 31, 26, 27, 36, 32, 56, 36, 24, 24
] // レベルごとの必要レッスン数
let targetDate = new Date(startDate) // 現在の日付を初期化
let targetGrade = startGrade
const startIndex = levels.indexOf(startLevel) // 開始レベルのインデックス
const table = document.getElementById('levelTable')
// **前回の結果をクリア**
for (let i = 1; i < table.rows.length; i++) {
table.rows[i].cells[1].textContent = '' // 学年のセルをクリア
table.rows[i].cells[2].textContent = '' // 日付のセルをクリア
}
// 学年を進めるための関数
function getNextGrade(grade) {
const gradeOrder = [
'小2',
'小3',
'小4',
'小5',
'小6',
'中1',
'中2',
'中3',
'高1',
'高2',
'高3',
'大1',
'大2',
'大3',
'大4'
]
const index = gradeOrder.indexOf(grade)
return index < gradeOrder.length - 1 ? gradeOrder[index + 1] : null // 大4を超えたら null を返す
}
// 学年を更新するための関数
function updateGrade(prevDate, targetDate, targetGrade) {
let resultGrade = targetGrade
// 年度切り替えが 4月1日
// 月は 0-based なので 3 が4月、日付は 1-based
// 例: new Date(year, 3, 1) が4/1
// 開始年から終了年までループ
// 余裕をもって +1年 までチェックしておく
for (
let y = prevDate.getFullYear();
y <= targetDate.getFullYear() + 1;
y++
) {
// 今年度のカットオフ日時
const cutoffDate = new Date(y, 3, 1) // 4月1日
// カットオフが開始日より後 かつ 終了日以前 or 同日ならカウント
if (cutoffDate > prevDate && cutoffDate <= targetDate) {
resultGrade = getNextGrade(resultGrade)
}
}
return resultGrade
}
// 生徒名が入力されていれば、レベルアップイメージを表示
if (studentName) {
document.getElementById('levelUpMessage').innerHTML =
studentName + 'さんのレベルアップイメージ'
} else {
document.getElementById('levelUpMessage').innerHTML =
'名前を入力してください'
}
// テーブルを更新するループ
for (let i = 0; i < levels.length; i++) {
let row = table.rows[table.rows.length - i - 1] // テーブルの行を逆順に取得
if (i < startIndex) {
// 開始レベルより前の行は空欄のままにする
continue
}
let lessonsNeeded = lessonCounts[i]
if (isHalfLesson && levels[i].startsWith('BP')) {
lessonsNeeded *= 2 // ハーフレッスンの場合は倍のレッスン数が必要
}
console.log('i', i)
console.log('lessonsNeeded', lessonsNeeded)
// 初回のみ、開始日をそのまま使用
if (i === startIndex) {
row.cells[1].textContent = targetGrade // 学年
row.cells[2].textContent = targetDate.toLocaleDateString('ja-JP') // 開始日
row.cells[3].textContent = 'ー'
continue
}
// 前回の日付を保存
prevDate = new Date(targetDate)
// 必要な週数を計算し、現在の日付を更新
let weeksNeeded = Math.ceil(lessonsNeeded / lessonFrequency)
targetDate.setDate(targetDate.getDate() + weeksNeeded * 7)
// 4月1日以降に進級する場合、学年を進める
targetGrade = updateGrade(prevDate, targetDate, targetGrade)
// 大4を超えたら表示を停止
if (!targetGrade) {
break // ループを終了
}
// テーブルに情報を表示
row.cells[1].textContent = targetGrade // 学年
row.cells[2].textContent = targetDate.toLocaleDateString('ja-JP') // 開始日
row.cells[3].textContent = weeksNeeded + '週後'
// 大4に到達したらループを終了
if (targetGrade === '大4') {
break
}
}
}
// PDF書き出し機能の追加
document.getElementById('downloadPDF').addEventListener('click', function () {
const studentName = document.getElementById('studentName').value
const levelUpMessage = studentName
? studentName + 'さんのレベルアップイメージ'
: '名前を入力してください'
// テーブルをコピーしてPDF用のコンテナを作成
const table = document.getElementById('levelTable')
const tableClone = table.cloneNode(true) // テーブルをコピー
const container = document.createElement('div')
// レベルアップイメージのテキストを追加
const levelUpText = document.createElement('h2')
levelUpText.innerText = levelUpMessage
levelUpText.style.fontSize = '16px'
levelUpText.style.marginBottom = '10px'
// コピーしたテーブルのサイズを調整
tableClone.style.width = '100%' // テーブルを全幅に設定
tableClone.style.fontSize = '10px' // フォントサイズを小さく調整
tableClone.style.borderCollapse = 'collapse' // ボーダーの隙間をなくす
// コンテナにテキストとコピーしたテーブルを追加
container.appendChild(levelUpText)
container.appendChild(tableClone)
// HTML2PDF.jsのオプション
const opt = {
margin: 0.5, // ページ余白を小さめに設定
filename: studentName + 'さんのレベルアップイメージ.pdf',
html2canvas: { scale: 2 }, // 解像度を高く設定
pagebreak: { avoid: 'table' }, // テーブルの分割を防ぐ
jsPDF: { unit: 'in', format: 'a4', orientation: 'landscape' } // 横向き
}
// PDF生成
html2pdf().from(container).set(opt).save()
})
// ページ読み込み時に今日の日付を入力欄に設定する
document.addEventListener('DOMContentLoaded', function () {
// 今日の日付を取得
const today = new Date()
// YYYY-MM-DD形式にフォーマット(input type="date"の形式)
const year = today.getFullYear()
const month = String(today.getMonth() + 1).padStart(2, '0')
const day = String(today.getDate()).padStart(2, '0')
const formattedDate = `${year}-${month}-${day}`
// 日付入力欄にデフォルト値として設定
document.getElementById('startDate').value = formattedDate
})