<commit_before># frozen_string_literal: true

require 'oauth'
require_dependency "#{Rails.root}/lib/wiki_edits"
require_dependency "#{Rails.root}/lib/list_course_manager"
require_dependency "#{Rails.root}/lib/tag_manager"
require_dependency "#{Rails.root}/lib/course_creation_manager"
require_dependency "#{Rails.root}/app/workers/update_course_worker"
require_dependency "#{Rails.root}/app/workers/notify_untrained_users_worker"
require_dependency "#{Rails.root}/app/workers/announce_course_worker"

#= Controller for course functionality
class CoursesController < ApplicationController
  include CourseHelper
  respond_to :html, :json
  before_action :require_permissions, only: %i[notify_untrained
                                               delete_all_weeks]

  ################
  # CRUD methods #
  ################

  def create
    require_signed_in
    course_creation_manager = CourseCreationManager.new(course_params, wiki_params,
                                                        initial_campaign_params,
                                                        instructor_role_description, current_user)

    unless course_creation_manager.valid?
      render json: { message: course_creation_manager.invalid_reason },
             status: :not_found
      return
    end
    @course = course_creation_manager.create
  end

  def update
    validate
    handle_course_announcement(@course.instructors.first)
    slug_from_params if should_set_slug?
    @course.update update_params
    update_boolean_flag :timeline_enabled
    update_boolean_flag :wiki_edits_enabled
    ensure_passcode_set
    UpdateCourseWorker.schedule_edits(course: @course, editing_user: current_user)
    render json: { course: @course }
  end

  def destroy
    validate
    DeleteCourseWorker.schedule_deletion(course: @course, current_user: current_user)
    render json: { success: true }
  end

  # /courses/school/title_(term)
  # /courses/school/title_(term)/subpage
  def show
    @course = find_course_by_slug("#{params[:school]}/#{params[:titleterm]}")
    protect_privacy
    verify_edit_credentials { return }
    set_enrollment_details_in_session

    # Only responds to HTML, so spiders fetching index.php will get a 404.
    respond_to do |format|
      format.html { render }
    end
  end

  def stats
    @course = find_course_by_slug("#{params[:school]}/#{params[:titleterm]}")
    response.headers.delete 'X-Frame-Options'
    respond_to do |format|
      format.html { render layout: 'stats' }
    end
  end

  ##############################
  # Course data json endpoints #
  ##############################

  # /courses/school/title_(term)/course.json
  def course
    set_course
    verify_edit_credentials { return }
  end

  def articles
    set_course<commit_msg>Isolated the action<commit_after># frozen_string_literal: true

require 'oauth'
require_dependency "#{Rails.root}/lib/wiki_edits"
require_dependency "#{Rails.root}/lib/list_course_manager"
require_dependency "#{Rails.root}/lib/tag_manager"
require_dependency "#{Rails.root}/lib/course_creation_manager"
require_dependency "#{Rails.root}/app/workers/update_course_worker"
require_dependency "#{Rails.root}/app/workers/notify_untrained_users_worker"
require_dependency "#{Rails.root}/app/workers/announce_course_worker"

#= Controller for course functionality
class CoursesController < ApplicationController
  include CourseHelper
  respond_to :html, :json
  before_action :require_permissions, only: %i[notify_untrained
                                               delete_all_weeks]

  ################
  # CRUD methods #
  ################

  def create
    require_signed_in
    course_creation_manager = CourseCreationManager.new(course_params, wiki_params,
                                                        initial_campaign_params,
                                                        instructor_role_description, current_user)

    unless course_creation_manager.valid?
      render json: { message: course_creation_manager.invalid_reason },
             status: :not_found
      return
    end
    @course = course_creation_manager.create
  end

  def update
    validate
    handle_course_announcement(@course.instructors.first)
    slug_from_params if should_set_slug?
    @course.update update_params
    update_boolean_flag :timeline_enabled
    update_boolean_flag :wiki_edits_enabled
    ensure_passcode_set
    UpdateCourseWorker.schedule_edits(course: @course, editing_user: current_user)
    render json: { course: @course }
  end

  def destroy
    validate
    DeleteCourseWorker.schedule_deletion(course: @course, current_user: current_user)
    render json: { success: true }
  end

  # /courses/school/title_(term)
  # /courses/school/title_(term)/subpage
  def show
    @course = find_course_by_slug("#{params[:school]}/#{params[:titleterm]}")
    protect_privacy
    verify_edit_credentials { return }
    set_enrollment_details_in_session

    # Only responds to HTML, so spiders fetching index.php will get a 404.
    respond_to do |format|
      format.html { render }
    end
  end

  ##############################
  # Course data json endpoints #
  ##############################

  # /courses/school/title_(term)/course.json
  def course
    set_course
    verify_edit_credentials { return }
  end

  def articles
    set_course
{'fraction_non_alphanumeric': 0.11119416931414688, 'fraction_numerical': 0.0011212857409829938, 'mean_word_length': 3.4749163879598663, '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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1993500', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1493, 'n_words': 292}