3faa320210e73e5d4c0b6d160d880294160cf4b8
[mkwsxb-moved-to-github.git] / mkwsbiblio / mkwsbiblio / mkwsbiblio.py
1 """Embed bibliographic widget from MKWS, the MasterKey Widget Set"""
2
3 import pkg_resources
4 import random
5
6 from xblock.core import XBlock
7 from xblock.fields import Integer, Scope, String, Any, Boolean, Dict
8 from xblock.fragment import Fragment
9
10 class MKWSBiblio(XBlock):
11     """Embed bibliographic widget from MKWS, the MasterKey Widget Set"""
12
13     # Fields
14     query = String(
15       help="Search query",
16       default="water",
17       scope=Scope.content
18     )
19     recid = String(
20       help="Record to display",
21       default="content: title modern water resources engineering",
22       scope=Scope.content
23     )
24     display_name = String(
25       default="MKWS bibliographic details",
26       scope=Scope.settings
27     )
28
29     def resource_string(self, path):
30         """Helper for accessing resources."""
31         data = pkg_resources.resource_string(__name__, path)
32         return data.decode("utf8")
33
34     def student_view(self, context=None):
35         """The primary view of the MKWS XBlock, shown to students when viewing courses."""
36         html = self.resource_string("static/html/student.html")
37         frag = Fragment(html.format(query=self.query, recid=self.recid, team=random.randint(0, 100000)))
38         # student.js uses require.js as it cannot guarantee mkws-complete.js has loaded 
39         # in studio without it. We'll need to add it if we're in the LMS:
40         #frag.add_javascript_url("/static/js/vendor/require.js");
41         frag.add_javascript_url("//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js");
42         # frag.add_resource_url("//mkws.indexdata.com/mkws-complete", "text/javascript", "head");
43         # frag.add_resource('<script src="//mkws.indexdata.com/mkws-complete.js"></script>', "text/html", "head");
44         frag.add_css(self.resource_string("static/css/mkwsbiblio.css"))
45         frag.add_javascript(self.resource_string("static/js/src/student.js"))
46         frag.initialize_js('MKWSBiblio')
47         return frag;
48
49     def author_view(self, context=None):
50         """The primary view of the MKWS XBlock, shown when authoring courses."""
51         # This should closely mirror the student_view. Here all we do is not include
52         # require.js as it's already in Studio and the lms path won't work.
53         html = self.resource_string("static/html/student.html")
54         frag = Fragment(html.format(query=self.query, recid=self.recid, team=random.randint(0, 100000)))
55         frag.add_css(self.resource_string("static/css/mkwsbiblio.css"))
56         frag.add_javascript(self.resource_string("static/js/src/student.js"))
57         frag.initialize_js('MKWSBiblio')
58         return frag;
59
60     def studio_view(self, context=None):
61         """Studio configuration view."""
62         html = self.resource_string("static/html/settings.html")
63         frag = Fragment(html.format(query=self.query, recid=self.recid))
64         frag.add_javascript(self.resource_string("static/js/settings.js"))
65         frag.initialize_js('MKWSBiblioSettings')
66         return frag
67
68     @XBlock.json_handler
69     def update_settings(self, data, suffix=''):
70         """Studio configuration callback."""
71         self.query = data['query']
72         self.recid = data['recid']
73         return {"result": "success"}
74
75     @staticmethod
76     def workbench_scenarios():
77         """A canned scenario for display in the workbench."""
78         return [
79             ("MKWSBiblio",
80              """<vertical_demo>
81                 <mkwsbiblio/>
82                 </vertical_demo>
83              """),
84         ]