public ActionResult CadastrarEvento(int?idEvento) { PopularDropDownsCadastro(idEvento); Evento evento = new Evento(); if (idEvento.HasValue) { evento = eventoRepository.GetEventoById((int)idEvento); evento.SelectedCursos = evento.Curso.Select(x => x.IdCurso).ToList(); } //retorna model return(PartialView("_CadastroEvento", evento)); }
/// <summary> /// Gera certificado para todos os alunos que tiverem 100% de presença no evento /// </summary> /// <param name="idEvento">evento desejado</param> /// <returns></returns> public static bool GeraCertificado(int idEvento) { AlunoRepository ar = new AlunoRepository(); Evento ev = eventoRepository.GetEventoById(idEvento); bool flag = true; string templatePath = caminhoTemplates + "template-certificado.docx"; //caminhoDoTemplate List <Documento> certificadosDoc = new List <Documento>(); var template = new FileStream(templatePath, FileMode.Open, FileAccess.Read); List <Aluno> alunos = eventoRepository.GetAlunosPresentes(ev); //alunos com presença foreach (Aluno al in alunos) { string curso = al.AlunoCurso.Select(x => x.Curso.Nome).FirstOrDefault().ToString(); string novoArquivo = CriaDiretorio(new string[] { curso, al.IdAluno.ToString(), "Certificado" }); novoArquivo = novoArquivo + "(" + al.IdUsuario + ") " + ev.IdEvento + " - " + "temp" + ev.NomeEvento + ".docx"; Documento documento = new Documento(); TipoDocumento tipoDoc = tipoDocumentoRepository.GetTipoDoc("Certificado"); documento.IdTipoDoc = tipoDoc.IdTipoDoc; documento.NomeDocumento = "(" + al.IdUsuario + ") " + ev.IdEvento + " - " + "temp" + ev.NomeEvento + ".pdf"; documento.Data = DateTime.Now; documento.IdAlunoCurso = al.AlunoCurso.Select(x => x.IdAlunoCurso).FirstOrDefault(); documento.IdEvento = ev.IdEvento; if (File.Exists(Path.ChangeExtension(novoArquivo, ".pdf"))) { // certificadosDoc.Add(documento); } else { { #region substitui informaçoes seguindo a template DocX doc = DocX.Create(novoArquivo); doc.ApplyTemplate(template, true); if (doc.Text.Contains("<NOME>")) { doc.ReplaceText("<NOME>", al.Usuario.Nome); } if (doc.Text.Contains("<EVENTO>")) { doc.ReplaceText("<EVENTO>", ev.NomeEvento); } if (doc.Text.Contains("<HORAS>")) { doc.ReplaceText("<HORAS>", ev.CargaHoraria.ToString()); } if (doc.Text.Contains("<DIA>")) { doc.ReplaceText("<DIA>", DateTime.Now.Day.ToString()); } if (doc.Text.Contains("<MES>")) { string mes = DateTime.Now.ToString("MMMM", CultureInfo.CreateSpecificCulture("pt-BR")); doc.ReplaceText("<MES>", mes); } if (doc.Text.Contains("<ANO>")) { doc.ReplaceText("<ANO>", DateTime.Now.Year.ToString()); } doc.Save(); doc.Dispose(); Document pdf = new Document(); pdf.LoadFromFile(novoArquivo); File.Delete(novoArquivo); novoArquivo = Path.ChangeExtension(novoArquivo, ".pdf"); pdf.SaveToFile(novoArquivo, FileFormat.PDF); } #region criptografa certificado try { string caminhonovo = novoArquivo.Replace("temp", ""); FileStream fs = new FileStream(caminhonovo, FileMode.Create); byte[] arquivo = File.ReadAllBytes(novoArquivo); File.Delete(novoArquivo); RijndaelManaged rmCryp = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fs, rmCryp.CreateEncryptor(Key, Key), CryptoStreamMode.Write); foreach (var data in arquivo) { cs.WriteByte((byte)data); } cs.Close(); fs.Close(); documento.CaminhoDocumento = caminhonovo; certificadosDoc.Add(documento); ar.AdicionaHoras(ev.CargaHoraria, al.IdAluno, ev.IdEvento); } catch (Exception e) { flag = false; } #endregion #endregion } } if (!documentoRepository.PersisteCertificados(certificadosDoc)) { flag = false; } else { } return(flag); }