Ejemplo n.º 1
0
        public ActionResult Action(QuestionViewModel mod)
        {
            Dal             db            = new Dal();
            List <Individu> destinataires = new List <Individu>();
            List <Groupe>   groupes       = new List <Groupe>();

            foreach (string str in mod.Dests.Split(';'))
            {
                destinataires.Add(db.GetIndividus().FirstOrDefault(i => i.userLogin == str));
            }

            foreach (string str in mod.Dests.Split(';'))
            {
                groupes.Add(db.GetGroupes().FirstOrDefault(g => g.nom == str));
            }
            Message content = new Message
            {
                Individu      = db.GetIndividus().Find(i => i.Id == int.Parse(User.Identity.Name)),
                contenu       = mod.Detail,
                sujet         = mod.Sujet,
                lu            = false,
                recu          = false,
                Id_expediteur = int.Parse(User.Identity.Name),
                Id            = db.GetLastIdMessage() + 1,
                envoi         = System.DateTime.Now,
                lecture       = System.DateTime.Now
            };

            if (!mod.question)
            {
                Questionnaire question = new Questionnaire
                {
                    Id   = db.GetLastQuestion() + 1,
                    type = mod.Type,
                    Type_Questionnaire = db.GetTypeQuestion(mod.Type),
                    Id_message         = content.Id,
                };
                foreach (int i in mod.Reponses)
                {
                    var opt   = db.Reponses()[i - 1];
                    int idrep = db.GetRep().Count;
                    idrep += i;
                    Reponses rep = new Reponses
                    {
                        Questionnaire        = question,
                        Id                   = idrep,
                        Id_question          = question.Id,
                        Id_reponse           = i,
                        Option_Questionnaire = opt
                    };
                    db.Addreponse(rep);
                    opt.Reponses.Add(rep);
                    question.Reponses.Add(rep);
                }

                db.GetIndividus().Find(i => i.Id == int.Parse(User.Identity.Name)).Message.Add(content);
                db.GetQuestion().Add(question);
                db.AddMessage(content);

                question.Message = content;
                content.Questionnaire.Add(question);
            }

            foreach (Individu ind in destinataires)
            {
                if (ind != null)
                {
                    Notification_Simple notif = new Notification_Simple
                    {
                        Id_individu = ind.Id,
                        Individu    = ind,
                        Id_message  = content.Id,
                        Message     = content
                    };
                    ind.Notification_Simple.Add(notif);
                    content.Notification_Simple.Add(notif);
                }
            }
            foreach (Groupe gr in groupes)
            {
                if (gr != null)
                {
                    Notification_Diffusion not = new Notification_Diffusion
                    {
                        Id_groupe  = gr.Id,
                        Groupe     = gr,
                        Id_message = content.Id,
                        Message    = content,
                    };
                    gr.Notification_Diffusion.Add(not);
                    content.Notification_Diffusion.Add(not);
                }
            }

            db.Savedb();
            NotificationsHub.GetNotificationHtml(content, db.GetIdsDestinataires(mod));
            return(RedirectToAction("Index", "Home"));
        }