MM2 - How to enable addon for individual courses

MM2 - How to enable addon for individual courses

by Zoran Jeremic -
Number of replies: 4

Hi,

I'm implementing addon for Moodle Mobile 2 and I registered it using $mmCoursesDelegate.registerPlugin, so it's available from the list of the courses. It works fine, but I need to enable/disable it for the individual courses. If teacher did not activate my plugin on the regular Moodle web application for his course, it should not be available for that course in Mobile application too. I implemented external service that receives courseid and returns information if this plugin should be enabled or not for that course. However, I'm not sure how to implement this on the front end.

Thanks,

Zoran




Average of ratings: -
In reply to Zoran Jeremic

Re: MM2 - How to enable addon for individual courses

by Frédéric Massart ⭐ -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Zoran,

You should be able to selectively disable a plugin by defining the method 'isEnabledForCourse()'. See

https://github.com/moodlehq/moodlemobile2/blob/master/www/core/components/courses/services/delegate.js#L35

Cheers,

Fred

In reply to Frédéric Massart ⭐

Re: MM2 - How to enable addon for individual courses

by Zoran Jeremic -

And how can I pass course id when I register plugin from main.js?

I registered plugin in this way:

.run(function($mmUserDelegate,$mmaMorphHandlers, $mmCoursesDelegate, $mmaMorph,mmaMorphPriority) {
    $mmCoursesDelegate.registerPlugin('mmaMorph', function() {
      if ($mmaMorph.isPluginMorphEnabled() && $mmaMorph.isEnabledForCourse()) {
            return {
                icon: 'ion-ios-list',
                state: 'site.morph-mainsubmenu',
                title: 'mma.morph.morphtitle'
            };
      }
    }, mmaMorphPriority);

This code registers plugin on "My Courses" page. It's executed only once, so I guess this is not the proper place where I should embed this call. Then I have the following factory. I tried to provide courseid as the function parameter, but I don't know how to pass it's value properly.

angular.module('mm.addons.morph')
.factory('$mmaMorph', function($mmSite, $mmCourse, $stateParams, $log, $q, $mmUser) {
$log = $log.getInstance('$mmaMorph');
var self = {};
self.isPluginMorphEnabled = function() {
var infos;
return true;
};
self.isEnabledForCourse = function() {
return true;
};
return self;
});






In reply to Zoran Jeremic

Re: MM2 - How to enable addon for individual courses

by Dani Palou -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hello Zoran,

as you say, the function passed to $mmCoursesDelegate.registerPlugin is not the place to check that, in there you only need to check if the plugin is available for the current site. This function is executed when a user logs in a site or the site data is updated.

I see you have implemented the isEnabledForCourse function inside $mmaMorph, as the delegate doc says. This function is going to be called for each course, and the courseId is passed as a parameter:

https://github.com/moodlehq/moodlemobile2/blob/master/www/core/components/courses/services/delegate.js#L80

So you don't need to manually call that function, it will be automatically called.

Hope this helps!

Dani

Average of ratings: Useful (1)
In reply to Dani Palou

Re: MM2 - How to enable addon for individual courses

by Zoran Jeremic -

Hi,

Thanks for explanation. It seems that support for this has been implemented recently, and I was using moodlemobile2 code which was 3-4 weeks old which didn't have that trigger. Once I updated to the recent code everything started to work fine.


Thanks,

Zoran