Пример #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 the LocationDb Object
 /// </summary>
 /// <returns></returns>
 public static LocationDB getLocationDB()
 {
     if (locDB == null)
     {
         locDB = new LocationDB("52.29.133.101", "3306", "MoodleLocation", "Ryan", "Pa$$w0rd");
     }
     return(locDB);
 }
        /// <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);
            }
        }
 /*****************************
  * Update locationDB
  ******************************/
 /// <summary>
 /// Updates the LocationDB
 /// </summary>
 public void updatesLocationDB()
 {
     if (DateTime.Now.DayOfWeek == DayOfWeek.Friday && DateTime.Now.Add(new TimeSpan(7, 0, 0, 0, 0)).Month != DateTime.Now.Month)
     { //Check if this is the last friday of month.
         updateLocationMonth();
         LocationDB.getLocationDB().removeWeek();
     }
     updateWeek();
     LocationDB.getLocationDB().removeDay();
 }
        /// <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);
        }
Пример #6
0
        static void mapTetst()
        {
            double        x   = 555601.5;
            double        y   = 31247.8;
            List <Campus> cam = LocationDB.getLocationDB().getCampus();

            foreach (Campus c in cam)
            {
                if (c.isIn(x, y))
                {
                    Console.WriteLine(c.name);
                }
            }
        }
Пример #7
0
 static void locationsget()
 {
     List <LocationEvent> evts = LocationDB.getLocationDB().getUserLocation(1);
 }
        /// <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);
        }
        public void updateLocationDBYear()
        {
            List <LocationAvg> evts = LocationDB.getLocationDB().getYearAvg();

            evts = this.sortLocationAvg(evts);
            int currentID = 0;
            int countOff  = 0;
            int countOn   = 0;


            foreach (LocationAvg avg in evts)
            {
                if (avg.userID == currentID)
                {
                    countOff = +avg.off;
                    countOn  = +avg.on;
                }
                else if (avg.userID != currentID && (countOn > 0 && countOff > 0 || countOn > 0 || countOff > 0))
                {
                    LocationDB.getLocationDB().addYearAverage(avg.userID, countOn, countOff);
                    countOff  = 0;
                    countOn   = 0;
                    currentID = avg.userID;
                    countOff  = +avg.off;
                    countOn   = +avg.on;
                }
                else
                {
                    currentID = avg.userID;
                }
            }
            //Course Avgs.
            Dictionary <int, List <LocationAvg> > courseAvgs = new Dictionary <int, List <LocationAvg> >(); currentID = 0;

            countOff = 0;
            countOn  = 0;
            List <LocationAvg> courseEvt = LocationDB.getLocationDB().getCourseYearAvg();

            evts.Sort();

            foreach (LocationAvg avg in courseEvt)
            {
                if (avg.userID == currentID)
                {
                    if (courseAvgs.ContainsKey(avg.courseID))
                    {
                        courseAvgs[avg.courseID].Add(avg);
                    }
                    else
                    {
                        List <LocationAvg> newList = new List <LocationAvg>();
                        newList.Add(avg);
                        courseAvgs.Add(avg.courseID, newList);
                    }
                }
                else if (avg.userID != currentID && courseEvt.Count > 0)
                {
                    foreach (int i in courseAvgs.Keys)
                    {
                        LocationDB.getLocationDB().addYearAverage(currentID, courseEvt[i].courseID, courseEvt[i].on, courseEvt[i].off);
                    }
                }
            }
        }
 /***********************
  * Constructors and get instances
  ****************************/
 /// <summary>
 /// Constructor for system.
 /// </summary>
 private System()
 {
     this.campus = LocationDB.getLocationDB().getCampus();
 }
        /// <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);
                }
            }
        }
 public int getUserTarget(int userID)
 {
     return(LocationDB.getLocationDB().getUserTarget(userID));
 }
 public void setUserTarget(int userID, int target)
 {
     LocationDB.getLocationDB().setUserTarget(userID, target);
 }
 /// <summary>
 /// add a users current location to the location db
 /// </summary>
 /// <param name="userID">User ID</param>
 /// <param name="x">lat</param>
 /// <param name="y">long</param>
 public void addLocation(int userID, double x, double y)
 {
     LocationDB.getLocationDB().addUserLocation(userID, x, y);
 }