# -*- coding: utf-8 -*-
#
# This file is part of INGInious. See the LICENSE and the COPYRIGHTS files for
# more information about the licensing of this file.
""" Displayable problems """
from abc import ABCMeta, abstractmethod
from random import Random
import gettext
import json
from inginious.common.tasks_problems import Problem, CodeProblem, CodeSingleLineProblem, \
MatchProblem, MultipleChoiceProblem, FileProblem, _get_problem_types
from inginious.frontend.parsable_text import ParsableText
[docs]def get_displayable_problem_types(name: str) -> dict:
""" Get the mapping of DisplayableProblem types available by inspecting a given module.
:param name: The name of the module to inspect.
:return: The mapping of problem name and problem class.
"""
raw = _get_problem_types(name, DisplayableProblem)
return {pbl_name: pbl_cls for pbl_name, pbl_cls in raw.items() if pbl_name is not None}
[docs]def get_default_displayable_problem_types() -> dict:
""" Get the mapping of default DisplayableProblem types available by inspecting the current
module.
:return: The mapping of problem name and problem class.
"""
return get_displayable_problem_types(__name__)
[docs]class DisplayableProblem(Problem, metaclass=ABCMeta):
"""Basic problem """
[docs] @classmethod
@abstractmethod
def get_type_name(cls, language):
pass
[docs] @classmethod
@abstractmethod
def show_editbox(cls, template_helper, key, language):
""" get the edit box html for this problem """
pass
[docs] @classmethod
@abstractmethod
def show_editbox_templates(cls, template_helper, key, language):
return ""
[docs]class DisplayableCodeProblem(CodeProblem, DisplayableProblem):
""" A basic class to display all BasicCodeProblem derivatives """
def __init__(self, problemid, content, translations, taskfs):
super(DisplayableCodeProblem, self).__init__(problemid, content, translations, taskfs)
[docs] @classmethod
def get_type_name(cls, language):
return _("code")
[docs] @classmethod
def show_editbox(cls, template_helper, key, language):
return template_helper.render("course_admin/subproblems/code.html", key=key, multiline=True)
[docs] @classmethod
def show_editbox_templates(cls, template_helper, key, language):
return ""
[docs]class DisplayableCodeSingleLineProblem(CodeSingleLineProblem, DisplayableProblem):
""" A displayable single code line problem """
def __init__(self, problemid, content, translations, taskfs):
super(DisplayableCodeSingleLineProblem, self).__init__(problemid, content, translations, taskfs)
[docs] @classmethod
def get_type_name(cls, language):
return _("single-line code")
[docs] @classmethod
def show_editbox(cls, template_helper, key, language):
return template_helper.render("course_admin/subproblems/code.html", key=key, multiline=False)
[docs] @classmethod
def show_editbox_templates(cls, template_helper, key, language):
return ""
[docs]class DisplayableFileProblem(FileProblem, DisplayableProblem):
""" A displayable code problem """
def __init__(self, problemid, content, translations, taskfs):
super(DisplayableFileProblem, self).__init__(problemid, content, translations, taskfs)
[docs] @classmethod
def get_type_name(cls, language):
return _("file upload")
[docs] @classmethod
def show_editbox(cls, template_helper, key, language):
return template_helper.render("course_admin/subproblems/file.html", key=key)
[docs] @classmethod
def show_editbox_templates(cls, template_helper, key, language):
return ""
[docs]class DisplayableMultipleChoiceProblem(MultipleChoiceProblem, DisplayableProblem):
""" A displayable multiple choice problem """
def __init__(self, problemid, content, translations, taskfs):
super(DisplayableMultipleChoiceProblem, self).__init__(problemid, content, translations, taskfs)
[docs] @classmethod
def get_type_name(cls, language):
return _("multiple choice")
[docs] @classmethod
def show_editbox(cls, template_helper, key, language):
return template_helper.render("course_admin/subproblems/multiple_choice.html", key=key)
[docs] @classmethod
def show_editbox_templates(cls, template_helper, key, language):
return template_helper.render("course_admin/subproblems/multiple_choice_templates.html", key=key)
[docs]class DisplayableMatchProblem(MatchProblem, DisplayableProblem):
""" A displayable match problem """
def __init__(self, problemid, content, translations, taskfs):
super(DisplayableMatchProblem, self).__init__(problemid, content, translations, taskfs)
[docs] @classmethod
def get_type_name(cls, language):
return _("match")
[docs] @classmethod
def show_editbox(cls, template_helper, key, language):
return template_helper.render("course_admin/subproblems/match.html", key=key)
[docs] @classmethod
def show_editbox_templates(cls, template_helper, key, language):
return ""