MDL-47778 tool_monitor: check courses exist before loop

Also -

1) Using format_string() instead of format_text().
2) Using the course context, rather than system context.