Example #1
0
        public static void editLecturer(int lecturerId, IEnumerable <string> selected_modules, string fname, string lname, string email)
        {
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                var lecturer = db.Lecturers.Where(l => l.lecturerId == lecturerId).FirstOrDefault();
                lecturer.User.email = email;
                lecturer.fName      = fname;
                lecturer.lName      = lname;

                var modules = db.Teachings.Where(t => t.lecturerId == lecturerId);
                db.Teachings.RemoveRange(modules);

                foreach (var module in selected_modules)
                {
                    Teaching teaching = new Teaching
                    {
                        lecturerId = lecturer.lecturerId,
                        moduleId   = Int32.Parse(module)
                    };
                    db.Teachings.Add(teaching);
                }

                db.SaveChanges();
            }
        }
Example #2
0
        public ActionResult Edit(ModuleModel updatedModule)
        {
            try
            {
                if (this.ModelState.IsValid)
                {
                    using (xinyuedbEntities db = new xinyuedbEntities())
                    {
                        var module = db.Modules.SingleOrDefault(x => x.moduleId == updatedModule.moduleId);
                        if (module != null)
                        {
                            module.name     = updatedModule.name;
                            module.degree   = updatedModule.degree;
                            module.year     = updatedModule.year;
                            module.semester = updatedModule.semester;

                            db.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return(this.RedirectToAction("Index"));
        }
Example #3
0
        public static void addLecturer(IEnumerable <string> selected_modules, string fname, string lname, string email, string password)
        {
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                User user = new User
                {
                    email       = email,
                    accountType = "lecturer",
                    password    = PasswordHash.HashPassword(password)
                };
                db.Users.Add(user);

                Lecturer lecturer = new Lecturer
                {
                    fName  = fname,
                    lName  = lname,
                    userId = user.userId,
                };
                db.Lecturers.Add(lecturer);

                foreach (var module in selected_modules)
                {
                    Teaching teaching = new Teaching
                    {
                        lecturerId = lecturer.lecturerId,
                        moduleId   = Int32.Parse(module)
                    };
                    db.Teachings.Add(teaching);
                }

                db.SaveChanges();
            }
        }
Example #4
0
        public ActionResult Delete(int id)
        {
            try
            {
                using (xinyuedbEntities db = new xinyuedbEntities())
                {
                    var module = db.Modules.SingleOrDefault(x => x.moduleId == id);
                    if (module != null)
                    {
                        var classes = db.Classes.Where(x => x.moduleId == id).ToList();
                        if (classes.Any())
                        {
                            foreach (var cla in classes)
                            {
                                var allocations = db.Allocations.Where(x => x.classId == cla.classId).ToList();
                                db.Allocations.RemoveRange(allocations);
                            }
                            db.Classes.RemoveRange(classes);
                        }

                        var teachings = db.Teachings.Where(x => x.moduleId == id).ToList();
                        db.Teachings.RemoveRange(teachings);

                        db.Modules.Remove(module);
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return(this.RedirectToAction("Index"));
        }
Example #5
0
        public ActionResult Add(ModuleModel module)
        {
            try
            {
                if (this.ModelState.IsValid)
                {
                    using (xinyuedbEntities db = new xinyuedbEntities())
                    {
                        db.Modules.Add(new Module
                        {
                            name     = module.name,
                            degree   = module.degree,
                            year     = module.year,
                            semester = module.semester
                        });

                        db.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return(this.RedirectToAction("Index"));
        }
Example #6
0
 public void changePublishState()
 {
     using (xinyuedbEntities db = new xinyuedbEntities())
     {
         var config = db.Configs.Where(c => c.name.Equals("published")).FirstOrDefault();
         config.value = config.value != 0 ? 0 : 1;
         db.SaveChanges();
     }
 }
Example #7
0
 public static void resetPassword(int lecturerId, string password)
 {
     using (xinyuedbEntities db = new xinyuedbEntities())
     {
         var lecturer = db.Lecturers.Where(l => l.lecturerId == lecturerId).FirstOrDefault();
         lecturer.User.password = PasswordHash.HashPassword(password);
         db.SaveChanges();
     }
 }
Example #8
0
        public void deleteLecturer(int lecturerId)
        {
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                db.Teachings.RemoveRange(db.Teachings.Where(t => t.lecturerId == lecturerId));
                db.Users.Remove(db.Lecturers.Where(l => l.lecturerId == lecturerId).FirstOrDefault().User);
                db.Lecturers.Remove(db.Lecturers.Where(l => l.lecturerId == lecturerId).FirstOrDefault());

                db.SaveChanges();
            }
        }
Example #9
0
 public void deleteApplication(int userId)
 {
     using (xinyuedbEntities db = new xinyuedbEntities())
     {
         var usr = db.Users.Find(userId);
         db.Users.Remove(usr);
         db.Students.Remove(db.Students.Where(s => s.userId == userId).FirstOrDefault());
         db.Preferences.RemoveRange(db.Preferences.Where(p => p.Student.userId == userId));
         db.Allocations.RemoveRange(db.Allocations.Where(a => a.Student.userId == userId));
         db.SaveChanges();
     }
 }
Example #10
0
        public static void saveWeight(int prefWeight, int yearWeight, int stuWeight)
        {
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                var config = db.Configs;
                config.Where(c => c.name.Equals("prefWeight")).FirstOrDefault().value = prefWeight;
                config.Where(c => c.name.Equals("yearWeight")).FirstOrDefault().value = yearWeight;
                config.Where(c => c.name.Equals("stuWeight")).FirstOrDefault().value  = stuWeight;

                db.SaveChanges();
            }
        }
Example #11
0
 public static void deleteClass(int classId)
 {
     using (xinyuedbEntities db = new xinyuedbEntities())
     {
         // remember to delete class from "preference" and "allocation" table
         var cla = db.Classes.Where(c => c.classId == classId).FirstOrDefault();
         if (cla != null)
         {
             db.Classes.Remove(cla);
             db.SaveChanges();
         }
     }
 }
Example #12
0
        public static void saveStudentsForMultiselectList(IEnumerable <string> selected_students, int classId)
        {
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                var    lab       = db.Classes.Where(c => c.classId == classId).FirstOrDefault();
                double classTime = (lab.endTime - lab.startTime).TotalHours;

                //remove old allocations
                var allocations = db.Allocations.Where(a => a.classId == classId);
                foreach (var allocation in allocations)
                {
                    var oldstudent = db.Students.Where(s => s.studentId == allocation.studentId).FirstOrDefault();
                    if (lab.Module.semester == 1)
                    {
                        oldstudent.workingHour1 -= classTime;
                    }
                    else
                    {
                        oldstudent.workingHour2 -= classTime;
                    }
                }
                db.Allocations.RemoveRange(allocations);

                //store new allocations
                if (selected_students != null)
                {
                    foreach (var stu in selected_students)
                    {
                        int studentId = Int32.Parse(stu);

                        Allocation allo = new Allocation();
                        allo.classId   = classId;
                        allo.studentId = studentId;
                        db.Allocations.Add(allo);

                        var newstudent = db.Students.Where(s => s.studentId == studentId).FirstOrDefault();
                        if (lab.Module.semester == 1)
                        {
                            newstudent.workingHour1 += classTime;
                        }
                        else
                        {
                            newstudent.workingHour2 += classTime;
                        }
                    }
                }

                db.SaveChanges();
            }
        }
Example #13
0
        public void clearStudentData()
        {
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                db.Students.RemoveRange(db.Students);
                db.Preferences.RemoveRange(db.Preferences);
                db.Allocations.RemoveRange(db.Allocations);
                db.Users.RemoveRange(db.Users.Where(x => x.accountType == "student"));
                var config = db.Configs.Where(c => c.name.Equals("published")).FirstOrDefault();
                config.value = 0;

                db.SaveChanges();
            }
        }
Example #14
0
        public static void updateClass(int classId, int moduleId, string type, int tutorNumber)
        {
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                var cla = db.Classes.FirstOrDefault(c => c.classId == classId);
                if (cla != null)
                {
                    cla.moduleId    = moduleId;
                    cla.type        = type;
                    cla.tutorNumber = tutorNumber;

                    db.SaveChanges();
                }
            }
        }
Example #15
0
 public void deleteAllocation()
 {
     using (xinyuedbEntities db = new xinyuedbEntities())
     {
         var students = db.Students;
         foreach (var student in students)
         {
             student.workingHour1 = 0;
             student.workingHour2 = 0;
         }
         db.Allocations.RemoveRange(db.Allocations);
         var config = db.Configs.Where(c => c.name.Equals("published")).FirstOrDefault();
         config.value = 0;
         db.SaveChanges();
     }
 }
Example #16
0
        public static void updateEventTime(int classId, string newStart, string newEnd)
        {
            // EventStart comes ISO 8601 format, eg:  "2000-01-10T10:00:00Z" - need to convert to DateTime
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                var cla = db.Classes.FirstOrDefault(c => c.classId == classId);
                if (cla != null)
                {
                    newStart = newStart.Replace("GMT+0000", "").Trim();
                    newEnd   = newEnd.Replace("GMT+0000", "").Trim();
                    newStart = newStart.Insert(3, ",");
                    newEnd   = newEnd.Insert(3, ",");

                    cla.startTime = DateTime.Parse(newStart, null, DateTimeStyles.RoundtripKind);
                    cla.endTime   = DateTime.Parse(newEnd, null, DateTimeStyles.RoundtripKind);

                    db.SaveChanges();
                }
            }
        }
Example #17
0
        public JsonResult Register(string email, string password, string first_name, string last_name, int matric_number, string degree, int level)
        {
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                var existing_user    = db.Users.Where(u => u.email == email);
                var existing_student = db.Students.Where(s => s.matricNumber == matric_number);

                if (existing_student.Any() || existing_user.Any())
                {
                    return(Json(new { success = false }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    User user = new User();
                    user.email       = email;
                    user.password    = PasswordHash.HashPassword(password);
                    user.accountType = "student";
                    db.Users.Add(user);

                    Student student = new Student();
                    student.userId       = user.userId;
                    student.fName        = first_name;
                    student.lName        = last_name;
                    student.matricNumber = matric_number;
                    student.degree       = degree;
                    student.year         = level;
                    student.maxHour      = 4;
                    student.applied      = true;
                    db.Students.Add(student);

                    db.SaveChanges();

                    Session["account"] = "student";
                    Session["userId"]  = user.userId.ToString();
                    Session["name"]    = first_name;
                    return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
                }
            }
        }
Example #18
0
        public static void addClass(int moduleId, string startTime, string endTime, string type, int tutorNumber)
        {
            // EventStart comes ISO 8601 format, eg:  "2000-01-10T10:00:00Z" - need to convert to DateTime
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                Class cla = new Class();

                cla.moduleId = moduleId;

                startTime     = startTime.Replace("GMT+0000", "").Trim();
                startTime     = startTime.Insert(3, ",");
                cla.startTime = DateTime.Parse(startTime, null, DateTimeStyles.RoundtripKind);

                endTime     = endTime.Replace("GMT+0000", "").Trim();
                endTime     = endTime.Insert(3, ",");
                cla.endTime = DateTime.Parse(endTime, null, DateTimeStyles.RoundtripKind);

                cla.type        = type;
                cla.tutorNumber = tutorNumber;

                db.Classes.Add(cla);
                db.SaveChanges();
            }
        }
Example #19
0
        public void allocateAlgorithm1()
        {
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                //get weight from database
                var config     = db.Configs;
                int prefWeight = config.Where(c => c.name.Equals("prefWeight")).FirstOrDefault().value;
                int yearWeight = config.Where(c => c.name.Equals("yearWeight")).FirstOrDefault().value;
                int stuWeight  = config.Where(c => c.name.Equals("stuWeight")).FirstOrDefault().value;

                //get all lab classes and sort them in descending order
                var        labs  = db.Classes.Where(c => c.type == "lab" && c.Module.semester == 1 && c.Module.year <= 3).OrderByDescending(l => l.Module.year).ToList();
                List <int> labId = new List <int>();
                foreach (var l in labs)
                {
                    labId.Add(l.classId);
                }

                Dictionary <int, double> workingHours = new Dictionary <int, double>();
                var        stu   = db.Students.Where(s => s.year <= 4);
                List <int> stuId = new List <int>();
                foreach (var s in stu)
                {
                    stuId.Add(s.studentId);
                    workingHours.Add(s.studentId, 0);
                }

                //create matric and set weight according to "preferences" and "1-year-approach"
                List <List <Weight> > weight = new List <List <Weight> >();
                for (var i = 0; i < labs.Count(); i++)
                {
                    weight.Add(new List <Weight>());
                    for (var j = 0; j < stuId.Count; j++)
                    {
                        weight[i].Add(new Weight(labId[i], stuId[j], prefWeight));
                        int lId = labId[i];
                        int sId = stuId[j];

                        //preference
                        string prefered = "neutral";
                        try
                        {
                            prefered = db.Preferences.Where(p => p.classId == lId).Where(p => p.studentId == sId).FirstOrDefault().prefered;
                            if (prefered.Equals("liked"))
                            {
                                weight[i][j].weight = 2 * prefWeight;
                            }
                            else
                            {
                                weight[i][j].weight = 0;
                            }
                        }
                        catch
                        {
                            System.Diagnostics.Debug.WriteLine("Preference doesn't exist!");
                        }

                        //check time clash
                        if (checkTimeClash(lId, sId))
                        {
                            weight[i][j].weight = null;
                        }

                        //1-year approach
                        int stuYear = db.Students.Where(s => s.studentId == sId).FirstOrDefault().year;
                        int labYear = db.Classes.Where(c => c.classId == lId).FirstOrDefault().Module.year;
                        if (stuYear > labYear)
                        {
                            switch (stuYear - labYear)
                            {
                            case 1: weight[i][j].weight += 3 * yearWeight; break;

                            case 2: weight[i][j].weight += 2 * yearWeight; break;

                            case 3: weight[i][j].weight += 1 * yearWeight; break;
                            }
                        }
                        else
                        {
                            weight[i][j].weight = null;
                        }
                    }
                }


                List <List <Weight> > sortedWeight = new List <List <Weight> >();

                for (var i = 0; i < weight.Count(); i++)
                {
                    //sort students for each lab class
                    List <Weight> sortedStudents = weight[i].OrderByDescending(w => w.weight).ToList();
                    sortedWeight.Add(sortedStudents);

                    int      classId     = weight[i][0].classId;
                    int      tutorNumber = db.Classes.Where(c => c.classId == classId).FirstOrDefault().tutorNumber;
                    DateTime startTime   = db.Classes.Where(c => c.classId == classId).FirstOrDefault().startTime;
                    DateTime endTime     = db.Classes.Where(c => c.classId == classId).FirstOrDefault().endTime;

                    //the number of tutors
                    for (var j = 0; j < (tutorNumber < sortedStudents.Count() ? tutorNumber : sortedStudents.Count()); j++)
                    {
                        int studentId = sortedStudents[j].studentId;

                        if (sortedStudents[j].weight != null)
                        {
                            int?maxHour = db.Students.Where(s => s.studentId == studentId).FirstOrDefault().maxHour;

                            if ((maxHour == null) || (workingHours[sortedStudents[j].studentId] + (endTime - startTime).TotalHours <= maxHour)) //maxHour
                            {
                                Allocation allo = new Allocation();
                                allo.classId   = sortedStudents[j].classId;
                                allo.studentId = sortedStudents[j].studentId;
                                db.Allocations.Add(allo);

                                workingHours[sortedStudents[j].studentId] += (endTime - startTime).TotalHours;

                                //more applicants assigned
                                for (var k = i + 1; k < weight.Count(); k++)
                                {
                                    foreach (var w in weight[k])
                                    {
                                        if (w.studentId == sortedStudents[j].studentId)
                                        {
                                            w.weight -= stuWeight;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                //set weight in other following classes to be null
                                for (var k = i; k < weight.Count(); k++)
                                {
                                    foreach (var w in weight[k])
                                    {
                                        if (w.studentId == sortedStudents[j].studentId)
                                        {
                                            w.weight = null;
                                        }
                                    }
                                }
                            }

                            //add applicant's allocated working hours to the "student" table
                            var st = db.Students.Where(s => s.studentId == studentId).FirstOrDefault();
                            st.workingHour1 = workingHours[sortedStudents[j].studentId];
                        }
                    }
                }

                db.SaveChanges();
            }
        }
Example #20
0
        public void populateDatabase()
        {
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                Random   rnd              = new Random();
                string[] degrees          = { "AC", "CS" };
                int      applicantsNumber = rnd.Next(25, 30);

                for (int i = 0; i < applicantsNumber; i++)
                {
                    Student stu = new Student();
                    stu.matricNumber = i;
                    stu.fName        = "Applicant";
                    stu.lName        = (i + 1).ToString();
                    stu.degree       = degrees[rnd.Next(0, 2)];
                    stu.year         = rnd.Next(2, 5);
                    stu.maxHour      = rnd.Next(2, 20);
                    stu.workingHour1 = 0;
                    stu.workingHour2 = 0;
                    stu.applied      = true;
                    db.Students.Add(stu);//add student[i] to "student" table

                    var myModules            = db.Modules.Where(m => m.degree.Contains(stu.degree) && m.year == stu.year);
                    List <ClassInfo> myClass = new List <ClassInfo>();
                    foreach (var m in myModules)
                    {
                        var myCla = db.Classes.Where(c => c.moduleId == m.moduleId);
                        foreach (var c in myCla)
                        {
                            ClassInfo cla = new ClassInfo();
                            cla.moduleId  = m.moduleId;
                            cla.title     = m.name;
                            cla.classId   = c.classId;
                            cla.startTime = c.startTime.ToString("yyyy-MM-ddTHH:mm:ss");
                            cla.endTime   = c.endTime.ToString("yyyy-MM-ddTHH:mm:ss");
                            myClass.Add(cla);//get student[i]'s own classes
                        }
                    }


                    List <ClassInfo> availableLabs = new List <ClassInfo>();
                    var modules = db.Modules.Where(m => m.year < stu.year && m.degree.Contains(stu.degree));
                    foreach (var module in modules)
                    {
                        var labs = db.Classes.Where(c => c.moduleId == module.moduleId).Where(c => c.type == "lab");
                        foreach (var lab in labs)
                        {
                            bool noTimeClash = true;
                            for (int index = 0; index < myClass.Count(); index++)
                            {
                                if (!((DateTime.Compare(DateTime.Parse(myClass[index].endTime), lab.startTime) <= 0) || (DateTime.Compare(DateTime.Parse(myClass[index].startTime), lab.endTime) >= 0)))
                                {
                                    noTimeClash = false;
                                    break;
                                }
                            }

                            if (noTimeClash)
                            {
                                ClassInfo cla = new ClassInfo();
                                cla.title     = module.name;
                                cla.classId   = lab.classId;
                                cla.startTime = lab.startTime.ToString("yyyy-MM-ddTHH:mm:ss");
                                cla.endTime   = lab.endTime.ToString("yyyy-MM-ddTHH:mm:ss");
                                cla.moduleId  = module.moduleId;
                                availableLabs.Add(cla);//get all lab classes that student[i] can tutor
                            }
                        }
                    }

                    //add student[i] to "preferences" table
                    int likedNumber = rnd.Next(0, availableLabs.Count());
                    for (int number = 0; number < likedNumber; number++)
                    {
                        Preference pref  = new Preference();
                        int        index = rnd.Next(0, availableLabs.Count());
                        pref.classId   = availableLabs[index].classId;
                        pref.studentId = stu.studentId;
                        pref.prefered  = "liked";
                        db.Preferences.Add(pref);
                        availableLabs.RemoveAt(index);
                    }
                    int dislikedNumber = rnd.Next(0, availableLabs.Count());
                    for (int number = 0; number < dislikedNumber; number++)
                    {
                        Preference pref  = new Preference();
                        int        index = rnd.Next(0, availableLabs.Count());
                        pref.classId   = availableLabs[index].classId;
                        pref.studentId = stu.studentId;
                        pref.prefered  = "disliked";
                        db.Preferences.Add(pref);
                    }

                    db.SaveChanges();
                }
            }
        }
Example #21
0
        public static void updatePreference(int maxhour, int studentId, string ni, IEnumerable <string> neutralList, IEnumerable <string> likedList, IEnumerable <string> dislikedList)
        {
            using (xinyuedbEntities db = new xinyuedbEntities())
            {
                var stu = db.Students.Where(s => s.studentId == studentId).FirstOrDefault();
                stu.maxHour = maxhour;
                if (ni != null)
                {
                    stu.NI = ni;
                }

                var pref = db.Preferences.Where(p => p.studentId == studentId);

                if (neutralList != null)
                {
                    foreach (var item in neutralList)
                    {
                        int classId = Int32.Parse(item);
                        var entity  = pref.Where(e => e.classId == classId).FirstOrDefault();
                        if (entity != null)
                        {
                            db.Preferences.Remove(entity);
                        }
                    }
                }
                if (likedList != null)
                {
                    foreach (var item in likedList)
                    {
                        int classId = Int32.Parse(item);
                        var entity  = pref.Where(e => e.classId == classId).FirstOrDefault();
                        if (entity != null)
                        {
                            entity.prefered = "liked";
                        }
                        else
                        {
                            Preference p = new Preference();
                            p.classId   = Int32.Parse(item);
                            p.studentId = studentId;
                            p.prefered  = "liked";
                            db.Preferences.Add(p);
                        }
                    }
                }
                if (dislikedList != null)
                {
                    foreach (var item in dislikedList)
                    {
                        int classId = Int32.Parse(item);
                        var entity  = pref.Where(e => e.classId == classId).FirstOrDefault();
                        if (entity != null)
                        {
                            entity.prefered = "disliked";
                        }
                        else
                        {
                            Preference p = new Preference();
                            p.classId   = Int32.Parse(item);
                            p.studentId = studentId;
                            p.prefered  = "disliked";
                            db.Preferences.Add(p);
                        }
                    }
                }

                db.SaveChanges();
            }
        }