/** * Add menu items to the entity menu * * @param string $hook the name of the hook * @param string $type the type of the hook * @param ElggMenuItem[] $items current return value * @param array $params supplied params * * @return void|ElggMenuItem[] */ function questions_entity_menu_handler($hook, $type, $items, $params) { if (empty($params) || !is_array($params)) { return; } $entity = elgg_extract('entity', $params); if (empty($entity) || !$entity instanceof ElggQuestion && !$entity instanceof ElggAnswer) { return; } if ($entity->canComment()) { if (elgg_extract('full_view', $params, false) || $entity instanceof ElggAnswer) { $items[] = ElggMenuItem::factory(['name' => 'comments', 'rel' => 'toggle', 'link_class' => 'elgg-toggler', 'href' => "#comments-add-{$entity->getGUID()}", 'text' => elgg_view_icon('speech-bubble'), 'priority' => 600]); } } if (elgg_in_context('questions') && $entity instanceof ElggAnswer && questions_can_mark_answer($entity)) { $question = $entity->getContainerEntity(); $answer = $question->getMarkedAnswer(); if (empty($answer)) { $items[] = ElggMenuItem::factory(['name' => 'questions_mark', 'text' => elgg_echo('questions:menu:entity:answer:mark'), 'href' => "action/answers/toggle_mark?guid={$entity->getGUID()}", 'is_action' => true]); } elseif ($entity->getGUID() === $answer->getGUID()) { // there is an anwser and it's this entity $items[] = ElggMenuItem::factory(['name' => 'questions_mark', 'text' => elgg_echo('questions:menu:entity:answer:unmark'), 'href' => "action/answers/toggle_mark?guid={$entity->getGUID()}", 'is_action' => true]); } } return $items; }
function questions_entity_menu_handler($hook, $type, $items, $params) { if (!empty($params) && is_array($params)) { $entity = elgg_extract("entity", $params); if (!empty($entity) && (elgg_instanceof($entity, "object", "question") || elgg_instanceof($entity, "object", "answer"))) { if ($entity->canComment()) { if (elgg_extract("full_view", $params, false) || elgg_instanceof($entity, "object", "answer")) { $items[] = ElggMenuItem::factory(array("name" => "comment", "rel" => "toggle", "link_class" => "elgg-toggler", "href" => "#comments-add-{$entity->guid}", "text" => elgg_view_icon("speech-bubble"), "priority" => 600)); } } if (elgg_instanceof($entity, "object", "answer") && questions_can_mark_answer($entity)) { $question = $entity->getContainerEntity(); $answer = $question->getCorrectAnswer(); if (empty($answer)) { $items[] = ElggMenuItem::factory(array("name" => "questions_mark", "text" => elgg_echo("questions:menu:entity:answer:mark"), "href" => "action/answers/toggle_mark?guid=" . $entity->getGUID(), "is_action" => true)); } elseif ($entity->getGUID() == $answer->getGUID()) { // there is an anwser and it's this entity $items[] = ElggMenuItem::factory(array("name" => "questions_mark", "text" => elgg_echo("questions:menu:entity:answer:unmark"), "href" => "action/answers/toggle_mark?guid=" . $entity->getGUID(), "is_action" => true)); } } } } return $items; }
<?php /** * This action marks an answer as the correct answer for a question. * */ $guid = (int) get_input('guid'); if (empty($guid)) { register_error(elgg_echo('InvalidParameterException:MissingParameter')); forward(REFERER); } elgg_entity_gatekeeper($guid, 'object', 'answer'); $entity = get_entity($guid); // are you allowed to mark answers as correct if (!questions_can_mark_answer($entity)) { register_error(elgg_echo('questions:action:answer:toggle_mark:error:not_allowed')); forward(REFERER); } $question = $entity->getContainerEntity(); $answer = $question->getMarkedAnswer(); if (empty($answer)) { // no answer yet, so mark this one $entity->markAsCorrect(); system_message(elgg_echo('questions:action:answer:toggle_mark:success:mark')); } elseif ($answer->getGUID() == $entity->getGUID()) { // the marked answer is this answer, so unmark $entity->undoMarkAsCorrect(); system_message(elgg_echo('questions:action:answer:toggle_mark:success:unmark')); } else { register_error(elgg_echo('questions:action:answer:toggle_mark:error:duplicate')); }
<?php /** * Mark answer as correct * * @package Questions * */ $guid = (int) get_input("guid"); if (!empty($guid)) { $answer = get_entity($guid); if (!empty($answer) && elgg_instanceof($answer, "object", "answer")) { if (questions_can_mark_answer($answer)) { $question = $answer->getQuestion(); $correctAnswer = $question->getCorrectAnswer(); if (!$correctAnswer) { $answer->markCorrect(); system_message(elgg_echo("questions:action:answer:toggle_mark:success:mark")); } elseif ($answer == $correctAnswer) { $answer->unmarkCorrect(); system_message(elgg_echo("questions:action:answer:toggle_mark:success:unmark")); } } else { register_error(elgg_echo("questions:action:answer:toggle_mark:error:not_allowed")); } } else { register_error(elgg_echo("InvalidParameterException:GUIDNotFound", array($guid))); } } else { register_error(elgg_echo("InvalidParameterException:MissingParameter")); }