From bfc96328c427087990966311e72dddc07d34f404 Mon Sep 17 00:00:00 2001 From: Thomas Hille Date: Tue, 28 Feb 2017 15:26:36 +0100 Subject: [PATCH] added interventions back to code. added post method to be able to save interventions --- app/assets/javascripts/editor/editor.js.erb | 18 ++++++++++++++---- app/controllers/exercises_controller.rb | 13 +++++++++++-- app/models/exercise.rb | 5 +++-- app/models/proxy_exercise.rb | 2 +- app/policies/exercise_policy.rb | 2 +- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/editor/editor.js.erb b/app/assets/javascripts/editor/editor.js.erb index 0e644d7d..3249fe9d 100644 --- a/app/assets/javascripts/editor/editor.js.erb +++ b/app/assets/javascripts/editor/editor.js.erb @@ -545,12 +545,14 @@ configureEditors: function () { $('#output_sidebar_collapsed').addClass('hidden'); $('#output_sidebar_uncollapsed').removeClass('hidden'); $('#output_sidebar').removeClass('output-col-collapsed').addClass('output-col'); + this.resizeAceEditors(); }, hideOutputBar: function() { $('#output_sidebar_collapsed').removeClass('hidden'); $('#output_sidebar_uncollapsed').addClass('hidden'); $('#output_sidebar').removeClass('output-col').addClass('output-col-collapsed'); + this.resizeAceEditors(); }, initializeSideBarTooltips: function() { @@ -587,15 +589,15 @@ configureEditors: function () { var percentile75 = data['working_time_75_percentile']; var accumulatedWorkTimeUser = data['working_time_accumulated']; - var timeUntilBreak = 15 * 60; + var timeUntilBreak = 15 * 60 * 1000; if ((accumulatedWorkTimeUser - percentile75) > 0) { // working time is already over 75 percentile - var timeUntilAskQuestion = 7 * 60; + var timeUntilAskQuestion = 10 * 60 * 1000; } else { // working time is less than 75 percentile // ensure we give user at least 10 minutes before we bother the user - var timeUntilAskQuestion = (percentile75 - accumulatedWorkTimeUser) > 10 * 60 ? (percentile75 - accumulatedWorkTimeUser) : 10 * 60; + var timeUntilAskQuestion = (percentile75 - accumulatedWorkTimeUser) > 10 * 60 * 1000 ? (percentile75 - accumulatedWorkTimeUser) : 10 * 60 * 1000; } // if notifications are too close to each other, ensure some time differences between them @@ -605,7 +607,15 @@ configureEditors: function () { setTimeout(function() { $('#intervention-text').text(`Willst du eine Pause machen? 75th percentile: ${percentile75} and accumulated time: ${accumulatedWorkTimeUser}`); - $('#intervention-modal').modal('show') + $('#intervention-modal').modal('show'); + $.ajax({ + data: { + exercise_id: $('#editor').data('exercise-id'), + user_id: $('#editor').data('user-id') + }, + dataType: 'json', + type: 'POST', + url: "localhost:3000/exercise/intervention"}); }, timeUntilBreak); setTimeout(function() { diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index b9f4972b..9dc950cc 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -6,7 +6,7 @@ class ExercisesController < ApplicationController before_action :handle_file_uploads, only: [:create, :update] before_action :set_execution_environments, only: [:create, :edit, :new, :update] - before_action :set_exercise, only: MEMBER_ACTIONS + [:clone, :implement, :working_times, :run, :statistics, :submit, :reload] + before_action :set_exercise, only: MEMBER_ACTIONS + [:clone, :implement, :working_times, :intervention, :run, :statistics, :submit, :reload] before_action :set_external_user, only: [:statistics] before_action :set_file_types, only: [:create, :edit, :new, :update] @@ -167,11 +167,20 @@ class ExercisesController < ApplicationController end def working_times - working_time_accumulated = @exercise.accumulated_working_time_for_only(current_user.id) + working_time_accumulated = @exercise.accumulated_working_time_for_only(current_user) working_time_75_percentile = @exercise.get_quantiles([0.75]).first render(json: {working_time_75_percentile: working_time_75_percentile, working_time_accumulated: working_time_accumulated}) end + def intervention + uei = UserExerciseIntervention.new( + user: current_user, exercise: @exercise, intervention: Intervention.first, + accumulated_worktime: @exercise.accumulated_working_time_for_only(current_user)) + + puts "user: #{current_user}, intervention: #{Intervention.first} #{uei.save}" + render(json: {success: 'true'}) + end + def index @search = policy_scope(Exercise).search(params[:q]) @exercises = @search.result.includes(:execution_environment, :user).order(:title).paginate(page: params[:page]) diff --git a/app/models/exercise.rb b/app/models/exercise.rb index 3419d9d8..1a399427 100644 --- a/app/models/exercise.rb +++ b/app/models/exercise.rb @@ -126,7 +126,8 @@ class Exercise < ActiveRecord::Base @working_time_statistics[user_id]["working_time"] end - def accumulated_working_time_for_only(user_id) + def accumulated_working_time_for_only(user) + user_type = user.external_user? ? "ExternalUser" : "InternalUser" Time.parse(self.class.connection.execute(""" SELECT sum(working_time_new) AS working_time FROM @@ -136,7 +137,7 @@ class Exercise < ActiveRecord::Base (created_at - lag(created_at) over (PARTITION BY user_id, exercise_id ORDER BY created_at)) AS working_time FROM submissions - WHERE exercise_id=#{id} and user_id=#{user_id} and user_type='ExternalUser') AS foo) AS bar + WHERE exercise_id=#{id} and user_id=#{user.id} and user_type='#{user_type}') AS foo) AS bar """).first["working_time"] || "00:00:00").seconds_since_midnight end diff --git a/app/models/proxy_exercise.rb b/app/models/proxy_exercise.rb index fe81f447..9558abb1 100644 --- a/app/models/proxy_exercise.rb +++ b/app/models/proxy_exercise.rb @@ -144,7 +144,7 @@ class ProxyExercise < ActiveRecord::Base return 0.0 end points_ratio_index = ((scoring_matrix.size - 1) * points_ratio).to_i - working_time_user = ex.accumulated_working_time_for_only(user.id) + working_time_user = ex.accumulated_working_time_for_only(user) quantiles_working_time = ex.get_quantiles(scoring_matrix_quantiles) quantile_index = quantiles_working_time.size quantiles_working_time.each_with_index do |quantile_time, i| diff --git a/app/policies/exercise_policy.rb b/app/policies/exercise_policy.rb index c89ad86a..6377488b 100644 --- a/app/policies/exercise_policy.rb +++ b/app/policies/exercise_policy.rb @@ -16,7 +16,7 @@ class ExercisePolicy < AdminOrAuthorPolicy define_method(action) { admin? || author?} end - [:implement?, :working_times?, :submit?, :reload?].each do |action| + [:implement?, :working_times?, :intervention?, :submit?, :reload?].each do |action| define_method(action) { everyone } end