/**
  * Obtains subject. Note this results in a DB query if the discussion
  * was not fully loaded in the first place.
  * @param bool $expectingquery True if code expects there to be a query;
  *   this just avoids a debugging() call.
  * @return string Subject or null if none
  */
 public function get_subject($expectingquery = false)
 {
     if (!isset($this->discussionfields->subject)) {
         if (!$expectingquery) {
             debugging('This get method made a DB query; if this is expected,
                 set the flag to say so', DEBUG_DEVELOPER);
         }
         $this->discussionfields->subject = forum_utils::get_field('forumng_posts', 'subject', 'id', $this->discussionfields->postid);
     }
     return $this->discussionfields->subject;
 }
 /**
  * Creates a forum object and all related data from a single course-module
  * ID. Intended to be used from pages that refer to a particular forum.
  * @param int $cmid Course-module ID of forum
  * @param int $cloneid Clone identifier (0 if not a shared forum) or
  *   CLONE_DIRECT constant
  * @return forum Forum object
  */
 public static function get_from_cmid($cmid, $cloneid)
 {
     global $COURSE;
     // Get modinfo for current course, because we usually already have it
     $modinfo = get_fast_modinfo($COURSE);
     if (array_key_exists($cmid, $modinfo->cms)) {
         // It's in the current course, no need for another query
         $courseid = $COURSE->id;
     } else {
         // Get courseid
         $courseid = forum_utils::get_field('course_modules', 'course', 'id', $cmid);
     }
     // Get course
     if (!empty($COURSE->id) && $COURSE->id === $courseid) {
         $course = $COURSE;
     } else {
         $course = forum_utils::get_record('course', 'id', $courseid);
     }
     // Get course-module
     $modinfo = forum::get_fast_modinfo($course, $cmid);
     if (!array_key_exists($cmid, $modinfo->cms)) {
         throw new forum_exception("Couldn't find forum with course-module ID {$cmid}");
     }
     $cm = $modinfo->cms[$cmid];
     if ($cm->modname != 'forumng') {
         throw new forum_exception("Course-module ID {$cmid} is not a forum");
     }
     // Get forum data
     $forumfields = forum_utils::get_record('forumng', 'id', $cm->instance);
     // Get context
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
     // Construct forum
     $result = new forum($course, $cm, $context, $forumfields);
     if ($result->is_shared()) {
         if (!$cloneid) {
             throw new forum_exception("Shared forum {$cmid} requires a clone id");
         }
         $result->set_clone_reference($cloneid);
     }
     return $result;
 }