public JsonResult CountryDashboardList(string country)   // accepts the country and returns the list of campaigns by the JSON call
        {
            DynamicChartsModel dM    = new DynamicChartsModel(); // assign country with this
            CampaignViewModel  camVM = new CampaignViewModel();
            // get all the campaigns in the list with thier ids
            List <CampaignDTO> camp = new List <CampaignDTO>();

            camp = camVM.GetAllCampaigns();

            List <string> campaingCountrylist = new List <string>();  // get the campaigns name from the list campaings model and add them to string list

            foreach (var item in camp)
            {
                if (!campaingCountrylist.Contains(item.Country.ToString()))
                {
                    campaingCountrylist.Add(item.Country);
                }
            }

            dM.Countries = campaingCountrylist; // string list of campaigns names, store them in the Campaigns list where declared in the model
            List <string> allCampaignsOfCountry = new List <string>();

            foreach (var cam in camp)
            {
                if (country == cam.Country)
                {
                    allCampaignsOfCountry.Add(cam.Description); // get all the campaigns related to the country
                }
            }
            dM.Campaigns = allCampaignsOfCountry;
            dM.Campagin  = allCampaignsOfCountry.First();
            dM.Country   = country;
            return(Json(dM, JsonRequestBehavior.AllowGet));
        }
        // GET: DynamicallyUpdateChart
        public ActionResult CountryChart()
        {
            DynamicChartsModel dM = new DynamicChartsModel();
            //CandidateDTO candidateDTO = new CandidateDTO();
            CampaignViewModel camVM = new CampaignViewModel();
            // get all the campaigns in the list with thier ids
            List <CampaignDTO> camp = new List <CampaignDTO>();

            camp = camVM.GetAllCampaigns();

            List <string> campaingCountrylist = new List <string>();  // get the campaigns name from the list campaings model and add them to string list

            foreach (var item in camp)
            {
                if (!campaingCountrylist.Contains(item.Country.ToString()))
                {
                    campaingCountrylist.Add(item.Country);
                }
            }
            campaingCountrylist.Sort();
            dM.Countries = campaingCountrylist;       // string list of campaigns names, store them in the Campaigns list where declared in the model

            dM.Country = campaingCountrylist.First(); // get the first country for the Campaigns
            var           country = dM.Country;
            List <string> allCampaignsOfCountry = new List <string>();

            foreach (var cam in camp)
            {
                if (country == cam.Country)
                {
                    allCampaignsOfCountry.Add(cam.Description); // get all the campaigns related to the country
                }
            }


            bool isEmpty = !allCampaignsOfCountry.Any();

            if (!isEmpty) // check if the list is not null than add the values to the object , else redirect the user that there are no campaigns in the country
            {
                dM.Campaigns = allCampaignsOfCountry;
                dM.Campagin  = allCampaignsOfCountry.First();

                CampaignDashBoardList(dM.Country, dM.Campagin); // default draw of the data
                return(View("~/Views/DynamicallyUpdateChart/CountryChart.cshtml", dM));
            }
            else
            {
                return(View("~/Views/Results/NotFound.cshtml")); // no ca
            }
        }
Ejemplo n.º 3
0
        public List <string> GetAllTheDeTailsRelatedToEachCountry(string selectedCountry, string selectedCampaign)
        {
            DynamicChartsModel dM = new DynamicChartsModel();
            // get the data from the DB
            ChartsViewModel          cVM   = new ChartsViewModel();
            List <RegisterViewModel> users = cVM.GetAllUsres(); // get the users

            // get all the Campaign to count the number of Campaigns in each country
            CampaignViewModel  campVM    = new CampaignViewModel();
            List <CampaignDTO> campaigns = campVM.GetAllCampaigns();

            // get all the candidates of the system and count the total number of candidates of each country
            CandidatesViewModel candidateVM = new CandidatesViewModel();
            List <CandidateDTO> candidates  = candidateVM.GetAllCandidates();

            // get alll the Votes for each candidates of each country.
            VoteViewModel   vVM   = new VoteViewModel();
            List <VotesDTO> votes = vVM.GetAllTheVotes();

            string cand = "";

            selectedCountry  = selectedCountry.Trim();
            selectedCampaign = selectedCampaign.Trim();

            List <string> countryCampainglist = new List <string>();

            foreach (var item in campaigns)
            {
                if (!countryCampainglist.Contains(item.Country.ToString()))
                {
                    countryCampainglist.Add(item.Country);
                }
            }
            int  total_votes                    = 0;
            int  total_users                    = 0;
            bool canFound                       = false;
            var  votesForEachCandidate          = "";
            var  totalUsresForEachCampaignVoted = "";

            foreach (var country in countryCampainglist) // get the candidates related to the selected country and campaigns
            {
                if (selectedCountry == country)
                {
                    foreach (var cam in campaigns)
                    {
                        if (selectedCampaign == cam.Description)
                        {
                            foreach (var can in candidates)
                            {
                                if (can.CampaignID == cam.CampaignID)
                                {
                                    cand    += can.FirstName + ",";
                                    canFound = true;
                                    foreach (var vot in votes)
                                    {
                                        if (can.CandidateId == vot.CandidateId) // total votes for the each candidate
                                        {
                                            total_votes++;
                                        }
                                    }
                                    if (canFound == true)
                                    {
                                        votesForEachCandidate += total_votes + ",";
                                        total_votes            = 0;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            foreach (var user in users) // count total number of users related to that country
            {
                if (selectedCountry == user.Country)
                {
                    total_users++;
                }
            }

            var totalVoters = total_users.ToString();

            cand = cand.TrimEnd(',', ' ');                                   // removing the last comma from the string
            votesForEachCandidate = votesForEachCandidate.TrimEnd(',', ' '); // removing the last comma from the string
            //totalUsresForEachCampaignVoted = totalUsresForEachCampaignVoted.TrimEnd(',', ' '); // removing the last comma from the string

            var arrayCand = votesForEachCandidate.Split(',');

            for (int i = 0; i < arrayCand.Length; i++)
            {
                total_users = (total_users - Int32.Parse(arrayCand[i]));// get the possiable votes left
            }
            totalUsresForEachCampaignVoted = total_users.ToString();
            dM.Candidates = cand;
            dM.Votes      = votesForEachCandidate;


            // this is just for the testing////////////////////////////DEMO/////////////////DEMO////////////////////////DEMO/////////////
            cand = "Hamid Karzai,Abdullah Abdullah,Ashraf Ghani,Abdul Rashid Dostum,Mohammad Hanif Atmar";
            votesForEachCandidate          = "350000,2100000,2800000,840000,910000";
            totalUsresForEachCampaignVoted = "3000000";
            totalVoters = "10000000";
            //////////////////////////////////////////////DEMO/////////////////////DEMO////////////////////DEMO/////////////////////////


            List <string> data = new List <string>// return this list to the view and extract the values in the script to chart.
            {
                selectedCountry,
                selectedCampaign,
                cand,
                votesForEachCandidate,
                totalUsresForEachCampaignVoted,
                totalVoters
            };

            return(data);  // return the data to the view
        }