示例#1
0
/**
 * 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());
}
示例#2
0
 if ($show_group_selector && elgg_is_active_plugin('groups')) {
     $group_options = ['type' => 'group', 'limit' => false, 'metadata_name_value_pairs' => ['name' => 'questions_enable', 'value' => 'yes'], 'joins' => ['JOIN ' . elgg_get_config('dbprefix') . 'groups_entity ge ON e.guid = ge.guid'], 'order_by' => 'ge.name ASC'];
     if (!$editing) {
         $owner = elgg_get_logged_in_user_entity();
         $group_options['relationship'] = 'member';
         $group_options['relationship_guid'] = elgg_get_logged_in_user_guid();
     } else {
         $owner = $question->getOwnerEntity();
     }
     // group selector
     $groups = new ElggBatch('elgg_get_entities_from_relationship', $group_options);
     // build group optgroup
     $group_optgroup = [];
     foreach ($groups as $group) {
         // can questions be asked in this group
         if (!questions_can_ask_question($group)) {
             continue;
         }
         $selected = ['value' => $group->getGUID()];
         if ($group->getGUID() == $question->getContainerGUID()) {
             $selected['selected'] = true;
         }
         $group_optgroup[] = elgg_format_element('option', $selected, $group->name);
     }
     if (!empty($group_optgroup)) {
         $container_options = true;
         $select_options = [];
         // add user to the list
         $selected = ['value' => ''];
         if ($owner->getGUID() == $question->getContainerGUID()) {
             $selected['selected'] = true;
示例#3
0
$adding = !$question->getGUID();
$editing = !$adding;
$moving = false;
if ($editing && !$question->canEdit()) {
    register_error(elgg_echo('actionunauthorized'));
    forward(REFERER);
}
$container_guid = (int) get_input('container_guid');
if (empty($container_guid)) {
    $container_guid = (int) $question->getOwnerGUID();
}
if ($editing && $container_guid != $question->getContainerGUID()) {
    $moving = true;
}
$container = get_entity($container_guid);
if ($adding && !questions_can_ask_question($container)) {
    register_error(elgg_echo('questions:action:question:save:error:container'));
    forward(REFERER);
}
if (questions_limited_to_groups() && $container_guid == $question->getOwnerGUID()) {
    register_error(elgg_echo('questions:action:question:save:error:limited_to_groups'));
    forward(REFERER);
}
$title = get_input('title');
$description = get_input('description');
$tags = string_to_tag_array(get_input('tags', ''));
$access_id = (int) get_input('access_id');
$comments_enabled = get_input('comments_enabled');
if (empty($container_guid) || empty($title)) {
    register_error(elgg_echo('questions:action:question:save:error:body', [$container_guid, $title]));
    forward(REFERER);