\n";
+ }
+
+ /**
+ * Title for this column. Not used if is_sortable returns an array.
+ * @param object $question the row from the $question table, augmented with extra information.
+ * @param string $rowclasses CSS class names that should be applied to this row of output.
+ */
+ protected abstract function get_title();
+
+ /**
+ * @return string a fuller version of the name. Use this when get_title() returns
+ * something very short, and you want a longer version as a tool tip.
+ */
+ protected function get_title_tip() {
+ return '';
+ }
+
+ /**
+ * Get a link that changes the sort order, and indicates the current sort state.
+ * @param $name internal name used for this type of sorting.
+ * @param $currentsort the current sort order -1, 0, 1 for descending, none, ascending.
+ * @param $title the link text.
+ * @param $defaultreverse whether the default sort order for this column is descending, rather than ascending.
+ * @return string HTML fragment.
+ */
+ protected function make_sort_link($sort, $title, $tip, $defaultreverse = false) {
+ $currentsort = $this->qbank->get_primary_sort_order($sort);
+ $newsortreverse = $defaultreverse;
+ if ($currentsort) {
+ $newsortreverse = $currentsort > 0;
+ }
+ if (!$tip) {
+ $tip = $title;
+ }
+ if ($newsortreverse) {
+ $tip = get_string('sortbyxreverse', '', $tip);
+ } else {
+ $tip = get_string('sortbyx', '', $tip);
+ }
+ $link = '';
+ $link .= $title;
+ if ($currentsort) {
+ $link .= $this->get_sort_icon($currentsort < 0);
+ }
+ $link .= '';
+ return $link;
+ }
+
+ /**
+ * Get an icon representing the corrent sort state.
+ * @param $reverse sort is descending, not ascending.
+ * @return string HTML image tag.
+ */
+ protected function get_sort_icon($reverse) {
+ global $OUTPUT;
+ if ($reverse) {
+ return $OUTPUT->pix_icon('t/sort_desc', get_string('desc'), '', array('class' => 'iconsort'));
+ } else {
+ return $OUTPUT->pix_icon('t/sort_asc', get_string('asc'), '', array('class' => 'iconsort'));
+ }
+ }
+
+ /**
+ * Output this column.
+ * @param object $question the row from the $question table, augmented with extra information.
+ * @param string $rowclasses CSS class names that should be applied to this row of output.
+ */
+ public function display($question, $rowclasses) {
+ $this->display_start($question, $rowclasses);
+ $this->display_content($question, $rowclasses);
+ $this->display_end($question, $rowclasses);
+ }
+
+ /**
+ * Output the opening column tag. If it is set as heading, it will use
tag instead of
+ *
+ * @param stdClass $question
+ * @param array $rowclasses
+ */
+ protected function display_start($question, $rowclasses) {
+ $tag = 'td';
+ $attr = array('class' => $this->get_classes());
+ if ($this->isheading) {
+ $tag = 'th';
+ $attr['scope'] = 'row';
+ }
+ echo \html_writer::start_tag($tag, $attr);
+ }
+
+ /**
+ * @return string the CSS classes to apply to every cell in this column.
+ */
+ protected function get_classes() {
+ $classes = $this->get_extra_classes();
+ $classes[] = $this->get_name();
+ return implode(' ', $classes);
+ }
+
+ /**
+ * @param object $question the row from the $question table, augmented with extra information.
+ * @return string internal name for this column. Used as a CSS class name,
+ * and to store information about the current sort. Must match PARAM_ALPHA.
+ */
+ public abstract function get_name();
+
+ /**
+ * @return array any extra class names you would like applied to every cell in this column.
+ */
+ public function get_extra_classes() {
+ return array();
+ }
+
+ /**
+ * Output the contents of this column.
+ * @param object $question the row from the $question table, augmented with extra information.
+ * @param string $rowclasses CSS class names that should be applied to this row of output.
+ */
+ protected abstract function display_content($question, $rowclasses);
+
+ /**
+ * Output the closing column tag
+ *
+ * @param object $question
+ * @param string $rowclasses
+ */
+ protected function display_end($question, $rowclasses) {
+ $tag = 'td';
+ if ($this->isheading) {
+ $tag = 'th';
+ }
+ echo \html_writer::end_tag($tag);
+ }
+
+ /**
+ * Return an array 'table_alias' => 'JOIN clause' to bring in any data that
+ * this column required.
+ *
+ * The return values for all the columns will be checked. It is OK if two
+ * columns join in the same table with the same alias and identical JOIN clauses.
+ * If to columns try to use the same alias with different joins, you get an error.
+ * The only table included by default is the question table, which is aliased to 'q'.
+ *
+ * It is importnat that your join simply adds additional data (or NULLs) to the
+ * existing rows of the query. It must not cause additional rows.
+ *
+ * @return array 'table_alias' => 'JOIN clause'
+ */
+ public function get_extra_joins() {
+ return array();
+ }
+
+ /**
+ * @return array fields required. use table alias 'q' for the question table, or one of the
+ * ones from get_extra_joins. Every field requested must specify a table prefix.
+ */
+ public function get_required_fields() {
+ return array();
+ }
+
+ /**
+ * Can this column be sorted on? You can return either:
+ * + false for no (the default),
+ * + a field name, if sorting this column corresponds to sorting on that datbase field.
+ * + an array of subnames to sort on as follows
+ * return array(
+ * 'firstname' => array('field' => 'uc.firstname', 'title' => get_string('firstname')),
+ * 'lastname' => array('field' => 'uc.lastname', 'field' => get_string('lastname')),
+ * );
+ * As well as field, and field, you can also add 'revers' => 1 if you want the default sort
+ * order to be DESC.
+ * @return mixed as above.
+ */
+ public function is_sortable() {
+ return false;
+ }
+
+ /**
+ * Helper method for building sort clauses.
+ * @param bool $reverse whether the normal direction should be reversed.
+ * @param string $normaldir 'ASC' or 'DESC'
+ * @return string 'ASC' or 'DESC'
+ */
+ protected function sortorder($reverse) {
+ if ($reverse) {
+ return ' DESC';
+ } else {
+ return ' ASC';
+ }
+ }
+
+ /**
+ * @param $reverse Whether to sort in the reverse of the default sort order.
+ * @param $subsort if is_sortable returns an array of subnames, then this will be
+ * one of those. Otherwise will be empty.
+ * @return string some SQL to go in the order by clause.
+ */
+ public function sort_expression($reverse, $subsort) {
+ $sortable = $this->is_sortable();
+ if (is_array($sortable)) {
+ if (array_key_exists($subsort, $sortable)) {
+ return $sortable[$subsort]['field'] . $this->sortorder($reverse, !empty($sortable[$subsort]['reverse']));
+ } else {
+ throw new coding_exception('Unexpected $subsort type: ' . $subsort);
+ }
+ } else if ($sortable) {
+ return $sortable . $this->sortorder($reverse);
+ } else {
+ throw new coding_exception('sort_expression called on a non-sortable column.');
+ }
+ }
+}
diff --git a/question/classes/bank/copy_action_column.php b/question/classes/bank/copy_action_column.php
new file mode 100644
index 00000000000..9d0d890e64c
--- /dev/null
+++ b/question/classes/bank/copy_action_column.php
@@ -0,0 +1,48 @@
+.
+
+namespace core_question\bank;
+
+/**
+ * Question bank column for the duplicate action icon.
+ *
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+class copy_action_column extends action_column_base {
+ /** @var string avoids repeated calls to get_string('duplicate'). */
+ protected $strcopy;
+
+ public function init() {
+ parent::init();
+ $this->strcopy = get_string('duplicate');
+ }
+
+ public function get_name() {
+ return 'copyaction';
+ }
+
+ protected function display_content($question, $rowclasses) {
+ // To copy a question, you need permission to add a question in the same
+ // category as the existing question, and ability to access the details of
+ // the question being copied.
+ if (question_has_capability_on($question, 'add') &&
+ (question_has_capability_on($question, 'edit') || question_has_capability_on($question, 'view'))) {
+ $this->print_icon('t/copy', $this->strcopy, $this->qbank->copy_question_url($question->id));
+ }
+ }
+}
diff --git a/question/classes/bank/creator_name_column.php b/question/classes/bank/creator_name_column.php
new file mode 100644
index 00000000000..6b13efa7c03
--- /dev/null
+++ b/question/classes/bank/creator_name_column.php
@@ -0,0 +1,63 @@
+.
+
+
+namespace core_question\bank;
+
+/**
+ * A column type for the name of the question creator.
+ *
+ * @copyright 2009 Tim Hunt
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+class creator_name_column extends column_base {
+ public function get_name() {
+ return 'creatorname';
+ }
+
+ protected function get_title() {
+ return get_string('createdby', 'question');
+ }
+
+ protected function display_content($question, $rowclasses) {
+ if (!empty($question->creatorfirstname) && !empty($question->creatorlastname)) {
+ $u = new \stdClass();
+ $u = username_load_fields_from_object($u, $question, 'creator');
+ echo fullname($u);
+ }
+ }
+
+ public function get_extra_joins() {
+ return array('uc' => 'LEFT JOIN {user} uc ON uc.id = q.createdby');
+ }
+
+ public function get_required_fields() {
+ $allnames = get_all_user_name_fields();
+ $requiredfields = array();
+ foreach ($allnames as $allname) {
+ $requiredfields[] = 'uc.' . $allname . ' AS creator' . $allname;
+ }
+ return $requiredfields;
+ }
+
+ public function is_sortable() {
+ return array(
+ 'firstname' => array('field' => 'uc.firstname', 'title' => get_string('firstname')),
+ 'lastname' => array('field' => 'uc.lastname', 'title' => get_string('lastname')),
+ );
+ }
+}
diff --git a/question/classes/bank/delete_action_column.php b/question/classes/bank/delete_action_column.php
new file mode 100644
index 00000000000..7ef065e7ae1
--- /dev/null
+++ b/question/classes/bank/delete_action_column.php
@@ -0,0 +1,56 @@
+.
+
+namespace core_question\bank;
+
+/**
+ * action to delete (or hide) a question, or restore a previously hidden question.
+ *
+ * @copyright 2009 Tim Hunt
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+class delete_action_column extends action_column_base {
+ protected $strdelete;
+ protected $strrestore;
+
+ public function init() {
+ parent::init();
+ $this->strdelete = get_string('delete');
+ $this->strrestore = get_string('restore');
+ }
+
+ public function get_name() {
+ return 'deleteaction';
+ }
+
+ protected function display_content($question, $rowclasses) {
+ if (question_has_capability_on($question, 'edit')) {
+ if ($question->hidden) {
+ $url = new \moodle_url($this->qbank->base_url(), array('unhide' => $question->id, 'sesskey' => sesskey()));
+ $this->print_icon('t/restore', $this->strrestore, $url);
+ } else {
+ $url = new \moodle_url($this->qbank->base_url(), array('deleteselected' => $question->id, 'q' . $question->id => 1,
+ 'sesskey' => sesskey()));
+ $this->print_icon('t/delete', $this->strdelete, $url);
+ }
+ }
+ }
+
+ public function get_required_fields() {
+ return array('q.id', 'q.hidden');
+ }
+}
diff --git a/question/classes/bank/edit_action_column.php b/question/classes/bank/edit_action_column.php
new file mode 100644
index 00000000000..404596919c0
--- /dev/null
+++ b/question/classes/bank/edit_action_column.php
@@ -0,0 +1,46 @@
+.
+
+namespace core_question\bank;
+
+/**
+ * Base class for question bank columns that just contain an action icon.
+ *
+ * @copyright 2009 Tim Hunt
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class edit_action_column extends action_column_base {
+ protected $stredit;
+ protected $strview;
+
+ public function init() {
+ parent::init();
+ $this->stredit = get_string('edit');
+ $this->strview = get_string('view');
+ }
+
+ public function get_name() {
+ return 'editaction';
+ }
+
+ protected function display_content($question, $rowclasses) {
+ if (question_has_capability_on($question, 'edit')) {
+ $this->print_icon('t/edit', $this->stredit, $this->qbank->edit_question_url($question->id));
+ } else if (question_has_capability_on($question, 'view')) {
+ $this->print_icon('i/info', $this->strview, $this->qbank->edit_question_url($question->id));
+ }
+ }
+}
diff --git a/question/classes/bank/modifier_name_column.php b/question/classes/bank/modifier_name_column.php
new file mode 100644
index 00000000000..6981f34ca87
--- /dev/null
+++ b/question/classes/bank/modifier_name_column.php
@@ -0,0 +1,62 @@
+.
+
+
+namespace core_question\bank;
+
+/**
+ * A column type for the name of the question last modifier.
+ *
+ * @copyright 2009 Tim Hunt
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class modifier_name_column extends column_base {
+ public function get_name() {
+ return 'modifiername';
+ }
+
+ protected function get_title() {
+ return get_string('lastmodifiedby', 'question');
+ }
+
+ protected function display_content($question, $rowclasses) {
+ if (!empty($question->modifierfirstname) && !empty($question->modifierlastname)) {
+ $u = new \stdClass();
+ $u = username_load_fields_from_object($u, $question, 'modifier');
+ echo fullname($u);
+ }
+ }
+
+ public function get_extra_joins() {
+ return array('um' => 'LEFT JOIN {user} um ON um.id = q.modifiedby');
+ }
+
+ public function get_required_fields() {
+ $allnames = get_all_user_name_fields();
+ $requiredfields = array();
+ foreach ($allnames as $allname) {
+ $requiredfields[] = 'um.' . $allname . ' AS modifier' . $allname;
+ }
+ return $requiredfields;
+ }
+
+ public function is_sortable() {
+ return array(
+ 'firstname' => array('field' => 'um.firstname', 'title' => get_string('firstname')),
+ 'lastname' => array('field' => 'um.lastname', 'title' => get_string('lastname')),
+ );
+ }
+}
diff --git a/question/classes/bank/preview_action_column.php b/question/classes/bank/preview_action_column.php
new file mode 100644
index 00000000000..f31d5cc359f
--- /dev/null
+++ b/question/classes/bank/preview_action_column.php
@@ -0,0 +1,41 @@
+.
+
+namespace core_question\bank;
+
+/**
+ * Question bank columns for the preview action icon.
+ *
+ * @copyright 2009 Tim Hunt
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class preview_action_column extends action_column_base {
+ public function get_name() {
+ return 'previewaction';
+ }
+
+ protected function display_content($question, $rowclasses) {
+ global $PAGE;
+ if (question_has_capability_on($question, 'use')) {
+ echo $PAGE->get_renderer('core_question')->question_preview_link(
+ $question->id, $this->qbank->get_most_specific_context(), false);
+ }
+ }
+
+ public function get_required_fields() {
+ return array('q.id');
+ }
+}
diff --git a/question/classes/bank/question_name_column.php b/question/classes/bank/question_name_column.php
new file mode 100644
index 00000000000..05d18c6df76
--- /dev/null
+++ b/question/classes/bank/question_name_column.php
@@ -0,0 +1,65 @@
+.
+
+namespace core_question\bank;
+
+/**
+ * A column type for the name of the question name.
+ *
+ * @copyright 2009 Tim Hunt
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class question_name_column extends column_base {
+ protected $checkboxespresent = null;
+
+ public function get_name() {
+ return 'questionname';
+ }
+
+ protected function get_title() {
+ return get_string('question');
+ }
+
+ protected function label_for($question) {
+ if (is_null($this->checkboxespresent)) {
+ $this->checkboxespresent = $this->qbank->has_column('core_question\bank\checkbox_column');
+ }
+ if ($this->checkboxespresent) {
+ return 'checkq' . $question->id;
+ } else {
+ return '';
+ }
+ }
+
+ protected function display_content($question, $rowclasses) {
+ $labelfor = $this->label_for($question);
+ if ($labelfor) {
+ echo '';
+ }
+ }
+
+ public function get_required_fields() {
+ return array('q.id', 'q.name');
+ }
+
+ public function is_sortable() {
+ return 'q.name';
+ }
+}
diff --git a/question/classes/bank/question_text_row.php b/question/classes/bank/question_text_row.php
new file mode 100644
index 00000000000..cf9df126bff
--- /dev/null
+++ b/question/classes/bank/question_text_row.php
@@ -0,0 +1,62 @@
+.
+
+
+namespace core_question\bank;
+
+/**
+ * A column type for the name of the question name.
+ *
+ * @copyright 2009 Tim Hunt
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class question_text_row extends row_base {
+ protected $formatoptions;
+
+ protected function init() {
+ $this->formatoptions = new \stdClass();
+ $this->formatoptions->noclean = true;
+ $this->formatoptions->para = false;
+ }
+
+ public function get_name() {
+ return 'questiontext';
+ }
+
+ protected function get_title() {
+ return get_string('questiontext', 'question');
+ }
+
+ protected function display_content($question, $rowclasses) {
+ $text = question_rewrite_question_preview_urls($question->questiontext, $question->id,
+ $question->contextid, 'question', 'questiontext', $question->id,
+ $question->contextid, 'core_question');
+ $text = format_text($text, $question->questiontextformat,
+ $this->formatoptions);
+ if ($text == '') {
+ $text = ' ';
+ }
+ echo $text;
+ }
+
+ public function get_extra_joins() {
+ return array('qc' => 'JOIN {question_categories} qc ON qc.id = q.category');
+ }
+
+ public function get_required_fields() {
+ return array('q.id', 'q.questiontext', 'q.questiontextformat', 'qc.contextid');
+ }
+}
diff --git a/question/classes/bank/question_type_column.php b/question/classes/bank/question_type_column.php
new file mode 100644
index 00000000000..da7c83928ba
--- /dev/null
+++ b/question/classes/bank/question_type_column.php
@@ -0,0 +1,49 @@
+.
+
+namespace core_question\bank;
+
+/**
+ * A column type for the name of the question type.
+ *
+ * @copyright 2009 Tim Hunt
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class question_type_column extends column_base {
+ public function get_name() {
+ return 'qtype';
+ }
+
+ protected function get_title() {
+ return get_string('qtypeveryshort', 'question');
+ }
+
+ protected function get_title_tip() {
+ return get_string('questiontype', 'question');
+ }
+
+ protected function display_content($question, $rowclasses) {
+ echo print_question_icon($question);
+ }
+
+ public function get_required_fields() {
+ return array('q.qtype');
+ }
+
+ public function is_sortable() {
+ return 'q.qtype';
+ }
+}
diff --git a/question/classes/bank/row_base.php b/question/classes/bank/row_base.php
new file mode 100644
index 00000000000..a091c95baa4
--- /dev/null
+++ b/question/classes/bank/row_base.php
@@ -0,0 +1,42 @@
+.
+
+namespace core_question\bank;
+
+/**
+ * Base class for 'columns' that are actually displayed as a row following the main question row.
+ *
+ * @copyright 2009 Tim Hunt
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+abstract class row_base extends column_base {
+ public function is_extra_row() {
+ return true;
+ }
+
+ protected function display_start($question, $rowclasses) {
+ if ($rowclasses) {
+ echo '
\n";
+ }
+
+ /**
+ * Prints a form to choose categories
+ * @deprecated since Moodle 2.7 MDL-40313.
+ * @see \core_question\bank\search\condition
+ * @todo MDL-41978 This will be deleted in Moodle 2.8
+ */
+ protected function display_category_form($contexts, $pageurl, $current) {
+ global $OUTPUT;
+
+ debugging('display_category_form() is deprecated, please use ' .
+ '\core_question\bank\search\condition instead.', DEBUG_DEVELOPER);
+ // Get all the existing categories now.
+ echo '
\n";
+ }
+
+ /**
+ * Display the options form.
+ * @param bool $recurse no longer used.
+ * @param bool $showhidden no longer used.
+ * @param bool $showquestiontext whether to show the question text.
+ * @deprecated since Moodle 2.7 MDL-40313.
+ * @see display_options_form
+ * @todo MDL-41978 This will be deleted in Moodle 2.8
+ * @see \core_question\bank\search\condition
+ */
+ protected function display_options($recurse, $showhidden, $showquestiontext) {
+ debugging('display_options() is deprecated, please use display_options_form instead.', DEBUG_DEVELOPER);
+ return $this->display_options_form($showquestiontext);
+ }
+
+ /**
+ * Print a single option checkbox.
+ * @deprecated since Moodle 2.7 MDL-40313.
+ * @see \core_question\bank\search\condition
+ * @see html_writer::checkbox
+ * @todo MDL-41978 This will be deleted in Moodle 2.8
+ */
+ protected function display_category_form_checkbox($name, $value, $label) {
+ debugging('display_category_form_checkbox() is deprecated, ' .
+ 'please use \core_question\bank\search\condition instead.', DEBUG_DEVELOPER);
+ echo '
';
+ echo '';
+ echo '';
+ echo "
\n";
+ }
+
+ /**
+ * Display the form with options for which questions are displayed and how they are displayed.
+ * @param bool $showquestiontext Display the text of the question within the list.
+ */
+ protected function display_options_form($showquestiontext) {
+ global $PAGE;
+
+ echo '';
+ }
+
+ /**
+ * Print the "advanced" UI elements for the form to select which questions. Hidden by default.
+ */
+ protected function display_advanced_search_form() {
+ print_collapsible_region_start('', 'advancedsearch', get_string('advancedsearchoptions', 'question'),
+ 'question_bank_advanced_search');
+ foreach ($this->searchconditions as $searchcondition) {
+ echo $searchcondition->display_options_adv($this);
+ }
+ print_collapsible_region_end();
+ }
+
+ /**
+ * Display the checkbox UI for toggling the display of the question text in the list.
+ * @param bool $showquestiontext the current or default value for whether to display the text.
+ */
+ protected function display_showtext_checkbox($showquestiontext) {
+ echo '
\n";
- }
-
- /**
- * Title for this column. Not used if is_sortable returns an array.
- * @param object $question the row from the $question table, augmented with extra information.
- * @param string $rowclasses CSS class names that should be applied to this row of output.
- */
- protected abstract function get_title();
-
- /**
- * @return string a fuller version of the name. Use this when get_title() returns
- * something very short, and you want a longer version as a tool tip.
- */
- protected function get_title_tip() {
- return '';
- }
-
- /**
- * Get a link that changes the sort order, and indicates the current sort state.
- * @param $name internal name used for this type of sorting.
- * @param $currentsort the current sort order -1, 0, 1 for descending, none, ascending.
- * @param $title the link text.
- * @param $defaultreverse whether the default sort order for this column is descending, rather than ascending.
- * @return string HTML fragment.
- */
- protected function make_sort_link($sort, $title, $tip, $defaultreverse = false) {
- $currentsort = $this->qbank->get_primary_sort_order($sort);
- $newsortreverse = $defaultreverse;
- if ($currentsort) {
- $newsortreverse = $currentsort > 0;
- }
- if (!$tip) {
- $tip = $title;
- }
- if ($newsortreverse) {
- $tip = get_string('sortbyxreverse', '', $tip);
- } else {
- $tip = get_string('sortbyx', '', $tip);
- }
- $link = '';
- $link .= $title;
- if ($currentsort) {
- $link .= $this->get_sort_icon($currentsort < 0);
- }
- $link .= '';
- return $link;
- }
-
- /**
- * Get an icon representing the corrent sort state.
- * @param $reverse sort is descending, not ascending.
- * @return string HTML image tag.
- */
- protected function get_sort_icon($reverse) {
- global $OUTPUT;
- if ($reverse) {
- return $OUTPUT->pix_icon('t/sort_desc', get_string('desc'), '', array('class' => 'iconsort'));
- } else {
- return $OUTPUT->pix_icon('t/sort_asc', get_string('asc'), '', array('class' => 'iconsort'));
- }
- }
-
- /**
- * Output this column.
- * @param object $question the row from the $question table, augmented with extra information.
- * @param string $rowclasses CSS class names that should be applied to this row of output.
- */
- public function display($question, $rowclasses) {
- $this->display_start($question, $rowclasses);
- $this->display_content($question, $rowclasses);
- $this->display_end($question, $rowclasses);
- }
-
- /**
- * Output the opening column tag. If it is set as heading, it will use
tag instead of
- *
- * @param stdClass $question
- * @param array $rowclasses
- */
- protected function display_start($question, $rowclasses) {
- $tag = 'td';
- $attr = array('class' => $this->get_classes());
- if ($this->isheading) {
- $tag = 'th';
- $attr['scope'] = 'row';
- }
- echo html_writer::start_tag($tag, $attr);
- }
-
- /**
- * @return string the CSS classes to apply to every cell in this column.
- */
- protected function get_classes() {
- $classes = $this->get_extra_classes();
- $classes[] = $this->get_name();
- return implode(' ', $classes);
- }
-
- /**
- * @param object $question the row from the $question table, augmented with extra information.
- * @return string internal name for this column. Used as a CSS class name,
- * and to store information about the current sort. Must match PARAM_ALPHA.
- */
- public abstract function get_name();
-
- /**
- * @return array any extra class names you would like applied to every cell in this column.
- */
- public function get_extra_classes() {
- return array();
- }
-
- /**
- * Output the contents of this column.
- * @param object $question the row from the $question table, augmented with extra information.
- * @param string $rowclasses CSS class names that should be applied to this row of output.
- */
- protected abstract function display_content($question, $rowclasses);
-
- /**
- * Output the closing column tag
- *
- * @param object $question
- * @param string $rowclasses
- */
- protected function display_end($question, $rowclasses) {
- $tag = 'td';
- if ($this->isheading) {
- $tag = 'th';
- }
- echo html_writer::end_tag($tag);
- }
-
- /**
- * Return an array 'table_alias' => 'JOIN clause' to bring in any data that
- * this column required.
- *
- * The return values for all the columns will be checked. It is OK if two
- * columns join in the same table with the same alias and identical JOIN clauses.
- * If to columns try to use the same alias with different joins, you get an error.
- * The only table included by default is the question table, which is aliased to 'q'.
- *
- * It is importnat that your join simply adds additional data (or NULLs) to the
- * existing rows of the query. It must not cause additional rows.
- *
- * @return array 'table_alias' => 'JOIN clause'
- */
- public function get_extra_joins() {
- return array();
- }
-
- /**
- * @return array fields required. use table alias 'q' for the question table, or one of the
- * ones from get_extra_joins. Every field requested must specify a table prefix.
- */
- public function get_required_fields() {
- return array();
- }
-
- /**
- * Can this column be sorted on? You can return either:
- * + false for no (the default),
- * + a field name, if sorting this column corresponds to sorting on that datbase field.
- * + an array of subnames to sort on as follows
- * return array(
- * 'firstname' => array('field' => 'uc.firstname', 'title' => get_string('firstname')),
- * 'lastname' => array('field' => 'uc.lastname', 'field' => get_string('lastname')),
- * );
- * As well as field, and field, you can also add 'revers' => 1 if you want the default sort
- * order to be DESC.
- * @return mixed as above.
- */
- public function is_sortable() {
- return false;
- }
-
- /**
- * Helper method for building sort clauses.
- * @param bool $reverse whether the normal direction should be reversed.
- * @param string $normaldir 'ASC' or 'DESC'
- * @return string 'ASC' or 'DESC'
- */
- protected function sortorder($reverse) {
- if ($reverse) {
- return ' DESC';
- } else {
- return ' ASC';
- }
- }
-
- /**
- * @param $reverse Whether to sort in the reverse of the default sort order.
- * @param $subsort if is_sortable returns an array of subnames, then this will be
- * one of those. Otherwise will be empty.
- * @return string some SQL to go in the order by clause.
- */
- public function sort_expression($reverse, $subsort) {
- $sortable = $this->is_sortable();
- if (is_array($sortable)) {
- if (array_key_exists($subsort, $sortable)) {
- return $sortable[$subsort]['field'] . $this->sortorder($reverse, !empty($sortable[$subsort]['reverse']));
- } else {
- throw new coding_exception('Unexpected $subsort type: ' . $subsort);
- }
- } else if ($sortable) {
- return $sortable . $this->sortorder($reverse);
- } else {
- throw new coding_exception('sort_expression called on a non-sortable column.');
- }
- }
-}
-
+class_alias('core_question\bank\column_base', 'question_bank_column_base', true);
/**
* A column with a checkbox for each question with name q{questionid}.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @deprecated since Moodle 2.7 MDL-40457
*/
-class question_bank_checkbox_column extends question_bank_column_base {
- protected $strselect;
- protected $firstrow = true;
-
- public function init() {
- $this->strselect = get_string('select');
- }
-
- public function get_name() {
- return 'checkbox';
- }
-
- protected function get_title() {
- return '';
- }
-
- protected function get_title_tip() {
- return get_string('selectquestionsforbulk', 'question');
- }
-
- protected function display_content($question, $rowclasses) {
- global $PAGE;
- echo '';
- if ($this->firstrow) {
- $PAGE->requires->strings_for_js(array('selectall', 'deselectall'), 'moodle');
- $PAGE->requires->yui_module('moodle-question-qbankmanager', 'M.question.qbankmanager.init',
- array('checkq' . $question->id));
- $this->firstrow = false;
- }
- }
-
- public function get_required_fields() {
- return array('q.id');
- }
-}
-
+class_alias('core_question\bank\checkbox_column', 'question_bank_checkbox_column', true);
/**
* A column type for the name of the question type.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @deprecated since Moodle 2.7 MDL-40457
*/
-class question_bank_question_type_column extends question_bank_column_base {
- public function get_name() {
- return 'qtype';
- }
-
- protected function get_title() {
- return get_string('qtypeveryshort', 'question');
- }
-
- protected function get_title_tip() {
- return get_string('questiontype', 'question');
- }
-
- protected function display_content($question, $rowclasses) {
- echo print_question_icon($question);
- }
-
- public function get_required_fields() {
- return array('q.qtype');
- }
-
- public function is_sortable() {
- return 'q.qtype';
- }
-}
+class_alias('core_question\bank\question_type_column', 'question_bank_question_type_column', true);
/**
@@ -492,48 +157,9 @@ class question_bank_question_type_column extends question_bank_column_base {
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @deprecated since Moodle 2.7 MDL-40457
*/
-class question_bank_question_name_column extends question_bank_column_base {
- protected $checkboxespresent = null;
-
- public function get_name() {
- return 'questionname';
- }
-
- protected function get_title() {
- return get_string('question');
- }
-
- protected function label_for($question) {
- if (is_null($this->checkboxespresent)) {
- $this->checkboxespresent = $this->qbank->has_column('checkbox');
- }
- if ($this->checkboxespresent) {
- return 'checkq' . $question->id;
- } else {
- return '';
- }
- }
-
- protected function display_content($question, $rowclasses) {
- $labelfor = $this->label_for($question);
- if ($labelfor) {
- echo '';
- }
- }
-
- public function get_required_fields() {
- return array('q.id', 'q.name');
- }
-
- public function is_sortable() {
- return 'q.name';
- }
-}
+class_alias('core_question\bank\question_name_column', 'question_bank_question_name_column', true);
/**
@@ -541,44 +167,9 @@ class question_bank_question_name_column extends question_bank_column_base {
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @deprecated since Moodle 2.7 MDL-40457
*/
-class question_bank_creator_name_column extends question_bank_column_base {
- public function get_name() {
- return 'creatorname';
- }
-
- protected function get_title() {
- return get_string('createdby', 'question');
- }
-
- protected function display_content($question, $rowclasses) {
- if (!empty($question->creatorfirstname) && !empty($question->creatorlastname)) {
- $u = new stdClass();
- $u = username_load_fields_from_object($u, $question, 'creator');
- echo fullname($u);
- }
- }
-
- public function get_extra_joins() {
- return array('uc' => 'LEFT JOIN {user} uc ON uc.id = q.createdby');
- }
-
- public function get_required_fields() {
- $allnames = get_all_user_name_fields();
- $requiredfields = array();
- foreach ($allnames as $allname) {
- $requiredfields[] = 'uc.' . $allname . ' AS creator' . $allname;
- }
- return $requiredfields;
- }
-
- public function is_sortable() {
- return array(
- 'firstname' => array('field' => 'uc.firstname', 'title' => get_string('firstname')),
- 'lastname' => array('field' => 'uc.lastname', 'title' => get_string('lastname')),
- );
- }
-}
+class_alias('core_question\bank\creator_name_column', 'question_bank_creator_name_column', true);
/**
@@ -586,44 +177,9 @@ class question_bank_creator_name_column extends question_bank_column_base {
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @deprecated since Moodle 2.7 MDL-40457
*/
-class question_bank_modifier_name_column extends question_bank_column_base {
- public function get_name() {
- return 'modifiername';
- }
-
- protected function get_title() {
- return get_string('lastmodifiedby', 'question');
- }
-
- protected function display_content($question, $rowclasses) {
- if (!empty($question->modifierfirstname) && !empty($question->modifierlastname)) {
- $u = new stdClass();
- $u = username_load_fields_from_object($u, $question, 'modifier');
- echo fullname($u);
- }
- }
-
- public function get_extra_joins() {
- return array('um' => 'LEFT JOIN {user} um ON um.id = q.modifiedby');
- }
-
- public function get_required_fields() {
- $allnames = get_all_user_name_fields();
- $requiredfields = array();
- foreach ($allnames as $allname) {
- $requiredfields[] = 'um.' . $allname . ' AS modifier' . $allname;
- }
- return $requiredfields;
- }
-
- public function is_sortable() {
- return array(
- 'firstname' => array('field' => 'um.firstname', 'title' => get_string('firstname')),
- 'lastname' => array('field' => 'um.lastname', 'title' => get_string('lastname')),
- );
- }
-}
+class_alias('core_question\bank\modifier_name_column', 'question_bank_modifier_name_column', true);
/**
@@ -631,28 +187,9 @@ class question_bank_modifier_name_column extends question_bank_column_base {
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @deprecated since Moodle 2.7 MDL-40457
*/
-abstract class question_bank_action_column_base extends question_bank_column_base {
-
- protected function get_title() {
- return ' ';
- }
-
- public function get_extra_classes() {
- return array('iconcol');
- }
-
- protected function print_icon($icon, $title, $url) {
- global $OUTPUT;
- echo '
- ';
- }
-
- public function get_required_fields() {
- // createdby is required for permission checks.
- return array('q.id', 'q.createdby');
- }
-}
+class_alias('core_question\bank\action_column_base', 'question_bank_action_column_base', true);
/**
@@ -660,87 +197,27 @@ abstract class question_bank_action_column_base extends question_bank_column_bas
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @deprecated since Moodle 2.7 MDL-40457
*/
-class question_bank_edit_action_column extends question_bank_action_column_base {
- protected $stredit;
- protected $strview;
-
- public function init() {
- parent::init();
- $this->stredit = get_string('edit');
- $this->strview = get_string('view');
- }
-
- public function get_name() {
- return 'editaction';
- }
-
- protected function display_content($question, $rowclasses) {
- if (question_has_capability_on($question, 'edit')) {
- $this->print_icon('t/edit', $this->stredit, $this->qbank->edit_question_url($question->id));
- } else if (question_has_capability_on($question, 'view')) {
- $this->print_icon('i/info', $this->strview, $this->qbank->edit_question_url($question->id));
- }
- }
-}
+class_alias('core_question\bank\edit_action_column', 'question_bank_edit_action_column', true);
/**
* Question bank column for the duplicate action icon.
*
* @copyright 2013 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @deprecated since Moodle 2.7 MDL-40457
*/
-class question_bank_copy_action_column extends question_bank_action_column_base {
- /** @var string avoids repeated calls to get_string('duplicate'). */
- protected $strcopy;
-
- public function init() {
- parent::init();
- $this->strcopy = get_string('duplicate');
- }
-
- public function get_name() {
- return 'copyaction';
- }
-
- protected function display_content($question, $rowclasses) {
- // To copy a question, you need permission to add a question in the same
- // category as the existing question, and ability to access the details of
- // the question being copied.
- if (question_has_capability_on($question, 'add') &&
- (question_has_capability_on($question, 'edit') || question_has_capability_on($question, 'view'))) {
- $this->print_icon('t/copy', $this->strcopy, $this->qbank->copy_question_url($question->id));
- }
- }
-}
+class_alias('core_question\bank\copy_action_column', 'question_bank_copy_action_column', true);
/**
* Question bank columns for the preview action icon.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @deprecated since Moodle 2.7 MDL-40457
*/
-class question_bank_preview_action_column extends question_bank_action_column_base {
- public function init() {
- parent::init();
- }
-
- public function get_name() {
- return 'previewaction';
- }
-
- protected function display_content($question, $rowclasses) {
- global $PAGE;
- if (question_has_capability_on($question, 'use')) {
- echo $PAGE->get_renderer('core_question')->question_preview_link(
- $question->id, $this->qbank->get_most_specific_context(), false);
- }
- }
-
- public function get_required_fields() {
- return array('q.id');
- }
-}
+class_alias('core_question\bank\preview_action_column', 'question_bank_preview_action_column', true);
/**
@@ -748,994 +225,34 @@ class question_bank_preview_action_column extends question_bank_action_column_ba
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @deprecated since Moodle 2.7 MDL-40457
*/
-class question_bank_delete_action_column extends question_bank_action_column_base {
- protected $strdelete;
- protected $strrestore;
-
- public function init() {
- parent::init();
- $this->strdelete = get_string('delete');
- $this->strrestore = get_string('restore');
- }
-
- public function get_name() {
- return 'deleteaction';
- }
-
- protected function display_content($question, $rowclasses) {
- if (question_has_capability_on($question, 'edit')) {
- if ($question->hidden) {
- $url = new moodle_url($this->qbank->base_url(), array('unhide' => $question->id, 'sesskey'=>sesskey()));
- $this->print_icon('t/restore', $this->strrestore, $url);
- } else {
- $url = new moodle_url($this->qbank->base_url(), array('deleteselected' => $question->id, 'q' . $question->id => 1, 'sesskey'=>sesskey()));
- $this->print_icon('t/delete', $this->strdelete, $url);
- }
- }
- }
-
- public function get_required_fields() {
- return array('q.id', 'q.hidden');
- }
-}
+class_alias('core_question\bank\delete_action_column', 'question_bank_delete_action_column', true);
/**
* Base class for 'columns' that are actually displayed as a row following the main question row.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @deprecated since Moodle 2.7 MDL-40457
*/
-abstract class question_bank_row_base extends question_bank_column_base {
- public function is_extra_row() {
- return true;
- }
-
- protected function display_start($question, $rowclasses) {
- if ($rowclasses) {
- echo '
\n";
- }
-
- /**
- * Prints a form to choose categories
- * @deprecated since Moodle 2.7 MDL-40313.
- * @see \core_question\bank\search\condition
- * @todo MDL-41978 This will be deleted in Moodle 2.8
- */
- protected function display_category_form($contexts, $pageurl, $current) {
- global $OUTPUT;
-
- debugging('display_category_form() is deprecated, please use ' .
- '\core_question\bank\search\condition instead.', DEBUG_DEVELOPER);
- /// Get all the existing categories now
- echo '
\n";
- }
-
- /**
- * Display the options form.
- * @param bool $recurse no longer used.
- * @param bool $showhidden no longer used.
- * @param bool $showquestiontext whether to show the question text.
- * @deprecated since Moodle 2.7 MDL-40313.
- * @see display_options_form
- * @todo MDL-41978 This will be deleted in Moodle 2.8
- * @see \core_question\bank\search\condition
- */
- protected function display_options($recurse, $showhidden, $showquestiontext) {
- debugging('display_options() is deprecated, please use display_options_form instead.', DEBUG_DEVELOPER);
- return $this->display_options_form($showquestiontext);
- }
-
- /**
- * Print a single option checkbox.
- * @deprecated since Moodle 2.7 MDL-40313.
- * @see \core_question\bank\search\condition
- * @see html_writer::checkbox
- * @todo MDL-41978 This will be deleted in Moodle 2.8
- */
- protected function display_category_form_checkbox($name, $value, $label) {
- debugging('display_category_form_checkbox() is deprecated, ' .
- 'please use \core_question\bank\search\condition instead.', DEBUG_DEVELOPER);
- echo '
';
- echo '';
- echo '';
- echo "
\n";
- }
-
- /**
- * Display the form with options for which questions are displayed and how they are displayed.
- * @param bool $showquestiontext Display the text of the question within the list.
- */
- protected function display_options_form($showquestiontext) {
- global $PAGE;
-
- echo '';
- }
-
- /**
- * Print the "advanced" UI elements for the form to select which questions. Hidden by default.
- */
- protected function display_advanced_search_form() {
- print_collapsible_region_start('', 'advancedsearch', get_string('advancedsearchoptions', 'question'),
- 'question_bank_advanced_search');
- foreach ($this->searchconditions as $searchcondition) {
- echo $searchcondition->display_options_adv($this);
- }
- print_collapsible_region_end();
- }
-
- /**
- * Display the checkbox UI for toggling the display of the question text in the list.
- * @param bool $showquestiontext the current or default value for whether to display the text.
- */
- protected function display_showtext_checkbox($showquestiontext) {
- echo '
\n";
- foreach ($this->extrarows as $row) {
- $row->display($question, $rowclasses);
- }
- }
-
- public function process_actions() {
- global $CFG, $DB;
- /// Now, check for commands on this page and modify variables as necessary
- if (optional_param('move', false, PARAM_BOOL) and confirm_sesskey()) {
- // Move selected questions to new category
- $category = required_param('category', PARAM_SEQUENCE);
- list($tocategoryid, $contextid) = explode(',', $category);
- if (! $tocategory = $DB->get_record('question_categories', array('id' => $tocategoryid, 'contextid' => $contextid))) {
- print_error('cannotfindcate', 'question');
- }
- $tocontext = context::instance_by_id($contextid);
- require_capability('moodle/question:add', $tocontext);
- $rawdata = (array) data_submitted();
- $questionids = array();
- foreach ($rawdata as $key => $value) { // Parse input for question ids
- if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
- $key = $matches[1];
- $questionids[] = $key;
- }
- }
- if ($questionids) {
- list($usql, $params) = $DB->get_in_or_equal($questionids);
- $sql = "";
- $questions = $DB->get_records_sql("
- SELECT q.*, c.contextid
- FROM {question} q
- JOIN {question_categories} c ON c.id = q.category
- WHERE q.id $usql", $params);
- foreach ($questions as $question){
- question_require_capability_on($question, 'move');
- }
- question_move_questions_to_category($questionids, $tocategory->id);
- redirect($this->baseurl->out(false,
- array('category' => "$tocategoryid,$contextid")));
- }
- }
-
- if (optional_param('deleteselected', false, PARAM_BOOL)) { // delete selected questions from the category
- if (($confirm = optional_param('confirm', '', PARAM_ALPHANUM)) and confirm_sesskey()) { // teacher has already confirmed the action
- $deleteselected = required_param('deleteselected', PARAM_RAW);
- if ($confirm == md5($deleteselected)) {
- if ($questionlist = explode(',', $deleteselected)) {
- // for each question either hide it if it is in use or delete it
- foreach ($questionlist as $questionid) {
- $questionid = (int)$questionid;
- question_require_capability_on($questionid, 'edit');
- if (questions_in_use(array($questionid))) {
- $DB->set_field('question', 'hidden', 1, array('id' => $questionid));
- } else {
- question_delete_question($questionid);
- }
- }
- }
- redirect($this->baseurl);
- } else {
- print_error('invalidconfirm', 'question');
- }
- }
- }
-
- // Unhide a question
- if(($unhide = optional_param('unhide', '', PARAM_INT)) and confirm_sesskey()) {
- question_require_capability_on($unhide, 'edit');
- $DB->set_field('question', 'hidden', 0, array('id' => $unhide));
-
- // Purge these questions from the cache.
- question_bank::notify_question_edited($unhide);
-
- redirect($this->baseurl);
- }
- }
-
- public function process_actions_needing_ui() {
- global $DB, $OUTPUT;
- if (optional_param('deleteselected', false, PARAM_BOOL)) {
- // make a list of all the questions that are selected
- $rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
- $questionlist = ''; // comma separated list of ids of questions to be deleted
- $questionnames = ''; // string with names of questions separated by with
- // an asterix in front of those that are in use
- $inuse = false; // set to true if at least one of the questions is in use
- foreach ($rawquestions as $key => $value) { // Parse input for question ids
- if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
- $key = $matches[1];
- $questionlist .= $key.',';
- question_require_capability_on($key, 'edit');
- if (questions_in_use(array($key))) {
- $questionnames .= '* ';
- $inuse = true;
- }
- $questionnames .= $DB->get_field('question', 'name', array('id' => $key)) . ' ';
- }
- }
- if (!$questionlist) { // no questions were selected
- redirect($this->baseurl);
- }
- $questionlist = rtrim($questionlist, ',');
-
- // Add an explanation about questions in use
- if ($inuse) {
- $questionnames .= ' '.get_string('questionsinuse', 'question');
- }
- $baseurl = new moodle_url('edit.php', $this->baseurl->params());
- $deleteurl = new moodle_url($baseurl, array('deleteselected'=>$questionlist, 'confirm'=>md5($questionlist), 'sesskey'=>sesskey()));
-
- echo $OUTPUT->confirm(get_string('deletequestionscheck', 'question', $questionnames), $deleteurl, $baseurl);
-
- return true;
- }
- }
-
- /**
- * Add another search control to this view.
- * @param \core_question\bank\search\condition $searchcondition the condition to add.
- */
- public function add_searchcondition($searchcondition) {
- $this->searchconditions[] = $searchcondition;
- }
-}
+class_alias('core_question\bank\view', 'question_bank_view', true);
/**
* Common setup for all pages for editing questions.
@@ -1819,7 +336,7 @@ function question_edit_setup($edittab, $baseurl, $requirecmid = false, $requirec
for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
$param = 'qbs' . $i;
- if (!$sort = optional_param($param, '', PARAM_ALPHAEXT)) {
+ if (!$sort = optional_param($param, '', PARAM_TEXT)) {
break;
}
$thispageurl->param($param, $sort);
diff --git a/question/upgrade.txt b/question/upgrade.txt
index f755b385ea9..4abcef00881 100644
--- a/question/upgrade.txt
+++ b/question/upgrade.txt
@@ -17,6 +17,14 @@ This files describes API changes for code that uses the question API.
To add filters, local plugins can now implement the function local_[pluginname]_get_question_bank_search_conditions,
+2) To make columns available to question_bank_view, plugins can extend core_question\bank\column_base.
+ Users may choose to display additional columns by setting $CFG->questionbankcolumns to a comma-delimited list of columns.
+
+3) The subsort separator has changed from _ to - in order to distinuguish subsorts vs frankenstyle component separators.
+
+4) Because of the move to autoloading, $knowncolumntypes and known_field_types() are no longer used.
+
+5) question_bank_column_base and it's derived classes have been namespaced to core_question\bank\column_base.
=== 2.6 ===