/** * Notify the experts that a new question was asked * * @param ElggQuestion $entity the question to notify about * @param bool $moving is this question being moved * * @return void */ function questions_notify_experts(ElggQuestion $entity, $moving = false) { // only if experts enabled if (!questions_experts_enabled()) { return; } // validate input if (!$entity instanceof ElggQuestion) { return; } $experts = []; $container = $entity->getContainerEntity(); if (!$container instanceof ElggGroup) { $container = elgg_get_site_entity(); } // get experts $options = ['type' => 'user', 'site_guids' => false, 'limit' => false, 'relationship' => QUESTIONS_EXPERT_ROLE, 'relationship_guid' => $container->getGUID(), 'inverse_relationship' => true]; $users = elgg_get_entities_from_relationship($options); if (!empty($users)) { $experts = $users; } // trigger a hook so others can extend the list $params = ['entity' => $entity, 'experts' => $experts, 'moving' => $moving]; $experts = elgg_trigger_plugin_hook('notify_experts', 'questions', $params, $experts); if (empty($experts) || !is_array($experts)) { return; } $subject_key = 'questions:notify_experts:create:subject'; $message_key = 'questions:notify_experts:create:message'; if ($moving) { $subject_key = 'questions:notify_experts:moving:subject'; $message_key = 'questions:notify_experts:moving:message'; } $subject = elgg_echo($subject_key); foreach ($experts as $expert) { if (!$expert instanceof ElggUser) { continue; } $message = elgg_echo($message_key, [$expert->name, $entity->title, $entity->getURL()]); notify_user($expert->getGUID(), $entity->getOwnerGUID(), $subject, $message, null, 'email'); } }
<?php elgg_make_sticky_form('question'); $guid = get_input('guid'); $question = new ElggQuestion($guid); $adding = !$question->guid; $editing = !$adding; if ($editing && !$question->canEdit()) { register_error(elgg_echo('questions:edit_error')); forward(REFERER); } $container_guid = get_input('container_guid'); if (!$container_guid) { $container_guid = elgg_get_logged_in_user_guid(); } /* if ($adding && !can_write_to_container(0, $container_guid, 'object', 'question')) { register_error("You do not have permission to add questions here!"); forward(REFERER); } */ $title = get_input('title', '', false); $description = get_input('description'); $tags = string_to_tag_array(get_input('tags', '')); if (empty($container_guid) || empty($title) || empty($description)) { register_error(elgg_echo('questions:empty_fields')); forward(REFERER); } $question->title = $title; $question->description = $description; $question->tags = $tags;
<?php $question = elgg_extract('entity', $vars); $show_group_selector = (bool) elgg_extract('show_group_selector', $vars, true); $editing = true; $container_options = false; $show_access_options = true; $access_setting = false; if (!$question) { $editing = false; $question = new ElggQuestion(); $question->container_guid = elgg_get_page_owner_guid(); $question->access_id = get_default_access(null, ['entity_type' => $question->getType(), 'entity_subtype' => $question->getSubtype(), 'container_guid' => $question->getContainerGUID()]); } $container = $question->getContainerEntity(); $title = ['name' => 'title', 'id' => 'question_title', 'value' => elgg_get_sticky_value('question', 'title', $question->title), 'required' => true]; $description = ['name' => 'description', 'id' => 'question_description', 'value' => elgg_get_sticky_value('question', 'description', $question->description)]; $tags = ['name' => 'tags', 'id' => 'question_tags', 'value' => elgg_get_sticky_value('question', 'tags', $question->tags)]; $comment_options = ['name' => 'comments_enabled', 'id' => 'questions-comments', 'value' => elgg_get_sticky_value('question', 'comments_enabled', $question->comments_enabled), 'options_values' => ['on' => elgg_echo('on'), 'off' => elgg_echo('off')], 'class' => 'mls']; if ($container instanceof ElggUser) { $access_setting = questions_get_personal_access_level(); if ($access_setting !== false) { $show_access_options = false; } } elseif ($container instanceof ElggGroup) { $access_setting = questions_get_group_access_level($container); if ($access_setting !== false) { $show_access_options = false; } } $access_id = ['name' => 'access_id', 'id' => 'question_access_id', 'value' => (int) elgg_get_sticky_value('question', 'access_id', $question->access_id)];
/** * Check if a user can answer a question * * @param ElggQuestion $question the question that needs answer * @param ElggUser $user the user askting the question (default: current user) * * @return bool */ function questions_can_answer_question(ElggQuestion $question, ElggUser $user = null) { static $general_experts_only; // default to page owner if (!$question instanceof ElggQuestion) { return false; } // default to current user if (!$user instanceof ElggUser) { $user = elgg_get_logged_in_user_entity(); } if (empty($user)) { // not logged in return false; } $container = $question->getContainerEntity(); if (!questions_experts_enabled()) { return questions_can_ask_question($container, $user); } // get plugin setting if (!isset($general_experts_only)) { $general_experts_only = questions_experts_only_answer(); } $is_expert = questions_is_expert($container, $user); // check general setting if ($general_experts_only && !$is_expert) { return false; } if (!$container instanceof ElggGroup) { return true; } // check group settings $group_experts_only = false; if ($container->getPrivateSetting('questions_who_can_answer') === 'experts') { $group_experts_only = true; } if ($group_experts_only && !$is_expert) { return false; } // are you a group member or can you edit the group return $container->isMember($user) || $container->canEdit($user->getGUID()); }
<?php /** * Save the question * * @package Questions * */ elgg_make_sticky_form("question"); $guid = (int) get_input("guid"); $question = new ElggQuestion($guid); $adding = !$question->guid; $editing = !$adding; $moving = false; if ($editing && !$question->canEdit()) { register_error(elgg_echo("InvalidParameterException:NoEntityFound")); forward(REFERER); } $container_guid = (int) get_input("container_guid"); if (empty($container_guid)) { $container_guid = (int) $question->owner_guid; } if ($editing && $container_guid != $question->getContainerGUID()) { $moving = true; } if ($adding && !can_write_to_container(0, $container_guid, "object", "question")) { register_error(elgg_echo("questions:action:question:save:error:container")); forward(REFERER); } if (questions_limited_to_groups() && $container_guid == $question->owner_guid) { register_error(elgg_echo("questions:action:question:save:error:limited_to_groups"));
<?php /** * Question form * * @package ElggQuestions */ $question = elgg_extract("entity", $vars); $editing = true; $container_options = false; $show_access_options = true; $access_setting = false; if (!$question) { $editing = false; $question = new ElggQuestion(); $question->container_guid = elgg_get_page_owner_guid(); $question->access_id = ACCESS_DEFAULT; } $container = $question->getContainerEntity(); $title = array("name" => "title", "id" => "question_title", "value" => elgg_get_sticky_value("question", "title", $question->title)); $description = array("name" => "description", "id" => "question_description", "value" => elgg_get_sticky_value("question", "description", $question->description)); $tags = array("name" => "tags", "id" => "question_tags", "value" => elgg_get_sticky_value("question", "tags", $question->tags)); if (elgg_instanceof($container, "user")) { $access_setting = questions_get_personal_access_level(); if ($access_setting !== false) { $show_access_options = false; } } elseif (elgg_instanceof($container, "group")) { $access_setting = questions_get_group_access_level($container); if ($access_setting !== false) { $show_access_options = false;
/** * Send a workflow notification for a question to the phase owner * * @param ElggQuestion $question the question the notification is about * @param QuesionsWorkflowPhase $phase the phase of the workflow * * @return bool */ function questions_send_workflow_notification(ElggQuestion $question, QuestionsWorkflowPhase $phase) { return elgg_send_email(questions_get_site_email(), $phase->email, elgg_echo("questions:workflow:email:subject", array($question->title)), elgg_echo("questions:workflow:email:body", array($question->getWorkflowURL()))); }
<?php /** * Question form * * @package ElggQuestions */ $question = elgg_extract("entity", $vars); $editing = true; $container_options = false; $show_access_options = true; $access_setting = false; if (!$question) { $editing = false; $question = new ElggQuestion(); $question->container_guid = elgg_get_page_owner_guid(); $question->access_id = ACCESS_DEFAULT; } $container = $question->getContainerEntity(); if ($container instanceof ElggGroup) { $access_setting = questions_get_group_access_level($container); if ($access_setting !== false) { $show_access_options = false; } } else { $access_setting = questions_get_personal_access_level(); if ($access_setting !== false) { $show_access_options = false; } } $access_id = array("name" => "access_id", "id" => "question_access_id", "value" => (int) elgg_get_sticky_value("question", "access_id", $question->access_id));