SELECT cat.*, c.shortname AS coursename
FROM {$CFG->prefix}question_categories cat, {$CFG->prefix}course c
WHERE c.id = cat.course AND (cat.course = $courseid $publishsql)
- ORDER BY cat.parent, cat.sortorder, cat.name ASC");
+ ORDER BY (CASE WHEN cat.course = $courseid THEN 0 ELSE 1 END), cat.parent, cat.sortorder, cat.name ASC");
$categories = add_indented_names($categories);
$categoriesarray = array();
* @param integer $courseid The id of the course whose default category is wanted
*/
function get_default_question_category($courseid) {
-
- if ($categories = get_records_select("question_categories", "course = '$courseid' AND parent = '0'", "id")) {
- foreach ($categories as $category) {
- return $category; // Return the first one (lowest id)
- }
+ // If it already exists, just return it.
+ if ($category = get_records_select("question_categories", "course = '$courseid' AND parent = '0'", 'id', '*', '', 1)) {
+ return reset($category);
}
// Otherwise, we need to make one
+ $category = new stdClass;
$category->name = get_string("default", "quiz");
$category->info = get_string("defaultinfo", "quiz");
$category->course = $courseid;
$category->parent = 0;
- // TODO: Figure out why we use 999 below
- $category->sortorder = 999;
+ $category->sortorder = 999; // By default, all categories get this number, and are sorted alphabetically.
$category->publish = 0;
$category->stamp = make_unique_id_code();
global $CFG;
/// Make sure the default category exists for this course
- if (!$categories = get_records("question_categories", "course", $course->id, "id ASC")) {
- if (!$category = get_default_question_category($course->id)) {
- notify("Error creating a default category!");
- }
- }
+ get_default_question_category($course->id);
/// Get all the existing categories now
- if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
- notify("Could not find any question categories!");
- return false; // Something is really wrong
- }
+ $catmenu = question_category_options($course->id, true);
- $categories = add_indented_names( $categories );
- foreach ($categories as $key => $category) {
- if ($catcourse = get_record("course", "id", $category->course)) {
- if ($category->publish && $category->course != $course->id) {
- $category->indentedname .= " ($catcourse->shortname)";
- }
- $catmenu[$category->id] = $category->indentedname;
- }
- }
$strcategory = get_string("category", "quiz");
$strshow = get_string("show", "quiz");
$streditcats = get_string("editcategories", "quiz");
print_heading_with_help($txt->importquestions, "import", "quiz");
/// Get all the existing categories now
- if (has_capability('moodle/question:manage', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // the admin can import into all categories
- if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
- error("Could not find any question categories!"); // Something is really wrong
- }
- } else { // select only the categories to which the teacher has write access
- $cats = get_records('question_categories');
- $categories = array();
- foreach ($cats as $cat) {
- if (has_capability('moodle/question:managecategory', get_context_instance(CONTEXT_COURSE, $cat->course))) {
- $categories[] = $cat;
- }
- }
- if (empty($categories)) {
- error("Could not find any question categories!");
- }
- }
- $categories = add_indented_names($categories);
- foreach ($categories as $key => $cat) {
- if ($catcourse = get_record("course", "id", $cat->course)) {
- if ($cat->publish && $cat->course != $course->id) {
- $cat->indentedname .= " ($catcourse->shortname)";
- }
- $catmenu[$cat->id] = $cat->indentedname;
- }
- }
+ $catmenu = question_category_options($course->id, false, true);
//==========
// DISPLAY