|
| 1 | +"""pdfXBlock main Python class.""" |
| 2 | + |
| 3 | +from django.utils.translation import gettext_noop as _ |
| 4 | +from xblock.core import XBlock |
| 5 | +from xblock.fields import Boolean, Scope, String |
| 6 | +from xblock.fragment import Fragment |
| 7 | +from xblock.utils.resources import ResourceLoader |
| 8 | + |
| 9 | +from .utils import bool_from_str, is_all_download_disabled |
| 10 | + |
| 11 | +resource_loader = ResourceLoader(__name__) |
| 12 | + |
| 13 | + |
| 14 | +@XBlock.needs('i18n') |
| 15 | +class PDFBlock(XBlock): |
| 16 | + """PDF XBlock. Allows authors to embed PDFs in their courses.""" |
| 17 | + |
| 18 | + icon_class = "other" |
| 19 | + |
| 20 | + display_name = String( |
| 21 | + display_name=_("Display Name"), |
| 22 | + default=_("PDF"), |
| 23 | + scope=Scope.settings, |
| 24 | + help=_("This name appears in the horizontal navigation at the top of the page.") |
| 25 | + ) |
| 26 | + |
| 27 | + url = String( |
| 28 | + display_name=_("PDF URL"), |
| 29 | + default=_("https://tutorial.math.lamar.edu/pdf/Trig_Cheat_Sheet.pdf"), |
| 30 | + scope=Scope.content, |
| 31 | + help=_("The URL for your PDF.") |
| 32 | + ) |
| 33 | + |
| 34 | + allow_download = Boolean( |
| 35 | + display_name=_("PDF Download Allowed"), |
| 36 | + default=True, |
| 37 | + scope=Scope.content, |
| 38 | + help=_("Display a download button for this PDF.") |
| 39 | + ) |
| 40 | + |
| 41 | + source_text = String( |
| 42 | + display_name=_("Source document button text"), |
| 43 | + default="", |
| 44 | + scope=Scope.content, |
| 45 | + help=_( |
| 46 | + "Add a download link for the source file of your PDF. " |
| 47 | + "Use it for example to provide the PowerPoint file used to create this PDF." |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + source_url = String( |
| 52 | + display_name=_("Source document URL"), |
| 53 | + default="", |
| 54 | + scope=Scope.content, |
| 55 | + help=_( |
| 56 | + "Add a download link for the source file of your PDF. " |
| 57 | + "Use it for example to provide the PowerPoint file used to create this PDF." |
| 58 | + ) |
| 59 | + ) |
| 60 | + |
| 61 | + def student_view(self, context=None): |
| 62 | + """Primary view of the XBlock, shown to students when viewing courses.""" |
| 63 | + context = { |
| 64 | + 'display_name': self.display_name, |
| 65 | + 'url': self.url, |
| 66 | + 'allow_download': self.allow_download, |
| 67 | + 'disable_all_download': is_all_download_disabled(), |
| 68 | + 'source_text': self.source_text, |
| 69 | + 'source_url': self.source_url, |
| 70 | + } |
| 71 | + html = resource_loader.render_django_template( |
| 72 | + 'templates/html/pdf_view.html', |
| 73 | + context=context, |
| 74 | + i18n_service=self.runtime.service(self, "i18n"), |
| 75 | + ) |
| 76 | + |
| 77 | + event_type = 'edx.pdf.loaded' |
| 78 | + event_data = { |
| 79 | + 'url': self.url, |
| 80 | + 'source_url': self.source_url, |
| 81 | + } |
| 82 | + self.runtime.publish(self, event_type, event_data) |
| 83 | + frag = Fragment(html) |
| 84 | + frag.add_javascript(resource_loader.load_unicode("static/js/pdf_view.js")) |
| 85 | + frag.initialize_js('pdfXBlockInitView') |
| 86 | + return frag |
| 87 | + |
| 88 | + def studio_view(self, context=None): |
| 89 | + """ |
| 90 | + Secondary view of the XBlock. |
| 91 | +
|
| 92 | + Shown to teachers when editing the XBlock. |
| 93 | + """ |
| 94 | + context = { |
| 95 | + 'display_name': self.display_name, |
| 96 | + 'url': self.url, |
| 97 | + 'allow_download': self.allow_download, |
| 98 | + 'disable_all_download': is_all_download_disabled(), |
| 99 | + 'source_text': self.source_text, |
| 100 | + 'source_url': self.source_url |
| 101 | + } |
| 102 | + html = resource_loader.render_django_template( |
| 103 | + 'templates/html/pdf_edit.html', |
| 104 | + context=context, |
| 105 | + i18n_service=self.runtime.service(self, "i18n"), |
| 106 | + ) |
| 107 | + frag = Fragment(html) |
| 108 | + frag.add_javascript(resource_loader.load_unicode("static/js/pdf_edit.js")) |
| 109 | + frag.initialize_js('pdfXBlockInitEdit') |
| 110 | + return frag |
| 111 | + |
| 112 | + @XBlock.json_handler |
| 113 | + def on_download(self, data, suffix=''): # pylint: disable=unused-argument |
| 114 | + """Download file event handler.""" |
| 115 | + event_type = 'edx.pdf.downloaded' |
| 116 | + event_data = { |
| 117 | + 'url': self.url, |
| 118 | + 'source_url': self.source_url, |
| 119 | + } |
| 120 | + self.runtime.publish(self, event_type, event_data) |
| 121 | + |
| 122 | + @XBlock.json_handler |
| 123 | + def save_pdf(self, data, suffix=''): # pylint: disable=unused-argument |
| 124 | + """Save handler.""" |
| 125 | + self.display_name = data['display_name'] |
| 126 | + self.url = data['url'] |
| 127 | + |
| 128 | + if not is_all_download_disabled(): |
| 129 | + self.allow_download = bool_from_str(data['allow_download']) |
| 130 | + self.source_text = data['source_text'] |
| 131 | + self.source_url = data['source_url'] |
| 132 | + |
| 133 | + return { |
| 134 | + 'result': 'success', |
| 135 | + } |
0 commit comments