// GET: Professor/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var professor = await _context.Professor
                            .Include(p => p.Reviews)
                            .ThenInclude(r => r.Author)
                            .Include(p => p.Reviews)
                            .ThenInclude(r => r.ProfessorReviewTagSet)
                            .ThenInclude(set => set.ProfessorReviewTag)
                            .Include(p => p.Reviews)
                            .ThenInclude(r => r.Comments)
                            .Include(s => s.Subjects)
                            .ThenInclude(s => s.Subject)
                            .ThenInclude(s => s.SubjectTagSet)
                            .ThenInclude(s => s.SubjectTag)
                            .Include(s => s.Subjects)
                            .ThenInclude(s => s.Subject)
                            .ThenInclude(s => s.Faculty)
                            .ThenInclude(f => f.University)
                            .ThenInclude(u => u.City)
                            .SingleOrDefaultAsync(m => m.ID == id);

            if (professor == null)
            {
                return(NotFound());
            }

            if (!professor.Validated && !await _authorizationService.AuthorizeAsync(User, "CanModerate"))
            {
                return(NotFound());
            }

            var faculties = professor.Subjects.Select(s => s.Subject).Select(s => s.Faculty).Distinct();
            var subjects  = professor.Subjects.Select(set => set.Subject);

            var gradesDict = new Dictionary <string, GradeInfo>();

            var AvgAccessibility = calcAccessibility(professor.Reviews);

            gradesDict.Add(
                "Pristupaènost",
                new GradeInfo {
                Grade = AvgAccessibility
            }
                );
            var AvgInteractivity = calcInteractivity(professor.Reviews);

            gradesDict.Add(
                "Angažiranost",
                new GradeInfo {
                Grade = AvgInteractivity
            }
                );
            var AvgMentoring = calcMentoring(professor.Reviews);

            gradesDict.Add(
                "Mentorstvo",
                new GradeInfo {
                Grade = AvgMentoring
            }
                );
            var AvgQuality = calcQuality(professor.Reviews);

            gradesDict.Add(
                "Kvaliteta",
                new GradeInfo {
                Grade = AvgQuality
            }
                );
            var AvgTotal = calcTotalGrade(professor.Reviews);

            gradesDict.Add(
                "Ukupna ocjena",
                new GradeInfo {
                Grade = AvgTotal
            }
                );
            var model = new ProfessorDetailViewModel
            {
                ID               = professor.ID,
                FirstName        = professor.FirstName,
                LastName         = professor.LastName,
                Reviews          = professor.Reviews.OrderBy(r => r.Points).Take(2).ToList(),
                Subjects         = subjects.ToList(),
                Biography        = professor.Biography,
                Validated        = professor.Validated,
                AvgAccessibility = AvgAccessibility,
                AvgInteractivity = AvgInteractivity,
                AvgMentoring     = AvgMentoring,
                AvgTotal         = AvgTotal,
                AvgQuality       = AvgQuality,
                Faculties        = faculties.ToList(),
                Universities     = faculties.Select(f => f.University).Distinct().ToList(),
                Interests        = subjects.SelectMany(s => s.SubjectTagSet).Select(s => s.SubjectTag).Distinct().ToList(),
                Grades           = gradesDict
            };

            return(View(model));
        }
示例#2
0
        public IActionResult ProfessorRankings(int sort = 5, int tag = -1)
        {
            var query = _context.Professor
                        .Include(p => p.Reviews)
                        .Include(s => s.Subjects)
                        .ThenInclude(s => s.Subject)
                        .ThenInclude(s => s.SubjectTagSet)
                        .ThenInclude(s => s.SubjectTag)
                        .AsNoTracking().Where(p => p.Reviews.Count > 0 && p.Validated).ToList();
            List <ProfessorDetailViewModel> profList = new List <ProfessorDetailViewModel>();

            foreach (var professor in query)
            {
                var subjects = professor.Subjects.Select(p => p.Subject);
                var model    = new ProfessorDetailViewModel
                {
                    ID               = professor.ID,
                    FirstName        = professor.FirstName,
                    LastName         = professor.LastName,
                    Reviews          = professor.Reviews,
                    Subjects         = subjects.ToList(),
                    Biography        = professor.Biography,
                    Validated        = professor.Validated,
                    Interests        = subjects.SelectMany(s => s.SubjectTagSet).Select(s => s.SubjectTag).Distinct().ToList(),
                    AvgAccessibility = calcAccessibility(professor.Reviews),
                    AvgInteractivity = calcInteractivity(professor.Reviews),
                    AvgMentoring     = calcMentoring(professor.Reviews),
                    AvgTotal         = calcTotalGrade(professor.Reviews),
                    AvgQuality       = calcQuality(professor.Reviews)
                };

                profList.Add(model);
            }

            if (tag != -1)
            {
                profList = profList.Where(p => p.Interests.Any(i => i.ID == tag)).ToList();
            }

            System.Linq.Expressions.Expression <Func <ProfessorDetailViewModel, object> > orderSelector = null;
            switch (sort)
            {
            case 1:
                orderSelector = d => d.AvgAccessibility;
                break;

            case 2:
                orderSelector = d => d.AvgInteractivity;
                break;

            case 3:
                orderSelector = d => d.AvgMentoring;
                break;

            case 4:
                orderSelector = d => d.AvgQuality;
                break;

            case 5:
                orderSelector = d => d.AvgTotal;
                break;

            default:
                orderSelector = d => d.AvgTotal;
                break;
            }

            var q      = profList.AsQueryable();
            var sorted = q.OrderByDescending(orderSelector).Take(10).ToList();

            var selectList = new SelectList(_context.SubjectTag, "ID", "Name");

            if (tag != -1)
            {
                var selected = selectList.Where(i => i.Value.Equals(tag.ToString())).First();
                selected.Selected = true;
            }

            ViewData["SubjectTag"] = selectList;
            return(View(new ProfessorRankingViewModel {
                Professors = sorted, sort = sort, tag = tag
            }));
        }