link to student manager class - closes #9650

pull/9651/head
Thibault Duplessis 2021-08-25 11:39:54 +02:00
parent 3dcfd09f1c
commit 86d7f1dc8d
4 changed files with 48 additions and 13 deletions

View File

@ -437,9 +437,10 @@ final class Clas(env: Env, authC: Auth) extends LilaController(env) {
Secure(_.Teacher) { implicit ctx => me =>
WithClassAndStudents(me, id) { (clas, students) =>
WithStudent(clas, username) { s =>
env.activity.read.recent(s.user, 14) map { activity =>
views.html.clas.student.show(clas, students, s, activity)
}
for {
withManagingClas <- env.clas.api.student.withManagingClas(s, clas)
activity <- env.activity.read.recent(s.user, 14)
} yield views.html.clas.student.show(clas, students, withManagingClas, activity)
}
}
}

View File

@ -15,12 +15,12 @@ object student {
def show(
clas: Clas,
students: List[Student],
s: Student.WithUser,
s: Student.WithUserAndManagingClas,
activities: Vector[lila.activity.ActivityView]
)(implicit ctx: Context) =
bits.layout(s.user.username, Left(clas withStudents students), s.student.some)(
cls := "student-show",
top(clas, s),
top(clas, s.withUser),
div(cls := "box__pad")(
standardFlash(),
ctx.flash("password").map { password =>
@ -56,7 +56,15 @@ object student {
title := trans.clas.upgradeFromManaged.txt()
)(trans.clas.release())
)
),
) orElse s.managingClas.map { managingClas =>
div(cls := "student-show__managed")(
p(trans.clas.thisStudentAccountIsManaged()),
a(href := routes.Clas.studentShow(managingClas.id.value, s.user.username))(
"Class: ",
managingClas.name
)
)
},
views.html.activity(s.user, activities)
)
)

View File

@ -142,13 +142,11 @@ final class ClasApi(
import framework._
Match($doc("clasId" -> clas.id) ++ selector) -> List(
PipelineOperator(
$doc(
"$lookup" -> $doc(
"from" -> userRepo.coll.name,
"as" -> "user",
"localField" -> "userId",
"foreignField" -> "_id"
)
$lookup.simple(
from = userRepo.coll,
as = "user",
local = "userId",
foreign = "_id"
)
),
UnwindField("user")
@ -199,6 +197,29 @@ final class ClasApi(
def get(clas: Clas, user: User): Fu[Option[Student.WithUser]] =
get(clas, user.id) map2 { Student.WithUser(_, user) }
def withManagingClas(s: Student.WithUser, clas: Clas): Fu[Student.WithUserAndManagingClas] = {
if (s.student.managed) fuccess(clas.some)
else
colls.student
.aggregateOne(ReadPreference.secondaryPreferred) { framework =>
import framework._
Match($doc("userId" -> s.user.id, "managed" -> true)) -> List(
PipelineOperator(
$lookup.simple(
from = colls.clas,
as = "clas",
local = "clasId",
foreign = "_id"
)
),
UnwindField("clas")
)
}
.map {
_.flatMap(_.getAsOpt[Clas]("clas"))
}
} map { Student.WithUserAndManagingClas(s, _) }
def update(from: Student, data: ClasForm.StudentData): Fu[Student] = {
val student = data update from
coll.update.one($id(student.id), student) inject student

View File

@ -46,6 +46,11 @@ object Student {
case class WithUser(student: Student, user: User)
case class WithUserAndManagingClas(withUser: WithUser, managingClas: Option[Clas]) {
def student = withUser.student
def user = withUser.user
}
case class WithPassword(student: Student, password: User.ClearPassword)
case class ManagedInfo(createdBy: User, clas: Clas)