With the database configuration out of the way, it’s time to update the PHP code. Firstly, a simple search and replace for the word “NEWMODULE” is all that’s needed to get the module into a working state. At this point it will be possible to view the module from the Moodle administration page and add it to existing courses. Although our module conforms to the requirements Moodle sets for an activity, it as yet has no functional capabilities, so let’s add some now. To begin with, we’ll update the lib.php file with some basic functions. This file is used for all Moodle modules as a repository for the module’s business logic. For instance, the default lib.php file comes with definitions for insert, update and delete functions. We need to update all of these to cater for our images table, plus we will need an additional function that retrieves the images for any given instance.
The insert function needs to return the ID of the new module, but this ID is also required as a foreign key to create the image records. So, we attempt to create the module record then, if successful, we create the image records. If all goes well, the original ID is returned from the function. Note that for this example, we will use three images – although with some extra HTML programming, this number could be selected by the user.
function draganddrop_add_instance($draganddrop) {
$draganddrop->timemodified = time();
$insert_id = insert_record(“draganddrop”, $draganddrop);
if (!$insert_id){
return false;
}
$image1->ddid = $insert_id;
$image1->url = $draganddrop->image1;
$image2->ddid = $insert_id;
$image2->url = $draganddrop->image2;
$image3->ddid = $insert_id;
$image3->url = $draganddrop->image3;
if (!insert_record(“draganddrop_images”, $image1)){
return false;
}
if (!insert_record(“draganddrop_images”, $image2)){
return false;
}
if (!insert_record(“draganddrop_images”, $image3)){
return false;
}
return $insert_id;
}
