You are here

function qformat_default::create_category_path in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/question/format.php \qformat_default::create_category_path()

find and/or create the category described by a delimited list e.g. $course$/tom/dick/harry or tom/dick/harry

removes any context string no matter whether $getcontext is set but if $getcontext is set then ignore the context and use selected category context.

Parameters

string catpath delimited category path:

string delimiter path delimiting character:

int courseid course to search for categories:

Return value

mixed category object or null if fails

1 call to qformat_default::create_category_path()
qformat_default::importprocess in includes/moodle/question/format.php
Process the file This method should not normally be overidden

File

includes/moodle/question/format.php, line 377

Class

qformat_default
Base class for question import and export formats.

Code

function create_category_path($catpath, $delimiter = '/') {
  $catpath = clean_param($catpath, PARAM_PATH);
  $catnames = explode($delimiter, $catpath);
  $parent = 0;
  $category = null;

  // check for context id in path, it might not be there in pre 1.9 exports
  $matchcount = preg_match('/^\\$([a-z]+)\\$$/', $catnames[0], $matches);
  if ($matchcount == 1) {
    $contextid = $this->translator
      ->string_to_context($matches[1]);
    array_shift($catnames);
  }
  else {
    $contextid = FALSE;
  }
  if ($this->contextfromfile && $contextid !== FALSE) {
    $context = get_context_instance_by_id($contextid);
    require_capability('moodle/question:add', $context);
  }
  else {
    $context = get_context_instance_by_id($this->category->contextid);
  }
  foreach ($catnames as $catname) {
    if ($category = get_record('question_categories', 'name', $catname, 'contextid', $context->id, 'parent', $parent)) {
      $parent = $category->id;
    }
    else {
      require_capability('moodle/question:managecategory', $context);

      // create the new category
      $category = new object();
      $category->contextid = $context->id;
      $category->name = $catname;
      $category->info = '';
      $category->parent = $parent;
      $category->sortorder = 999;
      $category->stamp = make_unique_id_code();
      if (!($id = insert_record('question_categories', $category))) {
        error("cannot create new category - {$catname}");
      }
      $category->id = $id;
      $parent = $id;
    }
  }
  return $category;
}