CollegesController.cs 8.3 KB

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