causal / doodle_client
一个小巧轻量级的库,用于与 Doodle (http://doodle.com) 交互
0.5.0
2017-11-30 10:37 UTC
Requires
- php: ^7.0
Requires (Dev)
- phpunit/phpunit: ~4.0
README
此库提供了 Doodle (https://doodle.com) 的一个缺失功能:一个 API,用于程序化访问和创建 Doodle 平台上的投票。
我发现只有一个基本的 API 可以 启动 投票(实际上并没有创建它们,只是预填充表单),但没有其他任何东西可以获取投票列表或相关参与者的答案,所以我联系了他们,并在 2015 年 9 月 25 日得到了以下答复
遗憾的是,Doodle 将不再提供 API。
-- Katharina
因此我写了这个 PHP 客户端。
基本用法
$doodleUsername = 'me@example.com';
$doodlePassword = 'my-very-secret-password';
$client = new \Causal\DoodleClient\Client($doodleUsername, $doodlePassword);
$client->connect();
$myPolls = $client->getPersonalPolls();
echo '<h1>My polls</h1>';
echo '<ul>';
foreach ($myPolls as $poll) {
echo '<li>';
echo '<a href="' . htmlspecialchars($poll->getPublicUrl()) . '">' . htmlspecialchars($poll->getTitle()) . '</a>';
echo '<blockquote>' . nl2br($poll->getDescription()) . '</blockquote>';
echo 'Export answers as: ' .
'<a href="' . htmlspecialchars($poll->getExportExcelUrl()) . '">Excel</a> | ' .
'<a href="' . htmlspecialchars($poll->getExportPdfUrl()) . '">PDF</a>';
echo '</li>';
}
echo '</ul>';
// Optional, if you want to prevent actually authenticating over and over again
// with future requests (thus reusing the local authentication cookies)
$client->disconnect();
创建投票(文本选项)
$newPoll = $client->createPoll([
'type' => 'text',
'title' => 'Dinner',
'location' => 'Restaurant Xtra',
'description' => 'I suggest we meet and have a nice time together',
'name' => 'John Doo',
'email' => 'john.doo@example.com',
'options' => [
'Lasagna',
'Pizza',
'Meat',
],
]);
echo 'link to new poll: ' . $newPoll->getPublicUrl();
创建投票(日期)
$newPoll = $client->createPoll([
'type' => 'date',
'title' => 'Dinner',
'location' => 'Restaurant Xtra',
'description' => 'I suggest we meet and have a nice time together',
'name' => 'John Doo',
'email' => 'john.doo@example.com',
'dates' => [
'20150929' => ['1930', '2000'],
'20150930' => ['2000'],
'20151030' => ['1945', '2000'],
],
]);
echo 'link to new poll: ' . $newPoll->getPublicUrl();
邀请参与者
// Selection of a given poll could be based on any "$poll" from the
// foreach loop in "Basic Usage" example or of course "$newPoll".
$emailAdresses = [
'someone@example.tld',
'someone-else@gmail.com',
];
$message = 'Hey there! Please check this doodle!';
$client->inviteParticipants($poll, $emailAddresses, $message);
删除投票
// Selection of a given poll could be based on any "$poll" from the
// foreach loop in "Basic Usage" example.
$client->deletePoll($poll);
答案表格
另一个用法示例,可以是获取特定投票的答案。
// Selection of a given poll could be based on any "$poll" from the
// foreach loop in "Basic Usage" example.
echo '<table>';
echo '<thead>';
echo '<tr>';
echo '<th></th>';
$options = $poll->getOptions();
foreach ($options as $option) {
echo '<th>' . htmlspecialchars($option) . '</th>';
}
echo '</tr>';
echo '</thead>';
echo '<tbody>';
$participants = $poll->getParticipants();
foreach ($participants as $participant) {
echo '<tr>';
echo '<td>' . htmlspecialchars($participant->getName()) . '</td>';
foreach ($participant->getPreferences() as $preference) {
switch ($preference) {
case '0':
$value = 'NO';
$color = '#ffccca';
break;
case '1':
$value = 'YES';
$color = '#d1f3d1';
break;
case '2':
$value = 'If needed';
$color = '#ffeda1';
break;
default:
$value = '?';
$color = '#eaeaea';
break;
}
echo '<td style="background-color:' . $color . '">' . htmlspecialchars($value) . '</td>';
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';