module WeekDaze.TeacherView.LessonResourceIds(
LessonResourceIds(
getLocationId,
getStudentClass
),
mkLessonResourceIds,
fromStudentView,
toStudentView
) where
import Control.Arrow((&&&))
import qualified Data.Set
import qualified Text.XHtml.Strict
import qualified Text.XML.HXT.Arrow.Pickle as HXT
import qualified ToolShed.SelfValidate
import qualified WeekDaze.Aggregate.StudentClass as Aggregate.StudentClass
import qualified WeekDaze.Model.ResourceUser as Model.ResourceUser
import qualified WeekDaze.StudentView.LessonResourceIds as StudentView.LessonResourceIds
import Text.XHtml.Strict((+++), (<<))
tag :: String
tag = "teacherViewLessonResourceIds"
data LessonResourceIds locationId = MkLessonResourceIds {
getLocationId :: locationId,
getStudentClass :: Aggregate.StudentClass.StudentClass
} deriving (Eq, Ord, Show)
instance Text.XHtml.Strict.HTML locationId => Text.XHtml.Strict.HTML (LessonResourceIds locationId) where
toHtml = (
\(locationId, studentClass) -> Text.XHtml.Strict.li << (locationId +++ '.') +++ Text.XHtml.Strict.li << (studentClass +++ '.')
) . (
getLocationId &&& Aggregate.StudentClass.toHtml . getStudentClass
)
instance HXT.XmlPickler locationId => HXT.XmlPickler (LessonResourceIds locationId) where
xpickle = HXT.xpElem tag . HXT.xpWrap (
uncurry MkLessonResourceIds,
getLocationId &&& getStudentClass
) $ HXT.xpickle `HXT.xpPair` (
HXT.xpElem Aggregate.StudentClass.tag . HXT.xpWrap (
Data.Set.fromList,
Data.Set.toList
) $ HXT.xpList1 HXT.xpickle
)
instance Eq locationId => Model.ResourceUser.ResourceUser (LessonResourceIds locationId) where
areIndependent l r = getLocationId l /= getLocationId r && Model.ResourceUser.areIndependent (getStudentClass l) (getStudentClass r)
instance Show locationId => ToolShed.SelfValidate.SelfValidator (LessonResourceIds locationId) where
getErrors lessonResourceIds = ToolShed.SelfValidate.extractErrors [
(Data.Set.null $ getStudentClass lessonResourceIds, "a class should be composed from one or more student-bodies; " ++ show lessonResourceIds)
]
mkLessonResourceIds
:: Show locationId
=> locationId
-> Aggregate.StudentClass.StudentClass
-> LessonResourceIds locationId
mkLessonResourceIds locationId studentClass
| ToolShed.SelfValidate.isValid lessonResourceIds = lessonResourceIds
| otherwise = error $ "WeekDaze.TeacherView.LessonResourceIds.mkLessonResourceIds:\t" ++ ToolShed.SelfValidate.getFirstError lessonResourceIds ++ "."
where
lessonResourceIds = MkLessonResourceIds locationId studentClass
fromStudentView :: Show locationId => StudentView.LessonResourceIds.LessonResourceIds locationId teacherId -> Aggregate.StudentClass.StudentClass -> LessonResourceIds locationId
fromStudentView resourceIds = mkLessonResourceIds (StudentView.LessonResourceIds.getLocationId resourceIds)
toStudentView :: teacherId -> LessonResourceIds locationId -> StudentView.LessonResourceIds.LessonResourceIds locationId teacherId
toStudentView teacherId resourceIds = StudentView.LessonResourceIds.MkLessonResourceIds {
StudentView.LessonResourceIds.getLocationId = getLocationId resourceIds,
StudentView.LessonResourceIds.getTeacherId = teacherId
}