Skip to content

Instantly share code, notes, and snippets.

@EminenceHC
Created November 7, 2013 23:00
Show Gist options
  • Save EminenceHC/7363367 to your computer and use it in GitHub Desktop.
Save EminenceHC/7363367 to your computer and use it in GitHub Desktop.
Sum or Average of Table Column
<h3>Result Sets</h3>
<div class="span12" style="width:100%;margin:5px;">
<% if current.loginable_type != 'Student' %>
<%= simple_form_for(result_sets_path, :method =>'get', :html => { :class => 'form-horizontal' }) do %>
<%= select_tag 'id', options_for_select(@quiz_id), { :prompt => 'Quiz Name'} %>
<%= select_tag 'student_id', options_for_select(@quiz_student_name), { :prompt => 'Student Name'} %>
<%= submit_tag 'Submit', :class => 'btn btn-primary' %>
<% end %>
<% else %>
<%= simple_form_for(result_sets_path, :method =>'get', :html => { :class => 'form-horizontal' }) do %>
<%= select_tag 'id', options_for_select(@quiz_id), { :prompt => 'All Quizzes'} %>
<% @student_only %>
<%= submit_tag 'Submit', :class => 'btn btn-primary' %>
<% end %>
<% end %>
<table class="table table-condensed table-bordered table-striped table-responsive">
<thead>
<tr>
<th>Student</th>
<th>Quiz</th>
<th>Survey ID</th>
<th>Result ID</th>
<th>Possible</th>
<th>Correct</th>
<th>Percent</th>
<th>View</th>
<% if current.loginable_type != 'Student' %>
<th>Delete</th>
<% end %>
</tr>
</thead>
<% @query.each do |a| %>
<tbody>
<tr>
<td><%= a.student.user.name %></td>
<td><%= a.survey.name %></td>
<td><%= a.survey_id %></td>
<td><%= a.id %></td>
<td><%= a.total_count.to_f %></td>
<td><%= a.correct_count.to_f %></td>
<td><%= a.percent %></td>
<td><%= link_to " View", result_set_path(a.id), class: 'btn btn-mini glyphicon glyphicon-eye-open' %></td>
<% if current.loginable_type != 'Student' %><td><%= link_to "", result_set_path(a.id), method: :delete, class: 'btn btn-mini btn-danger glyphicon glyphicon-trash', confirm: 'Are you sure?', style: 'margin-left:20%' %></td><% end %>
<%= @sum %>
</tr>
</tbody>
<% end %>
</table>
</div>
class ResultSetsController < ApplicationController
before_filter :new
def index
@quiz_id = ResultSet.all.map { |u| [u.survey.name, u.survey_id]}.uniq
@quiz_student_name = ResultSet.all.map { |u| [u.student.user.name, u.student_id]}.uniq
if current.loginable_type == 'Student'
@student_only = params[:student_id] = current_user.loginable.id
end
runquery = ResultSet
runquery = runquery.where(:survey_id => params[:id]) if params[:id].present?
runquery = runquery.where(:student_id => params[:student_id]) if params[:student_id].present?
runquery = runquery.select('result_sets.*')
@query = runquery
@query.each do |a|
@arr = []
@actual = []
@correct = []
@score = []
a.serial.to_a.each do |key,value|
@actual << value
end
a.survey.questions.each do |b|
@id = b.answers
@id.each do |c|
if c.correct == true
@correct = @arr << c.id
end
end
end
# Virtual Attributes for View
@score = @correct.collect{|x| "#{x}"} & @actual
a.correct_count = @score.count.to_f
a.total_count = @actual.count.to_f
a.percent = (a.correct_count / a.total_count) * 100
end
end
def new
@result_set = ResultSet.new
end
def create
@result_set = ResultSet.new(result_set_params)
@result_set.save!
if @result_set.save
redirect_to result_sets_path, notice: "Quiz Completed Successfully"
else
redirect_to result_sets_path, notice: "There was a problem, Quiz not saved."
end
end
def show
@result_set = ResultSet.find(params[:id])
@actual = []
@arr = []
@serial = @result_set.serial
@result_set.serial.to_a.each do |key,value|
@actual << value
end
@result_set.survey.questions.each do |b|
@id = b.answers
@id.each do |c|
if c.correct == true
@correct = @arr << c.id
end
end
end
end
def destroy
@result_set = ResultSet.find(params[:id])
@result_set.destroy
redirect_to result_sets_url, notice: "Successfully destroyed the user's quiz results."
end
private
def result_set_params
#params.require(:result_set).permit!
params.require(:result_set).permit(:id, :student_id, :survey_id, :created_at, :updated_at, survey_attributes: [:id, :name, :created_at, :updated_at, :student_id]).tap do |whitelisted|
whitelisted[:serial] = params[:result_set][:serial]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment