1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace qbank_customfields;
|
|
|
18 |
|
|
|
19 |
use core_question\local\bank\column_base;
|
|
|
20 |
use core_question\local\bank\view;
|
|
|
21 |
use qbank_customfields\customfield\question_handler;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* A column type for the name of the question creator.
|
|
|
25 |
*
|
|
|
26 |
* @package qbank_customfields
|
|
|
27 |
* @copyright 2021 Catalyst IT Australia Pty Ltd
|
|
|
28 |
* @author Matt Porritt <mattp@catalyst-au.net>
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class custom_field_column extends column_base {
|
|
|
32 |
|
|
|
33 |
/** @var \core_customfield\field_controller The custom field this column is displaying. */
|
|
|
34 |
protected $field;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Constructor.
|
|
|
38 |
*
|
|
|
39 |
* @param view $qbank the question bank view we are helping to render.
|
|
|
40 |
* @param \core_customfield\field_controller $field The custom field this column is displaying.
|
|
|
41 |
*/
|
|
|
42 |
public function __construct(\core_question\local\bank\view $qbank, \core_customfield\field_controller $field) {
|
|
|
43 |
parent::__construct($qbank);
|
|
|
44 |
$this->field = $field;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
public static function from_column_name(view $view, string $columnname, bool $ingoremissing = false): ?custom_field_column {
|
|
|
48 |
$handler = question_handler::create();
|
|
|
49 |
foreach ($handler->get_fields() as $field) {
|
|
|
50 |
if ($field->get('shortname') == $columnname) {
|
|
|
51 |
return new static($view, $field);
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
if ($ingoremissing) {
|
|
|
55 |
return null;
|
|
|
56 |
} else {
|
|
|
57 |
throw new \coding_exception('Custom field ' . $columnname . ' does not exist.');
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Get the internal name for this column. Used as a CSS class name,
|
|
|
63 |
* and to store information about the current sort. Must match PARAM_ALPHA.
|
|
|
64 |
*
|
|
|
65 |
* @return string column name.
|
|
|
66 |
*/
|
|
|
67 |
public function get_name(): string {
|
|
|
68 |
return 'customfield';
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Get the name of this column. This must be unique.
|
|
|
73 |
* When using the inherited class to make many columns from one parent,
|
|
|
74 |
* ensure each instance returns a unique value.
|
|
|
75 |
*
|
|
|
76 |
* @return string The unique name;
|
|
|
77 |
*/
|
|
|
78 |
public function get_column_name(): string {
|
|
|
79 |
return $this->field->get('shortname');
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Title for this column. Not used if is_sortable returns an array.
|
|
|
84 |
*
|
|
|
85 |
* @return string
|
|
|
86 |
*/
|
|
|
87 |
public function get_title(): string {
|
|
|
88 |
return $this->field->get_formatted_name();
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Output the contents of this column.
|
|
|
93 |
*
|
|
|
94 |
* @param object $question the row from the $question table, augmented with extra information.
|
|
|
95 |
* @param string $rowclasses CSS class names that should be applied to this row of output.
|
|
|
96 |
*/
|
|
|
97 |
protected function display_content($question, $rowclasses): void {
|
|
|
98 |
$fieldhandler = $this->field->get_handler();
|
|
|
99 |
if ($fieldhandler->can_view($this->field, $question->id)) {
|
|
|
100 |
$fielddata = $fieldhandler->get_field_data($this->field, $question->id);
|
|
|
101 |
echo $fieldhandler->display_custom_field_table($fielddata);
|
|
|
102 |
} else {
|
|
|
103 |
echo '';
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public function get_extra_classes(): array {
|
|
|
108 |
return ['pr-3'];
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
}
|