Skip to content

Commit

Permalink
Merge branch 'MDL-75497-master4' of https://github.com/raortegar/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Oct 20, 2022
2 parents b720790 + bf5cd9c commit de11a92
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 37 deletions.
13 changes: 11 additions & 2 deletions mod/data/classes/local/importer/preset_importer.php
Expand Up @@ -19,6 +19,7 @@
use mod_data\manager;
use mod_data\preset;
use stdClass;
use html_writer;

/**
* Abstract class used for data preset importers
Expand Down Expand Up @@ -212,7 +213,7 @@ public function get_preset_settings(): stdClass {
* @return bool Wether the importing has been successful.
*/
public function import(bool $overwritesettings): bool {
global $DB;
global $DB, $OUTPUT;

$params = $this->get_preset_settings();
$settings = $params->settings;
Expand Down Expand Up @@ -252,7 +253,12 @@ public function import(bool $overwritesettings): bool {
unset($fieldobject);
} else {
/* Make a new field */
include_once("field/$newfield->type/field.class.php");
$filepath = "field/$newfield->type/field.class.php";
if (!file_exists($filepath)) {
$missingfieldtypes[] = $newfield->name;
continue;
}
include_once($filepath);

if (!isset($newfield->description)) {
$newfield->description = '';
Expand All @@ -263,6 +269,9 @@ public function import(bool $overwritesettings): bool {
unset($fieldclass);
}
}
if (!empty($missingfieldtypes)) {
echo $OUTPUT->notification(get_string('missingfieldtypeimport', 'data') . html_writer::alist($missingfieldtypes));
}
}

// Get rid of all old unused data.
Expand Down
6 changes: 6 additions & 0 deletions mod/data/classes/output/template_editor_tools.php
Expand Up @@ -84,6 +84,9 @@ protected function get_field_tags(string $templatename): array {
$taglist = [];
$fields = $this->manager->get_fields();
foreach ($fields as $field) {
if ($field->type === 'unknown') {
continue;
}
$fieldname = $field->get_name();
$taglist["[[$fieldname]]"] = $fieldname;
}
Expand All @@ -105,6 +108,9 @@ protected function get_field_id_tags(string $templatename): array {
// Field IDs.
$fields = $this->manager->get_fields();
foreach ($fields as $field) {
if ($field->type === 'unknown') {
continue;
}
$fieldname = $field->get_name();
$taglist["[[$fieldname#id]]"] = "$fieldname id";
}
Expand Down
16 changes: 13 additions & 3 deletions mod/data/classes/search/entry.php
Expand Up @@ -299,6 +299,10 @@ protected function get_fields_for_entries($entry) {

foreach ($filteredcontents as $content) {
$classname = $this->get_field_class_name($content->fieldtype);
if (!$classname) {
$content->addtemplateposition = -1;
continue;
}
$content->priority = $classname::get_priority();
$content->addtemplateposition = strpos($template, '[['.$content->fldname.']]');
}
Expand Down Expand Up @@ -346,16 +350,22 @@ protected function get_fields_for_entries($entry) {
}

/**
* Returns the class name for that field type and includes it.
* Returns the class name for the given field type and includes it.
*
* @param string $fieldtype
* @return string
* @return string|null It will return the class name or null if the field type is not available.
*/
protected function get_field_class_name($fieldtype) {
global $CFG;

$fieldtype = trim($fieldtype);
require_once($CFG->dirroot . '/mod/data/field/' . $fieldtype . '/field.class.php');

$fieldpath = $CFG->dirroot . '/mod/data/field/' . $fieldtype . '/field.class.php';
if (!file_exists($fieldpath)) {
return null;
}

require_once($fieldpath);
return 'data_field_' . $fieldtype;
}

Expand Down
12 changes: 11 additions & 1 deletion mod/data/classes/template.php
Expand Up @@ -850,7 +850,17 @@ public function parse_add_entry(
$errors .= $renderer->notification($notification);
}
}
$replacements[] = $errors . $field->display_add_field($entryid, $entrydata);
$fielddisplay = '';
if ($field->type === 'unknown') {
if ($this->canmanageentries) { // Display notification for users that can manage entries.
$errors .= $renderer->notification(get_string('missingfieldtype', 'data',
(object)['name' => $field->field->name]));
}
} else {
$fielddisplay = $field->display_add_field($entryid, $entrydata);
}

$replacements[] = $errors . $fielddisplay;
}

// Replace the field id tag.
Expand Down
45 changes: 35 additions & 10 deletions mod/data/field.php
Expand Up @@ -260,9 +260,15 @@
// Print confirmation message.
$field = data_get_field_from_id($fid, $data);

echo $OUTPUT->confirm('<strong>'.$field->name().': '.$field->field->name.'</strong><br /><br />'. get_string('confirmdeletefield','data'),
'field.php?d='.$data->id.'&mode=delete&fid='.$fid.'&confirm=1',
'field.php?d='.$data->id);
if ($field->type === 'unknown') {
$fieldtypename = get_string('unknown', 'data');
} else {
$fieldtypename = $field->name();
}
echo $OUTPUT->confirm('<strong>'.$fieldtypename.': '.$field->field->name.'</strong><br /><br />'.
get_string('confirmdeletefield', 'data'),
'field.php?d='.$data->id.'&mode=delete&fid='.$fid.'&confirm=1',
'field.php?d='.$data->id);

echo $OUTPUT->footer();
exit;
Expand Down Expand Up @@ -317,6 +323,9 @@
$menufield = array();

foreach ($plugins as $plugin=>$fulldir){
if (!is_dir($fulldir)) {
continue;
}
$menufield[$plugin] = get_string('pluginname', 'datafield_'.$plugin); //get from language files
}
asort($menufield); //sort in alphabetical order
Expand Down Expand Up @@ -360,6 +369,7 @@
$table->wrap = array(false,false,false,false);

if ($fff = $DB->get_records('data_fields', array('dataid'=>$data->id),'id')){
$missingfieldtypes = [];
foreach ($fff as $ff) {

$field = data_get_field($ff, $data);
Expand All @@ -378,15 +388,30 @@
'mode' => 'delete',
));

$table->data[] = array(
html_writer::link($displayurl, $field->field->name),
$field->image() . '&nbsp;' . $field->name(),
// It display a notification when the field type does not exist.
$deletelink = html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete')));
$editlink = html_writer::link($displayurl, $OUTPUT->pix_icon('t/edit', get_string('edit')));
if ($field->type === 'unknown') {
$missingfieldtypes[] = $field->field->name;
$fieldnamedata = $field->field->name;
$fieltypedata = $field->field->type;
$fieldlinkdata = $deletelink;
} else {
$fieldnamedata = html_writer::link($displayurl, $field->field->name);
$fieltypedata = $field->image() . '&nbsp;' . $field->name();
$fieldlinkdata = $editlink . '&nbsp;' . $deletelink;
}

$table->data[] = [
$fieldnamedata,
$fieltypedata,
$field->field->required ? get_string('yes') : get_string('no'),
shorten_text($field->field->description, 30),
html_writer::link($displayurl, $OUTPUT->pix_icon('t/edit', get_string('edit'))) .
'&nbsp;' .
html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete'))),
);
$fieldlinkdata
];
}
if (!empty($missingfieldtypes)) {
echo $OUTPUT->notification(get_string('missingfieldtypes', 'data') . html_writer::alist($missingfieldtypes));
}
}
echo html_writer::table($table);
Expand Down
5 changes: 5 additions & 0 deletions mod/data/lang/en/data.php
Expand Up @@ -238,6 +238,11 @@
$string['invalidfieldname'] = 'Please choose another name for this field';
$string['invalidfieldtype'] = 'Field type is incorrect';
$string['invalidid'] = 'Incorrect data ID';
$string['missingfieldtype'] = 'Field type for {$a->name} not found';
$string['missingfieldtypes'] = 'The following fields do not have their corresponding field types installed and will not be included in the forms when adding or editing entries.
Their labels may still show on the form, so please update the "Add entry template" accordingly:';
$string['missingfieldtypeimport'] = 'The following fields were not imported because their corresponding field types are not installed:';
$string['unknown'] = 'Unknown field';
$string['invalidpreset'] = '{$a} is not a preset.';
$string['invalidrecord'] = 'Incorrect record';
$string['invalidurl'] = 'The URL you just entered is not valid';
Expand Down
71 changes: 55 additions & 16 deletions mod/data/lib.php
Expand Up @@ -400,6 +400,12 @@ function display_edit_field() {
if (empty($this->field)) { // No field has been defined yet, try and make one
$this->define_default_field();
}

// Throw an exception if field type doen't exist. Anyway user should never access to edit a field with an unknown fieldtype.
if ($this->type === 'unknown') {
throw new \moodle_exception(get_string('missingfieldtype', 'data', (object)['name' => $this->field->name]));
}

echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');

echo '<form id="editfield" action="'.$CFG->wwwroot.'/mod/data/field.php" method="post">'."\n";
Expand All @@ -417,7 +423,14 @@ function display_edit_field() {

echo $OUTPUT->heading($this->name(), 3);

require_once($CFG->dirroot.'/mod/data/field/'.$this->type.'/mod.html');

$filepath = $CFG->dirroot.'/mod/data/field/'.$this->type.'/mod.html';

if (!file_exists($filepath)) {
throw new \moodle_exception(get_string('missingfieldtype', 'data', (object)['name' => $this->field->name]));
} else {
require_once($filepath);
}

echo html_writer::start_div('mt-3');
echo html_writer::tag('input', null, array('type' => 'submit', 'value' => $savebutton,
Expand Down Expand Up @@ -916,7 +929,12 @@ function data_get_field_from_id($fieldid, $data){
function data_get_field_new($type, $data) {
global $CFG;

require_once($CFG->dirroot.'/mod/data/field/'.$type.'/field.class.php');
$filepath = $CFG->dirroot.'/mod/data/field/'.$type.'/field.class.php';
// It should never access this method if the subfield class doesn't exist.
if (!file_exists($filepath)) {
throw new \moodle_exception('invalidfieldtype', 'data');
}
require_once($filepath);
$newfield = 'data_field_'.$type;
$newfield = new $newfield(0, $data);
return $newfield;
Expand All @@ -928,20 +946,24 @@ function data_get_field_new($type, $data) {
* input: $param $field - record from db
*
* @global object
* @param object $field
* @param object $data
* @param object $cm
* @return data_field_base
* @param stdClass $field the field record
* @param stdClass $data the data instance
* @param stdClass|null $cm optional course module data
* @return data_field_base the field object instance or data_field_base if unkown type
*/
function data_get_field($field, $data, $cm=null) {
global $CFG;

if ($field) {
require_once('field/'.$field->type.'/field.class.php');
$newfield = 'data_field_'.$field->type;
$newfield = new $newfield($field, $data, $cm);
return $newfield;
if (!isset($field->type)) {
return new data_field_base($field);
}
$filepath = $CFG->dirroot.'/mod/data/field/'.$field->type.'/field.class.php';
if (!file_exists($filepath)) {
return new data_field_base($field);
}
require_once($filepath);
$newfield = 'data_field_'.$field->type;
$newfield = new $newfield($field, $data, $cm);
return $newfield;
}


Expand Down Expand Up @@ -1764,6 +1786,10 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order='
$fieldname = preg_quote($fieldname, '/');
$patterns[] = "/\[\[$fieldname\]\]/i";
$searchfield = data_get_field_from_id($field->field->id, $data);

if ($searchfield->type === 'unknown') {
continue;
}
if (!empty($search_array[$field->field->id]->data)) {
$replacement[] = $searchfield->display_search_field($search_array[$field->field->id]->data);
} else {
Expand Down Expand Up @@ -2450,7 +2476,7 @@ public function get_preset_settings() {
* @return bool
*/
function import($overwritesettings) {
global $DB, $CFG;
global $DB, $CFG, $OUTPUT;

$params = $this->get_preset_settings();
$settings = $params->settings;
Expand All @@ -2471,7 +2497,7 @@ function import($overwritesettings) {
}
else $preservedfields[$cid] = true;
}

$missingfieldtypes = [];
foreach ($newfields as $nid => $newfield) {
$cid = optional_param("field_$nid", -1, PARAM_INT);

Expand All @@ -2488,7 +2514,12 @@ function import($overwritesettings) {
unset($fieldobject);
} else {
/* Make a new field */
include_once("field/$newfield->type/field.class.php");
$filepath = "field/$newfield->type/field.class.php";
if (!file_exists($filepath)) {
$missingfieldtypes[] = $newfield->name;
continue;
}
include_once($filepath);

if (!isset($newfield->description)) {
$newfield->description = '';
Expand All @@ -2499,6 +2530,9 @@ function import($overwritesettings) {
unset($fieldclass);
}
}
if (!empty($missingfieldtypes)) {
echo $OUTPUT->notification(get_string('missingfieldtypeimport', 'data') . html_writer::alist($missingfieldtypes));
}
}

/* Get rid of all old unused data */
Expand Down Expand Up @@ -2954,7 +2988,12 @@ function data_import_csv($cm, $data, &$csvdata, $encoding, $fielddelimiter) {
unset($fieldnames[$id]); // To ensure the user provided content fields remain in the array once flipped.
} else {
$field = $rawfields[$name];
require_once("$CFG->dirroot/mod/data/field/$field->type/field.class.php");
$filepath = "$CFG->dirroot/mod/data/field/$field->type/field.class.php";
if (!file_exists($filepath)) {
$errorfield .= "'$name' ";
continue;
}
require_once($filepath);
$classname = 'data_field_' . $field->type;
$fields[$name] = new $classname($field, $data, $cm);
}
Expand Down

0 comments on commit de11a92

Please sign in to comment.