private HashSet <string> AllowedStudents(string email) { var allowedStudents = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase); foreach (var cls in ClassesCtrl.Get()) { if (cls.IsATeacher(email)) { allowedStudents.UnionWith(cls.Students); } } return(allowedStudents); }
public ActionResult Classes() { if (NeedLogin()) { return(LoginRedir()); } var classes = new List <Class>(); //Only classes we're allowed to see bool admin = IsAdmin(); string email = CurrentUserEmail(); foreach (Class cls in ClassesCtrl.Get()) { if (admin || cls.IsATeacher(email)) { classes.Add(cls); } } return(View("Classes", classes)); }
public ActionResult ClassDetails(string id) { if (NeedLogin()) { return(LoginRedir()); } var clazz = ClassesCtrl.Get(id); if (clazz == null) { return(new HttpNotFoundResult()); } if (!IsAdmin()) { if (!clazz.IsATeacher(CurrentUserEmail())) { //Don't have rights to this class return(RedirectToAction("Classes")); } } //Don't allow null lists if (clazz.Lessons == null) { clazz.Lessons = new List <string>(); } if (clazz.Students == null) { clazz.Students = new List <string>(); } //SPECIAL: if this is a memphis class with test in the name, we do some filtering if (clazz.Location.ToLower().Contains("memphis") && clazz.ClassID.ToLower().Contains("test")) { //No carl, test, or non-alpha students then... //Sort by len and take top 10 var nonAlpha = new System.Text.RegularExpressions.Regex("[^A-Z,a-z]+"); clazz.Students = clazz.Students .Where(s => !(s.Contains("carl") || s.Contains("test") || nonAlpha.IsMatch(s))) .OrderByDescending(s => s.Length) .Take(10) .ToList(); //only lessons that match lessonN where N is a number var lessonMatch = new System.Text.RegularExpressions.Regex("lesson[0-9]+"); clazz.Lessons = clazz.Lessons.Where(l => lessonMatch.IsMatch(l)).ToList(); } //Sort info in the class for display purposes //Students are easy to sort, but we need a special sort for lessons clazz.Lessons = clazz.Lessons.OrderBy(x => Utils.LessonIDSort(x)).ToList(); clazz.Students.Sort(); var lessons = new HashSet <String>(clazz.Lessons); var students = new HashSet <String>(clazz.Students); //Make dictionary of lesson:user var lookup = new Dictionary <Tuple <string, string>, StudentLessonActs>(); if (lessons.Count > 0 && students.Count > 0) { foreach (var turns in LessonsCtrl.DBConn().FindTurnsForStudents(students)) { if (!students.Contains(turns.UserID) || !lessons.Contains(turns.LessonID)) { continue; //Nope } var key = new Tuple <string, string>(turns.LessonID, turns.UserID); lookup[key] = turns; } } //Calculate user and lessons average: first get totals and then //calculate the averages. This seemingly strange method means we //only need to perform lesson/student nested loop once var lessonTots = new Dictionary <string, Tuple <int, int> >(); var userTots = new Dictionary <string, Tuple <int, int> >(); //Per-init student dict (since students are the inner loop) foreach (string userID in clazz.Students) { userTots[userID] = new Tuple <int, int>(0, 0); } //Find totals foreach (string lessonID in clazz.Lessons) { lessonTots[lessonID] = new Tuple <int, int>(0, 0); foreach (string userID in clazz.Students) { var key = new Tuple <string, string>(lessonID, userID); StudentLessonActs turns; if (lookup.TryGetValue(key, out turns)) { int ca = turns.CorrectAnswers; int ia = turns.IncorrectAnswers; if (ca + ia > 0) { lessonTots[lessonID] = TupleAdd(lessonTots[lessonID], ca, ia); userTots[userID] = TupleAdd(userTots[userID], ca, ia); } } } } //Set up model with all this data var modelObj = new ExpandoObject(); var modelDict = (IDictionary <string, object>)modelObj; modelDict["Class"] = clazz; modelDict["LUTurns"] = lookup; modelDict["LessonNames"] = LessonsCtrl.DBConn().FindLessonNames(); modelDict["LessonCounts"] = lessonTots; modelDict["StudentCounts"] = userTots; return(View("ClassDetail", modelObj)); }