Exemple #1
0
        public ActionResult Edit(session session)
        {
            var      sessionRepo   = new SessionRepository();
            session  oSession      = null;
            var      cu            = Session["user"] as ContextUser;
            DateTime?oldActualDate = null;

            if (session.Id == 0)
            {
                oSession           = new session();
                oSession.RowGUID   = Guid.NewGuid();
                oSession.CreatedAt = DateTime.Now;
                oSession.CreatedBy = cu.OUser.Id;
                oSession.Status    = SessionStatus.Pending.ToString();
            }
            else
            {
                oSession           = sessionRepo.Get(session.Id);
                oSession.UpdatedAt = DateTime.Now;
                oldActualDate      = oSession.ActualDateTime;
                // oSession.UpdatedBy = int.Parse(sid);
            }
            var currentStatus = (SessionStatus)Enum.Parse(typeof(SessionStatus), oSession.Status);


            if (cu.EnumRole == EnumUserRole.SuperAdmin && currentStatus == SessionStatus.Pending)
            {
                oSession.ProgramPurpose = "ProgramPurposeNotUsed";
                oSession.TargetGroup    = session.TargetGroup;

                if (oSession.SchoolID == null && session.SchoolID != null && session.SchoolID.Value != 0)
                {
                    var cor                   = new CoordinatorRepository().GetBySchool(session.SchoolID.Value);
                    var user                  = new AccountRepository().Get(oSession.CreatedBy);
                    var bogusController       = Util.CreateController <EmailTemplateController>();
                    EmailTemplateModel emodel =
                        new EmailTemplateModel
                    {
                        Title           = "Notification: Session Created For School",
                        User            = user.FirstName,
                        CoordinatorName = cor.CoordinatorName,
                        SessionTitle    = oSession.ProgramName,
                        UserName        = user.Username,
                        Email           = cor.CoordinatorEmail
                    };
                    string body =
                        Util.RenderViewToString(bogusController.ControllerContext, "SessionCreatedSchool", emodel);
                    EmailSender.SendSupportEmail(body, cor.CoordinatorEmail);
                }
                oSession.SchoolID                  = session.SchoolID == 0 ? null : session.SchoolID;
                oSession.ProgramName               = session.ProgramName;
                oSession.ProposedDateTime          = DateTime.ParseExact(session.PropesedDateString, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                oSession.ProposedEndDateTime       = session.ProposedEndDateTime;
                oSession.VolunteerId               = session.VolunteerId == 0 ? null : session.VolunteerId;
                oSession.StudentCertificate        = session.StudentCertificate == 0 ? null : session.StudentCertificate;
                oSession.VolunteerCertificate      = session.VolunteerCertificate == 0 ? null : session.VolunteerCertificate;
                oSession.StudentEvaluationCatagory = session.StudentEvaluationCatagory;
            }
            if (session.ActualDateString != null)
            {
                oSession.ActualDateTime = DateTime.ParseExact(session.ActualDateString, "dd/MM/yyyy", CultureInfo.InvariantCulture);
            }
            if (currentStatus == SessionStatus.DateChanges)
            {
                if (session.SendKitByMailCourier && !oSession.SendKitByMailCourier)
                {
                    var cor                   = oSession.school.coordinator_profile.First();
                    var user                  = new AccountRepository().Get(oSession.CreatedBy);
                    var bogusController       = Util.CreateController <EmailTemplateController>();
                    EmailTemplateModel emodel =
                        new EmailTemplateModel
                    {
                        Title           = "Injaz: Administrator send kit by mail courier.",
                        User            = user.FirstName,
                        CoordinatorName = cor.CoordinatorName,
                        SessionTitle    = oSession.ProgramName,
                    };
                    string body =
                        Util.RenderViewToString(bogusController.ControllerContext, "SendKitBymailCourier", emodel);
                    EmailSender.SendSupportEmail(body, cor.CoordinatorEmail);
                }
                oSession.SendKitByMailCourier = session.SendKitByMailCourier;
            }
            if (cu.EnumRole == EnumUserRole.Coordinator && (currentStatus == SessionStatus.Approved || currentStatus == SessionStatus.Rejected))
            {
                if (session.ConfirmKitReceivedBySchool && !oSession.ConfirmKitReceivedBySchool)
                {
                    var cor                   = oSession.school.coordinator_profile.First();
                    var user                  = new AccountRepository().Get(oSession.CreatedBy);
                    var bogusController       = Util.CreateController <EmailTemplateController>();
                    EmailTemplateModel emodel =
                        new EmailTemplateModel
                    {
                        Title           = "Injaz: Confirm Kit Received By School.",
                        CoordinatorName = cor.CoordinatorName,
                        SessionTitle    = oSession.ProgramName,
                        User            = cor.user.FirstName
                    };
                    string body =
                        Util.RenderViewToString(bogusController.ControllerContext, "ConfirmKitReceivedBySchool", emodel);
                    EmailSender.SendSupportEmail(body, user.Email);
                }
                oSession.ConfirmKitReceivedBySchool = session.ConfirmKitReceivedBySchool;
            }
            if (currentStatus == SessionStatus.Pending && oSession.ActualDateTime != null)
            {
                if (oSession.ProposedDateTime.Date == oSession.ActualDateTime.Value.Date)
                {
                    if (oSession.Status != SessionStatus.Approved.ToString())
                    {
                        SendEmailNotificationsApprovedByAdmin(oSession);
                    }
                    oSession.Status          = SessionStatus.Approved.ToString();
                    oSession.ApprovedByAdmin = true;
                }
                else
                {
                    if (oSession.Status != SessionStatus.DateChanges.ToString())
                    {
                        SendEmailNotificationDateChanged(oSession);
                    }
                    oSession.Status = SessionStatus.DateChanges.ToString();
                }
            }
            if (currentStatus == SessionStatus.Approved && oldActualDate != null && oSession.ActualDateTime != oldActualDate)
            {
                if (oSession.Status != SessionStatus.Approved.ToString())
                {
                    SendEmailNotificationDateChanged(oSession);
                }
                oSession.Status          = SessionStatus.DateChanges.ToString();
                oSession.ApprovedByAdmin = false;
            }
            if (session.SubmitButton != null)
            {
                if (session.SubmitButton == "approved")
                {
                    if (oSession.Status != SessionStatus.Approved.ToString())
                    {
                        SendEmailNotificationsApprovedByAdmin(oSession);
                    }
                    oSession.Status          = SessionStatus.Approved.ToString();
                    oSession.ApprovedByAdmin = true;
                }
                if (session.SubmitButton == "submitpre")
                {
                    int    participantId = new ParticipiantRepository().GetByUserId(cu.OUser.Id).Id;
                    string eve_cat       = oSession.StudentEvaluationCatagory;
                    string form          = new EvaluationFormRepository().Get().First(x => x.Catagory == eve_cat && x.SubCatagory == "pre").FormPath;
                    return(RedirectToAction(form, "EvaluationForm", new { participantid = participantId, sessionid = oSession.Id }));
                }
                if (session.SubmitButton == "submitpost")
                {
                    int    participantId = new ParticipiantRepository().GetByUserId(cu.OUser.Id).Id;
                    string eve_cat       = oSession.StudentEvaluationCatagory;
                    string form          = new EvaluationFormRepository().Get().First(x => x.Catagory == eve_cat && x.SubCatagory == "post").FormPath;
                    return(RedirectToAction(form, "EvaluationForm", new { participantid = participantId, sessionid = oSession.Id }));
                }
                if (session.SubmitButton == "request")
                {
                    //oSession.MarkedCompletedByVolunteer = true;
                    var volunteer = new VolunteerRepository().GetByGoogleId(cu.GoogleId) ?? new VolunteerRepository().GetByLinkedInId(cu.GoogleId);
                    oSession.VolunteerId = volunteer.Id;

                    var user                  = new AccountRepository().Get(oSession.CreatedBy);
                    var bogusController       = Util.CreateController <EmailTemplateController>();
                    EmailTemplateModel emodel =
                        new EmailTemplateModel
                    {
                        Title         = "Injaz:Volunteer Assign Session",
                        VolunteerName = volunteer.VolunteerName,
                        SessionTitle  = oSession.ProgramName,
                        User          = user.FirstName
                    };
                    string body =
                        Util.RenderViewToString(bogusController.ControllerContext, "VolunteerAssignSession", emodel);
                    EmailSender.SendSupportEmail(body, user.Email);
                }
                if (session.SubmitButton == "confirm")
                {
                    return(RedirectToAction("StudentAttendense", new { sessionId = oSession.RowGUID }));
                }
                if (session.SubmitButton == "reject")
                {
                    oSession.Status = SessionStatus.Rejected.ToString();
                }
                if (session.SubmitButton == "feedback")
                {
                    var participant = new ParticipiantRepository().GetByUserId(cu.OUser.Id);

                    return(RedirectToAction("FeedBack", new { sessionId = oSession.Id, participantId = participant.Id }));
                }
                if (session.SubmitButton == "coordinatorform")
                {
                    int corId = new CoordinatorRepository().GetByUserId(oSession.school.CoordinatorUserId).Id;
                    return(RedirectToAction("CoordinatorForm", "EvaluationForm", new { sessionId = oSession.Id, volId = oSession.volunteer_profile.Id, corId = corId }));
                }
                if (session.SubmitButton == "viewcertificate" || session.SubmitButton == "volunteerviewcertificate")
                {
                    var    participant     = new ParticipiantRepository().GetByUserId(cu.OUser.Id);
                    string certificatePath = null;

                    List <PdfCoordinatesModel> pdfCoordinates = null;
                    if (cu.EnumRole == EnumUserRole.Participant)
                    {
                        certificatePath = Server.MapPath(oSession.certificate.UploadFilePath);
                        pdfCoordinates  = new CertificateDictionary().GetPdfCoordinatesFromDictionary(oSession.certificate.Type);
                    }
                    else
                    {
                        certificatePath = Server.MapPath(oSession.certificate1.UploadFilePath);
                        pdfCoordinates  = new CertificateDictionary().GetPdfCoordinatesFromDictionary(oSession.certificate1.Type);
                        oSession.IsVolunteerCertificateGenerated = true;
                    }
                    foreach (var pc in pdfCoordinates)
                    {
                        switch (pc.CertifiacteData)
                        {
                        case CertificateEnum.NameOfStudent:
                            pc.Text = participant.Name + " " + participant.FatherName + " " + participant.Family;
                            break;

                        case CertificateEnum.NameOfCoordinator:
                            pc.Text = oSession.school.coordinator_profile.First().CoordinatorName;
                            break;

                        case CertificateEnum.ProgrammYear:
                            pc.Text = DateTime.Now.Year + "";
                            break;

                        case CertificateEnum.TranningDate:
                            pc.Text = Util.DateConversion(oSession.ActualDateTime.Value.ToShortDateString(), "Hijri", "en-us");
                            break;

                        case CertificateEnum.TranningHour:
                            pc.Text = ConfigurationManager.AppSettings["TranningHours"];
                            break;

                        case CertificateEnum.NameOfVolunteer:
                            pc.Text = oSession.volunteer_profile.VolunteerName;
                            break;
                        }
                    }
                    string fontFilePath = Server.MapPath("~/fonts/Arabic Fonts/trado.ttf");
                    var    outputFile   = PdfGenerator.GenerateOnflyPdf(certificatePath, pdfCoordinates, fontFilePath);
                    if (session.SubmitButton == "viewcertificate")
                    {
                        EmailSender.SendSupportEmail("Student Template", participant.Email, outputFile);
                        oSession.session_participant.Where(x => x.ParticipantID == participant.Id).First().IsCertificateGenerated = true;
                    }
                    else
                    {
                        EmailSender.SendSupportEmail("Volunteer Template", oSession.volunteer_profile.VolunteerEmail, outputFile);
                    }
                }
            }
            oSession.IsActive = session.IsActive;
            if (session.Id == 0)
            {
                sessionRepo.Post(oSession);
            }
            else
            {
                sessionRepo.Put(oSession.Id, oSession);
            }
            return(RedirectToAction("Index"));
        }