From 223df2ffa8a9f34dd23fc2a92a18ec6dee6d7726 Mon Sep 17 00:00:00 2001 From: Ralf Teusner Date: Mon, 4 Jul 2016 17:44:22 +0200 Subject: [PATCH] some cleanup of request for comments. Work in progress. Noticed a flaw when fetching the last submission, which is caused by timezone differences. First step to solve this. Existing Request for Comments still need to be updated with their current submissionId, the SQL to do that is not yet finished. --- app/assets/javascripts/editor.js.erb | 37 ++++++++++--------- .../request_for_comments_controller.rb | 2 +- app/models/request_for_comment.rb | 29 +++++++++++++++ app/models/submission.rb | 2 +- app/views/exercises/_editor_frame.html.slim | 4 +- app/views/request_for_comments/show.html.erb | 13 ++----- db/schema.rb | 3 +- 7 files changed, 57 insertions(+), 33 deletions(-) diff --git a/app/assets/javascripts/editor.js.erb b/app/assets/javascripts/editor.js.erb index 1e64195e..b706b437 100644 --- a/app/assets/javascripts/editor.js.erb +++ b/app/assets/javascripts/editor.js.erb @@ -1160,25 +1160,26 @@ $(function() { var file_id = $('.editor').data('id') var question = $('#question').val(); - $.ajax({ - method: 'POST', - url: '/request_for_comments', - data: { - request_for_comment: { - exercise_id: exercise_id, - file_id: file_id, - question: question, - "requested_at(1i)": 2015, // these are the timestamp values that the request handler demands - "requested_at(2i)":3, // they could be random here, because the timestamp is updated on serverside anyway - "requested_at(3i)":27, - "requested_at(4i)":17, - "requested_at(5i)":06 + var createRequestForComments = function(submission) { + console.log(submission); + $.ajax({ + method: 'POST', + url: '/request_for_comments', + data: { + request_for_comment: { + exercise_id: exercise_id, + file_id: file_id, + submission_id: submission.id, + question: question + } } - } - }).done(function() { - hideSpinner(); - $.flash.success({ text: $('#askForCommentsButton').data('message-success') }) - }).error(ajaxError); + }).done(function() { + hideSpinner(); + $.flash.success({ text: $('#askForCommentsButton').data('message-success') }); + }).error(ajaxError); + } + + createSubmission($('.requestCommentsButton'), null, createRequestForComments); $('#comment-modal').modal('hide'); var button = $('.requestCommentsButton'); diff --git a/app/controllers/request_for_comments_controller.rb b/app/controllers/request_for_comments_controller.rb index 72653ab2..37d8bef9 100644 --- a/app/controllers/request_for_comments_controller.rb +++ b/app/controllers/request_for_comments_controller.rb @@ -82,6 +82,6 @@ class RequestForCommentsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def request_for_comment_params - params.require(:request_for_comment).permit(:exercise_id, :file_id, :question, :requested_at, :solved).merge(user_id: current_user.id, user_type: current_user.class.name) + params.require(:request_for_comment).permit(:exercise_id, :file_id, :question, :requested_at, :solved, :submission_id).merge(user_id: current_user.id, user_type: current_user.class.name) end end diff --git a/app/models/request_for_comment.rb b/app/models/request_for_comment.rb index 57b9a079..1343f5b3 100644 --- a/app/models/request_for_comment.rb +++ b/app/models/request_for_comment.rb @@ -25,6 +25,35 @@ class RequestForComment < ActiveRecord::Base limit 1").first end + def last_submission_before_creation + submission1 = Submission.find_by_sql(" select * from submissions + where exercise_id = #{exercise_id} AND + user_id = #{user_id} AND + '#{created_at.localtime}' > created_at + order by created_at desc + limit 1").first + submission2 = Submission.find_by_sql(" select * from submissions + where exercise_id = #{exercise_id} AND + user_id = #{user_id} AND + '#{created_at}' > created_at + order by created_at desc + limit 1").first + submission3 = Submission.find_by_sql(" select * from submissions + where exercise_id = #{exercise_id} AND + user_id = #{user_id} AND + '#{created_at.strftime('%Y-%m-%d %H:%M:%S.%N')}' > created_at + order by created_at desc + limit 1").first + submission4 = Submission.find_by_sql(" select * from submissions + where exercise_id = #{exercise_id} AND + user_id = #{user_id} AND + '#{created_at.localtime.strftime('%Y-%m-%d %H:%M:%S.%N')}' > created_at + order by created_at desc + limit 1").first + binding.pry + submission1 + end + def comments_count submission.files.map { |file| file.comments.size}.sum end diff --git a/app/models/submission.rb b/app/models/submission.rb index 323f1d58..28e98555 100644 --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -2,7 +2,7 @@ class Submission < ActiveRecord::Base include Context include Creation - CAUSES = %w(assess download file render run save submit test autosave) + CAUSES = %w(assess download file render run save submit test autosave requestComments) FILENAME_URL_PLACEHOLDER = '{filename}' belongs_to :exercise diff --git a/app/views/exercises/_editor_frame.html.slim b/app/views/exercises/_editor_frame.html.slim index eacc62a9..01640fa8 100644 --- a/app/views/exercises/_editor_frame.html.slim +++ b/app/views/exercises/_editor_frame.html.slim @@ -14,6 +14,6 @@ .editor-content.hidden data-file-id=file.ancestor_id = file.content .editor data-file-id=file.ancestor_id data-indent-size=file.file_type.indent_size data-mode=file.file_type.editor_mode data-read-only=file.read_only data-id=file.id - button.btn.btn-primary.requestCommentsButton type='button' - i.fa.fa-comment-o + button.btn.btn-primary.requestCommentsButton type='button' id="requestComments" + i.fa.fa-comment = t('exercises.editor.requestComments') \ No newline at end of file diff --git a/app/views/request_for_comments/show.html.erb b/app/views/request_for_comments/show.html.erb index 6ef176bd..8e52150c 100644 --- a/app/views/request_for_comments/show.html.erb +++ b/app/views/request_for_comments/show.html.erb @@ -3,19 +3,12 @@

<% user = @request_for_comment.user - submission_id = ActiveRecord::Base.connection.execute("select id from submissions - where exercise_id = - #{@request_for_comment.exercise_id} AND - user_id = #{@request_for_comment.user_id} AND - '#{@request_for_comment.created_at}' > created_at - order by created_at desc - limit 1").first['id'].to_i - submission = Submission.find(submission_id) + submission = @request_for_comment.last_submission_before_creation %> - <%= user.displayname %> | <%= @request_for_comment.requested_at %> + <%= user.displayname %> | <%= @request_for_comment.requested_at.localtime %>

- <%= t('activerecord.attributes.exercise.instructions') %>: "<%= @request_for_comment.exercise.description %>" + <%= t('activerecord.attributes.exercise.description') %>: "<%= render_markdown(@request_for_comment.exercise.description) %>"
diff --git a/db/schema.rb b/db/schema.rb index 67b3b35c..287b5bd5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160624130951) do +ActiveRecord::Schema.define(version: 20160630154310) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -191,6 +191,7 @@ ActiveRecord::Schema.define(version: 20160624130951) do t.string "user_type" t.text "question" t.boolean "solved" + t.integer "submission_id" end create_table "submissions", force: true do |t|