Skip to content

Commit

Permalink
Merge pull request #56 from cs169/teacher-homeschool-feature
Browse files Browse the repository at this point in the history
Teacher homeschool feature
  • Loading branch information
ArushC authored Apr 26, 2024
2 parents 2eb837f + 2af33f6 commit 204bf60
Show file tree
Hide file tree
Showing 23 changed files with 247 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ gem "activerecord-import", require: false

gem "httparty", "~> 0.21.0"


gem "activestorage"
gem "country_select", "~> 8.0"

group :development do
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ PLATFORMS

DEPENDENCIES
activerecord-import
activestorage
annotate
aws-sdk-s3
axe-core-cucumber
Expand Down
24 changes: 23 additions & 1 deletion app/controllers/teachers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ def edit
@readonly = !is_admin?
end

def remove_file
file_attachment = @teacher.files.find(params[:file_id])
file_attachment.purge
flash[:notice] = "File was successfully removed"
redirect_back fallback_location: teacher_path(@teacher)
end

def upload_file
@teacher.files.attach(params[:file])
redirect_back fallback_location: teacher_path(@teacher), notice: "File was successfully uploaded"
end

def update
load_school
ordered_schools
Expand All @@ -117,6 +129,7 @@ def update
return
end

attach_new_files_if_any
send_email_if_application_status_changed_and_email_resend_enabled

if fail_to_update
Expand Down Expand Up @@ -255,9 +268,18 @@ def load_school
@school ||= School.find_or_create_by(**unique_school_params)
end

def attach_new_files_if_any
if params.dig(:teacher, :more_files).present?
params[:teacher][:more_files].each do |file|
@teacher.files.attach(file)
end
end
end

def teacher_params
teacher_attributes = [:first_name, :last_name, :school, :status, :snap,
:more_info, :personal_website, :education_level, :school_id, languages: []]
:more_info, :personal_website, :education_level, :school_id, languages: [], files: [],
more_files: []]
admin_attributes = [:application_status, :request_reason, :skip_email]
teacher_attributes.push(*admin_attributes) if is_admin?

Expand Down
14 changes: 14 additions & 0 deletions app/javascript/styles/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ body {
}
}

.btn-purple {
background-color: #8A2BE2; /* Light purple */
}

/* Style for the box around the file upload link */
.box-link {
display: inline-block;
padding: 5px 10px;
height: 40px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}

.btn-blue {
background-color: #9dc0ee;
}
Expand Down
5 changes: 5 additions & 0 deletions app/models/teacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Teacher < ApplicationRecord
WORLD_LANGUAGES = [ "Afrikaans", "Albanian", "Arabic", "Armenian", "Basque", "Bengali", "Bulgarian", "Catalan", "Cambodian", "Chinese (Mandarin)", "Croatian", "Czech", "Danish", "Dutch", "English", "Estonian", "Fiji", "Finnish", "French", "Georgian", "German", "Greek", "Gujarati", "Hebrew", "Hindi", "Hungarian", "Icelandic", "Indonesian", "Irish", "Italian", "Japanese", "Javanese", "Korean", "Latin", "Latvian", "Lithuanian", "Macedonian", "Malay", "Malayalam", "Maltese", "Maori", "Marathi", "Mongolian", "Nepali", "Norwegian", "Persian", "Polish", "Portuguese", "Punjabi", "Quechua", "Romanian", "Russian", "Samoan", "Serbian", "Slovak", "Slovenian", "Spanish", "Swahili", "Swedish ", "Tamil", "Tatar", "Telugu", "Thai", "Tibetan", "Tonga", "Turkish", "Ukrainian", "Urdu", "Uzbek", "Vietnamese", "Welsh", "Xhosa" ].freeze

has_many :email_addresses, dependent: :destroy
has_many_attached :files
has_many_attached :more_files
accepts_nested_attributes_for :email_addresses, allow_destroy: true

validates :first_name, :last_name, :status, presence: true
Expand Down Expand Up @@ -83,6 +85,7 @@ class Teacher < ApplicationRecord
developer: 6,
excite: 7,
middle_school_bjc: 8,
home_school_bjc: 9
}

# Always add to the bottom of the list!
Expand All @@ -96,6 +99,7 @@ class Teacher < ApplicationRecord
"I am a BJC curriculum or tool developer.",
"I am teaching with the ExCITE project",
"I am teaching Middle School BJC.",
"I am teaching homeschool with the BJC curriculum."
].freeze

# From an admin perspective, we want to know if a teacher has any **meaningful** change
Expand Down Expand Up @@ -150,6 +154,7 @@ def self.status_options
:excite,
:teals_teacher,
:teals_volunteer,
:home_school_bjc,
:other,
:developer,
]
Expand Down
2 changes: 1 addition & 1 deletion app/views/schools/_selectize_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="form-group">
<%= f.label :name, "Search for your school or add a new one", class: "label-required", for: "school_selectize" %>
<%= f.select :name,
options_for_select(@ordered_schools.collect(&:selectize_options)),
options_for_select( @ordered_schools.map(&:selectize_options)),
{prompt: "Enter the name or city of your school"},
{class: "select", id: "school_selectize" } %>
<%= javascript_pack_tag 'schools' %>
Expand Down
33 changes: 33 additions & 0 deletions app/views/teachers/_files_display.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div class="col-sm-8">
<% if teacher.files.attached? %>
<% teacher.files.each do |file| %>
<div class='form-group row'>
<%= link_to file.filename.to_s, rails_blob_path(file), target: "_blank", class: "box-link" %>
<% if show_delete_file %>
<%= button_to "❌", remove_file_teacher_path(teacher, file_id: file.id), method: :delete, data: { confirm: "Are you sure you want to remove this file?" }, style: "width: 40px;", class: "box-link" %>
<% end %>
</div>
<% end %>
<% else %>
<p>No files attached yet.</p>
<% end %>

<!-- Add file button -->
<% if show_add_file %>
<%= form_tag(upload_file_teacher_path(teacher), method: :post, multipart: true, id: "file-upload-form") do %>
<%= file_field_tag :file, id: "file-upload-field", style: "display: none;" %>
<label for="file-upload-field" class="btn btn-primary btn-purple mr-2">Add a File</label>
<% end %>
<% end %>
</div>

<% if show_add_file %>
<script>
$(document).ready(function() {
$('#file-upload-field').on('change', function() {
// Submit the form when a file is selected
$('#file-upload-form').submit();
});
});
</script>
<% end %>
82 changes: 55 additions & 27 deletions app/views/teachers/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,35 @@ status ONLY IF the person viewing this page is an admin. %>
:status,
options_for_select(Teacher.status_options, teacher.status_before_type_cast),
{ include_blank: "Select an option" },
{ class: 'form-control', required: true, onchange: 'requireVolunteerHostTeacher();' }
{ class: 'form-control', required: true, onchange: 'listenForStatusOptionChange();' }
) %>
</div>
</div>

<!-- File upload input field -->
<% if is_new_teacher_page %>
<div class='form-group row' id = 'upload_file_field'>
<div class='col-12'>
<%= f.label :files, "Upload supporting files: " %>
<%= f.file_field :files, multiple: true, direct_upload: true %>
</div>
</div>
<% else %>
<!-- Note that edit teacher page CANNOT allow direct adding/removing
of files because the nested forms result in undefined
behavior in Rails, including random controller
actions getting called from unrelated submit buttons -->
<div class='form-group row' id = 'upload_file_field'>
<div class='col-12'>
<label for="uploaded_files">Supporting files:</label>
<%= render 'files_display', teacher: @teacher, show_add_file: false, show_delete_file: false %>
</div>
<div class='col-12'>
<%= f.label :more_files, "Upload More Files: " %>
<%= f.file_field :more_files, multiple: true, direct_upload: true %>
</div>
</div>
<% end %>
<div class='form-group row'>
<div class='col-12'>
<%= f.label :education_level, "What's your education level target?", class: "label-required" %>
Expand All @@ -117,37 +141,35 @@ status ONLY IF the person viewing this page is an admin. %>
multiple: true,
include_blank: "Select an option",
class: 'selectize', required: true
) %>
) %>
</div>
</div>



<div class='form-group row'>
<div class='col-12'>
<%= f.label :more_info, "More Information", class: "label-required" %>
<%= f.text_area :more_info, placeholder: "I applied for access because...",
class: "form-control", required: true, rows: "2" %>
<%= f.label :more_info, "More Information", class: "label-required" %>
<%= f.text_area :more_info, placeholder: "I applied for access because...",
class: "form-control", required: true, rows: "2" %>
</div>
<small class="col-12 form-text text-muted"><strong id='teacher-more-info-reminder'>Please tell us why you need access.</strong></small>
</div>
<%= f.hidden_field :school_id %>
</div>
<%= f.hidden_field :school_id %>
</div>

<div class='form-group form-input-group'>
<%= form_with model: @teacher.school, local: true do |school| %>
<h2>Your School</h2>
<%= render 'schools/selectize_form', f: school %>
<%# NO END HERE -- NOTE BELOW %>
</div>

<%= f.submit (f.object.new_record? ? 'Submit' : 'Update'), class: 'btn btn-primary' %>
<%# TWO ENDS MUST COME AFTER SUBMIT -- Rails bug??? %>
<div class='form-group form-input-group' id='school_container'>
<%= form_with model: @teacher.school, local: true do |school| %>
<h2>Your School</h2>
<%= render 'schools/selectize_form', f: school %>
</div>

<%= f.submit (f.object.new_record? ? 'Submit' : 'Update'), class: 'btn btn-primary' %>
<!-- This apparent Rails bug of requiring two ends after a submit is likely related
to the use of the nested forms (here, the school form inside the teacher form)
which can result in inexplicable, and sometimes undefined behavior -->
<% end %>
<% end %>

<script>

$(document).ready(function() {
$('.selectize').selectize({
plugins: ['remove_button'],
Expand All @@ -156,8 +178,9 @@ status ONLY IF the person viewing this page is an admin. %>
create: false
});
//on loading the page, immediately update the reason field depending on
//the value of the application status
//the value of the application status and check teacher status option
updateReasonField();
listenForStatusOptionChange();
});

//JS to update the text description for the denial/request reason input
Expand Down Expand Up @@ -186,16 +209,21 @@ status ONLY IF the person viewing this page is an admin. %>
}
}

function requireVolunteerHostTeacher() {
var status_val = $( "#teacher_status" ).val();

function listenForStatusOptionChange() {
var status_val = $("#teacher_status").val();
debugger;
<%# For TEALS volunteer, we also ask for the name of their host teacher. %>
if (status_val == 3) {
$('#upload_file_field').hide();
$("#teacher-more-info-reminder").text("Please tell us why you need access, and include the name of your host teacher here.");
}
else {
} else {
$("#teacher-more-info-reminder").text("Please tell us why you need access.");
}

<%# If homeschool, then show upload file field %>
if (status_val == 9) {
$('#upload_file_field').show();
} else {
$('#upload_file_field').hide();
}
}
}
</script>
6 changes: 6 additions & 0 deletions app/views/teachers/_teacher_info.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
<%= teacher.display_application_status %>
</div>
</div>
<% if teacher.status_before_type_cast == 9 %>
<div class="row mb-4">
<div class="col-sm-4 font-weight-bold">Supporting Files:</div>
<%= render 'files_display', teacher: @teacher, show_add_file: true, show_delete_file: true %>
</div>
<% end %>
<div class="row mb-4">
<div class="col-sm-4 font-weight-bold">Personal or Course Website:</div>
<div class="col-sm-8">
Expand Down
2 changes: 1 addition & 1 deletion app/views/teachers/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
<hr/>
</div>
<div id='sign-up-form-container'>
<%= render 'form', teacher: @teacher, admin: @is_admin %>
<%= render 'form', teacher: @teacher, admin: @is_admin, is_new_teacher_page: false %>
</div>
2 changes: 1 addition & 1 deletion app/views/teachers/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<div id='sign-up-form-container'>
<%# If submission was successful, don't re-render the form. %>
<% if !flash[:success] %>
<%= render 'form', teacher: @teacher, admin: @is_admin %>
<%= render 'form', teacher: @teacher, admin: @is_admin, is_new_teacher_page: true %>
<% end %>
</div>
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# require "action_text/engine"
require "action_view/railtie"
# require "action_cable/engine"
require "active_storage/engine"
# require "sprockets/railtie"
# require "rails/test_unit/railtie"

Expand Down
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@

# Store files locally
# config.active_storage.service = :amazon
# config.active_storage.service = :local
config.active_storage.service = :local
end
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,5 @@
}

# Store files on Amazon S3. (Uncomment this when S3 is setup)
# config.active_storage.service = :amazon
config.active_storage.service = :amazon
end
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@
# config.action_view.annotate_rendered_view_with_filenames = true

# Store uploaded files on the local file system in a temporary directory.
# config.active_storage.service = :test
config.active_storage.service = :test
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
root to: "main#index"

resources :teachers do
post "upload_file", on: :member
resource :email_address, only: [:edit, :update, :create]
member do
post :resend_welcome_email
post :validate
post :deny
post :request_info
delete "remove_file", to: "teachers#remove_file"
end
collection { post :import }
end
Expand Down
2 changes: 1 addition & 1 deletion db/seed_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def self.create_schools
city: "Berkeley",
country: "US",
website: "https://bjc.berkeley.edu",
state: "CA",
state: "CA"
)
end

Expand Down
Loading

0 comments on commit 204bf60

Please sign in to comment.