/** * Constructs a post object and (recursively) all of its children from * information retrieved from the database. * @param $posts Array of post ID => fields from DB query * @param $children Array of post ID => array of child IDs * @param $id ID of post to construct * @param $parent Parent post or NULL if none * @return mod_forumng_post Newly-created post * @throws mod_forumng_exception If ID is invalid */ private function build_posts(&$posts, &$children, $id, $parent) { if (!array_key_exists($id, $posts)) { $msg = "No such post: {$id} (discussion " . $this->get_id() . '); ' . 'posts'; foreach ($posts as $id => $junk) { $msg .= ' ' . $id; } $msg .= '; children'; foreach ($children as $id => $junk) { $msg .= ' ' . $id; } throw new dml_exception($msg); } $post = new mod_forumng_post($this, $posts[$id], $parent); $this->totalsize += strlen($posts[$id]->message); $post->init_children(); if (array_key_exists($id, $children)) { foreach ($children[$id] as $childid) { $post->add_child($this->build_posts($posts, $children, $childid, $post)); } } return $post; }