diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 336c6937b..66f60d020 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -54,6 +54,11 @@ module ApplicationHelper course.is_excellent? ? true : false end + # 判断课程对成员是否可见 + def visible_course?(course) + (course.is_delete? or (!course.is_public? && !User.current.member_of_course?(course))) ? false : true + end + # 获取项目/课程总分 # 发布缺陷 4分 回复缺陷 1分 提交一次 4分 讨论帖子 2分 回复帖子 1分 发布新闻 1分 def static_project_score obj diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index 097a3c1a4..780d73de2 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -11,6 +11,32 @@ module CoursesHelper #StudentRoles = [5, 10] #AllPeople = StudentRoles+TeacherRoles ## return people count + + # 推荐的精品课程 + def excellent_course_recommend course + q = "%#{course.name.strip}%" + result = find_excelletn_course(q, course) + end + + # 查询符合条件的精品课程 + def find_excelletn_course keywords, course + # 用新课程名去匹配精品课程中按课程名和Tag名 + if !course.is_public? && !User.current.member_of_course?(course) + sql = "SELECT distinct c.* FROM `courses` c, tags t, taggings ts where t.id = ts.tag_id and ts.taggable_id = c.id and c.is_excellent = 1 and is_delete = 0 and + ts.taggable_type = 'Course' and (t.name like '%#{keywords}%'or c.name like '%#{keywords}%')" + resultSetTag = Course.find_by_sql(sql) + else + sql = "SELECT distinct c.* FROM `courses` c, tags t, taggings ts where t.id = ts.tag_id and ts.taggable_id = c.id and c.is_excellent = 1 and is_delete = 0 and + ts.taggable_type = 'Course' and (t.name like '%#{keywords}%'or c.name like '%#{keywords}%') ORDER BY c.updated_at desc limit 0,5;" + resultSetTag = Course.visible.find_by_sql(sql) + end + + end + + # 判断精品课程是否可见,非课程成员无法查看私有课程 + def visible_excellent_course obj + # if course.is_pu + end # 返回x项目成员数量,即roles表中定义的所有成员 def projectCount project diff --git a/app/models/course.rb b/app/models/course.rb index 24e955d15..13e6be0ae 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -72,7 +72,7 @@ class Course < ActiveRecord::Base before_save :self_validate # 公开课程变成私有课程,所有资源都变成私有 after_update :update_files_public,:update_course_ealasticsearch_index,:update_activity - after_create :create_board_sync, :act_as_course_activity, :act_as_course_message,:create_course_ealasticsearch_index + after_create :create_board_sync, :act_as_course_activity, :act_as_course_message,:create_course_ealasticsearch_index, :add_course_ead before_destroy :delete_all_members,:delete_course_ealasticsearch_index safe_attributes 'extra', @@ -384,6 +384,12 @@ class Course < ActiveRecord::Base def act_as_course_message self.course_messages << CourseMessage.new(:user_id => self.tea_id, :course_id => self.id, :viewed => false) end + + # 发布新课导语 + def add_course_ead + + end + #项目与课程分离后,很多课程的名称等信息为空,这些数据信息存储在项目表中!!就是数据兼容的问题 #def name # read_attribute('name') || Project.find_by_identifier(self.extra).try(:name) @@ -405,6 +411,7 @@ class Course < ActiveRecord::Base self.__elasticsearch__.index_document end end + def update_course_ealasticsearch_index if self.is_public == 1 and self.is_delete == 0 #如果是初次更新成为公开或者恢复被删除的情况,会报错,那么这条记录尚未被索引过。没有报错就是更新的其他属性 begin @@ -428,13 +435,17 @@ class Course < ActiveRecord::Base end end + + #动态的更新 + def update_activity + update_course_activity(self.class, self.id) + update_user_activity(self.class, self.id) + update_org_activity(self.class, self.id) + end + end -#动态的更新 -def update_activity - update_course_activity(self.class, self.id) - update_user_activity(self.class, self.id) - update_org_activity(self.class, self.id) -end + + # Delete the previous articles index in Elasticsearch # Course.__elasticsearch__.client.indices.delete index: Course.index_name rescue nil diff --git a/app/views/courses/_recommendation.html.erb b/app/views/courses/_recommendation.html.erb index 8e14e1130..c7e459c95 100644 --- a/app/views/courses/_recommendation.html.erb +++ b/app/views/courses/_recommendation.html.erb @@ -1,41 +1,18 @@ - \ No newline at end of file +<% unless excellent_course_recommend(course).count == 0 %> + +<% end %> + diff --git a/db/migrate/20160118014219_define_excellent_course.rb b/db/migrate/20160118014219_define_excellent_course.rb new file mode 100644 index 000000000..911c14a3e --- /dev/null +++ b/db/migrate/20160118014219_define_excellent_course.rb @@ -0,0 +1,17 @@ +class DefineExcellentCourse < ActiveRecord::Migration + def up + arr = [302,192,370,394,183,361,117,218,379,178,418,203,342,403,225] + for i in 0..arr.length-1 + begin + puts arr[i] + course = Course.find(arr[i]) + course.update_attribute(:is_excellent, true) + rescue + logger.error("Course is not found!") + end + end + end + + def down + end +end \ No newline at end of file