Ejemplo n.º 1
0
        public void LogQuiz(Model.Quiz quiz, int moduleId, int userId, string results)
        {
            //---- Log the quiz

            string sql = @"INSERT INTO DNNspot_Quiz_QuizLog(ModuleId,QuizName,UserId,results) VALUES(@moduleId,@quizName,@userId,@results)";
            List <SqlParameter> sqlParams = new List <SqlParameter>()
            {
                new SqlParameter("moduleId", moduleId),
                new SqlParameter("quizName", quiz.Name),
                new SqlParameter("userId", userId),
                new SqlParameter("results", results)
            };

            _dataProvider.ExecuteSQL(sql, sqlParams.ToArray());
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public bool CanUserTakeQuiz(int userId, Model.Quiz quiz)
        {
            if (quiz.LimitPerUser == 0 || userId == -1)
            {
                return(true);
            }

            string sql = @"SELECT COUNT(*) FROM DNNspot_Quiz_QuizLog WHERE UserId = @userId AND QuizName = @quizName";
            List <SqlParameter> sqlParams = new List <SqlParameter>()
            {
                new SqlParameter("quizName", quiz.Name),
                new SqlParameter("userId", userId)
            };
            int?count  = null;
            var reader = _dataProvider.ExecuteSQL(sql, sqlParams.ToArray());

            while (reader.Read())
            {
                count = reader.GetInt32(0);
            }

            return(count < quiz.LimitPerUser);
        }
Ejemplo n.º 4
0
        public static Quiz LoadFromXml(string xmlFile)
        {
            if (File.Exists(xmlFile))
            {
                var quiz = new Quiz();

                var xml = XElement.Load(xmlFile);
                quiz.Name           = xml.Attribute("name").Value;
                quiz.PassPercentage = WA.Parser.ToInt(xml.Attribute("passPercentage").Value).GetValueOrDefault();
                quiz.DisplayScore   = WA.Parser.ToBool(xml.GetAttributeValue("displayScore")).GetValueOrDefault(true);
                quiz.DisplayHints   = WA.Parser.ToBool(xml.GetAttributeValue("displayHints")).GetValueOrDefault(true);
                quiz.LimitPerUser   = WA.Parser.ToInt(xml.GetAttributeValue("limitPerUser")).GetValueOrDefault(0);

                xml.Element("capturefields")
                .Elements("field").ToList().ForEach(f =>
                                                    quiz.CaptureFields.Add(new CaptureField()
                {
                    Type        = WA.Enum <CaptureFieldType> .TryParseOrDefault(f.GetAttributeValue("type"), CaptureFieldType.Text),
                    Name        = f.Element("name").Value,
                    Placeholder = f.GetElementValue("placeholder"),
                    IsRequired  = WA.Parser.ToBool(f.GetElementValue("required")).GetValueOrDefault(false)
                }));
                xml.Element("questions")
                .Elements("question").ToList().ForEach(q =>
                                                       quiz.Questions.Add(new Question()
                {
                    Text       = q.Element("text").Value,
                    IsRequired = WA.Parser.ToBool(q.Element("required") == null ? "" : q.Element("required").Value).GetValueOrDefault(false),
                    Choices    = q.Element("choices").Elements("choice")
                                 .Select(c => new Choice()
                    {
                        Text            = c.Value,
                        IsCorrectChoice = WA.Parser.ToBool(c.GetAttributeValue("correct")).GetValueOrDefault(false)
                    }).ToList(),
                    Messages = q.Element("messages").Elements("message")
                               .Select(r => new Message()
                    {
                        Type = WA.Enum <MessageType> .TryParseOrDefault(r.GetAttributeValue("type"), MessageType.Incorrect),
                        Text = r.Value
                    }).ToList()
                }));

                xml.Element("actions")
                .Elements("action").ToList().ForEach(a =>
                                                     quiz.Actions.Add(new QuizAction()
                {
                    Condition = WA.Enum <QuizCondition> .TryParseOrDefault(a.GetAttributeValue("condition"), QuizCondition.Any),
                    Message   = a.GetElementValue("message"),
                    UserRoles = a.Element("roles") == null ? new List <RoleInfo>() : a.Element("roles")
                                .Elements("role").Select(r => new RoleInfo()
                    {
                        RoleName         = r.Value,
                        ExpiresAfterDays = WA.Parser.ToInt(r.GetAttributeValue("expiresAfterDays"))
                    }).ToList(),
                    Emails = a.Element("emails") == null ? new List <ActionEmail>() : a.Element("emails")
                             .Elements("email").Select(e => new ActionEmail()
                    {
                        From            = e.GetElementValue("from"),
                        To              = e.GetElementValue("to"),
                        Cc              = e.GetElementValue("cc"),
                        Bcc             = e.GetElementValue("bcc"),
                        SubjectTemplate = e.GetElementValue("subject"),
                        BodyTemplate    = e.GetElementValue("body"),
                    }).ToList()
                }));
                //xml.Element("settings")
                //    .Elements("setting").ToList().ForEach(s =>
                //        quiz.Settings.Add(new Setting()
                //                              {
                //                                  Name = s.GetElementValue("name"),
                //                                  Value = s.GetElementValue("value"),
                //                              })
                //    );

                return(quiz);
            }
            return(null);
        }