Skip to content

Commit

Permalink
Refactor field of type file
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Sep 25, 2023
1 parent a11ec69 commit 0d9b647
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 53 deletions.
2 changes: 1 addition & 1 deletion field/datalynxview/field_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
42 changes: 25 additions & 17 deletions field/file/field_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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)) {
Expand All @@ -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);

Expand Down Expand Up @@ -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.
Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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).
Expand Down Expand Up @@ -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',
Expand Down
46 changes: 16 additions & 30 deletions field/file/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 '';
Expand All @@ -96,20 +96,15 @@ 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()) {

$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);
}
}
Expand Down Expand Up @@ -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';

Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -239,6 +226,5 @@ public function render_csv($strfiles) {
}

return implode(",", $matches);

}
}
4 changes: 2 additions & 2 deletions field/picture/field_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ 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;

$fs = get_file_storage();
if (!$files = $fs->get_area_files($this->df->context->id, 'mod_datalynx', 'content',
$contentid)
) {
return;
return false;
}

// Update dimensions and regenerate thumbs.
Expand Down
2 changes: 1 addition & 1 deletion field/text/field_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
3 changes: 2 additions & 1 deletion field/time/lang/en/datalynxfield_time.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http:// Php.net/manual/en/function.strftime.php">PHP strftime format</a>.';
$string['displayformat_help'] = 'You can set a custom display format for the field value. Format options can be found at
<a href="https://php.net/manual/en/function.strftime.php">PHP strftime format</a>.';
$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: ';
Expand Down
1 change: 0 additions & 1 deletion styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0d9b647

Please sign in to comment.