Advertisement
Guest User

my class

a guest
Apr 12th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  BiletChildViewController.m
  3. //  diplom
  4. //
  5. //  Created by Sergey Kiselev on 29.01.14.
  6. //  Copyright (c) 2014 Sergey Kiselev. All rights reserved.
  7.  
  8. #import "BiletChildViewController.h"
  9.  
  10. @interface BiletChildViewController ()
  11.  
  12. @end
  13.  
  14. @implementation BiletChildViewController
  15.  
  16. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  17.    
  18.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  19.    
  20.     if (self) {
  21.     }
  22.    
  23.     return self;
  24. }
  25.  
  26. - (void)viewDidLoad {
  27.     [super viewDidLoad];
  28.     [self getAnswers];
  29.     self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  30. }
  31.  
  32. - (void)showComment {
  33.     NSArray *array = [self getAnswers];
  34.     NSUInteger arrayCount = array.count;
  35.     NSString *comment = [NSString stringWithFormat:@"%@ \n Правильный ответ - %d.", self.getAnswers[arrayCount - 1], [self.getAnswers[arrayCount - 2] intValue]];
  36.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Подсказка"
  37.                                                     message:comment
  38.                                                    delegate:self
  39.                                           cancelButtonTitle:@"ОК"
  40.                                           otherButtonTitles:nil];
  41.     [alert show];
  42. }
  43.  
  44. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSUInteger)section {
  45.     int height =  (_imageView.image == 0) ? 0 : 118;
  46.    
  47.     return height;
  48. }
  49.  
  50. - (NSMutableArray *)getAnswers {
  51.     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pdd.sqlite"];
  52.     const char *dbpath = [defaultDBPath UTF8String];
  53.     sqlite3_stmt *statement;
  54.     NSMutableArray *array = [[NSMutableArray alloc] init];
  55.     if (sqlite3_open(dbpath, &_pdd_ab) == SQLITE_OK) {
  56.         NSString *querySQL = [NSString stringWithFormat:@"SELECT RecNo, Picture, Question, Answer1, Answer2, Answer3, Answer4, Answer5, RightAnswer, Comment FROM paper_ab WHERE PaperNumber = \"%d\" AND QuestionInPaper = \"%d\"", (int)_biletNumber + 1, (int)_index + 1];
  57.         const char *query_stmt = [querySQL UTF8String];
  58.         if (sqlite3_prepare_v2(_pdd_ab, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
  59.             if (sqlite3_step(statement) == SQLITE_ROW) {
  60.                 NSData *picture = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 1) length: sqlite3_column_bytes(statement, 1)];
  61.                 _imageView.image = [UIImage imageWithData:picture];
  62.                 for (int i = 2; i < 10; i++) {
  63.                     if (sqlite3_column_text(statement, i) != NULL) {
  64.                         NSString *arrayelement = [[NSString alloc]
  65.                                                   initWithUTF8String:
  66.                                                   (const char *) sqlite3_column_text(statement, i)];
  67.                         [array addObject:arrayelement];
  68.                     }
  69.                 }
  70.                 sqlite3_finalize(statement);
  71.             }
  72.             else {
  73.                 NSLog(@"Rezultatov net!");
  74.             }
  75.         }
  76.         else {
  77.             NSLog(@"Ne mogu vypolnit' zapros!");
  78.         }
  79.         sqlite3_close(_pdd_ab);
  80.     }
  81.     else {
  82.         NSLog(@"Ne mogu ustanovit' soedinenie!");
  83.     }
  84.    
  85.     return array;
  86. }
  87.  
  88. - (void)didReceiveMemoryWarning {
  89.    
  90.     [super didReceiveMemoryWarning];
  91.    
  92. }
  93.  
  94. - (NSUInteger)numberOfSectionInTableView:(UITableView *)tableView {
  95.     return 1;
  96. }
  97.  
  98. - (NSUInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSUInteger)section {
  99.     NSArray *array = [self getAnswers];
  100.    
  101.     return array.count - 2;
  102. }
  103.  
  104. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  105.     NSUInteger rowNumber = [indexPath row];
  106.     static NSString *CellIdentifier = @"Cell";
  107.     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  108.     NSMutableArray *answerArray = [self getAnswers];
  109.     if (rowNumber == 0) {
  110.         cell.selectionStyle = UITableViewCellSelectionStyleNone;
  111.         cell.textLabel.font = [UIFont italicSystemFontOfSize:15.0];
  112.         cell.textLabel.textAlignment = NSTextAlignmentCenter;
  113.         cell.textLabel.text = [NSString stringWithFormat:@"%@", answerArray[0]];
  114.         cell.textLabel.numberOfLines = 0;
  115.     }
  116.     else {
  117.         cell.textLabel.font = [UIFont systemFontOfSize:15.0];
  118.         cell.textLabel.text = [NSString stringWithFormat:@"%ld. %@", (long)rowNumber, answerArray[rowNumber]];
  119.         cell.textLabel.numberOfLines = 0;
  120.     }
  121.    
  122.     UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
  123.     backView.backgroundColor = [UIColor clearColor];
  124.     cell.backgroundView = backView;
  125.     NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  126.                                           cell.textLabel.font, NSFontAttributeName,
  127.                                           nil];
  128.     CGRect textLabelSize = [cell.textLabel.text boundingRectWithSize:kExamenLabelFrameMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil];
  129.     cell.textLabel.frame = CGRectMake(5, 5, textLabelSize.size.width, textLabelSize.size.height);
  130.    
  131.     return cell;
  132. }
  133.  
  134. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  135.     NSUInteger rowNumber = [indexPath row];
  136.     NSArray *array = [self getAnswers];
  137.     if ([self.rightAnswersArray containsObject:[NSNumber numberWithLong:_index + 1]] ) { // делаю красиво, если пользователь возвращается к вопросу, на который уже правильно ответил
  138.         self.tableView.allowsSelection = NO;
  139.         if (rowNumber == [array[array.count - 2] intValue])  // если ответ правильный
  140.             cell.contentView.backgroundColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
  141.     }
  142.     else if ([self.wrongAnswersArray containsObject:[NSNumber numberWithLong:_index + 1]] ) { // делаю красиво, если пользователь возвращается к вопросу, на который уже НЕправильно ответил
  143.         long questnum = [self.wrongAnswersArray indexOfObject:[NSNumber numberWithInteger:_index + 1]];
  144.         self.tableView.allowsSelection = NO;
  145.         if (rowNumber != [array[array.count - 2] intValue] && [NSNumber numberWithLong:rowNumber] == [NSNumber numberWithInt:[[self.wrongAnswersSelectedArray objectAtIndex:questnum] intValue]])  // если ответ НЕправильный
  146.             cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
  147.     }
  148. }
  149.  
  150. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  151.     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
  152.     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  153.     NSUInteger rowNumber = [indexPath row];
  154.     NSArray *array = [self getAnswers];
  155.     if (rowNumber == 0) {
  156.         cell.contentView.backgroundColor = [UIColor whiteColor];
  157.         cell.selectionStyle = UITableViewCellSelectionStyleNone;
  158.         [self showComment];
  159.     }
  160.     else {
  161.         if (rowNumber == [array[array.count - 2] intValue]) { // если ответ правильный
  162.             if ([settings boolForKey:@"needVibro"]) {
  163.                 AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); // вибрация при правильном ответе
  164.             }
  165.             cell.contentView.backgroundColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
  166.             self.tableView.allowsSelection = NO;
  167.             [self.rightAnswersArray addObject:[NSNumber numberWithLong:_index + 1]];
  168.         }
  169.         else { // если ответ неправильный
  170.             cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
  171.             self.tableView.allowsSelection = NO;
  172.             [self.wrongAnswersArray addObject:[NSNumber numberWithLong:_index + 1]];
  173.             [self.wrongAnswersSelectedArray addObject:[NSNumber numberWithLong:rowNumber]];
  174.             if ([settings boolForKey:@"showComment"]) {
  175.                 [self showComment];
  176.             }
  177.         }
  178.     }
  179.     NSUInteger wrongCount = _wrongAnswersArray.count;
  180.     NSUInteger rightCount = _rightAnswersArray.count;
  181.     NSLog(@"Номер вопроса - %d, правильных ответов - %d, неправильных ответов - %d", (int)_index + 1, (int)rightCount, (int)wrongCount);
  182.     if (rightCount + wrongCount == 20) {
  183.         [self writeStatisticsToBase];
  184.         [self getResultOfTest];
  185.     }
  186. }
  187.  
  188. - (void)getResultOfTest {
  189.         [self performSegueWithIdentifier:@"ResultBilet" sender:self];
  190. }
  191.  
  192. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  193.     if ([[segue identifier] isEqualToString:@"ResultBilet"]) {
  194.         ResultViewController *detailViewController = [segue destinationViewController];
  195.         detailViewController.examen = false;
  196.         detailViewController.rightCount = (int)_rightAnswersArray.count;
  197.         detailViewController.time = _finishDate - _startDate;
  198.     }
  199. }
  200.  
  201. - (void)writeStatisticsToBase {
  202.     NSString *docsDir;
  203.     NSArray *dirPaths;
  204.     dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  205.     docsDir = [dirPaths objectAtIndex:0];
  206.     NSString *defaultDBPath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"pdd_stat.sqlite"]];
  207.     const char *dbpath = [defaultDBPath UTF8String];
  208.     sqlite3_stmt *statement;
  209.     NSDate *date = [[NSDate alloc] init];
  210.     _finishDate = [date timeIntervalSince1970];
  211.     if (sqlite3_open(dbpath, &_pdd_ab_stat) == SQLITE_OK) {
  212.         NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO paper_ab_stat(biletNumber, rightCount, wrongCount, startDate, finishDate) VALUES ('%d', '%d', '%d', '%11.0f', '%11.0f')", (int)_biletNumber + 1, (int)_rightAnswersArray.count, (int)_wrongAnswersArray.count, _startDate, _finishDate];
  213.         const char *insert_stmt = [insertSQL UTF8String];
  214.         sqlite3_prepare_v2(_pdd_ab_stat, insert_stmt, -1, &statement, NULL);
  215.         if (sqlite3_step(statement) == SQLITE_DONE) {
  216.             NSLog(@"Zapis' proizvedena uspeshno");
  217.         }
  218.         else {
  219.             NSLog(@"Zapis' proizvedena neuspeshno");
  220.         }
  221.         sqlite3_finalize(statement);
  222.         sqlite3_close(_pdd_ab_stat);
  223.     }
  224.     else {
  225.         NSLog(@"Ne mogu ustanovit' soedinenie!");
  226.     }
  227. }
  228.  
  229. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  230.     NSUInteger rowNumber = [indexPath row];
  231.     NSMutableArray *answerArray = [self getAnswers];
  232.     NSString *textLabel = [NSString stringWithFormat:@"%ld. %@", (long)rowNumber, answerArray[rowNumber]];
  233.     NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  234.                                           [UIFont systemFontOfSize:15.0f], NSFontAttributeName,
  235.                                           nil];
  236.     CGRect textLabelSize = [textLabel boundingRectWithSize:kExamenLabelFrameMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil];
  237.    
  238.     return kExamenDifference + textLabelSize.size.height;
  239. }
  240.  
  241. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement