# Path: mentoring/light_children.py
# class XBlockWithLightChildren(LightChildrenMixin, XBlock):
#     """
#     XBlock base class with support for LightChild
#     """
#     def __init__(self, *args, **kwargs):
#         super(XBlockWithLightChildren, self).__init__(*args, **kwargs)
#         self.xblock_container = self
#         self.load_children_from_xml_content()
# 
#     @XBlock.json_handler
#     def view(self, data, suffix=''):
#         """
#         Current HTML view of the XBlock, for refresh by client
#         """
#         frag = self.student_view({})
#         frag = self.fragment_text_rewriting(frag)
# 
#         return {
#             'html': frag.content,
#         }
# 
#     def fragment_text_rewriting(self, fragment):
#         """
#         Do replacements like `/jump_to_id` URL rewriting in the provided text
#         """
#         # TODO: Why do we need to use `xmodule_runtime` and not `runtime`?
#         try:
#             course_id = self.xmodule_runtime.course_id
#         except AttributeError:
#             # TODO-WORKBENCH-WORKAROUND: To allow to load from the workbench
#             course_id = 'sample-course'
# 
#         try:
#             jump_to_url = reverse('jump_to_id', kwargs={'course_id': course_id, 'module_id': ''})
#         except:
#             # TODO-WORKBENCH-WORKAROUND: To allow to load from the workbench
#             jump_to_url = '/jump_to_id'
# 
#         fragment = replace_jump_to_id_urls(course_id, jump_to_url, self, 'student_view', fragment, {})
#         return fragment
# 
# Path: mentoring/message.py
# class MentoringMessageBlock(LightChild):
#     """
#     A message which can be conditionally displayed at the mentoring block level,
#     for example upon completion of the block
#     """
#     content = String(help="Message to display upon completion", scope=Scope.content, default="")
#     type = String(help="Type of message", scope=Scope.content, default="completed")
#     has_children = True
# 
#     def mentoring_view(self, context=None):
#         fragment, named_children = self.get_children_fragment(context, view_name='mentoring_view')
#         fragment.add_content(render_template('templates/html/message.html', {
#             'self': self,
#             'named_children': named_children,
#         }))
#         return fragment
# 
# Path: mentoring/utils.py
# def get_scenarios_from_path(scenarios_path, include_identifier=False):
#     """
#     Returns an array of (title, xmlcontent) from files contained in a specified directory,
#     formatted as expected for the return value of the workbench_scenarios() method
#     """
#     base_fullpath = os.path.dirname(os.path.realpath(__file__))
#     scenarios_fullpath = os.path.join(base_fullpath, scenarios_path)
# 
#     scenarios = []
#     if os.path.isdir(scenarios_fullpath):
#         for template in os.listdir(scenarios_fullpath):
#             if not template.endswith('.xml'):
#                 continue
#             identifier = template[:-4]
#             title = identifier.replace('_', ' ').title()
#             template_path = os.path.join(scenarios_path, template)
#             if not include_identifier:
#                 scenarios.append((title, load_resource(template_path)))
#             else:
#                 scenarios.append((identifier, title, load_resource(template_path)))
# 
#     return scenarios
# 
# def load_resource(resource_path):
#     """
#     Gets the content of a resource
#     """
#     resource_content = pkg_resources.resource_string(__name__, resource_path)
#     return unicode(resource_content)
# 
# def render_template(template_path, context={}):
#     """
#     Evaluate a template by resource path, applying the provided context
#     """
#     template_str = load_resource(template_path)
#     template = Template(template_str)
#     return template.render(Context(context))


import logging
import uuid
from lxml import etree
from StringIO import StringIO
from xblock.core import XBlock
from xblock.fields import Boolean, Scope, String
from xblock.fragment import Fragment
from .light_children import XBlockWithLightChildren
from .message import MentoringMessageBlock
from .utils import get_scenarios_from_path, load_resource, render_template
# Classes ###########################################################

class MentoringBlock(XBlockWithLightChildren):
    """
    An XBlock providing mentoring capabilities

    Composed of text, answers input fields, and a set of multiple choice quizzes with advices. 
    A set of conditions on the provided answers and quizzes choices will determine if the
    student is a) provided mentoring advices and asked to alter his answer, or b) is given the
    ok to continue.
    """
    attempted = Boolean(help="Has the student attempted this mentoring step?",
                        default=False, scope=Scope.user_state)
    completed = Boolean(help="Has the student completed this mentoring step?",
                        default=False, scope=Scope.user_state)
    next_step = String(help="url_name of the next step the student must complete (global to all blocks)",
                       default='mentoring_first', scope=Scope.preferences)
    followed_by = String(help="url_name of the step after the current mentoring block in workflow",
                         default=None, scope=Scope.content)
    url_name = String(help="Name of the current step, used for URL building",
                      default='mentoring-default', scope=Scope.content)
    enforce_dependency = Boolean(help="Should the next step be the current block to complete?",
                                 default=True, scope=Scope.content, enforce_type=True)
    display_submit = Boolean(help="Allow to submit current block?", default=True, scope=Scope.content, enforce_type=True)
    xml_content = String(help="XML content", default='', scope=Scope.content)
    has_children = True
    icon_class = 'problem'

    def student_view(self, context):
        fragment, named_children = self.get_children_fragment(context, view_name='mentoring_view',

# Path: mentoring/light_children.py
# class XBlockWithLightChildren(LightChildrenMixin, XBlock):
#     """
#     XBlock base class with support for LightChild
#     """
#     def __init__(self, *args, **kwargs):
#         super(XBlockWithLightChildren, self).__init__(*args, **kwargs)
#         self.xblock_container = self
#         self.load_children_from_xml_content()
# 
#     @XBlock.json_handler
#     def view(self, data, suffix=''):
#         """
#         Current HTML view of the XBlock, for refresh by client
#         """
#         frag = self.student_view({})
#         frag = self.fragment_text_rewriting(frag)
# 
#         return {
#             'html': frag.content,
#         }
# 
#     def fragment_text_rewriting(self, fragment):
#         """
#         Do replacements like `/jump_to_id` URL rewriting in the provided text
#         """
#         # TODO: Why do we need to use `xmodule_runtime` and not `runtime`?
#         try:
#             course_id = self.xmodule_runtime.course_id
#         except AttributeError:
#             # TODO-WORKBENCH-WORKAROUND: To allow to load from the workbench
#             course_id = 'sample-course'
# 
#         try:
#             jump_to_url = reverse('jump_to_id', kwargs={'course_id': course_id, 'module_id': ''})
#         except:
#             # TODO-WORKBENCH-WORKAROUND: To allow to load from the workbench
#             jump_to_url = '/jump_to_id'
# 
#         fragment = replace_jump_to_id_urls(course_id, jump_to_url, self, 'student_view', fragment, {})
#         return fragment
# 
# Path: mentoring/message.py
# class MentoringMessageBlock(LightChild):
#     """
#     A message which can be conditionally displayed at the mentoring block level,
#     for example upon completion of the block
#     """
#     content = String(help="Message to display upon completion", scope=Scope.content, default="")
#     type = String(help="Type of message", scope=Scope.content, default="completed")
#     has_children = True
# 
#     def mentoring_view(self, context=None):
#         fragment, named_children = self.get_children_fragment(context, view_name='mentoring_view')
#         fragment.add_content(render_template('templates/html/message.html', {
#             'self': self,
#             'named_children': named_children,
#         }))
#         return fragment
# 
# Path: mentoring/utils.py
# def get_scenarios_from_path(scenarios_path, include_identifier=False):
#     """
#     Returns an array of (title, xmlcontent) from files contained in a specified directory,
#     formatted as expected for the return value of the workbench_scenarios() method
#     """
#     base_fullpath = os.path.dirname(os.path.realpath(__file__))
#     scenarios_fullpath = os.path.join(base_fullpath, scenarios_path)
# 
#     scenarios = []
#     if os.path.isdir(scenarios_fullpath):
#         for template in os.listdir(scenarios_fullpath):
#             if not template.endswith('.xml'):
#                 continue
#             identifier = template[:-4]
#             title = identifier.replace('_', ' ').title()
#             template_path = os.path.join(scenarios_path, template)
#             if not include_identifier:
#                 scenarios.append((title, load_resource(template_path)))
#             else:
#                 scenarios.append((identifier, title, load_resource(template_path)))
# 
#     return scenarios
# 
# def load_resource(resource_path):
#     """
#     Gets the content of a resource
#     """
#     resource_content = pkg_resources.resource_string(__name__, resource_path)
#     return unicode(resource_content)
# 
# def render_template(template_path, context={}):
#     """
#     Evaluate a template by resource path, applying the provided context
#     """
#     template_str = load_resource(template_path)
#     template = Template(template_str)
#     return template.render(Context(context))


# Path: mentoring/mentoring.py
import logging
import uuid
from lxml import etree
from StringIO import StringIO
from xblock.core import XBlock
from xblock.fields import Boolean, Scope, String
from xblock.fragment import Fragment
from .light_children import XBlockWithLightChildren
from .message import MentoringMessageBlock
from .utils import get_scenarios_from_path, load_resource, render_template

# Classes ###########################################################

class MentoringBlock(XBlockWithLightChildren):
    """
    An XBlock providing mentoring capabilities

    Composed of text, answers input fields, and a set of multiple choice quizzes with advices. 
    A set of conditions on the provided answers and quizzes choices will determine if the
    student is a) provided mentoring advices and asked to alter his answer, or b) is given the
    ok to continue.
    """
    attempted = Boolean(help="Has the student attempted this mentoring step?",
                        default=False, scope=Scope.user_state)
    completed = Boolean(help="Has the student completed this mentoring step?",
                        default=False, scope=Scope.user_state)
    next_step = String(help="url_name of the next step the student must complete (global to all blocks)",
                       default='mentoring_first', scope=Scope.preferences)
    followed_by = String(help="url_name of the step after the current mentoring block in workflow",
                         default=None, scope=Scope.content)
    url_name = String(help="Name of the current step, used for URL building",
                      default='mentoring-default', scope=Scope.content)
    enforce_dependency = Boolean(help="Should the next step be the current block to complete?",
                                 default=True, scope=Scope.content, enforce_type=True)
    display_submit = Boolean(help="Allow to submit current block?", default=True, scope=Scope.content, enforce_type=True)
    xml_content = String(help="XML content", default='', scope=Scope.content)
    has_children = True
    icon_class = 'problem'

    def student_view(self, context):
        fragment, named_children = self.get_children_fragment(context, view_name='mentoring_view',

                                                              not_instance_of=MentoringMessageBlock)