派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
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
package com.qxueyou.scc.exercise.service.impl.node;
 
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import org.apache.commons.lang3.StringUtils;
 
import com.qxueyou.scc.exercise.model.ExerciseItem;
import com.qxueyou.scc.exercise.model.ExerciseItemAnalisi;
import com.qxueyou.scc.exercise.model.ExerciseItemOption;
import com.qxueyou.scc.exercise.service.impl.Node;
import com.qxueyou.scc.exercise.service.impl.parser.ItemParser;
 
public class Item extends Node {
    
    public Item(){
        setName("ITEM");
        setParser(new ItemParser());
    }
 
    @Override
    public boolean isComplete() {
        
        ItemType type = (ItemType) getParent();
        
        if(type.getType()!=ItemType.TYPE_TRUE_OR_FALSE){
            if(getChildren().isEmpty()){
                return false;
            }
        }
        
        return getTitle().length()>4;
    }
    
    @SuppressWarnings("unchecked")
    public List<Option> getOptions(){
        List<Node> children = getChildren();
        
        if(children.isEmpty()){
            return Collections.EMPTY_LIST;
        }
        
        List<Option> options = new ArrayList<Option>(children.size());
        
        for(Node node:children){
            options.add((Option) node);
        }
        
        return options;
    }
    
    public Option getOption(String no){
        
        Node node = getChild("NO", no);
        
        return node==null?null:(Option)node;
    }
    
    public void setNo(String no){
        setAttribute("NO",no);
    }
    
    public void setTitle(String title){
        setAttribute("TITLE",title);
    }
    
    public void setAnalysis(String analysis){
        setAttribute("ANALYSIS",analysis);
    }
    
    public String getNo(){
        return (String) getAttribute("NO");
    }
    
    public String getAnswer(){
        return (String) getAttribute("ANSWER");
    }
    
    public void setAnswer(String answer){
        setAttribute("ANSWER",answer);
    }
    
    public String getTitle(){
        return (String) getAttribute("TITLE");
    }
    
    public String getAnalysis(){
        return (String) getAttribute("ANALYSIS");
    }
 
    /**
     * 转换为数据模型
     * @return
     */
    public ExerciseItem convertExerciseItem() {
        
        //new ExerciseItem
        ExerciseItem item = new ExerciseItem();
        
        //转换 ExerciseItemOption
        List<ExerciseItemOption> options = new ArrayList<ExerciseItemOption>();
        
        //确保判断题的OPTION
        ensureTrueOrFalseOpt();
        
        //更新参考答案 最终的答案 格式如A,B,C
        String resultAnswer = updateAnswer();
        
        for(Option opt:getOptions()){
            if(!opt.isComplete()){
                continue;
            }
            options.add(opt.convertExerciseItemOption());
        }
        
        // 设置正确答案
        item.setAnswer(resultAnswer);
        item.setTitle(this.getTitle());
        item.setType(getExerciseType());
        item.setOptions(options);
        item.setItemNo(Integer.valueOf(getNo()));
        
        //转换解析内容
        addAnalysis(item);
        
        //返回
        return item;
    }
    
    /**
     * 增加解析实体
     * @param item
     */
    private void addAnalysis(ExerciseItem item) {
        
        if(StringUtils.isEmpty(getAnalysis())){
            return;
        }
        
        ExerciseItemAnalisi analysis = new ExerciseItemAnalisi();
        analysis.setAnalysis(getAnalysis());
        analysis.setAccuracy(BigDecimal.ZERO);
        analysis.setSubmitCorrectNumber(BigInteger.ZERO);
        analysis.setSubmitNumber(BigInteger.ZERO);
        
        item.setAnalisis(analysis);
    }
 
    /**
     * 更新参考答案
     */
    private String updateAnswer() {
        
        String answer = getAnswer();
        String resultAnswer = "";
        if(StringUtils.isEmpty(answer)){
            return resultAnswer;
        }
        
        //判断题
        if(answer.equals("True")||answer.equals("False")){
            Node node = getChild("NO", answer);
            if(node==null){
                return resultAnswer;
            }
            Option opt = (Option) node;
            opt.setCheck(true);
            resultAnswer = answer;
        }else{//单选或多选题
            for(int i=0;i<answer.length();i++){
                String optAnswer = String.valueOf(answer.charAt(i));
                Node node = getChild("NO", optAnswer);
                if(node==null){
                    return resultAnswer;
                }
                Option opt = (Option) node;
                opt.setCheck(true);
                
                // 拼接答案
                resultAnswer += answer.charAt(i);
                if(i < answer.length()-1){//最后一次不添加
                    resultAnswer += ",";
                }
                
            }
        }
        
        return resultAnswer;
        
    }
 
    /**
     * 特殊处理判断题的 option,自动添加 True 和 False 选项
     */
    private void ensureTrueOrFalseOpt(){
        ItemType type = (ItemType) getParent();
        
        if(type.getType()==ItemType.TYPE_TRUE_OR_FALSE){
            
            getChildren().clear();
            
            Option optTrue = new Option();
            optTrue.setContent("True");
            optTrue.setNo("True");
            
            Option optFalse = new Option();
            optFalse.setContent("False");
            optFalse.setNo("False");
            
            addNode(optTrue);
            addNode(optFalse);
        }
    }
    
    /**
     * 获取习题类型
     * @return
     */
    public short getExerciseType(){
        ItemType type = (ItemType) (this.getParent());
        
        String typeStr = type.getType();
        
        if(ItemType.TYPE_SINGLE.equals(typeStr)){
            return ExerciseItem.TYPE_SINGLE_SELECT;
        }
        
        if(ItemType.TYPE_MULTI.equals(typeStr)){
            return ExerciseItem.TYPE_MULTI_SELECT;
        }
        
        if(ItemType.TYPE_TRUE_OR_FALSE.equals(typeStr)){
            return ExerciseItem.TYPE_TRUE_OR_FALSE;
        }
        
        return ExerciseItem.TYPE_SINGLE_SELECT;
        
    }
 
}