Activity plugin form with external API call and dynamic fields

Activity plugin form with external API call and dynamic fields

di F S -
Numero di risposte: 3

Hello,

my teammate and myself are developing a new activity plugin to display videos from a server on a course page.

We are stuck with the mod_form page.

Here is what we would like to achieve (prototype below to clarify) :

  1.  The user selects, in a drop down list, the type of video search request he wants to send to the server.
  2. He writes keywords in the input text below to specify the request value
  3. He clicks on the Search button.
  4. The form creates a GET request with the url to the server and the request parameters defined by the user above
  5. The server sends back a JSON answer containing the data of the videos
  6. These data are displayed in a table. Each row corresponds to one video.
  7. The user can select one video in the table, via a radio button (for now).
  8. He submits the form with the Save button.
  9. The video data are stored in our plugin database

We are asked to use the lib/formslib API for maintenance purpose. We have searched a lot in this forum about dynamic forms.
Right now we are working with the definition() and definition_after_data() methods but we don't know if it is the right way to achieve everything.

Is it possible to do it exclusively in PHP with the formlib API so that we can get the values of the user inputs on the go (and modify the form in function of thiese values), or do we have to use javascript, AJAX, Jquery to modify the form on the client side ?

Thanks
Allegato Capture d’écran 2019-04-27 à 15.11.32.png
Media dei voti:  -
In riposta a F S

Re: Activity plugin form with external API call and dynamic fields

di Marcus Green -
Immagine Core developers Immagine Particularly helpful Moodlers Immagine Plugin developers Immagine Testers

You probably need to investigate the form fragment api. It is not simple and I only discovered it recently when my very smart work colleague did  most of the  work while I thought 'I really need to get to know that'

Also I am doubtful about storing video within the Moodle hashed file format and you might want to consider an alternative such as a repository. I worked on a image/video related thing and we decided to use links to Youtube/Vimeo for our purposes to keep things simpler. We store *.png/jpg etc within the Moodle file system.

In riposta a Marcus Green

Re: Activity plugin form with external API call and dynamic fields

di F S -

Thanks for your answer.

Actually, we're not storing the video file, only the uri to the video file. When the user is the video activity view, the view loads this uri into the simple HTTP player.

We're reading everything we can find about forms, mod_forms, etc.. but we haven't found the answer yet. 

Is there a way to have 2 separate forms as :

  • the 1st one submits the url with the research keywords to the 2nd one
  • the 2nd one displays the table of video, and the user selects one of them. This is the form that creates the instance.
In riposta a F S

Re: Activity plugin form with external API call and dynamic fields

di Darko Miletić -

There are many ways to deal with this issue but for a start why don't you look into autocomplete field

https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#autocomplete

With regards to the change of form this might help:

class myform extends moodleform {

    protected function definition() {
        $mform = $this->_form;

        $mform->addElement('select', 'myselect', 'select', [1 => 'first', 2 => 'second', 3 => 'third']);
        $mform->setType('myselect', PARAM_INT);

        $this->add_action_buttons();
    }

    public function definition_after_data() {
        $mform = $this->_form;
        if ($this->is_submitted()) {
            $data = $this->get_data();
            if ($data and !empty($data->myselect)) {
                $mform->addElement('header', 'aaa', 'bbbb');
            }
        }
    }

}

We check if the value of select is something other than 0 and if it is a random element is added.