diff --git a/app/controllers/files_controller.rb b/app/controllers/files_controller.rb index bf8ac5f7bf7..22644a3843c 100644 --- a/app/controllers/files_controller.rb +++ b/app/controllers/files_controller.rb @@ -289,6 +289,7 @@ class FilesController < ApplicationController generate_new_page_view @attachment.context_module_action(@current_user, :read) if @current_user log_asset_access(@attachment, 'files', 'files') + @attachment.record_inline_view render :json => {:ok => true}.to_json else render_attachment(@attachment) @@ -313,10 +314,11 @@ class FilesController < ApplicationController # Right now we assume if they ask for json data on the attachment # which includes the scribd doc data, then that means they have # viewed or are about to view the file in some form. - attachment.context_module_action(@current_user, :read) if @current_user && ( - (feature_enabled?(:scribd) && attachment.scribd_doc) || - (service_enabled?(:google_docs_previews) && attachment.authenticated_s3_url) - ) + if @current_user && ((feature_enabled?(:scribd) && attachment.scribd_doc) || + (service_enabled?(:google_docs_previews) && attachment.authenticated_s3_url)) + attachment.context_module_action(@current_user, :read) + @attachment.record_inline_view + end options[:methods] = :authenticated_s3_url if service_enabled?(:google_docs_previews) && attachment.authenticated_s3_url log_asset_access(@attachment, "files", "files") end diff --git a/app/helpers/attachment_helper.rb b/app/helpers/attachment_helper.rb index d2020810f9c..869bf69e07b 100644 --- a/app/helpers/attachment_helper.rb +++ b/app/helpers/attachment_helper.rb @@ -38,6 +38,7 @@ module AttachmentHelper end attrs[:attachment_id] = attachment.id attrs[:mimetype] = attachment.mimetype + attrs[:attachment_view_inline_ping_url] = context_url(self.context, :context_file_inline_view_url, self.id) attrs.inject("") { |s,(attr,val)| s << "data-#{attr}=#{val} " } end end diff --git a/app/models/assignment.rb b/app/models/assignment.rb index 3d1eac8dd06..659a05282c9 100644 --- a/app/models/assignment.rb +++ b/app/models/assignment.rb @@ -1159,6 +1159,10 @@ class Assignment < ActiveRecord::Base :media_comment_type, :media_comment_id, :cached_attachments, :attachments] + attachment_fields = [:id, :comment_id, :content_type, :context_id, :context_type, + :crocodoc_available?, :display_name, :filename, :mime_class, + :scribd_doc, :scribdable?, :size, :submitter_id, :workflow_state] + res = as_json( :include => { :context => { :only => :id }, @@ -1197,7 +1201,16 @@ class Assignment < ActiveRecord::Base }, :only => submission_fields, :methods => [:versioned_attachments, :late] - ) + ).tap do |s| + if s['submission'] && s['submission']['versioned_attachments'] + s['submission']['versioned_attachments'].map! do |a| + a.as_json( + :only => attachment_fields, + :methods => [:view_inline_ping_url] + ) + end + end + end end end json diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 24d3f992ac3..082e924281c 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -1622,4 +1622,12 @@ class Attachment < ActiveRecord::Base def crocodoc_available? crocodoc_document.try(:available?) end + + def view_inline_ping_url + "/#{context_url_prefix}/files/#{self.id}/inline_view" + end + + def record_inline_view + update_attribute(:last_inline_view, Time.now) + end end diff --git a/app/views/files/show.html.erb b/app/views/files/show.html.erb index 7bcfd1704f9..bd55cb498cd 100644 --- a/app/views/files/show.html.erb +++ b/app/views/files/show.html.erb @@ -41,7 +41,7 @@ <%= link_to "Download #{@attachment.display_name}", download_url %> (<%= @attachment.readable_size %>) -
context_url(@context, :context_file_inline_view_url, @attachment.id)) %>>
+
>
<% end %> <% end %> <%= render :partial => 'shared/sequence_footer', :locals => {:asset => @attachment} %> diff --git a/db/migrate/20130521223335_add_last_inline_view_to_attachments.rb b/db/migrate/20130521223335_add_last_inline_view_to_attachments.rb new file mode 100644 index 00000000000..a61790e1ceb --- /dev/null +++ b/db/migrate/20130521223335_add_last_inline_view_to_attachments.rb @@ -0,0 +1,11 @@ +class AddLastInlineViewToAttachments < ActiveRecord::Migration + tag :predeploy + + def self.up + add_column :attachments, :last_inline_view, :datetime + end + + def self.down + remove_column :attachments, :last_inline_view + end +end diff --git a/public/javascripts/full_files.js b/public/javascripts/full_files.js index cf34cff476f..f5a8745e053 100644 --- a/public/javascripts/full_files.js +++ b/public/javascripts/full_files.js @@ -311,10 +311,10 @@ define([ // what value is there in knowing the student looked at the file browser, // we just care if they access files. }, - viewFile: function(context_string, id) { + viewInlinePingUrl: function(context_string, id) { var url = $("#file_context_links ." + context_string + "_inline_view_attachment_url").attr('href'); url = $.replaceTags(url, 'id', id); - $.ajaxJSON(url, 'POST', {}, function() { }, function() { }); + return url; }, selectFolder: function($original_node) { if(!files.selectFolder.forceRefresh && ($original_node.hasClass('active-node') || $original_node.hasClass('active-leaf'))) { return; } @@ -1578,9 +1578,9 @@ define([ height: '100%', crocodoc_session_url: data.crocodocSession, scribd_doc_id: data.scribd_doc && data.scribd_doc.attributes && data.scribd_doc.attributes.doc_id, - scribd_access_key: data.scribd_doc && data.scribd_doc.attributes && data.scribd_doc.attributes.access_key + scribd_access_key: data.scribd_doc && data.scribd_doc.attributes && data.scribd_doc.attributes.access_key, + attachment_view_inline_ping_url: files.viewInlinePingUrl(data.context_string, data.id) }); - files.viewFile(data.context_string, data.id); }; if (data.permissions && data.permissions.download && $.isPreviewable(data.content_type)) { if (data['crocodoc_available?'] && !data.crocodocSession) { diff --git a/public/javascripts/speed_grader.js b/public/javascripts/speed_grader.js index 11b00c2a0dd..fed67d634e4 100644 --- a/public/javascripts/speed_grader.js +++ b/public/javascripts/speed_grader.js @@ -1243,6 +1243,7 @@ define([ mimeType: attachment.content_type, attachment_id: attachment.id, submission_id: this.currentStudent.submission.id, + attachment_view_inline_ping_url: attachment.view_inline_ping_url, ready: function(){ EG.resizeFullHeight(); } diff --git a/spec/controllers/files_controller_spec.rb b/spec/controllers/files_controller_spec.rb index e820e907ca2..a3f4f115c35 100644 --- a/spec/controllers/files_controller_spec.rb +++ b/spec/controllers/files_controller_spec.rb @@ -272,6 +272,7 @@ describe FilesController do get 'show', :course_id => @course.id, :id => @file.id, :download => 1 @module.reload @module.evaluate_for(@user, true, true).state.should eql(:completed) + @file.reload.last_inline_view.should be_nil end it "should not mark a file as viewed for module progressions if the file is locked" do @@ -281,6 +282,7 @@ describe FilesController do get 'show', :course_id => @course.id, :id => @file.id, :download => 1 @module.reload @module.evaluate_for(@user, true, true).state.should eql(:unlocked) + @file.reload.last_inline_view.should be_nil end it "should not mark a file as viewed for module progressions just because the files#show view is rendered" do @@ -290,6 +292,7 @@ describe FilesController do get 'show', :course_id => @course.id, :id => @file.id @module.reload @module.evaluate_for(@user, true, true).state.should eql(:unlocked) + @file.reload.last_inline_view.should be_nil end it "should mark files as viewed for module progressions if the file is previewed inline" do @@ -298,6 +301,7 @@ describe FilesController do json_parse.should == {'ok' => true} @module.reload @module.evaluate_for(@user, true, true).state.should eql(:completed) + @file.reload.last_inline_view.should > 1.minute.ago end it "should mark files as viewed for module progressions if the file data is requested and it includes the scribd_doc data" do @@ -307,6 +311,7 @@ describe FilesController do get 'show', :course_id => @course.id, :id => @file.id, :format => :json @module.reload @module.evaluate_for(@user, true, true).state.should eql(:completed) + @file.reload.last_inline_view.should > 1.minute.ago end it "should not mark files as viewed for module progressions if the file data is requested and it doesn't include the scribd_doc data (meaning it got viewed in scribd inline) and google docs preview is disabled" do @@ -322,6 +327,7 @@ describe FilesController do get 'show', :course_id => @course.id, :id => @file.id, :format => :json @module.reload @module.evaluate_for(@user, true, true).state.should eql(:unlocked) + @file.reload.last_inline_view.should be_nil end it "should redirect to an existing attachment with the same path as a deleted attachment" do diff --git a/spec/models/assignment_spec.rb b/spec/models/assignment_spec.rb index 9bee2e0e459..d03706756d1 100644 --- a/spec/models/assignment_spec.rb +++ b/spec/models/assignment_spec.rb @@ -2316,6 +2316,17 @@ describe Assignment do submission[:late].should == user.submissions.first.late? end end + + it "should include inline view pingback url for files" do + course_with_teacher :active_all => true + student_in_course :active_all => true + assignment = @course.assignments.create! :submission_types => ['online_upload'] + attachment = @student.attachments.create! :uploaded_data => dummy_io, :filename => 'doc.doc', :display_name => 'doc.doc', :context => @student + submission = assignment.submit_homework @student, :submission_type => :online_upload, :attachments => [attachment] + json = assignment.speed_grader_json @teacher + attachment_json = json['submissions'][0]['submission_history'][0]['submission']['versioned_attachments'][0]['attachment'] + attachment_json['view_inline_ping_url'].should match %r{/users/#{@student.id}/files/#{attachment.id}/inline_view\z} + end end describe "update_student_submissions" do