Merge branch 'MDL-75498-master' of https://github.com/ferranrecio/moodle

This commit is contained in:
Andrew Nicols
2023-01-31 15:13:33 +08:00
30 changed files with 713 additions and 124 deletions
@@ -56,7 +56,7 @@ class template_editor_tools implements templatable, renderable {
public function export_for_template(\renderer_base $output): array {
$tools = [
$this->get_field_tags($this->templatename),
$this->get_field_id_tags($this->templatename),
$this->get_field_info_tags($this->templatename),
$this->get_action_tags($this->templatename),
$this->get_other_tags($this->templatename),
];
@@ -90,29 +90,30 @@ class template_editor_tools implements templatable, renderable {
$fieldname = $field->get_name();
$taglist["[[$fieldname]]"] = $fieldname;
}
$taglist['##otherfields##'] = get_string('otherfields', 'data');
return $this->get_optgroup_data($name, $taglist);
}
/**
* Return the field IDs template tags.
* Return the field information template tags.
*
* @param string $templatename the template name
* @return array|null array of tags.
*/
protected function get_field_id_tags(string $templatename): array {
$name = get_string('fieldids', 'data');
if ($templatename != 'addtemplate') {
return $this->get_optgroup_data($name, []);
}
protected function get_field_info_tags(string $templatename): array {
$name = get_string('fieldsinformationtags', 'data');
$taglist = [];
// 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";
if ($templatename == 'addtemplate') {
$taglist["[[$fieldname#id]]"] = get_string('fieldtagid', 'mod_data', $fieldname);
}
$taglist["[[$fieldname#name]]"] = get_string('fieldtagname', 'mod_data', $fieldname);
$taglist["[[$fieldname#description]]"] = get_string('fieldtagdescription', 'mod_data', $fieldname);
}
return $this->get_optgroup_data($name, $taglist);
}
@@ -129,11 +130,11 @@ class template_editor_tools implements templatable, renderable {
return $this->get_optgroup_data($name, []);
}
$taglist = [
'##actionsmenu##' => get_string('actionsmenu', 'data'),
'##edit##' => get_string('edit', 'data'),
'##delete##' => get_string('delete', 'data'),
'##approve##' => get_string('approve', 'data'),
'##disapprove##' => get_string('disapprove', 'data'),
'##actionsmenu##' => get_string('actionsmenu', 'data'),
];
if ($templatename != 'rsstemplate') {
$taglist['##export##'] = get_string('export', 'data');
+119 -20
View File
@@ -19,6 +19,7 @@ namespace mod_data;
use action_menu;
use action_menu_link_secondary;
use core\output\checkbox_toggleall;
use data_field_base;
use html_writer;
use mod_data\manager;
use moodle_url;
@@ -79,6 +80,9 @@ class template {
/** @var array The mod_data fields. */
protected $fields = [];
/** @var array All fields that are not present in the template content. */
protected $otherfields = [];
/**
* Class contructor.
*
@@ -213,6 +217,13 @@ class template {
return;
}
$this->tags = $matches['tags'];
// Check if some tag require some extra template scan.
foreach ($this->tags as $tagname) {
$methodname = "preprocess_tag_{$tagname}";
if (method_exists($this, $methodname)) {
$this->$methodname($templatecontent);
}
}
}
/**
@@ -309,9 +320,13 @@ class template {
$this->search,
$field->display_browse_field($entry->id, $this->templatename)
);
// Field id.
// Other dynamic field information.
$pattern = '[[' . $field->field->name . '#id]]';
$result[$pattern] = $field->field->id;
$pattern = '[[' . $field->field->name . '#name]]';
$result[$pattern] = $field->field->name;
$pattern = '[[' . $field->field->name . '#description]]';
$result[$pattern] = $field->field->description;
}
return $result;
}
@@ -707,6 +722,46 @@ class template {
return (string) $entry->id;
}
/**
* Prepare otherfield tag scanning the present template fields.
*
* @param string $templatecontent the template content
*/
protected function preprocess_tag_otherfields(string $templatecontent) {
$otherfields = [];
$fields = $this->manager->get_fields();
foreach ($fields as $field) {
if (strpos($templatecontent, "[[" . $field->field->name . "]]") === false) {
$otherfields[] = $field;
}
}
$this->otherfields = $otherfields;
}
/**
* Returns the ##otherfields## tag replacement for an entry.
*
* @param stdClass $entry the entry object
* @param bool $canmanageentry if the current user can manage this entry
* @return string the tag replacement
*/
protected function get_tag_otherfields_replacement(stdClass $entry, bool $canmanageentry): string {
global $OUTPUT;
$fields = [];
foreach ($this->otherfields as $field) {
$fieldvalue = highlight(
$this->search,
$field->display_browse_field($entry->id, $this->templatename)
);
$fieldinfo = [
'fieldname' => $field->field->name,
'fieldcontent' => $fieldvalue,
];
$fields[] = $fieldinfo;
}
return $OUTPUT->render_from_template('mod_data/fields_otherfields', ['fields' => $fields]);
}
/**
* Returns the ##actionsmenu## tag replacement for an entry.
*
@@ -842,6 +897,8 @@ class template {
?int $entryid = null,
?stdClass $entrydata = null
): string {
global $OUTPUT;
$manager = $this->manager;
$renderer = $manager->get_renderer();
$templatecontent = $this->templatecontent;
@@ -864,34 +921,38 @@ class template {
$replacements = [];
// Then we generate strings to replace.
$otherfields = [];
foreach ($possiblefields as $field) {
// To skip unnecessary calls to display_add_field().
$fieldinput = $this->get_field_input($processeddata, $entryid, $entrydata, $field);
if (strpos($templatecontent, "[[" . $field->field->name . "]]") !== false) {
// Replace the field tag.
$patterns[] = "[[" . $field->field->name . "]]";
$errors = '';
$fieldnotifications = $processeddata->fieldnotifications[$field->field->name] ?? [];
if (!empty($fieldnotifications)) {
foreach ($fieldnotifications as $notification) {
$errors .= $renderer->notification($notification);
}
}
$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;
$replacements[] = $fieldinput;
} else {
// Is in another fields.
$otherfields[] = [
'fieldname' => $field->field->name,
'fieldcontent' => $fieldinput,
];
}
// Replace the field id tag.
$patterns[] = "[[" . $field->field->name . "#id]]";
$replacements[] = 'field_' . $field->field->id;
$patterns[] = '[[' . $field->field->name . '#name]]';
$replacements[] = $field->field->name;
$patterns[] = '[[' . $field->field->name . '#description]]';
$replacements[] = $field->field->description;
}
$patterns[] = "##otherfields##";
if (!empty($otherfields)) {
$replacements[] = $OUTPUT->render_from_template(
'mod_data/fields_otherfields',
['fields' => $otherfields]
);
} else {
$replacements[] = '';
}
if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
@@ -902,4 +963,42 @@ class template {
$result .= str_ireplace($patterns, $replacements, $templatecontent);
return $result;
}
/**
* Return the input form html from a specific field.
*
* @param stdClass $processeddata the previous process data information.
* @param int|null $entryid the possible entry id
* @param stdClass|null $entrydata the entry data from a previous form or from a real entry
* @param data_field_base $field the field object
* @return string the add entry HTML content
*/
private function get_field_input(
stdClass $processeddata,
?int $entryid = null,
?stdClass $entrydata = null,
data_field_base $field
): string {
$renderer = $this->manager->get_renderer();
$errors = '';
$fieldnotifications = $processeddata->fieldnotifications[$field->field->name] ?? [];
if (!empty($fieldnotifications)) {
foreach ($fieldnotifications as $notification) {
$errors .= $renderer->notification($notification);
}
}
$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);
}
return $errors . $fielddisplay;
}
}