-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_codes_controller.rb
158 lines (136 loc) · 4.4 KB
/
source_codes_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require 'zip'
require 'find'
require 'uri'
require 'net/http'
class SourceCodesController < ApplicationController
before_filter :find_exercise
def find_exercise
@exercise= Exercise.find(params[:exercise_id])
end
# GET /source_codes
# GET /source_codes.xml
def index
@source_codes = @exercise.source_codes
@input = SimilarityInput.new
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @source_codes }
end
end
# GET /source_codes/1
# GET /source_codes/1.xml
def show
@source_code = @exercise.source_codes.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @source_code }
end
end
# GET /source_codes/new
# GET /source_codes/new.xml
def new
@source_code = SourceCode.new
@zipfile = ZipFile.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @source_code }
end
end
# GET /source_codes/1/edit
def edit
@source_code = @exercise.source_codes.find(params[:id])
end
# POST /source_codes
# POST /source_codes.xml
def create
@source_code = @exercise.source_codes.build(params[:source_code])
respond_to do |format|
if @source_code.save
format.html { redirect_to(course_exercise_source_code_path(@exercise.course, @exercise, @source_code), :notice => 'Source code was successfully created.') }
format.xml { render :xml => @source_code, :status => :created, :location => @source_code }
else
format.html { render :action => "new" }
format.xml { render :xml => @source_code.errors, :status => :unprocessable_entity }
end
end
end
# PUT /source_codes/1
# PUT /source_codes/1.xml
def update
@source_code = @exercise.source_codes.find(params[:id])
respond_to do |format|
if @source_code.update_attributes(params[:source_code])
format.html { redirect_to(course_exercise_source_code_path(@exercise.course, @exercise, @source_code), :notice => 'Source code was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @source_code.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /source_codes/1
# DELETE /source_codes/1.xml
def destroy
@source_code = @exercise.source_codes.find(params[:id])
@source_code.destroy
respond_to do |format|
format.html { redirect_to(course_exercise_source_codes_url(@exercise.course, @exercise)) }
format.xml { head :ok }
end
end
def upload_file
tmp = params[:zip_file][:file].tempfile
file = "#{RAILS_ROOT}/public/zipfiles/#{params[:zip_file][:file].original_filename.tr(' ','-')}"
FileUtils.cp(tmp.path, file)
unzip(file, "#{RAILS_ROOT}/public/zipfiles")
FileUtils.rm(file)
redirect_to course_exercise_path(@exercise.course, @exercise)
end
def unzip(file, to)
if !File.exists?( to )
FileUtils.mkdir( to )
end
#open Zip File
zip_file = Zip::ZipFile.open(file)
#UnZip file
zip_file.reverse_each do |entry|
file_path = File.join( to, entry.to_s )
if File.exists?( file_path ) && !File.directory?(file_path)
FileUtils.rm( file_path )
end
zip_file.extract( entry, file_path )
end
copy_to_database(file)
end
def copy_to_database(file)
file_path = file.chomp(".zip")
Find.find(file_path) do |subfile|
if subfile != file_path
if !File.directory?(subfile) && !File.extname(subfile).index('~')
@source_code = @exercise.source_codes.build
@source_code.language = File.extname(subfile)
@source_code.student_id = File.basename(subfile, @source_code.language)
file = File.open(subfile, "r")
code = ""
while(line = file.gets)
code+=line
end
@source_code.code = code
@source_code.save
end
end
end
FileUtils.rm_rf(file_path)
end
def compare
parametros = {}
parametros[:algorithms] = params[:algorithms]
parametros[:files] = []
params[:files].each do |index|
source_code = SourceCode.find(index)
parametros[:files] << {:id => index, :code => source_code.code}
end
#debugger
request = Net::HTTP.post_form(URI.parse('http://localhost:3001/compare'), parametros)
end
end