/**
 * Checks existence and consistency of a full template.
 * @uses $CFG
 * @param string $templatename The template's name.
 * @return bool Returns TRUE if the full template is consistency, FALSE otherwise.
 */
function vmoodle_exist_template($templatename)
{
    global $CFG;
    // Needed paths for checking.
    $templatedir_files = $CFG->dataroot . '/vmoodle/' . $templatename . '_vmoodledata';
    $templatedir_sql = $CFG->dataroot . '/vmoodle/' . $templatename . '_sql';
    return in_array($templatename, vmoodle_get_available_templates()) && is_readable($templatedir_files) && is_readable($templatedir_sql);
}
     $record = $DB->get_record_sql($sqlrequest);
     if (empty($record)) {
         $record = (object) array('name' => $submitteddata->name, 'shortname' => $submitteddata->shortname, 'description' => $submitteddata->description, 'vhostname' => $submitteddata->vhostname, 'vdbtype' => $submitteddata->vdbtype, 'vdbhost' => $submitteddata->vdbhost, 'vdblogin' => $submitteddata->vdblogin, 'vdbpass' => $submitteddata->vdbpass, 'vdbname' => $submitteddata->vdbname, 'vdbpersist' => $submitteddata->vdbpersist, 'vdbprefix' => $submitteddata->vdbprefix, 'vdbpersist' => $submitteddata->vdbpersist, 'vdatapath' => $submitteddata->vdatapath, 'mnet' => $submitteddata->mnet);
         $DB->insert_record('local_vmoodle', $record);
     }
     if (empty($automation)) {
         $message_object->message = get_string('plateformreactivate', 'local_vmoodle');
         $message_object->style = 'notifysuccess';
         $SESSION->vmoodle_ma['confirm_message'] = $message_object;
         redirect(new moodle_url('/local/vmoodle/view.php', array('view' => 'management')));
         die;
     }
     return 0;
 } else {
     // Checks if the chosen template still exists.
     $templates = vmoodle_get_available_templates();
     if (empty($templates) || !vmoodle_exist_template($submitteddata->vtemplate)) {
         // If the snapshot has been deleted between loading the add form and submitting it.
         $message_object->message = get_string('notemplates', 'local_vmoodle');
         if (empty($automation)) {
             $SESSION->vmoodle_ma['confirm_message'] = $message_object;
             redirect(new moodle_url('/local/vmoodle/view.php', array('view' => 'management')));
             die;
         }
         mtrace(get_string('notemplates', 'local_vmoodle'));
         return -1;
     }
     // Check if the required hostname has DNS resolution
     $domainname = preg_replace('/https?:\\/\\//', '', $submitteddata->vhostname);
     if (!gethostbynamel($domainname)) {
         if ($submitteddata->forcedns) {
 /**
  * Describes the form (each elements' name  corresponds to its name in database).
  */
 public function definition()
 {
     // Global configuration.
     global $CFG, $DB;
     // Settings variables.
     $mform =& $this->_form;
     $size_input_text = 'size="30"';
     $size_input_text_big = 'size="60"';
     /*
      * Host's id.
      */
     $mform->addElement('hidden', 'id');
     $mform->setType('id', PARAM_INT);
     /*
      * Features fieldset.
      */
     $mform->addElement('header', 'featuresform', get_string('addformfeaturesgroup', 'local_vmoodle'));
     // Name.
     $mform->addElement('text', 'name', get_string('addformname', 'local_vmoodle'), $size_input_text);
     $mform->addHelpButton('name', 'name', 'local_vmoodle');
     $mform->setType('name', PARAM_TEXT);
     if ($this->isInAddMode()) {
         // Shortname.
         $elmname = get_string('addformshortname', 'local_vmoodle');
         $mform->addElement('text', 'shortname', $elmname, $this->mode == 'edit' ? 'disabled="disabled" ' : '');
         $mform->addHelpButton('shortname', 'shortname', 'local_vmoodle');
         $mform->setType('shortname', PARAM_TEXT);
     }
     // Description.
     $elmname = get_string('addformdescription', 'local_vmoodle');
     $mform->addElement('textarea', 'description', $elmname, 'rows="15" cols="40"');
     $mform->addHelpButton('description', 'description', 'local_vmoodle');
     $mform->setType('description', PARAM_TEXT);
     if ($this->isInAddMode()) {
         // Host's name.
         $elmname = get_string('vhostname', 'local_vmoodle');
         $mform->addElement('text', 'vhostname', $elmname, ($this->mode == 'edit' ? 'disabled="disabled" ' : '') . $size_input_text);
         $mform->addHelpButton('vhostname', 'vhostname', 'local_vmoodle');
         $mform->addElement('checkbox', 'forcedns', get_string('forcedns', 'local_vmoodle'));
         $mform->setType('vhostname', PARAM_URL);
     }
     $mform->closeHeaderBefore('dbform');
     /*
      * Database fieldset.
      */
     $mform->addElement('header', 'dbform', get_string('addformdbgroup', 'local_vmoodle'));
     // Database type.
     $dbtypearray = array('mysqli' => 'MySQL', 'postgres' => 'PostgreSQL');
     $mform->addElement('select', 'vdbtype', get_string('vdbtype', 'local_vmoodle'), $dbtypearray);
     $mform->addHelpButton('vdbtype', 'vdbtype', 'local_vmoodle');
     $mform->setType('vdbtype', PARAM_TEXT);
     // Database host.
     $mform->addElement('text', 'vdbhost', get_string('vdbhost', 'local_vmoodle'));
     //$mform->addHelpButton('vdbhost', 'vdbhost', 'local_vmoodle');
     $mform->setType('vdbhost', PARAM_TEXT);
     // Database login.
     $mform->addElement('text', 'vdblogin', get_string('vdblogin', 'local_vmoodle'));
     $mform->setType('vdblogin', PARAM_TEXT);
     // Database password.
     $mform->addElement('password', 'vdbpass', get_string('vdbpass', 'local_vmoodle'));
     $mform->setType('vdbpass', PARAM_RAW);
     // Button for testing database connection.
     $mform->addElement('button', 'testconnection', get_string('testconnection', 'local_vmoodle'), 'onclick="opencnxpopup(\'' . $CFG->wwwroot . '\'); return true;"');
     // Database name.
     $mform->addElement('text', 'vdbname', get_string('vdbname', 'local_vmoodle'));
     $mform->addHelpButton('vdbname', 'vdbname', 'local_vmoodle');
     $mform->setType('vdbname', PARAM_TEXT);
     // Table's prefix.
     $mform->addElement('text', 'vdbprefix', get_string('vdbprefix', 'local_vmoodle'));
     $mform->setType('vdbprefix', PARAM_TEXT);
     // Connection persistance.
     $noyesarray = array('0' => get_string('no'), '1' => get_string('yes'));
     $mform->addElement('select', 'vdbpersist', get_string('vdbpersist', 'local_vmoodle'), $noyesarray);
     $mform->addHelpButton('vdbpersist', 'vdbpersist', 'local_vmoodle');
     $mform->setType('vdbpersist', PARAM_BOOL);
     $mform->closeHeaderBefore('nfform');
     /*
      * Network and data fieldset.
      */
     $mform->addElement('header', 'nfform', get_string('addformnfgroup', 'local_vmoodle'));
     // Path for "moodledata".
     $mform->addElement('text', 'vdatapath', get_string('vdatapath', 'local_vmoodle'), $size_input_text_big);
     $mform->addHelpButton('vdatapath', 'vdatapath', 'local_vmoodle');
     $mform->setType('vdatapath', PARAM_TEXT);
     // Button for testing datapath.
     $elmname = get_string('testdatapath', 'local_vmoodle');
     $mform->addElement('button', 'testdatapath', $elmname, 'onclick="opendatapathpopup(\'' . $CFG->wwwroot . '\'); return true;"');
     // MultiMNET.
     $subnetworks = array('-1' => get_string('nomnet', 'local_vmoodle'));
     $subnetworks['0'] = get_string('mnetfree', 'local_vmoodle');
     $sql = "\n            SELECT\n                *\n            FROM\n                {local_vmoodle}\n            WHERE\n                mnet > 0\n            ORDER BY\n                mnet\n        ";
     $subnetworksrecords = $DB->get_records_sql($sql);
     $newsubnetwork = 1;
     if (!empty($subnetworksrecords)) {
         $maxmnet = 0;
         foreach ($subnetworksrecords as $subnetworksrecord) {
             $subnetworks[$subnetworksrecord->mnet] = $subnetworksrecord->mnet;
             $maxmnet = max($maxmnet, $subnetworksrecord->mnet);
         }
         $newsubnetwork = $maxmnet + 1;
     }
     $subnetworks[$newsubnetwork] = $newsubnetwork . ' (' . get_string('mnetnew', 'local_vmoodle') . ')';
     $mform->addElement('select', 'mnet', get_string('multimnet', 'local_vmoodle'), $subnetworks, 'onchange="switcherServices(\'' . $newsubnetwork . '\'); return true;"');
     $mform->addHelpButton('mnet', 'mnet', 'local_vmoodle');
     $mform->setType('mnet', PARAM_TEXT);
     // Services strategy.
     $services_strategies = array('default' => get_string('servicesstrategydefault', 'local_vmoodle'), 'subnetwork' => get_string('servicesstrategysubnetwork', 'local_vmoodle'));
     $mform->addElement('select', 'services', get_string('servicesstrategy', 'local_vmoodle'), $services_strategies);
     $mform->addHelpButton('services', 'services', 'local_vmoodle');
     $mform->setType('services', PARAM_TEXT);
     if ($this->isInAddMode()) {
         // Template.
         $templatesarray = vmoodle_get_available_templates();
         $mform->addElement('select', 'vtemplate', get_string('vtemplate', 'local_vmoodle'), $templatesarray);
         $mform->addHelpButton('vtemplate', 'vtemplate', 'local_vmoodle');
         $mform->setType('vtemplate', PARAM_TEXT);
     }
     $mform->closeHeaderBefore('submitbutton');
     // Control buttons.
     $buttonarray = array();
     $buttonarray[] =& $mform->createElement('submit', 'submitbutton', get_string($this->mode == 'edit' ? 'edit' : 'create'));
     $buttonarray[] =& $mform->createElement('cancel');
     $mform->addGroup($buttonarray, 'controlbuttons', '', array(' '), false);
     // Rules for the add mode.
     if ($this->isInAddMode()) {
         $mform->addRule('name', get_string('addforminputtexterror', 'local_vmoodle'), 'required', null, 'client');
         $mform->addRule('shortname', get_string('addforminputtexterror', 'local_vmoodle'), 'required', null, 'client');
         $mform->addRule('vhostname', get_string('addforminputtexterror', 'local_vmoodle'), 'required', null, 'client');
         $mform->addRule('vdbhost', get_string('addforminputtexterror', 'local_vmoodle'), 'required', null, 'client');
         $mform->addRule('vdblogin', get_string('addforminputtexterror', 'local_vmoodle'), 'required', null, 'client');
         //$mform->addRule('vdbpass', get_string('addforminputtexterror', 'local_vmoodle'), 'required', null, 'client');
         $mform->addRule('vdbname', get_string('addforminputtexterror', 'local_vmoodle'), 'required', null, 'client');
         $mform->addRule('vdbprefix', get_string('addforminputtexterror', 'local_vmoodle'), 'required', null, 'client');
         $mform->addRule('vdatapath', get_string('addforminputtexterror', 'local_vmoodle'), 'required', null, 'client');
     }
 }