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