diff --git a/classes/event/metadata_saved.php b/classes/event/metadata_saved.php new file mode 100644 index 0000000..6d3ac42 --- /dev/null +++ b/classes/event/metadata_saved.php @@ -0,0 +1,57 @@ +. + +/** + * Subplugin info class. + * + * @package local_metadata + * @author Tim St.Clair + * @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'); + } +} \ No newline at end of file diff --git a/fieldtype/menu/classes/define.php b/fieldtype/menu/classes/define.php index d9179a2..df106ce 100644 --- a/fieldtype/menu/classes/define.php +++ b/fieldtype/menu/classes/define.php @@ -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"'); diff --git a/fieldtype/menu/classes/metadata.php b/fieldtype/menu/classes/metadata.php index 943ef4f..ddea6fc 100644 --- a/fieldtype/menu/classes/metadata.php +++ b/fieldtype/menu/classes/metadata.php @@ -36,6 +36,7 @@ class metadata extends \local_metadata\fieldtype\metadata { /** @var array $options */ public $options; + public $multiple = false; /** @var int $datakey */ public $datakey; @@ -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 + list($key,$value) = array_map('trim',explode("=>",$option)); + } else { + // fields listed in "text" get rendered as + $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; } @@ -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); + } } /** @@ -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; } /** diff --git a/fieldtype/menu/lang/en/metadatafieldtype_menu.php b/fieldtype/menu/lang/en/metadatafieldtype_menu.php index 42d595b..057ec86 100644 --- a/fieldtype/menu/lang/en/metadatafieldtype_menu.php +++ b/fieldtype/menu/lang/en/metadatafieldtype_menu.php @@ -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.'; \ No newline at end of file +$string['displayname'] = 'Dropdown menu'; \ No newline at end of file diff --git a/fieldtype/menu/version.php b/fieldtype/menu/version.php index 476020e..cb8426c 100644 --- a/fieldtype/menu/version.php +++ b/fieldtype/menu/version.php @@ -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). diff --git a/lang/en/local_metadata.php b/lang/en/local_metadata.php index ced6350..bf8511c 100644 --- a/lang/en/local_metadata.php +++ b/lang/en/local_metadata.php @@ -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.'; \ No newline at end of file diff --git a/lib.php b/lib.php index 5fec7bd..3be1962 100644 --- a/lib.php +++ b/lib.php @@ -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(); + } /** diff --git a/version.php b/version.php index 2e77325..0431edf 100644 --- a/version.php +++ b/version.php @@ -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'; \ No newline at end of file