/// <summary>
        /// Adds a Challenge to the display so it can be seen by the user.
        /// </summary>
        /// /// <param name="challenge">The challenge to add to the display.</param>
        public void AddChallengeToDisplay(Challenge challenge)
        {
            DisplayChallengeControl displayChallengeControl = (DisplayChallengeControl)LoadControl("DisplayChallengeControl.ascx");
            displayChallengeControl.LoadChallengeIntoDisplay(challenge);

            _displayChallengeControls.Controls.Add(displayChallengeControl);
        }
Exemplo n.º 2
0
        public void TestCreateChallenge()
        {
            using (_trans)
            {
                int id;

                Log("Creating challenge");
                Challenge challenge = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today, 1, Statistic.Steps, 500);

                Log("Adding challenge to the database");
                Assert.IsTrue((id = ChallengeDAO.CreateNewChallenge(challenge)) > 0);

                Log("Loading challenge from the database");
                Challenge retrieved = ChallengeDAO.GetChallengeFromChallengeId(id);

                Log("Verifying that a matching challenge was found");
                Assert.IsNotNull(retrieved);

                Log("Verifying the integrity of challenge fields");
                Assert.AreEqual(challenge.Name, retrieved.Name);
                Assert.AreEqual(challenge.Description, retrieved.Description);
                Assert.AreEqual(challenge.EndTime, retrieved.EndTime);
                Assert.AreEqual(challenge.IsActive, retrieved.IsActive);
                Assert.AreEqual(challenge.IsPersistent, retrieved.IsPersistent);
                Assert.AreEqual(challenge.Points, retrieved.Points);
                Assert.AreEqual(challenge.StatisticBinding, retrieved.StatisticBinding);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Saves a challenge as a new entry in the DB.
 /// </summary>
 /// <param name="challenge">Challenge object to add to the DB.</param>
 /// <returns>ID of the created challenge on success, 0 on failure.</returns>
 public static int CreateNewChallenge(Challenge challenge)
 {
     try
     {
         using (SqlConnection connection = ConnectionManager.GetConnection())
         {
             var data = new ActivEarthDataProvidersDataContext(connection);
             var challengeData = new ChallengeDataProvider
                 {
                     name = challenge.Name,
                     description = challenge.Description,
                     reward = challenge.Reward,
                     requirement = challenge.Requirement,
                     persistent = challenge.IsPersistent,
                     end_time = challenge.EndTime,
                     duration_days = challenge.Duration.Days,
                     statistic = (byte)challenge.StatisticBinding,
                     active = challenge.IsActive,
                     image_path = challenge.ImagePath
                 };
             data.ChallengeDataProviders.InsertOnSubmit(challengeData);
             data.SubmitChanges();
             return challengeData.id;
         }
     }
     catch (Exception)
     {
         return 0;
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new Challenge and adds it to the collection.
        /// </summary>
        /// <param name="name">Challenge Name.</param>
        /// <param name="description">Challenge Description.</param>
        /// <param name="points">Points to be awarded upon completion of the Challenge.</param>
        /// <param name="start">Time at which the challenge should begin.</param>
        /// <param name="durationInDays">Number of days that the challenge should be active.</param>
        /// <param name="persistent">True if the Challenge is persistent, false otherwise.</param>
        /// <param name="statistic">Statistic on which the Challenge is based.</param>
        /// <param name="requirement">Statistic value required to complete the challenge.</param>
        /// <returns></returns>
        public static int CreateChallenge(string name, string description, int points, bool persistent,
            DateTime start, int durationInDays, Statistic statistic, float requirement)
        {
            Challenge newChallenge = new Challenge(name, description, points, persistent,
                    start, durationInDays, statistic, requirement);

            int id = ChallengeDAO.CreateNewChallenge(newChallenge);

            return id;
        }
        /// <summary>
        /// Loads a Challenge's information into the display so it can be seen by the user.
        /// </summary>
        /// /// <param name="challenge">The challenge to load into the display.</param>
        public void LoadChallengeIntoDisplay(Challenge challenge)
        {
            var user = (User)Session["userDetails"];
            if (user == null)
            {
                Response.Redirect("~/Account/Login.aspx");
            }

            _challengeName.Text = challenge.Name;
            _challengeImage.ImageUrl = challenge.ImagePath;
            _activityPointsValue.Text = challenge.Reward.ToString();
            _challengeDescription.Text = challenge.Description;

            _challengeProgressBar.Value = challenge.Progress;
            _challengeProgressNumerical.Text = ChallengeManager.GetFormattedProgress(challenge.ID, user.UserID);

            if (ChallengeManager.IsComplete(challenge.ID, user.UserID))
            {
                _challengeProgressNumerical.Text = "Completed";
            }
        }
Exemplo n.º 6
0
        public void TestUpdateChallenge()
        {
            using (_trans)
            {
                int id;

                Log("Creating challenge");
                Challenge challenge = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today, 1, Statistic.Steps, 500);

                Log("Adding challenge to the database");
                Assert.IsTrue((id = ChallengeDAO.CreateNewChallenge(challenge)) > 0);

                Log("Loading challenge from the database");
                Challenge retrieved = ChallengeDAO.GetChallengeFromChallengeId(id);

                Log("Verifying that a matching challenge was found");
                Assert.IsNotNull(retrieved);

                Log("Verifying that the challenge is still active");
                Assert.IsTrue(retrieved.IsActive);

                retrieved.IsActive = false;

                Log("Updating the challenge to be expired");
                Assert.IsTrue(ChallengeDAO.UpdateChallenge(retrieved));

                Log("Reloading challenge from the database");
                Challenge retrieved2 = ChallengeDAO.GetChallengeFromChallengeId(id);

                Log("Verifying that a matching challenge was found");
                Assert.IsNotNull(retrieved2);

                Log("Verifying that the challenge is inactive");
                Assert.IsFalse(retrieved2.IsActive);

            }
        }
Exemplo n.º 7
0
        public void TestGetAllChallenges()
        {
            using (_trans)
            {
                Log("Creating expired challenge");
                Challenge challenge1 = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today.AddDays(-1), 1, Statistic.Steps, 500);
                challenge1.IsActive = false;

                Log("Creating valid challenge");
                Challenge challenge2 = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today, 1, Statistic.BikeDistance, 500);

                Log("Creating valid challenge");
                Challenge challenge3 = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today, 1, Statistic.ChallengesCompleted, 500);

                Log("Adding challenges to DB");
                ChallengeDAO.CreateNewChallenge(challenge1);
                ChallengeDAO.CreateNewChallenge(challenge2);
                ChallengeDAO.CreateNewChallenge(challenge3);

                Log("Verifying that GetAllChallenges returns three challenges");
                Assert.AreEqual(3, ChallengeDAO.GetAllChallenges().Count);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Updates an existing Challenge in the DB.
        /// </summary>
        /// <param name="challenge">Challenge whose record needs updating.</param>
        /// <returns>True on success, false on failure.</returns>
        public static bool UpdateChallenge(Challenge challenge)
        {
            try
            {
                using (SqlConnection connection = ConnectionManager.GetConnection())
                {
                    var data = new ActivEarthDataProvidersDataContext(connection);
                    ChallengeDataProvider dbChallenge =
                        (from c in data.ChallengeDataProviders where c.id == challenge.ID select c).FirstOrDefault();
                    if (dbChallenge != null)
                    {
                        dbChallenge.name = challenge.Name;
                        dbChallenge.description = challenge.Description;
                        dbChallenge.reward = challenge.Reward;
                        dbChallenge.requirement = challenge.Requirement;
                        dbChallenge.persistent = challenge.IsPersistent;
                        dbChallenge.end_time = challenge.EndTime;
                        dbChallenge.duration_days = challenge.Duration.Days;
                        dbChallenge.statistic = (byte)challenge.StatisticBinding;
                        dbChallenge.active = challenge.IsActive;
                        dbChallenge.image_path = challenge.ImagePath;

                        data.SubmitChanges();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
Exemplo n.º 9
0
        public void TestGetChallengeFormatString()
        {
            using (_trans)
            {
                Log("Creating challenges");
                Challenge challenge1 = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today, 1, Statistic.Steps, 500);
                int id1 = ChallengeDAO.CreateNewChallenge(challenge1);

                Challenge challenge2 = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today, 1, Statistic.GasSavings, 500);
                int id2 = ChallengeDAO.CreateNewChallenge(challenge2);

                Log("Retrieving challenges");
                Challenge stepChallenge = ChallengeDAO.GetChallengeFromChallengeId(id1);
                Challenge gasChallenge = ChallengeDAO.GetChallengeFromChallengeId(id2);

                Log("Verifying format strings");
                Assert.AreEqual("N0", stepChallenge.FormatString);
                Assert.AreEqual("C", gasChallenge.FormatString);

            }
        }
Exemplo n.º 10
0
        public void TestGetActiveChallengesByLength()
        {
            using (_trans)
            {
                int initialDaily = ChallengeDAO.GetActiveDailyChallenges().Count;
                int initialWeekly = ChallengeDAO.GetActiveWeeklyChallenges().Count;

                Log("Creating expired challenge");
                Challenge challenge1 = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today.AddDays(-1), 1, Statistic.Steps, 500);
                challenge1.IsActive = false;

                Log("Creating a daily challenge");
                Challenge challenge2 = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today, 1, Statistic.BikeDistance, 500);

                Log("Creating another daily challenge");
                Challenge challenge3 = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today, 1, Statistic.BikeDistance, 500);

                Log("Creating weekly challenge 1");
                Challenge challenge4 = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today, 7, Statistic.ChallengesCompleted, 500);

                Log("Creating weekly challenge 2");
                Challenge challenge5 = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today, 7, Statistic.ChallengesCompleted, 500);

                Log("Creating weekly challenge 3");
                Challenge challenge6 = new Challenge("Test Challenge", "This is a test challenge",
                    30, false, DateTime.Today, 7, Statistic.ChallengesCompleted, 500);

                Log("Adding challenges to DB");
                ChallengeDAO.CreateNewChallenge(challenge1);
                ChallengeDAO.CreateNewChallenge(challenge2);
                ChallengeDAO.CreateNewChallenge(challenge3);
                ChallengeDAO.CreateNewChallenge(challenge4);
                ChallengeDAO.CreateNewChallenge(challenge5);
                ChallengeDAO.CreateNewChallenge(challenge6);

                Log("Verifying that GetActiveDailyChallenges returns two challenges");
                Assert.AreEqual(initialDaily + 2, ChallengeDAO.GetActiveDailyChallenges().Count);

                Log("Verifying that GetActiveWeeklyChallenges returns three challenges");
                Assert.AreEqual(initialWeekly + 3, ChallengeDAO.GetActiveWeeklyChallenges().Count);
            }
        }