예제 #1
0
        static void addLocations()
        {
            List <MoodleEvent> events = MoodleDB.getMoodleDB().getDaysEvents();
            Random             rnd    = new Random();
            int craig = 0;
            int merch = 0;
            int off   = 0;

            foreach (MoodleEvent evt in events)
            {
                int num = rnd.Next(1, 6);
                if (num == 1) //craig
                {
                    LocationDB.getLocationDB().addUserLocation(evt.userID, 555507.3, 31423.4, evt.time);
                    craig++;
                }
                else if (num == 2) //Merch
                {
                    LocationDB.getLocationDB().addUserLocation(evt.userID, 555559.2, 31252.3, evt.time.AddMinutes(2));
                    merch++;
                }
                else//off
                {
                    LocationDB.getLocationDB().addUserLocation(evt.userID, 554039.9, 34650.7, evt.time.AddMinutes(2));
                    off++;
                }
                Console.WriteLine("Craig: {0}, Merch: {1} and Off: {2}", craig, merch, off);
            }
        }
        /// <summary>
        /// Get user by ID
        /// </summary>
        /// <param name="ID">Users ID</param>
        /// <returns>A moodleuser object</returns>
        public MoodleUser getUserByID(int ID)
        {
            MoodleUser user;

            user = MoodleDB.getMoodleDB().getUserByID(ID);
            return(user);
        }
예제 #3
0
        static void Main(string[] args)
        {
            MoodleUser        user  = MoodleDB.getMoodleDB().getUserByID(3);
            List <MoodleUser> users = MoodleDB.getMoodleDB().getUserWithSameModules(user);

            Console.ReadLine();
        }
        public void GetUserByID()
        {
            int ID = 1;

            MoodleUser user = MoodleDB.getMoodleDB().getUserByID(ID);

            Assert.AreEqual(user.ID, ID);
            Assert.IsNotNull(user.fName);
        }
        public void GetCourseByID()
        {
            int ID = 1;

            MoodleCourse course = MoodleDB.getMoodleDB().getCourse(ID);

            Assert.AreEqual(course.ID, ID);
            Assert.IsNotNull(course.desc);
        }
        /// <summary>
        /// Regester the user with locationDB
        /// </summary>
        /// <param name="moodleUsername"></param>
        /// <param name="moodlePassword"></param>
        /// <param name="password"></param>
        /// <param name="fName"></param>
        /// <param name="lName"></param>
        /// <param name="location"></param>
        public void Register(string moodleUsername, string moodlePassword, string password, string fName, string lName, string location)
        {
            int ID = MoodleDB.getMoodleDB().getUser(moodleUsername, moodlePassword);

            if (ID != -1)
            {
                LocationDB.getLocationDB().Register(moodleUsername, moodlePassword, ID, password, fName, lName, location);
            }
        }
예제 #7
0
        static void bestMatches()
        {
            MoodleUser        user  = MoodleDB.getMoodleDB().getUserByID(4);
            List <MoodleUser> users = System.getSystem().getBestMatches(user);

            foreach (MoodleUser mu in users)
            {
                Console.WriteLine(mu.toString());
            }
        }
        /// <summary>
        /// Login the user to the locationDB
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public MoodleUser Login(string username, string password)
        {
            MoodleUser user = null;
            int        id   = LocationDB.getLocationDB().login(username, password);

            if (id != -1)
            {
                user = MoodleDB.getMoodleDB().getUserByID(id);
            }
            return(user);
        }
예제 #9
0
        /// <summary>
        /// Gets user by ID
        /// </summary>
        /// <param name="ID">ID of the user</param>
        /// <returns>A Moodle user Object</returns>
        public MoodleUser getUserByID(int ID)
        {
            MoodleUser user  = null;
            string     query = String.Format("SELECT * FROM mdl_user WHERE ID ={0} ", ID);

            using (var reader = this.runQuery(query))
            {
                while (reader.Read())
                {
                    user = getUserObject(reader);
                }
            }
            user.courses = MoodleDB.getMoodleDB().getUsersCourses(user);
            return(user);
        }
        /*********************
         * Login and Register
         ***********************/

        /// <summary>
        /// Login the user to Moodle using rest.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public MoodleUser LoginMoodle(string username, string password)
        {
            int id = MoodleDB.getMoodleDB().getUser(username, password);

            return(MoodleDB.getMoodleDB().getUserByID(id));
        }
        /// <summary>
        /// Returns a list of users that are a best
        /// match for user will try to return a min of 5 users
        /// </summary>
        /// <param name="user">The user to find matches for</param>
        /// <returns>List of moodle user</returns>
        public List <MoodleUser> getBestMatches(MoodleUser user)
        {
            List <MoodleUser> users   = new List <MoodleUser>();
            List <MoodleUser> matches = new List <MoodleUser>(); // List to be returned.

            users = MoodleDB.getMoodleDB().getUserWithSameModules(user);

            //Perfect Match. //Main Loop
            foreach (MoodleUser moUser in users)
            {
                moUser.courses = MoodleDB.getMoodleDB().getUsersCourses(moUser);
                int courseMatch = 0;
                if (moUser.city.ToLower() == user.city.ToLower())
                {
                    foreach (MoodleCourse course in moUser.courses)
                    {
                        foreach (MoodleCourse userCourse in user.courses)
                        {
                            if (course.ID == userCourse.ID)
                            {
                                courseMatch++;
                            }
                        }
                    }
                }
                if (user.courses.Count == courseMatch)
                {
                    if (!matches.Contains(moUser))
                    {
                        matches.Add(moUser);
                    }
                }
            }
            foreach (MoodleUser mu in matches) // FY all matches from main list.
            {
                users.Remove(mu);
            }
            ///2 > Modules match
            int matchLevel = user.courses.Count;

            while (matches.Count < 5)
            {
                foreach (MoodleUser moUser in users)
                {
                    int courseMatch = 0;
                    foreach (MoodleCourse course in moUser.courses)
                    {
                        foreach (MoodleCourse c in user.courses)
                        {
                            if (c.ID == course.ID)
                            {
                                courseMatch++;
                            }
                        }
                    }
                    if (courseMatch == matchLevel)
                    {
                        matches.Add(moUser);
                    }
                }
                foreach (MoodleUser moUser in matches)
                {
                    users.Remove(moUser);
                }
                matchLevel--;
                if (users.Count == 0 || matchLevel == 0)
                {
                    break;
                }
            }
            return(matches);
        }
        /// <summary>
        /// Gets a user year and there matches avg
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Dictionary <MoodleCourse, List <LocationAvg> > getAvgsYear(int ID)
        {
            MoodleUser          user    = MoodleDB.getMoodleDB().getUserByID(ID);
            List <MoodleCourse> courses = MoodleDB.getMoodleDB().getUsersCourses(user);
            List <MoodleUser>   matches = System.getSystem().getBestMatches(user);
            Dictionary <MoodleCourse, List <LocationAvg> > averages = new Dictionary <MoodleCourse, List <LocationAvg> >();
            List <LocationAvg> avgs = LocationDB.getLocationDB().getAveragesYear(matches);

            ///Overall
            //User
            LocationAvg  userAvg = LocationDB.getLocationDB().getUsersAvgYear(ID);
            MoodleCourse global  = new MoodleCourse(-1, "Global", "Global", "");
            //Match Avg
            int totalOn = 0, totalOff = 0;

            foreach (MoodleUser avg in matches)
            {
                LocationAvg matchAvg = LocationDB.getLocationDB().getUsersAvgYear(ID);
                if (matchAvg != null)
                {
                    totalOn  = totalOn + matchAvg.on;
                    totalOff = totalOff + matchAvg.off;
                }
            }
            int avgMatchOn = 0, avgMatchOff = 0;

            //On
            if (matches.Count > 0 && totalOn > 0)
            {
                avgMatchOn = totalOn / matches.Count;
            }
            if (matches.Count > 0 && totalOff > 0)
            {
                avgMatchOff = totalOff / matches.Count;
            }
            LocationAvg        overallAvg = new LocationAvg(-1, DateTime.Now, avgMatchOn, avgMatchOff);
            List <LocationAvg> locationAvgs = new List <LocationAvg>();

            locationAvgs.Add(userAvg);
            locationAvgs.Add(overallAvg);
            averages.Add(global, locationAvgs);

            //Per Course
            //User
            foreach (MoodleCourse course in courses)
            {
                int cTotalOn = 0, cTotalOff = 0;
                //User
                LocationAvg avg = LocationDB.getLocationDB().getUsersAvgYear(course.ID, user.ID);

                int count = 0;
                foreach (MoodleUser mUser in matches)
                {
                    foreach (MoodleCourse c in user.courses)
                    {
                        if (course.ID == c.ID)
                        {
                            LocationAvg tempLA = LocationDB.getLocationDB().getUsersAvgYear(course.ID, mUser.ID);
                            if (tempLA != null)
                            {
                                cTotalOn  = cTotalOn + tempLA.on;
                                cTotalOff = cTotalOff + tempLA.off;
                                count++;
                            }
                        }
                    }
                }

                int cAvgMatchOn = 0, cAvgMatchOff = 0;
                if (matches.Count > 0 && cTotalOn > 0)
                {
                    cAvgMatchOn  = cTotalOn / matches.Count;
                    cAvgMatchOff = cTotalOff / matches.Count;
                }
                List <LocationAvg> cAvgs  = new List <LocationAvg>();
                LocationAvg        locAvg = new LocationAvg(-1, DateTime.Now, cAvgMatchOn, cAvgMatchOff);
                cAvgs.Add(avg);
                cAvgs.Add(locAvg);
                averages.Add(course, cAvgs);
            }
            return(averages);
        }
        /// <summary>
        /// Updates the location DB taking all of the items
        /// from week and adding them to the week
        /// </summary>
        private void updateWeek()
        {
            ///Get and Sort Events.
            DateTime           date         = DateTime.Now;
            List <MoodleEvent> todaysEvents = MoodleDB.getMoodleDB().getDaysEvents(); //Change

            todaysEvents = System.getSystem().sortEventsList(todaysEvents);

            List <MoodleEvent> userEvents = new List <MoodleEvent>();

            //Fixes Issue with foreach.
            MoodleEvent e = new MoodleEvent(0, 0, 0, "", DateTime.Now);

            todaysEvents.Add(e);
            int currentID = -1;

            foreach (MoodleEvent evt in todaysEvents)
            {
                if (currentID == evt.userID) //Sane user.
                {
                    userEvents.Add(evt);
                }
                //Current event has diffrent user than the current list.
                //so counts the current list, add to database then deals with
                //current event.
                else if (currentID != evt.userID && userEvents.Count > 0)
                {
                    int countOff = 0;
                    int countOn  = 0;
                    Dictionary <MoodleCourse, List <int> > courseAvgs = new Dictionary <MoodleCourse, List <int> >();
                    //Get user locations
                    List <LocationEvent> Locations = LocationDB.getLocationDB().getUserLocation(evt.userID);
                    foreach (MoodleEvent userEvt in userEvents)
                    {
                        //Compare events and location
                        foreach (MoodleEvent moodleEvt in userEvents) //Events
                        {
                            bool found = false;
                            foreach (LocationEvent loc in Locations) //Locations
                            {
                                //Check Time within 10 mins
                                DateTime timeplus = new DateTime();
                                timeplus = loc.time;
                                timeplus.AddMinutes(10);
                                if (Math.Abs((moodleEvt.time - loc.time).TotalMinutes) <= 10 && Math.Abs((moodleEvt.time - loc.time).TotalMinutes) >= 0)
                                {
                                    //Find if on campus
                                    foreach (Campus c in System.getSystem().campus)
                                    {
                                        if (c.isIn(loc.lat, loc.lng)) //Check Location
                                        {
                                            countOn++;
                                            found = true;
                                            bool foundCourse = false;


                                            MoodleCourse mc = null;
                                            foreach (MoodleCourse course in courseAvgs.Keys)
                                            {
                                                if (course.ID == moodleEvt.courseID)
                                                {
                                                    foundCourse = true;
                                                    mc          = course;
                                                    break;
                                                }
                                            }
                                            //Update
                                            if (foundCourse)
                                            {
                                                List <int> list = courseAvgs[mc];
                                                list[0]        = list[0]++;
                                                courseAvgs[mc] = list;
                                                mc             = null;
                                            }
                                            else //Add Course
                                            {
                                                List <int> list = new List <int>();
                                                list.Add(1);
                                                list.Add(0);
                                                if (moodleEvt.courseID > 0)
                                                {
                                                    mc = MoodleDB.getMoodleDB().getCourse(moodleEvt.courseID);
                                                    if (mc != null)
                                                    {
                                                        courseAvgs.Add(mc, list);
                                                    }
                                                    mc = null;
                                                }
                                            }
                                        }
                                    }
                                    if (!found)
                                    {
                                        countOff++;
                                        bool         foundCourse = false;
                                        MoodleCourse mc          = null;
                                        foreach (MoodleCourse course in courseAvgs.Keys)
                                        {
                                            if (course.ID == moodleEvt.courseID)
                                            {
                                                mc          = course;
                                                foundCourse = true;
                                            }
                                        }
                                        if (evt.courseID > 0)
                                        {
                                            //Update
                                            if (foundCourse)
                                            {
                                                List <int> list = courseAvgs[mc];
                                                list[1]        = list[1]++;
                                                courseAvgs[mc] = list;
                                                mc             = null;
                                            }
                                            else //course not in list
                                            {
                                                List <int> list = new List <int>();
                                                list.Add(0);
                                                list.Add(1);
                                                courseAvgs.Add(MoodleDB.getMoodleDB().getCourse(moodleEvt.courseID), list);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //get time at campus
                    int timeOn  = countOn * 10;
                    int timeOff = countOff * 10;

                    //Add to Database.
                    //Generic.
                    LocationDB.getLocationDB().addLocationWeekEvents(currentID, timeOn, timeOff);
                    //Per Course.
                    foreach (MoodleCourse course in courseAvgs.Keys)
                    {
                        List <int> list = courseAvgs[course];
                        int        on   = list[0] * 10;
                        int        off  = list[1] * 10;
                        //Add to DB.
                        LocationDB.getLocationDB().addLocationWeekEventsCourse(currentID, course.ID, on, off);
                    }
                    userEvents = new List <MoodleEvent>();
                } // End Final
                else if (currentID != evt.userID)
                {
                    currentID = evt.userID;
                    userEvents.Add(evt);
                }
            }
        }
 /// <summary>
 /// Get all the users of a course.
 /// </summary>
 /// <param name="ID">Course ID</param>
 /// <returns>A list of MoodleUser objects</returns>
 public List <MoodleUser> getUserByCourse(int ID)
 {
     return(MoodleDB.getMoodleDB().getUsersByCourse(ID));
 }
 /// <summary>
 /// Get a course from moodle
 /// Tested: Working
 /// </summary>
 /// <param name="ID">Course ID</param>
 /// <returns>MoodleCourse Object</returns>
 public MoodleCourse getCourse(int ID)
 {
     return(MoodleDB.getMoodleDB().getCourse(ID));
 }