Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add multi-select and keyed options support #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions classes/event/metadata_saved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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/>.

/**
* Subplugin info class.
*
* @package local_metadata
* @author Tim St.Clair <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/*
* metadata_saved event emitter
*
* emit an event when data is entered which contains all the field names and their values for this instance
* the observers of this event will have to decice whether they handle data for the supplied context
*
* triggered by: lib.php -> local_metadata_save_data()
*/

namespace local_metadata\event;

defined('MOODLE_INTERNAL') || die();


class metadata_saved extends \core\event\base {

protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
}

public function get_description() {
return "The user with id '{$this->userid}' saved metadata for course with id '{$this->contextinstanceid}'.";
}

public static function get_name() {
return 'Local Metadata CRUD save event'; // TODO ad to langpack
}

public function get_url() {
return new \moodle_url('/local/metadata/index.php');
}
}
4 changes: 4 additions & 0 deletions fieldtype/menu/classes/define.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function define_form_specific($form) {
// Param 1 for menu type contains the options.
$form->addElement('textarea', 'param1', get_string('profilemenuoptions', 'admin'), ['rows' => 6, 'cols' => 40]);
$form->setType('param1', PARAM_TEXT);
$form->addHelpButton('param1', 'menu_options', 'local_metadata');

// Param 2 for turning dropdown into mutli-select
$form->addElement('selectyesno', 'param2', get_string('select_multiple', 'local_metadata'));

// Default data.
$form->addElement('text', 'defaultdata', get_string('profiledefaultdata', 'admin'), 'size="50"');
Expand Down
41 changes: 35 additions & 6 deletions fieldtype/menu/classes/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class metadata extends \local_metadata\fieldtype\metadata {

/** @var array $options */
public $options;
public $multiple = false;

/** @var int $datakey */
public $datakey;
Expand All @@ -59,18 +60,34 @@ public function __construct($fieldid = 0, $instanceid = 0, $fielddata = null) {
} else {
$options = [];
}
// Param 1 for menu type is the options.
if (isset($this->field->param2)) {
$this->multiple = ((int)$this->field->param2 > 0);
} else {
$this->multiple = false;
}

$this->options = [];
if (!empty($this->field->required)) {
$this->options[''] = get_string('choose').'...';
}
foreach ($options as $key => $option) {
$this->options[$option] = format_string($option); // Multilang formatting with filters.
if (strpos($option,"=>")!==false) {
// fields listed in "key=>text" get rendered as <option value="key">text</option>
list($key,$value) = array_map('trim',explode("=>",$option));
} else {
// fields listed in "text" get rendered as <option value="text">text</option>
$key = $option;
$value = $option;
}
$this->options[$key] = format_string($value); // Multilang formatting with filters.
}

// Set the data key.
// Set the data key(s).
if ($this->data !== null) {
$key = $this->data;
if (isset($this->options[$key]) || ($key = array_search($key, $this->options)) !== false) {
if ($this->multiple) {
$this->datakey = explode("\n", str_replace("\r", '', $this->data)); // array of data which will be keys anyway
} else if (isset($this->options[$key]) || ($key = array_search($key, $this->options)) !== false) {
$this->data = $key;
$this->datakey = $key;
}
Expand All @@ -83,7 +100,10 @@ public function __construct($fieldid = 0, $instanceid = 0, $fielddata = null) {
* @param moodleform $mform Moodle form instance
*/
public function edit_field_add($mform) {
$mform->addElement('select', $this->inputname, format_string($this->field->name), $this->options);
$select = $mform->addElement('select', $this->inputname, format_string($this->field->name), $this->options);
if ($this->multiple) {
$select->setMultiple(true);
}
}

/**
Expand Down Expand Up @@ -112,7 +132,16 @@ public function edit_field_set_default($mform) {
* @return mixed Data or null
*/
public function edit_save_data_preprocess($data, $datarecord) {
return isset($this->options[$data]) ? $data : null;
if (is_array($data)) {
$string = '';
foreach ($data as $key) {
if(isset($this->options[$key])) {
$string .= $key."\r\n";
}
}
return substr($string,0,-2);
}
return isset($this->options[$data]) ? $data : NULL;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions fieldtype/menu/lang/en/metadatafieldtype_menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@
defined('MOODLE_INTERNAL') || die;

$string['pluginname'] = 'Menu metadata fieldtype';
$string['displayname'] = 'Dropdown menu';
$string['privacy:metadata'] = 'Fieldtypes do not store data.';
$string['displayname'] = 'Dropdown menu';
4 changes: 2 additions & 2 deletions fieldtype/menu/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2017070102;
$plugin->release = 'BETA3.3.4 (Build 2018062800)';
$plugin->version = 2018072301;
$plugin->release = 'BETA3.3.1 (Build 2017071200)';
$plugin->maturity = MATURITY_BETA;
$plugin->requires = 2016052300; // Requires this Moodle version.
$plugin->component = 'metadatafieldtype_menu'; // Full name of the plugin (used for diagnostics).
14 changes: 6 additions & 8 deletions lang/en/local_metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
$string['errorcontextnotfound'] = 'Invalid context subplugin "{$a->contextname}" requested.';
$string['instancemetadata'] = '{$a->instancename} metadata';
$string['metadata'] = 'Metadata';
$string['metadatadata'] = 'Value';
$string['metadatadescription'] = 'Description';
$string['metadatafor'] = 'Instance metadata';
$string['metadataname'] = 'Name';
$string['metadatasaved'] = 'Metadata saved.';
$string['privacy:metadata'] = 'Metadata plugin only manages data and fields. Individual subplugins may store personal data.';
$string['subplugintype_metadatacontext'] = 'Data context plugin';
$string['subplugintype_metadatacontext_plural'] = 'Data context plugins';
$string['subplugintype_metadatafieldtype'] = 'Data fieldtype plugin';
$string['subplugintype_metadatafieldtype_plural'] = 'Data fieldtype plugins';
$string['subplugintype_metadatacontext'] = 'Data context';
$string['subplugintype_metadatacontext_plural'] = 'Data contexts';
$string['subplugintype_metadatafieldtype'] = 'Data field type';
$string['subplugintype_metadatafieldtype_plural'] = 'Data field types';
$string['select_multiple'] = 'Allow multiple selection';
$string['menu_options_help'] = 'You can force the stored data values by entering lines in `value`=>`text` format, otherwise selected values will be the same as the text.';
15 changes: 15 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ function local_metadata_save_data($new, $contextlevel) {
$formfield->edit_save_data($new);
}
}

// get all the field values and package it into an event
foreach ($new as $key=>$value) {
if (strpos($key,"local_metadata_field_")!==false) {
$data[substr($key,21)]=$value;
}
}
$contextid = $DB->get_field_sql("select id from {context} where contextlevel=? and instanceid=?", [$contextlevel, $new->id]);
$params = array(
'contextid' => $contextid,
'other' => $data, // all current field values
);
$event = \local_metadata\event\metadata_saved::create($params);
$event->trigger();

}

/**
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*/

defined('MOODLE_INTERNAL') || die;
$plugin->version = 2017070104;
$plugin->release = 'BETA3.3.4 (Build 2018062800)';
$plugin->version = 2018072303;
$plugin->release = 'BETA3.3.3 (Build 2018052200)';
$plugin->maturity = MATURITY_BETA;
$plugin->requires = 2016052300; // Moodle 3.1 release and upwards.
$plugin->component = 'local_metadata';