MDL-47778 tool_monitor: check courses exist before loop

Also -

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