You are here

abstract class CourseObjectNode in Course 6

Same name and namespace in other branches
  1. 7.2 includes/CourseObjectNode.inc \CourseObjectNode
  2. 7 includes/CourseObjectNode.inc \CourseObjectNode

A course object that uses a node as a base.

Hierarchy

Expanded class hierarchy of CourseObjectNode

1 string reference to 'CourseObjectNode'
course_cron in ./course.module
Implements hook_cron().

File

includes/course_object.core.inc, line 1066

View source
abstract class CourseObjectNode extends CourseObject {
  protected $node;
  public function __construct($object, $user = NULL, $course = NULL) {

    // Pass configuration to parent.
    parent::__construct($object, $user, $course);
    $this->node = node_load($this
      ->getInstanceId());
  }
  public function hasNodePrivacySupport() {
    return module_exists('content_access') && module_exists('acl');
  }

  /**
   * Return a list of valid node types.
   *
   * @return array
   *   An array keyed by machine name of a node type.
   *   return array(
   *     'my_event' => 'My event content type',
   *   );
   */
  public abstract function getNodeTypes();

  /**
   * Simple node course object behavior is to just redirect to the node.
   */
  public function getTakeType() {
    return 'redirect';
  }
  public function getTakeUrl() {
    if ($this
      ->getInstanceId()) {
      return url("node/{$this->node->nid}");
    }
  }
  public function getEditUrl() {
    if ($this
      ->getInstanceId()) {
      return url("node/{$this->node->nid}/edit");
    }
  }
  public function create() {
    $node = new stdClass();
    $node->type = $this
      ->getOption('node_type');
    $node->title = $this
      ->getTitle();
    $node->uid = $this->user->uid;
    node_save($node);
    $this
      ->setNode($node);
  }

  /**
   * Set the node and instance ID (node ID) of this CourseObjectNode.
   *
   * @param mixed $node
   *   A node or node ID.
   */
  public function setNode($node) {
    $this->node = $node;
    $this
      ->setInstanceId($this->node->nid);
  }

  /**
   * Destroy the node instance.
   */
  public function delete() {
    node_delete($this
      ->getInstanceId());
  }
  public function optionsDefinition() {
    $defaults = parent::optionsDefinition();
    $defaults['private'] = 0;
    $options = array_intersect_key(node_get_types('names'), $this
      ->getNodeTypes());
    $defaults['node_type'] = key($options);
    return $defaults;
  }
  public function optionsForm(&$form, &$form_state) {
    parent::optionsForm($form, $form_state);
    $config = $this
      ->getOptions();
    if (!$this
      ->getInstanceId()) {
      $types = drupal_map_assoc($this
        ->getNodeTypes());
      $options = array_intersect_key(node_get_types('names'), $types);
      if (count($options) > 1) {
        array_unshift($options, '- select -');
      }
      $form['node_type'] = array(
        '#title' => 'Create node',
        '#type' => 'select',
        '#options' => $options,
        '#description' => 'Selecting a node type will automatically create this node and link it to this course object.',
        '#default_value' => $config['node_type'],
      );
      if (count($options) > 1) {
        $form['node_type']['#required'] = TRUE;
      }
    }
    $form['node'] = array(
      '#type' => 'fieldset',
      '#title' => 'Node',
      '#description' => 'Settings for node-based objects.',
    );
    $form['node']['instance'] = array(
      '#title' => 'Existing node',
      '#autocomplete_path' => 'course/autocomplete/node/' . implode(',', $this
        ->getNodeTypes()),
      '#type' => 'textfield',
      '#description' => t('Use an existing node instead of creating a new one.'),
    );
    $form['node']['private'] = array(
      '#title' => t('Private'),
      '#description' => $this
        ->hasNodePrivacySupport() ? t('This content will not be available to users who are not enrolled in this course.') : t('You must enable content_access and acl in order to restrict course content to users who are enrolled in this course.'),
      '#type' => 'checkbox',
      '#default_value' => $config['private'],
      '#disabled' => !$this
        ->hasNodePrivacySupport(),
    );
    $nid = $this
      ->getInstanceId();
    if ($nid) {
      $node = node_load($nid);
      $link = l(t("'%title' [node id %nid]", array(
        '%title' => $node->title,
        '%nid' => $node->nid,
      )), "node/{$node->nid}", array(
        'attributes' => array(
          'target' => '_blank',
          'title' => t('Open in new window'),
        ),
        'html' => TRUE,
      ));
      $form['node']['instance']['#description'] = t('Currently set to !link', array(
        '!link' => $link,
      ));
    }
  }

  /**
   * Validate the options form. Check the node type.
   */
  public function optionsValidate(&$form, &$form_state) {
    if (isset($form_state['values']['node_type']) && empty($form_state['values']['node_type'])) {
      form_set_error('node_type', 'Please select a node type.');
    }
  }
  public function optionsSubmit(&$form, &$form_state) {
    $nid = $form_state['values']['instance'];
    if (!is_numeric($nid)) {
      preg_match('/^(?:\\s*|(.*) )?\\[\\s*nid\\s*:\\s*(\\d+)\\s*\\]$/', $nid, $matches);
      $nid = $matches[2];
    }
    if ($nid) {
      $form_state['values']['instance'] = $nid;
    }
    else {

      // Unset it, or we'll erase the relationship (since the textfield is
      // actually blank).
      unset($form_state['values']['instance']);
    }
    parent::optionsSubmit($form, $form_state);
  }
  public function getWarnings() {
    $warnings = parent::getWarnings();
    if ($this
      ->getOption('private')) {
      $settings = variable_get('content_access_settings', array());
      if (!$settings['per_node'][$this
        ->getComponent()]) {
        $warnings[] = t('%t is set to Private, but the content type %c does not have access control lists enabled. Users will not be able to acces this content. Please visit !l to set up content access settings.', array(
          '%t' => $this
            ->getTitle(),
          '%c' => $this
            ->getComponent(),
          '!l' => l('Access control', "admin/content/node-type/{$this->getComponent()}/access"),
        ));
      }
    }
    return $warnings;
  }

  /**
   * Grant access to course content before going to it.
   */
  function grant() {
    if ($this
      ->hasNodePrivacySupport()) {
      if ($this
        ->getOption('private')) {
        $uid = $this->user->uid;
        module_load_include('inc', 'content_access', 'content_access.admin');
        $acl_id = content_access_get_acl_id($this->node, 'view');
        acl_add_user($acl_id, $uid);
        acl_node_add_acl($this->node->nid, $acl_id, 1, 0, 0, content_access_get_settings('priority', $this->node->type));
        node_save($this->node);
      }
    }
  }

  /**
   * Duration expired (or something) - CourseObject is telling us so.
   */
  function revoke() {
    if ($this
      ->hasNodePrivacySupport()) {
      if ($this
        ->getOption('private')) {
        $uid = $this->user->uid;
        module_load_include('inc', 'content_access', 'content_access.admin');
        $acl_id = content_access_get_acl_id($this->node, 'view');
        acl_remove_user($acl_id, $uid);
        node_save($this->node);
      }
    }
  }

  /**
   * On object write, set privacy on this node.
   */
  function save() {
    parent::save();
    if ($this
      ->hasNodePrivacySupport() && $this
      ->getOption('private')) {

      // Ensure that per-node access is enabled.
      $global_settings = content_access_get_settings();
      $global_settings['per_node'][$this->node->type] = 1;
      content_access_set_settings($global_settings);

      // Remove "view" permissions to everyone on this node.
      $settings = content_access_get_per_node_settings($this->node);
      $settings['view'] = array();
      content_access_save_per_node_settings($this->node, $settings);

      // Resave node to update access.
      node_save($this->node);
    }
  }

  /**
   * Freeze data to persist over cloning/exporting.
   * @return array
   *   An array of data to be frozen.
   */
  function freeze() {
    if ($this->node->nid != $this
      ->getCourse()
      ->getNode()->nid) {

      // Don't freeze the course, if this course is part of the objects.
      $ice = new stdClass();
      $ice->node = $this->node;
      return $ice;
    }
  }

  /**
   * Thaw data frozen from an earlier export/clone.
   *
   * @param array $data
   *   Unfrozen data.
   *
   * @return int
   *   The new instance ID.
   */
  function thaw($ice) {
    $this->node = $ice->node;
    unset($this->node->nid);
    node_save($this->node);
    return $this->node->nid;
  }
  function getCloneAbility() {
    return t('%object will be cloned as a node. Results may vary.', array(
      '%object' => $this
        ->getTitle(),
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CourseHandler::$accessMessages private property
CourseHandler::$config protected property
CourseHandler::$handlerType public property
CourseHandler::$primaryKey public property
CourseHandler::$serializedField public property
CourseHandler::$table public property
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 final public function Get an option stored in this CourseObject.
CourseHandler::optionsMerge private function Merge arrays with replace, not append.
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 final public function Set an option for this handler.
CourseHandler::setOptions final public function Set this entire handler's options.
CourseObject::$course private property
CourseObject::$courseObjectFulfillment private property
CourseObject::$user protected property
CourseObject::access public function Access functionality for course objects.
CourseObject::getComponent function Get the object component for this course object.
CourseObject::getCourse function Get the Course that contains this CourseObject.
CourseObject::getCourseNid function Get the course node ID this CourseObject belongs to.
CourseObject::getFulfillment public function Get this course object's fulfillment object.
CourseObject::getInstanceId function Get the instance ID. This could be the external component ID, a Node ID...
CourseObject::getMaxOccurences public static function Return the number of occurances that can be in a course at the same time. For example, the design of the Certificate module can only have 1 set of mappings per node. The same goes for Course Credit. We may also want a course object that can only be… 2
CourseObject::getModule function Get the module that provides this course object.
CourseObject::getOptions public function Get options, with session options having precedence. Overrides CourseHandler::getOptions
CourseObject::getOptionsSummary public function Get core options summary. Overrides CourseHandler::getOptionsSummary 2
CourseObject::getReport function Let the course object provide its own reports. 4
CourseObject::getReports function Let the course object provide its own reports. 4
CourseObject::getStatus public function Get the user's status in this course object. 1
CourseObject::getTitle function
CourseObject::getUrl public function Return the URL to the course object router.
CourseObject::hasPolling public function Specify whether fulfillment uses asynchronous polling. 2
CourseObject::isActive public function
CourseObject::isGraded function Is this object graded? 2
CourseObject::isRequired public function Is this course object required for course completion?
CourseObject::isTemporary function Checks the temporary status of a course object.
CourseObject::optionFilter private function
CourseObject::overrideNavigation public function Override navigation links. 1
CourseObject::overrideOutlineListItem public function Overrides a course outline list item. 1
CourseObject::poll function Give the course object a chance do asynchronous polling and set completion on demand.
CourseObject::renderOptionsSummary public function Get all course object implementations of getOptionsSummary().
CourseObject::setComponent function Set the object component for this course object.
CourseObject::setCourse public function Set the Course for this CourseObject.
CourseObject::setId function Set the internal course object ID.
CourseObject::setInstanceId function Set this object's instance ID.
CourseObject::setModule function Set the module that provides this course object.
CourseObject::setUser function Set the user fulfilling/creating this course object.
CourseObject::take public function 5
CourseObject::takeCourseObject final public function Take a course object.
CourseObject::unEnroll function Remove any records associated with this course object for the user. 1
CourseObjectNode::$node protected property
CourseObjectNode::create public function Creates a course object. Overrides CourseObject::create 7
CourseObjectNode::delete public function Destroy the node instance. Overrides CourseObject::delete
CourseObjectNode::freeze function Freeze data to persist over cloning/exporting. Overrides CourseObject::freeze
CourseObjectNode::getCloneAbility function Returns an translated error message if this object has issues with cloning. Overrides CourseObject::getCloneAbility 6
CourseObjectNode::getEditUrl public function Get the URL to edit this course object, if any. Overrides CourseObject::getEditUrl
CourseObjectNode::getNodeTypes abstract public function Return a list of valid node types. 8
CourseObjectNode::getTakeType public function Simple node course object behavior is to just redirect to the node. Overrides CourseObject::getTakeType 3
CourseObjectNode::getTakeUrl public function Get the URL to take this course object, if any. Overrides CourseObject::getTakeUrl 1
CourseObjectNode::getWarnings public function Return a list of warning strings about this handler. Overrides CourseHandler::getWarnings 2
CourseObjectNode::grant function Grant access to course content before going to it. Overrides CourseObject::grant
CourseObjectNode::hasNodePrivacySupport public function
CourseObjectNode::optionsDefinition public function Define configuration elements and their defaults. Overrides CourseObject::optionsDefinition 3
CourseObjectNode::optionsForm public function Default options form for all course objects. Overrides CourseObject::optionsForm 5
CourseObjectNode::optionsSubmit public function Save object configs to cache. Overrides CourseObject::optionsSubmit 2
CourseObjectNode::optionsValidate public function Validate the options form. Check the node type. Overrides CourseObject::optionsValidate
CourseObjectNode::revoke function Duration expired (or something) - CourseObject is telling us so. Overrides CourseObject::revoke
CourseObjectNode::save function On object write, set privacy on this node. Overrides CourseObject::save
CourseObjectNode::setNode public function Set the node and instance ID (node ID) of this CourseObjectNode.
CourseObjectNode::thaw function Thaw data frozen from an earlier export/clone. Overrides CourseObject::thaw 3
CourseObjectNode::__construct public function Construct a course object from a database record. Overrides CourseObject::__construct