diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -2,6 +2,7 @@ before_action :set_group, only: [:show, :edit, :update, :destroy, :add_user, :remove_user,:remove_all_user, :add_problem, :remove_problem,:remove_all_problem, + :toggle, ] before_action :admin_authorization @@ -49,6 +50,11 @@ redirect_to groups_url, notice: 'Group was successfully destroyed.' end + def toggle + @group.enabled = @group.enabled? ? false : true + @group.save + end + def remove_user user = User.find(params[:user_id]) @group.users.delete(user) @@ -99,6 +105,6 @@ # Only allow a trusted parameter "white list" through. def group_params - params.require(:group).permit(:name, :description) + params.require(:group).permit(:name, :description, :enabled) end end diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -188,7 +188,7 @@ @user = user end end - + protected def prepare_announcements(recent=nil) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -113,8 +113,9 @@ def toggle_button(on,toggle_url,id, option={}) btn_size = option[:size] || 'btn-xs' + btn_block = option[:block] || 'btn-block' link_to (on ? "Yes" : "No"), toggle_url, - {class: "btn btn-block #{btn_size} btn-#{on ? 'success' : 'default'} ajax-toggle", + {class: "btn #{btn_block} #{btn_size} btn-#{on ? 'success' : 'default'} ajax-toggle", id: id, data: {remote: true, method: 'get'}} end diff --git a/app/models/user.rb b/app/models/user.rb --- a/app/models/user.rb +++ b/app/models/user.rb @@ -273,9 +273,10 @@ end end + # new feature, get list of available problem in all enabled group that the user belongs to def available_problems_in_group problem = [] - self.groups.each do |group| + self.groups.where(enabled: true).each do |group| group.problems.where(available: true).each { |p| problem << p } end problem.uniq! @@ -296,6 +297,8 @@ end end + #check if the user has the right to view that problem + #this also consider group based problem policy def can_view_problem?(problem) return true if admin? return available_problems.include? problem diff --git a/app/views/application/_submission.html.haml b/app/views/application/_submission.html.haml --- a/app/views/application/_submission.html.haml +++ b/app/views/application/_submission.html.haml @@ -10,7 +10,7 @@ %td = submission.source_filename = " (#{submission.language.pretty_name}) " - = link_to('[load]',{:action => 'source', :id => submission.id}) + = link_to '[load]', download_submission_path(submission) %td - if submission.graded_at = "Graded at #{format_short_time(submission.graded_at)}." diff --git a/app/views/groups/_form.html.haml b/app/views/groups/_form.html.haml --- a/app/views/groups/_form.html.haml +++ b/app/views/groups/_form.html.haml @@ -5,12 +5,23 @@ %ul - @group.errors.full_messages.each do |msg| %li= msg - - .form-group.field - = f.label :name - = f.text_field :name, class: 'form-control' - .form-group.field - = f.label :description - = f.text_field :description, class: 'form-control' - .form-group.actions - = f.submit 'Save', class: 'btn btn-primary' + .row + .col-md-6 + .form-group.field + = f.label :name + = f.text_field :name, class: 'form-control' + .row + .col-md-6 + .form-group.field + = f.label :description + = f.text_field :description, class: 'form-control' + .row + .col-md-6 + .checkbox + = f.label :enabled do + = f.check_box :enabled + Enabled + .row + .col-md-6 + .form-group.actions + = f.submit 'Save', class: 'btn btn-primary' diff --git a/app/views/groups/index.html.haml b/app/views/groups/index.html.haml --- a/app/views/groups/index.html.haml +++ b/app/views/groups/index.html.haml @@ -7,14 +7,16 @@ %tr %th Name %th Description + %th Enabled? %th %th %tbody - @groups.each do |group| - %tr + %tr{:class => "#{(group.enabled?) ? "success" : "danger"}", id: "group-#{group.id}"} %td= group.name %td= group.description + %td= toggle_button(group.enabled?, toggle_group_path(group), "group-enabled-#{group.id}", size: ' ', block: ' ') %td= link_to 'View', group, class: 'btn btn-default' %td= link_to 'Destroy', group, :method => :delete, :data => { :confirm => 'Are you sure?' }, class: 'btn btn-danger' diff --git a/app/views/groups/toggle.js.haml b/app/views/groups/toggle.js.haml new file mode 100644 --- /dev/null +++ b/app/views/groups/toggle.js.haml @@ -0,0 +1,8 @@ += render partial: 'toggle_button', + locals: {button_id: "#group-enabled-#{@group.id}",button_on: @group.enabled } +:plain + r = $("#group-#{@group.id}"); + r.removeClass('success'); + r.removeClass('danger'); + r.addClass("#{@group.enabled? ? 'success' : 'danger'}"); + diff --git a/config/routes.rb b/config/routes.rb --- a/config/routes.rb +++ b/config/routes.rb @@ -54,6 +54,7 @@ post 'add_problem', to: 'groups#add_problem', as: 'add_problem' delete 'remove_problem/:problem_id', to: 'groups#remove_problem', as: 'remove_problem' delete 'remove_all_problem', to: 'groups#remove_all_problem', as: 'remove_all_problem' + get 'toggle' end collection do @@ -92,7 +93,6 @@ get 'download' get 'compiler_msg' get 'rejudge' - get 'source' end collection do get 'prob/:problem_id', to: 'submissions#index', as: 'problem' diff --git a/db/migrate/20200813083020_add_enabled_to_group.rb b/db/migrate/20200813083020_add_enabled_to_group.rb new file mode 100644 --- /dev/null +++ b/db/migrate/20200813083020_add_enabled_to_group.rb @@ -0,0 +1,5 @@ +class AddEnabledToGroup < ActiveRecord::Migration[5.2] + def change + add_column :groups, :enabled, :boolean, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_06_12_102327) do +ActiveRecord::Schema.define(version: 2020_08_13_083020) do create_table "announcements", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "author" @@ -80,6 +80,7 @@ create_table "groups", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "name" t.string "description" + t.boolean "enabled", default: true end create_table "groups_problems", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t|