Exemple #1
0
        private static void ProcessQuiz(LmsQuizDTO quiz)
        {
            foreach (var quizQuestion in quiz.question_list)
            {
                if (quizQuestion.question_text == null)
                {
                    quizQuestion.question_text = quizQuestion.question_name;
                }

                if (quizQuestion.presentation != null)
                {
                    if (quizQuestion.presentation.IndexOf("|", StringComparison.Ordinal) > -1)
                    {
                        var presentationIndex = quizQuestion.presentation.IndexOf("|");
                        quizQuestion.question_text = string.Format(
                            "{0} ({1}-{2})",
                            quizQuestion.question_text,
                            quizQuestion.presentation.Substring(0, presentationIndex),
                            quizQuestion.presentation.Substring(presentationIndex + 1));
                    }

                    if (quizQuestion.presentation.IndexOf(">>>>>", StringComparison.Ordinal) > -1)
                    {
                        var    separatorIndex = quizQuestion.presentation.IndexOf(">>>>>", System.StringComparison.Ordinal);
                        string answers        = separatorIndex > 0
                                             ? quizQuestion.presentation.Substring(separatorIndex + 5)
                                             : quizQuestion.presentation,
                               type = separatorIndex > 0
                                          ? quizQuestion.presentation.Substring(0, separatorIndex)
                                          : string.Empty;
                        if (type.Equals("d"))
                        {
                            quizQuestion.question_type = "dummy_rate";
                        }

                        quizQuestion.is_single = !type.Equals("c");
                        quizQuestion.answers   =
                            answers.Split('|')
                            .Select(
                                (a, i) =>
                                new AnswerDTO()
                        {
                            text =
                                a.IndexOf("####", StringComparison.Ordinal) > -1
                                                    ? a.Substring(
                                    a.IndexOf("####", StringComparison.Ordinal)
                                    + 4)
                                                    : a,
                            id = i
                        })
                            .ToList();
                    }
                }
            }
        }
Exemple #2
0
        public async Task <OperationResultWithData <IEnumerable <LmsQuizDTO> > > GetItemsForUserAsync(Dictionary <string, object> licenseSettings, bool isSurvey, IEnumerable <int> quizIds)
        {
            var apiParam = new SakaiExtendedParams
            {
                LtiMessageType = "egc_get_assessment_data",
            };

            var courseName    = (string)licenseSettings[LmsUserSettingNames.CourseName];
            var testsUrl      = $"http://sakai11.esynctraining.com/egcint/service/?lti_message_type=egc_get_assessments&sourcedid={ courseName }&lti_version=LTI-1p0&oauth_consumer_key=esynctraining.com&secret=07951-BAUER-41481-CRLSHM&user_id={licenseSettings[LmsUserSettingNames.UserId]}&ext_sakai_provider_eid={ WebUtility.UrlEncode((string)licenseSettings[LmsUserSettingNames.Username]) }";
            var testsResponse = await _httpClient.GetAsync(testsUrl);

            string testsJson = await testsResponse.Content.ReadAsStringAsync();

            SakaiSiteDto[] tests = null;
            if (!testsJson.StartsWith("Error", StringComparison.InvariantCultureIgnoreCase))
            {
                tests = JsonConvert.DeserializeObject <SakaiSiteDto[]>(testsJson);
            }

            if (tests == null)
            {
                throw new InvalidOperationException("Sakai api getting tests method failed!");
            }

            var quizDto = new List <LmsQuizDTO>();

            if (quizIds != null)
            {
                tests = tests.Where(x => quizIds.Contains(x.publishedAssessmentId)).ToArray();
            }
            foreach (var test in tests)
            {
                //var json = JsonConvert.SerializeObject(apiParam);
                var assessmentId = test.publishedAssessmentId;
                var quizzesIds   = new List <int>();
                quizzesIds.Add(assessmentId);
                var url =
                    $"http://sakai11.esynctraining.com/egcint/service/?lti_message_type=egc_get_assessment_data&sourcedid={ courseName }&assessmentId={ assessmentId }&lti_version=LTI-1p0&oauth_consumer_key=esynctraining.com&secret=07951-BAUER-41481-CRLSHM&user_id={licenseSettings[LmsUserSettingNames.UserId]}&ext_sakai_provider_eid={ WebUtility.UrlEncode((string)licenseSettings[LmsUserSettingNames.Username]) }";
                var response = await _httpClient.GetAsync(url);

                string resp = await response.Content.ReadAsStringAsync();

                var lqd = new LmsQuizDTO()
                {
                    course     = (string)licenseSettings[LmsUserSettingNames.CourseId],
                    courseName = courseName,
                    //description = test.name,
                    title     = test.name,
                    id        = test.publishedAssessmentId,
                    published = true
                };
                //if (quizIds != null && !quizIds.Contains(lqd.id))
                //{
                //    continue;
                //}
                //testData = egc.getAssessmentDetails(t.id, isSurvey);

                if (!resp.StartsWith("Error", StringComparison.InvariantCultureIgnoreCase))
                {
                    var td = JsonConvert.DeserializeObject <BBAssessmentDTO>(resp);
                    lqd.question_list = SakaiQuizParser.ParseQuestions(td, resp);
                    var repoImages = td.images as JToken;
                    if (repoImages != null)
                    {
                        var temp = repoImages.ToObject <Dictionary <string, string> >();
                        lqd.Images = temp.ToDictionary(x => x.Key, x => x.Value);
                    }
                }

                quizDto.Add(lqd);
            }

            return(((IEnumerable <LmsQuizDTO>)quizDto).ToSuccessResult()); //todo: handle error cases
        }
Exemple #3
0
        public async Task <OperationResultWithData <IEnumerable <LmsQuizDTO> > > GetItemsForUserAsync(Dictionary <string, object> licenseSettings, bool isSurvey, IEnumerable <int> quizIds)
        {
            WebserviceWrapper client = null;
            string            error;
            var tests = this.LoginIfNecessary(
                ref client,
                c =>
            {
                var egc     = c.getEdugameCloudWrapper();
                var content = c.getContentWrapper();

                var tos = content.getTOCsByCourseId((string)licenseSettings[LmsUserSettingNames.CourseId]);

                if (tos != null)
                {
                    tos =
                        tos.Where(
                            to =>
                            to.label != null &&
                            to.label.Equals("content", StringComparison.InvariantCultureIgnoreCase))
                        .ToArray();
                }

                ContentVO[] tsts = null;

                if (tos != null && tos.Any())
                {
                    var contentFilter = new ContentFilter()
                    {
                        filterType          = 3,
                        filterTypeSpecified = true,
                        contentId           = tos.First().courseContentsId
                    };

                    var loaded = content.loadFilteredContent((string)licenseSettings[LmsUserSettingNames.CourseId], contentFilter);
                    if (loaded != null)
                    {
                        tsts =
                            loaded.Where(
                                l =>
                                l.contentHandler != null &&
                                l.contentHandler.Equals(isSurvey ? "resource/x-bb-asmt-survey-link" : "resource/x-bb-asmt-test-link")).ToArray();
                    }
                }


                string testData = string.Empty;

                if (tsts != null)
                {
                    var quizDTO = new List <LmsQuizDTO>();

                    foreach (var t in tsts)
                    {
                        var lqd = new LmsQuizDTO()
                        {
                            course      = (string)licenseSettings[LmsUserSettingNames.CourseId],
                            courseName  = (string)licenseSettings[LmsUserSettingNames.CourseId],
                            description = t.body.ClearName(),
                            title       = t.title.ClearName(),
                            id          = BlackboardHelper.GetBBId(t.id),
                            published   = true
                        };
                        if (quizIds != null && !quizIds.Contains(lqd.id))
                        {
                            continue;
                        }
                        testData = egc.getAssessmentDetails(t.id, isSurvey);

                        if (testData != null && !testData.StartsWith("Error", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var td            = JsonConvert.DeserializeObject <BBAssessmentDTO>(testData);
                            lqd.question_list = BlackboardQuizParser.ParseQuestions(td, testData);
                            var repoImages    = td.images as JToken;
                            if (repoImages != null)
                            {
                                var temp   = repoImages.ToObject <Dictionary <string, string> >();
                                lqd.Images = temp.ToDictionary(x => x.Key, x => x.Value);
                            }
                        }
                        quizDTO.Add(lqd);
                    }

                    return(quizDTO.ToList());
                }

                return(new List <LmsQuizDTO> {
                });
            },
                licenseSettings,
                out error);

            return(string.IsNullOrEmpty(error)
                ? ((IEnumerable <LmsQuizDTO>)tests).ToSuccessResult()
                : OperationResultWithData <IEnumerable <LmsQuizDTO> > .Error(error));
        }
Exemple #4
0
        /// <summary>
        /// Parses the specified XML.
        /// </summary>
        /// <param name="xml">
        /// The XML.
        /// </param>
        /// <param name="errorMessage">
        /// The error message
        /// </param>
        /// <param name="error">
        /// The error text
        /// </param>
        /// <returns>
        /// Collection of Meeting Items.
        /// </returns>
        public static LmsQuizDTO Parse(XmlNode xml, ref string errorMessage, ref string error)
        {
            if (xml == null)
            {
                return(null);
            }

            XmlNode message = xml.SelectSingleNode(MessagePath);

            if (message != null)
            {
                errorMessage = message.InnerText;
            }

            XmlNode debugInfo = xml.SelectSingleNode(DebugPath);

            if (debugInfo != null)
            {
                error = debugInfo.InnerText;
            }

            XmlNode quiz = xml.SelectSingleNode(QuizPath);

            if (quiz == null)
            {
                return(null);
            }

            var ret = new LmsQuizDTO();

            ret.id          = int.Parse(quiz.GetNodeValue("id") ?? "0");
            ret.title       = quiz.GetNodeValue("name");
            ret.description = quiz.GetNodeValue("intro");

            XmlNode courseSingle = quiz.SelectSingleNode(CoursePath);

            if (courseSingle != null)
            {
                int courseIdInt;
                if (int.TryParse(courseSingle.GetNodeValue("id"), out courseIdInt))
                {
                    ret.course = courseIdInt.ToString();
                }

                ret.courseName = courseSingle.GetNodeValue("fullname");
            }

            XmlNodeList questions = quiz.SelectNodes(QuestionsPath);

            var quizQuestions = new List <LmsQuestionDTO>();

            if (questions != null)
            {
                foreach (XmlNode quest in questions)
                {
                    var q = new LmsQuestionDTO();
                    quizQuestions.Add(q);

                    q.id            = int.Parse(quest.GetNodeValue("id") ?? "0");
                    q.question_name = quest.GetNodeValue("name");
                    q.question_text = quest.GetNodeValue("questiontext");
                    q.question_type = quest.GetNodeValue("qtype") ?? quest.GetNodeValue("typ");
                    q.presentation  = quest.GetNodeValue("presentation");
                    q.is_mandatory  = int.Parse(quest.GetNodeValue("required") ?? "0") > 0;

                    XmlNodeList datasetsSingle = quest.SelectNodes(DataSetPath);
                    if (datasetsSingle != null)
                    {
                        foreach (XmlNode ds in datasetsSingle)
                        {
                            //var mds = new SakaiDataset();
                            //mds.Id = ds.GetNodeValue("id");
                            //mds.Name = ds.GetNodeValue("name");
                            //mds.Max = ds.GetNodeValue("maximum");
                            //mds.Min = ds.GetNodeValue("minimum");

                            //q.datasets.Add(mds);

                            //XmlNodeList items = ds.SelectNodes("KEY[@name='items']/MULTIPLE/SINGLE");
                            //if (items == null)
                            //{
                            //    continue;
                            //}

                            //mds.Items = new List<SakaiDataSetItem>();
                            //foreach (XmlNode it in items)
                            //{
                            //    var mit = new SakaiDataSetItem();
                            //    mit.ItemNumber = it.GetNodeValue("itemnumber");
                            //    string val = it.GetNodeValue("value");
                            //    double value;
                            //    if (val != null && double.TryParse(val, out value))
                            //    {
                            //        mit.Value = value;
                            //    }

                            //    mds.Items.Add(mit);
                            //}
                        }
                    }

                    XmlNode optionsSingle = quest.SelectSingleNode(OptionSinglePath);
                    if (optionsSingle == null)
                    {
                        continue;
                    }

                    string singleChoice = optionsSingle.GetNodeValue("single");
                    int    singleChoiceInt;
                    if (int.TryParse(singleChoice, out singleChoiceInt))
                    {
                        q.is_single = singleChoiceInt > 0;
                    }

                    XmlNodeList answers = optionsSingle.SelectNodes(AnswersPath);

                    if (answers != null)
                    {
                        foreach (XmlNode answ in answers)
                        {
                            var a = new AnswerDTO();
                            q.answers.Add(a);
                            a.id     = int.Parse(answ.GetNodeValue("id") ?? "0");
                            a.text   = answ.GetNodeValue("answer");
                            a.weight = (int)(double.Parse(answ.GetNodeValue("fraction") ?? "0") * 100);

                            double tolerance;
                            string tol = answ.GetNodeValue("tolerance");
                            if (tol != null && double.TryParse(tol, out tolerance))
                            {
                                a.margin = tolerance;
                            }
                        }
                    }

                    XmlNodeList qzs = optionsSingle.SelectNodes(QuestionsPath);

                    if (qzs != null && qzs.Count == 0)
                    {
                        qzs = optionsSingle.SelectNodes(SubquestionsPath);
                    }

                    if (qzs != null)
                    {
                        foreach (XmlNode quizNode in qzs)
                        {
                            var a = new AnswerDTO();
                            q.answers.Add(a);
                            a.id            = int.Parse(quizNode.GetNodeValue("id") ?? "0");
                            a.question_type = quizNode.GetNodeValue("questiontype");
                            a.question_text = quizNode.GetNodeValue("questiontext");
                            a.text          = quizNode.GetNodeValue("answertext");
                        }
                    }
                }
            }

            ret.question_list = quizQuestions.ToArray();

            ProcessQuiz(ret);

            return(ret);
        }