You are here

function opigno_og_prereq_og_role_grant in Opigno 7

Implements hook_og_role_grant().

1 call to opigno_og_prereq_og_role_grant()
opigno_og_prereq_og_role_revoke in modules/og_prereq/opigno_og_prereq.module
Implements hook_og_role_revoke().

File

modules/og_prereq/opigno_og_prereq.module, line 34
Module hook implementations.

Code

function opigno_og_prereq_og_role_grant($entity_type, $gid, $uid, $rid) {
  $account = user_load($uid, TRUE);
  if ($entity_type == 'node') {
    if (!og_user_access($entity_type, $gid, 'skip pre-required group when added as a member', $account)) {
      $required_courses = opigno_og_prereq_fetch_required_courses(node_load($gid));
      if (!empty($required_courses)) {
        foreach ($required_courses as $required_course) {

          // If the user already passed the pre-test, don't deactivate his membership.
          if (!opigno_og_prereq_user_passed_course($uid, $required_course)) {
            $query = new EntityFieldQuery();
            $query
              ->entityCondition('entity_type', 'og_membership', '=')
              ->propertyCondition('gid', $gid, '=')
              ->propertyCondition('entity_type', 'user', '=')
              ->propertyCondition('etid', $uid, '=');
            $result = $query
              ->execute();
            if (!empty($result['og_membership'])) {
              $og_membership = og_membership_load(current($result['og_membership'])->id);
              if ($og_membership->state == OG_STATE_ACTIVE) {
                $og_membership->state = OG_STATE_PENDING;
                og_membership_save($og_membership);
                drupal_set_message(t("The membership for %user was temporarily set to <em>pending</em>. The user must first fulfill the required courses to get full access to this group.", array(
                  '%user' => $account->name,
                )), 'warning', FALSE);
              }
            }
          }
        }
      }
    }
    else {
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', 'og_membership', '=')
        ->propertyCondition('gid', $gid, '=')
        ->propertyCondition('entity_type', 'user', '=')
        ->propertyCondition('etid', $uid, '=');
      $result = $query
        ->execute();
      if (!empty($result['og_membership'])) {
        $og_membership = og_membership_load(current($result['og_membership'])->id);
        if ($og_membership->state == OG_STATE_PENDING) {
          $og_membership->state = OG_STATE_ACTIVE;
          og_membership_save($og_membership);
        }
      }
    }
  }
}