Fixes bug 2334 http://moodle.org/bugs/bug.php?op=show&bugid=2334
Courses will sometimes fail to be moved across categories if there are collisions in the sortorder field, due to a combined unique index.
With this fix, the 'category move' logic is aware of sortorder and will do The Right Thing(tm) putting the course at the beginning of the category: MIN(sortorder)-1 WHERE category=$destcategory->id .
Also added further cleanup patches.