123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- class ElggAnswer extends ElggObject {
-
- const SUBTYPE = 'answer';
-
- /**
- * (non-PHPdoc)
- * @see ElggObject::initializeAttributes()
- */
- function initializeAttributes() {
- parent::initializeAttributes();
-
- $this->attributes['subtype'] = self::SUBTYPE;
- }
-
- /**
- * (non-PHPdoc)
- * @see ElggEntity::getURL()
- */
- public function getURL() {
- $container_entity = $this->getContainerEntity();
-
- $url = $container_entity->getURL() . "#elgg-object-{$this->getGUID()}";
-
- return $url;
- }
-
- /**
- * (non-PHPdoc)
- * @see ElggObject::canComment()
- */
- public function canComment($user_guid = 0) {
-
- return $this->getContainerEntity()->canComment($user_guid);
- }
-
- /**
- * (non-PHPdoc)
- * @see ElggEntity::__get()
- */
- public function __get($name) {
-
- if ($name === 'title') {
- $question = $this->getContainerEntity();
-
- return elgg_echo('questions:object:answer:title', [$question->title]);
- }
-
- return parent::__get($name);
- }
-
- /**
- * Get the metadata object for the correct answer
- *
- * @return false|ElggMetadata
- */
- public function getCorrectAnswerMetadata() {
- $result = false;
-
- $options = [
- 'metadata_name' => 'correct_answer',
- 'guid' => $this->getGUID(),
- ];
-
- $metadata = elgg_get_metadata($options);
- if ($metadata) {
- $result = $metadata[0];
- }
-
- return $result;
- }
-
- /**
- * Mark an answer as the correct answer for this question
- *
- * @return void
- */
- public function markAsCorrect() {
- // first set the mark
- $this->correct_answer = true;
-
- // trigger event for notifications
- elgg_trigger_event('correct', 'object', $this);
-
- // depending of the plugin settings, we also need to close the question
- if (questions_close_on_marked_answer()) {
- $question = $this->getContainerEntity();
-
- $question->close();
- }
- }
-
- /**
- * This answer is no longer the correct answer for this question
- *
- * @return void
- */
- public function undoMarkAsCorrect() {
- unset($this->correct_answer);
-
- // don't forget to reopen the question
- $question = $this->getContainerEntity();
-
- $question->reopen();
- }
-
- /**
- * Check if we can auto mark this as the correct answer
- *
- * @param bool $creating new answer or editing (default: editing)
- *
- * @return void
- */
- public function checkAutoMarkCorrect($creating = false) {
-
- $creating = (bool) $creating;
- if (empty($creating)) {
- // only on new entities
- return;
- }
-
- $question = $this->getContainerEntity();
- $container = $question->getContainerEntity();
-
- $user = $this->getOwnerEntity();
-
- if (questions_auto_mark_answer_correct($container, $user)) {
- $this->markAsCorrect();
- }
- }
- }
|