MDL-6773 - eliminate duplicate code for getting a list of question categories. Merged from MOODLE_18_STABLE.
This commit is contained in:
+8
-28
@@ -1545,35 +1545,13 @@ function add_indented_names($categories) {
|
||||
* @param integer $selected optionally, the id of a category to be selected by default in the dropdown.
|
||||
*/
|
||||
function question_category_select_menu($courseid, $published = false, $only_editable = false, $selected = "") {
|
||||
global $CFG;
|
||||
|
||||
// get sql fragment for published
|
||||
$publishsql="";
|
||||
if ($published) {
|
||||
$publishsql = " OR publish = 1";
|
||||
$categoriesarray = question_category_options($courseid, $published, $only_editable);
|
||||
if ($selected) {
|
||||
$nothing = '';
|
||||
} else {
|
||||
$nothing = 'choose';
|
||||
}
|
||||
|
||||
$categories = get_records_sql("
|
||||
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");
|
||||
|
||||
$categories = add_indented_names($categories);
|
||||
|
||||
echo "<select name=\"category\">\n";
|
||||
foreach ($categories as $category) {
|
||||
$cid = $category->id;
|
||||
$cname = question_category_coursename($category, $courseid);
|
||||
$seltxt = "";
|
||||
if ($cid==$selected) {
|
||||
$seltxt = "selected=\"selected\"";
|
||||
}
|
||||
if ((!$only_editable) || has_capability('moodle/question:managecategory', get_context_instance(CONTEXT_COURSE, $category->course))) {
|
||||
echo " <option value=\"$cid\" $seltxt>$cname</option>\n";
|
||||
}
|
||||
}
|
||||
echo "</select>\n";
|
||||
choose_from_menu($categoriesarray, 'category', $selected, $nothing);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1601,6 +1579,8 @@ function question_category_options($courseid, $published = false, $only_editable
|
||||
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");
|
||||
$categories = add_indented_names($categories);
|
||||
|
||||
$categoriesarray = array();
|
||||
foreach ($categories as $category) {
|
||||
$cid = $category->id;
|
||||
|
||||
@@ -93,40 +93,6 @@ function get_default_question_category($courseid) {
|
||||
return $category;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of categories nicely formatted
|
||||
* @param int courseid id of course
|
||||
* @param bool published true=include all published categories
|
||||
* @return array formatted category names
|
||||
*/
|
||||
function question_category_menu($courseid, $published=false) {
|
||||
/// Returns the list of categories
|
||||
$publish = "";
|
||||
if ($published) {
|
||||
$publish = "OR publish = '1'";
|
||||
}
|
||||
|
||||
if (!has_capability('moodle/question:manage', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
|
||||
$categories = get_records_select("question_categories", "course = '$courseid' $publish", 'parent, sortorder, name ASC');
|
||||
} else {
|
||||
$categories = get_records_select("question_categories", '', 'parent, sortorder, name ASC');
|
||||
}
|
||||
if (!$categories) {
|
||||
return false;
|
||||
}
|
||||
$categories = add_indented_names($categories);
|
||||
|
||||
foreach ($categories as $category) {
|
||||
if ($catcourse = get_record("course", "id", $category->course)) {
|
||||
if ($category->publish && ($category->course != $courseid)) {
|
||||
$category->indentedname .= " ($catcourse->shortname)";
|
||||
}
|
||||
$catmenu[$category->id] = $category->indentedname;
|
||||
}
|
||||
}
|
||||
return $catmenu;
|
||||
}
|
||||
|
||||
/**
|
||||
* prints a form to choose categories
|
||||
*/
|
||||
|
||||
@@ -116,7 +116,7 @@ class qformat_coursetestmanager extends qformat_default {
|
||||
// don't have to do this on linux, since it's alreay been done in the test above
|
||||
if (PHP_OS == "WINNT") { $question_categories = $this->getquestioncategories($filename); }
|
||||
// print the intermediary form
|
||||
if (!$categories = question_category_menu($course->id, true)) {
|
||||
if (!$categories = question_category_options($course->id, true)) {
|
||||
error("No categories!");
|
||||
}
|
||||
print_heading_with_help($strimportquestions, "import", "quiz");
|
||||
|
||||
+3
-2
@@ -10,8 +10,9 @@
|
||||
*/
|
||||
|
||||
require_once("../config.php");
|
||||
require_once( "editlib.php" );
|
||||
require_once($CFG->dirroot.'/lib/uploadlib.php');
|
||||
require_once("editlib.php" );
|
||||
require_once($CFG->libdir . '/uploadlib.php');
|
||||
require_once($CFG->libdir . '/questionlib.php');
|
||||
|
||||
// get parameters
|
||||
$params = new stdClass;
|
||||
|
||||
Reference in New Issue
Block a user