<commit_before><?php  // $Id$
///////////////////////////////////////////////////////////////////////////
//                                                                       //
// NOTICE OF COPYRIGHT                                                   //
//                                                                       //
// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
//          http://moodle.org                                            //
//                                                                       //
// Copyright (C) 2005 <NAME>  http://dougiamas.com             //
//                                                                       //
// This program 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 2 of the License, or     //
// (at your option) any later version.                                   //
//                                                                       //
// This program 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:                          //
//                                                                       //
//          http://www.gnu.org/copyleft/gpl.html                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

    require_once('../../config.php');
    require_once('lib.php');

    
    $id             = optional_param('id', 0, PARAM_INT);            // course module id
    $d              = optional_param('d', 0, PARAM_INT);             // database id
    $fid            = optional_param('fid', 0 , PARAM_INT);          // update field id
    $newtype        = optional_param('newtype','',PARAM_ALPHA);      // type of the new field
    $mode           = optional_param('mode','',PARAM_ALPHA);
    $defaultsort    = optional_param('defaultsort', 0, PARAM_INT);
    $defaultsortdir = optional_param('defaultsortdir', 0, PARAM_INT);
    $cancel         = optional_param('cancel', '');

    if ($cancel) {
        $mode = 'list';
    }
    
    
    if ($id) {
        if (! $cm = get_record('course_modules', 'id', $id)) {
            error('Course Module ID was incorrect');
        }
        if (! $course = get_record('course', 'id', $cm->course)) {
            error('Course is misconfigured');
        }
        if (! $data = get_record('data', 'id', $cm->instance)) {
            error('Course module is incorrect');
        }

    } else {
        if (! $data = get_record('data', 'id', $d)) {
            error('Data ID is incorrect');
        }
        if (! $course = get_record('course', 'id', $data->course)) {
            error('Course is misconfigured');
        }
        if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
            error('Course Module ID was incorrect');
        }
    }

    require_course_login($course, true, $cm);

    if (!isteacheredit($course->id)){
        error(get_string('noaccess','data'));
    }

    
    
    /************************************
     *        Data Processing           *
     ***********************************/
    switch ($mode) {

        case 'add':    ///add a new field
            if (confirm_sesskey() and $fieldinput = data_submitted($CFG->wwwroot.'/mod/data/field.php')){

            /// Only store this new field if it doesn't already exist.
                if (data_fieldname_exists($fieldinput->name, $data->id)) {

                    $displaynoticebad = get_string('invalidfieldname','data');

                } else {   
                    
                /// Check for arrays and convert to a comma-delimited string
                    data_convert_arrays_to_strings($fieldinput);

                /// Create a field object to collect and store the data safely
                    $type = required_param('type', PARAM_FILE);
                    $field = data_get_field_new($type, $data);

                    $field->define_field($fieldinput);
                    $field->insert_field();

                /// Update some templates
                    data_append_new_field_to_templates($data, $field->field->name);

                    add_to_log($course->id, 'data', 'fields add', 
                               "field.php?d=$data->id&amp;mode=display&amp;fid=$fid", $fid, $cm->id);
                    
                    $displaynoticegood = get_string('fieldadded','data');
                }
            }
            break;


        case 'update':    ///update a field
            if (confirm_sesskey() and $fieldinput = data_submitted($CFG->wwwroot.'/mod/data/field.php')){

                $fieldinput->name = optional_param('name','',PARAM_NOTAGS);

                if (data_fieldname_exists($fieldinput->name, $data->id, $fid)) {
                    $displaynoticebad = get_string('invalidfieldname','data');

                } else {
                /// Check for arrays and convert to a comma-delimited string
                    data_convert_arrays_to_strings($fieldinput);

                /// Create a field object to collect and store the data safely
                    $field = data_get_field_from_id($fid, $data);
                    $oldfieldname = $field->field->name;<commit_msg>Bug #5680 - Fixed duplicate field names; merged from MOODLE_16_STABLE<commit_after><?php  // $Id$
///////////////////////////////////////////////////////////////////////////
//                                                                       //
// NOTICE OF COPYRIGHT                                                   //
//                                                                       //
// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
//          http://moodle.org                                            //
//                                                                       //
// Copyright (C) 2005 <NAME>  http://dougiamas.com             //
//                                                                       //
// This program 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 2 of the License, or     //
// (at your option) any later version.                                   //
//                                                                       //
// This program 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:                          //
//                                                                       //
//          http://www.gnu.org/copyleft/gpl.html                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

    require_once('../../config.php');
    require_once('lib.php');

    
    $id             = optional_param('id', 0, PARAM_INT);            // course module id
    $d              = optional_param('d', 0, PARAM_INT);             // database id
    $fid            = optional_param('fid', 0 , PARAM_INT);          // update field id
    $newtype        = optional_param('newtype','',PARAM_ALPHA);      // type of the new field
    $mode           = optional_param('mode','',PARAM_ALPHA);
    $defaultsort    = optional_param('defaultsort', 0, PARAM_INT);
    $defaultsortdir = optional_param('defaultsortdir', 0, PARAM_INT);
    $cancel         = optional_param('cancel', '');

    if ($cancel) {
        $mode = 'list';
    }
    
    
    if ($id) {
        if (! $cm = get_record('course_modules', 'id', $id)) {
            error('Course Module ID was incorrect');
        }
        if (! $course = get_record('course', 'id', $cm->course)) {
            error('Course is misconfigured');
        }
        if (! $data = get_record('data', 'id', $cm->instance)) {
            error('Course module is incorrect');
        }

    } else {
        if (! $data = get_record('data', 'id', $d)) {
            error('Data ID is incorrect');
        }
        if (! $course = get_record('course', 'id', $data->course)) {
            error('Course is misconfigured');
        }
        if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
            error('Course Module ID was incorrect');
        }
    }

    require_course_login($course, true, $cm);

    if (!isteacheredit($course->id)){
        error(get_string('noaccess','data'));
    }

    
    
    /************************************
     *        Data Processing           *
     ***********************************/
    switch ($mode) {

        case 'add':    ///add a new field
            if (confirm_sesskey() and $fieldinput = data_submitted($CFG->wwwroot.'/mod/data/field.php')){

                $fieldinput->name = clean_param($fieldinput->name, PARAM_NOTAGS);
                $fieldinput->name = trim($fieldinput->name);
                
            /// Only store this new field if it doesn't already exist.
                if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id)) {

                    $displaynoticebad = get_string('invalidfieldname','data');

                } else {   
                    
                /// Check for arrays and convert to a comma-delimited string
                    data_convert_arrays_to_strings($fieldinput);

                /// Create a field object to collect and store the data safely
                    $type = required_param('type', PARAM_FILE);
                    $field = data_get_field_new($type, $data);

                    $field->define_field($fieldinput);
                    $field->insert_field();

                /// Update some templates
                    data_append_new_field_to_templates($data, $field->field->name);

                    add_to_log($course->id, 'data', 'fields add', 
                               "field.php?d=$data->id&amp;mode=display&amp;fid=$fid", $fid, $cm->id);
                    
                    $displaynoticegood = get_string('fieldadded','data');
                }
            }
            break;


        case 'update':    ///update a field
            if (confirm_sesskey() and $fieldinput = data_submitted($CFG->wwwroot.'/mod/data/field.php')){

                $fieldinput->name = clean_param($fieldinput->name, PARAM_NOTAGS);
                $fieldinput->name = trim($fieldinput->name);

                if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id, $fieldinput->fid)) {
                    
                    $displaynoticebad = get_string('invalidfieldname','data');

                } else {
                /// Check for arrays and convert to a comma-delimited string
                    data_convert_arrays_to_strings($fieldinput);

                /// Create a field object to collect and store the data safely
                    $field = data_get_field_from_id($fid, $data);
                    $oldfieldname = $field->field->name;
{'fraction_non_alphanumeric': 0.15605040566200587, 'fraction_numerical': 0.00224408769204212, 'mean_word_length': 1.2274125336409074, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1502947', 'n_tokens_mistral': 2823, 'n_tokens_neox': 2650, 'n_words': 669}