Skip to content

Commit

Permalink
feat: added Multi AUs support for iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
Talha-Rizwan committed Dec 27, 2023
1 parent 800bb42 commit d934531
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
52 changes: 48 additions & 4 deletions openedx_cmi5_xblock/openedx_cmi5_xblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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, Scope, String, List
from xblock.fragment import Fragment

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

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

current_au_index = Integer(
display_name=_('Current AU Index'),
help=_('Index of the currently displayed AU URL'),
default=0,
scope=Scope.user_state,
)

total_au_count = Integer(
display_name=_('Total AU Count'),
help=_('Total number of AU URLs'),
default=0,
scope=Scope.content,
)

has_author_view = True

def author_view(self, context=None):
Expand Down Expand Up @@ -171,9 +192,13 @@ def student_view(self, context=None):
'CMI5XBlock', json_args={
'popup_width': self.width or 800,
'popup_height': self.height or 800,
'au_urls': self.au_urls,
'current_au_index': self.current_au_index,
'total_au_count': self.total_au_count,
},
)
return frag


@XBlock.handler
def lrs_endpoint(self, request, _suffix):
Expand Down Expand Up @@ -260,6 +285,22 @@ def studio_submit(self, request, _suffix):
response['errors'].append(e.args[0])
return json_response(response)

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 = ''
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 + self.get_launch_url_params()
return lms_cmi5_url

# getters and setters
def get_credentials(self):
"""
Expand Down Expand Up @@ -397,6 +438,7 @@ def index_page_url(self):
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 +528,11 @@ 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_urls = root.findall('.//{prefix}au/{prefix}url'.format(prefix=prefix))
if au_urls is not None:
self.index_page_path = au_urls[0].text
self.au_urls = [self.launch_au_url(au_url.text) for au_url in au_urls]
self.total_au_count = len(self.au_urls)
else:
self.index_page_path = self.find_relative_file_path('index.html')

Expand Down
2 changes: 2 additions & 0 deletions openedx_cmi5_xblock/static/html/openedx_cmi5_xblock.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ <h3 class="xblock-title mb-2">{{title}}</h3>
height="{% if cmi5_xblock.height %}{{ cmi5_xblock.height }}{% else %}450{% endif %}"
>
</iframe>
<button id="nextButton">Next Assessment</button>
<button id="prevButton">Previous Assessment</button>
</div>
{% elif message %}
<p>{{ message }}</p>
Expand Down
52 changes: 51 additions & 1 deletion openedx_cmi5_xblock/static/js/src/openedx_cmi5_xblock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Javascript for CMI5XBlock. */
function CMI5XBlock(runtime, element) {
function CMI5XBlock(runtime, element, settings) {


$(function ($) {
Expand All @@ -10,5 +10,55 @@ function CMI5XBlock(runtime, element) {
*/

/* Here's where you'd do things on page load. */
var nextButton = $('#nextButton');
var prevButton = $('#prevButton');
prevButton.prop('disabled', true)
if(settings.total_au_count === 1){
nextButton.prop('disabled', true)
}

nextButton.click(function () {

console.log("the au_urls are : ", settings.au_urls)
console.log("total au count : ", settings.total_au_count)
console.log("current au index: ", settings.current_au_index)



if(settings.current_au_index < settings.total_au_count-1){
settings.current_au_index+=1
prevButton.prop('disabled', false)

if(settings.current_au_index === settings.total_au_count-1){
nextButton.prop('disabled', true)
}

}


$('.cmi5-embedded').attr('src', settings.au_urls[settings.current_au_index]);

});

prevButton.click(function () {

console.log("the au_urls are : ", settings.au_urls)
console.log("total au count : ", settings.total_au_count)
console.log("current au index: ", settings.current_au_index)

if(settings.current_au_index > 0){
settings.current_au_index-=1
nextButton.prop('disabled', false)

if(settings.current_au_index === 0){
prevButton.prop('disabled', true);
}
}


$('.cmi5-embedded').attr('src', settings.au_urls[settings.current_au_index]);

});

});
}

0 comments on commit d934531

Please sign in to comment.