Fixed bug in /mod/data/filter.php - content list for autolinking was not being

generated properly.

Updated /mod/data/fields.php - deletion of a field now also deletes
data_contents and any files associated as well.

Add/update/delete fields - template now gets updates in the following cases:
1) Deletion: remove template tags for the field from all templates
2) Edit: search and replace template tags for the field in all templates
3) Add: append template tag for field into the signe and rss templates, but
only if these templates are not empty. Otherwise we'd rather the user uses the
automatic template generation facility, which produces better display.
This commit is contained in:
vyshane 2006-02-06 09:13:37 +00:00
parent 20f601d324
commit f9eab7b0c9
4 changed files with 114 additions and 9 deletions

View file

@ -249,7 +249,28 @@ class data_field_file extends data_field_base {// extends
}
return false;
}
function delete_data_contents() {
// Delete the content files first.
$sql = 'SELECT dc.content AS content, '.
'df.dataid AS dataid, '.
'dc.recordid AS recordid '.
'FROM data_fields AS df, '.
'data_content AS dc '.
"WHERE df.id = {$this->id} ".
"AND dc.fieldid = {$this->id}";
if (!$results = recordset_to_array(get_recordset_sql($sql))) {
return false;
}
foreach ($results as $result) {
$this->delete_data_content_files($result->dataid, $result->recordid, $result->content);
}
// Then delete the data_content records.
return delete_records('data_content', 'fieldid', $this->id);
}
function delete_data_content_files($dataid, $recordid, $content){
global $CFG, $course;
$filepath = $CFG->dataroot.'/'.$course->id.'/'.$CFG->moddata.'/data/'.$dataid.'/'.$this->id.'/'.$recordid;

View file

@ -101,8 +101,11 @@
$field->$key = $str;
}
}
insert_record('data_fields', $field);
// Add the new field to the form templates.
data_append_field_in_form($field->dataid, $field->name);
$displayflag = get_string('fieldadded','data');
}
else { //no duplicate names allowed in one database!
@ -111,13 +114,27 @@
}
break;
case 'delete': ///delete a field
case 'delete': // Delete a field
if (confirm_sesskey()){
if ($confirm = optional_param('confirm', 0, PARAM_INT)){
delete_records('data_fields','id',$fid);
$displayflag = get_string('fielddeleted','data');
if ($confirm = optional_param('confirm', 0, PARAM_INT)) {
// Delete the associated data_contents and files.
if (!$fieldRecordSet = get_record('data_fields', 'id', $fid)) {
notify('Field not found');
exit;
}
$field = data_get_field($fieldRecordSet);
$field->delete_data_contents();
// Update the templates.
data_replace_field_in_forms($fieldRecordSet->dataid, $fieldRecordSet->name, '');
// Delete the field.
delete_records('data_fields', 'id', $fid);
$displayflag = get_string('fielddeleted', 'data');
}
else { //prints annoying confirmation dialogue
else {
// Print confirmation message.
$field = get_record('data_fields','id',$fid);
print_simple_box_start('center', '60%');
echo '<div align="center">';
@ -152,6 +169,10 @@
$g = data_get_field($currentfield);
$g->update($field);
unset($g);
// Update the templates.
data_replace_field_in_forms($currentfield->dataid, $currentfield->name, $field->name);
$displayflag = get_string('fieldupdated','data');
}
else {

View file

@ -35,6 +35,7 @@
'data_content AS dc ' .
"WHERE d.course = '$courseid' " .
'AND d.id = df.dataid ' .
'AND df.id = dc.fieldid ' .
'AND d.id = dr.dataid ' .
'AND dr.id = dc.recordid ' .
"AND df.type = 'text' " .

View file

@ -191,15 +191,22 @@ class data_field_base { //base class (text field)
* $param string $name *
* output boolean *
*************************************************************/
function notemptyfield($value, $name){
function notemptyfield($value, $name) {
return !empty($value);
}
/*********************************************************************
* This function deletes all data_contents associated with the field *
*********************************************************************/
function delete_data_contents() {
return delete_records('data_content', 'fieldid', $this->id);
}
/*************************************************************************
* This function checks if there's any file associated with this file, *
* and is uploaded, if so, it deletes it. E.g file or picture type field *
*************************************************************************/
function delete_data_content_files($dataid, $recordid, $content){
function delete_data_content_files($dataid, $recordid, $content) {
//does nothing
}
@ -271,6 +278,8 @@ function data_generate_default_form($dataid, $mode){
update_record('data', $data);
}
}
/*********************************************************
* generates an empty add form, if there is none. *
* input: @(int)id, id of the data *
@ -314,6 +323,59 @@ function data_generate_empty_add_form($id, $rid=0){
}
/***********************************************************************
* Search for a field name and replaces it with another one in all the *
* form templates. Set $newfieldname as '' if you want to delete the *
* field from the form. *
***********************************************************************/
function data_replace_field_in_forms($dataid, $searchfieldname, $newfieldname) {
if (!$data = get_record('data', 'id', $dataid)) {
// Form does not exist.
return false;
}
if (!empty($newfieldname)) {
$prestring = '[[';
$poststring = ']]';
}
else {
$prestring = '';
$poststring = '';
}
$data->singletemplate = str_replace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->singletemplate);
$data->listtemplate = str_replace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->listtemplate);
$data->addtemplate = str_replace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->addtemplate);
$data->rsstemplate = str_replace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->rsstemplate);
update_record('data', $data);
}
/********************************************************
* Appends a new field at the end of the form template. *
********************************************************/
function data_append_field_in_form($dataid, $newfieldname) {
if (!$data = get_record('data', 'id', $dataid)) {
// Form does not exist.
return false;
}
if (!empty($data->singletemplate)) {
$data->singletemplate .= ' [[' . $newfieldname .']]';
}
if (!empty($data->rsstemplate)) {
$data->rsstemplate .= ' [[' . $newfieldname . ']]';
}
update_record('data', $data);
}
/************************************************************************
* give a name in the format of field_## where ## is the field id, *
* this function creates an instance of the particular subfield class *