Thanks for this patch Iñaki. I've installed in on our Moodle 2.3 and it appears to have worked.
The only issue I had on install occured at the last step at Site Admin / Notifications / Upgrade. Moodle recognised that an upgrade was required OK, but when I ran the upgrade it returned the following error:
Fatal Error: Call to undefined function xmldb_auth_ldap2_install() in /home/XXXXXXXXXXXXXXX/public_html/lib/upgradelib.php on line 445
Even though it produced this error the new instance of LDAP (defined as ldap2) appeared as an authentication method and I was able to configure and use it with success. Should I be worried about the error ?
Also, do you know of a way to set up LDAP authentication so that two LDAP servers are defined, but the same authentication method 'ldap' is used for both servers and entered in user profiles. When a user with the authentication method of 'ldap' attempts to login, Moodle checks the first LDAP server and if that fails, checks the second LDAP server.