Skip to content

Commit

Permalink
feat: added Multi AUs support for iframe (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Talha-Rizwan authored Jan 15, 2024
1 parent 800bb42 commit c052aa7
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 32 deletions.
92 changes: 68 additions & 24 deletions openedx_cmi5_xblock/openedx_cmi5_xblock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Openedx CMI5 XBlock implementation."""

import copy
import hashlib
import json
import logging
Expand All @@ -21,7 +22,7 @@
from webob import Response
from xblock.completable import CompletableXBlockMixin
from xblock.core import XBlock
from xblock.fields import Boolean, DateTime, Dict, Float, Integer, Scope, String
from xblock.fields import Boolean, DateTime, Dict, Float, Integer, List, Scope, String
from xblock.fragment import Fragment

from openedx_cmi5_xblock.utils.utility import (
Expand Down Expand Up @@ -121,6 +122,13 @@ class CMI5XBlock(XBlock, CompletableXBlockMixin):
scope=Scope.settings,
)

au_urls = List(
display_name=_('AU URLs'),
help=_('List of AU URLs'),
default=[],
scope=Scope.content,
)

has_author_view = True

def author_view(self, context=None):
Expand Down Expand Up @@ -159,6 +167,7 @@ def student_view(self, context=None):
student_context = {
'title': self.display_name,
'index_page_url': self.index_page_url,
'au_urls': self.launch_all_au_urls,
'cmi5_xblock': self,
}
student_context.update(context or {})
Expand Down Expand Up @@ -260,6 +269,49 @@ def studio_submit(self, request, _suffix):
response['errors'].append(e.args[0])
return json_response(response)

@property
def launch_all_au_urls(self):
"""Gets the URLs of all the cmi5 AUs."""
temp_au_list = copy.deepcopy(self.au_urls)

for au in temp_au_list:
au['url'] = self.launch_au_url(au['url'])

return temp_au_list

def launch_au_url(self, url):
"""Handles the multiple AUs and append launch params with url."""
if not self.package_meta or not url:
return ''

lms_cmi5_url = self.make_launch_url(url)
return lms_cmi5_url

@property
def index_page_url(self):
"""
Gets the URL of the CMI5 index page.
Returns an empty string if the package metadata or index page path is not available.
"""
if not self.package_meta or not self.index_page_path:
return ''

lms_cmi5_url = self.make_launch_url(self.index_page_path)
return lms_cmi5_url

def make_launch_url(self, url):
"""Make the launch url for the AUs of cmi5."""
if is_url(url):
lms_cmi5_url = url
else:
folder = self.extract_folder_path
lms_cmi5_url = requests.utils.unquote(self.storage.url(os.path.join(folder, url)))

params_joining_symbol = '&' if is_params_exist(lms_cmi5_url) else '?'
lms_cmi5_url = lms_cmi5_url + params_joining_symbol
return lms_cmi5_url + self.get_launch_url_params()

# getters and setters
def get_credentials(self):
"""
Expand Down Expand Up @@ -377,26 +429,6 @@ def get_launch_url_params(self):

return all_parameters

@property
def index_page_url(self):
"""
Gets the URL of the CMI5 index page.
Returns an empty string if the package metadata or index page path is not available.
"""
if not self.package_meta or not self.index_page_path:
return ''

if is_url(self.index_page_path):
lms_cmi5_url = self.index_page_path
else:
folder = self.extract_folder_path
lms_cmi5_url = requests.utils.unquote(self.storage.url(os.path.join(folder, self.index_page_path)))

params_joining_symbol = '&' if is_params_exist(lms_cmi5_url) else '?'
lms_cmi5_url = lms_cmi5_url + params_joining_symbol
return lms_cmi5_url + self.get_launch_url_params()

@property
def extract_folder_path(self):
"""
Expand Down Expand Up @@ -486,9 +518,21 @@ def update_package_fields(self):
prefix = '{' + namespace + '}' if namespace else ''
self.set_course_detail(prefix, root)

au_url = root.find('.//{prefix}au/{prefix}url'.format(prefix=prefix))
if au_url is not None:
self.index_page_path = au_url.text
au_elements = root.findall('.//{prefix}au'.format(prefix=prefix))
if au_elements:
self.index_page_path = au_elements[0].find('./{prefix}url'.format(prefix=prefix)).text
au_data_list = []
for au in au_elements:
au_url = au.find('./{prefix}url'.format(prefix=prefix)).text
launch_method = au.get('launchMethod', 'AnyWindow')

au_data = {
'url': au_url,
'launch_method': launch_method
}
au_data_list.append(au_data)

self.au_urls = au_data_list
else:
self.index_page_path = self.find_relative_file_path('index.html')

Expand Down
17 changes: 16 additions & 1 deletion openedx_cmi5_xblock/static/html/openedx_cmi5_xblock.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@

<div class="openedx_cmi5_xblock_block">
<h3 class="xblock-title mb-2">{{title}}</h3>
<br>

{% if index_page_url %}
<h5 class="xblock-title mb-2"> {% trans "Available Assignables:" %}</h5>
<ol>
{% for au_url in au_urls %}
<li>
<a href="{{ au_url.url }}" target="_blank">
<span>{% trans "AU No." %}</span> {{ forloop.counter }}
</a>
({%trans "Launch: "%} {{au_url.launch_method}})
</li>

{% endfor %}
</ol>

<div class="cmi5-content">
<iframe
class="cmi5-embedded"
src="{{ index_page_url }}"
src=""
width="{% if cmi5_xblock.width %}{{ cmi5_xblock.width }}%{% else %}100%{% endif %}"
height="{% if cmi5_xblock.height %}{{ cmi5_xblock.height }}{% else %}450{% endif %}"
>
Expand Down
29 changes: 22 additions & 7 deletions openedx_cmi5_xblock/static/js/src/openedx_cmi5_xblock.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
/* Javascript for CMI5XBlock. */
function CMI5XBlock(runtime, element) {

$(function($) {
/*
Use `gettext` provided by django-statici18n for static translations
$(function ($) {
/*
Use `gettext` provided by django-statici18n for static translations
var gettext = CMI5XBlocki18n.gettext;
*/

var gettext = CMI5XBlocki18n.gettext;
*/
/* Here's where you'd do things on page load. */

/* Here's where you'd do things on page load. */
$('ol a').click(function(event) {
event.preventDefault();
var href = $(this).attr('href');
var liText = $(this).closest('li').text().trim();
updateIframeSrc(href, liText);

});

function updateIframeSrc(href, liText) {
if (liText.includes('AnyWindow')) {
$('.cmi5-embedded').attr('src', href);
} else if (liText.includes('OwnWindow')) {
window.open(href, '_blank');
}
}
});
}
}

0 comments on commit c052aa7

Please sign in to comment.