Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moments #51

Merged
merged 6 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 30 additions & 31 deletions lib/lanttern/assessments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ defmodule Lanttern.Assessments do
`:preloads` – preloads associated data
`:preload_full_rubrics` – boolean, preloads full associated rubrics using `Rubrics.full_rubric_query/0`
`:assessment_points_ids` – filter result by provided assessment points ids
`:activities_ids` – filter result by provided activities ids
`:activities_from_strand_id` – filter result by activities from provided strand id
`:moments_ids` – filter result by provided moments ids
`:moments_from_strand_id` – filter result by moments from provided strand id
`:strand_id` – filter result by provided strand id

## Examples
Expand Down Expand Up @@ -57,15 +57,15 @@ defmodule Lanttern.Assessments do
defp apply_assessment_points_filter({:assessment_points_ids, ids}, queryable),
do: from(ap in queryable, where: ap.id in ^ids)

defp apply_assessment_points_filter({:activities_ids, ids}, queryable),
do: from(ap in queryable, where: ap.activity_id in ^ids)
defp apply_assessment_points_filter({:moments_ids, ids}, queryable),
do: from(ap in queryable, where: ap.moment_id in ^ids)

defp apply_assessment_points_filter({:activities_from_strand_id, id}, queryable) do
defp apply_assessment_points_filter({:moments_from_strand_id, id}, queryable) do
from(
ap in queryable,
join: a in assoc(ap, :activity),
as: :activity,
where: a.strand_id == ^id
join: m in assoc(ap, :moment),
as: :moment,
where: m.strand_id == ^id
)
end

Expand All @@ -75,21 +75,21 @@ defmodule Lanttern.Assessments do
defp apply_assessment_points_filter(_, queryable), do: queryable

defp order_assessment_points(queryable, opts) do
activities_ids = Keyword.get(opts, :activities_ids)
strand_id = Keyword.get(opts, :activities_from_strand_id)
moments_ids = Keyword.get(opts, :moments_ids)
strand_id = Keyword.get(opts, :moments_from_strand_id)

cond do
activities_ids ->
moments_ids ->
from(
ap in queryable,
join: a in assoc(ap, :activity),
order_by: [a.position, ap.position]
join: m in assoc(ap, :moment),
order_by: [m.position, ap.position]
)

strand_id ->
from(
[ap, activity: a] in queryable,
order_by: [a.position, ap.position]
[ap, moment: m] in queryable,
order_by: [m.position, ap.position]
)

true ->
Expand Down Expand Up @@ -138,7 +138,7 @@ defmodule Lanttern.Assessments do
@doc """
Creates an assessment point.

The function handles the position field based on the learning context (activity or strand),
The function handles the position field based on the learning context (moment or strand),
appending (position = greater position in context + 1) the assessment point to the context.

## Examples
Expand Down Expand Up @@ -181,13 +181,13 @@ defmodule Lanttern.Assessments do
|> Repo.insert()
end

defp filter_assessment_points_by_context(query, %{activity_id: activity_id})
when not is_nil(activity_id) and activity_id != "",
do: from(q in query, where: q.activity_id == ^activity_id)
defp filter_assessment_points_by_context(query, %{moment_id: moment_id})
when not is_nil(moment_id) and moment_id != "",
do: from(q in query, where: q.moment_id == ^moment_id)

defp filter_assessment_points_by_context(query, %{"activity_id" => activity_id})
when not is_nil(activity_id) and activity_id != "",
do: from(q in query, where: q.activity_id == ^activity_id)
defp filter_assessment_points_by_context(query, %{"moment_id" => moment_id})
when not is_nil(moment_id) and moment_id != "",
do: from(q in query, where: q.moment_id == ^moment_id)

defp filter_assessment_points_by_context(query, %{strand_id: strand_id})
when not is_nil(strand_id) and strand_id != "",
Expand Down Expand Up @@ -659,8 +659,7 @@ defmodule Lanttern.Assessments do
@doc """
Returns the list of the assessment point entries for every student in the given strand.

Entries are ordered by `Activity` and `AssessmentPoint` positions,
which is the same order used by `list_strand_assessment_points/1` with `order_by_activities = true` opt.
Entries are ordered by `Moment` and `AssessmentPoint` positions.

## Options:

Expand Down Expand Up @@ -688,13 +687,13 @@ defmodule Lanttern.Assessments do
results =
from(
ap in AssessmentPoint,
join: a in assoc(ap, :activity),
join: m in assoc(ap, :moment),
join: s in subquery(students_query),
on: true,
left_join: e in AssessmentPointEntry,
on: e.student_id == s.id and e.assessment_point_id == ap.id,
where: a.strand_id == ^strand_id,
order_by: [s.name, a.position, ap.position],
where: m.strand_id == ^strand_id,
order_by: [s.name, m.position, ap.position],
select: {s, e}
)
|> Repo.all()
Expand All @@ -717,7 +716,7 @@ defmodule Lanttern.Assessments do
end

@doc """
Returns the list of the assessment point entries for every student in the given activity.
Returns the list of the assessment point entries for every student in the given moment.

Entries are ordered by `AssessmentPoint` position,
which is the same order used by `list_assessment_points/1`.
Expand All @@ -727,11 +726,11 @@ defmodule Lanttern.Assessments do
- `:classes_ids` – filter entries by classes
"""

@spec list_activity_students_entries(integer(), Keyword.t()) :: [
@spec list_moment_students_entries(integer(), Keyword.t()) :: [
{Student.t(), [AssessmentPointEntry.t()]}
]

def list_activity_students_entries(activity_id, opts \\ []) do
def list_moment_students_entries(moment_id, opts \\ []) do
students_query =
case Keyword.get(opts, :classes_ids) do
nil ->
Expand All @@ -752,7 +751,7 @@ defmodule Lanttern.Assessments do
on: true,
left_join: e in AssessmentPointEntry,
on: e.student_id == s.id and e.assessment_point_id == ap.id,
where: ap.activity_id == ^activity_id,
where: ap.moment_id == ^moment_id,
order_by: [s.name, ap.position],
select: {s, e}
)
Expand Down
17 changes: 11 additions & 6 deletions lib/lanttern/assessments/assessment_point.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Lanttern.Assessments.AssessmentPoint do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query, only: [from: 2]
import LantternWeb.Gettext

alias Lanttern.Repo
alias Lanttern.Assessments.AssessmentPointEntry
Expand All @@ -24,7 +25,7 @@ defmodule Lanttern.Assessments.AssessmentPoint do
belongs_to :curriculum_item, Lanttern.Curricula.CurriculumItem
belongs_to :scale, Lanttern.Grading.Scale
belongs_to :rubric, Lanttern.Rubrics.Rubric
belongs_to :activity, Lanttern.LearningContext.Activity
belongs_to :moment, Lanttern.LearningContext.Moment
belongs_to :strand, Lanttern.LearningContext.Strand

has_many :entries, Lanttern.Assessments.AssessmentPointEntry
Expand All @@ -51,7 +52,7 @@ defmodule Lanttern.Assessments.AssessmentPoint do
:curriculum_item_id,
:scale_id,
:rubric_id,
:activity_id,
:moment_id,
:strand_id,
:classes_ids,
:students_ids
Expand All @@ -61,19 +62,23 @@ defmodule Lanttern.Assessments.AssessmentPoint do
|> put_classes()
|> cast_entries()
|> unique_constraint([:strand_id, :curriculum_item_id],
message: "Curriculum item already added to this strand"
message: gettext("Curriculum item already added to this strand")
)
|> foreign_key_constraint(
:rubric_id,
name: :assessment_points_rubric_id_fkey,
message:
"Error linking rubric. Check if it exists and uses the same scale used in the assessment point."
gettext(
"Error linking rubric. Check if it exists and uses the same scale used in the assessment point."
)
)
|> foreign_key_constraint(
:scale_id,
name: :assessment_point_entries_scale_id_fkey,
message:
"You may already have some entries for this assessment point. Changing the scale when entries exist is not allowed, as it would cause data loss."
gettext(
"You may already have some entries for this assessment point. Changing the scale when entries exist is not allowed, as it would cause data loss."
)
)
end

Expand Down Expand Up @@ -183,7 +188,7 @@ defmodule Lanttern.Assessments.AssessmentPoint do
|> foreign_key_constraint(
:id,
name: :assessment_point_entries_assessment_point_id_fkey,
message: "Assessment point has linked entries."
message: gettext("Assessment point has linked entries.")
)
end
end
14 changes: 7 additions & 7 deletions lib/lanttern/curricula.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Lanttern.Curricula do
alias Lanttern.Curricula.CurriculumItem
alias Lanttern.Curricula.CurriculumRelationship

alias Lanttern.LearningContext.Activity
alias Lanttern.LearningContext.Moment

@doc """
Returns the list of curricula.
Expand Down Expand Up @@ -260,23 +260,23 @@ defmodule Lanttern.Curricula do
end

@doc """
Returns the list of curriculum items linked to the given activity.
Returns the list of curriculum items linked to the given moment.

## Options:

- `:preloads` – preloads associated data

## Examples

iex> list_activity_curriculum_items(1)
iex> list_moment_curriculum_items(1)
[%CurriculumItem{}, ...]

"""
def list_activity_curriculum_items(activity_id, opts \\ []) do
def list_moment_curriculum_items(moment_id, opts \\ []) do
from(
a in Activity,
join: ci in assoc(a, :curriculum_items),
where: a.id == ^activity_id,
m in Moment,
join: ci in assoc(m, :curriculum_items),
where: m.id == ^moment_id,
order_by: ci.name,
distinct: ci.id,
select: ci
Expand Down
Loading
Loading