How to Capturing the topic_id variable inside a form action (controller with route)? [resolved]

Discussion forum for Extension Writers regarding Extension Development.
User avatar
caiocald
Registered User
Posts: 213
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

How to Capturing the topic_id variable inside a form action (controller with route)? [resolved]

Post by caiocald »

After these last 10 months studying php I think I finally got the logic behind in programming.

Unfortunately I am not a programmer and my college is from a completely different area. So all my knowledge on the subject is still very limited.

So I decided to redo the topic on my extension project. Now I think I already know what to ask to be able to actually remedy my doubt.



Come on,
As some already know, I'm on the journey to create my first phpbb extension. It's not being easy, so I ask for your help.

My extension will basically be a punctuation tool for essays. Each topic created by the user (previously determined), will represent a text to be evaluated.

Currently, I've only been able to configure migration and template events.

This is my extension project in partial operation:
https://goo.gl/i9qtVT

link to the project repository:
https://goo.gl/jtQbnm

I think that's all I could put of information. Thank you guys :D !!!
Last edited by caiocald on Sat Dec 22, 2018 2:49 pm, edited 4 times in total.
User avatar
caiocald
Registered User
Posts: 213
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald »

The question that I want to do at the moment and that does not let me advance in the studies is: where do i start within the core events functions of phpbb?
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by mrgoldy »

You would have to use an "event listener" and listen to phpBB core events.
See Tutorial: Events
And Event list
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
caiocald
Registered User
Posts: 213
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald »

mrgoldy wrote: Mon Dec 17, 2018 12:04 pm You would have to use an "event listener" and listen to phpBB core events.
See Tutorial: Events
And Event list
Hi, Gijs. Thanks for the answer.

At the moment, I find myself "stuck" in this part of the tutorial. I do not know which core event to begin with.
At first, I'm trying to pull the information (manually injected) from the data pool to fill in the fields of my tool.

I need the class to get topic_id and user_id on the viewtopic page. For this, I found the most appropriate "core.viewtopic_modify_post_data" event.
But I still can not make progress.

In my normal php code, I can pull the data like this:

Code: Select all

        $consulta_nota_geral = "SELECT COUNT(*) as number_of_rows , avg(comp1) as competencia1 , avg(comp2) as competencia2 , avg(comp3) as competencia3 , avg(comp4) as competencia4 , avg(comp5) as competencia5  FROM phpbb_pontuacao_enem WHERE topic_id = '$topic_id'";
        $consulta_nota_geral_conect = mysqli_query($conn,$consulta_nota_geral) or die (mysqli_error($conn));
        $nota_geral = mysqli_fetch_assoc($consulta_nota_geral_conect);
        $numero_linhas = $nota_geral["number_of_rows"];
        round('2.57',1);
        if ($nota_geral['number_of_rows'] > 0) {
            $nota_geral_total = $nota_geral['competencia1'] + $nota_geral['competencia2'] + $nota_geral['competencia3'] + $nota_geral['competencia4'] + $nota_geral['competencia5'];
        }*/
However, I do not know how to convert this to a phpbb class.
Do you have any study tips for that? What is the name of the concept that I have to study? Any name already helps me search exactly what I need in google.

Thank you one more time
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by mrgoldy »

well, you asked on how to achieve something through events, that's why I provided those links.
However, events are when phpBB "does" something and you want to do something on that event aswell.
For example if someone makes a post, phpBB triggers multiple events which you can use to also do something with/to the post.

What you're trying to do, looks more like a separate action, not something that phpBB is already doing and you want to do something with/to.
That means you will probably have to create a route and a controller.
So you add a link to your route with the necessary information and do your stuff in the controller.
Which you can read more about here: Tutorial: Controllers and Routes

So in your case it would probably be something like a <form> with the action="" attribute set to your controller.
You request all the information from the form into your controller, do your calculations and output a page or redirect the user to where he was.

Side note, from the small piece of code you provided.
Coding in your native language might be easier for you, but if you seek help it's rather disadvantageous as nobody will know what it means.
Aswell as you're using mysqli_fetch_assoc() and such, while you should use the phpbb\db\driver\driver_interface to make sure your extension works for all database types.

Basically, before trying to make your own extension work, go through the example extension provided in the linked tutorials. That should give you a better understanding on how things work and what is possible (and what not). Or have a look at the phpBB Skeleton extension.
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
caiocald
Registered User
Posts: 213
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald »

mrgoldy wrote: Mon Dec 17, 2018 12:34 pm well, you asked on how to achieve something through events, that's why I provided those links.
However, events are when phpBB "does" something and you want to do something on that event aswell.
For example if someone makes a post, phpBB triggers multiple events which you can use to also do something with/to the post.

What you're trying to do, looks more like a separate action, not something that phpBB is already doing and you want to do something with/to.
That means you will probably have to create a route and a controller.
So you add a link to your route with the necessary information and do your stuff in the controller.
Which you can read more about here: https://area51.phpbb.com/docs/dev/3.2.x ... llers.html

So in your case it would probably be something like a <form> with the action="" attribute set to your controller.
You request all the information from the form into your controller, do your calculations and output a page or redirect the user to where he was.

Side note, from the small piece of code you provided.
Coding in your native language might be easier for you, but if you seek help it's rather disadvantageous as nobody will know what it means.
Aswell as you're using mysqli_fetch_assoc() and such, while you should use the phpbb\db\driver\driver_interface to make sure your extension works for all database types.

Basically, before trying to make your own extension work, go through the example extension provided in the linked tutorials. That should give you a better understanding on how things work and what is possible (and what not). Or have a look at the phpBB Skeleton extension.
Great answer. I really was not sure whether to use the core event or create a controller.

At the moment I'm using a pre-generated file in skeleton extension. It has helped me a lot to understand.

Do you have any extension that has a function similar to the one I want so I can study it?

Will not my project need a core event?

I'm sorry, I'll put the tags of my project in English
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by mrgoldy »

Once again, I do not know your project, nor do I (or anyone else probably) have the time to dig into it for you.
And there are a lot of extensions out there, some of which are likely to do something similar to what you're after.
You will have to look for yourself through the Customisation Database or the Extensions in Development and see if something is close to what you are after.

We are here to help you with specific issues, not to write the extension for you ;).
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
caiocald
Registered User
Posts: 213
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald »

Continuing ..

I created a controller and a php core to capture data from the database.

Code: Select all

 public function getuserandtopicid()
    {
        $sql = 'SELECT * 
                FROM ' . PONTUACAO_ENEM_TABLE . '
                WHERE user_id = ' . $this->user->data['user_id'];
        $result = $this->db->sql_query($sql);
        $rowset = $this->db->sql_fetchrowset($result);
        $user_id = $this->db->sql_fetchfield('user_id');
        $this->db->sql_freeresult($result);
        $rows = sizeof($rowset);

        return true;
    }
How can I capture the topic_id url that my form is located in?
Last edited by caiocald on Wed Dec 19, 2018 9:27 am, edited 1 time in total.
User avatar
Toxyy
Registered User
Posts: 950
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by Toxyy »

caiocald wrote: Tue Dec 18, 2018 11:47 pm How can I capture the topic_id to use in the WHERE function if I need to?
My own example with username:

Code: Select all

public function get_username($user_id)
{
        $sql = 'SELECT username
                FROM ' . USERS_TABLE . '
                WHERE user_id = ' . $user_id;
        $result = $this->db->sql_query($sql);
        $username = $this->db->sql_fetchfield('username');
        $this->db->sql_freeresult($result);
        unset($result);
        return $username;
}
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
User avatar
caiocald
Registered User
Posts: 213
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald »

Toxyy wrote: Wed Dec 19, 2018 12:41 am
caiocald wrote: Tue Dec 18, 2018 11:47 pm How can I capture the topic_id to use in the WHERE function if I need to?
My own example with username:

Code: Select all

public function get_username($user_id)
{
        $sql = 'SELECT username
                FROM ' . USERS_TABLE . '
                WHERE user_id = ' . $user_id;
        $result = $this->db->sql_query($sql);
        $username = $this->db->sql_fetchfield('username');
        $this->db->sql_freeresult($result);
        unset($result);
        return $username;
}
for the reply. I tried capturing the topic_id but I could not.

Ex.:
i can capture "User_id" with

Code: Select all

user_id = ' . $this->user->data['user_id'];
But "topic_id" does not work ...
thanks
User avatar
GanstaZ
Registered User
Posts: 1187
Joined: Wed Oct 11, 2017 10:29 pm
Location: GZOverse

Re: Help with the adaptation of a tool in php for phpbb extension

Post by GanstaZ »

Here you can find some basic examples Database Abstraction Layer and wiki:Database. You should read or re-read everything in phpBB Development Documents.
Usus est magister optimus! phpBB pre-Triton & latest php environment.
When answer lies in the question, question becomes redundant!
User avatar
caiocald
Registered User
Posts: 213
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald »

GanstaZ wrote: Wed Dec 19, 2018 4:12 pm Here you can find some basic examples Database Abstraction Layer and wiki:Database. You should read or re-read everything in phpBB Development Documents.

Gantaz, thanks for the tip!
I was a bit confused by that.

To capture the topic_id of the url in which my form is located, would it be something related to $ request or $ db?

Sorry, I'm learning about phpbb.
User avatar
GanstaZ
Registered User
Posts: 1187
Joined: Wed Oct 11, 2017 10:29 pm
Location: GZOverse

Re: Help with the adaptation of a tool in php for phpbb extension

Post by GanstaZ »

What do you mean by form? Is it forum?
Depends on what do you mean by get topic. Is it required for something or what is the purpose of getting it. The word request is already telling us, that one is requesting something and it can be combined with database or arrays or conditions and what not.
Best thing to do before starting a complex project is to write down a blueprint/sketch and use it as a base for development.
Usus est magister optimus! phpBB pre-Triton & latest php environment.
When answer lies in the question, question becomes redundant!
User avatar
caiocald
Registered User
Posts: 213
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald »

GanstaZ wrote: Wed Dec 19, 2018 6:53 pm What do you mean by form? Is it forum?
Depends on what do you mean by get topic. Is it required for something or what is the purpose of getting it. The word request is already telling us, that one is requesting something and it can be combined with database or arrays or conditions and what not.
Best thing to do before starting a complex project is to write down a blueprint/sketch and use it as a base for development.
Form is the form of my extension that is located in the footer of the viewtopic page
Attachments
Capturar.PNG
User avatar
caiocald
Registered User
Posts: 213
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald »

mrgoldy wrote: Mon Dec 17, 2018 12:45 pm Once again, I do not know your project, nor do I (or anyone else probably) have the time to dig into it for you.
And there are a lot of extensions out there, some of which are likely to do something similar to what you're after.
You will have to look for yourself through the Customisation Database or the Extensions in Development and see if something is close to what you are after.

We are here to help you with specific issues, not to write the extension for you ;).
Mr. Goldy.


Please help me at least with that.
I can not capture the topic_id of the url in any way.
I've read all the tutorials I could find, but none works.(https://wiki.phpbb.com/PhpBB3.1/RFC/Request_class)

Code: Select all

 $topic_id = $this->request->variable('t',0);
or

Code: Select all

$topic_id = $this->request->is_set('t');
I'm still lay in phpbb. I am stagnant in my studies because I need to be able to capture this variable to continue building my extension.

In normal PHP I would get with a GET, but in phpbb I do not know

Code: Select all

topic_id = $_GET['t'];
There is almost no content on phpbb in my native language. This forum is my last hope.

PLEASE
Last edited by caiocald on Fri Dec 21, 2018 12:55 pm, edited 4 times in total.
Post Reply

Return to “Extension Writers Discussion”