diff --git a/app/controllers/boards_controller.rb b/app/controllers/boards_controller.rb index d4eedb7e3..7952e0c9c 100644 --- a/app/controllers/boards_controller.rb +++ b/app/controllers/boards_controller.rb @@ -19,7 +19,7 @@ class BoardsController < ApplicationController layout 'base_projects'#by young default_search_scope :messages before_filter :find_project_by_project_id, :find_board_if_available, :except => [:join_to_org_subfields] - before_filter :authorize, :except => [:new, :show, :create, :index, :join_to_org_subfields] + before_filter :authorize, :except => [:new, :show, :create, :index, :join_to_org_subfields, :update_position, :update_name] accept_rss_auth :index, :show @@ -56,8 +56,12 @@ class BoardsController < ApplicationController @boards = @course.boards.includes(:last_message => :author).all end end - unless @course.boards.empty? - @board = @course.boards.first + if params[:board_id] + @board = Board.find params[:board_id].to_i + else + unless @course.boards.where("parent_id is NULL").empty? + @board = @course.boards.where("parent_id is NULL").first + end end show and return else @@ -192,18 +196,31 @@ class BoardsController < ApplicationController end end - def create - @board = @project.boards.build - @board.safe_attributes = params[:board] - - if @board.save - flash[:notice] = l(:notice_successful_create) - #Modified by young - #redirect_to_settings_in_projects - redirect_to project_board_url(@project, @board) - #Ended by young - else - render :action => 'new' + def create + if @project + @board = @project.boards.build + @board.safe_attributes = params[:board] + + if @board.save + flash[:notice] = l(:notice_successful_create) + #Modified by young + #redirect_to_settings_in_projects + redirect_to project_board_url(@project, @board) + #Ended by young + else + render :action => 'new' + end + elsif @course + parent = Board.find params[:board_id].to_i + board = @course.boards.build + board.name = params[:name] + board.description = board.name + board.project_id = -1 + board.position = parent.children.count + 1 + parent.children << board + respond_to do |format| + format.js + end end end @@ -224,8 +241,46 @@ class BoardsController < ApplicationController end def destroy + after_boards = @board.parent.children.where("position > #{@board.position}") + after_boards.update_all("position = position - 1") @board.destroy - redirect_to_settings_in_projects + if @course + respond_to do |format| + format.js + end + elsif @project + redirect_to_settings_in_projects + end + end + + def update_position + if @course + boards = @board.parent.children + if params[:opr] == 'up' && @board.position > 1 + before_board = boards.where("position = #{@board.position - 1}").first + if before_board && @board.update_attribute('position', @board.position - 1) + before_board.update_attribute('position', before_board.position + 1) + end + elsif params[:opr] == 'down' && @board.position < boards.count + after_board = boards.where("position = #{@board.position + 1}").first + if after_board && @board.update_attribute('position', @board.position + 1) + after_board.update_attribute('position', after_board.position - 1) + end + end + respond_to do |format| + format.js + end + end + end + + def update_name + if @course + @board.update_attribute("name", params[:name]) + @board.update_attribute("description", params[:name]) + respond_to do |format| + format.js + end + end end def join_to_org_subfields diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 31ca95789..f93599ee5 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -122,7 +122,7 @@ class MessagesController < ApplicationController if @project redirect_to project_boards_path(@project) elsif @course - redirect_to course_boards_path(@course) + redirect_to course_boards_path(@course, :board_id => @board.id) end else redirect_to board_message_url(@board, @message) @@ -132,7 +132,7 @@ class MessagesController < ApplicationController if @project redirect_to project_boards_path(@project, :flag => true) elsif @course - redirect_to course_boards_path(@course, :flag => true) + redirect_to course_boards_path(@course, :board_id => @board.id, :flag => true) end else layout_file = @project ? 'base_projects' : 'base_courses' diff --git a/app/models/board.rb b/app/models/board.rb index b66719edf..d8b29f5cd 100644 --- a/app/models/board.rb +++ b/app/models/board.rb @@ -35,7 +35,7 @@ class Board < ActiveRecord::Base includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args)) } - safe_attributes 'name', 'description', 'parent_id', 'move_to' + safe_attributes 'name', 'description', 'parent_id', 'move_to', 'position' def visible?(user=User.current) !user.nil? && user.allowed_to?(:view_messages, project) @@ -51,7 +51,11 @@ class Board < ActiveRecord::Base end def valid_parents - @valid_parents ||= project.boards - self_and_descendants + if project + @valid_parents ||= project.boards - self_and_descendants + elsif course + @valid_parents ||= course.boards - self_and_descendants + end end def reset_counters! diff --git a/app/views/boards/_course_show.html.erb b/app/views/boards/_course_show.html.erb index 6759e33ce..55d4cca62 100644 --- a/app/views/boards/_course_show.html.erb +++ b/app/views/boards/_course_show.html.erb @@ -22,7 +22,7 @@
<%= l(:label_message_new) %>