Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
X
xblock-poll
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
xblock-poll
Commits
1042076a
Commit
1042076a
authored
Jan 01, 2015
by
Jonathan Piacenti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Vote functionality implemented for Survey.
parent
17f35612
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
86 additions
and
51 deletions
+86
-51
poll/poll.py
+0
-0
poll/public/handlebars/poll_results.handlebars
+1
-1
poll/public/handlebars/survey_results.handlebars
+20
-0
poll/public/html/survey.html
+1
-1
poll/public/js/poll.js
+64
-49
No files found.
poll/poll.py
View file @
1042076a
This diff is collapsed.
Click to expand it.
poll/public/handlebars/poll_results.handlebars
View file @
1042076a
...
@@ -21,7 +21,7 @@
...
@@ -21,7 +21,7 @@
<
/div
>
<
/div
>
<
/div
>
<
/div
>
<
div
class
=
"poll-percent-container"
>
<
div
class
=
"poll-percent-container"
>
<
span
class
=
"poll-percent-display
{{#if
top
}}
poll-top-choice
{{/if}}
"
>
{{
percent
}}
%<
/span
>
<
span
class
=
"poll-percent-display
{{#if
first
}}
poll-top-choice
{{/if}}
"
>
{{
percent
}}
%<
/span
>
<
/div
>
<
/div
>
<
/li
>
<
/li
>
{{^
last
}}
{{^
last
}}
...
...
poll/public/handlebars/survey_results.handlebars
0 → 100644
View file @
1042076a
<script
id=
"survey-results-template"
type=
"text/html"
>
<
table
>
<
thead
>
<
tr
>
<
th
><
/th
>
{{#
each
answers
}}
<
th
>
{{
this
}}
<
/th
>
{{/
each
}}
<
/tr
>
<
/thead
>
{{#
each
tally
}}
<
tr
>
<
td
>
{{{
text
}}}
<
/td
>
{{#
each
answers
}}
<
td
>
{{
percent
}}
%<
/td
>
{{/
each
}}
<
/tr
>
{{/
each
}}
<
/table
>
</script>
poll/public/html/survey.html
View file @
1042076a
...
@@ -12,7 +12,7 @@
...
@@ -12,7 +12,7 @@
{% endfor %}
{% endfor %}
</tr>
</tr>
</thead>
</thead>
{% for key, question in questions
.items
%}
{% for key, question in questions %}
<tr>
<tr>
<td>
<td>
{{question}}
{{question}}
...
...
poll/public/js/poll.js
View file @
1042076a
...
@@ -10,21 +10,18 @@ function PollUtil (runtime, element, pollType) {
...
@@ -10,21 +10,18 @@ function PollUtil (runtime, element, pollType) {
this
.
runtime
=
runtime
;
this
.
runtime
=
runtime
;
this
.
submit
=
$
(
'input[type=button]'
,
element
);
this
.
submit
=
$
(
'input[type=button]'
,
element
);
this
.
answers
=
$
(
'input[type=radio]'
,
element
);
this
.
answers
=
$
(
'input[type=radio]'
,
element
);
this
.
resultsTemplate
=
Handlebars
.
compile
(
$
(
"#poll-results-template"
,
element
).
html
());
this
.
resultsTemplate
=
Handlebars
.
compile
(
$
(
"#"
+
self
.
pollType
+
"-results-template"
,
element
).
html
());
var
getResults
=
this
.
getResults
();
// If the submit button doesn't exist, the user has already
// If the submit button doesn't exist, the user has already
// selected a choice. Render results instead of initializing machinery.
// selected a choice. Render results instead of initializing machinery.
if
(
!
self
.
submit
.
length
)
{
if
(
!
self
.
submit
.
length
)
{
getResults
({
'success'
:
true
});
self
.
getResults
({
'success'
:
true
});
return
false
;
return
false
;
}
}
return
true
;
return
true
;
};
};
this
.
pollInit
=
function
(){
this
.
pollInit
=
function
(){
var
self
=
this
;
// Initialization function for PollBlocks.
// Initialization function for PollBlocks.
var
enableSubmit
=
self
.
enableSubmit
();
var
radio
=
$
(
'input[name=choice]:checked'
,
self
.
element
);
var
radio
=
$
(
'input[name=choice]:checked'
,
self
.
element
);
self
.
submit
.
click
(
function
()
{
self
.
submit
.
click
(
function
()
{
...
@@ -35,78 +32,96 @@ function PollUtil (runtime, element, pollType) {
...
@@ -35,78 +32,96 @@ function PollUtil (runtime, element, pollType) {
type
:
"POST"
,
type
:
"POST"
,
url
:
self
.
voteUrl
,
url
:
self
.
voteUrl
,
data
:
JSON
.
stringify
({
"choice"
:
choice
}),
data
:
JSON
.
stringify
({
"choice"
:
choice
}),
success
:
self
.
getResults
()
success
:
self
.
getResults
});
});
});
});
// If the user has refreshed the page, they may still have an answer
// If the user has refreshed the page, they may still have an answer
// selected and the submit button should be enabled.
// selected and the submit button should be enabled.
var
answers
=
$
(
'input[type=radio]'
,
self
.
element
);
var
answers
=
$
(
'input[type=radio]'
,
self
.
element
);
if
(
!
radio
.
val
())
{
if
(
!
radio
.
val
())
{
answers
.
bind
(
"change.enableSubmit"
,
enableSubmit
);
answers
.
bind
(
"change.enableSubmit"
,
self
.
enableSubmit
);
}
else
{
}
else
{
enableSubmit
();
self
.
enableSubmit
();
}
}
};
};
this
.
surveyInit
=
function
()
{
this
.
surveyInit
=
function
()
{
var
self
=
this
;
// Initialization function for Survey Blocks
// Initialization function for Survey Blocks
var
verifyAll
=
self
.
verifyAll
();
self
.
answers
.
bind
(
"change.enableSubmit"
,
self
.
verifyAll
);
self
.
answers
.
bind
(
"change.enableSubmit"
,
verifyAll
)
self
.
submit
.
click
(
function
()
{
$
.
ajax
({
type
:
"POST"
,
url
:
self
.
voteUrl
,
data
:
JSON
.
stringify
(
self
.
surveyChoices
()),
success
:
self
.
getResults
})
});
// If the user has refreshed the page, they may still have an answer
// selected and the submit button should be enabled.
self
.
verifyAll
();
};
this
.
surveyChoices
=
function
()
{
// Grabs all selections for survey answers, and returns a mapping for them.
var
choices
=
{};
self
.
answers
.
each
(
function
(
index
,
el
)
{
el
=
$
(
el
);
choices
[
el
.
prop
(
'name'
)]
=
$
(
self
.
checkedElement
(
el
)).
val
();
});
return
choices
;
};
this
.
checkedElement
=
function
(
el
)
{
// Given the DOM element of a radio, get the selector for the checked element
// with the same name.
return
"input[name='"
+
el
.
prop
(
'name'
)
+
"']:checked"
};
};
this
.
verifyAll
=
function
()
{
this
.
verifyAll
=
function
()
{
// Generates a function that will verify all questions have an answer selected.
// Verify that all questions have an answer selected.
var
self
=
this
;
var
doEnable
=
true
;
var
enableSubmit
=
self
.
enableSubmit
();
self
.
answers
.
each
(
function
(
index
,
el
)
{
return
function
()
{
if
(
!
$
(
self
.
checkedElement
(
$
(
el
)),
self
.
element
).
length
)
{
var
doEnable
=
true
;
doEnable
=
false
;
self
.
answers
.
each
(
function
(
index
,
el
)
{
return
false
if
(
!
$
(
"input[name='"
+
$
(
el
).
prop
(
'name'
)
+
"']:checked"
,
self
.
element
).
length
)
{
doEnable
=
false
;
return
false
}
});
if
(
doEnable
){
enableSubmit
();
}
}
});
if
(
doEnable
){
self
.
enableSubmit
();
}
}
};
};
this
.
getResults
=
function
()
{
this
.
getResults
=
function
(
data
)
{
// Generates a function that will grab and display results.
// Fetch the results from the server and render them.
var
self
=
this
;
if
(
!
data
[
'success'
])
{
return
function
(
data
)
{
alert
(
data
[
'errors'
].
join
(
'
\
n'
));
if
(
!
data
[
'success'
])
{
alert
(
data
[
'errors'
].
join
(
'
\
n'
));
}
$
.
ajax
({
// Semantically, this would be better as GET, but we can use helper
// functions with POST.
type
:
"POST"
,
url
:
self
.
tallyURL
,
data
:
JSON
.
stringify
({}),
success
:
function
(
data
)
{
console
.
log
(
self
);
$
(
'div.poll-block'
,
self
.
element
).
html
(
self
.
resultsTemplate
(
data
));
}
})
}
}
$
.
ajax
({
// Semantically, this would be better as GET, but we can use helper
// functions with POST.
type
:
"POST"
,
url
:
self
.
tallyURL
,
data
:
JSON
.
stringify
({}),
success
:
function
(
data
)
{
console
.
log
(
self
);
$
(
'div.poll-block'
,
self
.
element
).
html
(
self
.
resultsTemplate
(
data
));
}
})
};
};
this
.
enableSubmit
=
function
()
{
this
.
enableSubmit
=
function
()
{
// Generates a function which will enable the submit button.
// Enable the submit button.
var
self
=
this
;
self
.
submit
.
removeAttr
(
"disabled"
);
return
function
()
{
self
.
answers
.
unbind
(
"change.enableSubmit"
);
self
.
submit
.
removeAttr
(
"disabled"
);
self
.
answers
.
unbind
(
"change.enableSubmit"
);
}
};
};
this
.
pollType
=
pollType
;
var
run_init
=
this
.
init
(
runtime
,
element
);
var
run_init
=
this
.
init
(
runtime
,
element
);
if
(
run_init
)
{
if
(
run_init
)
{
var
init_map
=
{
'poll'
:
self
.
pollInit
,
'survey'
:
self
.
surveyInit
};
var
init_map
=
{
'poll'
:
self
.
pollInit
,
'survey'
:
self
.
surveyInit
};
init_map
[
pollType
]
.
call
(
self
)
init_map
[
pollType
]
(
)
}
}
}
}
function
PollBlock
(
runtime
,
element
)
{
function
PollBlock
(
runtime
,
element
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment