-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restricted notes: Clarify that admin can also mark notes as restricted
- Loading branch information
1 parent
104ebb0
commit ac3bb74
Showing
2 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
app/importers/mtss_referral/student_voice_mid_year_importer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Usage: | ||
# file_text = <<EOD | ||
# ... | ||
# EOD | ||
# importer = StudentVoiceMidYearImporter.new | ||
# rows = importer.process(file_text);nil | ||
# event_notes = rows.map {|row| EventNote.create!(row) };nil | ||
class StudentVoiceMidYearImporter | ||
def initialize(options = {}) | ||
@log = options.fetch(:log, Rails.env.test? ? LogHelper::Redirect.instance.file : STDOUT) | ||
@strptime_format = options.fetch(:strptime_format, ImportMatcher::GOOGLE_FORM_EXPORTED_TO_GOOGLE_SHEETS_TIMESTAMP_FORMAT) | ||
|
||
@matcher = ImportMatcher.new | ||
@fuzzy_student_matcher = FuzzyStudentMatcher.new | ||
reset_counters! | ||
end | ||
|
||
def process(file_text) | ||
reset_counters! | ||
|
||
event_note_hashes = [] | ||
create_streaming_csv(file_text).each_with_index do |row, index| | ||
maybe_row_attrs = process_row_or_nil(row) | ||
next if maybe_row_attrs.nil? | ||
event_note_hashes << maybe_row_attrs | ||
@valid_hashes_count += 1 | ||
end | ||
|
||
event_note_hashes | ||
end | ||
|
||
private | ||
def create_streaming_csv(file_text) | ||
csv_transformer = StreamingCsvTransformer.new(@log, { | ||
csv_options: { header_converters: nil } | ||
}) | ||
csv_transformer.transform(file_text) | ||
end | ||
|
||
# Map `row` into `EventNote` attributes, adding in `note_title` to each note | ||
# and otherwise just flattening the row into flat text. | ||
def process_row_or_nil(row) | ||
# match student by id first, fall back to name | ||
exact_match = @matcher.find_student_id(row['Student ID Number']) | ||
fuzzy_match = @fuzzy_student_matcher.match_from_full_name(row['Student Name (eg, Sofia Alonso Martinez)']) | ||
if fuzzy_match.nil? | ||
@invalid_student_name_count += 1 | ||
@invalid_student_names_list << student_name | ||
return nil | ||
end | ||
student_id = fuzzy_match[:student_id] | ||
|
||
# match educator | ||
educator_id = @matcher.find_educator_id(row['Email Address']) | ||
|
||
# timestamp | ||
recorded_at = DateTime.strptime(row['Timestamp'], @strptime_format) | ||
|
||
# flatten the rest of the fields | ||
{ | ||
student_id: student_id, | ||
educator_id: educator_id, | ||
recorded_at: recorded_at, | ||
is_restricted: false, | ||
event_note_type_id: 304, | ||
text: flattened_note_text(row) | ||
} | ||
end | ||
|
||
def flattened_note_text(row) | ||
note_title = 'MTSS Referral Form' | ||
text_from_fields = flat_text_from_fields(row, [ | ||
'Timestamp', | ||
'Email Address', | ||
'Student Name (eg, Sofia Alonso Martinez)', | ||
'Referring Staff Member' | ||
]) | ||
"#{note_title}\n\n#{text_from_fields}" | ||
end | ||
|
||
def flat_text_from_fields(raw_row, exclude_fields) | ||
bits = [] | ||
row = raw_row.to_h | ||
(row.keys - exclude_fields).each do |key| | ||
next if row[key].nil? || row[key].empty? || row[key] == '' | ||
bits << "#{key}\n#{row[key]}" | ||
end | ||
bits.join("\n\n") | ||
end | ||
|
||
def stats | ||
{ | ||
valid_hashes_count: @valid_hashes_count, | ||
invalid_row_columns_count: @invalid_row_columns_count, | ||
invalid_student_name_count: @invalid_student_name_count, | ||
invalid_student_names_list: @invalid_student_names_list | ||
} | ||
end | ||
|
||
def reset_counters! | ||
@valid_hashes_count = 0 | ||
@invalid_row_columns_count = 0 | ||
@invalid_student_name_count = 0 | ||
@invalid_student_names_list = [] | ||
end | ||
|
||
def log(msg) | ||
text = if msg.class == String then msg else JSON.pretty_generate(msg) end | ||
@log.puts "StudentVoiceMidYearImporter: #{text}" | ||
end | ||
end |