You are here

opigno.module in Opigno 7.0

Same filename and directory in other branches
  1. 7 opigno.module

Contains all hook_implementations and module specific API.

File

opigno.module
View source
<?php

/**
 * @file
 * Contains all hook_implementations and module specific API.
 */

/**
 * @todo
 *  - Add new permission + callback for views pages
 */

/**
 * Implements hook_menu()
 */
function opigno_menu() {
  $menu = array();
  if (module_exists('opigno_simple_ui')) {

    // Top level "Opigno" menu.

    /*$menu[opigno_simple_ui_get_admin_root()] = array(
        'title' => "Opigno",
        'description' => "Administer the suite of Opigno modules.",
        'page callback' => 'system_admin_menu_block_page',
        'access arguments' => array('access administration pages'),
        'file' => 'system.admin.inc',
        'file path' => drupal_get_path('module', 'system'),
        'weight' => -6
      );*/
  }
  $admin_root_path = opigno_get_admin_root_path('config', 'config');
  $menu[$admin_root_path] = array(
    'title' => module_exists('opigno_simple_ui') ? "Configuration" : "Opigno",
    'description' => "Administer the suite of Opigno modules.",
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array(
      'access administration pages',
    ),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
    'options' => array(
      'attributes' => array(
        'class' => array(
          'icon-settings',
          'opigno-admin-config',
        ),
      ),
    ),
    'type' => MENU_NORMAL_ITEM,
    'weight' => -50,
  );
  return $menu;
}

/**
 * Implements hook_hook_info()
 */
function opigno_hook_info() {
  $hooks = array(
    'opigno_widgets_info' => array(
      'group' => 'opigno',
    ),
    'opigno_widget_view' => array(
      'group' => 'opigno',
    ),
    'opigno_tools' => array(
      'group' => 'opigno',
    ),
  );
  return $hooks;
}

/**
 *
 */
function opigno_get_admin_root_path($drupal_system_path, $opigno_system_path) {
  if (module_exists('opigno_simple_ui')) {
    return opigno_simple_ui_get_admin_root() . "/{$opigno_system_path}";
  }
  else {
    return "admin/{$drupal_system_path}/opigno";
  }
}

/**
 * Implements hook_permission()
 */
function opigno_permission() {
  return array(
    'administer opigno' => array(
      'title' => t("Administer the Opigno LMS"),
    ),
  );
}

/**
 * Implements hook_block_info()
 */
function opigno_block_info() {
  $blocks = array();
  $opigno_widgets = module_invoke_all('opigno_widget_info');
  return $blocks + $opigno_widgets;
}

/**
 * Implements hook_block_view()
 */
function opigno_block_view($delta) {
  $group = og_context();
  return module_invoke_all('opigno_widget_view', $group, $delta);
}

/**
 * Implements hook_opigno_widget_info()
 */
function opigno_opigno_widget_info() {
  $widgets = array(
    'og_tools' => array(
      'info' => t("Opigno: OG tools"),
      'cache' => DRUPAL_CACHE_PER_PAGE,
    ),
    'add_og_content' => array(
      'info' => t("Opigno: Add OG content"),
      'cache' => DRUPAL_CACHE_PER_USER,
    ),
    'og_description' => array(
      'info' => t("Opigno: OG description"),
      'cache' => DRUPAL_CACHE_PER_PAGE,
    ),
    'og_course_progress' => array(
      'info' => t("Opigno: OG Course progress"),
      'cache' => DRUPAL_CACHE_PER_USER,
    ),
  );
  $views_blocks = views_block_info();

  // Calendar widget
  $key = md5('og_ressource_pages-widget_calendar');
  if (isset($views_blocks[$key])) {
    $widgets[$key] = $views_blocks[$key];
  }
  return $widgets;
}

/**
 * Implements hook_opigno_widget_view()
 */
function opigno_opigno_widget_view($group, $delta) {
  module_load_include('inc', 'opigno', 'includes/opigno.block');

  // First check our block
  switch ($delta) {
    case 'og_tools':
      return opigno_og_tools_block($group);
    case 'add_og_content':
      return opigno_og_add_content_block($group);
    case 'og_description':
      return opigno_og_description_block($group);
    case 'og_course_progress':
      return opigno_og_course_progress_block($group);
  }

  // Other widgets
  $views_blocks = views_block_info();
  if (isset($views_blocks[$delta])) {
    return $views_blocks[$delta];
  }
}

/**
 * Implements hook_theme()
 */
function opigno_theme() {
  return array(
    'opigno__og_tool' => array(
      'variables' => array(
        'machine_name' => NULL,
        'name' => NULL,
        'url' => NULL,
      ),
      'template' => 'theme/opigno--og-tool',
    ),
    'opigno__add_content_to_og_list' => array(
      'variables' => array(
        'tools' => array(),
      ),
      'template' => 'theme/opigno--add-content-to-og-list',
    ),
  );
}

/**
 * Implements hook_node_insert()
 */
function opigno_node_insert($node) {
  if (opigno_is_og_type($node->type)) {
    opigno_node_update($node);
  }
}

/**
 * Implements hook_node_update()
 */
function opigno_node_update($node) {
  if (opigno_is_og_type($node->type)) {
    variable_set('opigno_og_tools_nid_' . $node->nid, $node->tools_activate);
  }
}

/**
 * Implements hook_form_FORM_ID_alter()
 */
function opigno_form_course_node_form_alter(&$form, $form_state) {
  _opigno_form_og_node_form_alter($form, $form_state, 'course');
}

/**
 * Helper function for altering OG forms
 *
 * @param array &$form
 *
 * @param array $form_state
 *
 * @param string $type
 */
function _opigno_form_og_node_form_alter(&$form, $form_state, $type) {
  if (!isset($form['#node']->nid)) {
    $node = (object) array(
      'nid' => 0,
    );
  }
  else {
    $node = clone $form['#node'];
  }
  $form['tools'] = array(
    '#type' => 'fieldset',
    '#title' => $type == 'course' ? t("Course tools") : t("Workgroup tools"),
  );
  $tools = array();

  // We need a type on the node. Add one if it's empty (node/add form)
  if (!isset($node->type)) {
    $node->type = $form['type']['#value'];
  }
  foreach (opigno_get_og_tools($node, FALSE) as $tool => $tool_info) {
    $tools[$tool] = $tool_info['tool_name'];
  }
  $form['tools']['tools_activate'] = array(
    '#type' => 'checkboxes',
    '#title' => t("Choose which tools to activate"),
    '#options' => $tools,
    '#default_value' => variable_get('opigno_og_tools_nid_' . $node->nid, array()),
  );
  if (!isset($form['#submit'])) {
    $form['#submit'] = array();
  }
  $form['#submit'][] = '_opigno_form_og_node_form_alter_submit';
}

/**
 * Helper submit callback for altering OG forms
 */
function _opigno_form_og_node_form_alter_submit($form, $form_state) {
  $form['#node']->opigno_og_tools = $form_state['values']['tools_activate'];
}

/**
 * Implements hook_opigno_tools()
 */
function opigno_opigno_tools($node) {
  $tools = array();
  $tools['wikis'] = array(
    'tool_name' => t("Wikis"),
    'tool_url' => url("node/{$node->nid}/wikis"),
  );
  if (user_access('create wiki content')) {
    $tools['wikis']['create_label'] = t("Add a !type", array(
      '!type' => 'wiki',
    ));
    $tools['wikis']['create_url'] = url('node/add/wiki', array(
      'query' => array(
        'gids_node[]' => $node->nid,
      ),
    ));
  }
  $tools['calendar'] = array(
    'tool_name' => t("Calendar"),
    'tool_url' => url("node/{$node->nid}/calendar"),
  );
  if (user_access('create calendar_entry content')) {
    $tools['calendar']['create_label'] = t("Add a !type", array(
      '!type' => 'calendar entry',
    ));
    $tools['calendar']['create_url'] = url('node/add/calendar-entry', array(
      'query' => array(
        'gids_node[]' => $node->nid,
      ),
    ));
  }
  $tools['poll'] = array(
    'tool_name' => t("Polls"),
    'tool_url' => url("node/{$node->nid}/polls"),
  );
  if (user_access('create poll content')) {
    $tools['poll']['create_label'] = t("Add a !type", array(
      '!type' => 'poll',
    ));
    $tools['poll']['create_url'] = url('node/add/poll', array(
      'query' => array(
        'gids_node[]' => $node->nid,
      ),
    ));
  }
  $tools['workgroups'] = array(
    'tool_name' => t("Workgroups"),
    'tool_url' => url("node/{$node->nid}/workgroups"),
  );
  if (user_access('create workgroup content')) {
    $tools['workgroups']['create_label'] = t("Add a !type", array(
      '!type' => 'workgroup',
    ));
    $tools['workgroups']['create_url'] = url('node/add/workgroup', array(
      'query' => array(
        'gids_node[]' => $node->nid,
      ),
    ));
  }

  // Course specific tools
  if (opigno_is_course($node)) {
    $tools['quizzes'] = array(
      'tool_name' => t("Quizzes"),
      'tool_url' => url("node/{$node->nid}/quizzes"),
    );
    if (user_access('create quiz content')) {
      $tools['quizzes']['create_label'] = t("Add a !type", array(
        '!type' => 'quiz',
      ));
      $tools['quizzes']['create_url'] = url('node/add/quiz', array(
        'query' => array(
          'gids_node[]' => $node->nid,
        ),
      ));
    }
  }
  return $tools;
}

/**
 * Custom access callback
 * Use the settings from og_access to determine if a user has access to the current
 * node.
 *
 * @param object $node
 *
 * @param string $type
 *        The page type. Can be 'wiki', 'quiz', 'workgroup', etc
 *
 * @return bool
 */
function opigno_node_access($node, $type) {
  return TRUE;

  // @todo
}

/**
 * Returns the list tools for the OG node.
 *
 * @param object $group
 *
 * @param bool $only_active = TRUE
 *
 * @return array
 */
function opigno_get_og_tools($group, $only_active = TRUE) {
  $node = opigno_get_og_node($group);
  $tools = module_invoke_all('opigno_tools', $node);
  if ($only_active) {
    $all_tools = $tools;
    $tools = array();
    foreach (variable_get('opigno_og_tools_nid_' . $node->nid, array()) as $tool => $active) {
      if ($active) {
        $tools[$tool] = $all_tools[$tool];
      }
    }
  }
  return $tools;
}

/**
 *
 */
function opigno_get_og_node($group) {
  if (isset($group->nid)) {
    $node = $group;
  }
  else {
    $node = node_load($group->etid);
  }
  return $node;
}

/**
 * Determines if the node type is an Opigno specific OG type.
 *
 * @param string $type
 *
 * @return bool
 */
function opigno_is_og_type($type) {
  return in_array($type, array(
    'course',
    'workgroup',
  ));
}

/**
 *
 */
function opigno_is_course($node) {
  return in_array($node->type, array(
    'course',
  ));
}

/**
 *
 */
function opigno_is_workgroup($node) {
  return in_array($node->type, array(
    'workgroup',
  ));
}

Functions

Namesort descending Description
opigno_block_info Implements hook_block_info()
opigno_block_view Implements hook_block_view()
opigno_form_course_node_form_alter Implements hook_form_FORM_ID_alter()
opigno_get_admin_root_path
opigno_get_og_node
opigno_get_og_tools Returns the list tools for the OG node.
opigno_hook_info Implements hook_hook_info()
opigno_is_course
opigno_is_og_type Determines if the node type is an Opigno specific OG type.
opigno_is_workgroup
opigno_menu Implements hook_menu()
opigno_node_access Custom access callback Use the settings from og_access to determine if a user has access to the current node.
opigno_node_insert Implements hook_node_insert()
opigno_node_update Implements hook_node_update()
opigno_opigno_tools Implements hook_opigno_tools()
opigno_opigno_widget_info Implements hook_opigno_widget_info()
opigno_opigno_widget_view Implements hook_opigno_widget_view()
opigno_permission Implements hook_permission()
opigno_theme Implements hook_theme()
_opigno_form_og_node_form_alter Helper function for altering OG forms
_opigno_form_og_node_form_alter_submit Helper submit callback for altering OG forms