Add an activity chooser pop up on page load

Add an activity chooser pop up on page load

by tulasi atikamsetti -
Number of replies: 8

Can someone suggest me how to add an activity chooser pop up on page load.

Thank you in advance,

Tulasi.

Average of ratings: -
In reply to tulasi atikamsetti

Re: Add an activity chooser pop up on page load

by Rex Lorenzo -

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.

In reply to tulasi atikamsetti

Re: Add an activity chooser pop up on page load

by Darko Miletić -

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

here https://github.com/moodle/moodle/blob/382d80646b7efbcae6f80a1b78ebdad78f5fd24e/course/renderer.php#L174

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

https://github.com/moodle/moodle/blob/d70651f787e96bc02f697886d9a360177a30cc84/course/renderer.php#L195

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






In reply to Darko Miletić

Re: Add an activity chooser pop up on page load

by Rex Lorenzo -

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.

In reply to Rex Lorenzo

Re: Add an activity chooser pop up on page load

by Darko Miletić -

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.

In reply to Darko Miletić

Re: Add an activity chooser pop up on page load

by tulasi atikamsetti -

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. 


In reply to tulasi atikamsetti

Re: Add an activity chooser pop up on page load

by Darko Miletić -
You have to do better than just say "it does not work". Please upload the

code in question on GitHub and pass the link



El jue., 11 abr. 2019 4:49 p. m., tulasi atikamsetti (via Moodle.org) <
In reply to Darko Miletić

Re: Add an activity chooser pop up on page load

by Darko Miletić -

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.

Average of ratings: Useful (1)