/// <summary> /// Transfers user from SQLObject to MoodleCourse Object /// </summary> /// <param name="reader">MySQLReader</param> /// <returns>MoodleCourse Object</returns> protected MoodleCourse getCourseObject(MySqlDataReader reader) { MoodleCourse course; course = new MoodleCourse(int.Parse(reader["ID"].ToString()), reader["fullname"].ToString(), reader["shortname"].ToString(), reader["summary"].ToString()); return(course); }
public void GetCourseByID() { int ID = 1; MoodleCourse course = MoodleDB.getMoodleDB().getCourse(ID); Assert.AreEqual(course.ID, ID); Assert.IsNotNull(course.desc); }
public void toREST() { //Moodle.Instance.token = ConfigurationSettings.AppSettings["token"]; //Moodle.Instance.domain = "elearning.univermilenium.edu.mx"; //obtener id del curso //obtener id del grupo //obtener id del usuari //enrolar idusuario a curso //enrolar idusuario a grupo List <string> groups = new List <string>(); List <string> errors = new List <string>(); /* * dummies users */ this.Users = new List <User>(); User dummy = new User(); dummy.username = "******"; dummy.firstname = "crash"; dummy.lastname = "test dummy"; dummy.password = "******"; dummy.email = "*****@*****.**"; dummy.course1 = "PRUEBA"; dummy.group1 = "GRUPODUMMIES"; dummy.type1 = 1; this.Users.Add(dummy); if (this.Users.Count() > 0) { foreach (User user in this.Users) { try { //obtener id del curso. MoodleCourse course = Moodle.Instance.getCourse(user.course1); //crear - obtener grupo if (Moodle.Instance.createGroup(course, user.course1, "Grupo para " + course.name)) { } } catch { this.Errors.Add(user.toString()); } } } }
/// <summary> /// Gets a Course by ID /// </summary> /// <param name="ID">ID of the Course</param> /// <returns></returns> public MoodleCourse getCourse(int ID) { string query = String.Format("SELECT * FROM mdl_course WHERE Id={0}", ID); MoodleCourse course = null; using (var reader = this.runQuery(query)) { while (reader.Read()) { course = getCourseObject(reader); } } return(course); }
/// <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); } } }