General developer forum

Problems with has_capability()

 
Picture of Saúl Chacón Grimaldo
Problems with has_capability()
 

Hello, I'm new to Moodle and I'm trying to develop a module, at the moment I'm trying to get different views depending on the role of the user, I already saw that it gets done with has_capability, but it's not working, what I do is the following :


In the file access.php i have the following

// apart from the ones that come by default
// the module is called collaborative

'mod/collaborative:manage' => array(
        'captype' => 'read',
        'contextlevel' => CONTEXT_MODULE,
        'archetypes' => array(
            'teacher' => CAP_ALLOW,
            'editingteacher' => CAP_ALLOW,
            'manager' => CAP_ALLOW
        )
    ),


And in the view.php file i have the following:

require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/lib.php');

$id = optional_param('id', 0, PARAM_INT); // Course_module ID, or
$n  = optional_param('n', 0, PARAM_INT);  // ... collaborative instance ID - it should be named as the first character of the module.

if ($id) {
    $cm         = get_coursemodule_from_id('collaborative', $id, 0, false, MUST_EXIST);
    $course     = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
    $collaborative  = $DB->get_record('collaborative', array('id' => $cm->instance), '*', MUST_EXIST);
} else if ($n) {
    $collaborative  = $DB->get_record('collaborative', array('id' => $n), '*', MUST_EXIST);
    $course     = $DB->get_record('course', array('id' => $collaborative->course), '*', MUST_EXIST);
    $cm         = get_coursemodule_from_instance('collaborative', $collaborative->id, $course->id, false, MUST_EXIST);
} else {
    error('You must specify a course_module ID or an instance ID');
}

require_login($course, true, $cm);
$context = context_module::instance($cm->id);//obtiene el contexto
$event = \mod_collaborative\event\course_module_viewed::create(array(
    'objectid' => $collaborative->id,
    'context' => $context,
));
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('collaborative', $collaborative);
$event->trigger();

// Print the page header.

$PAGE->set_url('/mod/collaborative/view.php', array('id' => $cm->id));
$PAGE->set_context($context);
$PAGE->set_cm($cm);
$PAGE->set_title(format_string($collaborative->name));
$PAGE->set_heading(format_string($course->fullname));

// Output starts here.
echo $OUTPUT->header();

if (has_capability('mod/collaborative:manage', $context)){
    echo 'se puede ver este código...';
} //estoy intentando que un profesor sea quien vea ese código

// Finish the page.
echo $OUTPUT->footer();


//////////////////////////////////////////////////////////////////////////////////////////////////////

The problem is that with no role (student, teacher, administrator) I can see what is inside has_capability. I was guided by the official documentation that is on the page adding capabilities and roles and modules, and the modules that come installed in moodle.

I do not understand what else to do or where I am failing, if anyone could help me I would appreciate it.

 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: Problems with has_capability()
Core developersDocumentation writersParticularly helpful MoodlersPlugin developers

After changing access.php, did you increase the version number in version.php, then visit admin/index.php in your site to allow Moodle to update itself?

(Also, do you have developer Debugging on while you are developing?)

 
Average of ratings: -
Picture of Saúl Chacón Grimaldo
Re: Problems with has_capability()
 
Hello tim, yes I did all that and I checked that everything was in order. I informed you that I could solve the problems I had, what happened is that the file access.php I needed a comma to separate the capabilities correctly. Thanks for responding, greetings.
 
Average of ratings: -