Example #1
0
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int id = GetId();

            LoadQuiz(id);

            for (int i = 0; i < _quiz.Questions.Count; i++)
            {
                var question  = _quiz.Questions[i];
                var responses = new List <string>(Request.Form.GetValues("question" + i) ?? new string[] { });
                if (responses.Count > 0)
                {
                    for (int c = 0; c < question.Choices.Count; c++)
                    {
                        question.Choices[c].IsSelected = responses.Contains(question.Choices[c].Text);
                    }
                }
            }

            _results               = _quizService.ScoreQuiz(_quiz);
            pnlTakeQuiz.Visible    = false;
            pnlQuizResults.Visible = true;

            if (!_quizService.CanUserTakeQuiz(id, _quiz))
            {
                btnSubmitAgain.Visible = false;
            }

            var quizTakenActions = _results.Actions.Where(a => a.Condition == QuizCondition.QuizTaken);

            _quizService.LogQuiz(_quiz, ModuleId, id,
                                 quizTakenActions.FirstOrDefault() != null
                                     ? quizTakenActions.FirstOrDefault().Emails.FirstOrDefault().BodyTemplate
                                     : String.Empty);

            foreach (var a in quizTakenActions)
            {
                foreach (var email in a.Emails)
                {
                    EmailService.SendEmail(email.From, email.To, email.Cc, email.Bcc, email.SubjectTemplate, email.BodyTemplate);
                }
            }

            if (_results.IsPassingScore)
            {
                var quizPassedActions = _results.Actions.Where(a => a.Condition == QuizCondition.QuizPassed);

                if (UserId != -1)
                {
                    bool refreshUserRoles = false;
                    foreach (var action in quizPassedActions)
                    {
                        foreach (var roleToAdd in action.UserRoles)
                        {
                            RoleController roleController = new RoleController();
                            var            roleInfo       = roleController.GetRoleByName(PortalId, roleToAdd.RoleName);
                            if (roleInfo != null)
                            {
                                DateTime expireDate = roleToAdd.ExpiresAfterDays.HasValue
                                                          ? DateTime.Now.AddDays(roleToAdd.ExpiresAfterDays.Value)
                                                          : DotNetNuke.Common.Utilities.Null.NullDate;
                                roleController.AddUserRole(PortalId, UserId, roleInfo.RoleID, expireDate);
                                refreshUserRoles = true;
                            }
                            else
                            {
                                Exceptions.LogException(
                                    new ModuleLoadException("Tried to add user to non-existent role '" +
                                                            roleToAdd.RoleName + "'"));
                            }
                        }
                    }
                    if (refreshUserRoles)
                    {
                        // Clear the user's cached/stored role membership, will reload on next page cycle
                        PortalSecurity.ClearRoles();
                        DataCache.ClearUserCache(PortalId, UserInfo.Username);

                        // Load the current roles into the user's current context, for use in this page cycle
                        RoleController roleController = new RoleController();
                        UserInfo.Roles = roleController.GetRolesByUser(UserId, PortalId);
                    }
                }
            }
        }
Example #2
0
        private Dictionary <string, string> GetTokens(HttpRequest httpRequest, Model.Quiz quiz, Model.QuizResult results)
        {
            var tokens = new Dictionary <string, string>();

            tokens["QUIZ_NAME"]          = quiz.Name;
            tokens["QUIZ_PASSFAIL_TEXT"] = results.IsPassingScore ? "PASS" : "FAIL";
            tokens["QUIZ_SCORE"]         = results.PercentScore + "%";

            // capture fields
            var formFields = httpRequest.Form;

            foreach (string key in formFields.AllKeys)
            {
                if (key.StartsWith(CaptureFieldPrefix))
                {
                    string name = key.Replace(CaptureFieldPrefix, string.Empty).ToUpper();
                    tokens["CAPTURE_" + name] = formFields.GetValues(key).ToCsv();
                }
            }

            // results HTML
            StringBuilder html = new StringBuilder();

            html.Append("<ol>");
            foreach (var q in results.Quiz.Questions)
            {
                html.AppendFormat(@"<li>{0} <ol style=""list-style-type: lower-alpha;"">", q.Text);
                foreach (var c in q.Choices)
                {
                    html.AppendFormat("<li>{0}{1} {2}</li>", c.IsSelected ? "(selected)" : string.Empty, c.IsCorrectChoice ? "(correct)" : string.Empty, c.Text);
                }
                html.Append("</ol> </li>");
            }
            html.Append("</ol>");
            tokens["QUIZ_RESULTS_HTML"] = html.ToString();

            return(tokens);
        }