Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ClosedXML.Excel;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace GestionCoursWeb
- {
- public partial class PageHome : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Tab1.CssClass = "Clicked";
- Tab2.CssClass = "Initial";
- Tab3.CssClass = "Initial";
- Tab4.CssClass = "Initial";
- MainView.ActiveViewIndex = 0;
- }
- protected void Tab1_Click(object sender, EventArgs e)
- {
- Tab1.CssClass = "Clicked";
- Tab2.CssClass = "Initial";
- Tab3.CssClass = "Initial";
- Tab4.CssClass = "Initial";
- MainView.ActiveViewIndex = 0;
- }
- protected void Tab2_Click(object sender, EventArgs e)
- {
- Tab1.CssClass = "Initial";
- Tab2.CssClass = "Clicked";
- Tab3.CssClass = "Initial";
- Tab4.CssClass = "Initial";
- MainView.ActiveViewIndex = 1;
- }
- public List<Cours> recupererLesDonees()
- {
- List<Cours> listeDesCours = new List<Cours>();
- GridView gridViewHome = GridViewHome;
- int lignes = gridViewHome.Rows.Count - 1;
- int colonnes = gridViewHome.Columns.Count;
- // Pour chaque lignes
- for (int i = 0; i < gridViewHome.Rows.Count - 1; i++)
- {
- String nomDuCours = "";
- String nomDuBloc = "";
- List<DateTime> datesDuCours = new List<DateTime>();
- // Pour chaque colonnes
- for (int j = 0; j < gridViewHome.Rows[i].Cells.Count; j++)
- {
- if (gridViewHome.Rows[i].Cells[j].Text != null)
- {
- // Valeur de la case actuelle
- String currentValue = gridViewHome.Rows[i].Cells[j].Text.ToString();
- DateTime dateTime;
- // Si c'est la première case, on récupère le nom du bloc
- if (j == 0 && currentValue != nomDuBloc)
- {
- nomDuBloc = currentValue;
- }
- // A la deuxieme case eon recupere le nom du cours
- else if (j == 1) nomDuCours = currentValue;
- // Sinon on récupère la date si c'est possible
- else if (DateTime.TryParse(currentValue, out dateTime)) datesDuCours.Add(Convert.ToDateTime(currentValue));
- }
- }
- // On ajoute le cours à la liste des cours
- if (datesDuCours.Count > 0 && !String.IsNullOrEmpty(nomDuCours)) listeDesCours.Add(new Cours(nomDuCours, datesDuCours, new Bloc(nomDuBloc)));
- }
- System.Diagnostics.Debug.WriteLine(listeDesCours.Count);
- return listeDesCours;
- }
- protected void Tab3_Click(object sender, EventArgs e)
- {
- Tab1.CssClass = "Initial";
- Tab2.CssClass = "Initial";
- Tab3.CssClass = "Clicked";
- Tab4.CssClass = "Initial";
- MainView.ActiveViewIndex = 2;
- }
- protected void Tab4_Click(object sender, EventArgs e)
- {
- Tab1.CssClass = "Initial";
- Tab2.CssClass = "Initial";
- Tab3.CssClass = "Initial";
- Tab4.CssClass = "Clicked";
- MainView.ActiveViewIndex = 3;
- // Liste des cours récupérés
- List<Cours> listeDesCours = organiserUneListeDeCours(recupererLesDonees());
- System.Diagnostics.Debug.WriteLine(listeDesCours.Count);
- DataTable dt = new DataTable();
- dt.Columns.Add("Bloc");
- dt.Columns.Add("Module");
- dt.Columns.Add("Cours");
- dt.Columns.Add("Date");
- String leDernierBloc = listeDesCours[0].bloc.nom;
- int row = 0;
- foreach (Cours cour in listeDesCours)
- {
- if(leDernierBloc != cour.bloc.nom)
- {
- dt.Rows.Add();
- dt.Rows[row][0] = "#";
- row++;
- leDernierBloc = cour.bloc.nom;
- }
- dt.Rows.Add();
- dt.Rows[row][0] = cour.bloc.nbBloc;
- dt.Rows[row][1] = cour.bloc.nom;
- dt.Rows[row][2] = cour.nom;
- dt.Rows[row][3] = cour.dateChoisie.ToString("dd/MM/yyyy");
- row++;
- }
- GridViewRes3.DataSource = dt;
- GridViewRes3.DataBind();
- }
- public List<Cours> organiserUneListeDeCours(List<Cours> listeDesCours)
- {
- // valeur qui va être retournée
- List<Cours> sortie = new List<Cours>();
- // pour tous les cours
- for (int cour = 0; cour < listeDesCours.Count;)
- {
- // tableau qui contient les potentiel doublons
- List<Cours> tableauAvecLesDoublons = listeDesCours.Where(c => c.dateChoisie == listeDesCours[cour].dateChoisie).ToList();
- // on enlève le cours d'origine qui est forcement dans le tableau car x = x
- tableauAvecLesDoublons.Remove(listeDesCours[cour]);
- // si il reste des doublons
- if (tableauAvecLesDoublons.Count > 0)
- {
- // on récupère l'index de la date qui est en doublon
- int index = listeDesCours[cour].dates.IndexOf(listeDesCours[cour].dateChoisie);
- // si il reste une date à attribuer
- if (listeDesCours[cour].dates.Count - 1 > index)
- {
- // on attribue une nouvelle date au cours
- listeDesCours[cour].dateChoisie = listeDesCours[cour].dates[++index];
- }
- else
- {
- // si c'était déjà la dernière date, on met la première
- listeDesCours[cour].dateChoisie = listeDesCours[cour].dates[0];
- }
- // on recommence la detection des doublons au cas ou il s'en ai reformé
- cour = 0;
- }
- else
- {
- // si il y a aucun doublons, on continue la recherche
- cour++;
- }
- }
- sortie = listeDesCours.OrderBy(x => x.dateChoisie).ToList();
- String leDernierBloc = "fef";
- int nombreDeBLoc = 0;
- List<List<Cours>> listeDeListeDeCours = new List<List<Cours>>();
- for(int cour = 0; cour < sortie.Count; cour++)
- {
- if(leDernierBloc != sortie[cour].bloc.nom)
- {
- listeDeListeDeCours.Add(new List<Cours>());
- listeDeListeDeCours[listeDeListeDeCours.Count - 1].Add(sortie[cour]);
- leDernierBloc = sortie[cour].bloc.nom;
- }
- else
- {
- listeDeListeDeCours[listeDeListeDeCours.Count - 1].Add(sortie[cour]);
- }
- }
- foreach (Cours cr in listeDeListeDeCours[5])
- {
- System.Diagnostics.Debug.WriteLine(cr);
- }
- List<List<Cours>> listeFinale = new List<List<Cours>>();
- for (int listeDeCours = 0; listeDeCours < listeDeListeDeCours.Count; listeDeCours++)
- {
- List<Cours> listeActuelle = listeDeListeDeCours[listeDeCours];
- if (listeDeCours == 0)
- {
- listeFinale.Add(listeActuelle);
- continue;
- }
- if (listeDeCours == listeDeListeDeCours.Count - 1)
- {
- listeFinale.Add(listeActuelle);
- continue;
- }
- List<Cours> listeDavant = listeDeListeDeCours[listeDeCours - 1];
- List<Cours> listeDapres = listeDeListeDeCours[listeDeCours + 1];
- if (listeDavant[0].bloc.nom == listeDapres[0].bloc.nom)
- {
- if (listeDapres.Count >= listeActuelle.Count)
- {
- listeFinale[listeFinale.Count - 1] = listeDavant.Concat(listeDapres).ToList();
- }
- }
- else if (listeDavant[0].bloc.nom != listeDapres[0].bloc.nom)
- {
- listeFinale.Add(listeActuelle);
- }
- }
- List<Cours> caVaSortir = new List<Cours>();
- foreach(List<Cours> listeCours in listeFinale)
- {
- caVaSortir = caVaSortir.Concat(listeCours).ToList();
- }
- return caVaSortir;
- }
- protected void btnLoadData_Click(object sender, EventArgs e)
- {
- using (XLWorkbook workbook = new XLWorkbook(FileUpload1.PostedFile.InputStream))
- {
- IXLWorksheet sheet = workbook.Worksheet(1);
- DataTable dt = new DataTable();
- bool firstRow = true;
- foreach (IXLRow row in sheet.Rows())
- {
- if (firstRow)
- {
- foreach (IXLCell cell in row.Cells())
- {
- dt.Columns.Add(cell.Value.ToString());
- }
- firstRow = false;
- }
- else
- {
- dt.Rows.Add();
- int i = 0;
- foreach (IXLCell cell in row.Cells())
- {
- dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
- i++;
- }
- }
- GridViewHome.DataSource = dt;
- GridViewHome.DataBind();
- }
- }
- }
- protected void MainView_ActiveViewChanged(object sender, EventArgs e)
- {
- }
- protected void GridViewRes2_SelectedIndexChanged(object sender, EventArgs e)
- {
- }
- }
- public class Cours
- {
- public String nom
- {
- get;
- set;
- }
- public List<DateTime> dates
- {
- get;
- set;
- }
- public DateTime dateChoisie
- {
- get;
- set;
- }
- public Bloc bloc
- {
- get;
- set;
- }
- public Cours(String nom, List<DateTime> dates, Bloc bloc)
- {
- this.nom = nom;
- this.dates = dates;
- this.dateChoisie = dates[0];
- this.bloc = bloc;
- }
- public override string ToString()
- {
- String ret = "cours = " + this.nom + " ; dates = ";
- foreach (DateTime date in this.dates)
- {
- ret += " { " + date.ToString() + " } ";
- }
- ret += " dateChoisie = " + this.dateChoisie;
- return ret;
- }
- }
- public class Bloc
- {
- public String nom
- {
- get;
- set;
- }
- public int nbBloc
- {
- get;
- set;
- }
- public Bloc(String nom)
- {
- this.nom = nom;
- this.nbBloc = 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement