AnswerViewController.m 14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
//  AnswerViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2016/12/7.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "AnswerViewController.h"
#import "AssessmentShortAnswerTableViewCell.h"
#import "AssessmentHeaderView.h"
#import "AssessmentTableViewCell.h"
#import "AssessmentShortAnswerTableViewCell.h"
#import "EmigratedFinishViewController.h"


曹云霄's avatar
曹云霄 committed
17
@interface AnswerViewController ()<UITableViewDelegate,UITableViewDataSource,WYPopoverControllerDelegate,CompeteDelegate>
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38


@property (nonatomic,strong) WYPopoverController *popover;

/**
 闯关详情
 */
@property (nonatomic,strong) TOPassLevelEntity *emigrated;

/**
 当前加载section
 */
@property (nonatomic,assign) NSInteger sectionPage;

@end

@implementation AnswerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
39
    [self uiConfigAction];
40 41 42
    [self getPassLevelDeltails];
}

43 44 45 46 47 48
#pragma mark - UI
- (void)uiConfigAction
{
    self.answerTableView.backgroundColor = [UIColor clearColor];
}

49 50 51 52 53
#pragma mark - 查询题型
- (void)getPassLevelDeltails
{
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
54
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(GETPASSLEVELDETAILS),self.passLevelId] withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
55
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
56
        if (RESULT(returnValue)) {
57 58 59 60 61 62
            weakSelf.emigrated = [[TOPassLevelEntity alloc] initWithDictionary:returnValue[@"data"] error:nil];
            [weakSelf.answerTableView reloadData];
        }else {
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
        }
        
63
    } withFailureBlock:^(NSError *error) {
64
        [XBLoadingView hideHUDViewWithDefault];
65 66 67 68 69 70 71 72 73 74 75
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTOPassLevelTopicEntity *entity = self.emigrated.topics[self.sectionPage];
    if ([entity.topicType isEqualToString:PASSLEVELTOPICTYPE_GAPFILLING] || [entity.topicType isEqualToString:PASSLEVELTOPICTYPE_SHORTANSWER]) {
        AssessmentShortAnswerTableViewCell *shortAnswerCell = [tableView dequeueReusableCellWithIdentifier:@"AssessmentShortAnswerTableViewCell" forIndexPath:indexPath];
        shortAnswerCell.entity = entity;
76
        shortAnswerCell.backgroundColor = [UIColor clearColor];
77 78 79
        return shortAnswerCell;
    }
    AssessmentTableViewCell *assessmentCell = [tableView dequeueReusableCellWithIdentifier:@"AssessmentTableViewCell" forIndexPath:indexPath];
80
    assessmentCell.backgroundColor = [UIColor clearColor];
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
    assessmentCell.emigratedEntity = entity.options[indexPath.row];
    return assessmentCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CustomTOPassLevelTopicEntity *entity = self.emigrated.topics[self.sectionPage];
    return entity.titleHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTOPassLevelTopicEntity *entity = self.emigrated.topics[self.sectionPage];
    if (entity.options.count) {
        CustomTOPassLevelTopicOptionEntity *answer = entity.options[indexPath.row];
        return answer.answerHeight;
    }
    return 200;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    if ([cell isKindOfClass:[AssessmentShortAnswerTableViewCell class]]) {
        AssessmentShortAnswerTableViewCell *shortAnswerCell = (AssessmentShortAnswerTableViewCell *)cell;
        shortAnswerCell.shortAnswerTextView.text = nil;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return ONE;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CustomTOPassLevelTopicEntity *entity = self.emigrated.topics[self.sectionPage];
    if ([entity.topicType isEqualToString:PASSLEVELTOPICTYPE_GAPFILLING] || [entity.topicType isEqualToString:PASSLEVELTOPICTYPE_SHORTANSWER]) {
        return ONE;
    }
    return entity.options.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    AssessmentHeaderView *headerView = [tableView dequeueReusableCellWithIdentifier:@"AssessmentHeaderView"];
    headerView.emigratedEntity = self.emigrated.topics[self.sectionPage];
    return headerView.contentView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTOPassLevelTopicEntity *entitys = self.emigrated.topics[self.sectionPage];
    if ([entitys.topicType isEqualToString:STUDYTOPICTYPE_SINGLE] || [entitys.topicType isEqualToString:STUDYTOPICTYPE_TRUEORFALSE]) {
        for (int i=0; i<entitys.options.count; i++) {
            CustomTOPassLevelTopicOptionEntity *entity = entitys.options[i];
            if (i == indexPath.row) {
                entity.isSelected = !entity.isSelected;
            }else {
                entity.isSelected = NO;
            }
141
       }
142 143 144 145 146 147 148 149 150 151 152 153 154
    }else if ([entitys.topicType isEqualToString:STUDYTOPICTYPE_MULTIPLE]) {
        CustomTOPassLevelTopicOptionEntity *answer = entitys.options[indexPath.row];
        answer.isSelected = !answer.isSelected;
    }
    [self.answerTableView reloadData];
}

#pragma mark - 下一步<最后一道题自动提交>
- (IBAction)nextButtonClickAction:(UIButton *)sender {

    if (self.sectionPage == self.emigrated.topics.count-1) {
        [self submitAnswer];
    }else {
155 156 157
        if (![self judgeIsAnswer]) {
            [XBLoadingView showHUDViewWithText:@"答案不能为空"];return;
        }
158 159 160 161 162
        self.sectionPage += ONE;
        [self.answerTableView reloadData];
    }
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
#pragma mark - 判断是否已回答
- (BOOL)judgeIsAnswer
{
    CustomTOPassLevelTopicEntity *entitys = self.emigrated.topics[self.sectionPage];
    //简答题/填空题
    if ([entitys.topicType isEqualToString:PASSLEVELTOPICTYPE_SHORTANSWER] || [entitys.topicType isEqualToString:PASSLEVELTOPICTYPE_GAPFILLING]) {
        return ![[self class] isBlankString:entitys.answer];
    }
    //单选、多选、判断
    for (CustomTOPassLevelTopicOptionEntity *answer in entitys.options) {
        if (answer.isSelected) {
            return YES;
        }
    }
    return NO;
}

180 181 182
#pragma mark - 提交答案
- (void)submitAnswer
{
183 184
    if ([self.delegate respondsToSelector:@selector(dismissController:)]) {
        [self.delegate dismissController:NO];
曹云霄's avatar
曹云霄 committed
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
    TOPassLevelResultEntity *studyResult = [[TOPassLevelResultEntity alloc]init];
    studyResult.passLevelId = self.passLevelId;
    studyResult.submitTime = [[self class] getTimeby:0];
    studyResult.employeeId = [Shoppersmanager manager].Shoppers.employee.fid;
    NSArray *answerArray = self.emigrated.topics;
    NSMutableArray *submitAnswerArray = [NSMutableArray array];
    //判断考核结果
    NSInteger allScore = 0;//总分
    for (CustomTOPassLevelTopicEntity *topicEntity in answerArray) {
        
        TOPassLevelResultDetailEntity *resultEntity = [[TOPassLevelResultDetailEntity alloc] init];
        resultEntity.topicId = topicEntity.fid;
        //多选
        if ([topicEntity.topicType isEqualToString:STUDYTOPICTYPE_MULTIPLE]) {
            NSMutableString *answers = [[NSMutableString alloc] init];
            for (CustomTOPassLevelTopicOptionEntity *entity in topicEntity.options) {
                if (entity.isSelected == YES) {
                    [answers appendFormat:@"%@,",entity.value];
                }
            }
            resultEntity.answer = answers;
            //得分
            NSArray *answersArray = [topicEntity.answer componentsSeparatedByString:@","];
            NSInteger answerNumber = 0;
            for (NSString *answer in answersArray) {
                for (CustomTOStudyTopicOptionEntity *entity in topicEntity.options) {
                    if ([answer isEqualToString:entity.value] && entity.isSelected) {
                        answerNumber += 1;
                    }
                }
            }
            if (answerNumber == answersArray.count) {
                resultEntity.grade = topicEntity.grade;
                allScore += [topicEntity.grade integerValue];
220 221
            }else {
                resultEntity.grade = @(ZERO);
222 223 224 225 226 227 228 229 230 231
            }
        }
        //判断
        if ([topicEntity.topicType isEqualToString:STUDYTOPICTYPE_TRUEORFALSE]) {
            for (CustomTOPassLevelTopicOptionEntity *entity in topicEntity.options) {
                if (entity.isSelected == YES) {
                    resultEntity.answer = entity.name;
                    if ([topicEntity.answer isEqualToString:entity.name]) {
                        resultEntity.grade = topicEntity.grade;
                        allScore += [topicEntity.grade integerValue];
232 233
                    }else {
                        resultEntity.grade = @(ZERO);
234 235 236 237 238 239 240 241 242 243 244 245
                    }
                }
            }
        }
        //单选
        if ([topicEntity.topicType isEqualToString:STUDYTOPICTYPE_SINGLE]) {
            for (CustomTOPassLevelTopicOptionEntity *entity in topicEntity.options) {
                if (entity.isSelected == YES) {
                    resultEntity.answer = entity.value;
                    if ([topicEntity.answer isEqualToString:entity.value]) {
                        resultEntity.grade = topicEntity.grade;
                        allScore += [topicEntity.grade integerValue];
246 247
                    }else {
                        resultEntity.grade = @(ZERO);
248 249 250 251 252 253 254
                    }
                }
            }
        }
        //填空、简答
        if ([topicEntity.topicType isEqualToString:PASSLEVELTOPICTYPE_GAPFILLING] || [topicEntity.topicType isEqualToString:PASSLEVELTOPICTYPE_SHORTANSWER]) {
                resultEntity.answer = topicEntity.answer;
255
                studyResult.passResult = [NSString stringWithFormat:@"%ld",Audit];//人工审核
256 257 258 259 260
        }
        [submitAnswerArray addObject:resultEntity];
    }
    studyResult.details = (NSArray<TOPassLevelResultDetailEntity>*)submitAnswerArray;
    studyResult.grade = [NSNumber numberWithFloat:allScore];
261
    if ([[self class] isBlankString:studyResult.passResult]) {
262
        studyResult.passResult = [NSString stringWithFormat:@"%ld",(self.emigrated.passGrade > allScore)?NotThrough:Through];
263
    }
264 265
    [XBLoadingView showHUDViewWithDefault];
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
266
    [HTTP networkRequestWithURL:SERVERREQUESTURL(SUBMITEMIGRATEDANSWER) withRequestType:ZERO withParameter:studyResult withReturnValueBlock:^(id returnValue) {
267 268
        
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
269
        if (RESULT(returnValue)) {
270
            [weakSelf determineIsQualified:[studyResult.grade integerValue] passResult:[studyResult.passResult integerValue]];
271 272 273
            if ([weakSelf.delegate respondsToSelector:@selector(emigratedFinish)]) {
                [weakSelf.delegate emigratedFinish];
            }
274 275 276 277
        }else{
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
        }
        
278
    } withFailureBlock:^(NSError *error) {
279
        [XBLoadingView hideHUDViewWithDefault];
280 281 282 283 284
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark - 判断得分是否及格
285
- (void)determineIsQualified:(NSInteger)allScore passResult:(NSInteger)state
286
{
曹云霄's avatar
曹云霄 committed
287 288 289
    EmigratedFinishViewController *finish = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"EmigratedFinishViewController"];
    finish.delegate = self;
    finish.preferredContentSize = CGSizeMake(520, 400);
290
    if (self.emigrated.passGrade > allScore && state != Audit) {
291
        finish.state = ZERO;
曹云霄's avatar
曹云霄 committed
292
        finish.score = [NSString stringWithFormat:@"%ld",allScore];
293 294
        [self showPopoverView:finish];
        
295 296
    }else if (state == Audit) {
        finish.state = Audit;
297 298
        finish.score = [NSString stringWithFormat:@"%ld",allScore];
        [self showPopoverView:finish];
299 300 301
    }else {
        WS(weakSelf);
        [XBLoadingView showHUDViewWithDefault];
302
        NSString *url = [NSString stringWithFormat:SERVERREQUESTURL(GETINTEGRAL),RECRUITTHROUGH];
曹云霄's avatar
曹云霄 committed
303
        [HTTP networkWithDictionaryRequestWithURL:url withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
304
            [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
305
            if (RESULT(returnValue)) {
306
                finish.state = ONE;
曹云霄's avatar
曹云霄 committed
307
                finish.score = [NSString stringWithFormat:@"%ld",allScore];
308
                finish.integral = [NSString stringWithFormat:@"+%@",returnValue[@"data"]];
曹云霄's avatar
曹云霄 committed
309
                [weakSelf showPopoverView:finish];
310 311 312 313 314
                
            }else {
                [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
            }
            
315
        } withFailureBlock:^(NSError *error) {
316
            [XBLoadingView hideHUDViewWithDefault];
317 318 319 320 321
            [XBLoadingView showHUDViewWithText:error.localizedDescription];
        }];
    }
}

曹云霄's avatar
曹云霄 committed
322
#pragma mark - 完成
323
- (void)finished
曹云霄's avatar
曹云霄 committed
324 325 326 327
{
    [self.popover dismissPopoverAnimated:YES];
}

328 329
#pragma mark - 取消闯关
- (IBAction)exitButtonClickAction:(UIButton *)sender {
330 331
    
    WS(weakSelf);
332
    ShowDefaultAlertView(self, nil, @"是否退出闯关?", UIAlertControllerStyleActionSheet, ^{
333 334 335
        if ([weakSelf.delegate respondsToSelector:@selector(dismissController:)]) {
            [weakSelf.delegate dismissController:YES];
        }
336
    }, nil);
337 338
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
#pragma mark - 弹出框
- (void)showPopoverView:(BaseViewController *)controller
{
    self.popover = [[WYPopoverController alloc] initWithContentViewController:controller];
    self.popover.theme.fillBottomColor = [UIColor clearColor];
    self.popover.theme.fillTopColor = [UIColor clearColor];
    self.popover.theme.glossShadowColor = [UIColor clearColor];
    self.popover.delegate = self;
    [self.popover presentPopoverAsDialogAnimated:YES options:WYPopoverAnimationOptionFadeWithScale];
}

#pragma mark - 点击空白禁止收起
- (BOOL)popoverControllerShouldDismissPopover:(WYPopoverController *)popoverController
{
    return NO;
}





@end