Moving code inside agenda.lib.php see BT#7772
Fixing query.