question bank: MDL-14434 Cannot edit the only question category in a context.

This commit is contained in:
tjhunt 2008-11-27 11:50:29 +00:00
parent 004ff192d4
commit df4e224454
5 changed files with 30 additions and 27 deletions

View File

@ -2348,7 +2348,7 @@ function question_categorylist($categoryid) {
// 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);
}

View File

@ -435,8 +435,7 @@ class question_category_object {
// 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);
@ -451,10 +450,6 @@ class question_category_object {
// 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);
}
@ -470,7 +465,7 @@ class question_category_object {
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);

View File

@ -16,15 +16,7 @@ class question_category_edit_form extends moodleform {
$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'));

View File

@ -155,8 +155,9 @@
}
//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);

View File

@ -77,17 +77,32 @@ function get_questions_category( $category, $noparent=false, $recurse=true, $exp
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));
}
}