/** * Post a new reply. */ public function post_reply($contents, $anonymous = 0) { global $DB, $USER, $PAGE; $reply = new \stdClass(); $reply->qaqid = $this->id; $reply->userid = $USER->id; $reply->anonymous = $anonymous; $reply->content = $contents; $reply->timecreated = time(); $reply->timemodified = time(); $id = $DB->insert_record('qa_replies', $reply); $reply->id = $id; $event = \mod_qa\event\reply_posted::create(array('objectid' => $reply->id, 'context' => $PAGE->context, 'userid' => $anonymous ? 0 : $USER->id, 'other' => array('questionid' => $this->id, 'questiontitle' => $this->title))); $event->add_record_snapshot('qa_replies', $reply); $event->trigger(); return reply::from_db($reply); }
/** * Returns questions. */ public function get_questions() { global $DB; if (!isset($this->questions)) { $questions = $DB->get_records('qa_questions', array('qaid' => $this->data->id)); $this->questions = array(); if (empty($questions)) { return array(); } $ids = array_keys($questions); list($sql, $params) = $DB->get_in_or_equal($ids); $replies = $DB->get_records_sql('SELECT * FROM {qa_replies} WHERE qaqid ' . $sql, $params); $votes = $DB->get_records_sql('SELECT * FROM {qa_votes} WHERE qaqid ' . $sql, $params); foreach ($questions as $question) { $this->questions[$question->id] = question::from_db($question); $this->questions[$question->id]->qa = $this; $this->questions[$question->id]->replies = array(); foreach ($replies as $reply) { if ($reply->qaqid == $question->id) { $this->questions[$question->id]->replies[$reply->id] = reply::from_db($reply); } } $this->questions[$question->id]->votes = array(); foreach ($votes as $vote) { if ($vote->qaqid == $question->id) { $this->questions[$question->id]->votes[$vote->userid] = true; } } } } return $this->questions; }