Пример #1
0
 public ActionResult Edit(int classId, int problemSetId = 0)
 {
     try
     {
         if ((int) Session["ClassId"] != classId || (UserType) Session["UserType"] != UserType.Instructor)
         {
             return RedirectToAction("Unauthorized", "Error");
         }
         ProblemSetViewModel model = new ProblemSetViewModel();
         if (problemSetId == 0) //Create a new set
         {
             model.Set = new ProblemSetData();
             model.Set.Class = new ClassData((int) Session["ClassId"]);
         }
         else if (problemSetId == -1) //View the unassigned set
         {
             ProblemSetData unassigned = new ProblemSetData(-1);
             unassigned.Name = "Unassigned Problems";
             unassigned.Class = new ClassData((int) Session["ClassId"]);
             model.Set = unassigned;
             model.Problems = GlobalStaticVars.StaticCore.GetProblemsForSet(model.Set);
         }
         else //Edit a set normally
         {
             model.Set = GlobalStaticVars.StaticCore.GetSetById(problemSetId);
             model.Prereqs = GlobalStaticVars.StaticCore.GetSetPrereqs(problemSetId);
             model.Problems = GlobalStaticVars.StaticCore.GetProblemsForSet(model.Set);
         }
         return View(model);
     }
     catch (Exception e)
     {
         return RedirectToAction("ServerError", "Error");
     }
 }
Пример #2
0
        /// <summary>
        /// Calculates aggregated progress data for all sets in the specified class.
        /// If a non-null user is passed in, then the data is limited to that student's progress.
        /// If a non-null set is passed in, then the data is limited to problems in that set.
        /// </summary>
        /// <param name="cls">The ClassData object with the class's id</param>
        /// <param name="user">The UserData object with the user's id (optional)</param>
        /// <param name="set">The ProblemSetData object with the set's id (optional)</param>
        /// <returns>A non-null, possibly empty list of ProblemProgress objects</returns>
        public List<ProblemProgress> GetProblemProgress(ClassData cls, UserData user = null, ProblemSetData set = null)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                List<ProblemProgress> list = new List<ProblemProgress>();
                SqlCommand cmd = conn.CreateCommand();

                string selectCols = "probs.Id, probs.Name";

                StringBuilder query = new StringBuilder();
                query.AppendLine("Select " + selectCols + ", ");
                query.AppendLine("  Count(Case probs.IsCorrect When 1 Then 1 Else null End) as NumCorrect, ");
                query.AppendLine("  Avg(Cast(probs.NumAttempts as float)) as AvgAttempts ");
                query.AppendLine("from ( ");
                query.AppendLine("  Select distinct p.*, s.IsCorrect, IsNull(s.NumAttempts, 0) as NumAttempts ");
                query.AppendLine("  from dbo.[Problem] p ");
                query.AppendLine("  Left Join dbo.[Solution] s on s.ProblemId = p.Id ");
                query.AppendLine("  Join dbo.[ProblemSetProblem] psp on psp.ProblemId = p.Id ");
                query.AppendLine("  Where p.ClassId = @clsId ");
                if (user != null)
                {
                    query.AppendLine("  and (s.UserId = @userId or s.UserId is null) ");
                    cmd.Parameters.AddWithValue("@userId", user.Id);
                }
                if (set != null)
                {
                    query.AppendLine("  and psp.ProblemSetId = @setId ");
                    cmd.Parameters.AddWithValue("@setId", set.Id);
                }
                query.AppendLine(") probs ");
                query.AppendLine("Group by " + selectCols);

                //Class
                cmd.Parameters.AddWithValue("@clsId", cls.Id);

                cmd.CommandText = query.ToString();

                SqlDataReader reader = null;
                try
                {
                    conn.Open();
                    reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                        while (reader.Read())
                            list.Add(createProblemProgressFromReader(reader));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }

                return list;
            }
        }
Пример #3
0
 public ActionResult AddPrereq(ProblemSetData model)
 {
     try
     {
         return PartialView("EditorTemplates/PrereqRow", model);
     }
     catch (Exception e)
     {
         return RedirectToAction("ServerError", "Error");
     }
 }
Пример #4
0
        public void TestAdd()
        {
            ProblemSetData set = new ProblemSetData(0);
            set.Name = "Beginner Problems";
            set.Class = new ClassData(1);

            //This test works as of 5:15pm on 2/22
            //Commenting it out to prevent cluttering the database
            //- Josh
            //Assert.IsTrue(setModel.Add(set));
        }
Пример #5
0
 public ActionResult AddProblemSet(ProblemSetData model)
 {
     try
     {
         //Return the partial view to for a problem set table row
         return PartialView("EditorTemplates/ProblemSetRow", model);
     }
     catch (Exception e)
     {
         return RedirectToAction("ServerError", "Error");
     }
 }
Пример #6
0
        public ActionResult Home(int id)
        {
            try
            {
                ClassData cls = GlobalStaticVars.StaticCore.GetClassById(id);
                //If the current user is the instructor for the class...
                if (WebSecurity.CurrentUserId == cls.Instructor.Id)
                {
                    Session["ClassId"] = id;
                    Session["UserType"] = UserType.Instructor;

                    ClassViewModel model = new ClassViewModel();
                    model.Class = cls;
                    model.Sets = GlobalStaticVars.StaticCore.GetSetsForClass(id);

                    //Add in the unassigned set that captures any problems not assigned to any sets
                    ProblemSetData unassigned = new ProblemSetData(-1);
                    unassigned.Name = "Unassigned Problems";
                    unassigned.Class = new ClassData(id);
                    model.Sets.Add(unassigned);

                    return View("InstructorClassHome", model);
                }
                //If the current user is a student in the class...
                else if (GlobalStaticVars.StaticCore.IsStudent(WebSecurity.CurrentUserId, id))
                {
                    Session["ClassId"] = id;
                    Session["UserType"] = UserType.Student;

                    Tuple<List<ProblemSetData>, List<ProblemSetData>, List<ProblemSetData>> sets =
                        GlobalStaticVars.StaticCore.GetSetsForStudent(WebSecurity.CurrentUserId, id);
                    ViewBag.UnlockedSets = sets.Item1;
                    ViewBag.LockedSets = sets.Item2;
                    ViewBag.SolvedSets = sets.Item3;
                    return View("StudentClassHome", cls);
                }

                Session.Clear();
                return RedirectToAction("Home");
            }
            catch (Exception e)
            {
                return RedirectToAction("ServerError", "Error");
            }
        }
Пример #7
0
        public void TestPrereqs()
        {
            ProblemSetData parent = new ProblemSetData(3);
            List<ProblemSetData> original = setModel.GetPrereqs(parent);

            //Test remove prereqs
            Assert.IsTrue(setModel.UpdatePrereqs(parent, new List<ProblemSetData>()));
            Assert.AreEqual(0, setModel.GetPrereqs(parent).Count);

            //Test add prereqs
            Assert.IsTrue(setModel.UpdatePrereqs(parent, original));
            CollectionAssert.AreEqual(original, setModel.GetPrereqs(parent));

            //Test add bad data
            List<ProblemSetData> badList = new List<ProblemSetData>(original);
            badList.Add(new ProblemSetData { Id = original[0].Id, PrereqCount = original[0].PrereqCount }); //Can't have duplicate prereq entries
            badList.Add(new ProblemSetData { Id = 3, PrereqCount = 1 }); //Set can't require itself
            Assert.IsTrue(setModel.UpdatePrereqs(parent, badList));
            CollectionAssert.AreEqual(original, setModel.GetPrereqs(parent));

            //Test update prereqs
            foreach (ProblemSetData p in original)
                p.PrereqCount += 1;
            Assert.IsTrue(setModel.UpdatePrereqs(parent, original));
            foreach (ProblemSetData p in setModel.GetPrereqs(parent))
            {
                ProblemSetData op = original.Find(s => s.Id == p.Id);
                Assert.AreEqual(op.PrereqCount, p.PrereqCount);
            }

            foreach (ProblemSetData p in original)
                p.PrereqCount -= 1;
            Assert.IsTrue(setModel.UpdatePrereqs(parent, original));
            foreach (ProblemSetData p in setModel.GetPrereqs(parent))
            {
                ProblemSetData op = original.Find(s => s.Id == p.Id);
                Assert.AreEqual(op.PrereqCount, p.PrereqCount);
            }
        }
Пример #8
0
        public string ExportFromClass(int classID)
        {
            try
            {
                // First, construct our ClassExportObject to be exported
                var exportData = new PortableClassData();

                // Then, get the problem sets and prereqs for the class
                var psets = ModelFactory.ProblemSetModel.GetForClass(new ClassData(classID));
                foreach (var pset in psets)
                {
                    var prereqList = ModelFactory.ProblemSetModel.GetPrereqs(pset);
                    exportData.ProblemSets[pset] = prereqList;
                }

                // Then, get each problem for each problem set and add them to the export list (without repetition!)
                foreach (var pset in exportData.ProblemSets.Keys)
                {
                    var problems = ModelFactory.ProblemModel.GetForSet(pset);
                    foreach (var problem in problems)
                    {
                        if (!exportData.Problems.Keys.Contains(problem))
                        {
                            var list = new List<ProblemSetData>();
                            list.Add(pset);
                            exportData.Problems.Add(problem, list);
                        }
                        else
                        {
                            exportData.Problems[problem].Add(pset);
                        }
                    }
                }

                // Retrieve unassigned problems
                var unassignedPset = new ProblemSetData(-1);
                unassignedPset.Class = ModelFactory.ClassModel.GetById(classID);
                var unassignedProblems = ModelFactory.ProblemModel.GetForSet(unassignedPset);
                foreach (var problem in unassignedProblems)
                {
                    if (!exportData.Problems.Keys.Contains(problem))
                    {
                        var list = new List<ProblemSetData>();
                        list.Add(unassignedPset);
                        exportData.Problems.Add(problem, list);
                    }
                    else
                    {
                        exportData.Problems[problem].Add(unassignedPset);
                    }
                }

                // Serialize and export
                var x = new DataContractSerializer(exportData.GetType());
                using (var stream = new MemoryStream())
                {
                    x.WriteObject(stream, exportData);
                    stream.Seek(0, SeekOrigin.Begin);
                    var reader = new StreamReader(stream);
                    return reader.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                // Shit's jank
                return "There was an error in the export process.";
            }
        }
Пример #9
0
 public int AddSet(ProblemSetData set)
 {
     return setModel.Add(set);
 }
Пример #10
0
 public bool ModifySet(ProblemSetData set)
 {
     return setModel.Modify(set);
 }
Пример #11
0
 public List<ProblemData> GetProblemsForSet(ProblemSetData set)
 {
     return problemModel.GetForSet(set);
 }
Пример #12
0
        /// <summary>
        /// Calculates aggregated progress data for all students in the specified class.
        /// If a non-null set is passed in, then the data is limited to problems in that set.
        /// </summary>
        /// <param name="cls">The ClassData object with the class's id</param>
        /// <param name="set">The ProblemSetData object with the set's id (optional)</param>
        /// <returns>A non-null, possibly empty list of StudentProgress objects</returns>
        public List<StudentProgress> GetStudentProgress(ClassData cls, ProblemSetData set = null)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                List<StudentProgress> list = new List<StudentProgress>();
                SqlCommand cmd = conn.CreateCommand();

                string selectCols = "u.Id, u.Email, u.FirstName, u.LastName";

                StringBuilder query = new StringBuilder();
                query.AppendLine("Select " + selectCols + ", ");
                query.AppendLine("  Count(Case s.IsCorrect When 1 Then 1 Else null End) as NumCorrect, ");
                query.AppendLine("  Avg(Cast(s.NumAttempts as float)) as AvgAttempts ");
                query.AppendLine("from dbo.[User] u ");
                query.AppendLine("Join dbo.[Problem] p on p.ClassId = @clsId ");
                query.AppendLine("Join dbo.[Solution] s on s.UserId = u.Id and s.ProblemId = p.Id ");
                if (set != null)
                {
                    query.AppendLine("Where p.Id in ( ");
                    query.AppendLine("  Select ProblemId from dbo.[ProblemSetProblem] ");
                    query.AppendLine("  Where ProblemSetId = @setId ");
                    query.AppendLine(") ");
                    cmd.Parameters.AddWithValue("@setId", set.Id);
                }
                query.AppendLine("Group by " + selectCols);

                //Class
                cmd.Parameters.AddWithValue("@clsId", cls.Id);

                cmd.CommandText = query.ToString();

                SqlDataReader reader = null;
                try
                {
                    conn.Open();
                    reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                        while (reader.Read())
                            list.Add(createStudentProgressFromReader(reader));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }

                return list;
            }
        }
Пример #13
0
 /// <summary>
 /// Calculates aggregated progress data for all sets in the specified class.
 /// If a non-null user is passed in, then the data is limited to that student's progress.
 /// If a non-null set is passed in, then the data is limited to problems in that set.
 /// </summary>
 /// <param name="cls">The ClassData object with the class's id</param>
 /// <param name="user">The UserData object with the user's id (optional)</param>
 /// <param name="set">The ProblemSetData object with the set's id (optional)</param>
 /// <returns>A non-null, possibly empty list of ProblemProgress objects</returns>
 public List<ProblemProgress> GetProblemProgress(ClassData cls, UserData user = null, ProblemSetData set = null)
 {
     return progressDao.GetProblemProgress(cls, user, set);
 }
Пример #14
0
 /// <summary>
 /// Calculates aggregated progress data for all students in the specified class.
 /// If a non-null set is passed in, then the data is limited to problems in that set.
 /// </summary>
 /// <param name="cls">The ClassData object with the class's id</param>
 /// <param name="set">The ProblemSetData object with the set's id (optional)</param>
 /// <returns>A non-null, possibly empty list of StudentProgress objects</returns>
 public List<StudentProgress> GetStudentProgress(ClassData cls, ProblemSetData set = null)
 {
     return progressDao.GetStudentProgress(cls, set);
 }