/** * Publish post also on frontend * * @return bool */ public function publishOnFrontend() { if ($this->answerGuid == 1) { $answer = new ElggAnswer(); $answer->description = $this->description; $answer->intanswerGuid = $this->guid; $answer->container_guid = $this->container_guid; $answer->access_id = $this->getQuestion()->access_id; $publish_user = elgg_get_plugin_setting('workflow_publishuser', 'questions'); if ($publish_user && ($user = get_user_by_username($publish_user))) { $answer->owner_guid = $user->guid; // ignore access because we would maybe want to write in the name of publishuser. elgg_set_ignore_access(true); $answer->save(); elgg_set_ignore_access(false); } else { $answer->save(); } $this->answerGuid = $answer->guid; update_entity_last_action($answer->container_guid, $answer->time_created); } elseif (is_int($this->answerGuid)) { $answer = get_entity($this->answerGuid); if ($answer instanceof ElggAnswer) { // ignore access because we would maybe want to overwrite entity of publishuser. elgg_set_ignore_access(true); $answer->description = $this->description; $answer->save(); elgg_set_ignore_access(false); } } }
/** * Check if the user can mark this answer as the correct one * * @param ElggAnswer $entity the answer to check * @param ElggUser $user the use who is wants to do the action (defaults to current user) * * @return bool */ function questions_can_mark_answer(ElggAnswer $entity, ElggUser $user = null) { $result = false; static $experts_only; // check if we have a user if (empty($user) || !$user instanceof ElggUser) { $user = elgg_get_logged_in_user_entity(); } if (empty($user) || empty($entity) || !$entity instanceof ElggAnswer) { return false; } $container = $entity->getContainerEntity(); // are experts enabled if (!questions_experts_enabled()) { // no, so only question owner can mark if ($user->getGUID() == $container->getOwnerGUID()) { $result = true; } } else { // get plugin setting for who can mark the answer if (!isset($experts_only)) { $experts_only = false; $setting = elgg_get_plugin_setting('experts_mark', 'questions'); if ($setting == 'yes') { $experts_only = true; } } // are only experts allowed to mark if (!$experts_only) { // no, so the owner of a question can also mark if ($user->getGUID() == $container->getOwnerGUID()) { $result = true; } } // is the user an expert if (!$result && questions_is_expert($container->getContainerEntity(), $user)) { $result = true; } } return $result; }
<?php elgg_make_sticky_form('answer'); $guid = (int) get_input('guid'); $answer = new ElggAnswer($guid); $adding = !$answer->guid; $editing = !$adding; if ($editing && !$answer->canEdit()) { register_error(elgg_echo('InvalidParameterException:NoEntityFound')); forward(REFERER); } $container_guid = (int) get_input('container_guid'); $description = get_input('description'); if (empty($container_guid) || empty($description)) { register_error(elgg_echo('questions:action:answer:save:error:body', [$container_guid, $description])); forward(REFERER); } if ($adding && !can_write_to_container(0, $container_guid, 'object', 'answer')) { register_error(elgg_echo('questions:action:answer:save:error:container')); forward(REFERER); } $question = get_entity($container_guid); if (empty($question) || !$question instanceof ElggQuestion) { register_error(elgg_echo('ClassException:ClassnameNotClass', [$container_guid, elgg_echo('item:object:question')])); forward(REFERER); } if ($question->getStatus() != 'open') { elgg_clear_sticky_form('answer'); register_error(elgg_echo('questions:action:answer:save:error:question_closed')); forward(REFERER); }
<?php /** * Save answer object * * @package Questions * */ elgg_make_sticky_form('answer'); $guid = (int) get_input('guid'); $answer = new ElggAnswer($guid); $adding = !$answer->guid; $editing = !$adding; if ($editing && !$answer->canEdit()) { register_error(elgg_echo("InvalidParameterException:NoEntityFound")); forward(REFERER); } $container_guid = (int) get_input('container_guid'); $description = get_input('description'); if (empty($container_guid) || empty($description)) { register_error(elgg_echo("questions:action:answer:save:error:body", array($container_guid, $description))); forward(REFERER); } if ($adding && !can_write_to_container(0, $container_guid, 'object', 'answer')) { register_error(elgg_echo("questions:action:answer:save:error:container")); forward(REFERER); } $question = get_entity($container_guid); if (empty($question) || !elgg_instanceof($question, "object", "question")) { register_error(elgg_echo("ClassException:ClassnameNotClass", array($container_guid, elgg_echo("item:object:question")))); forward(REFERER);