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

#import "AssessmentViewController.h"
#import "AssessmentHeaderView.h"
#import "AssessmentTableViewCell.h"
12 13
#import "AssessmentQualifiedViewController.h"
#import "AssessmentUnqualifiedViewController.h"
14

15 16 17
@interface AssessmentViewController ()<UITableViewDelegate,UITableViewDataSource,UnqualifiedDelegate,BackDelegate>

@property (nonatomic,strong) TOStudyTaskEntity *taskDetails;
18 19 20 21 22 23


@end

@implementation AssessmentViewController

24 25 26 27 28 29 30 31 32 33 34



#pragma mark -渲染完成
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
35 36
}

37 38
#pragma mark -视图即将消失
- (void)viewWillDisappear:(BOOL)animated
39
{
40 41 42 43 44 45 46 47 48
    [super viewWillDisappear:animated];
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
49
    
50
    [self getExaminationQuestions];
51 52 53 54 55 56
}

#pragma mark - 获取考题
- (void)getExaminationQuestions
{
    WS(weakSelf);
57 58
    [XBLoadingView showHUDViewWithDefault];;
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(STUDYITEMDETAIL),self.taskId] WithRequestType:ONE WithParameter:nil WithReturnValueBlock:^(id returnValue) {
59
        
60
        [XBLoadingView hideHUDViewWithDefault];
61
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
62 63 64
            weakSelf.taskDetails = [[TOStudyTaskEntity alloc]initWithDictionary:returnValue[@"data"] error:nil];
            [weakSelf assessmentCountdown];
            [weakSelf.assessmentTableView reloadData];
65
        }else {
66
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
67 68
        }
        
69
    }WithFailureBlock:^(NSError *error) {
70
        [XBLoadingView hideHUDViewWithDefault];
71 72 73 74 75 76 77 78 79 80 81 82 83
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark - 考核计时
- (void)assessmentCountdown
{
    self.assessmentTitleLabel.text = self.taskDetails.title;
    [self.assessmentTimeLabel beginCountDownWithTimeInterval:self.taskDetails.examMinute*60];
    WS(weakSelf);
    //倒计时结束
    [self.assessmentTimeLabel setCountDownCompleteBlock:^{
        [weakSelf submitAnswer];
84 85 86 87 88 89 90
    }];
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AssessmentTableViewCell *assessmentCell = [tableView dequeueReusableCellWithIdentifier:@"AssessmentTableViewCell" forIndexPath:indexPath];
91 92
    TOStudyTopicEntity *entity = self.taskDetails.topics[indexPath.section];
    assessmentCell.studyEntity = entity.options[indexPath.row];
93 94 95
    return assessmentCell;
}

96 97 98 99 100 101 102 103 104 105 106 107 108
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CustomTOStudyTopicEntity *entity = self.taskDetails.topics[section];
    return entity.titleHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTOStudyTopicEntity *entity = self.taskDetails.topics[indexPath.section];
    CustomTOStudyTopicOptionEntity *answer = entity.options[indexPath.row];
    return answer.answerHeight;
}

109 110
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
111
    return self.taskDetails.topics.count;
112 113 114 115
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
116 117
    TOStudyTopicEntity *entity = self.taskDetails.topics[section];
    return entity.options.count;
118 119 120 121 122
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    AssessmentHeaderView *headerView = [tableView dequeueReusableCellWithIdentifier:@"AssessmentHeaderView"];
123 124
    headerView.questionEntity = self.taskDetails.topics[section];
    return headerView.contentView;
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
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TOStudyTopicEntity *entitys = self.taskDetails.topics[indexPath.section];
    if ([entitys.topicType isEqualToString:STUDYTOPICTYPE_SINGLE] || [entitys.topicType isEqualToString:STUDYTOPICTYPE_TRUEORFALSE]) {
        for (int i=0; i<entitys.options.count; i++) {
            CustomTOStudyTopicOptionEntity *entity = entitys.options[i];
            if (i == indexPath.row) {
                entity.isSelected = !entity.isSelected;
            }else {
                entity.isSelected = NO;
            }
        }
    }else if ([entitys.topicType isEqualToString:STUDYTOPICTYPE_MULTIPLE]) {
        CustomTOStudyTopicOptionEntity *answer = entitys.options[indexPath.row];
        answer.isSelected = !answer.isSelected;
    }
    [self.assessmentTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark - 提交
- (IBAction)submitButtonClickAction:(UIButton *)sender {
    
    [self.assessmentTimeLabel stopTimer];
    [self submitAnswer];
}

#pragma mark - 提交考核结果
- (void)submitAnswer
{
    TOStudyResultEntity *studyResult = [[TOStudyResultEntity alloc]init];
    studyResult.taskId = self.taskId;
    studyResult.employeeId = [Shoppersmanager manager].Shoppers.employee.fid;
159
    studyResult.submitTime = [[NSDate date] httpParameterString];
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
    NSArray *answerArray = self.taskDetails.topics;
    NSMutableArray *submitAnswerArray = [NSMutableArray array];
    NSInteger allScore = 0;//总分
    for (TOStudyTopicEntity *topicEntity in answerArray) {
        
        TOStudyResultDetailEntity *resultEntity = [[TOStudyResultDetailEntity alloc] init];
        resultEntity.topicId = topicEntity.fid;
        //多选
        if ([topicEntity.topicType isEqualToString:STUDYTOPICTYPE_MULTIPLE]) {
            NSMutableString *answers = [[NSMutableString alloc] init];
            for (CustomTOStudyTopicOptionEntity *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];
            }
        }
        //判断
        if ([topicEntity.topicType isEqualToString:STUDYTOPICTYPE_TRUEORFALSE]) {
            for (CustomTOStudyTopicOptionEntity *entity in topicEntity.options) {
                if (entity.isSelected == YES) {
                    resultEntity.answer = entity.name;
                    if ([topicEntity.answer isEqualToString:entity.name]) {
                        allScore += [topicEntity.grade integerValue];
                        resultEntity.grade = topicEntity.grade;
                    }
                }
            }
        }
        //单选
        if ([topicEntity.topicType isEqualToString:STUDYTOPICTYPE_SINGLE]) {
            for (CustomTOStudyTopicOptionEntity *entity in topicEntity.options) {
                if (entity.isSelected == YES) {
                    resultEntity.answer = entity.value;
                    if ([topicEntity.answer isEqualToString:entity.value]) {
                        allScore += [topicEntity.grade integerValue];
                        resultEntity.grade = topicEntity.grade;
                    }
                }
            }
        }
        [submitAnswerArray addObject:resultEntity];
    }
    studyResult.details = (NSArray<TOStudyResultDetailEntity>*)submitAnswerArray;
    studyResult.grade = [NSNumber numberWithInteger:allScore];
    [XBLoadingView showHUDViewWithDefault];
    WS(weakSelf);
    NSLog(@"%@",[studyResult toDictionary]);
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(SUBMITANSWER) WithRequestType:ZERO WithParameter:studyResult WithReturnValueBlock:^(id returnValue) {
        
        [XBLoadingView hideHUDViewWithDefault];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            [weakSelf determineIsQualified:[studyResult.grade integerValue]];
        }else{
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
        }
        
    } WithFailureBlock:^(NSError *error) {
232
        [XBLoadingView hideHUDViewWithDefault];
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}


#pragma mark - 判断得分是否及格
- (void)determineIsQualified:(NSInteger)allScore
{
    if (self.taskDetails.passGrade > allScore) {
        AssessmentUnqualifiedViewController *unQualified = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"AssessmentUnqualifiedViewController"];
        unQualified.allScore = [NSString stringWithFormat:@"%ld",allScore];
        unQualified.opportunityCount = 3;
        unQualified.delgate = self;
        unQualified.preferredContentSize = CGSizeMake(300, 300);
        unQualified.modalPresentationStyle = UIModalPresentationFormSheet;
        UIPopoverPresentationController *pop = unQualified.popoverPresentationController;
        pop.sourceView = unQualified.view;
        [self presentViewController:unQualified animated:YES completion:nil];
    
    }else {
        WS(weakSelf);
        [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
255
        [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(GETINTEGRAL),@"studyScore"] WithRequestType:ONE WithParameter:nil WithReturnValueBlock:^(id returnValue) {
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
            [XBLoadingView hideHUDViewWithDefault];
            if ([returnValue[@"code"] isEqualToNumber:@0]) {
                AssessmentQualifiedViewController *qualified = [[[weakSelf class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"AssessmentQualifiedViewController"];
                qualified.delegate = self;
                qualified.intrgral = [NSString stringWithFormat:@"+%@",returnValue[@"data"][@"optionValue"]];
                qualified.allScore = [NSString stringWithFormat:@"%ld",allScore];
                qualified.preferredContentSize = CGSizeMake(300, 300);
                qualified.modalPresentationStyle = UIModalPresentationFormSheet;
                UIPopoverPresentationController *pop = qualified.popoverPresentationController;
                pop.sourceView = qualified.view;
                [weakSelf presentViewController:qualified animated:YES completion:nil];
            }else {
                [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
            }
            
        } WithFailureBlock:^(NSError *error) {
272
            [XBLoadingView hideHUDViewWithDefault];
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
            [XBLoadingView showHUDViewWithText:error.localizedDescription];
        }];
    }
}

#pragma mark - <UnqualifiedDelegate>考核不通过
#pragma mark - 重新开始
- (void)startAgainAction
{
    WS(weakSelf);
    [self.assessmentTimeLabel stopTimer];
    [self dismissViewControllerAnimated:YES completion:^{
        [weakSelf.assessmentTableView setContentOffset:CGPointMake(0,0) animated:YES];
        [weakSelf getExaminationQuestions];
    }];
}

#pragma mark - 再学一遍
- (void)learnAgainAction
{
    WS(weakSelf);
    [self.assessmentTimeLabel stopTimer];
    [self dismissViewControllerAnimated:YES completion:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}

#pragma mark - <BackDelegate>返回
- (void)backClickAction
{
    WS(weakSelf);
    [self dismissViewControllerAnimated:YES completion:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}







315 316 317


@end