From 0d9b647ef76654262d25550e8ea5fd8ed36acaa4 Mon Sep 17 00:00:00 2001 From: David Bogner Date: Mon, 25 Sep 2023 16:42:58 +0200 Subject: [PATCH] Refactor field of type file --- field/datalynxview/field_form.php | 2 +- field/file/field_class.php | 42 ++++++++++++--------- field/file/renderer.php | 46 ++++++++--------------- field/picture/field_class.php | 4 +- field/text/field_form.php | 2 +- field/time/lang/en/datalynxfield_time.php | 3 +- styles.css | 1 - 7 files changed, 47 insertions(+), 53 deletions(-) diff --git a/field/datalynxview/field_form.php b/field/datalynxview/field_form.php index c112eb59..7a6f9de6 100644 --- a/field/datalynxview/field_form.php +++ b/field/datalynxview/field_form.php @@ -47,7 +47,7 @@ public function field_definition() { WHERE m.name = 'datalynx'"; if ($CFG->branch >= 32) { $sql .= " AND cm.deletioninprogress = 0"; - }; + } if ($datalynxs = $DB->get_records_sql($sql)) { foreach ($datalynxs as $dfid => $datalynx) { if ($dfid != $this->_df->id()) { diff --git a/field/file/field_class.php b/field/file/field_class.php index ba67c969..5badc387 100644 --- a/field/file/field_class.php +++ b/field/file/field_class.php @@ -43,8 +43,10 @@ class datalynxfield_file extends datalynxfield_base { // Content2 - download counter. /** + * Get alt text, delete, etc. + * @return string[] */ - protected function content_names() { + protected function content_names(): array { return array('filemanager', 'alttext', 'delete', 'editor'); } @@ -57,7 +59,7 @@ public function update_content(stdClass $entry, array $values = null) { $fieldid = $this->field->id; // This sets variables by resetting every part from the array key. - $filemanager = $alttext = $delete = $editor = null; + $filemanager = $alttext = $editor = null; if (!empty($values)) { foreach ($values as $name => $value) { if (!empty($name) and !empty($value)) { @@ -72,7 +74,7 @@ public function update_content(stdClass $entry, array $values = null) { } // Contentid to locate in table. - $contentid = isset($entry->{"c{$this->field->id}_id"}) ? $entry->{"c{$this->field->id}_id"} : null; + $contentid = $entry->{"c{$this->field->id}_id"} ?? null; $usercontext = context_user::instance($USER->id); @@ -115,19 +117,19 @@ public function update_content(stdClass $entry, array $values = null) { /** */ - protected function format_content($entry, array $values = null) { + protected function format_content($entry, array $values = null): array { return array(null, null, null); } /** */ - public function get_content_parts() { + public function get_content_parts(): array { return array('content', 'content1', 'content2'); } /** */ - public function prepare_import_content(&$data, $importsettings, $csvrecord = null, $entryid = null) { + public function prepare_import_content(&$data, $importsettings, $csvrecord = null, $entryid = null): bool { global $USER; // Check if not a csv import. @@ -138,7 +140,7 @@ public function prepare_import_content(&$data, $importsettings, $csvrecord = nul $fieldid = $this->field->id; $fieldname = $this->name(); $csvname = $importsettings[$fieldname]['name']; - $fileurls = explode(',', $csvrecord[$csvname]); ; + $fileurls = explode(',', $csvrecord[$csvname]); // Prepare the draftarea where to put all files $draftitemid = file_get_submitted_draft_itemid("field_{$fieldid}_{$entryid}_filemanager"); @@ -192,7 +194,7 @@ public function prepare_import_content(&$data, $importsettings, $csvrecord = nul * @param $url * @return bool */ - protected function validate_url($url) { + protected function validate_url($url): bool { $path = parse_url($url, PHP_URL_PATH); $encodedpath = array_map('urlencode', explode('/', $path)); $url = str_replace($path, implode('/', $encodedpath), $url); @@ -202,18 +204,22 @@ protected function validate_url($url) { /** */ - protected function update_content_files($contentid, $params = null) { + protected function update_content_files($contentid, $params = null): bool { return true; } /** + * @param $entry + * @param array|null $values + * @return bool + * @throws file_exception + * @throws stored_file_creation_exception */ - protected function save_changes_to_file($entry, array $values = null) { + protected function save_changes_to_file($entry, array $values = null): bool { $fieldid = $this->field->id; - $entryid = $entry->id; $fieldname = "field_{$fieldid}_{$entry->id}"; - $contentid = isset($entry->{"c{$this->field->id}_id"}) ? $entry->{"c{$this->field->id}_id"} : null; + $contentid = $entry->{"c{$this->field->id}_id"} ?? null; $options = array('context' => $this->df->context); $data = (object) $values; @@ -222,9 +228,9 @@ protected function save_changes_to_file($entry, array $values = null) { // Get the file content. $fs = get_file_storage(); - $file = reset( - $fs->get_area_files($this->df->context->id, 'mod_datalynx', 'content', $contentid, - 'sortorder', false)); + $array = $fs->get_area_files($this->df->context->id, 'mod_datalynx', 'content', $contentid, + 'sortorder', false); + $file = reset($array); $filecontent = $file->get_content(); // Find content position (between body tags). @@ -276,15 +282,17 @@ public function get_search_sql(array $search): array { * Are fields of this field type suitable for use in customfilters? * @return bool */ - public static function is_customfilterfield() { + public static function is_customfilterfield(): bool { return true; } /** * Is $value a valid content or do we see an empty input? + * + * @param $value * @return bool */ - public static function is_fieldvalue_empty($value) { + public static function is_fieldvalue_empty($value): bool { // TODO: We see a draftarea id, need to determine if files are linked to it. global $DB; $filesizes = $DB->get_records_menu('files', diff --git a/field/file/renderer.php b/field/file/renderer.php index b1f45875..2c52f243 100644 --- a/field/file/renderer.php +++ b/field/file/renderer.php @@ -38,7 +38,7 @@ public function render_edit_mode(MoodleQuickForm &$mform, stdClass $entry, array $entryid = $entry->id; // If we see a 0 in content there are no files stored. Create new draft area. - $content = isset($entry->{"c{$fieldid}_content"}) ? $entry->{"c{$fieldid}_content"} : null; + $content = $entry->{"c{$fieldid}_content"} ?? null; if ($content == 0 || !isset($entry->{"c{$fieldid}_id"})) { $contentid = null; } else { @@ -74,10 +74,10 @@ public function render_display_mode(stdClass $entry, array $params) { $fieldid = $field->id(); $entryid = $entry->id; - $content = isset($entry->{"c{$fieldid}_content"}) ? $entry->{"c{$fieldid}_content"} : null; - $content1 = isset($entry->{"c{$fieldid}_content1"}) ? $entry->{"c{$fieldid}_content1"} : null; - $content2 = isset($entry->{"c{$fieldid}_content2"}) ? $entry->{"c{$fieldid}_content2"} : null; - $contentid = isset($entry->{"c{$fieldid}_id"}) ? $entry->{"c{$fieldid}_id"} : null; + $content = $entry->{"c{$fieldid}_content"} ?? null; + $content1 = $entry->{"c{$fieldid}_content1"} ?? null; + $content2 = $entry->{"c{$fieldid}_content2"} ?? null; + $contentid = $entry->{"c{$fieldid}_id"} ?? null; if (empty($content)) { return ''; @@ -96,12 +96,6 @@ public function render_display_mode(stdClass $entry, array $params) { return ''; } - $altname = empty($content1) ? '' : s($content1); - - if (!empty($params['alt'])) { - return $altname; - } - $strfiles = array(); foreach ($files as $file) { if (!$file->is_directory()) { @@ -109,7 +103,8 @@ public function render_display_mode(stdClass $entry, array $params) { $filename = $file->get_filename(); $filenameinfo = pathinfo($filename); $path = "/{$field->df()->context->id}/mod_datalynx/content/$contentid"; - + // ToDo: Remove or implement altname. + $altname = ""; $strfiles[] = $this->display_file($file, $path, $altname, $params); } } @@ -148,9 +143,7 @@ public function render_search_mode(MoodleQuickForm &$mform, $i = 0, $value = '') /** */ - protected function display_file($file, $path, $altname, $params = null) { - global $CFG; - + protected function display_file($file, string $path, string $altname = '', ?array $params = null) { $filename = $file->get_filename(); $pluginfileurl = '/pluginfile.php'; @@ -177,20 +170,14 @@ protected function display_file($file, $path, $altname, $params = null) { /** */ - protected function display_link($file, $path, $altname, $params = null) { - global $OUTPUT, $CFG; + protected function display_link($file, $path, $altname, $params = null): string { + global $OUTPUT; $filename = $file->get_filename(); - $displayname = $altname ? $altname : $filename; - if ($CFG->branch >= 33) { - $fileicon = html_writer::empty_tag('img', - array('src' => $OUTPUT->image_url(file_mimetype_icon($file->get_mimetype())), + $displayname = $altname ?: $filename; + $fileicon = html_writer::empty_tag('img', + array('src' => $OUTPUT->image_url(file_mimetype_icon($file->get_mimetype())), 'alt' => $file->get_mimetype(), 'height' => 16, 'width' => 16)); - } else { - $fileicon = html_writer::empty_tag('img', - array('src' => $OUTPUT->pix_url(file_mimetype_icon($file->get_mimetype())), - 'alt' => $file->get_mimetype(), 'height' => 16, 'width' => 16)); - } if (!empty($params['download'])) { list(, $context, , , $contentid) = explode('/', $path); @@ -205,14 +192,14 @@ protected function display_link($file, $path, $altname, $params = null) { /** */ - public function pluginfile_patterns() { + public function pluginfile_patterns(): array { return array("[[{$this->_field->name()}]]"); } /** * Array of patterns this field supports */ - protected function patterns() { + protected function patterns(): array { $fieldname = $this->_field->name(); $patterns = parent::patterns(); @@ -230,7 +217,7 @@ protected function patterns() { /** * Returns comma seperated list of urls in this entry. */ - public function render_csv($strfiles) { + public function render_csv($strfiles): string { $regex = '/https?\:\/\/[^\" ]+/i'; $matches = array(); foreach ($strfiles as $strfile) { @@ -239,6 +226,5 @@ public function render_csv($strfiles) { } return implode(",", $matches); - } } diff --git a/field/picture/field_class.php b/field/picture/field_class.php index ef6e7335..36a099cc 100644 --- a/field/picture/field_class.php +++ b/field/picture/field_class.php @@ -114,7 +114,7 @@ public function delete_field() { * (Re)generate pic and thumbnail images according to the dimensions specified in the field * settings. */ - protected function update_content_files($contentid, $params = null) { + protected function update_content_files($contentid, $params = null): bool { $updatefile = isset($params['updatefile']) ? $params['updatefile'] : true; $updatethumb = isset($params['updatethumb']) ? $params['updatethumb'] : true; @@ -122,7 +122,7 @@ protected function update_content_files($contentid, $params = null) { if (!$files = $fs->get_area_files($this->df->context->id, 'mod_datalynx', 'content', $contentid) ) { - return; + return false; } // Update dimensions and regenerate thumbs. diff --git a/field/text/field_form.php b/field/text/field_form.php index ec486bb8..fde14a76 100644 --- a/field/text/field_form.php +++ b/field/text/field_form.php @@ -71,7 +71,7 @@ public function field_definition() { WHERE m.name = 'datalynx'"; if ($CFG->branch >= 32) { $sql .= " AND cm.deletioninprogress = 0"; - }; + } if ($datalynxs = $DB->get_records_sql($sql)) { foreach ($datalynxs as $dfid => $datalynx) { if ($dfid != $this->_df->id()) { diff --git a/field/time/lang/en/datalynxfield_time.php b/field/time/lang/en/datalynxfield_time.php index 7f86aa26..593acf47 100644 --- a/field/time/lang/en/datalynxfield_time.php +++ b/field/time/lang/en/datalynxfield_time.php @@ -25,7 +25,8 @@ $string['dateonly'] = 'Date only'; $string['dateonly_help'] = 'Select this option to display only the date portion of the field value and a date only selector when the field is edited.'; $string['displayformat'] = 'Display format'; -$string['displayformat_help'] = 'You can set a custom display format for the field value. Format options can be found at PHP strftime format.'; +$string['displayformat_help'] = 'You can set a custom display format for the field value. Format options can be found at + PHP strftime format.'; $string['stopyear'] = 'Stop year'; $string['stopyear_help'] = 'Year value (YYYY). This value determines the max year value in the date/time selector in editing mode. Leave 0 or empty to use Moodle default.'; $string['fromtimestamp'] = 'From timestamp: '; diff --git a/styles.css b/styles.css index 4ae4199a..68e5ae6f 100644 --- a/styles.css +++ b/styles.css @@ -144,7 +144,6 @@ body.jsenabled div.editor_atto_content_wrap button.datalynx-tag { border-radius: 0.5em; -webkit-touch-callout: none; -webkit-user-select: none; - -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none;