Skip to content
Joshua Monson edited this page Aug 3, 2013 · 2 revisions

Models are located under the app/models package.

List of models

  • AccountLink.scala - An account link is used when multiple accounts are merged. It stores the list of accounts that are merged as well as the primary account. When a user logs in with any of the merged accounts he/she will be logged in as the primary account.
  • Activity.scala - The activity model encapsulates every event in an activity stream which is saved.
  • AddCourseRequest.scala - This contains the request a student makes to join a course.
  • Announcements.scala - Announcements in a course.
  • Content.scala - Content on the site. It saves the ID of the resource, the type of content, settings, and a thumbnail. The ID of the resource can be different formats depending on the type of content.
    • video, audio, image, text - The resource ID is the ID in the resource library.
    • playlist - The resource ID is the ID of the PlayGraph.
    • questions - The resource ID is the ID of the Google Form.
  • ContentListing.scala - When a content object is added to a course, a content listing is created.
  • ContentOwnership.scala - This is used to save which user owns which content. Not really necessary because ownership is one-to-one and could be saved in the content, but this way there is potential to possibly have group ownership.
  • Course.scala - Stores course information.
  • CourseOwnership.scala - Saves who is enrolled in a course and what kind of enrollment they have (teacher or student).
  • Feedback.scala - When a user leaves feedback on the site it is saved in this model.
  • HelpPage.scala - One of these is created for every help page that is added.
  • HomePageContent.scala - On the home page there is a carousel of images with text. Each slide is saved in this model.
  • Notification.scala - When a user receives a notification, this model is used.
  • Scoring.scala - After a user submits a response to a Google Form, it is scored and the resulting grade is saved here.
  • Setting.scala - Each site setting is saved here.
  • TeacherRequest.scala - When a user requests to have their account role changed from student to teacher, this model is used to save that request.
  • User.scala - The user account.
  • WordList.scala - Each entry in a word list is saved with this model.

Caching DB queries

Several models have a caching layer built around certain getter functions in order to minimize the number of DB calls that need to be made. The layer looks something like this:

class Course {
  val cacheTarget = this
  object cache {
    var students: Option[List[User]] = None
    def getStudents = {
      if (students.isEmpty)
        students = Some(CourseMembership.listClassMembers(cacheTarget, teacher = false))
      students.get
    }
  }
  def getStudents = cache.getStudents
}

So if course.getStudents is called more than once, only one query to the DB is made.

MySQL traits

Basic database functionality (insert, update, delete, list) is added into the models via three traits: SQLDeletable, SQLSavable, SQLSelectable. These are found under the app/dataAcess/sqlTraits package. Have your model extend these traits and then add a basic method which calls the inherited method.

SQLDeletable

class MyModel extends SQLDeletable {
  val id: Pk[Long] = Id(123)

  def delete() {
    delete("model_table", id)
  }
}

SQLSavable

case class MyModel(id: Pk[Long], name: String) extends SQLSavable {
  def save: MyModel = {
    if (id.isDefined) {
      update("model_table", 'id -> id, 'name -> name)
      this
    } else {
      val id = insert("model_table", 'name -> name)
      this.copy(id)
    }
  }
}

SQLSelectable

case class MyModel(id: Pk[Long], name: String) { ... }

object MyModel extends SQLSelectable {
  val tableName = "model_table"

  val simple = {
    get[Pk[Long]](tableName + ".id") ~
    get[String](tableName + ".name") map {
      case id ~ name => MyModel(id, name)
    }
  }

  def list = list(tableName, simple)
}
Clone this wiki locally