Custom Function - Requiring Reproducibility

Get help from other users here.

Moderators: Developer, Contributor

Post Reply
KenTech
Posts: 33
Joined: 20 Dec 2011, 18:27
Location: Burbank, CA
Contact:

Custom Function - Requiring Reproducibility

Post by KenTech »

I am trying to create a custom function so that the first option in the Reproduciblity field is blank or "Not Set", and when the user tries to create a bug with that selected it won't allow them.

Here is what I have so far:

In /lang/strings_english.txt

Code: Select all

$s_reproducibility_enum_string = '5:not set,10:always,30:sometimes,50:random,70:have not tried,90:unable to reproduce,100:N/A';
$MANTIS_ERROR[ERROR_VALIDATE_REPRO] = 'The bug cannot be created because %s';
In custom_constants_inc.php

Code: Select all

define( 'REPRODUCIBILITY_NOTSET', 5 );
define( 'ERROR_VALIDATE_REPRO', 2000 );
In config.php:

Code: Select all

$g_reproducibility_enum_string	= '5:not set,10:always,30:sometimes,50:random,70:have not tried,90:unable to duplicate,100:N/A';
$g_default_bug_reproducibility = REPRODUCIBILITY_NOTSET;
I think that is all correct. However, I'm having trouble writing the actual function. First, does custom_functions_inc.php need to have the PHP open/close tags in it? I'm having trouble figuring out how to get the value of the Reproducibility field.

Code: Select all

function custom_function_default_issue_create_validate( $p_new_bug ) {
    if ( $p_new_bug->reproducibility == NOTSET ) {
        error_parameters( 'the reproducibility has not been set' );
        trigger_error( ERROR_VALIDATE_REPRO, ERROR );
    }
}

Thanks for any help you can give.
KenTech
Posts: 33
Joined: 20 Dec 2011, 18:27
Location: Burbank, CA
Contact:

Re: Custom Function - Requiring Reproducibility

Post by KenTech »

bump?
istvanb
Posts: 226
Joined: 22 Aug 2010, 21:00

Re: Custom Function - Requiring Reproducibility

Post by istvanb »

I dont have too much time to review this thing at the moment. Although an easier (and not as elegant) solution would be to insert a script into the bug report page to reject the submission if the reproducibility field has a certain value.
atrol
Site Admin
Posts: 8366
Joined: 26 Mar 2008, 21:37
Location: Germany

Re: Custom Function - Requiring Reproducibility

Post by atrol »

KenTech wrote: In /lang/strings_english.txt
Do not change this file, use custom_strings_inc.php
KenTech wrote: In config.php:
Typo? Should be config_inc.php
KenTech wrote: First, does custom_functions_inc.php need to have the PHP open/close tags in it?
you should use only open tag
KenTech wrote:

Code: Select all

function custom_function_default_issue_create_validate( $p_new_bug ) {
The name of the custom function must be function custom_function_override_issue_create_validate
Please use Search before posting and read the Manual
KenTech
Posts: 33
Joined: 20 Dec 2011, 18:27
Location: Burbank, CA
Contact:

Re: Custom Function - Requiring Reproducibility

Post by KenTech »

atrol wrote:
KenTech wrote: In /lang/strings_english.txt
Do not change this file, use custom_strings_inc.php

KenTech wrote: In config.php:
Typo? Should be config_inc.php

KenTech wrote: First, does custom_functions_inc.php need to have the PHP open/close tags in it?
you should use only open tag
KenTech wrote:

Code: Select all

function custom_function_default_issue_create_validate( $p_new_bug ) {
The name of the custom function must be function custom_function_override_issue_create_validate
I could have sworn I read some instructions in the Mantis manual (maybe a user comment) that mentioned using the original strings file NOT the custom one. However, I will use the custom file from now on (makes sense for updates).

Yes, typo. :oops:

Got it, thanks for the help!
KenTech
Posts: 33
Joined: 20 Dec 2011, 18:27
Location: Burbank, CA
Contact:

Re: Custom Function - Requiring Reproducibility

Post by KenTech »

istvanb wrote:I dont have too much time to review this thing at the moment. Although an easier (and not as elegant) solution would be to insert a script into the bug report page to reject the submission if the reproducibility field has a certain value.
Actually, that is a great idea. I'm pretty adept at JavaScript, so I may just write a function that checks the value of the selected option and return an error if it contains a certain value.
KenTech
Posts: 33
Joined: 20 Dec 2011, 18:27
Location: Burbank, CA
Contact:

Re: Custom Function - Requiring Reproducibility

Post by KenTech »

istvanb,

Okay so I've assigned an ID of 'reproducibility' to that field and wrote this little function that I'm calling using the onclick event handler.

Code: Select all

	function validateForm() {
		var repro = document.getElementById('reproducibility');
		console.log(repro.selectedIndex);
		
		if (repro.selectedIndex === 0) {
			alert('Select the Reproducibility');
			return false;
		}
	}
The code works up to the return false; line. The web page then submits anyway. Does it look like I'm missing something?


NEVER MIND! I forgot that I need to call the function with a return statement:

Code: Select all

<form name="report_bug_form" method="post" <?php if ( $tpl_show_attachments ) { echo 'enctype="multipart/form-data"'; } ?> action="bug_report.php" onsubmit="return validateForm()">
KenTech
Posts: 33
Joined: 20 Dec 2011, 18:27
Location: Burbank, CA
Contact:

Re: Custom Function - Requiring Reproducibility

Post by KenTech »

Okay, who's the man? <double-thumbs>This guy!</double-thumbs>

I added an item to the Reproducibility enumeration for "not set". Then, I took a stab and wrote a custom function that would check to see if that was selected, and if so, don't let the bug be created. I also added a custom option to the config file for "allow_no_reproducibility" in case we change our mind. And you know what? It works!

custom_functions_inc.php

Code: Select all

function custom_function_override_issue_create_validate( $p_new_issue_data ) {
		/* Wait until Category is set before running this function
			since it sits above Reproducibility on the page */
		if ( $p_new_issue_data->category_id != 0 ) {
			if ( $p_new_issue_data->reproducibility == REPRODUCIBILITY_NOTSET && !config_get ( 'allow_no_reproducibility' ) ) {
					error_parameters( lang_get( 'reproducibility' ) );
					trigger_error( ERROR_EMPTY_FIELD, ERROR );
			}
		}
}
custom_constant_inc.php <--shouldn't that file use a plural noun and be called custom_constants_inc.php?

Code: Select all

// Custom enum for Reproducibility
define( 'REPRODUCIBILITY_NOTSET', 5 );
custom_strings_inc.php

Code: Select all

$s_reproducibility_enum_string	= '5:(not set),10:always,30:sometimes,50:random,70:have not tried,90:unable to duplicate,100:N/A';
config_inc.php

Code: Select all

$g_reproducibility_enum_string	= '5:not set,10:always,30:sometimes,50:random,70:have not tried,90:unable to duplicate,100:N/A';
$g_default_bug_reproducibility = REPRODUCIBILITY_NOTSET;
$g_allow_no_reproducibility = OFF;

One additional note:
I changed the order of the if statements in the validate function in bug_api.php so that Category gets checked before Summary and Description.

Code: Select all

/**
	 * validate current bug object for database insert/update
	 * triggers error on failure
	 * @param bool $p_update_extended
	 */
	function validate( $p_update_extended =  true) {
		# Make sure a category is set
		if( 0 == $this->category_id && !config_get( 'allow_no_category' ) ) {
			error_parameters( lang_get( 'category' ) );
			trigger_error( ERROR_EMPTY_FIELD, ERROR );
		}
	
		# Summary cannot be blank
		if( is_blank( $this->summary ) ) {
			error_parameters( lang_get( 'summary' ) );
			trigger_error( ERROR_EMPTY_FIELD, ERROR );
		}

		if( $p_update_extended ) {
			# Description field cannot be empty
			if( is_blank( $this->description ) ) {
				error_parameters( lang_get( 'description' ) );
				trigger_error( ERROR_EMPTY_FIELD, ERROR );
			}
		}

		if( !is_blank( $this->duplicate_id ) && ( $this->duplicate_id != 0 ) && ( $this->id == $this->duplicate_id ) ) {
			trigger_error( ERROR_BUG_DUPLICATE_SELF, ERROR );
			# never returns
		}
	}
Post Reply