Skip to content

Commit

Permalink
MDL-6773 - eliminate duplicate code for getting a list of question ca…
Browse files Browse the repository at this point in the history
…tegories. Merged from MOODLE_18_STABLE.
  • Loading branch information
tjhunt committed Mar 1, 2007
1 parent 7c393e9 commit 3ed998f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 65 deletions.
36 changes: 8 additions & 28 deletions lib/questionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

$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";
}
$categoriesarray = question_category_options($courseid, $published, $only_editable);
if ($selected) {
$nothing = '';
} else {
$nothing = 'choose';
}
echo "</select>\n";
choose_from_menu($categoriesarray, 'category', $selected, $nothing);
}

/**
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 0 additions & 34 deletions question/editlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion question/format/coursetestmanager/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function importprocess($filename) {
// 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");
Expand Down
5 changes: 3 additions & 2 deletions question/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3ed998f

Please sign in to comment.