Example #1
0
        }         // Quiz_OnLoad

        /// <summary>
        /// This event is called when a quiz is completed.
        /// It updates the QuizSession table in Salt with information
        /// provided by the Toolbook application. This information is supplied
        /// via form elements that are posted to this page.
        /// </summary>
        /// <param name="sessionID">This is the session id that maps to the lesson that is currently loading</param>
        /// <param name="postData">This is the collection of http post data variables</param>
        private void Quiz_OnScore(string sessionID, NameValueCollection postData)
        {
            string strDuration;         // This isnt currently implemented in toolbook
            string strResponses;        // The responses given by the user
            string strScore;            // The score of the user
            int    intScore;            // The score of the user answering the quiz
            int    intDuration;         // The uration the user spent doing the quiz

            // Verify the necessary post parameters have been supplied
            strResponses = postData.Get("Responses");
            if (strResponses.Length == 0)
            {
                OutputToToolBook(
                    cm_strReturnCodeCriticalError                                                   // paramater 1 - ReturnCode
                    + cm_strDelimiter + "TBListener Error 1. Missing required parameter: Responses" // paramater 2 - Error Message
                    );
                return;
            }

            // Get the duration
            strDuration = postData.Get("Duration");
            if (strDuration.Length == 0)
            {
                OutputToToolBook(
                    cm_strReturnCodeCriticalError                                                  // paramater 1 - ReturnCode
                    + cm_strDelimiter + "TBListener Error 1. Missing required parameter: Duration" // paramater 2 - Error Message
                    );
                return;
            }

            // Get the score
            strScore = postData.Get("Score");
            if (strDuration.Length == 0)
            {
                OutputToToolBook(
                    cm_strReturnCodeCriticalError                                                  // paramater 1 - ReturnCode
                    + cm_strDelimiter + "TBListener Error 1. Missing required parameter: Duration" // paramater 2 - Error Message
                    );
                return;
            }

            // Check that Duration and Score contain numeric values
            if (IsInteger(strDuration) && IsInteger(strScore))
            {
                intDuration = Convert.ToInt32(strDuration);
                intScore    = Convert.ToInt32(strScore);
            }
            else
            {
                OutputToToolBook(
                    cm_strReturnCodeCriticalError                                                       // paramater 1 - ReturnCode
                    + cm_strDelimiter + "TBListener Error 1. Invalid parameter type: Duration or Score" // paramater 2 - Error Message
                    );
                return;
            }

            try
            {
                // All Answers To All Questions
                string[] aAnswersAllQuestions;

                // Answers to one particular question
                string[] aAnswersOneQuestions;

                // QuizQuestionID
                string strQuestionToolbookPageID;

                int intFirstMarker;
                int intSecondMarker;
                BusinessServices.Toolbook objToolboook = new Toolbook();

                // Remove one of the ['s
                strResponses = strResponses.Replace("[", "");

                // Use the other ] as a delimiter to split the string to an array.
                strResponses = strResponses.Substring(0, strResponses.Length - 1);

                aAnswersAllQuestions = strResponses.Split(']');
                foreach (string strAnswer in aAnswersAllQuestions)
                {
                    // Find first opening square bracket
                    intFirstMarker = strAnswer.IndexOf("{");

                    // Find the matching closing bracket.
                    intSecondMarker = strAnswer.IndexOf("}");

                    // This turns the string "Q1{1,2,3}" into an array containing '1' '2' and '3'
                    aAnswersOneQuestions = strAnswer.Substring(intFirstMarker + 1, intSecondMarker - intFirstMarker - 1).Split(',');

                    // Get the Question ID from the start of the string, ignore the Q at the start.
                    strQuestionToolbookPageID = strAnswer.Substring(0, intFirstMarker);
                    objToolboook.CreateQuizQuestionAudit(sessionID, strQuestionToolbookPageID);

                    // Iterate through each Answer for this question and add it to the salt database.
                    foreach (string strQuizAnswer in aAnswersOneQuestions)
                    {
                        // Only add the answer if there is a value provided
                        if (strQuizAnswer.Length > 0)
                        {
                            objToolboook.CreateQuizAnswerAudit(sessionID, strQuestionToolbookPageID, Convert.ToInt32(strQuizAnswer));
                        }
                    }
                }

                // Record that the Quiz has finished
                try
                {
                    DataTable endQuizInfo;
                    int       intUserID;
                    int       intQuizID;
                    int       intPassMark;
                    int       intUnitID;
                    int       intModuleID;
                    int       intQuizFrequency;
                    int       intOldCourseStatus;
                    int       intNewCourseStatus;
                    int       intNewQuizStatus;
                    int       intCourseID;
                    DateTime  dtmQuizCompletionDate;

                    //objToolboook.EndQuizSession(sessionID, intDuration, intScore);
                    endQuizInfo = objToolboook.BeforeQuizEnd(sessionID, intDuration, intScore);

                    DataRow tmpRow = endQuizInfo.Rows[0];
                    intUserID             = Int32.Parse(tmpRow["UserID"].ToString());
                    intQuizID             = Int32.Parse(tmpRow["QuizID"].ToString());
                    intPassMark           = Int32.Parse(tmpRow["PassMark"].ToString());
                    intUnitID             = Int32.Parse(tmpRow["UnitID"].ToString());
                    intModuleID           = Int32.Parse(tmpRow["ModuleID"].ToString());
                    intQuizFrequency      = Int32.Parse(tmpRow["QuizFrequency"].ToString());
                    intOldCourseStatus    = Int32.Parse(tmpRow["OldCourseStatus"].ToString());
                    intNewCourseStatus    = Int32.Parse(tmpRow["NewCourseStatus"].ToString());
                    intNewQuizStatus      = Int32.Parse(tmpRow["NewQuizStatus"].ToString());
                    intCourseID           = Int32.Parse(tmpRow["CourseID"].ToString());
                    dtmQuizCompletionDate = tmpRow["QuizCompletionDate"] == null?DateTime.Parse("1/1/1900") : (DateTime)tmpRow["QuizCompletionDate"];


                    objToolboook.EndQuizSession_UpdateTables(sessionID, intDuration, intScore, intUserID, intQuizID, intPassMark, intUnitID, intModuleID, intCourseID, intOldCourseStatus, intNewQuizStatus, intNewCourseStatus, intQuizFrequency, dtmQuizCompletionDate);
                }
                catch (Exception ex)
                {
                    ErrorHandler.ErrorLog Error = new ErrorHandler.ErrorLog(ex, ErrorLevel.Medium, "ToolBookListener.aspx.cs", "Quiz_OnScore", "Failed in objToolboook.EndQuizSession");
                    throw (ex);
                }

                // Everything has been saved to Salt successfully.
                // Send OK Return code to Toolbook to indicate completion
                OutputToToolBook(
                    cm_strReturnCodeOK                                          // paramater 1 - ReturnCode
                    + cm_strDelimiter + ""                                      // paramater 2 - Error Message
                    );
            }
            catch (Exception ex)
            {
                ErrorHandler.ErrorLog objError = new ErrorHandler.ErrorLog(ex, ErrorLevel.Medium, "ToolBookListener.aspx.cs", "Quiz_OnScore", "");
            }
        }         // Quiz_OnScore