diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 686070e70..ebf3c3ba5 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -115,6 +115,21 @@ class AttachmentsController < ApplicationController format.js end end + + def delete_homework + @bid = @attachment.container.bid + # Make sure association callbacks are called + container = @attachment.container + @attachment.container.attachments.delete(@attachment) + if container.attachments.empty? + container.delete + end + + respond_to do |format| + format.html { redirect_to_referer_or respond_path(@bid) } + format.js + end + end private def find_project diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0596f9693..80838947f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -135,6 +135,7 @@ class UsersController < ApplicationController @memberships.each do |membership| @bid += membership.project.homeworks end + @bid = @bid.group_by {|bid| bid.courses.first.id} @state = 1 else @membership = @user.memberships.all(:conditions => Project.visible_condition(User.current)) diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb index a4925343f..f61399c85 100644 --- a/app/helpers/attachments_helper.rb +++ b/app/helpers/attachments_helper.rb @@ -31,6 +31,15 @@ module AttachmentsHelper :locals => {:attachments => container.attachments, :options => options, :thumbnails => (options[:thumbnails] && Setting.thumbnails_enabled?)} end end + + def attach_delete(project) + if User.current.logged? && (User.current.admin? || (!Member.where('user_id = ? and project_id = ?', User.current.id, project.bid.courses.first.id).first.nil? && (Member.where('user_id = ? and project_id = ?', User.current.id, project.bid.courses.first.id).first.roles&Role.where('id = ? or id = ?', 3, 7)).size >0) || project.user_id == User.current.id) + true + else + false + end + + end def render_api_attachment(attachment, api) api.attachment do diff --git a/app/views/attachments/_links.html.erb b/app/views/attachments/_links.html.erb index d2ab22ced..81eb416af 100644 --- a/app/views/attachments/_links.html.erb +++ b/app/views/attachments/_links.html.erb @@ -9,11 +9,19 @@ <%= h(" - #{attachment.description}") unless attachment.description.blank? %> (<%= number_to_human_size attachment.filesize %>) <% if options[:deletable] %> + <% unless attachment.container_type == 'HomeworkAttach' %> <%= link_to image_tag('delete.png'), attachment_path(attachment), :data => {:confirm => l(:text_are_you_sure)}, :method => :delete, :class => 'delete', :title => l(:button_delete) %> + <% else %> + <%= link_to image_tag('delete.png'), {:controller => 'attachments', :action => 'delete_homework', :id => attachment.id}, + :data => {:confirm => l(:text_are_you_sure)}, + :method => :delete, + :class => 'delete', + :title => l(:button_delete) %> + <% end %> <% end %> <% if options[:author] %> diff --git a/app/views/attachments/delete.js.erb b/app/views/attachments/delete.js.erb new file mode 100644 index 000000000..3cfb5845f --- /dev/null +++ b/app/views/attachments/delete.js.erb @@ -0,0 +1 @@ +$('#attachments_<%= j params[:attachment_id] %>').remove(); diff --git a/app/views/bids/_homework_list.html.erb b/app/views/bids/_homework_list.html.erb index 62f5005c2..3e9713bdd 100644 --- a/app/views/bids/_homework_list.html.erb +++ b/app/views/bids/_homework_list.html.erb @@ -15,7 +15,7 @@
- <% options = {:author => true} %> + <% options = {:author => true, :deletable => attach_delete(homework)} %> <%= render :partial => 'attachments/links', :locals => {:attachments => homework.attachments, :options => options} %> <% end %> diff --git a/app/views/users/_my_homework.html.erb b/app/views/users/_my_homework.html.erb index a075fb6d2..d78c5dc0c 100644 --- a/app/views/users/_my_homework.html.erb +++ b/app/views/users/_my_homework.html.erb @@ -1,7 +1,10 @@ <% if @bid.size > 0%> -<% for bid in @bid%> +<% @bid.each do |bids|%> +