1

I am new in Moodle and i have a task to create local plugin with course manager role. In my system i have bulk of users in specific course and they are categories in different roles. Some of are bind with students and rest of are Managers. For my case i have received request from the client to make a local plugin which generate multiple reports against student records. I have successfully made the plugin but the condition is this plugin only can access those one who has enrolled in course as a Manager. I m trying with following code which i share you in below but no success. right now only admin can access local plugin rest of are received error messages from the moodle state.

["Sorry, but you do not currently have permissions to do that Project view "]

No idea how it will be resolved.

Please advise.

local/project/db/access.php

defined('MOODLE_INTERNAL') || die();

$capabilities = array(

    'local/project:view' => array(
        'riskbitmask' => RISK_PERSONAL,
        'captype' => 'read',
        'contextlevel' => CONTEXT_SYSTEM,
        'archetypes' => array(
            'manager' => CAP_ALLOW
        ),
    'local/project:manage' => array(
        'captype' => 'write',
        'contextlevel' => CONTEXT_SYSTEM,
        'archetypes' => array(
            'manager' => CAP_ALLOW
        )
    )
    )
);

local/project/header.php

require(dirname(__FILE__).'/../../config.php');
global $DB;
//Get the system context
$url = new moodle_url('/local/project/index.php');

require_login();
require_capability('local/project:view', context_system::instance());
Query Master
  • 6,989
  • 5
  • 35
  • 58

2 Answers2

2

I have successfully achieved the target against moodle permission. I have used has_capability method with course context and check authenticity with require_capability. Following procedure only work with admin and manager other would not access until when they get full rights from the site administration.

local/project/db/access.php

defined('MOODLE_INTERNAL') || die();

$capabilities = array(

    'local/project:view' => array(
        'riskbitmask' => RISK_SPAM,
        'captype' => 'write',
        'contextlevel' => CONTEXT_COURSE,
        'archetypes' => array(
            'manager' => CAP_ALLOW,
        ),
    )
);

local/project/header.php

require(dirname(__FILE__).'/../../config.php');
require_login();

//Get the system context
$context = context_course::instance($course_id);

if (!has_capability('local/project:view', $context)) {
    require_capability('local/project:view', $context);
}
Query Master
  • 6,989
  • 5
  • 35
  • 58
1

There is no standard Moodle role called 'Course manager'.

There is a role called 'Manager' ('manager') and another called 'Course creator' ('coursecreator').

If you have debugging enabled you might get some extra warning messages if there is anything wrong with the code (e.g. if you have not run the install/upgrade process to create the capabilities or if you have not increased the plugin version number after creating the capabilities).

davosmith
  • 6,037
  • 2
  • 14
  • 23
  • I have one more solution but its custom i checked user permission directly with the database its worked for me as i need but issue is this approach doesn't provide me error function which display like moodle error messages. could you please advise how i can display same error messages which moodle use `if(count($resposne) == 0){ echo 'Sorry, but you do not currently have permissions to do that Project'; exit(); }` – Query Master Apr 21 '16 at 14:30
  • If you are having to manually check the permission in the database, then there is something wrong with your capability definition or your capability check. require_capability() and has_capability() have worked correctly for me thousands of times over the years I've been developing for Moodle. – davosmith Apr 21 '16 at 14:51
  • for my case how could i use capability with require_capability() and has_capability()... there is no solution aspect moodle provided – Query Master Apr 21 '16 at 14:58