/// <summary>
        /// A the year avg for each user.
        /// </summary>
        /// <param name="courseID"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public LocationAvg getUsersAvgYear(int courseID, int userID)
        {
            LocationAvg avg   = null;
            string      query = String.Format("SELECT * FROM LocationCourseYear WHERE UserID = {0} AND courseID = {1}", userID, courseID);

            using (MySqlDataReader reader = this.runQuery(query))
            {
                while (reader.Read())
                {
                    avg = this.getLocationAvgObjectWithID(reader);
                }
            }
            return(avg);
        }
        /// <summary>
        /// A the year avg for user.
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public LocationAvg getUsersAvgYear(int UserID)
        {
            LocationAvg avg   = null;
            string      query = String.Format("SELECT * FROM LocationYear WHERE UserID={0}", UserID);

            using (MySqlDataReader reader = this.runQuery(query))
            {
                while (reader.Read())
                {
                    avg = getLocationAvgObject(reader);
                }
            }
            return(avg);
        }
        /// <summary>
        /// A the week avg for each user in users.
        /// </summary>
        /// <param name="users">A list of locationAvg</param>
        /// <returns></returns>
        public List <LocationAvg> getAveragesWeek(List <MoodleUser> users)
        {
            List <LocationAvg> avgs = new List <LocationAvg> ();

            foreach (MoodleUser user in users)
            {
                LocationAvg avg = this.getUsersAvgWeek(user.ID);
                if (avg != null)
                {
                    avgs.Add(avg);
                }
            }
            return(avgs);
        }
        /// <summary>
        /// Add or Update Year to DB
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="courseID"></param>
        /// <param name="on"></param>
        /// <param name="off"></param>
        public void addYearAverage(int userID, int courseID, int on, int off)
        {
            LocationAvg avg = this.yearExists(userID, courseID);

            if (avg != null)
            {
                avg.off = avg.off + off;
                avg.on  = avg.on + on;
                string.Format("Update LocationCourseYear SET inUni = {0}, offUni = {1} WHERE UserID = {2} AND LocTime = {3} AND CourseID = {4}", avg.on, avg.off, userID, avg.locTime, avg.courseID);
            }
            else
            {
                string query = String.Format("INSERT INTO LocationCourseYear VALUES({0},{1},{2},{3},{4})", userID, courseID, DateTime.Now.ToShortDateString(), on, off);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Updates the window with each swipe.
        /// </summary>
        private void redoWindow()
        {
            try
            {
                List <BarModel> bars = new List <BarModel> ();
                this.revLayout.RemoveView(chart);
                this.chart = null;
                this.chart = new BarChartView(this);
                LocationAvg[] avgs = new LocationAvg[3];
                //Get Data
                if (this.windowID == 0)
                {         //Week
                    this.Title = "Week, " + s.selectedCourse.name;
                    avgs       = s.weekData [s.selectedCourse];
                }
                else if (this.windowID == 1)
                {          //Month
                    foreach (MoodleCourse c in s.monthData.Keys)
                    {
                        if (c.ID == s.selectedCourse.ID)
                        {
                            avgs = s.monthData[c];
                        }
                    }
                    this.Title = "Month, " + s.selectedCourse.name;
                }
                else
                {          // Year
                    foreach (MoodleCourse c in s.YearData.Keys)
                    {
                        if (c.ID == s.selectedCourse.ID)
                        {
                            avgs = s.YearData [c];
                        }
                    }
                    this.Title = "Year, " + s.selectedCourse.name;
                }

                //Null Check
                for (int i = 0; i < avgs.Length; i++)
                {
                    LocationAvg avg = avgs [i];
                    if (avg == null)
                    {
                        avg = new LocationAvg(-1, DateTime.Now, 0, 0);
                    }
                    avgs [i] = avg;
                }

                //Create Bar Models
                BarModel userOn = new BarModel
                {         //UserOn
                    Value = avgs [0].on,
                    Color = Android.Graphics.Color.Aqua,
                    ValueCaptionHidden = false,
                    ValueCaption       = "User On"
                };
                BarModel userOff = new BarModel
                {          //UserOff
                    Value = avgs [0].off,
                    Color = Android.Graphics.Color.Red,
                    ValueCaptionHidden = false,
                    ValueCaption       = "User Off"
                };
                BarModel avgOn = new BarModel
                {         //AvgOn
                    Value = avgs [1].on,
                    Color = Android.Graphics.Color.Green,
                    ValueCaptionHidden = false,
                    ValueCaption       = "Avg On"
                };
                BarModel avgOff = new BarModel
                {           //AvgOff
                    Value = avgs [1].off,
                    Color = Android.Graphics.Color.LightBlue,
                    ValueCaptionHidden = false,
                    ValueCaption       = "Avg Off"
                };

                if (this.windowID == 0)           //Week
                {
                }
                else if (this.windowID == 1)             //Month
                {
                }
                else             //Year
                {
                }

                //Create Chart
                bars.Add(userOn);
                bars.Add(userOff);
                bars.Add(avgOn);
                bars.Add(avgOff);
                this.chart.ItemsSource = bars;
                this.revLayout.AddView(chart, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
            }catch (Exception e)
            {
                Toast.MakeText(this, "winGraph " + e.ToString(), ToastLength.Long).Show();
            }
        }
        /// <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 window with each swipe.
        /// </summary>
        private void redoWindow()
        {
            if (this.revLayout == null)
            {
                Toast.MakeText(this, "null", ToastLength.Long).Show();
            }
            List <BarModel> bars = new List <BarModel> ();

            this.revLayout.RemoveView(chart);
            this.chart = null;
            this.chart = new BarChartView(this);
            LocationAvg[] avgs;

            //Get Data
            if (this.windowID == 0)               //Week
            {
                avgs       = this.weekData [this.courses [courseWindow]];
                this.Title = "Week, " + this.courses [courseWindow].name;
            }
            else if (this.windowID == 1)                  //Month
            {
                Toast.MakeText(this, "Month Avg Window:" + courseWindow, ToastLength.Long).Show();
                avgs       = this.monthData [this.courses [courseWindow]];
                this.Title = "Month, " + this.courses [courseWindow].name;
            }
            else                  // Year
            {
                avgs       = this.YearData [this.courses [courseWindow]];
                this.Title = "Year, " + this.courses [courseWindow].name;
            }
            //Null Check
            for (int i = 0; i < avgs.Length; i++)
            {
                LocationAvg avg = avgs [i];
                if (avg == null)
                {
                    avg = new LocationAvg(-1, DateTime.Now, 0, 0);
                }
                avgs [i] = avg;
            }

            //Create Bar Models
            Toast.MakeText(this, "Create User on", ToastLength.Long).Show();
            BarModel userOn = new BarModel {             //UserOn
                Value = avgs [0].on,
                Color = Android.Graphics.Color.Aqua,
                ValueCaptionHidden = false,
                ValueCaption       = "User On"
            };

            Toast.MakeText(this, "Create User off", ToastLength.Long).Show();
            BarModel userOff = new BarModel {              //UserOff
                Value = avgs [0].off,
                Color = Android.Graphics.Color.Red,
                ValueCaptionHidden = false,
                ValueCaption       = "User Off"
            };

            Toast.MakeText(this, "Create avg on", ToastLength.Long).Show();
            BarModel avgOn = new BarModel {             //AvgOn
                Value = avgs [1].on,
                Color = Android.Graphics.Color.Green,
                ValueCaptionHidden = false,
                ValueCaption       = "Avg On"
            };

            Toast.MakeText(this, "Create avg off", ToastLength.Long).Show();
            BarModel avgOff = new BarModel {               //AvgOff
                Value = avgs [1].off,
                Color = Android.Graphics.Color.Blue,
                ValueCaptionHidden = false,
                ValueCaption       = "Avg Off"
            };


            if (this.windowID == 0)               //Week
            {
            }
            else if (this.windowID == 1)                 //Month
            {
            }
            else                 //Year
            {
            }

            //Create Chart
            bars.Add(userOn);
            bars.Add(userOff);
            bars.Add(avgOn);
            bars.Add(avgOff);
            this.chart.ItemsSource = bars;
            this.revLayout.AddView(chart, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
        }