How to make a block visible only for admin and teacher in moodle? - hide

I think there are more than one way to make block invisible for students.
1.
Hide the block
2.
Assign role to block and set permission to block
But these are done by admin by change the settings. I need a way by code. How can I write the code to make the block invisible for student.
For activity I can make invisible the activity by changing db/access.php
'mod/questionbank:view' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'legacy' => array(
//'guest' => CAP_ALLOW,
//'student' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'admin' => CAP_ALLOW
)
),
Like this how can I make the block invisible for student by code.
EDIT
according to Davosmith's answer.
I put inside get_content function
if (!has_capability('blocks/blockname:view')) {
return null;
}
in blocks/blockname/block_blockname.php
and in my blocks/blockname/db/access.php contain:
'blocks/blockname:view' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_BLOCK,
'legacy' => array(
//'guest' => CAP_ALLOW,
//'student' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
// 'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
But it results in error page saying
Coding error detected, it must be fixed by a programmer: PHP catchable
fatal error

For any block, if get_contents returns null (and editing is off), the block will not be displayed.
So, put the following in the get_content function of your block (but put in a real capability that you define in db/access.php):
if (!has_capability('block/myblock:somecapability', $this->context)) {
return null;
}

Related

How to apply extension hook in expressionengine

I'm working on expressionengine v2 and i have followed the docs at their site to make extension hooks, i've created a file in third party folder and install that from cp(control panel). Now i don't know how to call that function using hook just to show some text.
$hooks = array(
'after_channel_entry_insert' => 'after_channel_entry_insert',
'before_channel_entry_update' => 'before_channel_entry_update'
);
foreach ($hooks as $hook_name => $method_name) {
$data[] = array(
'class' => __CLASS__,
'method' => $method_name,
'hook' => $hook_name,
'settings' => serialize($this->settings),
'priority' => 10,
'version' => $this->version,
'enabled' => 'y'
);
}
//insert data in extension table
ee()->db->insert_batch('extensions', $data);
//now i want to call this function on hook
function after_channel_entry_insert($data = '') {
die('after_channel_entry_insert');
}

Array iteration with position in puppet

I'm planning to implement the possibility to add multiple ssh keys per user.
For a single key, I used:
if ($sshkey) {
ssh_authorized_key { $resourcename:
ensure => 'present',
type => 'ssh-rsa',
key => '$sshkey',
user => $title,
require => User[$title],
}
}
For multiple keys, i thought that this might work:
if ($sshkeyarray != []) {
$sshkeyarray.each |String $singlesshkey| {
ssh_authorized_key { $resourcename:
ensure => 'present',
type => 'ssh-rsa',
key => '$singlesshkey',
user => $title,
require => User[$title],
}
}
}
But the resourcename can only be used once, so I want to give names like "resourcename_1" for the first ssh key and "resourcename_n" for the n-th key.
How can I do this? Can i get the position of the singlesshkey from the array and add it to the resourdcename?
As described in the docs here you can do this:
$sshkeyarray.each |$index, String $singlesshkey| {
ssh_authorized_key { "${resourcename}_${index}":
ensure => 'present',
type => 'ssh-rsa',
key => $singlesshkey,
user => $title,
require => User[$title],
}
}
Notice that there's no need to test for an empty array either. Looping over an empty array causes nothing to happen anyway.

Restrict sonata_type_model_list to 1 context

I am trying to allow users to select from existing images in the system and have context set like the provided example and this works. How can I stop the users from selecting from any other context type?
$form->add('image', 'sonata_type_model_list', array(), array('link_parameters' => array('context' => 'news')))
Not tested but I just stumbled over the parameter: hide_context
$form->add('image', 'sonata_type_model_list', array(), array(
'link_parameters' => array(
'context' => 'news',
'hide_context' => true
)
))
Maybe you want to give it a try?

Pass arguments to a view in Drupal 6 via custom module

I'm using Drupal 6 to run a gallery I've created. I need to take a parameter from the AJAX request lets say "food" and pass that argument to a view I've created (Views 2) where "food" is a taxonomy term that I am using to get the data I want in return. Everything is working just fine and in my module's method for loading the view I can load the entire view because in the settings you have 'if no argument get all values', but I can't seem to pass arguments to it. Here is the method...
function ajax_methods_menu()
{
$items = array();
$items['admin/settings/ajax_methods'] = array(
'title' => t('AJAX Methods settings.'),
'description' => t('Define settings for the AJAX Methods'),
'page callback' => 'drupal_get_form',
'page arguments' => array('ajax_methods_admin'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM
);
$items['gateway'] = array(
'title' => 'AJAX Gateway',
'page callback' => 'ajax_methods_get_items',
'type' => MENU_CALLBACK,
'access arguments' => array('access content')
);
return $items;
}
function ajax_methods_get_items($args)
{
$content = views_get_view('All_Images');
return drupal_json(array('status' => 0, 'data' => $content->preview('default')));
exit;
}
In looking at the documentation views_get_view() doesn't seem to allow for arguments although I believe they are being passed to my ajax_methods_get_items() method. Thanks for reading!
Got it figured out, I needed to add
return arg(1);
seems to be working pretty well.

Drupal Profile alter: hook_form_alter

After reviewing some posts here and elsewhere, I still can't seem to manually add a select field to the profile. (I need the select list to be populated with a SQL query, not supported with core profile module.)
So far, I am trying two different ways: hook form alter ($form_id == 'user-register' & hook user ($op == 'register') -- but I can't even get the field to appear in the registration form.
function accountselect_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'register'){
$fields['account_select'] = array(
'#type' => 'fieldset',
'#title' => t('Your Security Question')
);
$fields['account_select']['account_name'] = array(
'#type' => 'select',
'#default_value' => 'Select',
'#description' => t('Select a verification question in case you forget your password'),
'#options' => array(t('Select One'),
t('Where you attended Elementry School'), t('Your Best Man'))
);
return $fields;
}
Here is the hook form alter attempt
function accountselect_form_alter(&$form, $form_state, $form_id){
if($form_id == 'user-register') {
$form['account_select']['account_name'] = array(
'#type' => 'select',
'#title' => t('Account'),
'#description' => t('Enter the account to which the contact belongs.'),
'#options' => array(t('Account1'),t('account2'), t('account3')),
'#default_value' => $edit ['Account']
);
}
return $fields;
}
Sorry Guys, the code here is correct. I did a little debugging when the module was first enabled. I thought I had successfully fixed the problem, but what really happened is that the module became disabled. So, no matter what was in there, it would have had no effect....
No worries, I've punched myself in the face for the stupid question....

Resources