Click here to Skip to main content
15,885,925 members
Articles / Web Development / ASP.NET

YouGrade - Asp.NET MVC Multimedia Exam Suite

Rate me:
Please Sign up or sign in to vote.
4.93/5 (99 votes)
8 Jun 2011CPOL15 min read 232.4K   6.3K   192  
A multimedia exam suite built on Asp.NET and Youtube
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YouGrade.Model;
using YouGrade.Dtos;
using AutoMapper;
using System.Text.RegularExpressions;

namespace YouGrade
{
    public class ExamManager
    {
        static ExamManager instance = null;
        ExamDef examDef;
        ExamTakeDto examTakeDto;
        int questionId = 1;
        private ExamManager()
        {
            var service = new YouGradeService();
            examDef = service.GetExamDef();

            var examDefDto = new ExamDefDto();
            Mapper.Map(examDef, examDefDto);

            examTakeDto = new ExamTakeDto();
            examTakeDto.ExamId = 1;
            examTakeDto.UserId = 1;
            examTakeDto.Answers = new List<AnswerDto>();
            examTakeDto.StartDateTime = DateTime.Now;
            examTakeDto.Status = ExamTakeStatus.Open.ToString();
            foreach (var q in examDefDto.Questions)
            {
                foreach (var a in q.Alternatives)
                {
                    examTakeDto.Answers.Add(new AnswerDto()
                    {
                        QuestionId = q.Id,
                        AlternativeId = a.Id,
                        ExamTakeId = 0,
                        IsChecked = false
                    });
                }
            }
        }

        public static ExamManager Instance
        {
            get
            {
                if (instance == null)
                    instance = new ExamManager();
                return instance;
            }
        }

        public ExamDefDto GetExam()
        {
            var examDefDto = new ExamDefDto();
            Mapper.Map(examDef, examDefDto);

            return examDefDto;
        }

        public QuestionDefDto GetQuestion()
        {
            var questionDef = examDef.QuestionDef.Where(e => e.Id == questionId).First();
            var questionDefDto = new QuestionDefDto();
            Mapper.Map(questionDef, questionDefDto);
            var query = from alt in questionDefDto.Alternatives
                        join ans in examTakeDto.Answers
                            on new { qId = alt.QuestionId, aId = alt.Id }
                            equals new { qId = ans.QuestionId, aId = ans.AlternativeId }
                        select new { ans = ans, alt = alt };

            foreach (var a in query)
            {
                a.alt.IsChecked = a.ans.IsChecked;
            }

            //Here we create time links for the video, wherever the question text
            //matches the time regular expression
            var regexTime = new Regex(@"(\d|\d\d):(\d{2})");
            string newQuestionText = regexTime.Replace(questionDefDto.Text, 
                new MatchEvaluator(
                    (target) => 
                        { 
                            var timeSplit = target.ToString().Split(':');
                            return string.Format("<a href=\"#\" onclick=\"ytplayer.seekTo(60*{0}+{1});return false;\">{0}:{1}</a>", 
                                timeSplit[0], timeSplit[1]);
                        }
                    ));

            questionDefDto.Text = newQuestionText;

            return questionDefDto;
        }

        public void SaveAnswer(int questionId, string answers)
        {
            var question = examDef.QuestionDef.Where(e => e.Id == questionId).First();

            var query = from ans in examTakeDto.Answers
                        where ans.QuestionId == questionId
                        select ans;

            foreach (var a in query)
            {
                a.IsChecked = (answers.ToCharArray().Contains(a.AlternativeId[0]));
            }
        }

        public bool MoveToPreviousQuestion()
        {
            bool canMove = false;

            if (questionId > 1)
            {
                questionId--;
                canMove = true;
            }

            return canMove;
        }

        public bool MoveToNextQuestion()
        {
            bool canMove = false;

            if (examDef.QuestionDef.Count > questionId)
            {
                questionId++;
                canMove = true;
            }

            return canMove;
        }

        public int EndExam()
        {
            var result = 0d;
            var service = new YouGradeService();
            var examDefDto = new ExamDefDto();
            Mapper.Map(examDef, examDefDto);

            result = service.SaveExamTake(examTakeDto);

            return (int)((100 * result) / examDefDto.Questions.Count());
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions