/**
  * Creates the mail queue and runs query to obtain list of posts that should
  * be mailed.
  * @param bool $tracetimes True if it should call mtrace to display
  *   performance information
  */
 function __construct($tracetimes)
 {
     global $CFG;
     $this->time = time();
     $this->forum = null;
     $this->discussion = null;
     $this->storedrecord = null;
     $this->postcount = 0;
     // Check if an earlier run got aborted. In that case we mark all
     // messages as mailed anyway because it's better to skip some than
     // to send out double-posts.
     if ($pending = get_config('forumng', $this->get_pending_flag_name())) {
         $this->mark_mailed($pending);
     }
     // Note that we are mid-run
     set_config($this->get_pending_flag_name(), $this->time, 'forumng');
     $querychunk = $this->get_query_chunk($this->time);
     if (!($this->rs = get_recordset_sql($sql = "\nSELECT\n    " . forum_utils::select_forum_fields('f') . ",\n    " . forum_utils::select_discussion_fields('fd') . ",\n    " . forum_utils::select_post_fields('discussionpost') . ",\n    " . forum_utils::select_post_fields('fp') . ",\n    " . forum_utils::select_post_fields('reply') . ",\n    " . forum_utils::select_course_module_fields('cm') . ",\n    " . forum_utils::select_context_fields('x') . ",\n    " . forum_utils::select_username_fields('u', true) . ",\n    " . forum_utils::select_username_fields('eu') . ",\n    " . forum_utils::select_username_fields('replyu') . ",\n    " . forum_utils::select_username_fields('replyeu') . ",\n    " . forum_utils::select_course_fields('c') . ",\n    clonecm.id AS cloneid\n{$querychunk}\nORDER BY\n    clonecm.course, f.id, fd.id, fp.id"))) {
         throw new forum_exception("Mail queue query failed");
     }
 }