/// <summary> /// Gets the learn sessions. /// </summary> /// <param name="lmId">The lm id.</param> /// <returns></returns> /// <remarks>Documented by Dev08, 2009-01-13</remarks> public List <int> GetLearnSessions(int lmId) { object learnSessionsCache = parent.CurrentUser.Cache[ObjectLifetimeIdentifier.GetIdentifier(CacheObject.StatisticsLearnSessions, lmId)]; MsSqlCeStatisticConnector c = MsSqlCeStatisticConnector.GetInstance(parent); //The data of GetLearnSession can be cached until a new session was created. if (runningSessionCopy == c.RunningSession && learnSessionsCache != null) { return(learnSessionsCache as List <int>); } //if cache is empty or the RunningSession has changed... SqlCeCommand cmd = MSSQLCEConn.CreateCommand(parent.CurrentUser); cmd.CommandText = "SELECT id FROM \"LearningSessions\" WHERE lm_id = @lmId AND user_id=@uid ORDER BY endtime ASC"; cmd.Parameters.Add("@lmId", lmId); cmd.Parameters.Add("@uid", parent.CurrentUser.Id); List <int> output = new List <int>(); SqlCeDataReader reader = MSSQLCEConn.ExecuteReader(cmd); while (reader.Read()) { object id = reader["id"]; int id_converted = Convert.ToInt32(id); output.Add(id_converted); } reader.Close(); runningSessionCopy = c.RunningSession; //Save to Cache parent.CurrentUser.Cache[ObjectLifetimeIdentifier.Create(CacheObject.StatisticsLearnSessions, lmId, Cache.DefaultStatisticValidationTime)] = output; return(output); }
/// <summary> /// Closes the user session. /// </summary> /// <param name="last_entry">The last_entry.</param> /// <remarks>Documented by Dev10, 2009-01-11</remarks> public void CloseUserSession(int last_entry) { SqlCeCommand cmd = MSSQLCEConn.CreateCommand(Parent.CurrentUser); cmd.CommandText = "UPDATE LearningSessions SET endtime=GETDATE() WHERE id=@id"; cmd.Parameters.Add("@id", last_entry); MSSQLCEConn.ExecuteNonQuery(cmd); //Following Statement does add the "RunningSession" = false to the current Statistic. MsSqlCeStatisticConnector connector = MsSqlCeStatisticConnector.GetInstance(Parent); connector.RunningSession = -1; }
/// <summary> /// Creates a new user session. /// The old one will be automatically closed, in case of inconsistance data. /// </summary> /// <param name="lm_id">The lm_id.</param> /// <returns></returns> /// <remarks>Documented by Dev10, 2009-01-11</remarks> /// <remarks>Documented by Dev08, 2009-04-28</remarks> public int OpenUserSession(int lm_id) { //1. Check if the old session is closed bool previousSessionExisting = false; DateTime?endtime = null; int latestSessionId = 0; SqlCeCommand cmd = MSSQLCEConn.CreateCommand(Parent.CurrentUser); cmd.CommandText = "SELECT * FROM LearningSessions WHERE lm_id=@lmid AND user_id=@userid ORDER BY starttime DESC"; cmd.Parameters.Add("@lmid", lm_id); cmd.Parameters.Add("@userid", Parent.CurrentUser.Id); SqlCeDataReader reader = MSSQLCEConn.ExecuteReader(cmd); if (reader.Read()) { previousSessionExisting = true; try { latestSessionId = Convert.ToInt32(reader["id"]); endtime = Convert.ToDateTime(reader["endtime"]); } catch { endtime = null; } } //2. Close the previous session, if it hasn't closed before (maybe cause of crash of ML) if (previousSessionExisting && !endtime.HasValue) { cmd.Parameters.Clear(); cmd.CommandText = "UPDATE LearningSessions SET endtime=GETDATE() WHERE id=@id"; cmd.Parameters.Add("@id", latestSessionId); MSSQLCEConn.ExecuteNonQuery(cmd); } //3. Add new session entry to DB cmd.Parameters.Clear(); cmd.CommandText = "INSERT INTO LearningSessions (user_id, lm_id, starttime, sum_right, sum_wrong, pool_content, box1_content, box2_content, box3_content, " + "box4_content, box5_content, box6_content, box7_content, box8_content, box9_content, box10_content)" + "VALUES(@userid, @lmid, GETDATE(), 0, 0, @pool, @b1, @b2, @b3, @b4, @b5, @b6, @b7, @b8, @b9, @b10); SELECT @@IDENTITY;"; cmd.Parameters.Add("@userid", Parent.CurrentUser.Id); cmd.Parameters.Add("@lmid", lm_id); int counter = 0; int cardsInBoxes = 0; BoxSizes boxContent = GetCurrentBoxContent(); foreach (int box in boxContent.Sizes) { if (counter == 0) { cmd.Parameters.Add("@pool", box); ++counter; continue; } cmd.Parameters.Add("@b" + Convert.ToString(counter++), box); cardsInBoxes += box; } int newSessionId = MSSQLCEConn.ExecuteScalar <int>(cmd).Value; //Following Statement does add the "RunningSession" = true to the current Statistic. MsSqlCeStatisticConnector connector = MsSqlCeStatisticConnector.GetInstance(Parent); connector.RunningSession = newSessionId; return(newSessionId); }