Click here to Skip to main content
15,885,856 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.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using YouGrade.Silverlight;
using model = YouGrade.Silverlight.Core.Model;
using service = YouGrade.Silverlight.Web.Model;
using System.Data;
using YouGrade.Silverlight.Web.Model;
using TOModel = YouGrade.Silverlight.Core.Model;
using YouGrade.Silverlight.Core.Model;
using YouGrade.Model;

namespace YouGrade.Silverlight.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class YouGradeService
    {
        static ExamDef examDef = new ExamDef();
        static User user;

        public YouGradeService()
        {
        }

        [OperationContract]
        public User GetUser(string login, string password)
        {
            using (YouGradeEntities1 db = new YouGradeEntities1())
            {
                var query = db.User
                    .Where(e => e.Login.Equals(login, StringComparison.InvariantCultureIgnoreCase))
                    .Where(e => e.Password.Equals(password, StringComparison.InvariantCultureIgnoreCase)
                    );

                if (query.Any())
                {
                    return query.AsQueryable<User>().First();
                }
                else
                {
                    return null;
                }
            }
        }

        [OperationContract]
        public ExamDef GetExamDef()
        {
            using (YouGradeEntities1 ctx = new YouGradeEntities1())
            {
                return ctx.ExamDef.Include("QuestionDef.Alternative").First();
            }
        }

        [OperationContract]
        public IQueryable<Alternative> GetAlternatives(int questionId)
        {
            using (YouGradeEntities1 ctx = new YouGradeEntities1())
            {
                return ctx.Alternative.Where(e => e.QuestionDef.Id == questionId);
            }
        }

        [OperationContract]
        public double SaveExamTake(ExamTakeTO examTakeTO)
        {
            double grade = 0;
            try
            {
                using (YouGradeEntities1 ctx = new YouGradeEntities1())
                {
                    User user = ctx.User.Where(e => e.Id == examTakeTO.UserId).First();
                    ExamDef examDef = ctx.ExamDef.Where(e => e.Id == examTakeTO.ExamId).First();

                    service.ExamTake newExamTake = service.ExamTake.CreateExamTake
                        (
                            0,
                            examTakeTO.StartDateTime,
                            examTakeTO.Duration,
                            examTakeTO.Grade,
                            examTakeTO.Status.ToString()
                        );

                    newExamTake.User = user;
                    newExamTake.ExamDef = examDef;

                    ctx.AddToExamTake(newExamTake);

                    ctx.SaveChanges();

                    foreach (AnswerTO a in examTakeTO.Answers)
                    {
                        ExamTake examTake = ctx.ExamTake.Where(e => e.Id == newExamTake.Id).First();
                        Alternative alternative = ctx.Alternative.Where(e => e.QuestionId == a.QuestionId).Where(e => e.Id == a.AlternativeId).First();
                        Answer newAnswer = Answer.CreateAnswer(newExamTake.Id, a.QuestionId, a.AlternativeId, a.IsChecked);
                        newAnswer.ExamTake = examTake;
                        newAnswer.Alternative = alternative;
                        ctx.AddToAnswer(newAnswer);
                    }

                    ctx.SaveChanges();

                    foreach (QuestionDef q in ctx.QuestionDef)
                    {
                        var query = from qd in ctx.QuestionDef
                                    join a in ctx.Answer on qd.Id equals a.QuestionId
                                    join alt in ctx.Alternative on new { qId = a.QuestionId, aId = a.AlternativeId } equals new { qId = alt.QuestionId, aId = alt.Id }
                                    where qd.Id == q.Id
                                    where a.ExamTakeId == newExamTake.Id
                                    select new { alt.Correct, a.IsChecked };

                        bool correct = true;
                        foreach (var v in query)
                        {
                            if (v.Correct != v.IsChecked)
                            {
                                correct = false;
                                break;
                            }
                        }
                        grade += correct ? 1 : 0;
                    }

                    int examTakeId = examTakeTO.Id;
                }

                using (YouGradeEntities1 ctx = new YouGradeEntities1())
                {
                    ExamTake et = ctx.ExamTake.First();
                    string s = et.Status;
                }
                
                return grade;
            }
            catch (Exception exc)
            {
                string s = exc.ToString();
                throw;
            }
        }
    }
}

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