/** * */ public static function fields_selection_definition($mform, $dataformid, $prefix = null) { $options = array(0 => get_string('all'), 1 => get_string('selected', 'form')); $mform->addElement('select', $prefix . 'fieldselection', get_string('fields', 'dataform'), $options); $items = array(); if ($items = \mod_dataform_field_manager::instance($dataformid)->fields_menu) { $items = array_combine($items, $items); } $select =& $mform->addElement('select', $prefix . 'fields', null, $items); $select->setMultiple(true); $mform->disabledIf($prefix . 'fields', $prefix . 'fieldselection', 'eq', 0); }
/** * Generates a dataform filter. * @param array|stdClass $record * @param array $options * @return stdClass generated object */ public function create_filter($record, array $options = null) { $record = (object) (array) $record; $filter = new \mod_dataform\pluginbase\dataformfilter($record); $fieldman = new \mod_dataform_field_manager($record->dataid); // Append sort options if specified. if (!empty($record->sortoptions)) { // Convert fieldname[,element] sortdir to // fieldid => (element,sortdir). $sorties = array(); foreach (explode(';', $record->sortoptions) as $sortoption) { list($fieldname, $element, $sortdir) = explode(',', $sortoption); if ($field = $fieldman->get_field_by_name($fieldname)) { $sorties[$field->id] = array($element, $sortdir); } } $filter->append_sort_options($sorties); } // Append search options if specified. if (!empty($record->searchoptions)) { // Convert AND|OR,fieldname,element,[NOT],operator,value to // fieldid => (endor => (element, not, operator, value)). $searchoptions = array(); foreach (explode(';', $record->searchoptions) as $searchoption) { list($andor, $fieldname, $element, $isnot, $op, $value) = explode(',', $searchoption); if ($field = $fieldman->get_field_by_name($fieldname)) { $searchy = array($andor => array(array($element, $isnot, $op, $value))); if (empty($searchoptions[$field->id])) { $searchoptions[$field->id] = $searchy; } else { $searchoptions[$field->id] = array_merge_recursive($searchoptions[$field->id], $searchy); } } } $filter->append_search_options($searchoptions); } $filter->update(); return $filter->instance; }
/** * Returns the field manager of the Dataform this mannager works for. * * @return mod_dataform_field_manager */ public function get_field_manager() { return mod_dataform_field_manager::instance($this->dataformid); }
/** * */ public static function custom_search_definition($mform, $dataformid, $customsearch, array $fields = array(), $showlabel = false) { if (!$fields and $dataformid) { $fieldman = new \mod_dataform_field_manager($dataformid); $fields = $fieldman->get_fields(); } $andoroptions = array('' => get_string('andor', 'dataform'), 'AND' => get_string('and', 'dataform'), 'OR' => get_string('or', 'dataform')); $fieldoptions = self::get_field_search_options_menu($fields); $isnotoptions = array('' => get_string('is', 'dataform'), 'NOT' => get_string('not', 'dataform')); $operatoroptions = array('' => get_string('empty', 'dataform'), '=' => get_string('equal', 'dataform'), '>' => get_string('greaterthan', 'dataform'), '<' => get_string('lessthan', 'dataform'), '>=' => get_string('greaterorequal', 'dataform'), '<=' => get_string('lessorequal', 'dataform'), 'BETWEEN' => get_string('between', 'dataform'), 'LIKE' => get_string('contains', 'dataform'), 'IN' => get_string('in', 'dataform')); // Add current options. $searchcriteria = array(); if ($customsearch) { if (!is_array($customsearch)) { $searchfields = unserialize($customsearch); } else { $searchfields = $customsearch; } foreach ($searchfields as $fieldid => $searchfield) { if (empty($fields[$fieldid])) { continue; } foreach ($searchfield as $andor => $searchoptions) { foreach ($searchoptions as $searchoption) { if (!is_array($searchoption) or count($searchoption) != 4) { continue; } list($element, $not, $operator, $value) = $searchoption; $searchcriteria[] = array("{$fieldid},{$element}", $andor, $not, $operator, $value); } } } } // Add 2 more empty options. $searchcriteria[] = array(null, null, null, null, null); $searchcriteria[] = array(null, null, null, null, null); // Add form definition for each existing criterion. $fieldlabel = get_string('filtersearchfieldlabel', 'dataform'); foreach ($searchcriteria as $i => $searchcriterion) { if (count($searchcriterion) != 5) { continue; } $label = $showlabel ? "{$fieldlabel}{$i}" : ''; list($fieldid, $andor, $not, $operator, $value) = $searchcriterion; $arr = array(); $arr[] =& $mform->createElement('select', "searchandor{$i}", null, $andoroptions); $arr[] =& $mform->createElement('selectgroups', "searchfield{$i}", null, $fieldoptions); $arr[] =& $mform->createElement('select', "searchnot{$i}", null, $isnotoptions); $arr[] =& $mform->createElement('select', "searchoperator{$i}", '', $operatoroptions); $arr[] =& $mform->createElement('text', "searchvalue{$i}", ''); $mform->addGroup($arr, "customsearcharr{$i}", $label, ' ', false); $mform->setType("searchvalue{$i}", PARAM_TEXT); $mform->setDefault("searchandor{$i}", $andor); $mform->setDefault("searchfield{$i}", $fieldid); $mform->setDefault("searchnot{$i}", $not); $mform->setDefault("searchoperator{$i}", $operator); $mform->setDefault("searchvalue{$i}", $value); $mform->disabledIf("searchfield{$i}", "searchandor{$i}", 'eq', ''); $mform->disabledIf("searchnot{$i}", "searchfield{$i}", 'eq', ''); $mform->disabledIf("searchoperator{$i}", "searchfield{$i}", 'eq', ''); $mform->disabledIf("searchvalue{$i}", "searchoperator{$i}", 'eq', ''); if ($i) { $prev = $i - 1; $mform->disabledIf("customsearcharr{$i}", "searchfield{$prev}", 'eq', ''); } $i++; } }
/** * Deletes plugin instances. * * @return void */ public function delete_instances() { global $DB; $params = array('type' => $this->name); if (!($instances = $DB->get_records_menu('dataform_fields', $params, 'dataid', 'id,dataid'))) { return; } $bydataform = array(); foreach ($instances as $instanceid => $dataformid) { if (empty($bydataform[$dataformid])) { $bydataform[$dataformid] = array(); } $bydataform[$dataformid][] = $instanceid; } foreach ($bydataform as $dataformid => $instanceids) { $entryman = \mod_dataform_field_manager::instance($dataformid); $entryman->process_fields('delete', $instanceids, true); } }