Esempio n. 1
0
        public static async Task <AnswerKeyVM> GetAnswerKeyVMAsync(string quizID)
        {
            AnswerKeyVM daBoss = new AnswerKeyVM();

            daBoss.RunningQuizID = quizID;

            var runningQuizJson = await MongoManager.GetOneQuizAsync(quizID, "runningQuizzes");

            var runningQuiz = JsonConvert.DeserializeObject <RunningQuiz>(runningQuizJson);

            var submittedAnswersJson = await MongoManager.GetAllAnswerFormsAsync(quizID, "answers");

            var submittedAnswers = JsonConvert.DeserializeObject <List <AnswerForm> >(submittedAnswersJson);

            // Stoppa in allt på rätt sätt i rätt AKVM

            var answerkeySongs = new List <AnswerKeySongVM>();

            int counter = 0;

            for (int i = 0; i < runningQuiz.Songs.Count; i++)
            {
                var questions = new List <AnswerKeyQuestionVM>();
                for (int j = 0; j < runningQuiz.Songs[i].Questions.Count; j++)
                {
                    var    answers = new List <AnswerKeySubmittedAnswerVM>();
                    string answer  = "";

                    for (int k = 0; k < submittedAnswers.Count; k++)
                    {
                        answer = submittedAnswers[k].Answers[counter];
                        answers.Add(new AnswerKeySubmittedAnswerVM {
                            Answer = answer, SubmitterName = submittedAnswers[k].SubmitterName, IsCorrect = (answer.ToLower() == (runningQuiz.Songs[i].Questions[j].CorrectAnswer).ToLower())
                        });
                    }
                    var correctAnswer = runningQuiz.Songs[i].Questions[j].CorrectAnswer;
                    var text          = runningQuiz.Songs[i].Questions[j].Text;
                    questions.Add(new AnswerKeyQuestionVM {
                        CorrectAnswer = correctAnswer, Text = text, SubmittedAnswers = new List <AnswerKeySubmittedAnswerVM>()
                    });
                    questions[j].SubmittedAnswers.AddRange(answers);
                    counter++;
                }
                var artist = runningQuiz.Songs[i].Artist;
                var title  = runningQuiz.Songs[i].Title;
                answerkeySongs.Add(new AnswerKeySongVM {
                    Artist = artist, Title = title, Questions = new List <AnswerKeyQuestionVM>()
                });
                answerkeySongs[i].Questions.AddRange(questions);
            }

            daBoss.Songs.AddRange(answerkeySongs);
            return(daBoss);
        }
Esempio n. 2
0
        public static async Task <bool> ExportPlaylistAsync(SpotifyTokens token, string quizId)
        {
            bool createResult = false;
            var  quizToExport = JsonConvert.DeserializeObject <Quiz>(await MongoManager.GetOneQuizAsync(quizId, "Quizzes"));
            var  playlistName = quizToExport.Name;

            // Check if it already exists, in that case warn user? Replace all songs?
            // The way this works now is that a new playlist is created with the same name if it already exists.
            //var listOfPlaylists = await SpotifyManager.GetAllUserPlaylists(token);
            //bool playlistExists = false;
            //foreach (var item in listOfPlaylists.Items)
            //{
            //    if (item.Name == playlistName)
            //    {
            //        playlistExists = true;
            //        break;
            //    }
            //}
            if (true /*!playlistExists*/)
            {
                // Create a new spotifyPlaylist with the chosen name if it does not exist.
                // In SpotifyManager.
                var newPlaylistId = await SpotifyManager.CreateNewPlaylistAsync(token, playlistName);

                // Add tracks to the newly created playlist.
                // In SpotifyManager.
                StringBuilder tracks = new StringBuilder();
                foreach (var song in quizToExport.Songs)
                {
                    tracks.Append("spotify:track:" + song.SpotifyReferenceID);
                    if (song != quizToExport.Songs[quizToExport.Songs.Count - 1])
                    {
                        tracks.Append(",");
                    }
                }
                var tracksAddResult = SpotifyManager.AddTracksToPlaylist(token, newPlaylistId, tracks.ToString());
                if (tracksAddResult == "Created")
                {
                    createResult = true;
                    await MongoManager.UpdateSpotifyPlaylistRefAsync(quizId, newPlaylistId);
                }
            }
            return(createResult);
        }
Esempio n. 3
0
        public static async Task <QuizMasterVM> StartQuiz(string _id)
        {
            await MongoManager.ClearOldAnswers(_id);

            int pin;
            // Hämta quiz från quizzes
            RunningQuiz runningQuiz;

            runningQuiz = JsonConvert.DeserializeObject <RunningQuiz>(await MongoManager.GetOneQuizAsync(_id, "Quizzes"));

            do
            {
                pin = randomizer.Next(1000, 9999);
            }while (await MongoManager.CheckIfPinExistsAsync(pin.ToString(), "runningQuizzes"));


            runningQuiz.Pin = pin.ToString();
            // Stoppa ner quizzet med PIN i runningQuizzes

            if (await MongoManager.CheckIfDocumentExistsAsync(runningQuiz._id, "runningQuizzes"))
            {
                await MongoManager.ReplaceOneQuizAsync(runningQuiz._id, JsonConvert.SerializeObject(runningQuiz), "runningQuizzes");
            }
            else
            {
                await MongoManager.SaveDocumentAsync("runningQuizzes", JsonConvert.SerializeObject(runningQuiz));
            }
            // Generera QuizMasterVM och returnera

            var           quizMasterVM = new QuizMasterVM(runningQuiz);
            StringBuilder songList     = new StringBuilder();

            foreach (var song in quizMasterVM.Songs)
            {
                songList.Append(song.SpotifyReferenceID);
                if (song != quizMasterVM.Songs[quizMasterVM.Songs.Count - 1])
                {
                    songList.Append(",");
                }
            }
            quizMasterVM.SongList = songList.ToString();

            return(quizMasterVM);
        }
Esempio n. 4
0
        public async Task <IActionResult> EditQuiz(string _id)
        {
            try
            {
                var quiz = await MongoManager.GetOneQuizAsync(_id, "Quizzes");

                await MongoManager.SaveQuizToSession(quiz, HttpContext.User.Identity.Name);

                return(RedirectToAction("EditQuiz"));
            }
            catch (MongoException mex)
            {
                HttpContext.Session.SetString("error", mex.Message);
                return(RedirectToAction("Error", "Home"));
            }
            catch (Exception ex)
            {
                HttpContext.Session.SetString("error", ex.Message);
                return(RedirectToAction("Error", "Home"));
            }
        }