View source
<?php
abstract class CourseObjectNode extends CourseObject {
public function __construct($values, $entityType) {
parent::__construct($values, $entityType);
}
public static function context() {
if (arg(0) == 'node' && ($node = node_load(arg(1)))) {
$instances = static::getNodeInstances($node);
if (!empty($instances)) {
$node = node_load($instances[0]);
}
if (isset($node->nid)) {
return array(
'object_type' => $node->type,
'instance' => $node->nid,
);
}
}
}
public static function getNodeInstances($node) {
return array();
}
public function hasNodePrivacySupport() {
return module_exists('content_access') && module_exists('acl');
}
public abstract function getNodeTypes();
public function getTakeType() {
return 'redirect';
}
public function getTakeUrl() {
if ($this
->getNode()) {
return url("node/{$this->getNode()->nid}");
}
}
public function getEditUrl() {
if ($this
->getNode()) {
return url("node/{$this->getNode()->nid}/edit");
}
}
public function getViewUrl() {
if ($this
->getNode()) {
return url("node/{$this->getNode()->nid}");
}
}
public function create($node = NULL) {
global $user;
if (!$node) {
$node = new stdClass();
}
if (!isset($node->type)) {
$node->type = $this
->getOption('node_type');
}
$node->title = $this
->getTitle();
$node->uid = $user->uid;
if (drupal_multilingual()) {
$language = language_default();
$node->language = $language->language;
}
else {
$node->language = LANGUAGE_NONE;
}
node_object_prepare($node);
node_save($node);
$this
->setInstanceId($node->nid);
}
public function delete() {
node_delete($this
->getInstanceId());
}
public function optionsDefinition() {
$defaults = parent::optionsDefinition();
$defaults['private'] = !$this
->getInstanceId() && $this
->hasNodePrivacySupport();
$options = array_intersect_key(node_type_get_names(), drupal_map_assoc($this
->getNodeTypes()));
$defaults['node_type'] = key($options);
$defaults['use_node_title'] = 0;
$defaults['clone_and_reference'] = 0;
$defaults['use_existing_node'] = 0;
return $defaults;
}
public function optionsForm(&$form, &$form_state) {
parent::optionsForm($form, $form_state);
$form['node'] = array(
'#type' => 'fieldset',
'#title' => t('Content'),
'#description' => 'Settings for course object content.',
'#group' => 'course_tabs',
'#weight' => 2,
);
$config = $this
->getOptions();
$types = drupal_map_assoc($this
->getNodeTypes());
$options = array_intersect_key(node_type_get_names(), $types);
$form['node']['use_existing_node'] = array(
'#type' => 'checkbox',
'#title' => t('Use existing content'),
'#default_value' => $this
->getOption('use_existing_node'),
'#weight' => 1,
'#access' => $this
->isTemporary(),
);
$form['node']['node_type'] = array(
'#title' => t('Create node'),
'#type' => 'select',
'#options' => $options,
'#description' => t('Selecting a node type will automatically create this node and link it to this course object.'),
'#default_value' => $config['node_type'],
'#states' => array(
'visible' => array(
':input[name="use_existing_node"]' => array(
'checked' => FALSE,
),
),
),
'#weight' => 2,
'#access' => $this
->isTemporary(),
);
if (count($options) > 1) {
$form['node']['node_type']['#required'] = TRUE;
}
$form['node']['instance'] = array(
'#title' => t('Existing content'),
'#autocomplete_path' => 'course/autocomplete/node/' . implode(',', $this
->getNodeTypes()),
'#type' => 'textfield',
'#description' => t('Use existing content instead of creating a new one.'),
'#default_value' => !empty($this
->getInstanceId()) ? check_plain($this
->getNode()->title) . " [nid: {$this->getInstanceId()}]" : NULL,
'#maxlength' => 255,
'#states' => array(
'visible' => array(
':input[name="use_existing_node"]' => array(
'checked' => TRUE,
),
),
),
'#weight' => 3,
);
if (module_exists('clone') && !$this
->getInstanceId()) {
$form['node']['clone_and_reference'] = array(
'#title' => t('Clone and reference'),
'#type' => 'checkbox',
'#description' => t('This will clone the selected content first.'),
'#default_value' => $config['clone_and_reference'],
'#weight' => 4,
'#states' => array(
'visible' => array(
':input[name="use_existing_node"]' => array(
'checked' => TRUE,
),
),
),
);
}
$form['node']['use_node_title'] = array(
'#type' => 'checkbox',
'#title' => t('Use existing title'),
'#description' => t("Use the referenced content's title as this course object's title."),
'#default_value' => $config['use_node_title'],
'#weight' => 5,
);
$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(),
'#weight' => 6,
);
$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,
));
}
if (!empty($config['instance']) && $config['instance'] == $this
->getCourseNid()) {
$form['delete']['delete_instance']['#disabled'] = TRUE;
$form['delete']['delete_instance']['#description'] = t('You cannot delete this instance, as it references this Course.');
}
}
public function optionsValidate(&$form, &$form_state) {
parent::optionsValidate($form, $form_state);
$nid = $form_state['values']['instance'];
if (empty($nid) && isset($form_state['values']['node_type']) && empty($form_state['values']['node_type'])) {
form_set_error('node_type', t('Please select a node type.'));
}
$missing_node = !preg_match('/^(?:\\s*|(.*) )?\\[\\s*nid\\s*:\\s*(\\d+)\\s*\\]$/', $nid);
if (($form_state['values']['use_existing_node'] || !$this
->isTemporary()) && $missing_node) {
form_set_error('instance', t('Please select a node.'));
}
}
public function optionsSubmit(&$form, &$form_state) {
if (isset($form_state['values']['instance'])) {
$nid = $form_state['values']['instance'];
if (!is_numeric($nid)) {
if (preg_match('/^(?:\\s*|(.*) )?\\[\\s*nid\\s*:\\s*(\\d+)\\s*\\]$/', $nid, $matches)) {
$nid = $matches[2];
}
}
if ($nid) {
$form_state['values']['instance'] = $nid;
}
else {
unset($form_state['values']['instance']);
}
}
parent::optionsSubmit($form, $form_state);
}
function save() {
if ($this
->getOption('clone_and_reference')) {
module_load_include('inc', 'clone', 'clone.pages');
$new_nid = clone_node_save($this
->getOption('instance'));
$this
->setInstanceId($new_nid);
$this
->setOption('clone_and_reference', 0);
}
parent::save();
$privacy_enabled = $this
->hasNodePrivacySupport() && $this
->getOption('private');
$external_node = $this
->getInstanceId() > 0 && $this
->getInstanceId() != $this
->getCourseNid();
if ($privacy_enabled && $external_node) {
$type_settings = content_access_get_settings('all', $this
->getNode()->type);
$type_settings['per_node'] = 1;
content_access_set_settings($type_settings, $this
->getNode()->type);
$settings = content_access_get_per_node_settings($this
->getNode());
$grant_roles = array_filter(variable_get('course_object_private_roles', array()));
$settings['view'] = $grant_roles;
content_access_save_per_node_settings($this
->getNode(), $settings);
node_access_acquire_grants($this
->getNode());
}
}
function freeze() {
if ($this
->getInstanceId() != $this
->getCourse()
->getNode()->nid) {
$ice = new stdClass();
$ice->node = $this
->getNode();
return $ice;
}
}
function thaw($ice) {
$node = $ice->node;
unset($node->nid);
unset($node->vid);
$context = array(
'method' => 'save-edit',
);
drupal_alter('clone_node', $node, $context);
node_save($node);
$this
->setInstanceId($node->nid);
return $this
->getInstanceId();
}
function getCloneAbility() {
return t('%object will be cloned as a node. Results may vary.', array(
'%object' => $this
->getTitle(),
));
}
function getTitle() {
if ($this
->getOption('use_node_title') && $this
->getNode()) {
return $this
->getNode()->title;
}
else {
return parent::getTitle();
}
}
function getNode() {
return node_load($this->instance);
}
function getOptionsSummary() {
$summary = parent::getOptionsSummary();
if ($this
->getOption('instance') == $this
->getCourseNid()) {
unset($summary['instance']);
}
return $summary;
}
function getWarnings() {
$warnings = parent::getWarnings();
if ($this
->getInstanceId() && !$this
->getNode()) {
$warnings[] = t('The content associated with this object has been deleted.<br/>Saving the course will create new content from the object settings.');
}
return $warnings;
}
function access($op = 'view', $account = NULL) {
if ($op == 'take' && !$this
->getNode()) {
return FALSE;
}
return parent::access($op, $account);
}
}