Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding delete button for exams #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app/controllers/exams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,39 @@ def index
end
end

def destroy
exam_dir = 'public/examfiles/'
@exam = Exam.find(params[:id])
exam_path = exam_dir+ @exam.filename
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exam_path = exam_dir + @exam.filename

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, file paths should probably be joined with File.join.

File.delete(exam_path)
@exam.destroy
redirect_to :back

end

def update_form
@exam = Exam.find_by_id(params[:id])
end

def update
exam_dir = 'public/examfiles/'
@exam = Exam.find_by_id(params[:id])
exam_path = exam_dir+ @exam.filename
exam_file = params[:file_info]

begin
f = File.open(exam_path, 'wb')
if @exam.valid? and not f.nil?
f.write(exam_file.read)
@exam.save
flash[:notice] = "Exam Reuploaded!"
redirect_to :back
end
ensure
f.close if f
end
end

def department
@dept_name, @courses = Exam.get_dept_name_courses_tuples(params[:dept_abbr])

Expand Down
7 changes: 7 additions & 0 deletions app/views/admin/studrel/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<h1>Studrel Admin</h1>
<h3>Add a new exam file</h3>
<%= button_to "New Exam File", exams_new_path, :method => :get %>
<h3>Delete or Update an exam file</h3>
<div id="examfiles" class="searchcontrol">
<%= form_tag(exams_search_path, :method => :get) do %>
<%= text_field_tag :q, params[:controller].eql?('exams') ? h(params[:q]) : '', :placeholder=>"Search Exams", :class=>"text autoclear", :id => "examfiles_q" %>
<%= submit_tag "Search", :name=>nil %>
<%- end -%>
</div>
10 changes: 8 additions & 2 deletions app/views/exams/course.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@
</td>
<%- ['mt1', 'mt2', 'mt3', 'f'].each do |short_type| -%>
<td>
<%- exam = exams[short_type] -%>
<%- sol = solutions[short_type] -%>
<%- exam = exams[short_type] -%>
<%- sol = solutions[short_type] -%>
<%= (link_to "[#{exam.file_type}]", @exam_path+exam.filename) unless exam.nil?%><%= (link_to '[solution]', @exam_path+sol.filename) unless sol.nil? %>
<% if @auth["studrel"] %>
<%= (link_to '[Remove exam]', exams_destroy_path(exam.id) , :method => :delete, :confirm => 'Are you sure?') unless exam.nil?%>
<%= (link_to '[Update exam]', exams_update_form_path(exam.id), :method => :get) unless exam.nil?%>
<%= (link_to '[Remove solution]' , exams_destroy_path(sol.id) , :method => :delete, :confirm => 'Are you sure?') unless sol.nil?%>
<%= (link_to '[Update solution]' , exams_update_form_path(sol.id) , :method => :get) unless sol.nil?%>
<% end %>
</td>
<%- end -%>
</tr>
Expand Down
41 changes: 41 additions & 0 deletions app/views/exams/update_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<% content_for :header do %>
<%= javascript_include_tag "jquery-autocomplete.min" %>
<%= stylesheet_link_tag "jquery-autocomplete" %>

<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: '/admin/tutor/find_courses',
success: function(data) {
d = []
for(var item in data) {
d[item] = {text: data[item]["text"], url: data[item]["url"]}
}

$("#course_query").autocomplete(data, {
formatItem: function(item) {
return item.text;
}
}).result(function(event, item) {
$("#course_id").attr("value", item.url);
});
}
});
});
</script>


<% end %>

<%= form_for @exam, :url => :exams_update , :html => { :multipart => true , :method => :post } do |f| %>

<div>

<div id="upload">
<%= file_field_tag :file_info %>
</div>

<%= f.submit "Update Exam File" %>

</div>
<% end %>
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@
:as => :exams_new
post 'create' => "exams#create",
:as => :exams_create
delete ':id/destroy' => "exams#destroy",
:as => :exams_destroy
get ':id/update_form' => "exams#update_form",
:as => :exams_update_form
post ':id/update' => "exams#update",
:as => :exams_update
end
#resources :exams
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Rails' in-built resources routing method here would make more sense, I would think. Exams appear to have pretty standard CRUD operations defined for them. See http://guides.rubyonrails.org/v3.2.13/routing.html for more information.


Expand Down