Skip to content

Commit

Permalink
MDL-40457 Question Bank: Allow plugins to add columns to question ban…
Browse files Browse the repository at this point in the history
…k view

Allows plugins to add columns to the question bank view by extending core_question\bank\column_base
Columns to display are set in $CFG->questionbankcolumns. Columns are namespaced and autoloaded to support this.
  • Loading branch information
sensei-hacker committed Jun 30, 2014
1 parent 7784c3a commit 17f229f
Show file tree
Hide file tree
Showing 18 changed files with 1,935 additions and 1,526 deletions.
35 changes: 22 additions & 13 deletions mod/quiz/editlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ public function get_required_fields() {
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quiz_question_bank_view extends question_bank_view {
class quiz_question_bank_view extends core_question\bank\view {
protected $quizhasattempts = false;
/** @var object the quiz settings. */
protected $quiz = false;
Expand All @@ -1123,16 +1123,27 @@ public function __construct($contexts, $pageurl, $course, $cm, $quiz) {
$this->quiz = $quiz;
}

protected function known_field_types() {
$types = parent::known_field_types();
$types[] = new question_bank_add_to_quiz_action_column($this);
$types[] = new question_bank_question_name_text_column($this);
return $types;
}

protected function wanted_columns() {
return array('addtoquizaction', 'checkbox', 'qtype', 'questionnametext',
'editaction', 'copyaction', 'previewaction');
global $CFG;

if (empty($CFG->quizquestionbankcolumns)) {
$quizquestionbankcolumns = array('add_to_quiz_action_column', 'checkbox_column', 'question_type_column',
'question_name_column', 'edit_action_column', 'preview_action_column');
} else {
$quizquestionbankcolumns = explode(',', $CFG->quizquestionbankcolumns);
}

foreach ($quizquestionbankcolumns as $fullname) {
if (! class_exists($fullname)) {
if (class_exists('question_bank_' . $fullname)) {
$fullname = 'question_bank_' . $fullname;
} else {
throw new coding_exception("No such class exists: $fullname");
}
}
$this->requiredcolumns[$fullname] = new $fullname($this);
}
return $this->requiredcolumns;
}

/**
Expand All @@ -1145,9 +1156,7 @@ protected function heading_column() {
}

protected function default_sort() {
$this->requiredcolumns['qtype'] = $this->knowncolumntypes['qtype'];
$this->requiredcolumns['questionnametext'] = $this->knowncolumntypes['questionnametext'];
return array('qtype' => 1, 'questionnametext' => 1);
return array('question_bank_question_type_column' => 1, 'question_bank_question_name_column' => 1);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions question/classes/bank/action_column_base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_question\bank;

/**
* A base class for actions that are an icon that lets you manipulate the question in some way.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

abstract class action_column_base extends column_base {

protected function get_title() {
return '&#160;';
}

public function get_extra_classes() {
return array('iconcol');
}

protected function print_icon($icon, $title, $url) {
global $OUTPUT;
echo '<a title="' . $title . '" href="' . $url . '">
<img src="' . $OUTPUT->pix_url($icon) . '" class="iconsmall" alt="' . $title . '" /></a>';
}

public function get_required_fields() {
// Createdby is required for permission checks.
return array('q.id', 'q.createdby');
}
}
60 changes: 60 additions & 0 deletions question/classes/bank/checkbox_column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_question\bank;

/**
* 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
*/
class checkbox_column extends 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 '<input type="checkbox" disabled="disabled" id="qbheadercheckbox" />';
}

protected function get_title_tip() {
return get_string('selectquestionsforbulk', 'question');
}

protected function display_content($question, $rowclasses) {
global $PAGE;
echo '<input title="' . $this->strselect . '" type="checkbox" name="q' .
$question->id . '" id="checkq' . $question->id . '" value="1"/>';
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');
}
}
Loading

0 comments on commit 17f229f

Please sign in to comment.