CollegesController.cs 13 KB

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