Can someone suggest me how to add an activity chooser pop up on page load.
Thank you in advance,
Tulasi.
Yeah, is there a js call we can make? We can inject a call. We just don't know what invokes the modal window.
We need the activity chooser to display without any interaction from the user because they are returning from an action that we want them to immediately add an activity or resource.
That dialog is specifically tailored for a course main page and depends on it's structure. If you want to use this dialog in custom page created by you it is not going to be simple.
The code also depends on the version of moodle you are planning to use.
For eaxmple for Moodle 2.9 the code that renders that in course frontpage is located
Javascript is located in moodle-course-modchooser YUI based plugin located here:
https://github.com/moodle/moodle/tree/MOODLE_29_STABLE/course/yui/src/modchooser
In Moodlee 3.5 on the other hand the code is still there but it is diferently structured
And
https://github.com/moodle/moodle/blob/MOODLE_35_STABLE/course/classes/output/modchooser.php
So if we are talking about custom page starting point might look like this:
require(__DIR__.'/config.php');
require_once($CFG->dirroot.'/course/lib.php');
require_login();
$PAGE->set_url('/path/to/myscript.php');
$PAGE->set_context(context_system::instance());
$PAGE->set_title('title');
$PAGE->set_heading('title');
/** @var core_renderer $OUTPUT */
$OUTPUT;
$course = get_course(somecourseid);
$modules = get_module_metadata($course, get_module_types_names());
/** @var core_course_renderer $courserenderer */
$courserenderer = $PAGE->get_renderer('core_course');
echo $OUTPUT->header();
//TODO: add whatever else is needed to make this work.
echo $courserenderer->course_modchooser($modules, $course);
echo $OUTPUT->footer();
Just to clarify, we aren't on a custom page. We are on the course page with editing turned on. We just want the activity chooser to already be displayed.
That is why it's always good to clarify things in the first place.
In case you want to activate the activity selector on some course page approach is different.
You would need to:
create custom plugin (block or local)
In that plugin's lib.php file implement hook for extending navigation. In case of local plugin called local_jstest it might look like this:
function local_jstest_extend_navigation($nav) { ($nav); global $PAGE, $ME; if (preg_match('#^/course/view\.php\?.*?#iu', $ME) === 1) { $PAGE->requires->js_amd_inline( " require(['jquery'], function() { var element = $('li#section-1 .section-modchooser-link.a'); if (element) { element.get(0).click(); } }); " ); } }
This should activate the link for section-1 in the course. You would of course have to determine the actual CSS selector etc. by expanding on this code. But it is a start. If the succession of events is too fast put a delay to this code.
Hi Darko Miletić,
I tried the above code, but it works on console as expected but not on page load. Thank you for your response.
Given that example was untested and just given as guide example here is the tested version that does work. It was required to add timeout to ensure previous javascript initializes correctly.
function local_jstest_extend_navigation($nav) { global $PAGE, $ME; if (preg_match('#^/course/view\.php\?.*?#iu', $ME) === 1) { $PAGE->requires->js_amd_inline( " require(['jquery'], function($) { setTimeout(function() { var element = $('li#section-1 span.section-modchooser-link > a'); if (element.length > 0) { element.get(0).click(); } }, 1000); }); " ); } }
This is hardcoded for section 1 within a course. To use different section change li#section-1
to whatever section you want to use.
Thank you Darko Miletić for walking me through even though I didn't share the code for reference. It worked yay!