-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoursController.cs
286 lines (245 loc) · 9.55 KB
/
CoursController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FinalProject.Data.EF;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using FinalProject.UI.MVC.Models;
namespace FinalProject.UI.MVC.Controllers
{
public class CoursController : Controller
{
private FinalProjectEntities db = new FinalProjectEntities();
// GET: Cours
public ActionResult Index()
{
var currentUser = User.Identity.GetUserId();
//var courseviews = db.CourseViews
// .Where(x => x.UserID == currentUser);
//var courses = db.Courses
// .Where(x => !x.CourseViews.Any(y => y.CourseID == x.CourseID));
// get count of courses
decimal courseCount = db.Courses.Count();
// get count of courses completed
decimal lessonCount = db.CourseViews.
Where(x => x.UserID == currentUser).Count();
decimal percentage = (lessonCount / courseCount) * 100;
percentage = (int)percentage;
ViewBag.Percentage = percentage;
return View(db.Courses.ToList());
}
public ActionResult TopClasses()
{
return View();
}
[Authorize]
public ActionResult RemainingCourses()
{
var currentUser = User.Identity.GetUserId();
var courseviews = db.CourseViews
.Where(x => x.UserID == currentUser);
var courses = db.Courses
.Where(x => !courseviews.Any(y => y.CourseID == x.CourseID));
#region Calculate percentage completed
decimal courseCount = db.Courses.Count();
// get count of courses completed
decimal lessonCount = db.CourseViews.
Where(x => x.UserID == currentUser).Count();
decimal percentage = (lessonCount / courseCount) * 100;
percentage = (int)percentage;
ViewBag.Percentage = percentage;
#endregion
return View(courses.ToList());
// return all courses
//return View(db.Courses.ToList());
}
[Authorize]
public ActionResult Congratulations()
{
var userManager = System.Web.HttpContext.Current.
GetOwinContext().GetUserManager<ApplicationUserManager>();
//var user = userManager.FindById(item.UserID);
var currentUser = User.Identity.GetUserId();
ViewBag.Name = Session["FirstName"] + " " + Session["LastName"];
//ViewBag.UserID = user.LastName + ", " + user.FirstName;
//ViewBag.UserID = currentUser
//ViewBag.Name = "Larry";
return View();
}
// GET: Cours/Details/5
[Authorize]
public ActionResult Details(int? id)
{
if (id == null)
{
Response.Redirect("/error.html");
//return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Cours cours = db.Courses.Find(id);
if (cours == null)
{
Response.Redirect("/error.html");
//return HttpNotFound();
}
var userID = User.Identity.GetUserId();
CourseView cv = new CourseView();
var hasViewed = db.CourseViews.ToList()
.Where(x => x.UserID == userID)
.Where(x => x.CourseID == id).Count();
cv.UserID = userID;
cv.CourseID = id ?? default(int);
cv.DateViewed = DateTime.Now;
if (hasViewed == 0)
{
db.CourseViews.Add(cv);
db.SaveChanges();
}
return View(cours);
}
// GET: Cours/Create
[Authorize(Roles = "Admin")]
public ActionResult Create()
{
return View();
}
// POST: Cours/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[Authorize(Roles = "Admin")]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CourseID,CourseName,CourseDescription,VideoURL,coursePDF,IsActive")] Cours cours, HttpPostedFileBase coursePDF)
{
if (ModelState.IsValid)
{
string pdfName = "";
// if a file was sent
if(coursePDF != null)
{
// reassign the variable to the filename sent over
pdfName = coursePDF.FileName;
// create a variable for the extension
string ext = pdfName.Substring(pdfName.LastIndexOf('.'));
// create a list of valid extensions
string[] goodExts = {".pdf"};
// if our extension is valid, assign a GUID as the name
// and concatonate the extension
if(goodExts.Contains(ext.ToLower()))
{
// save the file to the web server
coursePDF.SaveAs(Server.MapPath("~/Content/Images/PDF/" + pdfName));
cours.PDF = pdfName;
}
else
{
// default
pdfName = "";
}
}
cours.IsActive = true;
db.Courses.Add(cours);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(cours);
}
// GET: Cours/Edit/5
[Authorize(Roles = "Admin, Manager")]
public ActionResult Edit(int? id)
{
if (id == null)
{
Response.Redirect("/error.html");
//return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Cours cours = db.Courses.Find(id);
if (cours == null)
{
Response.Redirect("/error.html");
//return HttpNotFound();
}
return View(cours);
}
// POST: Cours/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[Authorize(Roles = "Admin, Manager")]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "CourseID,CourseName,CourseDescription,VideoURL,coursePDF,IsActive")] Cours cours, HttpPostedFileBase coursePDF)
{
if (ModelState.IsValid)
{
string pdfName = "";
// if a file was sent
if (coursePDF != null)
{
// reassign the variable to the filename sent over
pdfName = coursePDF.FileName;
// create a variable for the extension
string ext = pdfName.Substring(pdfName.LastIndexOf('.'));
// create a list of valid extensions
string[] goodExts = { ".pdf" };
// if our extension is valid, assign a GUID as the name
// and concatonate the extension
if (goodExts.Contains(ext.ToLower()))
{
// save the file to the web server
coursePDF.SaveAs(Server.MapPath("~/Content/Images/PDF/" + pdfName));
cours.PDF = pdfName;
}
else
{
// default
pdfName = "";
}
}
db.Courses.Add(cours);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(cours);
}
// GET: Cours/Delete/5
[Authorize(Roles = "Admin")]
public ActionResult Delete(int? id)
{
if (id == null)
{
Response.Redirect("/error.html");
//return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Cours cours = db.Courses.Find(id);
if (cours == null)
{
Response.Redirect("/error.html");
//return HttpNotFound();
}
return View(cours);
}
// POST: Cours/Delete/5
[HttpPost, ActionName("Delete")]
[Authorize(Roles = "Admin")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Cours cours = db.Courses.Find(id);
db.Courses.Remove(cours);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}