Skip to content

Commit

Permalink
Merge 4c1c73e into 1c29dd5
Browse files Browse the repository at this point in the history
  • Loading branch information
MehakMehta committed Apr 1, 2014
2 parents 1c29dd5 + 4c1c73e commit 1d5910c
Show file tree
Hide file tree
Showing 5 changed files with 437 additions and 22 deletions.
118 changes: 117 additions & 1 deletion app/controllers/grades_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,122 @@ def action_allowed?
case params[:action]
when 'view_my_scores'
current_role_name.eql? 'Student'
when 'all_scores'
current_role_name.eql? 'Student'
else
['Instructor',
'Teaching Assistant',
'Administrator'].include? current_role_name

end
true
end

# method to return scores of user for all assignments
def all_scores
@scores = Hash.new

# Single query to return all scores for user
st = ActiveRecord::Base.connection.raw_connection.prepare("select assignments.name, questionnaire_type, avg(score), min(score), max(score) from
(select r.id as response_id, team_id, tu.user_id as user_id, (SUM(weight*score)*100)/(sum(weight)*max_question_score) as score, parent_id as assignment_id, qs.type as questionnaire_type from scores s ,responses r, response_maps rm, questions q, questionnaires qs , teams_users tu , teams t
WHERE rm.id = r.map_id AND r.id=s.response_id AND q.id = s.question_id AND qs.id = q.questionnaire_id AND tu.team_id = rm.reviewee_id AND tu.team_id = t.id group by r.id
) as participant_score_aggregate, assignments where assignments.id = participant_score_aggregate.assignment_id
and user_id = ? group by assignment_id, questionnaire_type")

# Execute query and pass current user id
results = st.execute(current_user.id)

# Loop through results to populate scores hash
# row[0] is assignment name
# row[1] is questionnaire type
# row[2] is avg
# row[3] is min
# row[4] is max

results.each do |row|
@scores[row[0].to_s.to_sym] = Hash.new
@scores[row[0].to_s.to_sym][row[1].to_s.to_sym] = Hash.new
@scores[row[0].to_s.to_sym][row[1].to_s.to_sym][:avg] = row[2].to_f.round(2)
@scores[row[0].to_s.to_sym][row[1].to_s.to_sym][:min] = row[3].to_f.round(2)
@scores[row[0].to_s.to_sym][row[1].to_s.to_sym][:max] = row[4].to_f.round(2)
end
end
def view_course_scores

# Create empty scores hash
@scores = Hash.new

@scores[params[:course_id]]= Hash.new

course_assignments = Array.new
# Get all the assignments for this course
course_assignments = Assignment.find_all_by_course_id(params[:course_id])

# Hash creation begins. A hash is created to have the same structure as required by the view
# This hash is not created after the query according to the resultset because
# the query only returns results for participants who got responses and scores.
# We want an empty placeholder for participants that have no scores.
# Loop through the course assignments array
course_assignments.each do |assignment|
@scores[params[:course_id]][assignment.name.to_sym] = Hash.new

# Loop through all the participants of this assignment
assignment.participants.each do |participant|

# Get the user name of the participant from the user id
participant_name = User.find(participant.user_id).name

@scores[params[:course_id]][assignment.name.to_sym][participant_name.to_s.to_sym]=Hash.new

# Loop through the assignment questionnaires
assignment.questionnaires.each do |questionnaire|
@scores[params[:course_id]][assignment.name.to_sym][participant_name.to_s.to_sym][questionnaire.symbol] = Hash.new
@scores[params[:course_id]][assignment.name.to_sym][participant_name.to_s.to_sym][questionnaire.symbol][:scores] = Hash.new
@scores[params[:course_id]][assignment.name.to_sym][participant_name.to_s.to_sym][questionnaire.symbol][:scores][:avg] = nil
@scores[params[:course_id]][assignment.name.to_sym][participant_name.to_s.to_sym][questionnaire.symbol][:scores][:min] = nil
@scores[params[:course_id]][assignment.name.to_sym][participant_name.to_s.to_sym][questionnaire.symbol][:scores][:max] = nil
end
end
end
# Hash creation completed

# Single sql query
st = ActiveRecord::Base.connection.raw_connection.prepare("select assignments.name, user_id, questionnaire_type, avg(score), min(score), max(score) from
(select r.id as response_id, team_id, u.name as user_id, (SUM(weight*score)*100)/(sum(weight)*max_question_score) as score, t.parent_id as assignment_id, qs.type as questionnaire_type from scores s ,responses r, response_maps rm, questions q, questionnaires qs , users u, teams_users tu , teams t
WHERE rm.id = r.map_id AND r.id=s.response_id AND q.id = s.question_id AND qs.id = q.questionnaire_id AND tu.team_id = rm.reviewee_id AND tu.team_id = t.id AND tu.user_id=u.id group by r.id
) as participant_score_aggregate, assignments where assignments.id = participant_score_aggregate.assignment_id
and assignments.course_id = ? group by assignment_id, questionnaire_type")
# Run the sql query with the course id as parameter
results = st.execute(params[:course_id])

# Loop through the results
# row[0] is assignment name
# row[1] is user id
# row[2] is questionnaire typ
# row[3] is avg
# row[4] is min
# row[5] is max
results.each do |row|
# Set the type of questionnaire
case row[2].to_sym
when :ReviewQuestionnaire
quetionnaire_type= :review
when :AuthorFeedbackQuestionnaire
quetionnaire_type= :feedback
when :MetareviewQuestionnaire
quetionnaire_type= :metareview
when :TeammateReviewQuestionnaire
quetionnaire_type= :teammate
end

# Set the avg, min and max scores
@scores[params[:course_id]][row[0].to_s.to_sym][row[1].to_s.to_sym][quetionnaire_type][:scores][:avg]=row[3].to_f.round(2)
@scores[params[:course_id]][row[0].to_s.to_sym][row[1].to_s.to_sym][quetionnaire_type][:scores][:min]=row[4].to_f.round(2)
@scores[params[:course_id]][row[0].to_s.to_sym][row[1].to_s.to_sym][quetionnaire_type][:scores][:max]=row[5].to_f.round(2)
end

@scores
end

#the view grading report provides the instructor with an overall view of all the grades for
#an assignment. It lists all participants of an assignment and all the reviews they received.
Expand Down Expand Up @@ -281,6 +391,7 @@ def calculate_all_penalties(assignment_id)
calculate_for_participants = true
end
Participant.find_all_by_parent_id(assignment_id).each do |participant|
if participant.type=='AssignmentParticipant'
penalties = calculate_penalty(participant.id)
@total_penalty =0
if(penalties[:submission] != 0 || penalties[:review] != 0 || penalties[:meta_review] != 0)
Expand Down Expand Up @@ -314,9 +425,14 @@ def calculate_all_penalties(assignment_id)
@all_penalties[participant.id][:review] = penalties[:review]
@all_penalties[participant.id][:meta_review] = penalties[:meta_review]
@all_penalties[participant.id][:total_penalty] = @total_penalty
end
end
if @assignment.is_penalty_calculated == false
@assignment.update_attribute(:is_penalty_calculated, true)
end
end
end




end
118 changes: 97 additions & 21 deletions app/models/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,85 @@ def get_scores(questions)
scores = Hash.new

scores[:participants] = Hash.new

# Create empty hash
# This is a placeholder for scores that will be returned from database.
# Reason to do this before running the query is because the query returns results
# only for submissions that did get responses. We want the rest to be present
# in the hash, even if nil.
# Loop through all the participants of this assignment
self.participants.each do |participant|
scores[:participants][participant.id.to_s.to_sym] = participant.get_scores(questions)
scores[:participants][participant.id.to_s.to_sym] = Hash.new
scores[:participants][participant.id.to_s.to_sym][:participant] = participant

# Loop through all the questionnaires
self.questionnaires.each do |questionnaire|
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol] = Hash.new
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol][:scores] = Hash.new
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol][:assessments] = questionnaire.get_assessments_for(participant)
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol][:scores][:avg] = nil
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol][:scores][:min] = nil
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol][:scores][:max] = nil
end

scores[:participants][participant.id.to_s.to_sym][:total_score] = 0
quiz_responses = Array.new

quiz_response_mappings = QuizResponseMap.find_all_by_reviewer_id(participant.id)
quiz_response_mappings.each do |qmapping|
if (qmapping.response)
quiz_responses << qmapping.response
end
end

scores[:participants][participant.id.to_s.to_sym][:quiz] = Hash.new
scores[:participants][participant.id.to_s.to_sym][:quiz][:assessments] = quiz_responses
scores[:participants][participant.id.to_s.to_sym][:quiz][:scores] = Score.compute_quiz_scores(scores[:participants][participant.id.to_s.to_sym][:quiz][:assessments])
scores[:teams] = Hash.new
end
# End of creation of empty hash

# Single query
st = ActiveRecord::Base.connection.raw_connection.prepare("select pid as participant_id, avg(score), min(score), max(score), questionnaire_type from (select r.id as response_id, p.id as pid, p.user_id as user_id, (SUM(weight*score)*100)/(sum(weight)*max_question_score) as score, parent_id , qs.type as questionnaire_type from scores s ,responses r, response_maps rm, questions q, questionnaires qs , participants p WHERE rm.id = r.map_id AND r.id=s.response_id AND q.id = s.question_id AND qs.id = q.questionnaire_id AND p.id = rm.reviewee_id AND parent_id= ? group by r.id) as assignment_score_aggregate group by questionnaire_type, pid")
# Execute the SQL query with the assignment id
results = st.execute(self.id)

# Loop through the result returned to populate the hash
# row[0] is participant id
# row[1] is avg
# row[1] is min
# row[1] is max
# row[1] is questionnaire_type
results.each do |row|
participant=self.participants.find(row[0].to_int)

scores[:participants][participant.id.to_s.to_sym][:participant] = participant

questionnaire_type_score = Hash.new
questionnaire_type = row[4]
questionnaire_type_score[questionnaire_type.to_sym] = Hash.new
questionnaire_type_score[questionnaire_type.to_sym][:avg] = row[1].to_f.round(2)
questionnaire_type_score[questionnaire_type.to_sym][:min] = row[2].to_f.round(2)
questionnaire_type_score[questionnaire_type.to_sym][:max] = row[3].to_f.round(2)

# Loop through questionnaires
self.questionnaires.each do |questionnaire|
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol] = Hash.new
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol][:assessments] = questionnaire.get_assessments_for(participant)
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol][:scores] = Hash.new

# Check the type of questionnaire
if questionnaire.symbol==:review && !questionnaire_type_score[:ReviewQuestionnaire].nil?
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol][:scores] = questionnaire_type_score[:ReviewQuestionnaire]
elsif questionnaire.symbol==:feedback && !questionnaire_type_score[:AuthorFeedbackQuestionnaire].nil?
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol][:scores] = questionnaire_type_score[:AuthorFeedbackQuestionnaire]
elsif questionnaire.symbol==:metareview && !questionnaire_type_score[:MetareviewQuestionnaire].nil?
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol][:scores] = questionnaire_type_score[:MetareviewQuestionnaire]
elsif questionnaire.symbol==:teammate && !questionnaire_type_score[:TeammateReviewQuestionnaire].nil?
scores[:participants][participant.id.to_s.to_sym][questionnaire.symbol][:scores] = questionnaire_type_score[:TeammateReviewQuestionnaire]
end

end

# for all quiz questionnaires (quizzes) taken by the participant
quiz_responses = Array.new
Expand All @@ -327,27 +403,23 @@ def get_scores(questions)

scores[:participants][participant.id.to_s.to_sym][:total_score] = compute_total_score(scores[:participants][participant.id.to_s.to_sym])
scores[:participants][participant.id.to_s.to_sym][:total_score] += participant.compute_quiz_scores(scores[:participants][participant.id.to_s.to_sym])



index = 0

# get scores for teams. This is retained from previous code
#ACS Removed the if condition(and corressponding else) which differentiate assignments as team and individual assignments
# to treat all assignments as team assignments
self.teams.each do |team|
scores[:teams][index.to_s.to_sym] = Hash.new
scores[:teams][index.to_s.to_sym][:team] = team
assessments = TeamReviewResponseMap.get_assessments_for(team)
scores[:teams][index.to_s.to_sym][:scores] = Score.compute_scores(assessments, questions[:review])
#... = ScoreCache.get_participant_score(team, id, questionnaire.display_type)
index = index + 1
end
end
#ACS Removed the if condition(and corressponding else) which differentiate assignments as team and individual assignments
# to treat all assignments as team assignments



scores[:teams] = Hash.new
index = 0
self.teams.each do |team|
scores[:teams][index.to_s.to_sym] = Hash.new
scores[:teams][index.to_s.to_sym][:team] = team
assessments = TeamReviewResponseMap.get_assessments_for(team)
scores[:teams][index.to_s.to_sym][:scores] = Score.compute_scores(assessments, questions[:review])
#... = ScoreCache.get_participant_score(team, id, questionnaire.display_type)
index = index + 1
end
scores
end
end

def get_contributor(contrib_id)
AssignmentTeam.find(contrib_id)
Expand Down Expand Up @@ -394,12 +466,12 @@ def check_condition(column, topic_id=nil)
.order('due_at')
next_due_date = next_due_dates.first

return false if next_due_date.nil?
return false if next_due_date.first.nil?

# command pattern - get the attribute with the name in column
# Here, column is usually something like 'review_allowed_id'

right_id = next_due_date.send column
right_id = next_due_date.first.send column

right = DeadlineRight.find(right_id)
(right && (right.name == 'OK' || right.name == 'Late'))
Expand Down Expand Up @@ -915,4 +987,8 @@ def clean_up_due_dates
def has_partner_ads?(id)
Team.find_by_sql("select * from teams where parent_id = "+id+" AND advertise_for_partner='1'").size > 0
end
def scores(id)
scores=Array.new
scores = AssignmentScoreView.find_by_sql ["SELECT team_id, score, assignment_id FROM assignment_score_views WHERE assignment_id = ?", id]
end
end

0 comments on commit 1d5910c

Please sign in to comment.