You are here

function _opigno_statistics_app_get_courses_classes_block in Opigno Statistics App 7

File

./opigno_statistics_app.module, line 593
Module file. Defines module hooks.

Code

function _opigno_statistics_app_get_courses_classes_block() {
  $blocks = array();
  if (user_access('view own courses classes statistics')) {
    global $user;

    // Get all the courses and classes where the user can access statistics
    // First, need to get all the nodes where the user is member.
    $member_nodes = db_select('og_membership', 'om')
      ->fields('om', array(
      'gid',
    ))
      ->condition('entity_type', 'user')
      ->condition('group_type', 'node')
      ->condition('etid', $user->uid)
      ->execute()
      ->fetchAllAssoc('gid');
    $nids = array_keys($member_nodes);
    $member_nodes = node_load_multiple($nids);

    // Now, for each nodes, check the type and the access permissions before
    //   adding the node to the options list.
    $options_html = '';
    foreach ($member_nodes as $node) {

      // If the node is not a course or a class, leave the loop.
      if (!in_array($node->type, array(
        'course',
        'class',
      ))) {
        continue;
      }

      // If the user has no access to the stats page, leave the loop.
      if (!og_user_access('node', $node->nid, 'view group statistics')) {
        continue;
      }
      $options_html .= '<option value="' . $node->nid . '">' . $node->title . '</option>';
    }

    // Creates the block.
    $blocks['50001 Select list -1'] = array(
      'position' => '',
      'title' => t('Courses / Classes'),
      'content' => '
        <dl class="admin-list">
          <dt>' . t('Select your course or class') . '</dt>
          <dd><select id="course_stats">
            <option value="" selected="selected">-' . t('None') . '-</option>' . $options_html . '</select></dd>
        </dl>',
      'show' => true,
    );

    // Add the javascript that redirect when the user change the selection.
    drupal_add_js('
      jQuery(function() {
        var $ = jQuery;
        $("#course_stats").bind("change", function() {
          var node_id = $(this).find(":selected").val();
          if (node_id) {
            window.location = Drupal.settings.basePath +"node/"+ node_id +"/opigno-statistics";
          }
          return false;
        });
      });
    ', 'inline');
  }
  return $blocks;
}