function specific_validation($data) { $valid = true; $optionlist = array(); if (in_array('optionlist', array_keys((array) $data))) { //$optionlist = ilp_element_plugin_itemlist::optlist2Array( $data[ 'optionlist' ] ); $optionlist = ilp_element_plugin_itemlist::optlist2Array($data->optionlist); } //optionlist is now the options just submitted from the mform, but this is not complete: //we must merge it with the options from the items table //$data_exists = $this->dbc->listelement_item_exists( $this->data_entry_tablename, array( 'parent_id' => ILP_DEFAULT_USERSTATUS_RECORD ) ); //$status = new ilp_element_plugin_status(); $existing_options = $this->dbc->listelement_item_exists($this->items_tablename, array('parent_id' => ILP_DEFAULT_USERSTATUS_RECORD)); foreach ($existing_options as $obj) { //$optionlist[ $obj->value ] = $obj->name; $optionlist[$obj->value] = $obj->name; } //all contents of $data->fail and $data->pass must match valid keys or values in $optionlist $sep = "\n"; $keysep = ":"; $fail_item_list = explode($sep, $data->fail); $pass_item_list = explode($sep, $data->pass); foreach (array($fail_item_list, $pass_item_list) as $item_list) { foreach ($item_list as $submitted_item) { if (trim($submitted_item) && !$this->is_valid_item($submitted_item, $optionlist, $keysep)) { $this->errors[] = get_string('ilp_element_plugin_error_not_valid_item', 'block_ilp') . ": <em>{$submitted_item}</em>"; $valid = false; } } } return $valid; }
protected function specific_process_data($data) { $optionlist = isset($data->optionlist) ? ilp_element_plugin_itemlist::optlist2Array($data->optionlist) : array(); $sep = "\n"; $keysep = ":"; //entries from data to go into $this->tablename and $this->items_tablename $gradekeylist = array('pass', 'fail', 'notcounted'); foreach ($gradekeylist as $key) { $v = $key . '_list'; ${$v} = explode($sep, $data->{$key}); //deal with pesky whitespace foreach (${$v} as &$entry) { $entry = trim($entry); $entryparts = explode($keysep, $entry); if (1 < count($entryparts)) { //admin has copied a whole key:value string into the pass or fail textarea //so throw away the key $entry = $entryparts[1]; } } } //we now have 2 lists: $pass_list and $fail_list $plgrec = !empty($data->reportfield_id) ? $this->dbc->get_plugin_record($this->tablename, $data->reportfield_id) : false; if (empty($plgrec)) { //options for this dropdown need to be written to the items table //each option is one row $element_id = $this->dbc->create_plugin_record($this->tablename, $data); //$itemrecord is a container for item data $itemrecord = new stdClass(); $itemrecord->parent_id = $element_id; foreach ($optionlist as $key => $itemname) { //one item row inserted here $itemrecord->value = $key; $itemrecord->name = $itemname; $itemrecord->passfail = $this->deduceItemState(array($itemname, $key), $fail_list, $pass_list, $notcounted_list); $this->dbc->create_plugin_record($this->items_tablename, $itemrecord); } } else { //get the old record from the elements plugins table $oldrecord = $this->dbc->get_form_element_by_reportfield($this->tablename, $data->reportfield_id); $data_exists = $this->dbc->plugin_data_item_exists($this->tablename, $data->reportfield_id); $element_id = $this->dbc->get_element_id_from_reportfield_id($this->tablename, $data->reportfield_id); //$itemrecord is a container for item data $itemrecord = new stdClass(); $itemrecord->parent_id = $element_id; if (empty($data_exists)) { //no user data - go ahead and delete existing items for this element, to be replaced by the submitted ones in $data $delstatus = $this->dbc->delete_element_listitems($this->tablename, $data->reportfield_id); //if $delstatus false, there has been an error - alert the user } else { //user data has been submitted already - don't delete existing items, but add new ones if they are in $data //purge $optionlist of already existing item_keys //then it will be safe to write the items to the items table foreach ($optionlist as $key => $itemname) { if ($this->dbc->listelement_item_exists($this->items_tablename, array('parent_id' => $element_id, 'value' => $key))) { //this should never happen, because it shouldn't have passed validation, but you never know unset($optionlist[$key]); //alert the user } } } //now write fresh options from $data foreach ($optionlist as $key => $itemname) { //one item row inserted here $itemrecord->value = $key; $itemrecord->name = $itemname; $itemrecord->passfail = $this->deduceItemState(array($itemname, $key), $fail_list, $pass_list, $notcounted_list); $this->dbc->create_plugin_record($this->items_tablename, $itemrecord); } //create a new object to hold the updated data $pluginrecord = new stdClass(); $pluginrecord->id = $oldrecord->id; $pluginrecord->optionlist = $data->optionlist; $pluginrecord->selecttype = ILP_OPTIONSINGLE; //update the plugin with the new data //return $this->dbc->update_plugin_record($this->tablename,$pluginrecord); } }
protected function specific_process_data($data) { $optionlist = array(); if (in_array('optionlist', array_keys((array) $data))) { //dd type needs to take values from admin form and writen them to items table $optionlist = ilp_element_plugin_itemlist::optlist2Array($data->optionlist); } //entries from data to go into $this->tablename and $this->items_tablename $plgrec = !empty($data->reportfield_id) ? $this->dbc->get_plugin_record($this->tablename, $data->reportfield_id) : false; if (empty($plgrec)) { //options for this dropdown need to be written to the items table //each option is one row $element_id = $this->dbc->create_plugin_record($this->tablename, $data); //$itemrecord is a container for item data $itemrecord = new stdClass(); $itemrecord->parent_id = $element_id; foreach ($optionlist as $key => $itemname) { //one item row inserted here $itemrecord->value = $key; $itemrecord->name = $itemname; $this->dbc->create_plugin_record($this->items_tablename, $itemrecord); } } else { //get the old record from the elements plugins table $oldrecord = $this->dbc->get_form_element_by_reportfield($this->tablename, $data->reportfield_id); $data_exists = $this->dbc->plugin_data_item_exists($this->tablename, $data->reportfield_id); $element_id = $this->dbc->get_element_id_from_reportfield_id($this->tablename, $data->reportfield_id); //$itemrecord is a container for item data $itemrecord = new stdClass(); $itemrecord->parent_id = $element_id; if (empty($data_exists)) { //no user data - go ahead and delete existing items for this element, to be replaced by the submitted ones in $data $delstatus = $this->dbc->delete_element_listitems($this->tablename, $data->reportfield_id); //if $delstatus false, there has been an error - alert the user } else { //user data has been submitted already - don't delete existing items, but add new ones if they are in $data //purge $optionlist of already existing item_keys //then it will be safe to write the items to the items table foreach ($optionlist as $key => $itemname) { if ($this->dbc->listelement_item_exists($this->items_tablename, array('parent_id' => $element_id, 'value' => $key))) { //this should never happen, because it shouldn't have passed validation, but you never know unset($optionlist[$key]); //alert the user } } } //now write fresh options from $data foreach ($optionlist as $key => $itemname) { //one item row inserted here $itemrecord->value = $key; $itemrecord->name = $itemname; $this->dbc->create_plugin_record($this->items_tablename, $itemrecord); } //create a new object to hold the updated data $pluginrecord = new stdClass(); $pluginrecord->id = $oldrecord->id; $pluginrecord->optionlist = $data->optionlist; $pluginrecord->selecttype = ILP_OPTIONSINGLE; //update the plugin with the new data //return $this->dbc->update_plugin_record($this->tablename,$pluginrecord); } }