Skip to content

Commit

Permalink
Merge branch 'media-search' into mampf-next
Browse files Browse the repository at this point in the history
  • Loading branch information
zebleck committed Jan 18, 2023
2 parents 9f6f60c + e2ece97 commit 4fa9e8d
Show file tree
Hide file tree
Showing 35 changed files with 977 additions and 151 deletions.
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,6 @@ GEM
sprockets (> 3.0)
sprockets-rails
tilt
select2-rails (4.0.13)
selectize-rails (0.12.6)
selenium-webdriver (4.1.0)
childprocess (>= 0.5, < 5.0)
rexml (~> 3.2, >= 3.2.5)
Expand Down Expand Up @@ -735,7 +733,6 @@ DEPENDENCIES
rubocop-rails
rubyzip (~> 2.3.0)
sass-rails (>= 6)
select2-rails
selenium-webdriver
shrine
sidekiq
Expand Down
4 changes: 2 additions & 2 deletions app/abilities/medium_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def initialize(user)
user ||= User.new
clear_aliased_actions

can [:index, :new], Medium
can [:index, :new, :search], Medium

can [:show, :show_comments], Medium do |medium|
medium.visible_for_user?(user) &&
Expand All @@ -28,7 +28,7 @@ def initialize(user)
user.can_edit?(medium.teachable)
end

can [:search, :fill_teachable_select, :fill_media_select,
can [:fill_teachable_select, :fill_media_select,
:fill_medium_preview, :render_medium_actions,
:render_import_media, :render_import_vertex,
:cancel_import_media, :cancel_import_vertex,
Expand Down
23 changes: 23 additions & 0 deletions app/assets/javascripts/media.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ $(document).on 'turbolinks:load', ->
selector.enable()
return

# disable search fields associated with radio buttons
prev = null
$('[type="radio"]').on 'change', ->

try
selector = document.getElementById(this.dataset.id).tomselect
if $(this).prop('checked')
selector.enable()
else
selector.disable()
catch e

try
selector = document.getElementById(prev.dataset.id).tomselect
if $(prev).prop('checked')
selector.enable()
else
selector.disable()
catch e

prev = this
return

# issue a warning if an input has been changed in the media form
# extract the teachable type and id from the teachable selection and
# store it in hidden fields' values
Expand Down
64 changes: 54 additions & 10 deletions app/controllers/media_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ def publish
@errors = publisher.errors
return if @errors.present?
@medium.update(publisher: publisher)
@medium.publish! if publisher.release_now
if publisher.release_now
@medium.publish!
@medium.update(released: publisher.release_for, released_at: Time.zone.now,
publisher: nil)
end
redirect_to edit_medium_path(@medium)
end

Expand Down Expand Up @@ -207,17 +211,49 @@ def inspect
# return all media that match the search parameters
def search
authorize! :search, Medium.new

# get all media, then set them to only those that are visible to the current user
if current_user.generic? || search_params[:access].blank?
filter_media = true
params["search"]["access"] = 'irrelevant'
end
params["search"]["answers_count"] = 'irrelevant' if search_params[:answers_count].blank?

search = Medium.search_by(search_params, params[:page])
search.execute
results = search.results
@total = search.total

# in the case of a search with tag_operator 'or', we
# execute two searches and merge the results, where media
# with the selected tags are now shown at the front of the list
if search_params[:tag_operator] == "or" and search_params[:all_tags] == "0" and search_params[:fulltext].size >= 2
params["search"]["all_tags"] = '1'
search_no_tags = Medium.search_by(search_params, params[:page])
search_no_tags.execute
results_no_tags = search_no_tags.results
results = (results + results_no_tags).uniq
@total = results.size
params["search"]["all_tags"] = '0'
end

if filter_media
search_arel = Medium.where(id: results.pluck(:id))
visible_search_results = current_user.filter_visible_media(search_arel)
results &= visible_search_results
@total = results.size
end

@media = Kaminari.paginate_array(results, total_count: @total)
.page(params[:page]).per(search_params[:per])
@purpose = search_params[:purpose]
@results_as_list = search_params[:results_as_list] == 'true'
if @purpose.in?(['quiz', 'import'])
render template: "media/catalog/import_preview"
return
end
return unless @total.zero?
return unless search_params[:fulltext]&.length.to_i > 1
end

# play the video using thyme player
Expand Down Expand Up @@ -608,19 +644,27 @@ def sanitize_per!
end

def search_params
types = params[:search][:types]
types = params[:search][:types] || []
types = [types] if types && !types.kind_of?(Array)
types -= [''] if types
types = nil if types == []
params[:search][:types] = types
params.require(:search).permit(:all_types, :all_teachables, :all_tags,
:all_editors, :tag_operator, :quiz, :access,
:teachable_inheritance, :fulltext, :per,
:clicker, :purpose, :answers_count,
types: [],
teachable_ids: [],
tag_ids: [],
editor_ids: [])
params[:search][:user_id] = current_user.id
params.require(:search)
.permit(:all_types, :all_teachables, :all_tags,
:all_editors, :tag_operator, :quiz, :access,
:teachable_inheritance, :fulltext, :per,
:clicker, :purpose, :answers_count,
:results_as_list, :all_terms, :all_teachers,
:lecture_option, :user_id,
types: [],
teachable_ids: [],
tag_ids: [],
editor_ids: [],
term_ids: [],
teacher_ids: [],
media_lectures: [])
#.with_defaults(access: 'all')
end

# destroy all notifications related to this medium
Expand Down
10 changes: 0 additions & 10 deletions app/helpers/lectures_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,6 @@ def lecture_border(lecture)
'border-danger'
end

def lecture_hits_per_page(results_as_list)
return [[10, 10], [20, 20], [50, 50]] if results_as_list
[[3,3],[4,4],[6,6], [12,12]]
end

def default_lecture_hits_per_page(results_as_list)
return 20 if results_as_list
6
end

def lecture_access_icon(lecture)
return lecture_edit_icon if current_user.can_edit?(lecture)
lecture_view_icon
Expand Down
1 change: 1 addition & 0 deletions app/helpers/media_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def media_sorts_select(purpose)
return add_prompt(Medium.select_quizzables) if purpose == 'quiz'
return Medium.select_question if purpose == 'clicker'
return add_prompt(Medium.select_importables) if purpose == 'import'
return add_prompt(Medium.select_generic) if !current_user.admin?
add_prompt(Medium.select_sorts)
end

Expand Down
10 changes: 10 additions & 0 deletions app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ module SearchHelper
def plural_n(tags, filtered_tags)
(tags.count - filtered_tags.count) > 1 ? 'n' : ''
end

def hits_per_page(results_as_list)
return [[10, 10], [20, 20], [50, 50]] if results_as_list
[[3,3],[4,4],[6,6], [12,12]]
end

def default_hits_per_page(results_as_list)
return 20 if results_as_list
6
end
end
5 changes: 5 additions & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ def users
.pluck(:user_id).uniq)
end

def user_ids
User.where(id: LectureUserJoin.where(lecture: lectures)
.pluck(:user_id).uniq).pluck(:id)
end

# a course is addable by the user if the user is an editor or teacher of
# this course or a lecture of this course
def addable_by?(user)
Expand Down
3 changes: 3 additions & 0 deletions app/models/lecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class Lecture < ApplicationRecord
end
end

def self.select
Lecture.all.map { |l| [l.title, l.id] }
end

# The next methods coexist for lectures and lessons as well.
# Therefore, they can be called on any *teachable*
Expand Down
Loading

0 comments on commit 4fa9e8d

Please sign in to comment.