/**
  * Called when a post is deleted or undeleted or modified, or there is a
  * larger change to the discussion
  * @param mod_forumng_post $post Post that has changed; null to always recalculate
  */
 function possible_lastpost_change($post = null)
 {
     $recalculate = false;
     if (!$post) {
         $recalculate = true;
     } else {
         if ($post->get_deleted()) {
             // For deleted posts, recalculate if this was previously
             // considered the latest post
             $recalculate = $this->discussionfields->lastpostid == $post->get_id();
         } else {
             // For other posts, recalculate if this is now newer than the
             // stored last post
             $recalculate = $post->get_modified() > $this->discussionfields->timemodified;
         }
     }
     // If necessary, recalculate the date
     if ($recalculate) {
         global $DB;
         $change = new stdClass();
         $change->id = $this->get_id();
         $records = $DB->get_records_sql("SELECT id " . "FROM {forumng_posts} WHERE discussionid = ? AND deleted = 0 AND oldversion = 0 " . "ORDER BY modified DESC", array($this->get_id()), 0, 1);
         if (count($records) == 0) {
             throw new moodle_exception('errorfindinglastpost', 'forumng');
         }
         $rec = reset($records);
         $change->lastpostid = $rec->id;
         if ($change->lastpostid != $this->discussionfields->lastpostid) {
             $DB->update_record('forumng_discussions', $change);
         }
     }
 }