title: How to make a block visible only for admin and teacher in moodle?
body: <p>I think there are more than one way to <em>make block invisible for students</em>.</p>
  <blockquote>
  <p><strong>1.</strong></p>
  <p><code>Hide the block</code></p>
  <p><strong>2.</strong></p>
  <p><code>Assign role to block and set permission to block</code></p>
  </blockquote>
  <p>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.</p>
  <p>For <code>activity I can make invisible the activity</code> by changing <em>db/access.php</em></p>
  <pre><code> 'mod/questionbank:view' =&gt; array(
          'captype' =&gt; 'read',
          'contextlevel' =&gt; CONTEXT_MODULE,
          'legacy' =&gt; array(
              //'guest' =&gt; CAP_ALLOW,
              //'student' =&gt; CAP_ALLOW,
              'teacher' =&gt; CAP_ALLOW,
              'editingteacher' =&gt; CAP_ALLOW,
              'admin' =&gt; CAP_ALLOW
          )
      ),
  </code></pre>
  <p>Like this <strong>how can I make the block invisible for student by code.</strong></p>
  <p><strong>EDIT</strong></p>
  <p>according to <strong>Davosmith's</strong> answer.</p>
  <p>I put inside <code>get_content</code> function</p>
  <pre><code>if (!has_capability('blocks/blockname:view')) {
          return null;
          }
  </code></pre>
  <p>in <strong>blocks/blockname/block_blockname.php</strong></p>
  <p>and in my <strong>blocks/blockname/db/access.php</strong> contain:</p>
  <pre><code>'blocks/blockname:view' =&gt; array(
          'captype' =&gt; 'read',
          'contextlevel' =&gt; CONTEXT_BLOCK,
          'legacy' =&gt; array(
              //'guest' =&gt; CAP_ALLOW,
              //'student' =&gt; CAP_ALLOW,
              'teacher' =&gt; CAP_ALLOW,
             // 'editingteacher' =&gt; CAP_ALLOW,
              'manager' =&gt; CAP_ALLOW
          )
      ),
  </code></pre>
  <p>But it results in error page saying</p>
  <blockquote>
  <p>Coding error detected, it must be fixed by a programmer: PHP catchable
  fatal error</p>
  </blockquote>