116 lines
4.1 KiB
Crystal
116 lines
4.1 KiB
Crystal
# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
|
|
# Copyright (C) 2022 Dominic Grimm
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
require "random/secure"
|
|
|
|
module Backend
|
|
module Worker
|
|
module Jobs
|
|
# Assigns students to teachers when all students voted
|
|
class AssignStudentsJob < Mosquito::QueuedJob
|
|
def rescheduleable?
|
|
false
|
|
end
|
|
|
|
# :ditto:
|
|
def perform : Nil
|
|
user_count = Db::User.query.count
|
|
teacher_count = Db::Teacher.query.count
|
|
student_count = Db::Student.query.count
|
|
vote_count = Db::Vote.query.count
|
|
|
|
if user_count == 0
|
|
log "No users found, skipping assignment"
|
|
fail
|
|
elsif teacher_count == 0
|
|
log "No teachers found, skipping assignment"
|
|
fail
|
|
elsif student_count == 0
|
|
log "No students found, skipping assignment"
|
|
fail
|
|
elsif student_count != Db::User.query.where(role: Db::UserRole::Student).count
|
|
log "Not all students registered, skipping assignment"
|
|
fail
|
|
elsif vote_count < student_count
|
|
log "Not all students voted, skipping assignment"
|
|
fail
|
|
end
|
|
|
|
# possibilities = [] of Possibility
|
|
# teachers = Db::Teacher.query.to_a.select! { |t| t.assignments.count < t.max_students }
|
|
# empty_assignment = Hash.zip(teachers.map(&.id), [[] of StudentAssignment] * teachers.size)
|
|
# random_votes = Db::Student.query.to_a.select!(&.vote).map do |s|
|
|
# {
|
|
# student: s.id,
|
|
# teachers: s.vote.not_nil!.teacher_votes.to_a
|
|
# .sort_by!(&.priority)
|
|
# .reverse!
|
|
# .map(&.teacher.id),
|
|
# }
|
|
# end
|
|
|
|
# Backend.config.assignment_retry_count.times do
|
|
# pp! random_votes.shuffle!(Random::Secure)
|
|
# a = empty_assignment.clone
|
|
# random_votes.
|
|
|
|
# possibilities << {assignment: a, weighting: 0}
|
|
# end
|
|
|
|
# pp! possibilities
|
|
|
|
# teacher_ids = Db::Teacher.query
|
|
# .select("id")
|
|
# .where { raw("(SELECT COUNT(*) FROM assignments WHERE teacher_id = teachers.id)") < max_students }
|
|
# .map(&.id)
|
|
students = Db::Student.query
|
|
.where do
|
|
raw("NOT EXISTS (SELECT 1 FROM assignments WHERE student_id = students.id)") &
|
|
raw("EXISTS (SELECT 1 FROM votes WHERE student_id = students.id)")
|
|
end
|
|
.with_vote(&.with_teacher_votes(&.order_by(:priority, :asc)))
|
|
# student_ids = students.map(&.id)
|
|
|
|
# votes = Hash.zip(
|
|
# student_ids,
|
|
# students.map do |s|
|
|
# s.vote.not_nil!.teacher_votes.map(&.teacher_id)
|
|
# end,
|
|
# )
|
|
# pp! votes, student_ids
|
|
|
|
random_votes = students.map do |s|
|
|
{
|
|
student: s.id,
|
|
teachers: s.vote.not_nil!.teacher_votes.map(&.teacher_id),
|
|
}
|
|
end
|
|
|
|
assignments = [] of {assignment: Hash(Int32, Array(Int32)), weighting: Int32}
|
|
# empty_assignment = Hash.zip(teacher_ids, [[] of {assignment: Hash(Int32, Array(Int32)), weighting: Int32}] * teacher_ids.size)
|
|
Backend.config.assignment_possibility_count.times do
|
|
random_votes.shuffle!(Random::Secure)
|
|
|
|
# votes = random_votes.dup
|
|
# a = empty_assignment.clone
|
|
end
|
|
pp! assignments
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|