// returns a comma separated list of ids of the category and all subcategories
$categorylist = $categoryid;
- if ($subcategories = $DB->get_records('question_categories', array('parent'=>$categoryid), 'sortorder ASC', 'id, id')) {
+ if ($subcategories = $DB->get_records('question_categories', array('parent'=>$categoryid), 'sortorder ASC', 'id, 1')) {
foreach ($subcategories as $subcategory) {
$categorylist .= ','. question_categorylist($subcategory->id);
}
// Get the record we are updating.
$oldcat = $DB->get_record('question_categories', array('id' => $updateid));
- $lastcategoryinthiscontext = !$DB->record_exists_select('question_categories',
- 'contextid = ? AND id <> ?', array($oldcat->contextid, $updateid));
+ $lastcategoryinthiscontext = question_is_only_toplevel_category_in_context($updateid);
if (!empty($newparent) && !$lastcategoryinthiscontext) {
list($parentid, $tocontextid) = explode(',', $newparent);
// If moving to another context, check permissions some more.
if ($oldcat->contextid != $tocontextid){
- if ($lastcategoryinthiscontext) {
- // Don't allow the last category in a context to be moved.
- print_error('cannotmovecate', 'question', $this->pageurl->out(), $newname);
- }
$tocontext = get_context_instance_by_id($tocontextid);
require_capability('moodle/question:managecategory', $tocontext);
}
print_error('cannotupdatecate', 'question', $this->pageurl->out(), $newname);
}
- // If the question name has changed, rename any random questions in that category.
+ // If the category name has changed, rename any random questions in that category.
if ($oldcat->name != $cat->name) {
$randomqname = $QTYPES[RANDOM]->question_name($cat);
$DB->set_field('question', 'name', $randomqname, array('category' => $cat->id), 'qtype', RANDOM);
$questioncategoryel = $mform->addElement('questioncategory', 'parent', get_string('parent', 'quiz'),
array('contexts'=>$contexts, 'top'=>true, 'currentcat'=>$currentcat, 'nochildrenof'=>$currentcat));
$mform->setType('parent', PARAM_SEQUENCE);
- // This next test is actually looking to see if $currentcat is the id of
- // a category that already exists, and is the only top-level category in
- // it context. If so, we stop it from being moved.
- if (1 == $DB->count_records_sql("SELECT count(*)
- FROM {question_categories} c1,
- {question_categories} c2
- WHERE c2.id = ?
- AND c1.contextid = c2.contextid
- AND c1.parent = 0 AND c2.parent = 0", array($currentcat))){
+ if (question_is_only_toplevel_category_in_context($currentcat)) {
$mform->hardFreeze('parent');
}
$mform->setHelpButton('parent', array('categoryparent', get_string('parent', 'quiz'), 'question'));
}
//adjust sortorder before we make the cat a peer of it's new peers
- $peers = $DB->get_records_select_menu('question_categories', "contextid = ? AND parent = ?", array($toparent->contextid, $toparent->id),
- "sortorder ASC", "id, id");
+ $peers = $DB->get_records_select_menu('question_categories',
+ 'contextid = ? AND parent = ?', array($toparent->contextid, $toparent->id),
+ 'sortorder ASC', 'id, 1');
$peers = array_keys($peers);
if ($totop){
array_unshift($peers, $cattomove->id);
return $qresults;
}
+/**
+ * @param integer $categoryid a category id.
+ * @return boolean whether this is the only top-level category in a context.
+ */
+function question_is_only_toplevel_category_in_context($categoryid) {
+ global $DB;
+ return 1 == $DB->count_records_sql("
+ SELECT count(*)
+ FROM {question_categories} c1,
+ {question_categories} c2
+ WHERE c2.id = ?
+ AND c1.contextid = c2.contextid
+ AND c1.parent = 0 AND c2.parent = 0", array($categoryid));
+}
-function question_can_delete_cat($todelete){
- global $CFG, $DB;
- $record = $DB->get_record_sql("SELECT count(*) as count, c1.contextid as contextid FROM {question_categories} c1,
- {question_categories} c2 WHERE c2.id = ?
- AND c1.contextid = c2.contextid GROUP BY c1.contextid", array($todelete));
- $contextid = $record->contextid;
- $count = $record->count;
- if ($count < 2) {
+/**
+ * Check whether this user is allowed to delete this category.
+ *
+ * @param integer $todelete a category id.
+ */
+function question_can_delete_cat($todelete) {
+ global $DB;
+ if (question_is_only_toplevel_category_in_context($todelete)) {
print_error('cannotdeletecate', 'question');
} else {
+ $contextid = $DB->get_field('question_categories', 'contextid', array('id' => $todelete));
require_capability('moodle/question:managecategory', get_context_instance_by_id($contextid));
}
}