<commit_before>package models.organization

import play.api.db.slick.Config.driver.simple._
import play.api.db.slick.DB
import play.api.Play.current
import mathml._
import mathml.scalar._
import models.question.derivative.table._
import models.organization.table._
import service._
import models.id._
import org.joda.time.DateTime
import models.organization.view._
import service.table._
import service.Access._
import models.question.derivative.Quiz

case class SectionTmp(name: String, courseId: CourseId, owner: UserId, editCode: String, viewCode: String, date: DateTime) {
	def apply(id: SectionId) = { Section(id, name, courseId, owner, editCode, viewCode, date, date) }
}

case class Section(id: SectionId, name: String, courseId: CourseId, owner: UserId, editCode: String, viewCode: String, creationDate: DateTime, updateDate: DateTime) extends Secured {

	def course(implicit session: Session) = Courses.find(courseId).get

	def details(implicit user: User, session: Session) = SectionDetails(this, access)

	def students(implicit session: Session) = Sections.students(id)

	def results(quiz: Quiz)(implicit session: Session) = students.map(s => quiz.results(s))

	protected def linkAccess(implicit user: User, session: Session): Access = Sections.otherAccess(user, id)

	/**
	 * In terms of access level Users can:
	 *     1) Own the Section which grants Own access
	 *     2) Have association access granted at either the Edit or View
	 *     2) Have access to the Section's Course which provides a maximum of Edit access to the section
	 * This means users who are granted access to Sections should also be granted access to the corresponding course.
	 */
	def access(implicit user: User, session: Session) = {
		val courseAccess = course.access.maxEdit
		Seq(courseAccess, directAccess).max
	}

	def grantAccess(access: Access)(implicit user: User, session: Session) = Sections.grantAccess(this, access)

}

object Sections {

	// ======= CREATE ======
	def create(sectionTmp: SectionTmp)(implicit session: Session) = sectionTmp((new SectionsTable).insert(sectionTmp))

	// ======= FIND ======
	def find(sectionId: SectionId)(implicit session: Session) = Query(new SectionsTable).where(_.id === sectionId).firstOption

	def findByCourse(courseId: CourseId)(implicit session: Session) = Query(new SectionsTable).where(_.courseId === courseId).list

	def findDetails(sectionId: SectionId)(implicit user: User, session: Session) = Query(new SectionsTable).where(_.id === sectionId).firstOption.map(_.details)

	def students(sectionId: SectionId)(implicit session: Session) =
		(for (
			u <- (new UserTable);
			us <- (new UsersSectionsTable) if us.userId === u.id && us.id === sectionId && us.access === service.Access.view
		) yield u).list

	// ======= AUTHORIZATION ======<commit_msg>student show up sorted by email in section results<commit_after>package models.organization

import play.api.db.slick.Config.driver.simple._
import play.api.db.slick.DB
import play.api.Play.current
import mathml._
import mathml.scalar._
import models.question.derivative.table._
import models.organization.table._
import service._
import models.id._
import org.joda.time.DateTime
import models.organization.view._
import service.table._
import service.Access._
import models.question.derivative.Quiz

case class SectionTmp(name: String, courseId: CourseId, owner: UserId, editCode: String, viewCode: String, date: DateTime) {
	def apply(id: SectionId) = { Section(id, name, courseId, owner, editCode, viewCode, date, date) }
}

case class Section(id: SectionId, name: String, courseId: CourseId, owner: UserId, editCode: String, viewCode: String, creationDate: DateTime, updateDate: DateTime) extends Secured {

	def course(implicit session: Session) = Courses.find(courseId).get

	def details(implicit user: User, session: Session) = SectionDetails(this, access)

	def students(implicit session: Session) = Sections.students(id)

	def results(quiz: Quiz)(implicit session: Session) = students.map(s => quiz.results(s))

	protected def linkAccess(implicit user: User, session: Session): Access = Sections.otherAccess(user, id)

	/**
	 * In terms of access level Users can:
	 *     1) Own the Section which grants Own access
	 *     2) Have association access granted at either the Edit or View
	 *     2) Have access to the Section's Course which provides a maximum of Edit access to the section
	 * This means users who are granted access to Sections should also be granted access to the corresponding course.
	 */
	def access(implicit user: User, session: Session) = {
		val courseAccess = course.access.maxEdit
		Seq(courseAccess, directAccess).max
	}

	def grantAccess(access: Access)(implicit user: User, session: Session) = Sections.grantAccess(this, access)

}

object Sections {

	// ======= CREATE ======
	def create(sectionTmp: SectionTmp)(implicit session: Session) = sectionTmp((new SectionsTable).insert(sectionTmp))

	// ======= FIND ======
	def find(sectionId: SectionId)(implicit session: Session) = Query(new SectionsTable).where(_.id === sectionId).firstOption

	def findByCourse(courseId: CourseId)(implicit session: Session) = Query(new SectionsTable).where(_.courseId === courseId).list

	def findDetails(sectionId: SectionId)(implicit user: User, session: Session) = Query(new SectionsTable).where(_.id === sectionId).firstOption.map(_.details)

	def students(sectionId: SectionId)(implicit session: Session) =
		(for (
			u <- (new UserTable);
			us <- (new UsersSectionsTable) if us.userId === u.id && us.id === sectionId && us.access === service.Access.view
		) yield u).sortBy(_.email).list

	// ======= AUTHORIZATION ======
{'fraction_non_alphanumeric': 0.12254294315565788, 'fraction_numerical': 0.0010625110678236232, 'mean_word_length': 5.119176598049838, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3383926', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1608, 'n_words': 508}