<commit_before>class StudentsController < ApplicationController
  include SerializeDataHelper

  rescue_from Exceptions::EducatorNotAuthorized, with: :redirect_unauthorized!

  before_action :authorize!, except: [:names]

  def authorize!
    student = Student.find(params[:id])
    raise Exceptions::EducatorNotAuthorized unless current_educator.is_authorized_for_student(student)
  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)
    }
  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 sped_referral
    set_up_sped_data
    respond_to do |format|
      format.pdf do
        render pdf: "sped_referral"
      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)
    else
      render json: { errors: service.errors.full_messages }, status: 422
    end
  end

  # Used by the search bar to query for student names
  def names
    q = params[:q]
    authorized_students = Student.with_school.select do |student|
      current_educator.is_authorized_for_student(student)
    end
    sorted_students = search_and_score(q, authorized_students)
    truncated_students = sorted_students.take(20)
    @sorted_results = truncated_students.map {|student| student.decorate.presentation_for_autocomplete }

    respond_to do |format|
      format.json { render json: @sorted_results }
    end
  end

  private

  def student_profile_attendance_data(student)
    student_school_years = student.student_school_years
    {
      discipline_incidents: flatmap_and_sort(student_school_years) {|year| year.discipline_incidents },
      tardies: flatmap_and_sort(student_school_years) {|year| year.tardies },
      absences: flatmap_and_sort(student_school_years) {|year| year.absences }<commit_msg>Add this year counts to student attendance data hash<commit_after>class StudentsController < ApplicationController
  include SerializeDataHelper

  rescue_from Exceptions::EducatorNotAuthorized, with: :redirect_unauthorized!

  before_action :authorize!, except: [:names]

  def authorize!
    student = Student.find(params[:id])
    raise Exceptions::EducatorNotAuthorized unless current_educator.is_authorized_for_student(student)
  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)
    }
  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 sped_referral
    set_up_sped_data
    respond_to do |format|
      format.pdf do
        render pdf: "sped_referral"
      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)
    else
      render json: { errors: service.errors.full_messages }, status: 422
    end
  end

  # Used by the search bar to query for student names
  def names
    q = params[:q]
    authorized_students = Student.with_school.select do |student|
      current_educator.is_authorized_for_student(student)
    end
    sorted_students = search_and_score(q, authorized_students)
    truncated_students = sorted_students.take(20)
    @sorted_results = truncated_students.map {|student| student.decorate.presentation_for_autocomplete }

    respond_to do |format|
      format.json { render json: @sorted_results }
    end
  end

  private

  def student_profile_attendance_data(student)
    student_school_years = student.student_school_years
    most_recent_school_year = student.most_recent_school_year

    return {
      discipline_incidents: flatmap_and_sort(student_school_years) {|year| year.discipline_incidents },
      tardies: flatmap_and_sort(student_school_years) {|year| year.tardies },
      absences: flatmap_and_sort(student_school_years) {|year| year.absences },
      discipline_incidents_this_school_year: most_recent_school_year.discipline_incidents.count,
      tardies_this_school_year: most_recent_school_year.tardies.count,
      abseneces_this_school_year: most_recent_school_year.absences.count,
{'fraction_non_alphanumeric': 0.0728952772073922, 'fraction_numerical': 0.0014667057788207685, 'mean_word_length': 3.952069716775599, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2749713', 'n_tokens_mistral': 2114, 'n_tokens_neox': 2045, 'n_words': 395}