more polls dev

This commit is contained in:
zotlabs 2020-01-27 15:54:43 -08:00
parent 8572cf1650
commit 69f9cfeb64
4 changed files with 209 additions and 4 deletions

View file

@ -1752,15 +1752,81 @@ class Activity {
} }
static function update_poll($act,$content) {
$multi = false;
$r = q("select * from item where mid = '%s' and verb = 'Create' and item_wall = 1",
dbesc($act->obj['id'])
);
if (! $r) {
return false;
}
$item = array_shift($r);
$o = json_decode($item['obj'],true);
if($o && array_key_exists('anyOf',$o)) {
$multi = true;
}
$answer_found = false;
$found = false;
if ($multi) {
foreach($o['anyOf'] as $a) {
if($a['name'] === $content['name'] ) {
$answer_found = true;
foreach($o['anyOf']['replies'] as $reply) {
if($reply['id'] === $act->id) {
$found = true;
}
}
}
}
if (! $found) {
$o['anyOf']['replies']['totalItems'] ++;
$o['anyOf']['replies']['items'][] = [ 'id' => $act->id, 'type' => 'Note' ];
$update = true;
}
}
else {
foreach($o['oneOf'] as $a) {
if($a['name'] === $content['name'] ) {
$answer_found = false;
foreach($o['oneOf']['replies'] as $reply) {
if($reply['id'] === $act->id) {
$found = true;
}
}
}
}
if (! $found) {
$o['oneOf']['replies']['totalItems'] ++;
$o['oneOf']['replies']['items'][] = [ 'id' => $act->id, 'type' => 'Note' ];
}
}
if ($answer_found && ! $found) {
$x = q("update item set obj = '%s', updated = '%s' where id = %d",
dbesc(json_encode($o)),
dbesc(datetime_convert()),
intval($item['id'])
);
Master::Summon( [ 'Notifier', 'wall-new', $item['id'] ] );
return true;
}
return false;
}
static function decode_note($act) { static function decode_note($act) {
$response_activity = false; $response_activity = false;
$poll_handled = false;
$s = []; $s = [];
if (is_array($act->obj)) { if (is_array($act->obj)) {
$content = self::get_content($act->obj); $content = self::get_content($act->obj);
if($act->type === 'Note' && $act->obj['type'] === 'Question' && $content['name']) {
$poll_handled = self::update_poll($act,$content);
}
} }
$s['owner_xchan'] = $act->actor['id']; $s['owner_xchan'] = $act->actor['id'];
@ -1901,6 +1967,11 @@ class Activity {
$s['verb'] = self::activity_mapper($act->type); $s['verb'] = self::activity_mapper($act->type);
if ($poll_handled) {
$s['verb'] = 'Answer';
}
$s['obj_type'] = self::activity_obj_mapper($act->obj['type']); $s['obj_type'] = self::activity_obj_mapper($act->obj['type']);
$s['obj'] = $act->obj; $s['obj'] = $act->obj;
if (is_array($obj) && array_path_exists('actor/id',$s['obj'])) { if (is_array($obj) && array_path_exists('actor/id',$s['obj'])) {

124
Zotlabs/Module/Vote.php Normal file
View file

@ -0,0 +1,124 @@
<?php
namespace Zotlabs\Module;
use App;
use Zotlabs\Web\Controller;
use Zotlabs\Lib\Activity;
use Zotlabs\Daemon\Master;
use Zotlabs\Lib\Libsync;
class Vote extends Controller {
function init() {
$channel = App::get_channel();
if (! $channel) {
http_status_exit(403, 'Permission denied');
killme();
}
$fetch = null;
$id = argv(1);
$response = $_REQUEST['answer'];
if ($id) {
$fetch = q("select * from item where id = %d limit 1",
intval($id)
);
}
if ($fetch && $fetch[0]['obj_type'] === 'Question') {
$obj = json_decode($fetch[0]['obj'],true);
}
else {
http_status_exit(404, 'not found');
killme();
}
$valid = false;
if ($obj['oneOf']) {
foreach($obj['oneOf'] as $selection) {
logger('selection: ' . $selection);
logger('response: ' . $response);
if($selection['name'] && $selection['name'] === $response) {
$valid = true;
}
}
}
$choices = [];
if ($obj['anyOf']) {
foreach ($obj['oneOf'] as $selection) {
$choices[] = $selection['name'];
}
foreach ($response as $res) {
if (! in_array($res,$choices)) {
$valid = false;
break;
}
$valid = true;
}
}
if (! $valid) {
http_status_exit(400,'Bad request');
}
if (! is_array($response)) {
$response = [ $response ];
}
foreach ($response as $res) {
$item = [];
$item['aid'] = $channel['channel_account_id'];
$item['uid'] = $channel['channel_id'];
$item['item_origin'] = true;
$item['parent'] = $fetch[0]['id'];
$item['parent_mid'] = $fetch[0]['mid'];
$item['uuid'] = new_uuid();
$item['mid'] = z_root() . '/item/' . $item['uuid'];
$item['verb'] = 'Answer';
$item['title'] = $res;
$item['author_xchan'] = $channel['channel_hash'];
$item['owner_xchan'] = $fetch[0]['author_xchan'];
$item['obj'] = $obj;
$item['obj_type'] = 'Question';
$x = item_store($item);
retain_item($fetch[0]['id']);
if($x['success']) {
$itemid = $x['item_id'];
Master::Summon( [ 'Notifier', 'like', $itemid ] );
}
$r = q("select * from item where id = %d",
intval($itemid)
);
if ($r) {
xchan_query($r);
$sync_item = fetch_post_tags($r);
Libsync::build_sync_packet($channel['channel_id'], [ 'item' => [ encode_item($sync_item[0],true) ] ]);
}
}
json_return_and_die([ 'success' => true ]);
}
}

View file

@ -1673,7 +1673,7 @@ function format_poll($item,$s,$opts) {
if ($act['type'] === 'Question') { if ($act['type'] === 'Question') {
if ($activated and $commentable) { if ($activated and $commentable) {
$output .= '<form action="vote/' . $item['id'] . '" method="post" >'; $output .= '<form id="question-form-' . $item['id'] . '" >';
} }
if (array_key_exists('anyOf',$act)) { if (array_key_exists('anyOf',$act)) {
foreach ($act['anyOf'] as $poll) { foreach ($act['anyOf'] as $poll) {
@ -1686,7 +1686,7 @@ function format_poll($item,$s,$opts) {
$total = 0; $total = 0;
} }
if ($activated && $commentable) { if ($activated && $commentable) {
$output .= '<input type="checkbox" name="answer[]" value="' . $text . '"> ' . $text . '</input>' . ' (' . $total . ')' . EOL; $output .= '<input type="checkbox" name="answer[]" value="' . htmlspecialchars($text) . '"> ' . $text . '</input>' . ' (' . $total . ')' . EOL;
} }
else { else {
$output .= '[ ] ' . $text . ' (' . $total . ')' . EOL; $output .= '[ ] ' . $text . ' (' . $total . ')' . EOL;
@ -1714,7 +1714,7 @@ function format_poll($item,$s,$opts) {
} }
} }
if ($activated and $commentable) { if ($activated and $commentable) {
$output .= '<input onclick="this.submit(); return false;" type="submit" name="submit" value="' . t('Submit') . '"></form>'; $output .= '<input type="button" name="vote" value="vote" onclick="submitPoll(' . $item['id'] . '); return false;">'. '</form>';
} }
} }

View file

@ -1322,6 +1322,16 @@ function filestorage(event, nick, id) {
}); });
} }
function submitPoll(id) {
$.post('vote/' + id,
$('#question-form-' + id).serialize(),
function(data) {
});
}
function post_comment(id) { function post_comment(id) {
unpause(); unpause();
commentBusy = true; commentBusy = true;