moved question_make_default_categories function to questionlib.php since it is needed by other modules outside the question bank editing interface.

This commit is contained in:
jamiesensei 2007-09-18 11:23:29 +00:00
parent 41b6d0011c
commit 8f0f605ddf
2 changed files with 38 additions and 36 deletions

View File

@ -1660,6 +1660,44 @@ function question_category_select_menu($contexts, $top = false, $currentcat = 0,
choose_from_menu_nested($categoriesarray, 'category', $selected, $nothing);
}
/**
* Gets the default category in the most specific context.
* If no categories exist yet then default ones are created in all contexts.
*
* @param array $contexts The context objects for this context and all parent contexts.
* @return object The default category - the category in the course context
*/
function question_make_default_categories($contexts) {
$toreturn = null;
// If it already exists, just return it.
foreach ($contexts as $key => $context) {
if (!$categoryrs = get_recordset_select("question_categories", "contextid = '{$context->id}'", 'sortorder, name', '*', '', 1)) {
error('error getting category record');
} else {
if (!$category = rs_fetch_record($categoryrs)){
// Otherwise, we need to make one
$category = new stdClass;
$contextname = print_context_name($context, false, true);
$category->name = addslashes(get_string('defaultfor', 'question', $contextname));
$category->info = addslashes(get_string('defaultinfofor', 'question', $contextname));
$category->contextid = $context->id;
$category->parent = 0;
$category->sortorder = 999; // By default, all categories get this number, and are sorted alphabetically.
$category->stamp = make_unique_id_code();
if (!$category->id = insert_record('question_categories', $category)) {
error('Error creating a default category for context '.print_context_name($context));
}
}
}
if ($context->contextlevel == CONTEXT_COURSE){
$toreturn = clone($category);
}
}
return $toreturn;
}
/**
* Get all the category objects, including a count of the number of questions in that category,
* for all the categories in the lists $contexts.

View File

@ -75,42 +75,6 @@ function get_questions_category( $category, $noparent=false, $recurse=true, $exp
return $qresults;
}
/**
* Gets the default category in the most specific context.
* If no categories exist yet then default ones are created in all contexts.
*
* @param array $contexts The context objects for this context and all parent contexts.
* @return object The default category - the category in the course context
*/
function question_make_default_categories($contexts) {
// If it already exists, just return it.
foreach ($contexts as $key => $context) {
if (!$categoryrs = get_recordset_select("question_categories", "contextid = '{$context->id}'", 'sortorder, name', '*', '', 1)) {
error('error getting category record');
} else {
if (!$category = rs_fetch_record($categoryrs)){
// Otherwise, we need to make one
$category = new stdClass;
$contextname = print_context_name($context, false, true);
$category->name = addslashes(get_string('defaultfor', 'question', $contextname));
$category->info = addslashes(get_string('defaultinfofor', 'question', $contextname));
$category->contextid = $context->id;
$category->parent = 0;
$category->sortorder = 999; // By default, all categories get this number, and are sorted alphabetically.
$category->stamp = make_unique_id_code();
if (!$category->id = insert_record('question_categories', $category)) {
error('Error creating a default category for context '.print_context_name($context));
}
}
}
if ($context->contextlevel == CONTEXT_COURSE){
$toreturn = clone($category);
}
}
return $toreturn;
}
function question_can_delete_cat($todelete){
global $CFG;