<commit_before>class ProblemsController < ApplicationController
	before_action :logged_in_user
	before_action :admin_user, only: [:destroy, :create, :edit, :update, :new, :remove_hint, :add_hint]
	before_action :belong_to_team, only: [:index, :show]
	before_action :competition_active, only: [:index, :show]
	
	def index
		@problems = Problem.all
		if params[:problem_id]
			@problem_view = Problem.find(params[:problem_id])
		end
	end

	def new
		@problem = Problem.new
	end

	def create
		@problem = Problem.new(problem_params)
		if @problem.save
      redirect_to problems_url
		else
			render 'new'
		end
	end

	def show
		@problem = Problem.find(params[:id])
		redirect_to controller: 'problems', action: 'index', anchor: "heading_#{(@problem.id-1).to_s}", problem_id: @problem.id
	end

	def remove_hint
		# Hint implements reference counting (i.e., don't worry about it here)
		@problem = Problem.find(params[:problem_id])
		@problem.remove(params[:hint_id])
    flash[:success] = "Hint removed successfully"
    redirect_to edit_problem_path(@problem)
	end

	def add_hint
		# Hint implements reference counting (i.e., don't worry about it here)
		if (@problem = Problem.find(params[:problem_id])) && Hint.find(params[:hint_id])
			@problem.add(params[:hint_id])
			flash[:success] = "Hint added successfully"
			render json: { status: :ok }
		else
			flash[:error] = "Hint or Problem ID invalid"
			redirect_to problems_path
		end
	end

	def destroy
    Problem.find(params[:id]).destroy
    flash[:success] = "Problem deleted"
    redirect_to problems_url
  end

	def edit
		@problem = Problem.find(params[:id])
		@hints = Hint.all
	end

	def update
		@problem = Problem.find(params[:id])
		if @problem.update_attributes(problem_params)
			flash[:success] = "Changes saved successfully"
			redirect_to problems_url
		else
			render 'edit'
		end
	end

	private
		def problem_params
			params.require(:problem).permit(:name, :category, :description, :points, :solution, :correct_message, :false_message, :picture)
		end

		def logged_in_user
			unless logged_in?<commit_msg>When team is destroyed, updates all users<commit_after>class ProblemsController < ApplicationController
	before_action :logged_in_user
	before_action :admin_user, only: [:destroy, :create, :edit, :update, :new, :remove_hint, :add_hint]
	before_action :belong_to_team, only: [:index, :show]
	before_action :competition_active, only: [:index, :show]
	
	def index
		@problems = Problem.all
		if params[:problem_id]
			@problem_view = Problem.find(params[:problem_id])
		end
	end

	def new
		@problem = Problem.new
	end

	def create
		@problem = Problem.new(problem_params)
		if @problem.save
      redirect_to problems_url
		else
			render 'new'
		end
	end

	def show
		@problem = Problem.find(params[:id])
		redirect_to controller: 'problems', action: 'index', anchor: "heading_#{(@problem.id-1).to_s}", problem_id: @problem.id
	end

	def remove_hint
		# Hint implements reference counting (i.e., don't worry about it here)
		@problem = Problem.find(params[:problem_id])
		@problem.remove(params[:hint_id])
    flash[:success] = "Hint removed successfully"
    redirect_to edit_problem_path(@problem)
	end

	def add_hint
		# Hint implements reference counting (i.e., don't worry about it here)
		if (@problem = Problem.find(params[:problem_id])) && Hint.find(params[:hint_id])
			@problem.add(params[:hint_id])
			flash[:success] = "Hint added successfully"
			render json: { status: :ok }
		else
			flash[:error] = "Hint or Problem ID invalid"
			redirect_to problems_path
		end
	end

	def destroy
		# Remove team from all members
		users = User.where(team_id = params[:id])
		for user in users
			user.update_attributes(team_id: nil)
		end

		# Destroy team
    Problem.find(params[:id]).destroy
    flash[:success] = "Problem deleted"
    redirect_to problems_url
  end

	def edit
		@problem = Problem.find(params[:id])
		@hints = Hint.all
	end

	def update
		@problem = Problem.find(params[:id])
		if @problem.update_attributes(problem_params)
			flash[:success] = "Changes saved successfully"
			redirect_to problems_url
		else
			render 'edit'
		end
	end

	private
		def problem_params
			params.require(:problem).permit(:name, :category, :description, :points, :solution, :correct_message, :false_message, :picture)
		end

		def logged_in_user
			unless logged_in?
{'fraction_non_alphanumeric': 0.11565877213152449, 'fraction_numerical': 0.0004598758335249483, 'mean_word_length': 4.534351145038168, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '510069', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1412, 'n_words': 370}