NewAssessmentViewController.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // NewAssessmentViewController.swift
  3. // LMS
  4. //
  5. // Created by Suraj Kumar Mandal on 19/10/23.
  6. //
  7. import UIKit
  8. import SideMenu
  9. class NewAssessmentViewController: UIViewController {
  10. @IBOutlet var navigationBar: UINavigationBar!
  11. @IBOutlet var assessmentListCollectionView: UICollectionView!
  12. var viewModel = NewAssessmentViewModel()
  13. var newAssessmentListModel = [NewAssessmentModel]()
  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. assessmentListCollectionView.delegate = self
  22. assessmentListCollectionView.dataSource = self
  23. setupUI()
  24. }
  25. func setupUI() {
  26. navigationBar.topItem?.title = "Assessment"
  27. }
  28. override func viewWillAppear(_ animated: Bool) {
  29. if Reachability.isConnectedToNetwork() {
  30. viewModel.getNewAssessmentList(userId: userData[0].id)
  31. } else {
  32. Alert.showInternetFailureAlert(on: self)
  33. }
  34. }
  35. /*
  36. // MARK: - Navigation
  37. // In a storyboard-based application, you will often want to do a little preparation before navigation
  38. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  39. // Get the new view controller using segue.destination.
  40. // Pass the selected object to the new view controller.
  41. }
  42. */
  43. @IBAction func sideMenuAction(_ sender: UIBarButtonItem) {
  44. let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
  45. present(menu, animated: true, completion: nil)
  46. }
  47. }
  48. extension NewAssessmentViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  49. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  50. return newAssessmentListModel.count
  51. }
  52. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  53. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewAssessmentCollectionViewCell", for: indexPath as IndexPath) as! NewAssessmentCollectionViewCell
  54. cell.customView.layer.borderWidth = 1
  55. cell.customView.layer.borderColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
  56. cell.customView.layer.cornerRadius = 5
  57. cell.serialNoLabel.text = "\(indexPath.row + 1)"
  58. cell.testNameLabel.text = newAssessmentListModel[indexPath.row].name
  59. cell.scoreLabel.text = "Highest Score:" + "\(newAssessmentListModel[indexPath.row].achievedMarks ?? 0)" + "/" + "\(newAssessmentListModel[indexPath.row].totalMarks ?? 0)"
  60. cell.attemptsLabel.text = "No. Of Attempts: \(newAssessmentListModel[indexPath.row].numberofAtmt ?? 0)"
  61. cell.takeExamButton.layer.cornerRadius = 5
  62. cell.takeExamButton.tag = indexPath.row
  63. cell.takeExamButton.addTarget(self, action: #selector(startQuiz), for: .touchUpInside)
  64. return cell
  65. }
  66. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  67. return CGSize(width: (collectionView.frame.size.width-10)/2, height: 280)
  68. }
  69. // func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  70. // let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewAssessmentCollectionViewCell", for: indexPath as IndexPath) as! NewAssessmentCollectionViewCell
  71. //
  72. // cell.testNameLabel.text = newAssessmentListModel[indexPath.row].name
  73. //
  74. // // Calculate the cell size based on the label's content and constraints
  75. // let labelSize = cell.testNameLabel.sizeThatFits(CGSize(width: cell.testNameLabel.frame.width, height: .greatestFiniteMagnitude))
  76. // let cellHeight = labelSize.height
  77. //
  78. // return CGSize(width: (collectionView.frame.size.width-10)/2, height: cellHeight + 220)
  79. // }
  80. @objc func startQuiz(sender: UIButton) {
  81. let vc = self.storyboard?.instantiateViewController(withIdentifier: "InstructionViewController") as! InstructionViewController
  82. vc.assessmentId = newAssessmentListModel[sender.tag].id ?? 0
  83. vc.assessmentName = newAssessmentListModel[sender.tag].name ?? ""
  84. vc.quesCount = newAssessmentListModel[sender.tag].totalQuestions ?? 0
  85. vc.assessmentType = "Assessment"
  86. self.navigationController?.pushViewController(vc, animated: true)
  87. }
  88. }
  89. extension NewAssessmentViewController: NewAssessmentProtocol {
  90. func startLoader() {
  91. ActivityIndicator.start()
  92. }
  93. func stopLoader() {
  94. ActivityIndicator.stop()
  95. }
  96. func showError(error: String) {
  97. self.view.makeToast(error)
  98. }
  99. func assessmentListModel(model: [NewAssessmentModel]) {
  100. self.newAssessmentListModel = model
  101. self.assessmentListCollectionView.reloadData()
  102. }
  103. }