You are here

function question_has_capability_on in Quiz 6.5

Same name in this branch
  1. 6.5 includes/moodle_support.php \question_has_capability_on()
  2. 6.5 includes/moodle/lib/questionlib.php \question_has_capability_on()
Same name and namespace in other branches
  1. 6.6 includes/moodle_support.php \question_has_capability_on()
  2. 6.6 includes/moodle/lib/questionlib.php \question_has_capability_on()

Check capability on category

Parameters

mixed $question object or id:

string $cap 'add', 'edit', 'view', 'use', 'move':

integer $cachecat useful to cache all question records in a category:

Return value

boolean this user has the capability $cap for this question $question?

2 calls to question_has_capability_on()
qformat_default::exportprocess in includes/moodle/question/format.php
Do the export For most types this should not need to be overrided
question_require_capability_on in includes/moodle/lib/questionlib.php
Require capability on question.

File

includes/moodle/lib/questionlib.php, line 2178

Code

function question_has_capability_on($question, $cap, $cachecat = -1) {
  global $USER;

  // nicolasconnault@gmail.com In some cases I get $question === false. Since no such object exists, it can't be deleted, we can safely return true
  if ($question === false) {
    return true;
  }

  // these are capabilities on existing questions capabilties are

  //set per category. Each of these has a mine and all version. Append 'mine' and 'all'
  $question_questioncaps = array(
    'edit',
    'view',
    'use',
    'move',
  );
  static $questions = array();
  static $categories = array();
  static $cachedcat = array();
  if ($cachecat != -1 && array_search($cachecat, $cachedcat) === FALSE) {
    $questions += get_records('question', 'category', $cachecat);
    $cachedcat[] = $cachecat;
  }
  if (!is_object($question)) {
    if (!isset($questions[$question])) {
      if (!($questions[$question] = get_record('question', 'id', $question))) {
        print_error('questiondoesnotexist', 'question');
      }
    }
    $question = $questions[$question];
  }
  if (!isset($categories[$question->category])) {
    if (!($categories[$question->category] = get_record('question_categories', 'id', $question->category))) {
      print_error('invalidcategory', 'quiz');
    }
  }
  $category = $categories[$question->category];
  if (array_search($cap, $question_questioncaps) !== FALSE) {
    if (!has_capability('moodle/question:' . $cap . 'all', get_context_instance_by_id($category->contextid))) {
      if ($question->createdby == $USER->id) {
        return has_capability('moodle/question:' . $cap . 'mine', get_context_instance_by_id($category->contextid));
      }
      else {
        return false;
      }
    }
    else {
      return true;
    }
  }
  else {
    return has_capability('moodle/question:' . $cap, get_context_instance_by_id($category->contextid));
  }
}