Ajax request failed error

Forums: 

May you all live in peace!

I am a Moodle developer. Currently, I have installed H5P plugin on one of the Moodle installations and am trying to customize my Summary slide. So, within my Moodle theme, I have added overridden functions to run custom JS code. Below is the code I used to override summary slide in my theme renderer:

public function hvp_alter_scripts(&$scripts, $libraries, $embedType) {
global $CFG;
if (
isset($libraries['H5P.CoursePresentation']) &&
$libraries['H5P.CoursePresentation']['majorVersion'] == '1'
) {
$include_file = 'customSum.js';
$scripts[] = (object)array(
'path' => $CFG->wwwroot . '/theme/academi/js/' . $include_file,
'version' => '?ver=0.0.1',
);
}
}

The issue is that when I add this code, the course presentation loads fine without error
BUT
when I go to edit the H5P activity, it give ajax error shown in attached image.
Any thoughts would be highly regarded.
Warm regards,
Mahtab Hussain
Attachments: 
icc's picture

That is strange and should not happen.
Could you open the network tab and have a look at the failing request? What does the response look like? 

May you live in peace!

Thanks for responding. Please find attached network tab's screenshot. I do not find any script failing there. Thanks again for your help. Any pointers?

Warm regards,

Mahtab Hussain

Attachments: 
icc's picture

Try to filter on 'XHR' in the top bar of the network tab, this should make the request easier to spot. It's probably the last request visible.

Sorry for late response. I did not receive email notification for your update. I just logged in and found your post. I have filtered using XHR and result is attached. Sorry but I could not find any of these 4 failing. Can you please point out a solution?

Attachments: 
icc's picture

If you switch over to the 'Console' tab do you still see the error message? 

Yes. The error which I shared earlier is still there on console tab.

Just in case, this is helpful, I am adding the code of JS file being called.

(function ($) {
// Replace Show solutions button with feedback.
var dashboardURL = 'http://192.168.10.107/oolaga/my';
var passPercent = 70;
var feedbackPass = 'Bien joué. Vous avez réussi la formation.';
var feedbackFail = 'Pardon. Tu ne pouvais pas réussir. Veuillez réessayer.';
var obtMarks = parseInt(document.getElementsByClassName('h5p-joubelui-score-number-counter')[0].innerText);
var maxMarks = parseInt(document.getElementsByClassName('h5p-joubelui-score-max')[0].innerText);
var percentMarks = (obtMarks / maxMarks) * 100;
if (percentMarks < passPercent) {
document.getElementsByClassName('h5p-show-solutions')[0].outerHTML = '<p class="h5p-show-solutions">' + feedbackFail + '</p>';
} else {
document.getElementsByClassName('h5p-show-solutions')[0].outerHTML = '<p class="h5p-show-solutions">' + feedbackPass + '</p>';
//Replace Retry button with Exit Training button
document.getElementsByClassName('h5p-cp-retry-button')[0].outerHTML = '<a class="h5p-exit-training-button h5p-joubelui-button" href="' + dashboardURL + '">Sortie</a>';
}

})(H5P.jQuery);
icc's picture

If the error message is still there then there is a request failing in the Network tab. You need to find that request if you're going to figure out what's failing.

Just in case this is helpful for someone else. Following code did the work for me: The code checks that the page is a course presentation which is not in edit mode.

if (
isset($libraries['H5P.CoursePresentation']) &&
!isset($libraries['H5PEditor.CoursePresentation'])
) {
$include_file = 'customSum.js';
$scripts[] = (object)array(
'path' => $CFG->wwwroot . '/theme/academi/js/' . $include_file,
'version' => '?ver=0.0.1',
);
}
BV52's picture

Hi Mahtab,

Thank you for the information this will help those in the community that encounters a similar issue.

-BV52