<commit_before>﻿using LearningSystem.App.ViewModels;
using LearningSystem.Data;
using LearningSystem.Models;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using TeamAzureDragon.Utils;

namespace LearningSystem.App.Controllers
{
    [Authorize]
    public class SkillController : Controller
    {
        IUoWLearningSystem db;
        public SkillController(IUoWLearningSystem db)
        {
            this.db = db;
        }
        //
        // GET: /Skill/
        [Authorize]
        public ActionResult Index(int skillId)
        {
            var user = db.Users.All().Single(x => x.UserName == User.Identity.Name);

            var skill = db.Skills.All().FirstOrDefault(x => x.SkillId == skillId);
            List<LessonViewModel> sortedLessons = new List<LessonViewModel>();
            IEnumerable<Lesson> learnedLessons;

            if (skill != null && skill.Users.Contains(user))
            {
                var lessons = skill.Lessons.ToList();


                learnedLessons = user.Lessons.Where(x => x.SkillId == skill.SkillId && lessons.Any(y => y.LessonId == x.LessonId));


                TopologicalSort(sortedLessons, lessons);

            }
            else
            {
                return View("Error", new ErrorModel
                {
                    ActionName = "Index",
                    ControllerName = "Skill",
                    Exception = new InvalidOperationException("Skill doesn't exist or User is not learning this skill")
                });
            }

            SkillViewModel vm = new SkillViewModel();

            foreach (var item in sortedLessons)
            {
                if (learnedLessons.Any(x => x.LessonId == item.Id))
                {
                    item.IsLearned = true;
                }
            }

            vm.SkillName = skill.Name;
            vm.SkillId = skill.SkillId;
            vm.Lessons = sortedLessons.GroupBy(x => x.LevelInSkillTree);
            return View(vm);
        }

        private void TopologicalSort(List<LessonViewModel> sortedLessons, List<Lesson> lessons)
        {
            Dictionary<Lesson, int> added = new Dictionary<Lesson, int>();
            var parentlessLessons = lessons.Where(x => x.Requirements.Count == 0).ToList();

            sortedLessons.AddRange(parentlessLessons.ToLessonViewModel(0));
            parentlessLessons.ForEach(x => added.Add(x, 0));

            int levelInSkillTree = 1;
            int notInPlace = lessons.Count - added.Count;

            while (notInPlace > 0)
            {
                foreach (var item in lessons)
                {
                    if (!added.ContainsKey(item) && item.Requirements.All(added.ContainsKey))
                    {
                        int thisLevel = 1 + item.Requirements.Max(req => added[req]);
                        added[item] = thisLevel;
                        sortedLessons.Add(item.ToLessonViewModel(thisLevel));
                        notInPlace--;
                    }
                }

                if (levelInSkillTree > notInPlace)
                {
                    break;
                }

                levelInSkillTree++;
            }
        }


        [Authorize]
        public ActionResult SignUpForSkill(int skillId)
        {
            var user = db.Users.All().Single(x => x.UserName == User.Identity.Name);

            var skill = db.Skills.All().FirstOrDefault(x => x.SkillId == skillId);



            if (!user.Skills.Contains(skill))
            {
                user.Skills.Add(skill);
                db.SaveChanges();
                ViewBag.Message = "Skill successfully assigned!";
                ViewBag.Success = true;
            }
            else<commit_msg>removed level in skill tree check<commit_after>﻿using LearningSystem.App.ViewModels;
using LearningSystem.Data;
using LearningSystem.Models;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using TeamAzureDragon.Utils;

namespace LearningSystem.App.Controllers
{
    [Authorize]
    public class SkillController : Controller
    {
        IUoWLearningSystem db;
        public SkillController(IUoWLearningSystem db)
        {
            this.db = db;
        }
        //
        // GET: /Skill/
        [Authorize]
        public ActionResult Index(int skillId)
        {
            var user = db.Users.All().Single(x => x.UserName == User.Identity.Name);

            var skill = db.Skills.All().FirstOrDefault(x => x.SkillId == skillId);
            List<LessonViewModel> sortedLessons = new List<LessonViewModel>();
            IEnumerable<Lesson> learnedLessons;

            if (skill != null && skill.Users.Contains(user))
            {
                var lessons = skill.Lessons.ToList();


                learnedLessons = user.Lessons.Where(x => x.SkillId == skill.SkillId && lessons.Any(y => y.LessonId == x.LessonId));


                TopologicalSort(sortedLessons, lessons);

            }
            else
            {
                return View("Error", new ErrorModel
                {
                    ActionName = "Index",
                    ControllerName = "Skill",
                    Exception = new InvalidOperationException("Skill doesn't exist or User is not learning this skill")
                });
            }

            SkillViewModel vm = new SkillViewModel();

            foreach (var item in sortedLessons)
            {
                if (learnedLessons.Any(x => x.LessonId == item.Id))
                {
                    item.IsLearned = true;
                }
            }

            vm.SkillName = skill.Name;
            vm.SkillId = skill.SkillId;
            vm.Lessons = sortedLessons.GroupBy(x => x.LevelInSkillTree);
            return View(vm);
        }

        public void TopologicalSort(List<LessonViewModel> sortedLessons, List<Lesson> lessons)
        {
            Dictionary<Lesson, int> added = new Dictionary<Lesson, int>();
            var parentlessLessons = lessons.Where(x => x.Requirements.Count == 0).ToList();

            sortedLessons.AddRange(parentlessLessons.ToLessonViewModel(0));
            parentlessLessons.ForEach(x => added.Add(x, 0));

            int notInPlace = lessons.Count - added.Count;

            while (notInPlace > 0)
            {
                foreach (var item in lessons)
                {
                    if (!added.ContainsKey(item) && item.Requirements.All(added.ContainsKey))
                    {
                        int thisLevel = 1 + item.Requirements.Max(req => added[req]);
                        added[item] = thisLevel;
                        sortedLessons.Add(item.ToLessonViewModel(thisLevel));
                        notInPlace--;
                    }
                }
            }
        }


        [Authorize]
        public ActionResult SignUpForSkill(int skillId)
        {
            var user = db.Users.All().Single(x => x.UserName == User.Identity.Name);

            var skill = db.Skills.All().FirstOrDefault(x => x.SkillId == skillId);



            if (!user.Skills.Contains(skill))
            {
                user.Skills.Add(skill);
                db.SaveChanges();
                ViewBag.Message = "Skill successfully assigned!";
                ViewBag.Success = true;
            }
            else
{'fraction_non_alphanumeric': 0.10154496236630134, 'fraction_numerical': 0.0014525287204542453, 'mean_word_length': 1.5744391570360299, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1558725', 'n_tokens_mistral': 2081, 'n_tokens_neox': 1969, 'n_words': 394}