tom
2023-12-06 9e968679ed2e6937aeb7b50a6c450d5d19251f42
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
var swipe, aBook=[], aQuestion=[], iBook, iQuestion, iAnswer;
    
$(document).ready( onDocumentReady );
 
function onDocumentReady() {
    pageInit(showBook);
}
 
function showBook() {
    menuSwitch("book");
    loadBook();
}
 
function loadBook() {
    jsonRequest("quiz", "load_book", {}, onBookResponse, "正在获取培训题库…请稍候");
}
 
function onBookResponse(res) {
    aBook=res.book_list;
    
    var html="";
    for (var i=0; i<aBook.length; i++) {
        var book=aBook[i];
        html+=div("div_book_item", 
            div("div_book_code", book.code)+
            div("div_book_title", book.title)+
            div("div_book_stats_group", 
                div("div_book_stats_item", "总共: "+book.question_num+"题")+
                div("div_book_stats_item", "已回答: "+book.finish_num+"题")+
                div("div_book_stats_item", "得分: "+book.score+"分")+
                div("div_book_stats_item", "正确: "+book.correct_num+"题") ),
                    "iBook="+i+"; showQuestion()");
    }
    $("#div_book_group").html(html);
}
 
function showQuestion() {
    pageGo("question");
    loadQuestion();
}
 
function hideQuestion() {
    pageBack("menu", loadBook);
}
 
function loadQuestion() {
    var req={};
    req.book_uid=aBook[iBook].uid;
    jsonRequest("quiz", "load_question", req, onQuestionResponse, "正在获取题目……请稍候");
}
 
function onQuestionResponse(res) {
    aQuestion=res.question_list;
    $("#div_question_header_title").html(aBook[iBook].code+" - "+aBook[iBook].title);
    $("#div_answer_header_title").html(aBook[iBook].code+" - "+aBook[iBook].title);
    var html="";
    for (var i=0; i<aQuestion.length; i++) {
        var question=aQuestion[i];
        var result=question.result? div("div_question_"+(question.result.correct?"correct":"wrong"), "") : "";
        html+=div("div_question_item",
            div("div_question_code", (i+1)+".")+
            div("div_question_title", aQuestion[i].title)+
            result,
                "iQuestion="+i+"; showAnswer()");
    }
    if (getAccount().level=="RM") {
        html+=div("div_question_item", "添加新的题目", "showAddQuestion()");
    }
    $("#div_question_group").html(html);
    
    iQuestion=0;
    var html=div("div_answer_start","");
    for (i=0; i<aQuestion.length; i++) {
        html+=div("div_answer_frame", "", "", "div_answer_frame_"+i);
    }
    html+=div("div_answer_frame","", "", "div_answer_end");
    var wrap=div("swipe-wrap", html);
    $("#div_answer_swipe").html(wrap);
    for (i=0; i<aQuestion.length; i++) {
        loadAnswer(i);
    }
    $("#page_answer").show();
    var option = {
      continuous: false,
      disableScroll: false,
      stopPropagation: false,
      transitionEnd: refreshAnswer,
      startSlide:0
    }
    swipe = new Swipe(document.getElementById("div_answer_swipe"), option);
    $("#page_answer").hide();
}
 
function loadAnswer(i) {
    var question=aQuestion[i];
    console.log(question);
    var html="";
    html+=div("div_answer_question", question.title);
    for (var j=0; j<question.answer_list.length; j++) {
        var answer=question.answer_list[j];
        var r=answer.rank;
        var status=(question.result && question.result.rank==r)?"on":"off";
        html+=div("div_answer_"+status,
            div("div_index_"+status, String.fromCharCode(64+answer.rank), "", "index_"+i+"_"+r)+answer.title,
                "selectAnswer("+r+")", "answer_"+i+"_"+r);
    }
    if (getAccount().level=="RM") {
        html+=div("div_answer_off", div("div_index_off", "+")+"添加答案选项", "showAddAnswer()");
    }
    if (question.photo) {
        html+=img("img_question_photo", "quiz/"+question.photo);
    }
    $("#div_answer_frame_"+i).html(html);
}
 
function startAnswer() {
    iQuestion=0;
    showAnswer();
}
 
function showAnswer() {
    swipe.slide(0,0);
    $("#page_answer").show();
    setTimeout(slideAnswer, 100);
}
 
function slideAnswer() {
    swipe.slide(iQuestion+1);
}
 
function refreshAnswer() {
    var i=swipe.getPos(), n=aQuestion.length;
    if (i==0) {
        $("#page_answer").hide();
    } else if (i==n+1) {
        $("#div_question_index").html("");
        checkResult();
    } else {
        iQuestion=i-1;
        var question=aQuestion[iQuestion];
        var aAnswer=question.answer_list;
        $("#div_question_index").html("第"+question.rank+"题");
        var bPrev=(iQuestion>0);
        $("#div_prev_question").toggle(bPrev);
        var bNext=(iQuestion<n-1);
        $("#div_next_question").toggle(bNext);
        var bSubmit=(iQuestion==n-1);
        $("#div_submit_result").toggle(bSubmit);
        var bHide=(iQuestion==0);
        $("#div_hide_answer").toggle(bHide);
        var percent=Math.floor(((iQuestion+1)*100)/n);
        $("#div_answer_progress").css("width", percent+"%");    
    }
}
 
function hideAnswer() {
    swipe.slide(0);
}
 
function selectAnswer(r) {
    if (aQuestion[iQuestion].result) {
        var r0=aQuestion[iQuestion].result.rank;
        $("#index_"+iQuestion+"_"+r0).attr("class", "div_index_off");
        $("#answer_"+iQuestion+"_"+r0).attr("class", "div_answer_off");
    }
    aQuestion[iQuestion].result={rank:r};
    $("#index_"+iQuestion+"_"+r).attr("class", "div_index_on");
    $("#answer_"+iQuestion+"_"+r).attr("class", "div_answer_on");
}
 
function nextQuestion() {
    swipe.next();
}
 
function prevQuestion() {
    swipe.prev();
}
 
function getBlank() {
    var blank=0;
    for (var i=0; i<aQuestion.length; i++) {
        blank += aQuestion[i].result?0:1;
    }
    return blank;
}    
 
function checkResult() {
    var blank=getBlank();
    if (blank>0) {
        showConfirm("还有"+blank+"道题未回答,是否提交?", "submitResult()");
    } else {
        showConfirm("是否确认提交?", "submitResult()");
    }
}        
    
function submitResult() {
    var list=[];
    for (var i=0; i<aQuestion.length; i++) {
        if (aQuestion[i].result) {
            list.push({uid:aQuestion[i].uid, rank:aQuestion[i].result.rank});
        }
    }
 
    var req={};
    req.book_uid=aBook[iBook].uid;
    req.result_list=list;    
    jsonRequest("quiz", "add_result", req, onSubmitResultResponse, "正在提交答案……请稍候");
}
 
function onSubmitResultResponse() {
    hideAnswer();
    loadQuestion();
}
 
/************ ADMIN ****************/
 
function showAddQuestion() {
    $("#input_add_question_title").val("");
    $("#div_add_question").show();
}
 
function hideAddQuestion() {
    $("#div_add_question").hide();
}
 
function submitAddQuestion() {
    var req={};
    req.book_uid=aBook[iBook].uid;
    req.title=$("#input_add_question_title").val();
    hideAddQuestion();
    jsonRequest("quiz", "add_question", req, loadQuestion);
}
 
function showAddAnswer() {
    $("#input_add_answer_title").val("");
    $("#div_add_answer").show();
}
 
function hideAddAnswer() {
    $("#div_add_answer").hide();
}
 
function submitAddAnswer(correct) {
    var req={};
    req.question_uid=aQuestion[iQuestion].uid;
    req.title=$("#input_add_answer_title").val();
    req.correct=correct;
    hideAddAnswer();
    jsonRequest("quiz", "add_answer", req, onAddAnswerResponse);
}
 
function onAddAnswerResponse(res) {
    aQuestion[iQuestion].answer_list.push(res);
    showAnswer();
}