Clean work.php to move queries to functions
It is dangerous and ugly to have queries in work.php as we cannot easily guarantee the maintenance of the security level of all parameters used.
Move all queries to functions inside work.lib.php and call them from work.php.
There are about 50 queries in total, most of which are probably repeated, and some of them might already have functions providing it.
The opening of this task is related to security issue #2082