Skip to content

Commit

Permalink
Merge branch 'master_MDL-71585-managecategories' of https://github.co…
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Sep 6, 2021
2 parents c467ce7 + fb10b36 commit 3b903ae
Show file tree
Hide file tree
Showing 51 changed files with 2,471 additions and 703 deletions.
2 changes: 1 addition & 1 deletion lib/classes/event/question_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function get_url() {
['courseid' => $this->courseid, 'cat' => $cat, 'lastchanged' => $this->objectid]);
}
// Lets try viewing from the frontpage for contexts above course.
return new \moodle_url('/question/category.php',
return new \moodle_url('/question/bank/managecategories/category.php',
['courseid' => SITEID, 'edit' => $this->other['categoryid'], 'lastchanged' => $this->objectid]);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/classes/event/question_category_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function get_url() {
return new \moodle_url('/question/edit.php', ['courseid' => $this->courseid, 'cat' => $cat]);
}
// Lets try viewing from the frontpage for contexts above course.
return new \moodle_url('/question/category.php', ['courseid' => SITEID, 'edit' => $this->objectid]);
return new \moodle_url('/question/bank/managecategories/category.php', ['courseid' => SITEID, 'edit' => $this->objectid]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/event/question_moved.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function get_url() {
['courseid' => $this->courseid, 'cat' => $cat, 'lastchanged' => $this->objectid]);
}
// Lets try viewing from the frontpage for contexts above course.
return new \moodle_url('/question/category.php',
return new \moodle_url('/question/bank/managecategories/category.php',
['courseid' => SITEID, 'edit' => $this->other['newcategoryid'], 'lastchanged' => $this->objectid]);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/classes/event/questions_exported.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public function get_url() {
}
return new \moodle_url('/question/edit.php', ['courseid' => $this->courseid, 'cat' => $cat]);
}
return new \moodle_url('/question/category.php', ['courseid' => SITEID, 'edit' => $this->other['categoryid']]);
return new \moodle_url('/question/bank/managecategories/category.php',
['courseid' => SITEID, 'edit' => $this->other['categoryid']]);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/classes/event/questions_imported.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public function get_url() {
}
return new \moodle_url('/question/edit.php', ['courseid' => $this->courseid, 'cat' => $cat]);
}
return new \moodle_url('/question/category.php', ['courseid' => SITEID, 'edit' => $this->other['categoryid']]);
return new \moodle_url('/question/bank/managecategories/category.php',
['courseid' => SITEID, 'edit' => $this->other['categoryid']]);
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/classes/plugin_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,7 @@ public static function standard_plugins_list($type) {
'exporttoxml',
'exportquestions',
'importquestions',
'managecategories',
'viewcreator',
'viewquestionname',
'viewquestiontext',
Expand Down
5 changes: 3 additions & 2 deletions lib/form/questioncategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

global $CFG;
use qbank_managecategories\helper;
require_once("$CFG->libdir/form/selectgroups.php");
require_once("$CFG->libdir/questionlib.php");

Expand Down Expand Up @@ -59,8 +60,8 @@ public function __construct($elementName = null, $elementLabel = null, $options
if (is_array($options)) {
$this->_options = $options + $this->_options;
$this->loadArrayOptGroups(
question_category_options($this->_options['contexts'], $this->_options['top'], $this->_options['currentcat'],
false, $this->_options['nochildrenof'], false));
helper::question_category_options($this->_options['contexts'], $this->_options['top'],
$this->_options['currentcat'], false, $this->_options['nochildrenof'], false));
}
}

Expand Down
218 changes: 53 additions & 165 deletions lib/questionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,16 @@ function match_grade_options($gradeoptionsfull, $grade, $matchgrades = 'error')
* - random questions
*
* @param int $categoryid The category ID.
* @deprecated since Moodle 4.0 MDL-71585
* @see qbank_managecategories\helper
* @todo Final deprecation on Moodle 4.4 MDL-72438
*/
function question_remove_stale_questions_from_category($categoryid) {
global $DB;

$select = 'category = :categoryid AND (qtype = :qtype OR hidden = :hidden)';
$params = ['categoryid' => $categoryid, 'qtype' => 'random', 'hidden' => 1];
$questions = $DB->get_recordset_select("question", $select, $params, '', 'id');
foreach ($questions as $question) {
// The function question_delete_question does not delete questions in use.
question_delete_question($question->id);
}
$questions->close();
debugging('Function question_remove_stale_questions_from_category()
has been deprecated and moved to qbank_managecategories plugin,
Please use qbank_managecategories\helper::question_remove_stale_questions_from_category() instead.',
DEBUG_DEVELOPER);
\qbank_managecategories\helper::question_remove_stale_questions_from_category($categoryid);
}

/**
Expand Down Expand Up @@ -1146,65 +1144,29 @@ function sort_categories_by_tree(&$categories, $id = 0, $level = 1) {
* @param int $id the category to start the indenting process from.
* @param int $depth the indent depth. Used in recursive calls.
* @return array a new array of categories, in the right order for the tree.
* @deprecated since Moodle 4.0 MDL-71585
* @see qbank_managecategories\helper
* @todo Final deprecation on Moodle 4.4 MDL-72438
*/
function flatten_category_tree(&$categories, $id, $depth = 0, $nochildrenof = -1) {

// Indent the name of this category.
$newcategories = array();
$newcategories[$id] = $categories[$id];
$newcategories[$id]->indentedname = str_repeat('   ', $depth) .
$categories[$id]->name;

// Recursively indent the children.
foreach ($categories[$id]->childids as $childid) {
if ($childid != $nochildrenof) {
$newcategories = $newcategories + flatten_category_tree(
$categories, $childid, $depth + 1, $nochildrenof);
}
}

// Remove the childids array that were temporarily added.
unset($newcategories[$id]->childids);

return $newcategories;
debugging('Function flatten_category_tree() has been deprecated and moved to qbank_managecategories plugin,
Please use qbank_managecategories\helper::flatten_category_tree() instead.', DEBUG_DEVELOPER);
return \qbank_managecategories\helper::flatten_category_tree($categories, $id, $depth, $nochildrenof);
}

/**
* Format categories into an indented list reflecting the tree structure.
*
* @param array $categories An array of category objects, for example from the.
* @return array The formatted list of categories.
* @deprecated since Moodle 4.0 MDL-71585
* @see qbank_managecategories\helper
* @todo Final deprecation on Moodle 4.4 MDL-72438
*/
function add_indented_names($categories, $nochildrenof = -1) {

// Add an array to each category to hold the child category ids. This array
// will be removed again by flatten_category_tree(). It should not be used
// outside these two functions.
foreach (array_keys($categories) as $id) {
$categories[$id]->childids = array();
}

// Build the tree structure, and record which categories are top-level.
// We have to be careful, because the categories array may include published
// categories from other courses, but not their parents.
$toplevelcategoryids = array();
foreach (array_keys($categories) as $id) {
if (!empty($categories[$id]->parent) &&
array_key_exists($categories[$id]->parent, $categories)) {
$categories[$categories[$id]->parent]->childids[] = $id;
} else {
$toplevelcategoryids[] = $id;
}
}

// Flatten the tree to and add the indents.
$newcategories = array();
foreach ($toplevelcategoryids as $id) {
$newcategories = $newcategories + flatten_category_tree(
$categories, $id, 0, $nochildrenof);
}

return $newcategories;
debugging('Function add_indented_names() has been deprecated and moved to qbank_managecategories plugin,
Please use qbank_managecategories\helper::add_indented_names() instead.', DEBUG_DEVELOPER);
return \qbank_managecategories\helper::add_indented_names($categories, $nochildrenof);
}

/**
Expand All @@ -1218,30 +1180,15 @@ function add_indented_names($categories, $nochildrenof = -1) {
* @param integer $only_editable if true, exclude categories this user is not allowed to edit.
* @param integer $selected optionally, the id of a category to be selected by
* default in the dropdown.
* @deprecated since Moodle 4.0 MDL-71585
* @see qbank_managecategories\helper
* @todo Final deprecation on Moodle 4.4 MDL-72438
*/
function question_category_select_menu($contexts, $top = false, $currentcat = 0,
$selected = "", $nochildrenof = -1) {
$categoriesarray = question_category_options($contexts, $top, $currentcat,
false, $nochildrenof, false);
if ($selected) {
$choose = '';
} else {
$choose = 'choosedots';
}
$options = array();
foreach ($categoriesarray as $group => $opts) {
$options[] = array($group => $opts);
}
echo html_writer::label(get_string('questioncategory', 'core_question'), 'id_movetocategory', false, array('class' => 'accesshide'));
$attrs = array(
'id' => 'id_movetocategory',
'class' => 'custom-select',
'data-action' => 'toggle',
'data-togglegroup' => 'qbank',
'data-toggle' => 'action',
'disabled' => true,
);
echo html_writer::select($options, 'category', $selected, $choose, $attrs);
debugging('Function question_category_select_menu() has been deprecated and moved to qbank_managecategories plugin,
Please use qbank_managecategories\helper::question_category_select_menu() instead.', DEBUG_DEVELOPER);
\qbank_managecategories\helper::question_category_select_menu($contexts, $top, $currentcat, $selected, $nochildrenof);
}

/**
Expand Down Expand Up @@ -1366,16 +1313,14 @@ function question_make_default_categories($contexts) {
* @param string $sortorder used as the ORDER BY clause in the select statement.
* @param bool $top Whether to return the top categories or not.
* @return array of category objects.
* @deprecated since Moodle 4.0 MDL-71585
* @see qbank_managecategories\helper
* @todo Final deprecation on Moodle 4.4 MDL-72438
*/
function get_categories_for_contexts($contexts, $sortorder = 'parent, sortorder, name ASC', $top = false) {
global $DB;
$topwhere = $top ? '' : 'AND c.parent <> 0';
return $DB->get_records_sql("
SELECT c.*, (SELECT count(1) FROM {question} q
WHERE c.id = q.category AND q.hidden='0' AND q.parent='0') AS questioncount
FROM {question_categories} c
WHERE c.contextid IN ($contexts) $topwhere
ORDER BY $sortorder");
debugging('Function get_categories_for_contexts() has been deprecated and moved to qbank_managecategories plugin,
Please use qbank_managecategories\helper::get_categories_for_contexts() instead.', DEBUG_DEVELOPER);
return \qbank_managecategories\helper::get_categories_for_contexts($contexts, $sortorder, $top);
}

/**
Expand All @@ -1388,81 +1333,27 @@ function get_categories_for_contexts($contexts, $sortorder = 'parent, sortorder,
* @param int $nochildrenof
* @param boolean $escapecontextnames Whether the returned name of the thing is to be HTML escaped or not.
* @return array
* @deprecated since Moodle 4.0 MDL-71585
* @see qbank_managecategories\helper
* @todo Final deprecation on Moodle 4.4 MDL-72438
*/
function question_category_options($contexts, $top = false, $currentcat = 0,
$popupform = false, $nochildrenof = -1, $escapecontextnames = true) {
global $CFG;
$pcontexts = array();
foreach ($contexts as $context) {
$pcontexts[] = $context->id;
}
$contextslist = join(', ', $pcontexts);

$categories = get_categories_for_contexts($contextslist, 'parent, sortorder, name ASC', $top);

if ($top) {
$categories = question_fix_top_names($categories);
}

$categories = question_add_context_in_key($categories);
$categories = add_indented_names($categories, $nochildrenof);

// sort cats out into different contexts
$categoriesarray = array();
foreach ($pcontexts as $contextid) {
$context = context::instance_by_id($contextid);
$contextstring = $context->get_context_name(true, true, $escapecontextnames);
foreach ($categories as $category) {
if ($category->contextid == $contextid) {
$cid = $category->id;
if ($currentcat != $cid || $currentcat == 0) {
$a = new stdClass;
$a->name = format_string($category->indentedname, true,
array('context' => $context));
if ($category->idnumber !== null && $category->idnumber !== '') {
$a->idnumber = s($category->idnumber);
}
if (!empty($category->questioncount)) {
$a->questioncount = $category->questioncount;
}
if (isset($a->idnumber) && isset($a->questioncount)) {
$formattedname = get_string('categorynamewithidnumberandcount', 'question', $a);
} else if (isset($a->idnumber)) {
$formattedname = get_string('categorynamewithidnumber', 'question', $a);
} else if (isset($a->questioncount)) {
$formattedname = get_string('categorynamewithcount', 'question', $a);
} else {
$formattedname = $a->name;
}
$categoriesarray[$contextstring][$cid] = $formattedname;
}
}
}
}
if ($popupform) {
$popupcats = array();
foreach ($categoriesarray as $contextstring => $optgroup) {
$group = array();
foreach ($optgroup as $key => $value) {
$key = str_replace($CFG->wwwroot, '', $key);
$group[$key] = $value;
}
$popupcats[] = array($contextstring => $group);
}
return $popupcats;
} else {
return $categoriesarray;
}
debugging('Function question_category_options() has been deprecated and moved to qbank_managecategories plugin,
Please use qbank_managecategories\helper::question_category_options() instead.', DEBUG_DEVELOPER);
return \qbank_managecategories\helper::question_category_options($contexts, $top, $currentcat,
$popupform, $nochildrenof, $escapecontextnames);
}

/**
* @deprecated since Moodle 4.0 MDL-71585
* @see qbank_managecategories\helper
* @todo Final deprecation on Moodle 4.4 MDL-72438
*/
function question_add_context_in_key($categories) {
$newcatarray = array();
foreach ($categories as $id => $category) {
$category->parent = "$category->parent,$category->contextid";
$category->id = "$category->id,$category->contextid";
$newcatarray["$id,$category->contextid"] = $category;
}
return $newcatarray;
debugging('Function question_add_context_in_key() has been deprecated and moved to qbank_managecategories plugin,
Please use qbank_managecategories\helper::question_add_context_in_key() instead.', DEBUG_DEVELOPER);
return \qbank_managecategories\helper::question_add_context_in_key($categories);
}

/**
Expand All @@ -1471,17 +1362,14 @@ function question_add_context_in_key($categories) {
* @param array $categories An array of question categories.
* @param boolean $escape Whether the returned name of the thing is to be HTML escaped or not.
* @return array The same question category list given to the function, with the top category names being translated.
* @deprecated since Moodle 4.0 MDL-71585
* @see qbank_managecategories\helper
* @todo Final deprecation on Moodle 4.4 MDL-72438
*/
function question_fix_top_names($categories, $escape = true) {

foreach ($categories as $id => $category) {
if ($category->parent == 0) {
$context = context::instance_by_id($category->contextid);
$categories[$id]->name = get_string('topfor', 'question', $context->get_context_name(false, false, $escape));
}
}

return $categories;
debugging('Function question_fix_top_names() has been deprecated and moved to qbank_managecategories plugin,
Please use qbank_managecategories\helper::question_fix_top_names() instead.', DEBUG_DEVELOPER);
return \qbank_managecategories\helper::question_fix_top_names($categories, $escape);
}

/**
Expand Down
Loading

0 comments on commit 3b903ae

Please sign in to comment.