Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit da01e526e4e8f9ce81f71c77519b06ae6b21f127 @chadsmith committed Jul 2, 2010
Showing with 292 additions and 0 deletions.
  1. 0 README
  2. +8 −0 applets/poll/applet.json
  3. BIN applets/poll/icon.png
  4. +11 −0 applets/poll/script.js
  5. +21 −0 applets/poll/twiml.php
  6. +47 −0 applets/poll/ui.php
  7. +18 −0 db.sql
  8. +12 −0 plugin.json
  9. +34 −0 polls.js
  10. +141 −0 polls.php
0 README
No changes.
@@ -0,0 +1,8 @@
+{
+ "name" : "Poll",
+ "sms_name" : "Poll",
+ "voice_title" : "Poll",
+ "sms_title" : "Poll",
+ "description" : "Add a poll response.",
+ "type" : ["sms", "voice"]
+}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,11 @@
+$(function(){
+ $('.vbx-polls select[name=poll]').live('change',function(){
+ var $select=$(this);
+ var $options=$('select[name=option]', $select.parents('.vbx-polls'));
+ $options.empty();
+ $.getJSON('../../p/polls',{poll:$select.val()},function(options,i){
+ for(i=0; i<options.length; i++)
+ $options.append($('<option>').attr({value:i}).text(options[i]));
+ });
+ });
+})
@@ -0,0 +1,21 @@
+<?php
+$ci =& get_instance();
+$poll = AppletInstance::getValue('poll');
+$option = AppletInstance::getValue('option');
+$number = 'voice' == AppletInstance::getFlowType() ? normalize_phone_to_E164($_REQUEST['Caller']) : normalize_phone_to_E164($_REQUEST['From']);
+
+$ci->db->delete('polls_responses', array('poll' => $poll, 'value' => $number));
+$ci->db->insert('polls_responses', array(
+ 'poll' => $poll,
+ 'value' => $number,
+ 'response' => $option,
+ 'time' => time()
+));
+
+$response = new Response();
+
+$next = AppletInstance::getDropZoneUrl('next');
+if(!empty($next))
+ $response->addRedirect($next);
+
+$response->Respond();
@@ -0,0 +1,47 @@
+<?php
+ $user = OpenVBX::getCurrentUser();
+ $tenant_id = $user->values['tenant_id'];
+ $ci =& get_instance();
+ $queries = explode(';', file_get_contents(dirname(dirname(dirname(__FILE__))).'/db.sql'));
+ foreach($queries as $query)
+ if(trim($query))
+ $ci->db->query($query);
+ $polls = $ci->db->query(sprintf('SELECT id, name FROM polls WHERE tenant=%d', $tenant_id))->result();
+ $poll = AppletInstance::getValue('poll');
+ $poll = $poll ? $poll : count($polls) ? $polls[0]->id : null;
+ $options = json_decode($ci->db->query(sprintf('SELECT data FROM polls WHERE tenant=%d AND id=%d', $tenant_id, $poll))->row()->data);
+ $option = AppletInstance::getValue('option');
+?>
+<div class="vbx-applet vbx-polls">
+<?php if(count($polls)): ?>
+ <div class="vbx-full-pane">
+ <h3>Poll</h3>
+ <fieldset class="vbx-input-container">
+ <select class="medium" name="poll">
+<?php foreach($polls as $p): ?>
+ <option value="<?php echo $p->id; ?>"<?php echo $poll==$p->id?' selected="selected" ':''; ?>><?php echo $p->name; ?></option>
+<?php endforeach; ?>
+ </select>
+ </fieldset>
+ <h3>Selection</h3>
+ <fieldset class="vbx-input-container">
+ <p>
+ <select class="medium" name="option">
+<?php if(count($options)) foreach($options as $i => $name): ?>
+ <option value="<?php echo $i; ?>"<?php echo $option == $i?' selected="selected"':''; ?>><?php echo $name; ?></option>
+<?php endforeach; ?>
+ </select>
+ </p>
+ </fieldset>
+ </div>
+ <h2>Next</h2>
+ <p>After recording the response, continue to the next applet</p>
+ <div class="vbx-full-pane">
+ <?php echo AppletUI::DropZone('next'); ?>
+ </div><!-- .vbx-full-pane -->
+<?php else: ?>
+ <div class="vbx-full-pane">
+ <h3>You need to create a poll first.</h3>
+ </div>
+<?php endif; ?>
+</div><!-- .vbx-applet -->
18 db.sql
@@ -0,0 +1,18 @@
+CREATE TABLE IF NOT EXISTS `polls` (
+ `id` int(11) NOT NULL AUTO_INCREMENT,
+ `tenant` bigint(20) NOT NULL,
+ `name` varchar(100) NOT NULL,
+ `data` text,
+ PRIMARY KEY (`id`),
+ KEY `tenant` (`tenant`,`name`)
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
+
+CREATE TABLE IF NOT EXISTS `polls_responses` (
+ `id` int(11) NOT NULL AUTO_INCREMENT,
+ `poll` int(11) NOT NULL,
+ `value` varchar(15) NOT NULL,
+ `response` int(11) NOT NULL,
+ `time` int(11) NOT NULL,
+ PRIMARY KEY (`id`),
+ KEY `poll` (`poll`,`value`,`response`,`time`)
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
@@ -0,0 +1,12 @@
+{
+ "name" : "Polls",
+ "author" : "Chad Smith <chad@nospam.me>",
+ "description" : "Allows for phone and text message polling.",
+ "url" : "http://twitter.com/chadsmith",
+ "links" : [{
+ "menu" : "Polls",
+ "url" : "polls",
+ "script" : "polls.php",
+ "label" : "Manage Polls"
+ }]
+}
@@ -0,0 +1,34 @@
+$(function(){
+ $('#button-add-poll').click(function(e) {
+ $('.vbx-polls form:not(:first):visible,.vbx-polls .option').slideUp();
+ $('.vbx-polls form:first').slideToggle();
+ return false;
+ });
+ $('.vbx-polls form button').eq(0).click(function(){
+ var $label=$('label.option');
+ $label.last().after($('<div>').append($label.first().clone()).html());
+ return false;
+ });
+ $('.vbx-polls a.options').click(function(){
+ var $poll=$(this).parent().parent().parent();
+ var id=$poll.attr('id');
+ $('.vbx-polls .option:not(.'+id+'):visible,.vbx-polls form').slideUp();
+ $('.vbx-polls .option.'+id).slideToggle();
+ return false;
+ });
+ $('.vbx-polls .poll a.delete').click(function(){
+ var $poll=$(this).parent().parent().parent();
+ var id=$poll.attr('id');
+ if(confirm('You are about to delete "'+$poll.children().children('span').eq(0).text()+'" and all its responses.'))
+ $.ajax({
+ type:'POST',
+ url:window.location,
+ data:{remove:id.match(/([\d]+)/)[1]},
+ success:function(){
+ $poll.add('.vbx-polls .option.'+id).hide(500);
+ },
+ dataType:'text'
+ });
+ return false
+ });
+})
141 polls.php
@@ -0,0 +1,141 @@
+<?php
+ $user = OpenVBX::getCurrentUser();
+ $tenant_id = $user->values['tenant_id'];
+ $ci =& get_instance();
+ $queries = explode(';', file_get_contents(dirname(__FILE__).'/db.sql'));
+ foreach($queries as $query)
+ if(trim($query))
+ $ci->db->query($query);
+ if($remove = intval($_POST['remove'])){
+ $ci->db->delete('polls', array('id' => $remove, 'tenant' => $tenant_id));
+ if($ci->db->affected_rows())
+ $ci->db->delete('polls_responses', array('poll' => $remove));
+ die();
+ }
+ if($poll = intval($_REQUEST['poll'])){
+ echo $ci->db->query(sprintf('SELECT data FROM polls WHERE tenant=%d AND id=%d', $tenant_id, $poll))->row()->data;
+ die();
+ }
+ if(($name = htmlentities($_POST['name']))&&($options = $_POST['option'])&&is_array($options)){
+ foreach($options as &$option)
+ $option = htmlentities($option);
+ $ci->db->insert('polls', array(
+ 'tenant' => $tenant_id,
+ 'name' => $name,
+ 'data' => json_encode($options)
+ ));
+ }
+ $polls = $ci->db->query(sprintf('SELECT id, name, data, (SELECT COUNT(id) FROM polls_responses WHERE polls_responses.poll=polls.id) AS responses FROM polls WHERE tenant=%d', $tenant_id))->result();
+ OpenVBX::addJS('polls.js');
+?>
+<style>
+ .vbx-polls h3 {
+ font-size:16px;
+ font-weight:bold;
+ margin-top:0;
+ }
+ .vbx-polls .poll,
+ .vbx-polls div.option {
+ clear:both;
+ width:95%;
+ overflow:hidden;
+ margin:0 auto;
+ padding:5px 0;
+ border-bottom:1px solid #eee;
+ list-style:disc;
+ }
+ .vbx-polls div.option {
+ display:none;
+ background:#ccc;
+ }
+ .vbx-polls .poll span {
+ display:inline-block;
+ width:25%;
+ text-align:center;
+ float:left;
+ vertical-align:middle;
+ line-height:24px;
+ }
+ .vbx-polls .option span {
+ display:inline-block;
+ width:25%;
+ text-align:center;
+ float:left;
+ vertical-align:middle;
+ line-height:24px;
+ }
+ .vbx-polls .poll a {
+ text-decoration:none;
+ color:#111;
+ }
+ .vbx-polls form {
+ display:none;
+ padding:20px 5%;
+ background:#eee;
+ border-bottom:1px solid #ccc;
+ }
+ .vbx-polls a.delete {
+ display:inline-block;
+ height:24px;
+ width:24px;
+ text-indent:-999em;
+ background:transparent url(/assets/i/action-icons-sprite.png) no-repeat -68px 0;
+ }
+</style>
+<div class="vbx-content-main">
+ <div class="vbx-content-menu vbx-content-menu-top">
+ <h2 class="vbx-content-heading">Polls</h2>
+ <ul class="vbx-menu-items-right">
+ <li class="menu-item"><button id="button-add-poll" class="inline-button add-button"><span>Add Poll</span></button></li>
+ </ul>
+ </div><!-- .vbx-content-menu -->
+ <div class="vbx-table-section vbx-polls">
+ <form method="post" action="">
+ <h3>Add Poll</h3>
+ <fieldset class="vbx-input-container">
+ <label class="field-label">Poll Name
+ <input type="text" class="medium" name="name" />
+ </label>
+ <label class="field-label option">Option
+ <input type="text" class="medium" name="option[]" />
+ </label>
+ <p>
+ <button type="submit" class="inline-button submit-button"><span>Add Option</span></button>
+ <button type="submit" class="inline-button submit-button"><span>Save</span></button>
+ </p>
+ </fieldset>
+ </form>
+<?php if(count($polls)): ?>
+ <div class="poll">
+ <h3>
+ <span>Name</span>
+ <span>Options</span>
+ <span>Responses</span>
+ <span>Delete</span>
+ </h3>
+ </div>
+<?php foreach($polls as $poll):
+ $options = json_decode($poll->data);
+ $responses = $ci->db->query(sprintf('SELECT COUNT(id) AS num FROM polls_responses WHERE polls_responses.poll=%d GROUP BY response ORDER BY response', $poll->id))->result(); ?>
+ <div class="poll" id="poll_<?php echo $poll->id; ?>">
+ <p>
+ <span><?php echo $poll->name; ?></span>
+ <span><a href="" class="options"><?php echo count($options); ?></a></span>
+ <span><a href="" class="options"><?php echo $poll->responses; ?></a></span>
+ <span><a href="" class="delete">X</a></span>
+ </p>
+ </div>
+<?php foreach($options as $i => $option): ?>
+ <div class="option poll_<?php echo $poll->id; ?>">
+ <p>
+ <span><?php echo $option; ?></span>
+ <span>&nbsp;</span>
+ <span><?php echo $ci->db->query(sprintf('SELECT COUNT(id) AS num FROM polls_responses WHERE poll=%d AND response=%d', $poll->id, $i))->row()->num; ?></span>
+ <span>&nbsp;</span>
+ </p>
+ </div>
+<?php endforeach; ?>
+<?php endforeach; ?>
+<?php endif; ?>
+ </div>
+</div>

0 comments on commit da01e52

Please sign in to comment.