Merge branch 'w51_MDL-37303_m25_enrolinstancestatus' of git://github.com/skodak/moodle
authorDan Poltawski <dan@moodle.com>
Tue, 8 Jan 2013 06:22:11 +0000 (14:22 +0800)
committerDan Poltawski <dan@moodle.com>
Tue, 8 Jan 2013 06:22:11 +0000 (14:22 +0800)
1  2 
enrol/locallib.php

diff --combined enrol/locallib.php
@@@ -586,29 -586,16 +586,29 @@@ class course_enrolment_manager 
       */
      public function unassign_role_from_user($userid, $roleid) {
          global $DB;
 -        require_capability('moodle/role:assign', $this->context);
 -        $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
 -        try {
 -            role_unassign($roleid, $user->id, $this->context->id, '', NULL);
 -        } catch (Exception $e) {
 +        // Admins may unassign any role, others only those they could assign.
 +        if (!is_siteadmin() and !array_key_exists($roleid, $this->get_assignable_roles())) {
              if (defined('AJAX_SCRIPT')) {
 -                throw $e;
 +                throw new moodle_exception('invalidrole');
              }
              return false;
          }
 +        $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
 +        $ras = $DB->get_records('role_assignments', array('contextid'=>$this->context->id, 'userid'=>$user->id, 'roleid'=>$roleid));
 +        foreach ($ras as $ra) {
 +            if ($ra->component) {
 +                if (strpos($ra->component, 'enrol_') !== 0) {
 +                    continue;
 +                }
 +                if (!$plugin = enrol_get_plugin(substr($ra->component, 6))) {
 +                    continue;
 +                }
 +                if ($plugin->roles_protected()) {
 +                    continue;
 +                }
 +            }
 +            role_unassign($ra->roleid, $ra->userid, $ra->contextid, $ra->component, $ra->itemid);
 +        }
          return true;
      }
  
      public function get_user_roles($userid) {
          $roles = array();
          $ras = get_user_roles($this->context, $userid, true, 'c.contextlevel DESC, r.sortorder ASC');
 +        $plugins = $this->get_enrolment_plugins(false);
          foreach ($ras as $ra) {
              if ($ra->contextid != $this->context->id) {
                  if (!array_key_exists($ra->roleid, $roles)) {
              if (array_key_exists($ra->roleid, $roles) && $roles[$ra->roleid] === false) {
                  continue;
              }
 -            $roles[$ra->roleid] = ($ra->itemid == 0 and $ra->component === '');
 +            $changeable = true;
 +            if ($ra->component) {
 +                $changeable = false;
 +                if (strpos($ra->component, 'enrol_') === 0) {
 +                    $plugin = substr($ra->component, 6);
 +                    if (isset($plugins[$plugin])) {
 +                        $changeable = !$plugins[$plugin]->roles_protected();
 +                    }
 +                }
 +            }
 +
 +            $roles[$ra->roleid] = $changeable;
          }
          return $roles;
      }
  
          $userroles = $this->get_other_users($sort, $direction, $page, $perpage);
          $roles = $this->get_all_roles();
 +        $plugins = $this->get_enrolment_plugins(false);
  
          $context    = $this->get_context();
          $now = time();
              }
              $a = new stdClass;
              $a->role = $roles[$userrole->roleid]->localname;
 -            $changeable = ($userrole->component == '');
              if ($contextid == $this->context->id) {
 +                $changeable = true;
 +                if ($userrole->component) {
 +                    $changeable = false;
 +                    if (strpos($userrole->component, 'enrol_') === 0) {
 +                        $plugin = substr($userrole->component, 6);
 +                        if (isset($plugins[$plugin])) {
 +                            $changeable = !$plugin[$plugin]->roles_protected();
 +                        }
 +                    }
 +                }
                  $roletext = get_string('rolefromthiscourse', 'enrol', $a);
              } else {
                  $changeable = false;
              // Roles
              $details['roles'] = array();
              foreach ($this->get_user_roles($user->id) as $rid=>$rassignable) {
 -                $details['roles'][$rid] = array('text'=>$allroles[$rid]->localname, 'unchangeable'=>(!$rassignable || !isset($assignable[$rid])));
 +                $unchangeable = !$rassignable;
 +                if (!is_siteadmin() and !isset($assignable[$rid])) {
 +                    $unchangeable = true;
 +                }
 +                $details['roles'][$rid] = array('text'=>$allroles[$rid]->localname, 'unchangeable'=>$unchangeable);
              }
  
              // Users
                  $details['enrolments'][$ue->id] = array(
                      'text' => $ue->enrolmentinstancename,
                      'period' => $period,
-                     'dimmed' =>  ($periodoutside || $ue->status != ENROL_USER_ACTIVE),
+                     'dimmed' =>  ($periodoutside or $ue->status != ENROL_USER_ACTIVE or $ue->enrolmentinstance->status != ENROL_INSTANCE_ENABLED),
                      'actions' => $ue->enrolmentplugin->get_user_enrolment_actions($manager, $ue)
                  );
              }