AssessmentBeneficiaryViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // AssessmentBeneficiaryViewController.swift
  3. // LMS
  4. //
  5. // Created by Suraj Kumar Mandal on 24/08/22.
  6. //
  7. import UIKit
  8. import SideMenu
  9. class AssessmentBeneficiaryViewController: UIViewController {
  10. @IBOutlet var navigationBar: UINavigationBar!
  11. @IBOutlet var assessmentListTableView: UITableView!
  12. var viewModel = AssessmentBeneficiaryViewModel()
  13. var assessmentListModel = [AssessmentListModel]()
  14. let userData = DBManager.sharedInstance.database.objects(UserDetailsModel.self)
  15. var upcomingAssessment = [AssessmentListModel]()
  16. var pastAssessment = [AssessmentListModel]()
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. // Do any additional setup after loading the view.
  20. viewModel.delegate = self
  21. assessmentListTableView.delegate = self
  22. assessmentListTableView.dataSource = self
  23. setupUI()
  24. }
  25. func setupUI() {
  26. navigationBar.topItem?.title = "Final Exam"
  27. }
  28. override func viewWillAppear(_ animated: Bool) {
  29. if Reachability.isConnectedToNetwork() {
  30. viewModel.getAssessmentList(userId: userData[0].id, month: getCurrentMonth())
  31. } else {
  32. Alert.showInternetFailureAlert(on: self)
  33. }
  34. }
  35. func formattedDateFromString(dateString: String, withFormat format: String) -> String? {
  36. let inputFormatter = DateFormatter()
  37. inputFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  38. if let date = inputFormatter.date(from: dateString) {
  39. let outputFormatter = DateFormatter()
  40. outputFormatter.dateFormat = format
  41. return outputFormatter.string(from: date)
  42. }
  43. return nil
  44. }
  45. func formattedTimeFromString(dateString: String, withFormat format: String) -> String? {
  46. let inputFormatter = DateFormatter()
  47. inputFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  48. if let date = inputFormatter.date(from: dateString) {
  49. let outputFormatter = DateFormatter()
  50. outputFormatter.dateFormat = format
  51. outputFormatter.timeStyle = .short
  52. return outputFormatter.string(from: date)
  53. }
  54. return nil
  55. }
  56. func getCurrentMonth() -> String {
  57. let date = Date()
  58. let format = DateFormatter()
  59. format.dateFormat = "MM-yyyy"
  60. let formattedMonth = format.string(from: date)
  61. print(formattedMonth)
  62. return formattedMonth
  63. }
  64. private func executeRepeatedly() {
  65. // put your code here
  66. self.assessmentListTableView.reloadData()
  67. DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) { [weak self] in
  68. self?.executeRepeatedly()
  69. }
  70. }
  71. func setArrayData(array: [AssessmentListModel]) {
  72. let date = Date()
  73. let format = DateFormatter()
  74. format.dateFormat = "yyyy-MM-dd HH:mm:ss"
  75. let time = format.string(from: date)
  76. for item in array {
  77. let assessmentTime = format.date(from: item.startDateTime!)
  78. let currentTime = format.date(from: time)
  79. if assessmentTime?.compare(currentTime!) == .orderedAscending {
  80. pastAssessment.append(item)
  81. } else {
  82. upcomingAssessment.append(item)
  83. }
  84. }
  85. let upcoming = upcomingAssessment.sorted(by: {$0.startDateTime! < $1.startDateTime!})
  86. let past = pastAssessment.sorted(by: {$0.startDateTime! > $1.startDateTime!})
  87. self.assessmentListModel = upcoming + past
  88. executeRepeatedly()
  89. }
  90. /*
  91. // MARK: - Navigation
  92. // In a storyboard-based application, you will often want to do a little preparation before navigation
  93. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  94. // Get the new view controller using segue.destination.
  95. // Pass the selected object to the new view controller.
  96. }
  97. */
  98. @IBAction func sideMenuAction(_ sender: UIBarButtonItem) {
  99. let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
  100. present(menu, animated: true, completion: nil)
  101. }
  102. }
  103. extension AssessmentBeneficiaryViewController: UITableViewDelegate, UITableViewDataSource {
  104. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  105. return assessmentListModel.count
  106. }
  107. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  108. guard let cell = tableView.dequeueReusableCell(withIdentifier: "AssessmentListTableViewCell", for: indexPath) as? AssessmentListTableViewCell else {
  109. return UITableViewCell()
  110. }
  111. cell.selectionStyle = .none
  112. cell.customView.layer.cornerRadius = 10
  113. cell.customView.layer.borderWidth = 1
  114. cell.customView.layer.borderColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
  115. cell.serialNoLabel.text = "\(indexPath.row + 1)"
  116. cell.activeStatusLabel.text = assessmentListModel[indexPath.row].assessment?.mode
  117. if assessmentListModel[indexPath.row].assessment?.mode == "Online" {
  118. cell.activeStatusImageView.tintColor = #colorLiteral(red: 0.3843137255, green: 0.6470588235, blue: 0.262745098, alpha: 1)
  119. } else {
  120. cell.activeStatusImageView.tintColor = #colorLiteral(red: 0.9098039216, green: 0.2980392157, blue: 0.2470588235, alpha: 1)
  121. }
  122. cell.timeLabel.isHidden = true
  123. cell.dateLabel.text = " " + (formattedDateFromString(dateString: assessmentListModel[indexPath.row].startDateTime!, withFormat: "dd MMM hh:mm a") ?? "") + " - " + (formattedDateFromString(dateString: assessmentListModel[indexPath.row].endDateTime!, withFormat: "dd MMM hh:mm a") ?? "") + " "
  124. cell.timeLabel.text = (formattedTimeFromString(dateString: assessmentListModel[indexPath.row].startDateTime!, withFormat: "HH:mm") ?? "") + " to " + (formattedTimeFromString(dateString: assessmentListModel[indexPath.row].endDateTime!, withFormat: "HH:mm") ?? "")
  125. cell.assessmentNameLabel.text = assessmentListModel[indexPath.row].assessment?.name
  126. cell.startButton.layer.cornerRadius = 5
  127. if assessmentListModel[indexPath.row].attempted == true {
  128. cell.startButton.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
  129. cell.startButton.setTitle("Submitted", for: .normal)
  130. cell.startButton.isUserInteractionEnabled = false
  131. } else {
  132. cell.startButton.backgroundColor = #colorLiteral(red: 0.00400000019, green: 0.200000003, blue: 0.3919999897, alpha: 1)
  133. cell.startButton.setTitle("Start", for: .normal)
  134. cell.startButton.isUserInteractionEnabled = true
  135. cell.startButton.tag = indexPath.row
  136. cell.startButton.addTarget(self, action: #selector(startQuiz), for: .touchUpInside)
  137. }
  138. return cell
  139. }
  140. @objc func startQuiz(sender: UIButton) {
  141. let date = Date()
  142. let format = DateFormatter()
  143. format.dateFormat = "yyyy-MM-dd HH:mm:ss"
  144. let time = format.string(from: date)
  145. let currentTime = format.date(from: time)
  146. let startTime = format.date(from: assessmentListModel[sender.tag].startDateTime!)
  147. let endTime = format.date(from: assessmentListModel[sender.tag].endDateTime!)
  148. if currentTime! >= startTime! && currentTime! <= endTime! {
  149. if assessmentListModel[sender.tag].assessment?.mode == "Online" {
  150. let vc = self.storyboard?.instantiateViewController(withIdentifier: "InstructionViewController") as! InstructionViewController
  151. vc.assessmentId = assessmentListModel[sender.tag].assessment?.id ?? 0
  152. vc.assessmentName = assessmentListModel[sender.tag].assessment?.name ?? ""
  153. vc.quesCount = assessmentListModel[sender.tag].assessment?.totalQuestions ?? 0
  154. vc.assessmentType = "Final Exam"
  155. self.navigationController?.pushViewController(vc, animated: true)
  156. } else {
  157. let vc = self.storyboard?.instantiateViewController(withIdentifier: "OfflineAssessmentViewController") as! OfflineAssessmentViewController
  158. let fileType = assessmentListModel[sender.tag].assessment?.fileType
  159. let fileExtension = fileType!.components(separatedBy: "/")[1]
  160. vc.fileExtension = fileExtension
  161. vc.assessmentName = assessmentListModel[sender.tag].assessment?.name ?? ""
  162. vc.assessmentId = assessmentListModel[sender.tag].assessment?.id ?? 0
  163. vc.fileId = assessmentListModel[sender.tag].assessment?.fileId ?? ""
  164. self.navigationController?.pushViewController(vc, animated: true)
  165. }
  166. } else if currentTime! >= startTime! && currentTime! >= endTime! {
  167. Alert.showAlert(on: self, with: "Times Up", message: "Exam is already finished!")
  168. } else {
  169. Alert.showAlert(on: self, with: "Upcoming", message: "Exam will start on \(formattedDateFromString(dateString: assessmentListModel[sender.tag].startDateTime!, withFormat: "dd-MMM-yyyy hh:mm a") ?? "")")
  170. }
  171. }
  172. }
  173. extension AssessmentBeneficiaryViewController: AssessmentBeneficiaryProtocol {
  174. func startLoader() {
  175. ActivityIndicator.start()
  176. }
  177. func stopLoader() {
  178. ActivityIndicator.stop()
  179. }
  180. func showError(error: String) {
  181. self.view.makeToast(error)
  182. }
  183. func assessmentListModel(model: [AssessmentListModel]) {
  184. self.setArrayData(array: model)
  185. }
  186. func quizAttempts(assessmentId: Int, count: Int) {
  187. if count < 2 {
  188. let vc = self.storyboard?.instantiateViewController(withIdentifier: "InstructionViewController") as! InstructionViewController
  189. vc.assessmentId = assessmentId
  190. self.navigationController?.pushViewController(vc, animated: true)
  191. } else {
  192. Alert.showAlert(on: self, with: "Attempts Exceed!", message: "You Have Already Attemped Exam 2 Times.")
  193. }
  194. }
  195. }