<commit_before>class TeachersController < ApplicationController

  def create
    school = School.find_by(id: params[:id])
    # TODO, create auth function that we can use in this controller to verify admin rights.
    if SchoolsAdmins.find_by(school: school, user: current_user)
      @teacher = User.find_by(email: teacher_params[:email])
      if @teacher
        # Teacher exists.
        if SchoolsUsers.find_by(user: @teacher, school: school)
          # Teacher is already in the school, let the admin know.
          message = "#{teacher_params[:first_name]} #{teacher_params[:last_name]} is already registered to #{school.name}."
        else
          # Send invite to the school to the teacher via email.
          message = "An email has been sent to #{teacher_params[:email]} asking them to join #{school.name}."
          JoinSchoolEmailWorker.perform_async(@teacher.id, school.id)
        end
      else
        # Create a new teacher, and automatically join them to the school.
        teacher_attributes = teacher_params.merge({password: teacher_params[:last_name]})
        @teacher = school.users.create(teacher_attributes)
        AccountCreatedEmailWorker.perform_async(@teacher.id, teacher_params[:last_name], current_user.name)
        message = "An email has been sent to #{teacher_params[:email]} asking them to set up their account."
      end
      if @teacher.errors.empty?
        # Return the message to the admin
        render json: {message: message}, status: 200
      else
         # Return errors if there are any.
        render json: @teacher.errors, status: 422
      end
    else
      render json: {errors: 'Something went wrong. If this problem persists, please contact us at <EMAIL>'}, status: 422
    end
  end

  # Called when a teacher accepts an invite to the school. This is sent to them via email.
  def add_school
    if current_user.present? && current_user == User.find_by(id: params[:id])
      # Current user is the user that was invited.
      school = School.find(params[:school_id])
      # User should only belong to one school, so we find their existing link and update the school.
      school_user = SchoolsUsers.find_or_initialize_by(user_id: params[:id])
      school_user.school = school
      if school_user.save
        # redirect to profile with confirmation
        redirect_to profile_path, flash: {notice: "You have successfully joined #{school.name}."}
      else
        # redirect to profile with errors.
        redirect_to profile_path, flash: school_user.errors
      end
    else
      # User is not signed in or they are not the invitee, redirect to sign in page
      redirect_to new_session_path, flash: {error: "You must be signed in to add a school."}
    end
  end

  def admin_dashboard
    if current_user.present? && current_user.admin?
      render 'admin'
    else
      redirect_to profile_path
    end
  end

  def current_user_json
    render json: current_user.to_json
  end

  def classrooms_i_teach_with_students
    render json: {classrooms: current_user.classrooms_i_teach_with_students}
  end

  def classrooms_i_own_with_students
    render json: {classrooms: current_user.classrooms_i_own_with_students}
  end

  def classrooms_i_teach_with_lessons
    classrooms =  ActiveRecord::Base.connection.execute(
      "SELECT DISTINCT classrooms.* FROM classrooms
       JOIN classrooms_teachers ON classrooms_teachers.classroom_id = classrooms.id
       JOIN classroom_units ON classroom_units.classroom_id = classrooms.id
       JOIN units ON classroom_units.unit_id = units.id
       JOIN unit_activities ON unit_activities.unit_id = units.id
       JOIN activities ON activities.id = unit_activities.activity_id
       WHERE activities.activity_classification_id = 6
            AND classrooms_teachers.user_id = #{current_user.id}"
    ).to_a
    render json: {classrooms: classrooms}
  end

  def update_current_user
    if current_user.update(teacher_params)
      render json: current_user
    else
      render json: {errors: current_user.errors}, status: 400
    end
  end

  def get_completed_diagnostic_unit_info
    begin
      last_finished_diagnostic = current_user.finished_diagnostic_unit_ids_with_classroom_id_and_activity_id.first
      unit_info = { unit_id: last_finished_diagnostic.unit_id, classroom_id: last_finished_diagnostic.classroom_id, activity_id: last_finished_diagnostic.activity_id }
    rescue
      unit_info = {}
    end
    render json: {unit_info: unit_info}
  end

  def get_diagnostic_info_for_dashboard_mini
    records = ActiveRecord::Base.connection.execute("SELECT cu.id AS classroom_unit_id,
      units.id AS unit_id,
      classroom.id AS classroom_id,
      acts.id AS activity_id,
      actsesh.completed_at FROM classroom_units cu
               JOIN units ON cu.unit_id = units.id
               JOIN classrooms AS classroom ON cu.classroom_id = classroom.id
               JOIN activity_sessions AS actsesh ON actsesh.classroom_unit_id = cu.id
               JOIN activities AS acts ON actsesh.activity_id = acts.id
               WHERE units.user_id = #{current_user.id}
               AND acts.activity_classification_id = 4
               ORDER BY actsesh.completed_at DESC").to_a
    if records.length > 0
      most_recently_completed = records.find { |r| r['completed_at'] != nil }
      # checks to see if the diagnostic was completed within a week
      if most_recently_completed && 1.week.ago < most_recently_completed['completed_at']
        number_of_finished_students = ActiveRecord::Base.connection.execute("SELECT COUNT(actsesh.id) FROM activity_sessions actsesh
                              JOIN classroom_units AS cu ON actsesh.classroom_unit_id = cu.id
                              WHERE cu.id = #{ActiveRecord::Base.sanitize(most_recently_completed['classroom_unit_id'])}
                              AND actsesh.state = 'finished'
                              AND actsesh.visible = 'true'
                              AND cu.visible = 'true'").to_a.first['count']<commit_msg>update the query for students who have finished the diagnostic<commit_after>class TeachersController < ApplicationController

  def create
    school = School.find_by(id: params[:id])
    # TODO, create auth function that we can use in this controller to verify admin rights.
    if SchoolsAdmins.find_by(school: school, user: current_user)
      @teacher = User.find_by(email: teacher_params[:email])
      if @teacher
        # Teacher exists.
        if SchoolsUsers.find_by(user: @teacher, school: school)
          # Teacher is already in the school, let the admin know.
          message = "#{teacher_params[:first_name]} #{teacher_params[:last_name]} is already registered to #{school.name}."
        else
          # Send invite to the school to the teacher via email.
          message = "An email has been sent to #{teacher_params[:email]} asking them to join #{school.name}."
          JoinSchoolEmailWorker.perform_async(@teacher.id, school.id)
        end
      else
        # Create a new teacher, and automatically join them to the school.
        teacher_attributes = teacher_params.merge({password: teacher_params[:<PASSWORD>]})
        @teacher = school.users.create(teacher_attributes)
        AccountCreatedEmailWorker.perform_async(@teacher.id, teacher_params[:last_name], current_user.name)
        message = "An email has been sent to #{teacher_params[:email]} asking them to set up their account."
      end
      if @teacher.errors.empty?
        # Return the message to the admin
        render json: {message: message}, status: 200
      else
         # Return errors if there are any.
        render json: @teacher.errors, status: 422
      end
    else
      render json: {errors: 'Something went wrong. If this problem persists, please contact us at <EMAIL>'}, status: 422
    end
  end

  # Called when a teacher accepts an invite to the school. This is sent to them via email.
  def add_school
    if current_user.present? && current_user == User.find_by(id: params[:id])
      # Current user is the user that was invited.
      school = School.find(params[:school_id])
      # User should only belong to one school, so we find their existing link and update the school.
      school_user = SchoolsUsers.find_or_initialize_by(user_id: params[:id])
      school_user.school = school
      if school_user.save
        # redirect to profile with confirmation
        redirect_to profile_path, flash: {notice: "You have successfully joined #{school.name}."}
      else
        # redirect to profile with errors.
        redirect_to profile_path, flash: school_user.errors
      end
    else
      # User is not signed in or they are not the invitee, redirect to sign in page
      redirect_to new_session_path, flash: {error: "You must be signed in to add a school."}
    end
  end

  def admin_dashboard
    if current_user.present? && current_user.admin?
      render 'admin'
    else
      redirect_to profile_path
    end
  end

  def current_user_json
    render json: current_user.to_json
  end

  def classrooms_i_teach_with_students
    render json: {classrooms: current_user.classrooms_i_teach_with_students}
  end

  def classrooms_i_own_with_students
    render json: {classrooms: current_user.classrooms_i_own_with_students}
  end

  def classrooms_i_teach_with_lessons
    classrooms =  ActiveRecord::Base.connection.execute(
      "SELECT DISTINCT classrooms.* FROM classrooms
       JOIN classrooms_teachers ON classrooms_teachers.classroom_id = classrooms.id
       JOIN classroom_units ON classroom_units.classroom_id = classrooms.id
       JOIN units ON classroom_units.unit_id = units.id
       JOIN unit_activities ON unit_activities.unit_id = units.id
       JOIN activities ON activities.id = unit_activities.activity_id
       WHERE activities.activity_classification_id = 6
            AND classrooms_teachers.user_id = #{current_user.id}"
    ).to_a
    render json: {classrooms: classrooms}
  end

  def update_current_user
    if current_user.update(teacher_params)
      render json: current_user
    else
      render json: {errors: current_user.errors}, status: 400
    end
  end

  def get_completed_diagnostic_unit_info
    begin
      last_finished_diagnostic = current_user.finished_diagnostic_unit_ids_with_classroom_id_and_activity_id.first
      unit_info = { unit_id: last_finished_diagnostic.unit_id, classroom_id: last_finished_diagnostic.classroom_id, activity_id: last_finished_diagnostic.activity_id }
    rescue
      unit_info = {}
    end
    render json: {unit_info: unit_info}
  end

  def get_diagnostic_info_for_dashboard_mini
    records = ActiveRecord::Base.connection.execute("SELECT cu.id AS classroom_unit_id,
      units.id AS unit_id,
      classroom.id AS classroom_id,
      acts.id AS activity_id,
      actsesh.completed_at FROM classroom_units cu
               JOIN units ON cu.unit_id = units.id
               JOIN classrooms AS classroom ON cu.classroom_id = classroom.id
               JOIN activity_sessions AS actsesh ON actsesh.classroom_unit_id = cu.id
               JOIN activities AS acts ON actsesh.activity_id = acts.id
               WHERE units.user_id = #{current_user.id}
               AND acts.activity_classification_id = 4
               ORDER BY actsesh.completed_at DESC").to_a
    if records.length > 0
      most_recently_completed = records.find { |r| r['completed_at'] != nil }
      # checks to see if the diagnostic was completed within a week
      if most_recently_completed && 1.week.ago < most_recently_completed['completed_at']
        number_of_finished_students = ActiveRecord::Base.connection.execute("SELECT COUNT(actsesh.user_id) FROM activity_sessions actsesh
                              JOIN classroom_units AS cu ON actsesh.classroom_unit_id = cu.id
                              WHERE cu.id = #{ActiveRecord::Base.sanitize(most_recently_completed['classroom_unit_id'])}
                              AND actsesh.state = 'finished'
                              AND actsesh.visible = 'true'
                              AND cu.visible = 'true'
                              GROUP BY actsesh.user_id
                              ").to_a.length
{'fraction_non_alphanumeric': 0.0694478527607362, 'fraction_numerical': 0.0026175869120654395, 'mean_word_length': 2.907318632150847, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5759117', 'n_tokens_mistral': 3530, 'n_tokens_neox': 3411, 'n_words': 1014}