Personalize subnet field as a select in quiz form settings

Personalize subnet field as a select in quiz form settings

by A. gtdino -
Number of replies: 3
Picture of Plugin developers

Hello,

I'm trying to implement some funcionality in the quiz form. For do that I've change the textbox 'subnet' for a select where have an array of all subnet of my organitation.

One of the array options have 'Others ranges of ip' that means that the user have to type the ip manually in another textbox named 'other_ip'.

The problems is that cannot acces to data field submited to change the value of subnet for the other_ip when this option has selected. And when the forms load I need access to the data value to comprobe if the select has this option available and has to put into the textbox. The value of the 'other_ip' textbox is not writen into database, because I've to use the select for do that.

Any help would be "so much" apreciated.

Thank's and regards

Average of ratings: -
In reply to A. gtdino

Re: Personalize subnet field as a select in quiz form settings

by A. gtdino -
Picture of Plugin developers
Some captures to explain more this problem:

and Some code :

unset($option_subnet);
$option_subnet = array();
$options_subnet[''] = "All sites are allowed";
$options_subnet['172.17.1.0/24'] = "Room 22";
$options_subnet['172.17.2.0/24'] = "Floor 33";
$options_subnet['1'] = "Other IP Range";

$mform->addElement('select', 'subnet', get_string("requiresubnet", "quiz"), $options_subnet);
$mform->setType('subnet', PARAM_TEXT);
$mform->setHelpButton('subnet', array("requiresubnet", get_string("requiresubnet", "quiz"), "quiz"));

$mform->addElement('text', 'other_ip', get_string('other_ip', "quiz"));
$mform->setHelpButton('other_ip', array('other_ip', get_string('other_ip', "quiz"), "quiz"));
$mform->setType('other_ip', PARAM_TEXT);
$mform->disabledIf('other_ip', 'subnet', 'neq', 1);

¿How can initialize 'other_ip' textbox?
¿Can access field data there?

Attachment somecaptures.JPG
In reply to A. gtdino

Re: Personalize subnet field as a select in quiz form settings

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
You need to add a set_data method to the form class. Or, possibly, you coudl use the data_preprocessing function. Yes. data_preprocessing is the way to go here, I think.
In reply to Tim Hunt

Re: Personalize subnet field as a select in quiz form settings

by A. gtdino -
Picture of Plugin developers

Hi I do it!!, Thanks

I add this code to mod_form.php

============================

$quizsubnet = get_field('quiz', 'subnet', 'id', $this->_instance);

if (!isset($options_subnet[$quizsubnet])) {
   $mform->setDefault('other_ip', $quizsubnet);
   $defaultValues['subnet'] = array('1');
  } else {
          $defaultValues['subnet'] = array($quizsubnet);}

$mform->setDefaults($defaultValues);

=============================

and this other in the lib.php (mod\quiz dir)

=============================

function quiz_process_options(&$quiz) {
    $quiz->timemodified = time();
    if ($quiz->subnet == 1)  { $quiz->subnet=$quiz->other_ip; }

=============================

this lines do the job very well,

Thank's and regards.