123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- //
- // AssessmentBeneficiaryViewController.swift
- // LMS
- //
- // Created by Suraj Kumar Mandal on 24/08/22.
- //
- import UIKit
- import SideMenu
- class AssessmentBeneficiaryViewController: UIViewController {
- @IBOutlet var navigationBar: UINavigationBar!
- @IBOutlet var assessmentListTableView: UITableView!
-
- var viewModel = AssessmentBeneficiaryViewModel()
- var assessmentListModel = [AssessmentListModel]()
- let userData = DBManager.sharedInstance.database.objects(UserDetailsModel.self)
- var upcomingAssessment = [AssessmentListModel]()
- var pastAssessment = [AssessmentListModel]()
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- viewModel.delegate = self
- assessmentListTableView.delegate = self
- assessmentListTableView.dataSource = self
- setupUI()
- }
-
- func setupUI() {
- navigationBar.topItem?.title = "Final Exam"
- }
-
- override func viewWillAppear(_ animated: Bool) {
- if Reachability.isConnectedToNetwork() {
- viewModel.getAssessmentList(userId: userData[0].id, month: getCurrentMonth())
- } else {
- Alert.showInternetFailureAlert(on: self)
- }
- }
-
- func formattedDateFromString(dateString: String, withFormat format: String) -> String? {
- let inputFormatter = DateFormatter()
- inputFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
- if let date = inputFormatter.date(from: dateString) {
- let outputFormatter = DateFormatter()
- outputFormatter.dateFormat = format
- return outputFormatter.string(from: date)
- }
- return nil
- }
-
- func formattedTimeFromString(dateString: String, withFormat format: String) -> String? {
- let inputFormatter = DateFormatter()
- inputFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
- if let date = inputFormatter.date(from: dateString) {
- let outputFormatter = DateFormatter()
- outputFormatter.dateFormat = format
- outputFormatter.timeStyle = .short
- return outputFormatter.string(from: date)
- }
- return nil
- }
-
- func getCurrentMonth() -> String {
- let date = Date()
- let format = DateFormatter()
- format.dateFormat = "MM-yyyy"
- let formattedMonth = format.string(from: date)
- print(formattedMonth)
- return formattedMonth
- }
-
- private func executeRepeatedly() {
- // put your code here
- self.assessmentListTableView.reloadData()
- DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) { [weak self] in
- self?.executeRepeatedly()
- }
- }
-
- func setArrayData(array: [AssessmentListModel]) {
- let date = Date()
- let format = DateFormatter()
- format.dateFormat = "yyyy-MM-dd HH:mm:ss"
- let time = format.string(from: date)
-
- for item in array {
- let assessmentTime = format.date(from: item.startDateTime!)
- let currentTime = format.date(from: time)
- if assessmentTime?.compare(currentTime!) == .orderedAscending {
- pastAssessment.append(item)
- } else {
- upcomingAssessment.append(item)
- }
- }
-
- let upcoming = upcomingAssessment.sorted(by: {$0.startDateTime! < $1.startDateTime!})
- let past = pastAssessment.sorted(by: {$0.startDateTime! > $1.startDateTime!})
- self.assessmentListModel = upcoming + past
- executeRepeatedly()
- }
-
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destination.
- // Pass the selected object to the new view controller.
- }
- */
-
- @IBAction func sideMenuAction(_ sender: UIBarButtonItem) {
- let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
- present(menu, animated: true, completion: nil)
- }
- }
- extension AssessmentBeneficiaryViewController: UITableViewDelegate, UITableViewDataSource {
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return assessmentListModel.count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- guard let cell = tableView.dequeueReusableCell(withIdentifier: "AssessmentListTableViewCell", for: indexPath) as? AssessmentListTableViewCell else {
- return UITableViewCell()
- }
-
- cell.selectionStyle = .none
- cell.customView.layer.cornerRadius = 10
- cell.customView.layer.borderWidth = 1
- cell.customView.layer.borderColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
- cell.serialNoLabel.text = "\(indexPath.row + 1)"
- cell.activeStatusLabel.text = assessmentListModel[indexPath.row].assessment?.mode
- if assessmentListModel[indexPath.row].assessment?.mode == "Online" {
- cell.activeStatusImageView.tintColor = #colorLiteral(red: 0.3843137255, green: 0.6470588235, blue: 0.262745098, alpha: 1)
- } else {
- cell.activeStatusImageView.tintColor = #colorLiteral(red: 0.9098039216, green: 0.2980392157, blue: 0.2470588235, alpha: 1)
- }
- cell.timeLabel.isHidden = true
- 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") ?? "") + " "
- cell.timeLabel.text = (formattedTimeFromString(dateString: assessmentListModel[indexPath.row].startDateTime!, withFormat: "HH:mm") ?? "") + " to " + (formattedTimeFromString(dateString: assessmentListModel[indexPath.row].endDateTime!, withFormat: "HH:mm") ?? "")
- cell.assessmentNameLabel.text = assessmentListModel[indexPath.row].assessment?.name
- cell.startButton.layer.cornerRadius = 5
- if assessmentListModel[indexPath.row].attempted == true {
- cell.startButton.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
- cell.startButton.setTitle("Submitted", for: .normal)
- cell.startButton.isUserInteractionEnabled = false
- } else {
- cell.startButton.backgroundColor = #colorLiteral(red: 0.00400000019, green: 0.200000003, blue: 0.3919999897, alpha: 1)
- cell.startButton.setTitle("Start", for: .normal)
- cell.startButton.isUserInteractionEnabled = true
- cell.startButton.tag = indexPath.row
- cell.startButton.addTarget(self, action: #selector(startQuiz), for: .touchUpInside)
- }
-
- return cell
- }
-
- @objc func startQuiz(sender: UIButton) {
- let date = Date()
- let format = DateFormatter()
- format.dateFormat = "yyyy-MM-dd HH:mm:ss"
- let time = format.string(from: date)
- let currentTime = format.date(from: time)
- let startTime = format.date(from: assessmentListModel[sender.tag].startDateTime!)
- let endTime = format.date(from: assessmentListModel[sender.tag].endDateTime!)
-
- if currentTime! >= startTime! && currentTime! <= endTime! {
- if assessmentListModel[sender.tag].assessment?.mode == "Online" {
- let vc = self.storyboard?.instantiateViewController(withIdentifier: "InstructionViewController") as! InstructionViewController
- vc.assessmentId = assessmentListModel[sender.tag].assessment?.id ?? 0
- vc.assessmentName = assessmentListModel[sender.tag].assessment?.name ?? ""
- vc.quesCount = assessmentListModel[sender.tag].assessment?.totalQuestions ?? 0
- vc.assessmentType = "Final Exam"
- self.navigationController?.pushViewController(vc, animated: true)
- } else {
- let vc = self.storyboard?.instantiateViewController(withIdentifier: "OfflineAssessmentViewController") as! OfflineAssessmentViewController
- let fileType = assessmentListModel[sender.tag].assessment?.fileType
- let fileExtension = fileType!.components(separatedBy: "/")[1]
- vc.fileExtension = fileExtension
- vc.assessmentName = assessmentListModel[sender.tag].assessment?.name ?? ""
- vc.assessmentId = assessmentListModel[sender.tag].assessment?.id ?? 0
- vc.fileId = assessmentListModel[sender.tag].assessment?.fileId ?? ""
- self.navigationController?.pushViewController(vc, animated: true)
- }
- } else if currentTime! >= startTime! && currentTime! >= endTime! {
- Alert.showAlert(on: self, with: "Times Up", message: "Exam is already finished!")
- } else {
- Alert.showAlert(on: self, with: "Upcoming", message: "Exam will start on \(formattedDateFromString(dateString: assessmentListModel[sender.tag].startDateTime!, withFormat: "dd-MMM-yyyy hh:mm a") ?? "")")
- }
- }
- }
- extension AssessmentBeneficiaryViewController: AssessmentBeneficiaryProtocol {
- func startLoader() {
- ActivityIndicator.start()
- }
-
- func stopLoader() {
- ActivityIndicator.stop()
- }
-
- func showError(error: String) {
- self.view.makeToast(error)
- }
-
- func assessmentListModel(model: [AssessmentListModel]) {
- self.setArrayData(array: model)
- }
-
- func quizAttempts(assessmentId: Int, count: Int) {
- if count < 2 {
- let vc = self.storyboard?.instantiateViewController(withIdentifier: "InstructionViewController") as! InstructionViewController
- vc.assessmentId = assessmentId
- self.navigationController?.pushViewController(vc, animated: true)
- } else {
- Alert.showAlert(on: self, with: "Attempts Exceed!", message: "You Have Already Attemped Exam 2 Times.")
- }
- }
- }
|