original_string: public function unenrol_user($username, $courseid) {
          global $CFG, $DB;
  
          if (!$client = get_mnet_remote_client()) {
              die('Callable via XML-RPC only');
          }
  
          $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$client->id));
  
          if ($user === false) {
              throw new mnet_server_exception(5014, 'usernotfound', 'enrol_mnet');
          }
  
          if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
              throw new mnet_server_exception(5012, 'coursenotfound', 'enrol_mnet');
          }
  
          $courses = $this->available_courses();
          $isavailable = false;
          foreach ($courses as $available) {
              if ($available->remoteid == $course->id) {
                  $isavailable = true;
                  break;
              }
          }
          if (!$isavailable) {
              // if they can not enrol, they can not unenrol
              throw new mnet_server_exception(5013, 'courseunavailable', 'enrol_mnet');
          }
  
          // try to load host specific enrol_mnet instance first
          $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>$client->id), '*', IGNORE_MISSING);
  
          if ($instance === false) {
              // if not found, try to load instance for all hosts
              $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING);
              $instanceforall = true;
          }
  
          if ($instance === false) {
              // this should not happen as the course was returned by {@see self::available_courses()}
              throw new mnet_server_exception(5017, 'noenrolinstance', 'enrol_mnet');
          }
  
          if (!$enrol = enrol_get_plugin('mnet')) {
              throw new mnet_server_exception(5018, 'couldnotinstantiate', 'enrol_mnet');
          }
  
          if ($DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$user->id))) {
              try {
                  $enrol->unenrol_user($instance, $user->id);
  
              } catch (Exception $e) {
                  throw new mnet_server_exception(5020, 'couldnotunenrol', 'enrol_mnet', $e->getMessage());
              }
          }
  
          if (empty($instanceforall)) {
              // if the user was enrolled via 'All hosts' instance and the specific one
              // was created after that, the first enrolment would be kept.
              $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING);
  
              if ($instance) {
                  // repeat the same procedure for 'All hosts' instance, too. Note that as the host specific
                  // instance exists, it will be used for the future enrolments
  
                  if ($DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$user->id))) {
                      try {
                          $enrol->unenrol_user($instance, $user->id);
  
                      } catch (Exception $e) {
                          throw new mnet_server_exception(5020, 'couldnotunenrol', 'enrol_mnet', $e->getMessage());
                      }
                  }
              }
          }
  
          return true;
      }
code: public function unenrol_user($username, $courseid) {
          global $CFG, $DB;
  
          if (!$client = get_mnet_remote_client()) {
              die('Callable via XML-RPC only');
          }
  
          $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$client->id));
  
          if ($user === false) {
              throw new mnet_server_exception(5014, 'usernotfound', 'enrol_mnet');
          }
  
          if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
              throw new mnet_server_exception(5012, 'coursenotfound', 'enrol_mnet');
          }
  
          $courses = $this->available_courses();
          $isavailable = false;
          foreach ($courses as $available) {
              if ($available->remoteid == $course->id) {
                  $isavailable = true;
                  break;
              }
          }
          if (!$isavailable) {
              // if they can not enrol, they can not unenrol
              throw new mnet_server_exception(5013, 'courseunavailable', 'enrol_mnet');
          }
  
          // try to load host specific enrol_mnet instance first
          $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>$client->id), '*', IGNORE_MISSING);
  
          if ($instance === false) {
              // if not found, try to load instance for all hosts
              $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING);
              $instanceforall = true;
          }
  
          if ($instance === false) {
              // this should not happen as the course was returned by {@see self::available_courses()}
              throw new mnet_server_exception(5017, 'noenrolinstance', 'enrol_mnet');
          }
  
          if (!$enrol = enrol_get_plugin('mnet')) {
              throw new mnet_server_exception(5018, 'couldnotinstantiate', 'enrol_mnet');
          }
  
          if ($DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$user->id))) {
              try {
                  $enrol->unenrol_user($instance, $user->id);
  
              } catch (Exception $e) {
                  throw new mnet_server_exception(5020, 'couldnotunenrol', 'enrol_mnet', $e->getMessage());
              }
          }
  
          if (empty($instanceforall)) {
              // if the user was enrolled via 'All hosts' instance and the specific one
              // was created after that, the first enrolment would be kept.
              $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING);
  
              if ($instance) {
                  // repeat the same procedure for 'All hosts' instance, too. Note that as the host specific
                  // instance exists, it will be used for the future enrolments
  
                  if ($DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$user->id))) {
                      try {
                          $enrol->unenrol_user($instance, $user->id);
  
                      } catch (Exception $e) {
                          throw new mnet_server_exception(5020, 'couldnotunenrol', 'enrol_mnet', $e->getMessage());
                      }
                  }
              }
          }
  
          return true;
      }
code_tokens: public function unenrol_user ( $ username , $ courseid ) { global $ CFG , $ DB ; if ( ! $ client = get_mnet_remote_client ( ) ) { die ( 'Callable via XML-RPC only' ) ; } $ user = $ DB -> get_record ( 'user' , array ( 'username' => $ username , 'mnethostid' => $ client -> id ) ) ; if ( $ user === false ) { throw new mnet_server_exception ( 5014 , 'usernotfound' , 'enrol_mnet' ) ; } if ( ! $ course = $ DB -> get_record ( 'course' , array ( 'id' => $ courseid ) ) ) { throw new mnet_server_exception ( 5012 , 'coursenotfound' , 'enrol_mnet' ) ; } $ courses = $ this -> available_courses ( ) ; $ isavailable = false ; foreach ( $ courses as $ available ) { if ( $ available -> remoteid == $ course -> id ) { $ isavailable = true ; break ; } } if ( ! $ isavailable ) { // if they can not enrol, they can not unenrol throw new mnet_server_exception ( 5013 , 'courseunavailable' , 'enrol_mnet' ) ; } // try to load host specific enrol_mnet instance first $ instance = $ DB -> get_record ( 'enrol' , array ( 'courseid' => $ course -> id , 'enrol' => 'mnet' , 'customint1' => $ client -> id ) , '*' , IGNORE_MISSING ) ; if ( $ instance === false ) { // if not found, try to load instance for all hosts $ instance = $ DB -> get_record ( 'enrol' , array ( 'courseid' => $ course -> id , 'enrol' => 'mnet' , 'customint1' => 0 ) , '*' , IGNORE_MISSING ) ; $ instanceforall = true ; } if ( $ instance === false ) { // this should not happen as the course was returned by {@see self::available_courses()} throw new mnet_server_exception ( 5017 , 'noenrolinstance' , 'enrol_mnet' ) ; } if ( ! $ enrol = enrol_get_plugin ( 'mnet' ) ) { throw new mnet_server_exception ( 5018 , 'couldnotinstantiate' , 'enrol_mnet' ) ; } if ( $ DB -> record_exists ( 'user_enrolments' , array ( 'enrolid' => $ instance -> id , 'userid' => $ user -> id ) ) ) { try { $ enrol -> unenrol_user ( $ instance , $ user -> id ) ; } catch ( Exception $ e ) { throw new mnet_server_exception ( 5020 , 'couldnotunenrol' , 'enrol_mnet' , $ e -> getMessage ( ) ) ; } } if ( empty ( $ instanceforall ) ) { // if the user was enrolled via 'All hosts' instance and the specific one // was created after that, the first enrolment would be kept. $ instance = $ DB -> get_record ( 'enrol' , array ( 'courseid' => $ course -> id , 'enrol' => 'mnet' , 'customint1' => 0 ) , '*' , IGNORE_MISSING ) ; if ( $ instance ) { // repeat the same procedure for 'All hosts' instance, too. Note that as the host specific // instance exists, it will be used for the future enrolments if ( $ DB -> record_exists ( 'user_enrolments' , array ( 'enrolid' => $ instance -> id , 'userid' => $ user -> id ) ) ) { try { $ enrol -> unenrol_user ( $ instance , $ user -> id ) ; } catch ( Exception $ e ) { throw new mnet_server_exception ( 5020 , 'couldnotunenrol' , 'enrol_mnet' , $ e -> getMessage ( ) ) ; } } } } return true ; }
docstring: Unenrol remote user from our course
  
  Only users enrolled via enrol_mnet plugin can be unenrolled remotely. If the
  remote user is enrolled into the local course via some other enrol plugin
  (enrol_manual for example), the remote host can't touch such enrolment. Please
  do not report this behaviour as bug, it is a feature ;-)
  
  @uses mnet_remote_client Callable via XML-RPC only
  @param string $username of the remote user
  @param int $courseid of our local course
  @return bool true if the unenrolment has been successful, throws exception otherwise
docstring_tokens: Unenrol remote user from our course