From ce7b2ceba093403c47abd19361b187ebc5e01019 Mon Sep 17 00:00:00 2001 From: yanxd Date: Mon, 4 Nov 2013 09:45:56 +0800 Subject: [PATCH] fix teacher/student search Identifier --- app/helpers/courses_helper.rb | 53 ++++++++++++++++++++++++------ app/views/projects/member.html.erb | 4 +-- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index fac4f5b68..e21d66031 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -1,31 +1,64 @@ ## This helper be included in applicationHelper module CoursesHelper - # 返回学生数量,即roles表中定义的Reporter, 返回结果 -1 为查询失败 +=begin + 1. define TeacherRoles, StudentRoles + 2. define count function + 3. define search by roles + 4. define search member function +=end + TeacherRoles = [3, 4, 7, 9] + StudentRoles = [5, 10] + + + # 返回学生数量,即roles表中定义的Reporter def studentCount project - searchCountByRoles project, 5 + searchStudent(project).count end - # 返回教师数量,即roles表中定义的Manager, 返回结果 -1 为查询失败 + # 返回教师数量,即roles表中定义的Manager def teacherCount project - searchCountByRoles project, 3 + searchTeacherAndAssistant(project).count end - # 返回TA数量,即roles表中定义的TA, 返回结果 -1 为查询失败 - def teacherAssistantCount project - searchCountByRoles project, 7 + # ===================================================================================== + + def searchTeacherAndAssistant project + members = [] + TeacherRoles.each do |role| + members += searchPeopleByRoles(project, role) + end + members end - # 根据角色查询 + def searchStudent project + members = [] + StudentRoles.each do |role| + members += searchPeopleByRoles(project, role) + end + members + end + # ===================================================================================== + #useless def searchCountByRoles project, roles_id people = searchPeopleByRoles project, roles_id people.count end - # 根据角色查人 + #useless + def searchPeopleByRole project, role_id + people = [] + begin + people = project.members.joins(:member_roles).where("member_roles.role_id = :role_id", {:role_id => role_id }) + rescue Exception => e + logger.error "[CoursesHelper] ===> #{e}" + end + people + end + def searchPeopleByRoles project, roles_id people = [] begin - people = project.members.joins(:member_roles).where("member_roles.role_id = :role_id", {:role_id => roles_id }) + people = project.members.joins(:member_roles).where("member_roles.role_id IN (:role_id)", {:role_id => roles_id}) rescue Exception => e logger.error "[CoursesHelper] ===> #{e}" end diff --git a/app/views/projects/member.html.erb b/app/views/projects/member.html.erb index 83c668744..c922399e7 100644 --- a/app/views/projects/member.html.erb +++ b/app/views/projects/member.html.erb @@ -8,8 +8,8 @@ <% roles = Role.find_all_givable %> <% members = @project.member_principals.includes(:roles, :principal).all.sort %> - <% members = searchPeopleByRoles(@project, 3).sort if params[:role] == '1' %> - <% members = searchPeopleByRoles(@project, 5).sort if params[:role] == '2' %> + <% members = searchTeacherAndAssistant(@project) if params[:role] == '1' %> + <% members = searchStudent(@project) if params[:role] == '2' %> <% if @project.project_type == 1 %> <%= render :partial => 'member_list', :locals => {:members => members} %> <% else %>