public static function add_entry($databaseid, $groupid, $data) {
        global $DB;

        $params = array('databaseid' => $databaseid, 'groupid' => $groupid, 'data' => $data);
        $params = self::validate_parameters(self::add_entry_parameters(), $params);
        $warnings = array();
        $fieldnotifications = array();

        list($database, $course, $cm, $context) = self::validate_database($params['databaseid']);
        // Check database is open in time.
        data_require_time_available($database, null, $context);

        // Determine default group.
        if (empty($params['groupid'])) {
            // Check to see if groups are being used here.
            $groupmode = groups_get_activity_groupmode($cm);
            if ($groupmode) {
                $groupid = groups_get_activity_group($cm);
            } else {
                $groupid = 0;
            }
        }

        // Group is validated inside the function.
        if (!data_user_can_add_entry($database, $groupid, $groupmode, $context)) {
            throw new moodle_exception('noaccess', 'data');
        }

        // Prepare the data as is expected by the API.
        $datarecord = new stdClass;
        foreach ($params['data'] as $data) {
            $subfield = ($data['subfield'] !== '') ? '_' . $data['subfield'] : '';
            // We ask for JSON encoded values because of multiple choice forms or checkboxes that use array parameters.
            $datarecord->{'field_' . $data['fieldid'] . $subfield} = json_decode($data['value']);
        }
        // Validate to ensure that enough data was submitted.
        $fields = $DB->get_records('data_fields', array('dataid' => $database->id));
        $processeddata = data_process_submission($database, $fields, $datarecord);

        // Format notifications.
        if (!empty($processeddata->fieldnotifications)) {
            foreach ($processeddata->fieldnotifications as $field => $notififications) {
                foreach ($notififications as $notif) {
                    $fieldnotifications[] = [
                        'fieldname' => $field,
                        'notification' => $notif,
                    ];
                }
            }
        }

        // Create a new (empty) record.
        $newentryid = 0;
        if ($processeddata->validated && $recordid = data_add_record($database, $groupid)) {
            $newentryid = $recordid;
            // Now populate the fields contents of the new record.
            data_add_fields_contents_to_new_record($database, $context, $recordid, $fields, $datarecord, $processeddata);
        }

        $result = array(
            'newentryid' => $newentryid,
            'generalnotifications' => $processeddata->generalnotifications,
            'fieldnotifications' => $fieldnotifications,
        );
        return $result;
    }
public static function add_entry($databaseid, $groupid, $data) {
        global $DB;

        $params = array('databaseid' => $databaseid, 'groupid' => $groupid, 'data' => $data);
        $params = self::validate_parameters(self::add_entry_parameters(), $params);
        $warnings = array();
        $fieldnotifications = array();

        list($database, $course, $cm, $context) = self::validate_database($params['databaseid']);
        // Check database is open in time.
        data_require_time_available($database, null, $context);

        // Determine default group.
        if (empty($params['groupid'])) {
            // Check to see if groups are being used here.
            $groupmode = groups_get_activity_groupmode($cm);
            if ($groupmode) {
                $groupid = groups_get_activity_group($cm);
            } else {
                $groupid = 0;
            }
        }

        // Group is validated inside the function.
        if (!data_user_can_add_entry($database, $groupid, $groupmode, $context)) {
            throw new moodle_exception('noaccess', 'data');
        }

        // Prepare the data as is expected by the API.
        $datarecord = new stdClass;
        foreach ($params['data'] as $data) {
            $subfield = ($data['subfield'] !== '') ? '_' . $data['subfield'] : '';
            // We ask for JSON encoded values because of multiple choice forms or checkboxes that use array parameters.
            $datarecord->{'field_' . $data['fieldid'] . $subfield} = json_decode($data['value']);
        }
        // Validate to ensure that enough data was submitted.
        $fields = $DB->get_records('data_fields', array('dataid' => $database->id));
        $processeddata = data_process_submission($database, $fields, $datarecord);

        // Format notifications.
        if (!empty($processeddata->fieldnotifications)) {
            foreach ($processeddata->fieldnotifications as $field => $notififications) {
                foreach ($notififications as $notif) {
                    $fieldnotifications[] = [
                        'fieldname' => $field,
                        'notification' => $notif,
                    ];
                }
            }
        }

        // Create a new (empty) record.
        $newentryid = 0;
        if ($processeddata->validated && $recordid = data_add_record($database, $groupid)) {
            $newentryid = $recordid;
            // Now populate the fields contents of the new record.
            data_add_fields_contents_to_new_record($database, $context, $recordid, $fields, $datarecord, $processeddata);
        }

        $result = array(
            'newentryid' => $newentryid,
            'generalnotifications' => $processeddata->generalnotifications,
            'fieldnotifications' => $fieldnotifications,
        );
        return $result;
    }
public static function add_entry ( $ databaseid , $ groupid , $ data ) { global $ DB ; $ params = array ( 'databaseid' => $ databaseid , 'groupid' => $ groupid , 'data' => $ data ) ; $ params = self :: validate_parameters ( self :: add_entry_parameters ( ) , $ params ) ; $ warnings = array ( ) ; $ fieldnotifications = array ( ) ; list ( $ database , $ course , $ cm , $ context ) = self :: validate_database ( $ params [ 'databaseid' ] ) ; // Check database is open in time. data_require_time_available ( $ database , null , $ context ) ; // Determine default group. if ( empty ( $ params [ 'groupid' ] ) ) { // Check to see if groups are being used here. $ groupmode = groups_get_activity_groupmode ( $ cm ) ; if ( $ groupmode ) { $ groupid = groups_get_activity_group ( $ cm ) ; } else { $ groupid = 0 ; } } // Group is validated inside the function. if ( ! data_user_can_add_entry ( $ database , $ groupid , $ groupmode , $ context ) ) { throw new moodle_exception ( 'noaccess' , 'data' ) ; } // Prepare the data as is expected by the API. $ datarecord = new stdClass ; foreach ( $ params [ 'data' ] as $ data ) { $ subfield = ( $ data [ 'subfield' ] !== '' ) ? '_' . $ data [ 'subfield' ] : '' ; // We ask for JSON encoded values because of multiple choice forms or checkboxes that use array parameters. $ datarecord -> { 'field_' . $ data [ 'fieldid' ] . $ subfield } = json_decode ( $ data [ 'value' ] ) ; } // Validate to ensure that enough data was submitted. $ fields = $ DB -> get_records ( 'data_fields' , array ( 'dataid' => $ database -> id ) ) ; $ processeddata = data_process_submission ( $ database , $ fields , $ datarecord ) ; // Format notifications. if ( ! empty ( $ processeddata -> fieldnotifications ) ) { foreach ( $ processeddata -> fieldnotifications as $ field => $ notififications ) { foreach ( $ notififications as $ notif ) { $ fieldnotifications [ ] = [ 'fieldname' => $ field , 'notification' => $ notif , ] ; } } } // Create a new (empty) record. $ newentryid = 0 ; if ( $ processeddata -> validated && $ recordid = data_add_record ( $ database , $ groupid ) ) { $ newentryid = $ recordid ; // Now populate the fields contents of the new record. data_add_fields_contents_to_new_record ( $ database , $ context , $ recordid , $ fields , $ datarecord , $ processeddata ) ; } $ result = array ( 'newentryid' => $ newentryid , 'generalnotifications' => $ processeddata -> generalnotifications , 'fieldnotifications' => $ fieldnotifications , ) ; return $ result ; }
Adds a new entry to a database

@param int $databaseid the data instance id
@param int $groupid (optional) group id, 0 means that the function will determine the user group
@param array $data the fields data to be created
@return array of warnings and status result
@since Moodle 3.3
@throws moodle_exception
Adds a new entry to a database