<commit_before>﻿using ChalmersxTools.Database;
using ChalmersxTools.Models;
using ChalmersxTools.Models.Database;
using ChalmersxTools.Models.View;
using ChalmersxTools.Sessions;
using LtiLibrary.Core.Outcomes.v1;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChalmersxTools.Tools
{
    public class PresentationTool : SimpleDataStorageToolBase
    {
        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static readonly string CONSUMER_KEY = "ChalmersxPresentationTool";

        public override string ConsumerKey { get { return CONSUMER_KEY; } }
        override protected string ConsumerSecret { get { return ConfigurationManager.AppSettings["ltiConsumerSecret"]; } }

        public override CsvFileData HandleDataRequest()
        {
            string data = "";
            var presentations = GetAllStudentPresentationsForCourseRun();
            data += "name,presentation,location,latitude,longitude\n";
            foreach (var presentation in presentations)
            {
                data += "\"" + presentation.Name.Replace('"', '\'').Replace("\r", @"\r").Replace("\n", @"\n") + "\",\"" +
                    presentation.Presentation.Replace('"', '\'').Replace("\r", @"\r").Replace("\n", @"\n") + "\",\"" +
                    presentation.LocationName.Replace('"', '\'').Replace("\r", @"\r").Replace("\n", @"\n") + "\",\"" +
                    presentation.LocationLat.ToString() + "\",\"" +
                    presentation.LocationLong.ToString() + "\"\n";
            }
            return new CsvFileData(_session.ContextId + "-presentations.csv",
                new System.Text.UTF8Encoding().GetBytes(data));
        }

        protected override string Create(HttpRequestBase request)
        {
            var res = "";
            try
            {
                _sessionManager.DbContext.StudentPresentations.Add(new StudentPresentation()
                {
                    UserId = _session.UserId,
                    ContextId = _session.ContextId,
                    Name = request.Form["name"].ToString(),
                    LocationName = System.Web.HttpUtility.HtmlEncode(request.Form["location"].ToString()),
                    LocationLat = Double.Parse(request.Form["latitude"].ToString(), CultureInfo.InvariantCulture),
                    LocationLong = Double.Parse(request.Form["longitude"].ToString(), CultureInfo.InvariantCulture),
                    Presentation = System.Web.HttpUtility.HtmlEncode(request.Form["presentation"].ToString())
                });

                _sessionManager.DbContext.SaveChanges();

                OutcomesClient.PostScore(
                    _session.LtiRequest.LisOutcomeServiceUrl,
                    _session.LtiRequest.ConsumerKey,
                    ConsumerSecret,
                    _session.LtiRequest.LisResultSourcedId,
                    1.0);
            }
            catch (Exception e)
            {
                _log.Error("Failed to create student presentation: " + e.Message);
                throw new Exception("Failed to create student presentation.", e);
            }
            return res;
        }

        protected override string Edit(HttpRequestBase request)
        {
            var res = "";

            try
            {
                StudentPresentation existingStudentPresentation =
                    (from sp in _sessionManager.DbContext.StudentPresentations
                     where sp.UserId == _session.UserId &&
                     sp.ContextId == _session.ContextId
                     select sp).SingleOrDefault();

                existingStudentPresentation.Name = request.Form["name"].ToString();
                existingStudentPresentation.LocationName = request.Form["location"].ToString();
                existingStudentPresentation.LocationLat = Double.Parse(request.Form["latitude"].ToString(), CultureInfo.InvariantCulture);
                existingStudentPresentation.LocationLong = Double.Parse(request.Form["longitude"].ToString(), CultureInfo.InvariantCulture);
                existingStudentPresentation.Presentation = request.Form["presentation"].ToString();<commit_msg>HTML encode on edit also.<commit_after>﻿using ChalmersxTools.Database;
using ChalmersxTools.Models;
using ChalmersxTools.Models.Database;
using ChalmersxTools.Models.View;
using ChalmersxTools.Sessions;
using LtiLibrary.Core.Outcomes.v1;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChalmersxTools.Tools
{
    public class PresentationTool : SimpleDataStorageToolBase
    {
        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static readonly string CONSUMER_KEY = "ChalmersxPresentationTool";

        public override string ConsumerKey { get { return CONSUMER_KEY; } }
        override protected string ConsumerSecret { get { return ConfigurationManager.AppSettings["ltiConsumerSecret"]; } }

        public override CsvFileData HandleDataRequest()
        {
            string data = "";
            var presentations = GetAllStudentPresentationsForCourseRun();
            data += "name,presentation,location,latitude,longitude\n";
            foreach (var presentation in presentations)
            {
                data += "\"" + presentation.Name.Replace('"', '\'').Replace("\r", @"\r").Replace("\n", @"\n") + "\",\"" +
                    presentation.Presentation.Replace('"', '\'').Replace("\r", @"\r").Replace("\n", @"\n") + "\",\"" +
                    presentation.LocationName.Replace('"', '\'').Replace("\r", @"\r").Replace("\n", @"\n") + "\",\"" +
                    presentation.LocationLat.ToString() + "\",\"" +
                    presentation.LocationLong.ToString() + "\"\n";
            }
            return new CsvFileData(_session.ContextId + "-presentations.csv",
                new System.Text.UTF8Encoding().GetBytes(data));
        }

        protected override string Create(HttpRequestBase request)
        {
            var res = "";
            try
            {
                _sessionManager.DbContext.StudentPresentations.Add(new StudentPresentation()
                {
                    UserId = _session.UserId,
                    ContextId = _session.ContextId,
                    Name = request.Form["name"].ToString(),
                    LocationName = System.Web.HttpUtility.HtmlEncode(request.Form["location"].ToString()),
                    LocationLat = Double.Parse(request.Form["latitude"].ToString(), CultureInfo.InvariantCulture),
                    LocationLong = Double.Parse(request.Form["longitude"].ToString(), CultureInfo.InvariantCulture),
                    Presentation = System.Web.HttpUtility.HtmlEncode(request.Form["presentation"].ToString())
                });

                _sessionManager.DbContext.SaveChanges();

                OutcomesClient.PostScore(
                    _session.LtiRequest.LisOutcomeServiceUrl,
                    _session.LtiRequest.ConsumerKey,
                    ConsumerSecret,
                    _session.LtiRequest.LisResultSourcedId,
                    1.0);
            }
            catch (Exception e)
            {
                _log.Error("Failed to create student presentation: " + e.Message);
                throw new Exception("Failed to create student presentation.", e);
            }
            return res;
        }

        protected override string Edit(HttpRequestBase request)
        {
            var res = "";

            try
            {
                StudentPresentation existingStudentPresentation =
                    (from sp in _sessionManager.DbContext.StudentPresentations
                     where sp.UserId == _session.UserId &&
                     sp.ContextId == _session.ContextId
                     select sp).SingleOrDefault();

                existingStudentPresentation.Name = request.Form["name"].ToString();
                existingStudentPresentation.LocationName = request.Form["location"].ToString();
                existingStudentPresentation.LocationLat = Double.Parse(request.Form["latitude"].ToString(), CultureInfo.InvariantCulture);
                existingStudentPresentation.LocationLong = Double.Parse(request.Form["longitude"].ToString(), CultureInfo.InvariantCulture);
                existingStudentPresentation.Presentation = System.Web.HttpUtility.HtmlEncode(request.Form["presentation"].ToString());
{'fraction_non_alphanumeric': 0.11494252873563218, 'fraction_numerical': 0.0013656538067599864, 'mean_word_length': 2.2155140870837906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3506150', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2047, 'n_words': 363}