CollegesController.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Net;
  2. using System.Web.Mvc;
  3. using CD67.FicheCollege.Entity;
  4. using CD67.FicheCollege.Factory;
  5. using CD67.FicheCollege.MVC.Models;
  6. using System.Linq;
  7. using System.Collections.Generic;
  8. namespace CD67.FicheCollege.MVC.Controllers
  9. {
  10. public class CollegesController : Controller
  11. {
  12. private Entities db = new Entities();
  13. // GET: Colleges
  14. public ActionResult Index(int? annee_id)
  15. {
  16. if (annee_id == null)
  17. {
  18. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  19. }
  20. AnneeFactory fact = new AnneeFactory(db);
  21. Annee annee = fact.getById(annee_id);
  22. AnneeViewModel model = new AnneeViewModel(annee, db, ModeAcces.Lecture);
  23. return View(model);
  24. }
  25. // GET: Colleges/Details/5
  26. public ActionResult Details(int? id)
  27. {
  28. if (id == null)
  29. {
  30. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  31. }
  32. CollegeFactory collegeFactory = new CollegeFactory(db);
  33. Entity.College college = collegeFactory.getById(id);
  34. if (college == null)
  35. {
  36. return HttpNotFound();
  37. }
  38. college.hydrate();
  39. // Chargement des données Educ'facile
  40. if (!string.IsNullOrEmpty(college.CodeRne))
  41. {
  42. EducfEntities educfdb = new EducfEntities();
  43. EducfEtablissementFactory educfFact = new EducfEtablissementFactory(educfdb);
  44. etablissement educf_etablissement = educfFact.getByRne(college.CodeRne);
  45. if (educf_etablissement != null)
  46. {
  47. college.educfData.Add("Effectifs", educf_etablissement.etabeffectifannees.Where(e=>e.annee== college.Annee.AnneeRentree).Sum(e => e.effectif).ToString());
  48. college.educfData.Add("Effectif Bilingue", educf_etablissement.eleveannees.Where(e=>e.annee== college.Annee.AnneeRentree & e.idetablissement == 3 & e.idfiliere == 6).Count().ToString());
  49. college.educfData.Add("Demi-pensionnaires", educf_etablissement.etablissementannees.Where(e=>e.annee== college.Annee.AnneeRentree).First().demipensionnaires.ToString());
  50. college.educfData.Add("Internes", educf_etablissement.etablissementannees.Where(e=>e.annee== college.Annee.AnneeRentree).First().capaciteeleves.ToString());
  51. college.educfData.Add("Accessibilité handicapés", educf_etablissement.accueilhandicape != 0 ? "Oui" : "Non");
  52. college.educfData.Add("Capacité Théorique", educf_etablissement.etablissementannees.Where(e => e.annee == college.Annee.AnneeRentree).First().internes.ToString());
  53. List<string> liste_niveaux = new List<string>();
  54. foreach(etabeffectifannee effectif in educf_etablissement.etabeffectifannees.Where(e => e.annee == college.Annee.AnneeRentree).OrderBy(e=>e.niveau.ordre))
  55. {
  56. liste_niveaux.Add(effectif.niveau.nom + ": " + effectif.effectif);
  57. }
  58. college.educfData.Add("Niveaux", string.Join(" - ", liste_niveaux));
  59. List<string> liste_options = new List<string>();
  60. foreach (etaboptionsannee opt in educf_etablissement.etaboptionsannees)
  61. {
  62. liste_options.Add(opt.matiereoption.nom);
  63. }
  64. college.educfData.Add("Options", string.Join(" - ", liste_options));
  65. }
  66. else
  67. {
  68. college.educfData.Add("Données Educ'Facile", "(Données introuvables)");
  69. }
  70. }
  71. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Lecture);
  72. return View(model);
  73. }
  74. // GET: Colleges/Create
  75. public ActionResult Create(int? annee_id)
  76. {
  77. if (annee_id == null)
  78. {
  79. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  80. }
  81. Entity.College college = new Entity.College();
  82. college.Annee_Id = annee_id.Value;
  83. AnneeFactory fact = new AnneeFactory(db);
  84. college.Annee = fact.getById(annee_id);
  85. college.hydrate();
  86. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Creation);
  87. return View("Edit", model);
  88. }
  89. // POST: Colleges/Create
  90. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  91. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  92. [HttpPost]
  93. [ValidateAntiForgeryToken]
  94. public ActionResult Create(Entity.College college)
  95. {
  96. if (ModelState.IsValid)
  97. {
  98. CollegeFactory collegeFactory = new CollegeFactory(db);
  99. collegeFactory.add(ref college);
  100. return RedirectToAction("Details", new { id = college.Id });
  101. }
  102. college.hydrate();
  103. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Creation);
  104. return View("Edit", model);
  105. }
  106. // GET: Colleges/Edit/5
  107. public ActionResult Edit(int? id)
  108. {
  109. CollegeFactory collegeFactory = new CollegeFactory(db);
  110. Entity.College college = collegeFactory.getById(id);
  111. if (college == null)
  112. {
  113. return HttpNotFound();
  114. }
  115. college.hydrate();
  116. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Modification);
  117. return View(model);
  118. }
  119. // POST: Colleges/Edit/5
  120. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  121. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  122. [HttpPost]
  123. [ValidateAntiForgeryToken]
  124. public ActionResult Edit(Entity.College college)
  125. {
  126. if (ModelState.IsValid)
  127. {
  128. CollegeFactory collegeFactory = new CollegeFactory(db);
  129. collegeFactory.update(ref college);
  130. return RedirectToAction("Details", new { Id = college.Id });
  131. }
  132. college.hydrate();
  133. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Modification);
  134. return View(model);
  135. }
  136. // GET: College/Delete/5
  137. public ActionResult Delete(int? id)
  138. {
  139. if (id == null)
  140. {
  141. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  142. }
  143. CollegeFactory collegeFactory = new CollegeFactory(db);
  144. Entity.College college = collegeFactory.getById(id);
  145. if (college == null)
  146. {
  147. return HttpNotFound();
  148. }
  149. college.hydrate();
  150. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Suppression);
  151. return View("Details", model);
  152. }
  153. // POST: College/Delete/5
  154. [HttpPost, ActionName("Delete")]
  155. [ValidateAntiForgeryToken]
  156. public ActionResult DeleteConfirmed(int? id)
  157. {
  158. if (id == null)
  159. {
  160. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  161. }
  162. CollegeFactory collegeFactory = new CollegeFactory(db);
  163. Entity.College college = collegeFactory.getById(id);
  164. int annee_id = college.Annee_Id;
  165. collegeFactory.delete(ref college);
  166. return RedirectToAction("Index", new { annee_id = annee_id });
  167. }
  168. protected override void Dispose(bool disposing)
  169. {
  170. if (disposing)
  171. {
  172. db.Dispose();
  173. }
  174. base.Dispose(disposing);
  175. }
  176. }
  177. }