private static ChoiceDetails[] GetChoiceDetails(IReadOnlyList <PromptChoiceDetails> promptChoiceDetails)
        {
            var choices = new ChoiceDetails[promptChoiceDetails.Count];

            for (int i = 0; i < promptChoiceDetails.Count; i++)
            {
                choices[i] = new ChoiceDetails
                {
                    Label       = promptChoiceDetails[i].Label,
                    HelpMessage = promptChoiceDetails[i].HelpMessage,
                    // There were intended to enable hotkey use for choice selections,
                    // but currently VSCode does not do anything with them.
                    // They can be exposed once VSCode supports them.
                    HotKeyIndex     = -1,
                    HotKeyCharacter = null,
                };
            }
            return(choices);
        }
        /// <summary>
        /// Prompts the user to make a choice using the provided details.
        /// </summary>
        /// <param name="promptCaption">
        /// The caption string which will be displayed to the user.
        /// </param>
        /// <param name="promptMessage">
        /// The descriptive message which will be displayed to the user.
        /// </param>
        /// <param name="choices">
        /// The list of choices from which the user will select.
        /// </param>
        /// <param name="defaultChoice">
        /// The default choice to highlight for the user.
        /// </param>
        /// <param name="cancellationToken">
        /// A CancellationToken that can be used to cancel the prompt.
        /// </param>
        /// <returns>
        /// A Task instance that can be monitored for completion to get
        /// the user's choice.
        /// </returns>
        public Task<int> PromptForChoice(
            string promptCaption,
            string promptMessage,
            ChoiceDetails[] choices,
            int defaultChoice,
            CancellationToken cancellationToken)
        {
            // TODO: Guard against multiple calls

            this.Caption = promptCaption;
            this.Message = promptMessage;
            this.Choices = choices;
            this.DefaultChoice = defaultChoice;
            this.promptTask = new TaskCompletionSource<int>();

            // Cancel the TaskCompletionSource if the caller cancels the task
            cancellationToken.Register(this.CancelPrompt, true);

            // Show the prompt to the user
            this.ShowPrompt(PromptStyle.Full);

            return this.promptTask.Task;
        }
Exemple #3
0
        public IActionResult GetQuizByQuizID([FromBody] int quizID)
        {
            string connectionString = Configuration["ConnectionStrings:DefaultConnectionString"];

            //Create list of all quizzes
            var quiz = new QuizDetails();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                //Create the SQL command and set type to stored procedure.
                SqlCommand command = new SqlCommand("Quiz_GetByQuizID", connection);
                command.CommandType = System.Data.CommandType.StoredProcedure;

                //Set the parameters for the command.
                command.Parameters.AddWithValue("@quizID", quizID);

                connection.Open();

                //Execute the query and store the result.
                //Get all quizzes that belong to a group.
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            quiz = new QuizDetails
                            {
                                GroupID  = reader.GetInt32(reader.GetOrdinal("GroupID")),
                                QuizID   = reader.GetInt32(reader.GetOrdinal("QuizID")),
                                QuizName = reader.GetString(reader.GetOrdinal("QuizName"))
                            };
                        }
                        reader.Close();
                    }
                    else
                    {
                        return(BadRequest("Could not find matching quiz."));
                    }
                }

                //Create list of all questions
                var questions = new List <QuestionDetails>();

                command             = new SqlCommand("Questions_GetByQuizID", connection);
                command.CommandType = System.Data.CommandType.StoredProcedure;

                //Get all questions for each quiz.

                //Set the parameters for the command.
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@quizID", quiz.QuizID);

                //Execute the query and store the result
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            var question = new QuestionDetails
                            {
                                QuestionID   = reader.GetInt32(reader.GetOrdinal("QuestionID")),
                                QuizID       = reader.GetInt32(reader.GetOrdinal("QuizID")),
                                QuestionText = reader.GetString(reader.GetOrdinal("QuestionText"))
                            };

                            questions.Add(question);
                        }
                        reader.Close();
                    }
                    else
                    {
                        return(BadRequest("Could not find matching questions."));
                    }
                }

                //Create list of all choices
                var choices = new List <ChoiceDetails>();

                command             = new SqlCommand("Choices_GetByQuizID", connection);
                command.CommandType = System.Data.CommandType.StoredProcedure;

                //Set the parameters for the command.
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@quizID", quiz.QuizID);

                //Execute the query and store the result
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            var choice = new ChoiceDetails
                            {
                                ChoiceID   = reader.GetInt32(reader.GetOrdinal("ChoiceID")),
                                QuizID     = reader.GetInt32(reader.GetOrdinal("QuizID")),
                                QuestionID = reader.GetInt32(reader.GetOrdinal("QuestionID")),
                                ChoiceText = reader.GetString(reader.GetOrdinal("ChoiceText")),
                                isCorrect  = reader.GetBoolean(reader.GetOrdinal("IsCorrect"))
                            };

                            choices.Add(choice);
                        }
                        reader.Close();
                    }
                    else
                    {
                        return(BadRequest("Could not find matching choices."));
                    }
                }

                //Add the choices to the matching question item.
                foreach (QuestionDetails question in questions)
                {
                    var tempChoices = new List <ChoiceDetails>();

                    foreach (ChoiceDetails choice in choices)
                    {
                        if (choice.QuestionID == question.QuestionID)
                        {
                            tempChoices.Add(choice);
                        }
                    }
                    question.Choices = tempChoices.ToArray();
                }

                quiz.Questions = questions.ToArray();

                connection.Close();
            }

            //Return OK result with quizzes
            return(Ok(
                       quiz
                       ));
        }
Exemple #4
0
        public IActionResult GetCompletedQuizzesForTrainee([FromBody] TraineeGetQuizzes userAndGroupIDDTO)
        {
            var    queryResult      = -1; //Set query result to fail.
            string connectionString = Configuration["ConnectionStrings:DefaultConnectionString"];
            int    result           = -1;

            //Create list of all quizzes
            var quizzes          = new List <QuizDetails>();
            var completedQuizzes = new List <QuizDetails>();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                //Create the SQL command and set type to stored procedure.
                SqlCommand command = new SqlCommand("Quizzes_GetByGroupID", connection);
                command.CommandType = System.Data.CommandType.StoredProcedure;

                //Set the parameters for the command.
                command.Parameters.AddWithValue("@groupID", userAndGroupIDDTO.GroupID);

                connection.Open();

                //Execute the query and store the result.
                //Get all quizzes that belong to a group.
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            var quiz = new QuizDetails
                            {
                                GroupID  = reader.GetInt32(reader.GetOrdinal("GroupID")),
                                QuizID   = reader.GetInt32(reader.GetOrdinal("QuizID")),
                                QuizName = reader.GetString(reader.GetOrdinal("QuizName"))
                            };

                            quizzes.Add(quiz);
                        }
                        reader.Close();
                    }
                    else
                    {
                        return(BadRequest("Could not find matching quiz."));
                    }
                }

                //Create list of all questions
                var questions = new List <QuestionDetails>();

                command             = new SqlCommand("Questions_GetByQuizID", connection);
                command.CommandType = System.Data.CommandType.StoredProcedure;

                //Get all questions for each quiz.
                foreach (QuizDetails quiz in quizzes)
                {
                    //Set the parameters for the command.
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("@quizID", quiz.QuizID);

                    //Execute the query and store the result
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                var question = new QuestionDetails
                                {
                                    QuestionID   = reader.GetInt32(reader.GetOrdinal("QuestionID")),
                                    QuizID       = reader.GetInt32(reader.GetOrdinal("QuizID")),
                                    QuestionText = reader.GetString(reader.GetOrdinal("QuestionText"))
                                };

                                questions.Add(question);
                            }
                            reader.Close();
                        }
                        else
                        {
                            return(BadRequest("Could not find matching questions."));
                        }
                    }
                }

                //Create list of all choices
                var choices = new List <ChoiceDetails>();

                command             = new SqlCommand("Choices_GetByQuizID", connection);
                command.CommandType = System.Data.CommandType.StoredProcedure;

                //Get all choices for each quiz.
                foreach (QuizDetails quiz in quizzes)
                {
                    //Set the parameters for the command.
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("@quizID", quiz.QuizID);

                    //Execute the query and store the result
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                var choice = new ChoiceDetails
                                {
                                    ChoiceID   = reader.GetInt32(reader.GetOrdinal("ChoiceID")),
                                    QuizID     = reader.GetInt32(reader.GetOrdinal("QuizID")),
                                    QuestionID = reader.GetInt32(reader.GetOrdinal("QuestionID")),
                                    ChoiceText = reader.GetString(reader.GetOrdinal("ChoiceText")),
                                    isCorrect  = reader.GetBoolean(reader.GetOrdinal("IsCorrect"))
                                };

                                choices.Add(choice);
                            }
                            reader.Close();
                        }
                        else
                        {
                            return(BadRequest("Could not find matching choices."));
                        }
                    }
                }

                //Add the choices to the matching question item.
                foreach (QuestionDetails question in questions)
                {
                    var temp = new List <ChoiceDetails>();

                    foreach (ChoiceDetails choice in choices)
                    {
                        if (choice.QuestionID == question.QuestionID)
                        {
                            temp.Add(choice);
                        }
                    }
                    question.Choices = temp.ToArray();
                }

                //Add the questions to the matching quiz item.
                foreach (QuizDetails quiz in quizzes)
                {
                    var temp = new List <QuestionDetails>();

                    foreach (QuestionDetails question in questions)
                    {
                        if (question.QuizID == quiz.QuizID)
                        {
                            temp.Add(question);
                        }
                    }
                    quiz.Questions = temp.ToArray();
                }

                //Create the SQL command and set type to stored procedure.
                command             = new SqlCommand("Results_CheckIfQuizCompleted", connection);
                command.CommandType = System.Data.CommandType.StoredProcedure;

                foreach (QuizDetails quiz in quizzes)
                {
                    //Set the parameters for the command.
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("@quizID", quiz.QuizID);
                    command.Parameters.AddWithValue("@userID", userAndGroupIDDTO.UserID);
                    command.Parameters.Add("@result", System.Data.SqlDbType.Int).Direction = System.Data.ParameterDirection.Output;

                    //Execute the query and store the result.
                    queryResult = command.ExecuteNonQuery();

                    //Get the id for the quiz previously created.
                    result = (int)command.Parameters["@result"].Value;

                    if (result == 1)
                    {
                        completedQuizzes.Add(quiz);
                    }
                }

                connection.Close();
            }

            //Return OK result with uncompleted quizzes
            return(Ok(
                       completedQuizzes
                       ));
        }
        /// <summary>
        /// Prompts the user to make a choice using the provided details.
        /// </summary>
        /// <param name="promptCaption">
        /// The caption string which will be displayed to the user.
        /// </param>
        /// <param name="promptMessage">
        /// The descriptive message which will be displayed to the user.
        /// </param>
        /// <param name="choices">
        /// The list of choices from which the user will select.
        /// </param>
        /// <param name="defaultChoice">
        /// The default choice to highlight for the user.
        /// </param>
        /// <returns>
        /// A Task instance that can be monitored for completion to get
        /// the user's choice.
        /// </returns>
        public Task<int> PromptForChoice(
            string promptCaption,
            string promptMessage,
            ChoiceDetails[] choices,
            int defaultChoice)
        {
            // TODO: Guard against multiple calls

            this.Caption = promptCaption;
            this.Message = promptMessage;
            this.Choices = choices;
            this.DefaultChoice = defaultChoice;
            this.promptTask = new TaskCompletionSource<int>();

            // Show the prompt to the user
            this.ShowPrompt(PromptStyle.Full);

            return this.promptTask.Task;
        }