how to write a code to edit any activity?

how to write a code to edit any activity?

by Monika Gujar -
Number of replies: 10

Hello,

I am developing moodle plugin (activity module) for the 1st time. As I am new to moodle, facing lots of challenges. I got stuck somewhere... After clicking on edit activity, I am getting the blank form, where as I want that form filled as we are editing the form.

I have written the code in lib.php to update the form data into the database, that's working fine.

I have only 1 issue, my form is not getting auto-filled after clicking on edit activity.

Average of ratings: -
In reply to Monika Gujar

Re: how to write a code to edit any activity?

by Monika Gujar -
Somebody... Please help
I want my form to be pre-filled when I click on edit
I am not getting any clue from other discussions
In reply to Monika Gujar

Re: how to write a code to edit any activity?

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi Monika,

Do you already have your code in a git ? (It will be more easy to help you.)

But In case of... I'll try that to help you.

in the mod_form.php

I've declare this public function

    // Load all the values from mymodule of the current activity.
    public function data_preprocessing(&$defaultvalues) {
        global $DB;

        $mymod= $DB->get_record('mymod', array('id'=>$this->current->id), '*', IGNORE_MISSING);
        if($dynamo != false) {
            $defaultvalues['mymod_participation'] = $mymod->val1;
            $defaultvalues['mymod_responsability'] = $mymod->val2;
              ...

      } else {
         // you can put default value when the recor is not yet created
        }
    }
}

in the definition() function of mod_form.php

I've fields like

        $mform->addElement('text', 'mymod_participation',    get_string('mymodvalparticipation', 'mod_mymod'), array('size' => '80','maxlength'=>'200'));

I hope that data_preprocessing() function is what you looking for.

Reading the code of other module that do what you want is also a good help.

https://github.com/moodle/moodle/blob/master/mod/lesson/mod_form.php

CU

Dominique.

Average of ratings: Useful (1)
In reply to Dominique Palumbo

Re: how to write a code to edit any activity?

by Monika Gujar -
Thank you so much for your reply
here is my code https://github.com/monikagujar/TGWhiteboard
All I need to do is display my form as pre-filled when editing.
Only 'name' field is displaying with the value and other field are blank... In which file I have to do a code to edit? I am trying in the same file, mod_form.php
In reply to Dominique Palumbo

Re: how to write a code to edit any activity?

by Monika Gujar -
Hey,
Thank you so much
It's working big grin

Need 1 more help
See the attached file "data.png", which shows the data is coming on the page but I want to bind that data to the checkboxes.
The data is coming from below code:

$tg_students= $DB->get_records('tg_students', array('tgwhiteboard_id'=>$this->current->id));

All checkboxes are coming as selected but I want only those checkboxes selected which I have selected while creating the activity.
Below is the code:

$studentdetail = tgwhiteboard_getstudentdetail();
$student = array();
foreach ($studentdetail as $key => $value) {
$student[$value->id] = $value->username;
$typeitem[] = &$mform->createElement('advcheckbox',$key, '', $student[$value->id], array('name' => $key,'group'=>1), key);
$mform->setDefault("types[$key]", true);
}
$mform->addGroup($typeitem, 'types',get_string('student_id', 'tgwhiteboard'));
$this->add_checkbox_controller(1);

See the image "checkbox.png" for reference... Sonali was not selected while creating the activity.


Attachment checkbox.png
Attachment data.png
In reply to Monika Gujar

Re: how to write a code to edit any activity?

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi Monika,

I read in your mind !

I've send you files by the git you give.
I've modified your code to display start and end date and selected or unselected student.
I've also forgot to said that you should (or must) call the field tgwhiteboard_id  -> builder. It's standard in Moodle and will help you to write code for backup, restore, duplicate and so on...

You already do a good work.
If I can give you an advice, follow from the start coding covention, it'll make you gain a lot of time.


and this is a copy of the message in the commit... 

CU.

Dominique.
(I don't see my commit in git, I hope you yes. If not I send you the zip by mail.)

-----------

Hi Monika,

I've mainly changed in the code enddt et startdt to tg_start_starttime and tg_end_starttime. To make a match from db to field. So now date is displayed. I've also created in locallib a function called tgwhiteboard_getstudentdetail2($courseid) it's use to display selected students and unselected students.

//$studentdetail = tgwhiteboard_getstudentdetail();
$studentdetail = tgwhiteboard_getstudentdetail2($COURSE->id);
$student = array();
foreach ($studentdetail as $key => $value) {
$student[$value->id] = $value->username;
$typeitem[] = &$mform->createElement('advcheckbox',$key, '', $student[$value->id], array('name' => $key,'group'=>1), $key);
if($value->tg_studentid != '') {
$mform->setDefault("types[$key]", true);
} else {
$mform->setDefault("types[$key]", false);
}
}

I also add in the query the courseid or it will take all the students of your Moodle.
So maybe you've to review your two other queries.
I've laso remark that they don't have a table call tg_status (maybe it's not install and you created locally, or still not created...)

I've change a little bit of alignment etc. To see the changes use a file comparator.
Average of ratings: Useful (1)
In reply to Dominique Palumbo

Re: how to write a code to edit any activity?

by Monika Gujar -
Hello
My mail id:
swteeka@gmail.com

and thank you so much for your co-operation :D
In reply to Dominique Palumbo

Re: how to write a code to edit any activity?

by Monika Gujar -
And about "tg_status", I had that column in my DB... forgot to remove, not it is not in use... Thank you
In reply to Dominique Palumbo

Re: how to write a code to edit any activity?

by Monika Gujar -
Hey,
I am done with my work... thank you so much for your help.

FYI, Below is the code worked for me big grin

foreach ($tg_students as $student)
{
if($student->tg_studentid != 0) {
$mform->setDefault("types[$student->tg_studentid]", true);
}
else {
$mform->setDefault("types[$student->tg_studentid]", false);
}
}

I will contact you, if I want any other help EVER again smile