first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,111 @@
<?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 qbank_customfields;
use core_question\local\bank\column_base;
use core_question\local\bank\view;
use qbank_customfields\customfield\question_handler;
/**
* A column type for the name of the question creator.
*
* @package qbank_customfields
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Matt Porritt <mattp@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_field_column extends column_base {
/** @var \core_customfield\field_controller The custom field this column is displaying. */
protected $field;
/**
* Constructor.
*
* @param view $qbank the question bank view we are helping to render.
* @param \core_customfield\field_controller $field The custom field this column is displaying.
*/
public function __construct(\core_question\local\bank\view $qbank, \core_customfield\field_controller $field) {
parent::__construct($qbank);
$this->field = $field;
}
public static function from_column_name(view $view, string $columnname, bool $ingoremissing = false): ?custom_field_column {
$handler = question_handler::create();
foreach ($handler->get_fields() as $field) {
if ($field->get('shortname') == $columnname) {
return new static($view, $field);
}
}
if ($ingoremissing) {
return null;
} else {
throw new \coding_exception('Custom field ' . $columnname . ' does not exist.');
}
}
/**
* Get the internal name for this column. Used as a CSS class name,
* and to store information about the current sort. Must match PARAM_ALPHA.
*
* @return string column name.
*/
public function get_name(): string {
return 'customfield';
}
/**
* Get the name of this column. This must be unique.
* When using the inherited class to make many columns from one parent,
* ensure each instance returns a unique value.
*
* @return string The unique name;
*/
public function get_column_name(): string {
return $this->field->get('shortname');
}
/**
* Title for this column. Not used if is_sortable returns an array.
*
* @return string
*/
public function get_title(): string {
return $this->field->get_formatted_name();
}
/**
* 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 function display_content($question, $rowclasses): void {
$fieldhandler = $this->field->get_handler();
if ($fieldhandler->can_view($this->field, $question->id)) {
$fielddata = $fieldhandler->get_field_data($this->field, $question->id);
echo $fieldhandler->display_custom_field_table($fielddata);
} else {
echo '';
}
}
public function get_extra_classes(): array {
return ['pr-3'];
}
}
@@ -0,0 +1,331 @@
<?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 qbank_customfields\customfield;
use core_customfield\api;
use core_customfield\field_controller;
use core_customfield\output\field_data;
/**
* Question handler for custom fields.
*
* @package qbank_customfields
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Matt Porritt <mattp@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_handler extends \core_customfield\handler {
/**
* @var question_handler
*/
static protected $singleton;
/**
* @var \context
*/
protected $parentcontext;
/** @var int Field is displayed in the question display and question preview, visible to everybody */
const VISIBLETOALL = 2;
/** @var int Field is displayed in the question display and question preview but only for "teachers" */
const VISIBLETOTEACHERS = 1;
/** @var int Field is not displayed in the question display and question preview */
const NOTVISIBLE = 0;
/**
* Creates the custom field handler and returns a singleton.
* Itemid is always zero as the custom fields are the same
* for every question across the system.
*
* @param int $itemid Always zero.
* @return \qbank_customfields\customfield\question_handler
*/
public static function create(int $itemid = 0): \core_customfield\handler {
if (static::$singleton === null) {
self::$singleton = new static(0);
}
return self::$singleton;
}
/**
* Run reset code after unit tests to reset the singleton usage.
*/
public static function reset_caches(): void {
if (!PHPUNIT_TEST) {
throw new \coding_exception('This feature is only intended for use in unit tests');
}
static::$singleton = null;
}
/**
* The current user can configure custom fields on this component.
*
* @return bool true if the current can configure custom fields, false otherwise
*/
public function can_configure(): bool {
return has_capability('qbank/customfields:configurecustomfields', $this->get_configuration_context());
}
/**
* The current user can edit custom fields for the given question.
*
* @param field_controller $field
* @param int $instanceid id of the question to test edit permission
* @return bool true if the current can edit custom fields, false otherwise
*/
public function can_edit(field_controller $field, int $instanceid = 0): bool {
if ($instanceid) {
$context = $this->get_instance_context($instanceid);
} else {
$context = $this->get_parent_context();
}
return (!$field->get_configdata_property('locked') ||
has_capability('qbank/customfields:changelockedcustomfields', $context));
}
/**
* The current user can view custom fields for the given question.
*
* @param field_controller $field
* @param int $instanceid id of the question to test edit permission
* @return bool true if the current can edit custom fields, false otherwise
*/
public function can_view(field_controller $field, int $instanceid): bool {
$visibility = $field->get_configdata_property('visibility');
if ($visibility == self::NOTVISIBLE) {
return false;
} else if ($visibility == self::VISIBLETOTEACHERS) {
return has_capability('qbank/customfields:viewhiddencustomfields', $this->get_instance_context($instanceid));
} else {
return true;
}
}
/**
* Determine if the current user can view custom field in their given context.
* This determines if the user can see the field at all not just the field for
* a particular instance.
* Used primarily in showing or not the field in the question bank table.
*
* @param field_controller $field The field trying to be viewed.
* @param \context $context The context the field is being displayed in.
* @return bool true if the current can edit custom fields, false otherwise.
*/
public function can_view_type(field_controller $field, \context $context): bool {
$visibility = $field->get_configdata_property('visibility');
if ($visibility == self::NOTVISIBLE) {
return false;
} else if ($visibility == self::VISIBLETOTEACHERS) {
return has_capability('qbank/customfields:viewhiddencustomfields', $context);
} else {
return true;
}
}
/**
* Sets parent context for the question.
*
* This may be needed when question is being created, there is no question context but we need to check capabilities
*
* @param \context $context
*/
public function set_parent_context(\context $context): void {
$this->parentcontext = $context;
}
/**
* Returns the parent context for the question.
*
* @return \context
*/
protected function get_parent_context(): \context {
if ($this->parentcontext) {
return $this->parentcontext;
} else {
return \context_system::instance();
}
}
/**
* Context that should be used for new categories created by this handler.
*
* @return \context the context for configuration
*/
public function get_configuration_context(): \context {
return \context_system::instance();
}
/**
* URL for configuration page for the fields for the question custom fields.
*
* @return \moodle_url The URL to configure custom fields for this component
*/
public function get_configuration_url(): \moodle_url {
return new \moodle_url('/question/customfield.php');
}
/**
* Returns the context for the data associated with the given instanceid.
*
* @param int $instanceid id of the record to get the context for
* @return \context the context for the given record
* @throws \coding_exception
*/
public function get_instance_context(int $instanceid = 0): \context {
if ($instanceid > 0) {
$questiondata = \question_bank::load_question_data($instanceid);
$contextid = $questiondata->contextid;
$context = \context::instance_by_id($contextid);
return $context;
} else {
throw new \coding_exception('Instance id must be provided.');
}
}
/**
* Given a field and instance id get all the filed data.
*
* @param field_controller $field The field to get the data for.
* @param int $instanceid The instance id to get the data for.
* @return \core_customfield\data_controller The fetched data.
*/
public function get_field_data(\core_customfield\field_controller $field, int $instanceid): \core_customfield\data_controller {
$fields = [$field->get('id') => $field];
$fieldsdata = api::get_instance_fields_data($fields, $instanceid);
return $fieldsdata[$field->get('id')];
}
/**
* For a given instance id (question id) get the categories and the
* fields with any data. Return an array of categories containing an
* array of field names and values that is ready to be passed to a renderer.
*
* @param int $instanceid The instance id to get the data for.
* @return array $cfdata The fetched data
*/
public function get_categories_fields_data(int $instanceid): array {
// Prepare custom fields data.
$instancedata = $this->get_instance_data($instanceid);
$cfdata = [];
foreach ($instancedata as $instance) {
$field = $instance->get_field();
if ($this->can_view($field, $instanceid)) {
$category = $instance->get_field()->get_category()->get('name');
$fieldname = $field->get_formatted_name();
$fieldvalue = $this->get_field_data($field, $instanceid)->export_value();
$cfdata[$category][] = ['name' => $fieldname, 'value' => $fieldvalue];
}
}
return $cfdata;
}
/**
* Get the custom data for the given field
* and render HTML ready for display in question table.
*
* @param object $fielddata The field data used for display.
* @return string The HTML to display in the table column.
*/
public function display_custom_field_table(object $fielddata): string {
global $PAGE;
$output = $PAGE->get_renderer('qbank_customfields');
$outputdata = new field_data($fielddata);
return $output->render_for_table($outputdata);
}
/**
* Render the custom field category and filed data as HTML ready for display.
*
* @param array $catfielddata Array of categories and field names and values.
* @return string The HTML to display.
*/
public function display_custom_categories_fields(array $catfielddata): string {
global $PAGE;
$output = $PAGE->get_renderer('qbank_customfields');
return $output->render_for_preview($catfielddata);
}
/**
* Add custom controls to the field configuration form that will be saved.
*
* @param \MoodleQuickForm $mform The form to add the custom fields to.
*/
public function config_form_definition(\MoodleQuickForm $mform): void {
$mform->addElement('header', 'question_handler_header',
get_string('customfieldsettings', 'qbank_customfields'));
$mform->setExpanded('question_handler_header', true);
// If field is locked.
$mform->addElement('selectyesno', 'configdata[locked]',
get_string('customfield_islocked', 'qbank_customfields'));
$mform->addHelpButton('configdata[locked]', 'customfield_islocked', 'qbank_customfields');
// Field data visibility.
$visibilityoptions = [
self::VISIBLETOALL => get_string('customfield_visibletoall', 'qbank_customfields'),
self::VISIBLETOTEACHERS => get_string('customfield_visibletoteachers', 'qbank_customfields'),
self::NOTVISIBLE => get_string('customfield_notvisible', 'qbank_customfields')
];
$mform->addElement('select', 'configdata[visibility]',
get_string('customfield_visibility', 'qbank_customfields'),
$visibilityoptions);
$mform->addHelpButton(
'configdata[visibility]', 'customfield_visibility', 'qbank_customfields');
}
/**
* Creates or updates the question custom field data when restoring from a backup.
*
* @param \restore_task $task
* @param array $data
*
* @return int|void Conditionally returns the ID of the created or updated record.
*/
public function restore_instance_data_from_backup(\restore_task $task, array $data) {
$editablefields = $this->get_editable_fields($data['newquestion']);
$records = api::get_instance_fields_data($editablefields, $data['newquestion']);
$target = $task->get_target();
$override = ($target != \backup::TARGET_CURRENT_ADDING && $target != \backup::TARGET_EXISTING_ADDING);
foreach ($records as $d) {
$field = $d->get_field();
if ($field->get('shortname') === $data['shortname'] && $field->get('type') === $data['type']) {
if (!$d->get('id') || $override) {
$d->set($d->datafield(), $data['value']);
$d->set('value', $data['value']);
$d->set('valueformat', $data['valueformat']);
$d->set('valuetrust', !empty($data['valuetrust']));
$d->set('contextid', $data['fieldcontextid']);
$d->save();
}
return $d->get('id');
}
}
}
}
@@ -0,0 +1,41 @@
<?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 qbank_customfields\event;
use core\event\question_deleted;
use qbank_customfields\customfield\question_handler;
/**
* Event observer for question deletion
*
* @package qbank_customfields
* @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_deleted_observer {
/**
* Delete any custom field data for the deleted question.
*
* @param question_deleted $event
* @return void
*/
public static function delete_question_customfields(question_deleted $event): void {
question_handler::create()->delete_instance($event->objectid);
}
}
@@ -0,0 +1,59 @@
<?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 qbank_customfields\output;
/**
* Class renderer.
*
* @package qbank_customfields
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Matt Porritt <mattp@catalyst-ca.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends \plugin_renderer_base {
/**
* Render custom field data for table display.
*
* @param object $fielddata The field data to display.
* @return string The rendered HTML.
*/
public function render_for_table(object $fielddata): string {
$context = $fielddata->export_for_template($this);
return $this->render_from_template('qbank_customfields/table_display', $context);
}
/**
* Render custom filed data for table display.
*
* @param array $catfielddata The category and field data.
* @return string The rendered HTML.
*/
public function render_for_preview(array $catfielddata): string {
$context = ['categories' => []];
foreach ($catfielddata as $key => $value) {
$context['categories'][] = [
'catname' => $key,
'fields' => $value
];
}
return $this->render_from_template('qbank_customfields/preview_display', $context);
}
}
@@ -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 qbank_customfields;
use core_question\local\bank\plugin_features_base;
use core_question\local\bank\view;
use qbank_customfields\customfield\question_handler;
/**
* Class plugin_feature is the entrypoint for the columns.
*
* @package qbank_customfields
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Ghaly Marc-Alexandre <marc-alexandreghaly@catalyst-ca.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugin_feature extends plugin_features_base {
/**
* This method will return the array of objects to be rendered as a prt of question bank columns/actions.
*
* @param view $qbank
* @return array
*/
public function get_question_columns(view $qbank): array {
// We make a column for each custom field and load the data into it.
$columns = [];
// First get all the available question custom fields.
$customfieldhandler = question_handler::create();
$fields = $customfieldhandler->get_fields();
$context = $qbank->get_most_specific_context();
// Iterate through the fields initialising a column for each.
// We don't need to know the values that questions have at this stage.
foreach ($fields as $field) {
if ($customfieldhandler->can_view_type($field, $context)) {
$customfieldcolumn = new custom_field_column($qbank, $field);
$columns[] = $customfieldcolumn;
}
}
return $columns;
}
}
@@ -0,0 +1,38 @@
<?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 qbank_customfields\privacy;
/**
* Privacy Subsystem for qbank_customfields implementing null_provider.
*
* @package qbank_customfields
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Matt Porritt <mattp@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}