Exemple #1
0
 /**
  * Returns HTML for submitlink form element.
  *
  * @return string
  */
 function toHtml()
 {
     require_once 'HTML/QuickForm/Renderer/Default.php';
     $renderer = new HTML_QuickForm_Renderer_Default();
     $renderer->setElementTemplate('{element}');
     parent::accept($renderer);
     return $renderer->toHtml();
 }
 /**
  * Returns HTML for advchecbox form element.
  *
  * @return string
  */
 function toHtml()
 {
     include_once 'HTML/QuickForm/Renderer/Default.php';
     $renderer = new HTML_QuickForm_Renderer_Default();
     $renderer->setElementTemplate('{element}');
     parent::accept($renderer);
     $html = $this->_wrap[0];
     if ($this->_usedcreateelement) {
         $html .= html_writer::tag('span', $renderer->toHtml(), array('class' => 'fdate_time_selector'));
     } else {
         $html .= $renderer->toHtml();
     }
     $html .= $this->_wrap[1];
     return $html;
 }
Exemple #3
0
 /**
  * Called by HTML_QuickForm whenever form event is made on this element.
  *
  * @param string $event Name of event
  * @param mixed $arg event arguments
  * @param moodleform $caller calling object
  * @return mixed
  */
 public function onQuickFormEvent($event, $arg, &$caller)
 {
     switch ($event) {
         case 'createElement':
             // The first argument is the name.
             $name = $arg[0];
             // Set disable actions.
             $caller->disabledIf($name . '[modgrade_scale]', $name . '[modgrade_type]', 'neq', 'scale');
             $caller->disabledIf($name . '[modgrade_point]', $name . '[modgrade_type]', 'neq', 'point');
             // Set validation rules for the sub-elements belonging to this element.
             // A handy note: the parent scope of a closure is the function in which the closure was declared.
             // Because of this using $this is safe despite the closures being called statically.
             // A nasty magic hack!
             $checkmaxgrade = function ($val) {
                 // Closure to validate a max points value. See the note above about scope if this confuses you.
                 if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'point') {
                     if (!isset($val['modgrade_point'])) {
                         return false;
                     }
                     return $this->validate_point($val['modgrade_point']);
                 }
                 return true;
             };
             $checkvalidscale = function ($val) {
                 // Closure to validate a scale value. See the note above about scope if this confuses you.
                 if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'scale') {
                     if (!isset($val['modgrade_scale'])) {
                         return false;
                     }
                     return $this->validate_scale($val['modgrade_scale']);
                 }
                 return true;
             };
             $maxgradeexceeded = get_string('modgradeerrorbadpoint', 'grades', get_config('core', 'gradepointmax'));
             $invalidscale = get_string('modgradeerrorbadscale', 'grades');
             // When creating the rules the sixth arg is $force, we set it to true because otherwise the form
             // will attempt to validate the existence of the element, we don't want this because the element
             // is being created right now and doesn't actually exist as a registered element yet.
             $caller->addRule($name, $maxgradeexceeded, 'callback', $checkmaxgrade, 'server', false, true);
             $caller->addRule($name, $invalidscale, 'callback', $checkvalidscale, 'server', false, true);
             break;
         case 'updateValue':
             // As this is a group element with no value of its own we are only interested in situations where the
             // default value or a constant value are being provided to the actual element.
             // In this case we expect an int that is going to translate to a scale if negative, or to max points
             // if positive.
             // A constant value should be given as an int.
             // The default value should be an int and should really be $CFG->gradepointdefault.
             $value = $this->_findValue($caller->_constantValues);
             if (null === $value) {
                 if ($caller->isSubmitted()) {
                     break;
                 }
                 $value = $this->_findValue($caller->_defaultValues);
             }
             if (!is_null($value) && !is_scalar($value)) {
                 // Something unexpected (likely an array of subelement values) has been given - this will be dealt
                 // with somewhere else - where exactly... likely the subelements.
                 debugging('An invalid value (type ' . gettype($value) . ') has arrived at ' . __METHOD__, DEBUG_DEVELOPER);
                 break;
             }
             // Set element state for existing data.
             // This is really a pretty hacky thing to do, when data is being set the group element is called
             // with the data first and the subelements called afterwards.
             // This means that the subelements data (inc const and default values) can be overridden by form code.
             // So - when we call this code really we can't be sure that will be the end value for the element.
             if (!empty($this->_elements)) {
                 if (!empty($value)) {
                     if ($value < 0) {
                         $this->_elements[1]->setValue('scale');
                         $this->_elements[4]->setValue($value * -1);
                     } else {
                         if ($value > 0) {
                             $this->_elements[1]->setValue('point');
                             $this->_elements[7]->setValue($value);
                         }
                     }
                 } else {
                     $this->_elements[1]->setValue('none');
                     $this->_elements[7]->setValue('');
                 }
             }
             break;
     }
     // Always let the parent do its thing!
     return parent::onQuickFormEvent($event, $arg, $caller);
 }
Exemple #4
0
 /**
  * Returns HTML for advchecbox form element.
  *
  * @return string
  */
 function toHtml()
 {
     include_once 'HTML/QuickForm/Renderer/Default.php';
     $renderer = new HTML_QuickForm_Renderer_Default();
     $renderer->setElementTemplate('{element}');
     parent::accept($renderer);
     return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
 }
Exemple #5
0
 /**
  * Called by HTML_QuickForm whenever form event is made on this element
  *
  * @param     string    $event  Name of event
  * @param     mixed     $arg    event arguments
  * @param     object    $caller calling object
  * @since     1.0
  * @access    public
  * @return    void
  */
 function onQuickFormEvent($event, $arg, &$caller)
 {
     switch ($event) {
         case 'updateValue':
             // constant values override both default and submitted ones
             // default values are overriden by submitted
             $value = $this->_findValue($caller->_constantValues);
             if (null === $value) {
                 $value = $this->_findValue($caller->_submitValues);
                 if (null === $value) {
                     $value = $this->_findValue($caller->_defaultValues);
                 }
             }
             if (!is_array($value)) {
                 $value = array('value' => $value);
             }
             if (null !== $value) {
                 $this->setValue($value);
             }
             return true;
             break;
     }
     return parent::onQuickFormEvent($event, $arg, $caller);
 }
 /**
  * Called by HTML_QuickForm whenever form event is made on this element
  *
  * @param string $event Name of event
  * @param mixed $arg event arguments
  * @param object $caller calling object
  * @return void
  */
 public function onQuickFormEvent($event, $arg, &$caller)
 {
     switch ($event) {
         case 'updateValue':
             // constant values override both default and submitted ones
             // default values are overriden by submitted
             $value = $this->_findValue($caller->_constantValues);
             if (null === $value) {
                 // if no boxes were checked, then there is no value in the array
                 // yet we don't want to display default value in this case
                 if ($caller->isSubmitted()) {
                     $value = $this->_findValue($caller->_submitValues);
                 } else {
                     $value = $this->_findValue($caller->_defaultValues);
                 }
             }
             $requestvalue = $value;
             if (!is_array($value)) {
                 $value = array('value' => $requestvalue);
             }
             if (null !== $value) {
                 $this->setValue($value);
             }
             break;
         default:
             return parent::onQuickFormEvent($event, $arg, $caller);
     }
 }
Exemple #7
0
 /**
  * Create advance group of elements
  *
  * @param MoodleQuickForm_group $group Passed by reference
  * @param bool $required if input is required field
  * @param string $error error message to display
  */
 function startGroup(&$group, $required, $error)
 {
     global $OUTPUT;
     // Make sure the element has an id.
     $group->_generateId();
     // Prepend 'fgroup_' to the ID we generated.
     $groupid = 'fgroup_' . $group->getAttribute('id');
     // Update the ID.
     $group->updateAttributes(array('id' => $groupid));
     $advanced = isset($this->_advancedElements[$group->getName()]);
     $html = $OUTPUT->mform_element($group, $required, $advanced, $error, false);
     $fromtemplate = !empty($html);
     if (!$fromtemplate) {
         if (method_exists($group, 'getElementTemplateType')) {
             $html = $this->_elementTemplates[$group->getElementTemplateType()];
         } else {
             $html = $this->_elementTemplates['default'];
         }
         if (isset($this->_advancedElements[$group->getName()])) {
             $html = str_replace(' {advanced}', ' advanced', $html);
             $html = str_replace('{advancedimg}', $this->_advancedHTML, $html);
         } else {
             $html = str_replace(' {advanced}', '', $html);
             $html = str_replace('{advancedimg}', '', $html);
         }
         if (method_exists($group, 'getHelpButton')) {
             $html = str_replace('{help}', $group->getHelpButton(), $html);
         } else {
             $html = str_replace('{help}', '', $html);
         }
         $html = str_replace('{id}', $group->getAttribute('id'), $html);
         $html = str_replace('{name}', $group->getName(), $html);
         $html = str_replace('{typeclass}', 'fgroup', $html);
         $html = str_replace('{type}', 'group', $html);
         $html = str_replace('{class}', $group->getAttribute('class'), $html);
         $emptylabel = '';
         if ($group->getLabel() == '') {
             $emptylabel = 'femptylabel';
         }
         $html = str_replace('{emptylabel}', $emptylabel, $html);
     }
     $this->_templates[$group->getName()] = $html;
     // Fix for bug in tableless quickforms that didn't allow you to stop a
     // fieldset before a group of elements.
     // if the element name indicates the end of a fieldset, close the fieldset
     if (in_array($group->getName(), $this->_stopFieldsetElements) && $this->_fieldsetsOpen > 0) {
         $this->_html .= $this->_closeFieldsetTemplate;
         $this->_fieldsetsOpen--;
     }
     if (!$fromtemplate) {
         parent::startGroup($group, $required, $error);
     } else {
         $this->_html .= $html;
     }
 }
Exemple #8
0
 /**
  * Called by HTML_QuickForm whenever form event is made on this element.
  *
  * @param string $event Name of event
  * @param mixed $arg event arguments
  * @param moodleform $caller calling object
  * @return mixed
  */
 public function onQuickFormEvent($event, $arg, &$caller)
 {
     $this->setMoodleForm($caller);
     switch ($event) {
         case 'createElement':
             // The first argument is the name.
             $name = $arg[0];
             // Set disable actions.
             $caller->disabledIf($name . '[modgrade_scale]', $name . '[modgrade_type]', 'neq', 'scale');
             $caller->disabledIf($name . '[modgrade_point]', $name . '[modgrade_type]', 'neq', 'point');
             $caller->disabledIf($name . '[modgrade_rescalegrades]', $name . '[modgrade_type]', 'neq', 'point');
             // Set validation rules for the sub-elements belonging to this element.
             // A handy note: the parent scope of a closure is the function in which the closure was declared.
             // Because of this using $this is safe despite the closures being called statically.
             // A nasty magic hack!
             $checkgradetypechange = function ($val) {
                 // Nothing is affected by changes to the grade type if there are no grades yet.
                 if (!$this->hasgrades) {
                     return true;
                 }
                 // Check if we are changing the grade type when grades are present.
                 if (isset($val['modgrade_type']) && $val['modgrade_type'] !== $this->currentgradetype) {
                     return false;
                 }
                 return true;
             };
             $checkscalechange = function ($val) {
                 // Nothing is affected by changes to the scale if there are no grades yet.
                 if (!$this->hasgrades) {
                     return true;
                 }
                 // Check if we are changing the scale type when grades are present.
                 // If modgrade_type is empty then use currentgradetype.
                 $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype;
                 if ($gradetype === 'scale') {
                     if (isset($val['modgrade_scale']) && $val['modgrade_scale'] !== $this->currentscaleid) {
                         return false;
                     }
                 }
                 return true;
             };
             $checkmaxgradechange = function ($val) {
                 // Nothing is affected by changes to the max grade if there are no grades yet.
                 if (!$this->hasgrades) {
                     return true;
                 }
                 // If we are not using ratings we can change the max grade.
                 if (!$this->useratings) {
                     return true;
                 }
                 // Check if we are changing the max grade if we are using ratings and there is a grade.
                 // If modgrade_type is empty then use currentgradetype.
                 $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype;
                 if ($gradetype === 'point') {
                     if (isset($val['modgrade_point']) && grade_floats_different($this->currentgrade, $val['modgrade_point'])) {
                         return false;
                     }
                 }
                 return true;
             };
             $checkmaxgrade = function ($val) {
                 // Closure to validate a max points value. See the note above about scope if this confuses you.
                 // If modgrade_type is empty then use currentgradetype.
                 $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype;
                 if ($gradetype === 'point') {
                     if (isset($val['modgrade_point'])) {
                         return $this->validate_point($val['modgrade_point']);
                     }
                 }
                 return true;
             };
             $checkvalidscale = function ($val) {
                 // Closure to validate a scale value. See the note above about scope if this confuses you.
                 // If modgrade_type is empty then use currentgradetype.
                 $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype;
                 if ($gradetype === 'scale') {
                     if (isset($val['modgrade_scale'])) {
                         return $this->validate_scale($val['modgrade_scale']);
                     }
                 }
                 return true;
             };
             $checkrescale = function ($val) {
                 // Nothing is affected by changes to grademax if there are no grades yet.
                 if (!$this->isupdate || !$this->hasgrades || !$this->canrescale) {
                     return true;
                 }
                 // Closure to validate a scale value. See the note above about scope if this confuses you.
                 // If modgrade_type is empty then use currentgradetype.
                 $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype;
                 if ($gradetype === 'point' && isset($val['modgrade_point'])) {
                     // Work out if the value was actually changed in the form.
                     if (grade_floats_different($this->currentgrade, $val['modgrade_point'])) {
                         if (empty($val['modgrade_rescalegrades'])) {
                             // This was an "edit", the grademax was changed and the process existing setting was not set.
                             return false;
                         }
                     }
                 }
                 return true;
             };
             $cantchangegradetype = get_string('modgradecantchangegradetype', 'grades');
             $cantchangemaxgrade = get_string('modgradecantchangeratingmaxgrade', 'grades');
             $maxgradeexceeded = get_string('modgradeerrorbadpoint', 'grades', get_config('core', 'gradepointmax'));
             $invalidscale = get_string('modgradeerrorbadscale', 'grades');
             $cantchangescale = get_string('modgradecantchangescale', 'grades');
             $mustchooserescale = get_string('mustchooserescaleyesorno', 'grades');
             // When creating the rules the sixth arg is $force, we set it to true because otherwise the form
             // will attempt to validate the existence of the element, we don't want this because the element
             // is being created right now and doesn't actually exist as a registered element yet.
             $caller->addRule($name, $cantchangegradetype, 'callback', $checkgradetypechange, 'server', false, true);
             $caller->addRule($name, $cantchangemaxgrade, 'callback', $checkmaxgradechange, 'server', false, true);
             $caller->addRule($name, $maxgradeexceeded, 'callback', $checkmaxgrade, 'server', false, true);
             $caller->addRule($name, $invalidscale, 'callback', $checkvalidscale, 'server', false, true);
             $caller->addRule($name, $cantchangescale, 'callback', $checkscalechange, 'server', false, true);
             $caller->addRule($name, $mustchooserescale, 'callback', $checkrescale, 'server', false, true);
             break;
         case 'updateValue':
             // As this is a group element with no value of its own we are only interested in situations where the
             // default value or a constant value are being provided to the actual element.
             // In this case we expect an int that is going to translate to a scale if negative, or to max points
             // if positive.
             // Set the maximum points field to disabled if the rescale option has not been chosen and there are grades.
             $caller->disabledIf($this->getName() . '[modgrade_point]', $this->getName() . '[modgrade_rescalegrades]', 'eq', '');
             // A constant value should be given as an int.
             // The default value should be an int and be either $CFG->gradepointdefault or whatever was set in set_data().
             $value = $this->_findValue($caller->_constantValues);
             if (null === $value) {
                 if ($caller->isSubmitted() && $this->_findValue($caller->_submitValues) !== null) {
                     // Submitted values are array, one value for each individual element in this group.
                     // When there is submitted data let parent::onQuickFormEvent() process it.
                     break;
                 }
                 $value = $this->_findValue($caller->_defaultValues);
             }
             if (!is_null($value) && !is_scalar($value)) {
                 // Something unexpected (likely an array of subelement values) has been given - this will be dealt
                 // with somewhere else - where exactly... likely the subelements.
                 debugging('An invalid value (type ' . gettype($value) . ') has arrived at ' . __METHOD__, DEBUG_DEVELOPER);
                 break;
             }
             // Set element state for existing data.
             // This is really a pretty hacky thing to do, when data is being set the group element is called
             // with the data first and the subelements called afterwards.
             // This means that the subelements data (inc const and default values) can be overridden by form code.
             // So - when we call this code really we can't be sure that will be the end value for the element.
             if (!empty($this->_elements)) {
                 if (!empty($value)) {
                     if ($value < 0) {
                         $this->gradetypeformelement->setValue('scale');
                         $this->scaleformelement->setValue($value * -1);
                     } else {
                         if ($value > 0) {
                             $this->gradetypeformelement->setValue('point');
                             $maxvalue = !empty($this->currentgrade) ? (string) unformat_float($this->currentgrade) : $value;
                             $this->maxgradeformelement->setValue($maxvalue);
                         }
                     }
                 } else {
                     $this->gradetypeformelement->setValue('none');
                     $this->maxgradeformelement->setValue('');
                 }
             }
             break;
     }
     // Always let the parent do its thing!
     return parent::onQuickFormEvent($event, $arg, $caller);
 }
Exemple #9
0
 /**
  * Called by HTML_QuickForm whenever form event is made on this element.
  *
  * @param string $event Name of event
  * @param mixed $arg event arguments
  * @param object $caller calling object
  * @return mixed
  */
 public function onQuickFormEvent($event, $arg, &$caller)
 {
     global $COURSE;
     switch ($event) {
         case 'updateValue':
             $value = $this->_findValue($caller->_constantValues);
             if (null === $value) {
                 if ($caller->isSubmitted()) {
                     $value = $this->_findValue($caller->_submitValues);
                 } else {
                     $value = $this->_findValue($caller->_defaultValues);
                 }
             }
             $name = $this->getName();
             // Set disable actions.
             $caller->disabledIf($name . '[modgrade_scale]', $name . '[modgrade_type]', 'neq', 'scale');
             $caller->disabledIf($name . '[modgrade_point]', $name . '[modgrade_type]', 'neq', 'point');
             // Set element state for existing data.
             if (!empty($this->_elements)) {
                 if (!empty($value)) {
                     if ($value < 0) {
                         $this->_elements[1]->setValue('scale');
                         $this->_elements[4]->setValue($value * -1);
                     } else {
                         if ($value > 0) {
                             $this->_elements[1]->setValue('point');
                             $this->_elements[7]->setValue($value);
                         }
                     }
                 } else {
                     $this->_elements[1]->setValue('none');
                     $this->_elements[7]->setValue('');
                 }
             }
             // Value Validation.
             if ($name && $caller->elementExists($name)) {
                 $checkmaxgrade = function ($val) {
                     if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'point') {
                         if (!isset($val['modgrade_point'])) {
                             return false;
                         }
                         return $this->validate_point($val['modgrade_point']);
                     }
                     return true;
                 };
                 $checkvalidscale = function ($val) {
                     if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'scale') {
                         if (!isset($val['modgrade_scale'])) {
                             return false;
                         }
                         return $this->validate_scale($val['modgrade_scale']);
                     }
                     return true;
                 };
                 $maxgradeexceeded = get_string('modgradeerrorbadpoint', 'grades', get_config('core', 'gradepointmax'));
                 $invalidscale = get_string('modgradeerrorbadscale', 'grades');
                 $caller->addRule($name, $maxgradeexceeded, 'callback', $checkmaxgrade);
                 $caller->addRule($name, $invalidscale, 'callback', $checkvalidscale);
             }
             break;
     }
     return parent::onQuickFormEvent($event, $arg, $caller);
 }