You are here

class CourseHandler in Course 7

Same name and namespace in other branches
  1. 6 includes/course.core.inc \CourseHandler
  2. 7.2 includes/CourseHandler.inc \CourseHandler

Master class for anything Course related.

Anything implementing CourseHandler is expected to have a table and a serialized field for storing options defined by other modules.

Hierarchy

Expanded class hierarchy of CourseHandler

File

includes/CourseHandler.inc, line 9

View source
class CourseHandler extends Entity {
  function __construct($values, $entityType) {
    parent::__construct($values, $entityType);
    foreach ($this
      ->optionsDefinition() as $key => $value) {
      if (!isset($this->{$key})) {
        $this->{$key} = $value;
      }
    }
  }

  // Handlers must have an ID.
  function getId() {
    return $this
      ->identifier();
  }

  /**
   * Get the summary of an object's options.
   *
   * @return array
   *   An associative array of summary keys and values.
   */
  public function getOptionsSummary() {
    $summary = array();
    foreach ($this
      ->getWarnings() as $warning) {
      $warning = '<span class="error">' . $warning . '</span>';
      $summary['warnings'] = filter_xss_admin($warning);
    }
    return $summary;
  }

  /**
   * Get an object's configuration.
   *
   * This can be overridden. For example, values stored in courseobject sessions
   * need to have priority over those in the database.
   *
   * @return array
   */
  public function getOptions() {
    return get_object_vars($this);
  }

  /**
   * Get an handler option's value.
   *
   * @return mixed
   */
  public function getOption($key) {
    $options = $this
      ->getOptions();
    if (isset($options[$key])) {
      return $options[$key];
    }
    else {
      return NULL;
    }
  }

  /**
   * Set an option for this handler.
   *
   * @param string $option
   *   An option key.
   * @param mixed $value
   *   The option value.
   *
   * @return CourseHandler
   */
  public function setOption($option, $value) {
    if (!preg_match('/\\*/', $option) && is_scalar($option)) {
      $this->{$option} = $value;
    }
    return $this;
  }

  /**
   * Set this entire handler's options.
   *
   * Deserialize the serialized column if necessary.
   *
   * @param array $options
   *   An array of options.
   *
   * @return CourseHandler
   */
  public final function setOptions($options) {
    foreach ($options as $key => $option) {
      $this
        ->setOption($key, $option);
    }
  }

  /**
   * Merge an array of options onto the existing options.
   *
   * @param array $options
   *
   * @return CourseHandler
   *   Some type of CourseHandler (probably CourseObject or
   *   CourseObjectFulfillment)
   */
  public final function addOptions(array $options) {
    $this
      ->setOptions($this
      ->optionsMerge($this
      ->getOptions(), $options));
    return $this;
  }

  /**
   * Merge arrays with replace, not append.
   *
   * @see http://www.php.net/manual/en/function.array-merge-recursive.php#102379
   */
  private function optionsMerge($Arr1, $Arr2) {
    foreach ($Arr2 as $key => $Value) {
      if (array_key_exists($key, $Arr1) && is_array($Value)) {
        $Arr1[$key] = $this
          ->optionsMerge($Arr1[$key], $Arr2[$key]);
      }
      else {
        $Arr1[$key] = $Value;
      }
    }
    return $Arr1;
  }

  /**
   * Handlers can declare their defaults if they have a configuration form.
   */
  protected function optionsDefinition() {
    return array();
  }

  /**
   * Handlers can declare a form.
   */
  public function optionsForm(&$form, &$form_state) {
  }

  /**
   * Validate?
   */
  public function optionsValidate(&$form, &$form_state) {
  }

  /**
   * Save data somewhere.
   *
   * This can be overridden. For example, values stored in CourseObject sessions
   * need to have priority over those in the database.
   */
  public function optionsSubmit(&$form, &$form_state) {
  }

  /**
   * Return an array of database fields. This determines what fields should be
   * serialized instead of stored.
   */
  protected function getDatabaseFields() {
    $schema = drupal_get_schema($this->entityInfo['base table']);
    $fields = field_info_instances($this
      ->entityType());
    $fields = $fields[$this
      ->entityType()];
    return array_keys($fields + $schema['fields']);
  }

  /**
   * Return a list of warning strings about this handler.
   *
   * For example, if a user adds a quiz to a course with no questions, trigger a
   * message.
   *
   * @see CourseObjectQuiz
   * @see CourseObjectWebform
   */
  public function getWarnings() {
    return array();
  }

  /**
   * Set an access message to be displayed along with the course object when it
   * is in the outline. For example, "This activity will open on XYZ" or "Please
   * complete Step 1 to take this activity."
   *
   * @param string $key
   *   Message key.
   * @param string $message
   *   Message text.
   */
  public function setAccessMessage($key = NULL, $message = NULL) {
    if ($key == NULL) {
      return $this->accessMessages;
    }
    if (empty($message)) {
      unset($this->accessMessages[$key]);
    }
    else {
      $this->accessMessages[$key] = $message;
    }
  }

  /**
   * Get an array of access messages.
   *
   * @return array
   */
  public function getAccessMessages() {
    return $this
      ->setAccessMessage();
  }
  public function save() {
    $info = $this
      ->entityInfo();
    $schema = drupal_get_schema($info['base table']);
    foreach ($schema['fields'] as $field_name => $info) {
      if (!empty($info['serialize'])) {
        $serialized_field = $field_name;
      }
      $real_fields[$field_name] = $field_name;
    }
    $options = $this
      ->getOptions();
    $strict_options = array_intersect_key($options, $this
      ->optionsDefinition());
    $this->{$serialized_field} = array_diff_key($strict_options, $real_fields);
    parent::save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CourseHandler::addOptions final public function Merge an array of options onto the existing options.
CourseHandler::getAccessMessages public function Get an array of access messages.
CourseHandler::getDatabaseFields protected function Return an array of database fields. This determines what fields should be serialized instead of stored.
CourseHandler::getId function
CourseHandler::getOption public function Get an handler option's value.
CourseHandler::getOptions public function Get an object's configuration. 1
CourseHandler::getOptionsSummary public function Get the summary of an object's options. 1
CourseHandler::getWarnings public function Return a list of warning strings about this handler. 1
CourseHandler::optionsDefinition protected function Handlers can declare their defaults if they have a configuration form. 2
CourseHandler::optionsForm public function Handlers can declare a form. 1
CourseHandler::optionsMerge private function Merge arrays with replace, not append.
CourseHandler::optionsSubmit public function Save data somewhere. 1
CourseHandler::optionsValidate public function Validate? 1
CourseHandler::save public function Permanently saves the entity. Overrides Entity::save 2
CourseHandler::setAccessMessage public function Set an access message to be displayed along with the course object when it is in the outline. For example, "This activity will open on XYZ" or "Please complete Step 1 to take this activity."
CourseHandler::setOption public function Set an option for this handler.
CourseHandler::setOptions final public function Set this entire handler's options.
CourseHandler::__construct function Overrides Entity::__construct 1
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::$wrapper protected property
Entity::buildContent public function Builds a structured array representing the entity's content. Overrides EntityInterface::buildContent 1
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::defaultLabel protected function Defines the entity label if the 'entity_class_label' callback is used. 1
Entity::defaultUri protected function Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info().
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.