派生自 projectDept/qhighschool

胡仁荣
2022-10-31 fe7381d6e8ec1f427408de0297ac7f41533202f6
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;
        
    }
 
}