public OperationResultDto IsEventValid(CompanyQuizEventMappingSaveDTO eventQuizMapping) { var defaultAcDomain = CompanyAcServerModel.GetOneById(eventQuizMapping.companyAcDomainId).Value; if (defaultAcDomain == null) { return(OperationResultDto.Error("Company was not found")); } var acUri = new Uri(defaultAcDomain.AcServer); var acProvider = new AdobeConnectProvider(new ConnectionDetails(acUri)); var acProxy = new AdobeConnectProxy(acProvider, Logger, acUri); var loginResult = acProxy.Login(new UserCredentials(defaultAcDomain.Username, defaultAcDomain.Password)); if (!loginResult.Success) { return(null); } var eventRegistrationDetails = acProxy.GetEventRegistrationDetails(eventQuizMapping.acEventScoId); string[] requiredFieldNames = new string[3] { "School", "State", "Trainee number or ID" }; StringBuilder errorMessage = new StringBuilder(); foreach (var fieldName in requiredFieldNames) { if (!eventRegistrationDetails.EventFields.Any(ef => string.Equals(ef.Description, fieldName, StringComparison.OrdinalIgnoreCase))) { errorMessage.Append($"{fieldName} field is missed.{Environment.NewLine}"); } } var eventInfo = acProxy.GetEventInfo(eventQuizMapping.acEventScoId); if (string.IsNullOrEmpty(eventInfo.EventInfo.Info)) { errorMessage.Append( $"Event info should be set for the event with sco-id {eventQuizMapping.acEventScoId}.{Environment.NewLine}"); } if (string.IsNullOrEmpty(eventInfo.EventInfo.SpeakerName)) { errorMessage.Append( $"Speaker name should be set for the event with sco-id {eventQuizMapping.acEventScoId}.{Environment.NewLine}"); } if (!string.IsNullOrEmpty(errorMessage.ToString())) { errorMessage.Append( $"Please fix the event details in Adobe Connect and try again.{Environment.NewLine}"); } return(string.IsNullOrEmpty(errorMessage.ToString()) ? OperationResultDto.Success() : OperationResultDto.Error(errorMessage.ToString())); }
// TRICK: used by OfflineQuizService.SendAnswers public OperationResultDto SendCertificate(string postQuizResultGuid) { Guid guid; var canParse = Guid.TryParse(postQuizResultGuid, out guid); if (!canParse) { var error = new Error(Errors.CODE_ERRORTYPE_GENERIC_ERROR, ErrorsTexts.GetResultError_NotFound, "No item with such id found"); this.LogError("EmailService.SendCertificate", error); throw new FaultException <Error>(error, error.errorMessage); } var postQuizResult = QuizResultModel.GetOneByGuid(guid).Value; var acDomain = CompanyAcServerModel.GetOneById(postQuizResult.EventQuizMapping.CompanyAcDomain.Id).Value; var acUrl = acDomain.AcServer; var apiUrl = new Uri(acUrl); var scoId = postQuizResult.EventQuizMapping.AcEventScoId; var proxy = new AdobeConnectProxy(new AdobeConnectProvider(new ConnectionDetails(apiUrl)), Logger, apiUrl); var eventInfo = proxy.GetEventInfo(scoId); if (!eventInfo.Success) { throw new InvalidOperationException(); } var emailsNotSend = new List <string>(); if (string.IsNullOrEmpty(postQuizResult.ACEmail)) { Logger.Warn($"[SendCertificate] Email is empty. quizResultId={postQuizResult.Id}"); emailsNotSend.Add(postQuizResult.ParticipantName); } var model = new CertificateEmailModel(Settings) { CertificateLink = postQuizResult.BuildDownloadUrl(Settings.CertificatesUrl), ParticipantName = postQuizResult.ParticipantName, EventName = eventInfo.EventInfo.Name }; bool sentSuccessfully = MailModel.SendEmailSync(postQuizResult.ParticipantName, postQuizResult.ACEmail, Emails.CertificateSubject, model, Common.AppEmailName, Common.AppEmail); if (!sentSuccessfully) { emailsNotSend.Add(postQuizResult.ACEmail); } return(emailsNotSend.Any() ? OperationResultDto.Error("Not all emails were sent correctly") : OperationResultDto.Success()); }
//public OperationResultDto SendCertificateToTeacher(string postQuizResultGuid) //{ // Guid guid; // var canParse = Guid.TryParse(postQuizResultGuid, out guid); // if (!canParse) // { // var error = new Error(Errors.CODE_ERRORTYPE_GENERIC_ERROR, ErrorsTexts.GetResultError_NotFound, "No item with such id found"); // this.LogError("EmailService.SendCertificateToTeacher", error); // throw new FaultException<Error>(error, error.errorMessage); // } // var postQuizResult = QuizResultModel.GetOneByGuid(guid).Value; // var emailsNotSend = new List<string>(); // if (string.IsNullOrEmpty(postQuizResult.ACEmail)) // { // Logger.Warn($"[SendCertificateToTeacher] Email is empty. quizResultId={postQuizResult.Id}"); // emailsNotSend.Add(postQuizResult.ParticipantName); // } // var eventMapping = CompanyEventQuizMappingModel.GetOneById(postQuizResult.EventQuizMapping.Id).Value; // var acUrl = eventMapping.CompanyAcDomain.AcServer; // var login = eventMapping.CompanyAcDomain.Username; // var password = eventMapping.CompanyAcDomain.Password; // var adobeConnectProxy = AdobeConnectAccountService.GetProvider(new AdobeConnectAccess(new Uri(acUrl), login, password), false); // var loginResult = adobeConnectProxy.Login(new UserCredentials(login, password)); // if (!loginResult.Success) // throw new InvalidOperationException("Can't login to AC"); // var dynamicQuestionAnswers = GetDynamicQuestionAnswers(acUrl, eventMapping.AcEventScoId, // loginResult.Status.SessionInfo, postQuizResult.ACEmail); // var schoolNumber = dynamicQuestionAnswers["school"]; // var school = SchoolModel.GetOneByName(schoolNumber); // var schoolEmail = school.SchoolEmail; // if (string.IsNullOrEmpty(schoolEmail)) // { // Logger.Warn($"[SendCertificateToTeacher] Teacher Email is empty. quizResultId={postQuizResult.Id}"); // emailsNotSend.Add(schoolEmail); // } // var schoolAccountName = school.AccountName; // var model = new TeacherCertificateEmailModel(Settings) // { // CertificateLink = // $"{Settings.CertificatesUrl}/quiz-certificate/{postQuizResult.Guid}/download", // ParticipantName = postQuizResult.ParticipantName, // TeacherName = schoolAccountName // }; // bool sentSuccessfully = MailModel.SendEmailSync(schoolAccountName, schoolEmail, // Emails.CertificateSubject, // model, Common.AppEmailName, Common.AppEmail); // if (!sentSuccessfully) // { // emailsNotSend.Add(schoolEmail); // } // return emailsNotSend.Any() ? OperationResultDto.Error("Not all emails were sent correctly") : OperationResultDto.Success(); //} //private static Dictionary<string, string> GetDynamicQuestionAnswers(string acUrl, string scoId, string breezeSession, string userEmail) //{ // var userStateSchoolAnswersUrl = // $"{acUrl}/api/xml?action=report-event-participants-complete-information&sco-id={scoId}&session={breezeSession}"; // var reply = string.Empty; // using (var client = new HttpClient()) // { // client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml")); // var request = new HttpRequestMessage(HttpMethod.Get, userStateSchoolAnswersUrl); // reply = client.SendAsync(request).Result.Content.ReadAsStringAsync().Result; // } // var doc = XDocument.Parse(reply); // var questions = doc.Root?.Descendants("registration_questions").Descendants("question").ToList(); // var stateNode = // questions?.FirstOrDefault( // x => // x.Attribute("description") // .Value.ToString() // .ToLower() // .Equals("state", StringComparison.OrdinalIgnoreCase)); // var schoolNode = // questions?.FirstOrDefault( // x => // x.Attribute("description") // .Value.ToString() // .ToLower() // .Equals("school", StringComparison.OrdinalIgnoreCase)); // var stateQuestionId = stateNode?.Attribute("id")?.Value.ToString() ?? string.Empty; // var schoolQuestionId = schoolNode?.Attribute("id")?.Value.ToString() ?? string.Empty; // var userAnswers = doc.Root?.Descendants("user_list").Descendants("user").ToList(); // var userAnswer = // userAnswers?.FirstOrDefault( // x => // x.Attribute("login") // .Value.ToString() // .ToLower() // .Equals(userEmail, StringComparison.OrdinalIgnoreCase)); // var stateAnswer = userAnswer?.Attribute("registration_question_" + stateQuestionId)?.Value ?? String.Empty; // var schoolAnswer = userAnswer?.Attribute("registration_question_" + schoolQuestionId)?.Value ?? String.Empty; // var result = new Dictionary<string, string>() { { "state", stateAnswer }, { "school", schoolAnswer } }; // return result; //} public OperationResultDto SendEventQuizResultEmail(int[] quizResultIds) { var quizResults = QuizResultModel.GetAllByIds(quizResultIds.ToList()); var mapping = quizResults.FirstOrDefault()?.EventQuizMapping; if (mapping == null) { return(OperationResultDto.Error("There is no event for this result.")); } // var mapping = CompanyEventQuizMappingModel.GetOneById(quizResults.First().EventQuizMappingId).Value; var acDomain = CompanyAcServerModel.GetOneById(mapping.CompanyAcDomain.Id).Value; var acUrl = acDomain.AcServer; var apiUrl = new Uri(acUrl); var scoId = mapping.AcEventScoId; var proxy = new AdobeConnectProxy(new AdobeConnectProvider(new ConnectionDetails(apiUrl)), Logger, apiUrl); var eventInfo = proxy.GetEventInfo(scoId); if (!eventInfo.Success) { throw new InvalidOperationException(); } var emailsNotSend = new List <string>(); foreach (var quizResult in quizResults) { if (quizResult.Quiz.Id != mapping.PreQuiz.Id) { throw new InvalidOperationException("Wrong quiz mapping"); } if (string.IsNullOrEmpty(quizResult.ACEmail) && string.IsNullOrEmpty(quizResult.Email)) { Logger.Warn($"[SendEventQuizResultEmail] Email is empty. quizResultId={quizResult.Id}"); emailsNotSend.Add(quizResult.ParticipantName); continue; } string email = null; try { //todo: create model based on success/fail var model = new EventQuizResultSuccessModel(Settings) { Name = quizResult.ParticipantName, EventName = eventInfo.EventInfo?.Name ?? scoId, MailSubject = "AC Event Post quiz result", PostQuizUrl = Settings.CertificatesUrl + "/UI/#/?quizResultGuid=" + quizResult.Guid, Date = quizResult.EndTime //PostQuizUrl = "https://app.edugamecloud.com" }; email = !string.IsNullOrEmpty(quizResult.ACEmail) ? quizResult.ACEmail : quizResult.Email; bool sentSuccessfully = MailModel.SendEmailSync(quizResult.ParticipantName, email, Emails.GoddardPostQuizSubject, model, Common.AppEmailName, Common.AppEmail); if (!sentSuccessfully) { emailsNotSend.Add(email); } } catch (Exception e) { Logger.Error($"[SendEventQuizResultEmail] error.", e); emailsNotSend.Add(email); } } return(emailsNotSend.Any() ? OperationResultDto.Error("Not all emails were sent correctly") : OperationResultDto.Success()); }