Skip to content

Commit

Permalink
feat: add récursive dispo in planning.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dim145 committed Dec 13, 2024
1 parent b10412d commit 54fb57a
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 39 deletions.
26 changes: 21 additions & 5 deletions app/controllers/parameters/planning_parameters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,38 @@ def get_hours_before_cancelling_activity
end
end

def show_activity_code
render json: { show_activity_code: Parameter.get_value("planning.card.show_activity_code", default: false) }
def school_planning_params
authorize! :manage, Parameter

render json: {
show_activity_code: Parameter.get_value("planning.card.show_activity_code", default: false),
recurrence_activated: Parameter.get_value("planning.recurrence_activated", default: false)
}
end

def update_show_activity_code
def update_school_planning_params
authorize! :manage, Parameter

show_activity_code = Parameter.find_or_create_by(
label: "planning.card.show_activity_code",
value_type: "boolean"
)

show_activity_code.value = (params[:show_activity_code]&.to_s == "true").to_s

saved = show_activity_code.save!
show_activity_code.save!

recurrence_activated = Parameter.find_or_create_by(
label: "planning.recurrence_activated",
value_type: "boolean"
)

recurrence_activated.value = (params[:recurrence_activated]&.to_s == "true").to_s

recurrence_activated.save!

respond_to do |format|
format.json { render json: { success: saved } }
format.json { render json: { success: true } }
end
end
end
2 changes: 2 additions & 0 deletions app/controllers/planning_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class PlanningController < ApplicationController
skip_before_action :verify_authenticity_token, only: [:update_availabilities]

before_action -> { @recurrence_activated = Parameter.get_value("planning.recurrence_activated", default: false) }, only: %i[show show_generic show_all_rooms show_for_conflict show_for_room]

def index_for_teachers
@current_user = current_user
plannings = Planning.includes(:user).where(users: { is_teacher: true })
Expand Down
41 changes: 38 additions & 3 deletions app/models/planning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,54 @@ def update_intervals(intervals, season_id)
end

intervals.each do |i|
# isRecurrent if only a param on planning when new interval is created
if i["recurrentType"]&.present?
interval = TimeInterval.new(start: i["start"], end: i["end"], kind: i["kind"] || "d", is_validated: i["is_validated"] || false)

season = Season.from_interval(interval).first || Season.current

recurrence_end = season.end
recurrence_step = case i["recurrentType"]
when "weekly"
1.week
when "biweekly"
2.weeks
when "monthly"
1.month
when "bimonthly"
2.months
when "yearly"
1.year
else
1.week
end

holiday_dates = season.holidays.map { |h| h.date }

while interval.end < recurrence_end
next if holiday_dates.include?(interval.start&.to_date)
intervalList << interval if interval.save

interval = interval.dup
interval.id = nil
interval.start += recurrence_step
interval.end += recurrence_step
end
else
if i["isNew"] == true
interval = TimeInterval.new
interval = TimeInterval.new
else
interval = TimeInterval.find_or_initialize_by(id: i['id'])
interval = TimeInterval.find_or_initialize_by(id: i['id'])
end
interval.start = i["start"]
interval.end = i["end"]
if !season.nil?
interval.convert_to_first_week_of_season(season)
interval.convert_to_first_week_of_season(season)
end
interval.kind = i["kind"] || "d"
interval.is_validated = i["is_validated"] || false
interval.save
end

if self.time_intervals.where(id: interval.id).none?
intervalList << interval
Expand Down
3 changes: 2 additions & 1 deletion app/views/planning/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@
show_availabilities: @show_availabilities,
teacher_can_edit: @teacher_can_edit,
currentUserId: current_user&.id,
show_activity_code: @show_activity_code
show_activity_code: @show_activity_code,
recurrenceActivated: @recurrence_activated
}) %>
2 changes: 1 addition & 1 deletion app/views/planning/show_all_rooms.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="clearfix">
<h2><%= @rooms.select {|r| r.id == room_id.to_i}.first.label %></h2>
<%= react_component("planning/Planning", { show_activity_code: @show_activity_code, room: @rooms.select {|r| r.id == room_id}.first, isTeacher: current_user.teacher?, isAdmin: current_user.admin?, displayOnly: true, intervals: time_intervals, rooms: @rooms, modal: false, levels: @levels, locations: @locations, room_refs: @rooms, isRoom: true, planning: time_intervals, activity_refs: @activity_refs, teachers: @teachers,
currentUserId: current_user&.id}) %>
currentUserId: current_user&.id, recurrenceActivated: @recurrence_activated}) %>
</div>
<% end %>

Expand Down
3 changes: 2 additions & 1 deletion app/views/planning/show_for_conflict.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
season: @season,
conflict: @conflict,
currentUserId: current_user&.id,
show_activity_code: @show_activity_code
show_activity_code: @show_activity_code,
recurrenceActivated: @recurrence_activated
}) %>
3 changes: 2 additions & 1 deletion app/views/planning/show_for_room.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
teachers: @teachers,
displayRaw: true,
currentUserId: current_user&.id,
show_activity_code: @show_activity_code
show_activity_code: @show_activity_code,
recurrenceActivated: @recurrence_activated
}) %>
3 changes: 2 additions & 1 deletion app/views/planning/show_generic.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
day: @day,
generic: true,
currentUserId: current_user&.id,
show_activity_code: @show_activity_code
show_activity_code: @show_activity_code,
recurrenceActivated: @recurrence_activated
}) %>

4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
get "planning_parameters", to: "planning_parameters#index"
post "planning_parameters", to: "planning_parameters#update"

get "show_activity_code", to: "planning_parameters#show_activity_code"
post "show_activity_code", to: "planning_parameters#update_show_activity_code"
get "planning/school_planning_params", to: "planning_parameters#school_planning_params"
post "planning/school_planning_params", to: "planning_parameters#update_school_planning_params"

get "hours_before_cancelling_activity", to: "planning_parameters#get_hours_before_cancelling_activity"
post "hours_before_cancelling_activity", to: "planning_parameters#save_hours_before_cancelling_activity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import swal from "sweetalert2";
export default function PlanningDisplayParameters()
{
const [showActivityCode, setShowActivityCode] = React.useState(false);
const [recurrenceActivated, setRecurrenceActivated] = React.useState(false);

useEffect(() =>
{
api.set()
.success((data) =>
{
setShowActivityCode(data.show_activity_code);
setRecurrenceActivated(data.recurrence_activated);
})
.error(() =>
{
Expand All @@ -20,7 +22,7 @@ export default function PlanningDisplayParameters()
type: "error",
});
})
.get("/parameters/show_activity_code", {});
.get("/parameters/planning/school_planning_params", {});
}, []);

const onSubmit = (e) =>
Expand All @@ -40,7 +42,10 @@ export default function PlanningDisplayParameters()
type: "error",
});
})
.post("/parameters/show_activity_code", { show_activity_code: showActivityCode });
.post("/parameters/planning/school_planning_params", {
show_activity_code: showActivityCode,
recurrence_activated: recurrenceActivated
});
};

return <div className="row">
Expand All @@ -49,10 +54,18 @@ export default function PlanningDisplayParameters()

<div className="form-group mb-3">

<input id="show_activity_code" type="checkbox" checked={showActivityCode} onChange={() => setShowActivityCode(!showActivityCode)} />
<input id="show_activity_code" type="checkbox" checked={showActivityCode}
onChange={() => setShowActivityCode(!showActivityCode)} />
<label htmlFor="show_activity_code" className="ml-2 font-normal">Afficher le code de l'activité</label>
</div>

<div className="form-group mb-3">

<input id="recurrence_activated" type="checkbox" checked={recurrenceActivated}
onChange={() => setRecurrenceActivated(!recurrenceActivated)} />
<label htmlFor="recurrence_activated" className="ml-2 font-normal">Permettre la récurrence des disponibilités</label>
</div>

<button className="btn btn-success pull-right mt-5" onClick={onSubmit}>Valider</button>
</div>
</div>;
Expand Down
Loading

0 comments on commit 54fb57a

Please sign in to comment.