Fix bug -refs BT#7959

Cherrypick from upstream chamilo/chamilo.
Original commits:
d7d278bfda67fa6c0825c16d06708a4d07d0db57
a6c1885cdf9d0cf3c56312f8718bc1cdd28125a9