Disabling buttons on TinyMCE Editor

Disabling buttons on TinyMCE Editor

by Joe Stuart -
Number of replies: 2

Dear Fellow Moodlers,


I am new to Moodle Development and I am current working on a use case where I need to enable "Media Button" for courses belonging to one root category and make sure that only course administrator can see the "Media Button". I think I have partially achieved the result by editing the moodle/lib/editor/tinymce/plugins/moodlemedia/lib.php but may be this is not the right way to achieve this. Since I am new to Moodle Development I don't know the best practices, so I would appreciate if anyone could advise me on this.


//Course Category for which the Media Button should be enabled

define('PERMITTED_COURSE_CATEGORY_ID',2);

class tinymce_moodlemedia extends editor_tinymce_plugin {

    /** @var array list of buttons defined by this plugin */

    protected $buttons = array('moodlemedia');

    protected function update_init_params(array &$params, context $context,

            array $options = null) {


global $DB,$PAGE,$COURSE,$USER;


//Current Course Category

$course_category = $COURSE->category;


//List of Valid Role IDs (1 - Manager, 3 - editingTeacher)

$valid_role_ids = array(1,3);


//Get User Role

$context = get_context_instance(CONTEXT_COURSE, $COURSE->id, true);

$roles = get_user_roles($context, $USER->id, true);

$role = key($roles);

$roleid = (int)$roles[$role]->roleid;


/*

* Used this snippet but gives me the following error "Coding error detected, it must be fixed by a programmer: Legacy capabilities can not be used any more!"

$context = get_context_instance(CONTEXT_COURSE,$COURSE->id);


if (has_capability('moodle/legacy:student', $context, $USER->id, false) ) {

echo "is Student<br/>";

}

if (has_capability('moodle/legacy:teacher', $context, $USER->id, false) ) {

echo "is Assitent Teacher<br/>";

}

if (has_capability('moodle/legacy:editingteacher', $context, $USER->id, false)) {

echo "is Teacher<br/>";

}

if (has_capability('moodle/legacy:admin', $context, $USER->id, false)) {

echo "is ADMIN<br/>";

}

*/

 // Add file picker callback.

//Checking whether the current course belongs to Course Category and check user has got the appropriate roles 

if(PERMITTED_COURSE_CATEGORY_ID == $course_category && in_array($roleid,$valid_role_ids))

{

        if (empty($options['legacy'])) {

            if (isset($options['maxfiles']) and $options['maxfiles'] != 0) {

                $params['file_browser_callback'] = "M.editor_tinymce.filepicker";

            }

        }


        if ($row = $this->find_button($params, 'moodleemoticon')) {

            // Add button after 'moodleemoticon' icon.

            $this->add_button_after($params, $row, 'moodlemedia', 'moodleemoticon');

        } else if ($row = $this->find_button($params, 'image')) {

            // Note: We know that the plugin emoticon button has already been added

            // if it is enabled because this plugin has higher sortorder.

            // Otherwise add after 'image'.

            $this->add_button_after($params, $row, 'moodlemedia', 'image');

        } else {

            // Add this button in the end of the first row (by default 'image' button should be in the first row).

            $this->add_button_after($params, 1, 'moodlemedia');

        }


        // Add JS file, which uses default name.

        $this->add_js_plugin($params);

}

    }


    protected function get_sort_order() {

        return 110;

    }

}


Kind Regards,

Joe

Average of ratings: -
In reply to Joe Stuart

Re: Disabling buttons on TinyMCE Editor

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Firstly, it's a bad idea to change core Moodle files, as it causes a lot of conflicts during upgrades.

Far better to create a new TinyMCE plugin to handle this.

The best way to achieve this is a plugin that defines a new capability that can be used to determine who can see the button (it should go in lib/editor/tinymce/plugins/MYPLUGINNAME/db/access.php - look at other examples to see the format).

You should then create a lib.php file in the MYPLUGINNAME directory, with an 'update_init_params' function. The update_init_params function has a 'context' already, so you don't need to use the old get_context_instance functions (if you needed, you could call context_course::instance($courseid), but you don't need to in this case).

Firstly do a 'has_capability('name of the new capability you created', $context)' to check if the user is able to add see the buttons at all. Next, as a site admin, you can override the role definitions at the level of the category, so that only 'course creators' (or is it 'editing teachers') within that specific category (and sub categories and courses) have that capability.

Once you've established the capability, you can then look through the contents of the '$params' variable to remove the 'moodlemedia' button, if it is found in any of the 'theme_advanced_buttonsXX' parts (unless they do have the required capability).

If you makes sure your plugin returns 9999 from 'get_sort_order', then it will run after the moodlemedia plugin, so it can remove the button from the settings, after it has been initialised.

Feel free to ask for more explanation if that isn't enough help.

Average of ratings: Useful (1)
In reply to Davo Smith

Re: Disabling buttons on TinyMCE Editor

by Luka M -
Hi there,

I am totally new to Moodle but have a similar situation. After searching the net, I have found your advice, but I am not sure I fully understand.

As you suggested, I have created my own Plug-in Folder with a db/access.php file and a lib.php file. Is the access.php file supposed to be like a Configuration file or what is it supposed to do? You then write the following:

"Firstly do a 'has_capability('name of the new capability you created', $context)' to check if the user is able to add see the buttons at all. Next, as a site admin, you can override the role definitions at the level of the category, so that only 'course creators' (or is it 'editing teachers') within that specific category (and sub categories and courses) have that capability.

Once you've established the capability, you can then look through the contents of the '$params' variable to remove the 'moodlemedia' button, if it is found in any of the 'theme_advanced_buttonsXX' parts (unless they do have the required capability)."

Could you explain this a little bit more? I really appreciate any help, as I said, I have never come across Moodle before and it is still rather confusing. Thank you very much in advance.