public object GeraCertificados(int idEvento) { bool flag = DirDoc.GeraCertificado(idEvento); if (flag) { try { var evento = eventoRepository.GetEventoById(idEvento); var url = System.Web.Hosting.HostingEnvironment.MapPath("~/Views/Email/CertificadoGerado.cshtml"); string viewCode = System.IO.File.ReadAllText(url); var eventoEmail = eventoRepository.ConverToEmailModel(evento, Url.Action("Login", "Account", null, Request.Url.Scheme)); var html = RazorEngine.Razor.Parse(viewCode, eventoEmail); List <Aluno> alunosPresentes = eventoRepository.GetAlunosPresentes(evento); eventoRepository.AlteraStatusEvento(idEvento, EnumStatusEvento.certificados); try { if (alunosPresentes != null && alunosPresentes.Count > 0 && alunosPresentes.Any(x => x.Usuario.E_mail != "")) { var to = alunosPresentes.Where(x => !string.IsNullOrEmpty(x.Usuario.E_mail)).Select(x => x.Usuario.E_mail).ToArray(); var from = System.Configuration.ConfigurationManager.AppSettings["MailFrom"].ToString(); Email.EnviarEmail(from, to, string.Format("Certificado do evento {0} gerado", evento.NomeEvento), html); } } catch (Exception ex) { } return(Json(new { Status = true, Type = "success", Message = "Certificados gerados com sucesso!", ReturnUrl = Url.Action("Index") }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { Status = false, Type = "error", Message = "Houve um erro, tente novamente mais tarde!" }, JsonRequestBehavior.AllowGet)); } } return(Json(new { Status = false, Type = "error", Message = "Houve um erro, tente novamente mais tarde!" }, JsonRequestBehavior.AllowGet)); }
/// <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); }