Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for poll and survey xblocks #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions examples/course.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,58 @@ <h5>
</li>
</ul>
</section>
<h4>
Survey
</h4>
<section data-mu-type="survey">
<h5 data-max_submissions="10" data-private_results="true" data-xblock-family="xblock.v1">
My Survey
</h5>
<p>
Question 1
</p>
<p>
Question 2
</p>
<p>
Question 3
</p>
<ul>
<li>
Answer 1
</li>
<li>
Answer 2
</li>
<li>
Answer 3
</li>
</ul>
<code>
This is your feedback text after submitting the survey.
</code>
</section>
<section data-mu-type="poll">
<h5 data-max_submissions="10" data-private_results="true" data-xblock-family="xblock.v1">
My Poll
</h5>
<p>
Can you see this poll example?
</p>
<ul>
<li>
Yes
</li>
<li>
No
</li>
<li>
Maybe
</li>
</ul>
<code>
This is your feedback text after submitting the poll.
</code>
</section>
</body>
</html>
34 changes: 34 additions & 0 deletions examples/course.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,37 @@ How many legs does a healthy snake have?

:::

#### Survey

::: {mu-type=survey}

#### My Survey {private_results=true max_submissions=10}

Question 1

Question 2

Question 3

* Answer 1
* Answer 2
* Answer 3

`This is your feedback text after submitting the survey.`

:::

::: {mu-type=poll}

#### My Poll {private_results=true max_submissions=10}

Can you see this poll example?

* Yes
* No
* Maybe

`This is your feedback text after submitting the poll.`

:::

Binary file modified examples/olx.tar.gz
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/olx/problem/f2310074b275914a6e36bca277682a0a.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
</additional_answer>
<additional_answer answer="0">
</additional_answer>
<textline size="4">
</textline>
</stringresponse>
</problem>
2 changes: 2 additions & 0 deletions examples/olx/sequential/5e20663dadd1e483ac628951dd582ea8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
</vertical>
<vertical url_name="936721dab1ecfa97632df0cfccd801f4">
</vertical>
<vertical url_name="21ab14cf7e8b30af0299c634785a5782">
</vertical>
</sequential>
69 changes: 69 additions & 0 deletions examples/olx/vertical/21ab14cf7e8b30af0299c634785a5782.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<vertical display_name="Survey">
<survey answers='[
[
"answer_1",
"Answer 1"
],
[
"answer_2",
"Answer 2"
],
[
"answer_3",
"Answer 3"
]
]' block_name="My Survey" feedback="This is your feedback text after submitting the survey." max_submissions="10" private_results="true" questions='[
[
"question_1",
{
"img": "",
"img_alt": "",
"label": "Question 1"
}
],
[
"question_2",
{
"img": "",
"img_alt": "",
"label": "Question 2"
}
],
[
"question_3",
{
"img": "",
"img_alt": "",
"label": "Question 3"
}
]
]' url_name="c00c90a1289c6f97a797d7f8a8b2df7e" xblock-family="xblock.v1">
</survey>
<poll answers='[
[
"yes",
{
"img": "",
"img_alt": "",
"label": "Yes"
}
],
[
"no",
{
"img": "",
"img_alt": "",
"label": "No"
}
],
[
"maybe",
{
"img": "",
"img_alt": "",
"label": "Maybe"
}
]
]' display_name="My Poll" feedback="This is your feedback text after submitting the poll." max_submissions="10" private_results="true" question="Can you see this poll example?" url_name="3ff7105da79eabe8a9adf96e98a0d6d7" xblock-family="xblock.v1">
</poll>
</vertical>
6 changes: 6 additions & 0 deletions mu/formats/base/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ def on_video(self, unit: units.Video) -> None:

def on_rawhtml(self, unit: units.RawHtml) -> None:
pass

def on_poll(self, unit: units.Poll) -> None:
pass

def on_survey(self, unit: units.Survey) -> None:
pass
61 changes: 61 additions & 0 deletions mu/formats/html/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ def on_section(self, unit_html: BeautifulSoup) -> t.Iterable[units.Unit]:
elif unit_type == "ftq":
# Free text question
yield from process_ftq(unit_html)
elif unit_type == "survey":
# Survey questionaire
yield from process_survey(unit_html)
elif unit_type == "poll":
# Poll question
yield from process_poll(unit_html)
else:
logger.warning("Unit type is unsupported by HTML reader: %s", unit_type)

Expand Down Expand Up @@ -231,6 +237,36 @@ def process_ftq(unit_html: BeautifulSoup) -> t.Iterable[units.Unit]:
yield units.FreeTextQuestion(title=title, question=question, answers=answers)


def process_survey(unit_html: BeautifulSoup) -> t.Iterable[units.Unit]:
title, questions, answers = get_questions_answers(unit_html)
feedback = unit_html.find("code").string.strip() if unit_html.find("code") else ""
yield units.Survey(
title=title,
questions=questions,
answers=answers,
feedback=feedback,
attributes={
attr.replace("data-", "", 1): val
for attr, val in unit_html.find(re.compile("^h[1-6]$")).attrs.items()
},
)


def process_poll(unit_html: BeautifulSoup) -> t.Iterable[units.Unit]:
title, question, answers = get_question_answers(unit_html)
feedback = unit_html.find("code").string.strip() if unit_html.find("code") else ""
yield units.Poll(
title=title,
question=question,
answers=answers,
feedback=feedback,
attributes={
attr.replace("data-", "", 1): val
for attr, val in unit_html.find(re.compile("^h[1-6]$")).attrs.items()
},
)


def process_video(unit_html: BeautifulSoup) -> t.Iterable[units.Unit]:
"""
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
Expand Down Expand Up @@ -288,6 +324,31 @@ def get_question_answers(unit_html: BeautifulSoup) -> t.Tuple[str, str, t.List[s
return title, question, answers


def get_questions_answers(
unit_html: BeautifulSoup,
) -> t.Tuple[str, t.List[str], t.List[str]]:
title = ""
if title_html := find_title(unit_html):
title = title_html.string

answers = []

ul_html = unit_html.find("ul")
if ul_html is None:
raise MuError(f"Missing <ul> element in multiple choice question: {unit_html}")
for li_html in ul_html.find_all("li"):
answer = li_html.string.strip()
answers.append(answer.strip())

questions = []
for q in unit_html.findAll("p"):
if "code" not in [c.name for c in q.children]:
questions.append(q.string.strip())
if len(questions) < 1:
raise MuError(f"Missing <p> element in survey: {unit_html}")
return title, questions, answers


def find_title(unit_html: BeautifulSoup) -> t.Optional[BeautifulSoup]:
return unit_html.find(
[
Expand Down
55 changes: 55 additions & 0 deletions mu/formats/html/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,61 @@ def on_freetextquestion(self, unit: units.FreeTextQuestion) -> None:

self.append_to_body(section_html)

def on_survey(self, unit: units.Survey) -> None:
section_html = Tag(name="section", attrs={TYPE_ATTR: "survey"})

# Write title
section_html.append(self.get_header(unit))

# Write questions
for question in unit.questions:
question_html = Tag(name="p")
question_html.string = question
section_html.append(question_html)

answers_html = Tag(name="ul")

# Write answers
for answer in unit.answers:
answer_html = Tag(name="li")
answer_html.string = answer
answers_html.append(answer_html)
section_html.append(answers_html)

# Write Feedback
feedback_html = Tag(name="code")
feedback_html.string = unit.feedback
section_html.append(feedback_html)

self.append_to_body(section_html)

def on_poll(self, unit: units.Poll) -> None:
section_html = Tag(name="section", attrs={TYPE_ATTR: "poll"})

# Write title
section_html.append(self.get_header(unit))

# Write question
question_html = Tag(name="p")
question_html.string = unit.question
section_html.append(question_html)

answers_html = Tag(name="ul")

# Write answers
for answer in unit.answers:
answer_html = Tag(name="li")
answer_html.string = answer
answers_html.append(answer_html)
section_html.append(answers_html)

# Write Feedback
feedback_html = Tag(name="code")
feedback_html.string = unit.feedback
section_html.append(feedback_html)

self.append_to_body(section_html)

def on_video(self, unit: units.Video) -> None:
"""
We parse the video sources. If one is youtube, we include a youtube iframe.
Expand Down
Loading