<commit_before>class StudentsController < ApplicationController
  include SerializeDataHelper
  include ApplicationHelper

  rescue_from Exceptions::EducatorNotAuthorized, with: :redirect_unauthorized!

  before_action :authorize!, except: [          # The :names and lasids actions are subject to
    :names, :lasids                             # educator authentication via :authenticate_educator!
  ]                                             # inherited from ApplicationController.
                                                # They then get further authorization filtering using
                                                # The custom authorization methods below.

  before_action :authorize_for_districtwide_access_admin, only: [:lasids]

  def authorize!
    student = Student.find(params[:id])
    raise Exceptions::EducatorNotAuthorized unless current_educator.is_authorized_for_student(student)
  end

  def authorize_for_districtwide_access_admin
    unless current_educator.admin? && current_educator.districtwide_access?
      render json: { error: "You don't have the correct authorization." }
    end
  end

  def show
    student = Student.find(params[:id])
    chart_data = StudentProfileChart.new(student).chart_data

    @serialized_data = {
      current_educator: current_educator,
      student: serialize_student_for_profile(student),          # Risk level, school homeroom, most recent school year attendance/discipline counts
      feed: student_feed(student, restricted_notes: false),     # Notes, services
      chart_data: chart_data,                                   # STAR, MCAS, discipline, attendance charts
      dibels: student.student_assessments.by_family('DIBELS'),
      service_types_index: service_types_index,
      event_note_types_index: event_note_types_index,
      educators_index: Educator.to_index,
      access: student.latest_access_results,
      attendance_data: student_profile_attendance_data(student.student_school_years)
    }
  end

  def restricted_notes
    raise Exceptions::EducatorNotAuthorized unless current_educator.can_view_restricted_notes

    student = Student.find(params[:id])
    @serialized_data = {
      current_educator: current_educator,
      student: serialize_student_for_profile(student),
      feed: student_feed(student, restricted_notes: true),
      event_note_types_index: event_note_types_index,
      educators_index: Educator.to_index,
    }
  end

  def student_report
    set_up_student_report_data
    respond_to do |format|
      format.pdf do
        footer = "Somerville Public Schools Student Report -- Generated #{format_date(todays_date)} by #{current_educator.full_name} -- Page [page] of [topage]"
        render({
          pdf: 'student_report', 
          title: 'Student Report', 
          footer: { center: footer, font_name: 'Open Sans', font_size: 9}, 
          javascript_delay: 1000,
          show_as_html: Rails.env.development? && params.key?('debug')
        })
      end
    end
  end

  # post
  def service
    clean_params = params.require(:service).permit(*[
      :student_id,
      :service_type_id,
      :date_started,
      :provided_by_educator_name
    ])
    service = Service.new(clean_params.merge({
      recorded_by_educator_id: current_educator.id,
      recorded_at: Time.now
    }))
    if service.save
      render json: serialize_service(service)<commit_msg>Allow PDF debug param in production;<commit_after>class StudentsController < ApplicationController
  include SerializeDataHelper
  include ApplicationHelper

  rescue_from Exceptions::EducatorNotAuthorized, with: :redirect_unauthorized!

  before_action :authorize!, except: [          # The :names and lasids actions are subject to
    :names, :lasids                             # educator authentication via :authenticate_educator!
  ]                                             # inherited from ApplicationController.
                                                # They then get further authorization filtering using
                                                # The custom authorization methods below.

  before_action :authorize_for_districtwide_access_admin, only: [:lasids]

  def authorize!
    student = Student.find(params[:id])
    raise Exceptions::EducatorNotAuthorized unless current_educator.is_authorized_for_student(student)
  end

  def authorize_for_districtwide_access_admin
    unless current_educator.admin? && current_educator.districtwide_access?
      render json: { error: "You don't have the correct authorization." }
    end
  end

  def show
    student = Student.find(params[:id])
    chart_data = StudentProfileChart.new(student).chart_data

    @serialized_data = {
      current_educator: current_educator,
      student: serialize_student_for_profile(student),          # Risk level, school homeroom, most recent school year attendance/discipline counts
      feed: student_feed(student, restricted_notes: false),     # Notes, services
      chart_data: chart_data,                                   # STAR, MCAS, discipline, attendance charts
      dibels: student.student_assessments.by_family('DIBELS'),
      service_types_index: service_types_index,
      event_note_types_index: event_note_types_index,
      educators_index: Educator.to_index,
      access: student.latest_access_results,
      attendance_data: student_profile_attendance_data(student.student_school_years)
    }
  end

  def restricted_notes
    raise Exceptions::EducatorNotAuthorized unless current_educator.can_view_restricted_notes

    student = Student.find(params[:id])
    @serialized_data = {
      current_educator: current_educator,
      student: serialize_student_for_profile(student),
      feed: student_feed(student, restricted_notes: true),
      event_note_types_index: event_note_types_index,
      educators_index: Educator.to_index,
    }
  end

  def student_report
    set_up_student_report_data
    respond_to do |format|
      format.pdf do
        footer = "Somerville Public Schools Student Report -- Generated #{format_date(todays_date)} by #{current_educator.full_name} -- Page [page] of [topage]"
        render({
          pdf: 'student_report', 
          title: 'Student Report', 
          footer: { center: footer, font_name: 'Open Sans', font_size: 9}, 
          javascript_delay: 1000,
          show_as_html: params.key?('debug')
        })
      end
    end
  end

  # post
  def service
    clean_params = params.require(:service).permit(*[
      :student_id,
      :service_type_id,
      :date_started,
      :provided_by_educator_name
    ])
    service = Service.new(clean_params.merge({
      recorded_by_educator_id: current_educator.id,
      recorded_at: Time.now
    }))
    if service.save
      render json: serialize_service(service)
{'fraction_non_alphanumeric': 0.07341065922771986, 'fraction_numerical': 0.0014682131845543973, 'mean_word_length': 2.9743290548424737, '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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '959897', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1827, 'n_words': 413}