Skip to content

Commit

Permalink
fix: displaying AUs based on launch methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Talha-Rizwan committed Jan 1, 2024
1 parent 39eaf13 commit ef0b2a2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 49 deletions.
25 changes: 12 additions & 13 deletions openedx_cmi5_xblock/openedx_cmi5_xblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,6 @@ class CMI5XBlock(XBlock, CompletableXBlockMixin):
scope=Scope.content,
)

launch_methods = List(
display_name=_('Launch Methods'),
help=_('List of launch methods of AU URLs'),
default=[],
scope=Scope.content,
)

current_au_index = Integer(
display_name=_('Current AU Index'),
help=_('Index of the currently displayed AU URL'),
Expand Down Expand Up @@ -200,7 +193,6 @@ def student_view(self, context=None):
'popup_width': self.width or 800,
'popup_height': self.height or 800,
'au_urls': self.au_urls,
'launch_methods': self.launch_methods,
'current_au_index': self.current_au_index,
'total_au_count': self.total_au_count,
},
Expand Down Expand Up @@ -536,12 +528,19 @@ def update_package_fields(self):

au_elements = root.findall('.//{prefix}au'.format(prefix=prefix))
if au_elements:
au_urls = [au.find('./{prefix}url'.format(prefix=prefix)).text for au in au_elements]
launch_methods = [au.get('launchMethod', 'AnyWindow') for au in au_elements]
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': self.launch_au_url(au_url),
'launch_method': launch_method
}
au_data_list.append(au_data)

self.index_page_path = au_urls[0]
self.au_urls = [self.launch_au_url(url) for url in au_urls]
self.launch_methods = launch_methods
self.index_page_path = au_data_list[0]['url']
self.au_urls = au_data_list
self.total_au_count = len(self.au_urls)
else:
self.index_page_path = self.find_relative_file_path('index.html')
Expand Down
10 changes: 5 additions & 5 deletions openedx_cmi5_xblock/static/html/openedx_cmi5_xblock.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
<div class="openedx_cmi5_xblock_block">
<h3 class="xblock-title mb-2">{{title}}</h3>
<br>
<h5 class="xblock-title mb-2">Available Assignables</h5>

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

<div class="cmi5-content">
<iframe
class="cmi5-embedded"
Expand All @@ -20,8 +22,6 @@ <h5 class="xblock-title mb-2">Available Assignables</h5>
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
49 changes: 18 additions & 31 deletions openedx_cmi5_xblock/static/js/src/openedx_cmi5_xblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,35 @@ function CMI5XBlock(runtime, element, settings) {
/* Here's where you'd do things on page load. */
var nextButton = $('#nextButton');
var prevButton = $('#prevButton');
console.log("the launch_methods are : ", settings.launch_methods)
console.log("the au_urls are : ", settings.au_urls)

prevButton.prop('disabled', true)

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

nextButton.click(function () {

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 () {

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]);
});

$('ol a').click(function (event) {
event.preventDefault(); // Prevent the default behavior of the link
var href = $(this).attr('href'); // Get the href attribute of the clicked link
updateIframeSrc(href); // Update the iframe src with the href value
event.preventDefault();
var href = $(this).attr('href');
updateIframeSrc(href);
});

function updateIframeSrc(src) {
$('.cmi5-embedded').attr('src', src);
function updateIframeSrc(href) {
var index = settings.au_urls.findIndex(function (au) {
return au.url === href;
});

if (index !== -1) {
if (settings.au_urls[index].launch_method === 'OwnWindow') {
// Open in a new browser window
window.open(href, '_blank');
} else {
// Display in the iframe
$('.cmi5-embedded').attr('src', href);
}
}
}
});
}

0 comments on commit ef0b2a2

Please sign in to comment.