CollegesController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. try {
  49. college.educfData.Add("Effectifs", educf_etablissement.etabeffectifannees.Where(e => e.annee == college.Annee.AnneeRentree).Sum(e => e.effectif).ToString());
  50. } catch { college.educfData.Add("Effectifs", "(Données introuvables)"); }
  51. try {
  52. college.educfData.Add("Effectif Bilingue", educf_etablissement.eleveannees.Where(e=>e.annee== college.Annee.AnneeRentree & e.idetablissement == 3 & e.idfiliere == 6).Count().ToString());
  53. } catch { college.educfData.Add("Effectif Bilingue", "(Données introuvables)"); }
  54. try {
  55. college.educfData.Add("Demi-pensionnaires", educf_etablissement.etablissementannees.Where(e=>e.annee== college.Annee.AnneeRentree).First().demipensionnaires.ToString());
  56. } catch { college.educfData.Add("Demi-pensionnaires", "(Données introuvables)"); }
  57. try {
  58. college.educfData.Add("Internes", educf_etablissement.etablissementannees.Where(e=>e.annee== college.Annee.AnneeRentree).First().capaciteeleves.ToString());
  59. } catch { college.educfData.Add("Internes", "(Données introuvables)"); }
  60. try {
  61. college.educfData.Add("Accessibilité handicapés", educf_etablissement.accueilhandicape != 0 ? "Oui" : "Non");
  62. } catch { college.educfData.Add("Accessibilité handicapés", "(Données introuvables)"); }
  63. try {
  64. college.educfData.Add("Capacité Théorique", educf_etablissement.etablissementannees.Where(e => e.annee == college.Annee.AnneeRentree).First().internes.ToString());
  65. } catch { college.educfData.Add("Capacité Théorique", "(Données introuvables)"); }
  66. try {
  67. List<string> liste_niveaux = new List<string>();
  68. foreach(etabeffectifannee effectif in educf_etablissement.etabeffectifannees.Where(e => e.annee == college.Annee.AnneeRentree).OrderBy(e=>e.niveau.ordre))
  69. {
  70. liste_niveaux.Add(effectif.niveau.nom + ": " + effectif.effectif);
  71. }
  72. college.educfData.Add("Niveaux", string.Join(" - ", liste_niveaux));
  73. } catch { college.educfData.Add("Niveaux", "(Données introuvables)"); }
  74. try {
  75. List<string> liste_options = new List<string>();
  76. foreach (etaboptionsannee opt in educf_etablissement.etaboptionsannees)
  77. {
  78. liste_options.Add(opt.matiereoption.nom);
  79. }
  80. college.educfData.Add("Options", string.Join(" - ", liste_options));
  81. } catch { college.educfData.Add("Options", "(Données introuvables)"); }
  82. }
  83. else
  84. {
  85. college.educfData.Add("Données Educ'Facile", "(Données introuvables)");
  86. }
  87. }
  88. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Lecture);
  89. return View(model);
  90. }
  91. // GET: Colleges/Create
  92. public ActionResult Create(int? annee_id)
  93. {
  94. if (annee_id == null)
  95. {
  96. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  97. }
  98. Entity.College college = new Entity.College();
  99. college.Annee_Id = annee_id.Value;
  100. college.TokenId = Guid.NewGuid();
  101. AnneeFactory fact = new AnneeFactory(db);
  102. college.Annee = fact.getById(annee_id);
  103. college.hydrate();
  104. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Creation);
  105. return View("Edit", model);
  106. }
  107. // POST: Colleges/Create
  108. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  109. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  110. [HttpPost]
  111. [ValidateAntiForgeryToken]
  112. public ActionResult Create(Entity.College college)
  113. {
  114. if (ModelState.IsValid)
  115. {
  116. CollegeFactory collegeFactory = new CollegeFactory(db);
  117. collegeFactory.add(ref college);
  118. return RedirectToAction("Details", new { id = college.Id });
  119. }
  120. college.hydrate();
  121. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Creation);
  122. return View("Edit", model);
  123. }
  124. // GET: Colleges/Edit/5
  125. public ActionResult Edit(int? id)
  126. {
  127. CollegeFactory collegeFactory = new CollegeFactory(db);
  128. Entity.College college = collegeFactory.getById(id);
  129. if (college == null)
  130. {
  131. return HttpNotFound();
  132. }
  133. college.hydrate();
  134. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Modification);
  135. return View(model);
  136. }
  137. // POST: Colleges/Edit/5
  138. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  139. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  140. [HttpPost]
  141. [ValidateAntiForgeryToken]
  142. public ActionResult Edit(Entity.College college)
  143. {
  144. if (ModelState.IsValid)
  145. {
  146. CollegeFactory collegeFactory = new CollegeFactory(db);
  147. collegeFactory.update(ref college);
  148. return RedirectToAction("Details", new { Id = college.Id });
  149. }
  150. college.hydrate();
  151. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Modification);
  152. return View(model);
  153. }
  154. // GET: College/Type/5
  155. public ActionResult Type(int? id)
  156. {
  157. if (id == null)
  158. {
  159. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  160. }
  161. CollegeFactory collegeFactory = new CollegeFactory(db);
  162. College col = collegeFactory.getById(id.Value);
  163. if (id == null)
  164. {
  165. return HttpNotFound();
  166. }
  167. col.hydrate();
  168. CollegeViewModel model = new CollegeViewModel(col, db, ModeAcces.Modification);
  169. return View(model);
  170. }
  171. // POST: College/Type/5
  172. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  173. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  174. [HttpPost]
  175. [ValidateAntiForgeryToken]
  176. public ActionResult Type(Entity.College college)
  177. {
  178. if (ModelState.IsValid)
  179. {
  180. CollegeFactory collegeFactory = new CollegeFactory(db);
  181. collegeFactory.update(ref college);
  182. return RedirectToAction("Index", "Restauration", new { annee_id = college.Annee_Id });
  183. }
  184. college.hydrate();
  185. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Modification);
  186. return View(model);
  187. }
  188. // GET: College/Delete/5
  189. public ActionResult Delete(int? id)
  190. {
  191. if (id == null)
  192. {
  193. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  194. }
  195. CollegeFactory collegeFactory = new CollegeFactory(db);
  196. Entity.College college = collegeFactory.getById(id);
  197. if (college == null)
  198. {
  199. return HttpNotFound();
  200. }
  201. college.hydrate();
  202. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Suppression);
  203. return View("Details", model);
  204. }
  205. // POST: College/Delete/5
  206. [HttpPost, ActionName("Delete")]
  207. [ValidateAntiForgeryToken]
  208. public ActionResult DeleteConfirmed(int? id)
  209. {
  210. if (id == null)
  211. {
  212. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  213. }
  214. CollegeFactory collegeFactory = new CollegeFactory(db);
  215. Entity.College college = collegeFactory.getById(id);
  216. int annee_id = college.Annee_Id;
  217. collegeFactory.delete(ref college);
  218. return RedirectToAction("Index", new { annee_id = annee_id });
  219. }
  220. public List<College> colleges_non_importes(int annee_id)
  221. {
  222. List<College> non_importes = new List<College>();
  223. AnneeFactory fact = new AnneeFactory(db);
  224. Annee annee_prec = fact.getById(annee_id - 1);
  225. foreach (College college in annee_prec.Colleges)
  226. {
  227. // Pour chaque college de l'année précédente, on vérifie s'il existe dans l'année en cours en se basant sur le TokenId
  228. if (!db.Colleges.Any(c=> c.TokenId == college.TokenId && c.Annee_Id == annee_id))
  229. {
  230. non_importes.Add(college.flat());
  231. }
  232. }
  233. return non_importes;
  234. }
  235. // GET: Colleges
  236. public ActionResult Import(int? annee_id)
  237. {
  238. if (annee_id == null)
  239. {
  240. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  241. }
  242. ImportCollegeViewModel model = new ImportCollegeViewModel();
  243. model.Annee_Id = annee_id.Value;
  244. model.set_liste(colleges_non_importes(annee_id.Value));
  245. return View(model);
  246. }
  247. [HttpPost]
  248. [ValidateAntiForgeryToken]
  249. public ActionResult Import(int Annee_Id, List<int> colleges_ids, List<bool> selection)
  250. {
  251. CollegeFactory fact = new CollegeFactory(db);
  252. for (int i = 0; i < selection.Count; i++)
  253. {
  254. if (selection[i])
  255. {
  256. int college_id = colleges_ids[i];
  257. College college = fact.getById(college_id);
  258. fact.clone_to_year(college, Annee_Id);
  259. }
  260. }
  261. return RedirectToAction("Index", new { annee_id = Annee_Id });
  262. }
  263. protected override void Dispose(bool disposing)
  264. {
  265. if (disposing)
  266. {
  267. db.Dispose();
  268. }
  269. base.Dispose(disposing);
  270. }
  271. }
  272. }