[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/data/field/radiobutton/ -> field.class.php (source)

   1  <?php
   2  ///////////////////////////////////////////////////////////////////////////
   3  //                                                                       //
   4  // NOTICE OF COPYRIGHT                                                   //
   5  //                                                                       //
   6  // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
   7  //          http://moodle.org                                            //
   8  //                                                                       //
   9  // Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
  10  //                                                                       //
  11  // This program is free software; you can redistribute it and/or modify  //
  12  // it under the terms of the GNU General Public License as published by  //
  13  // the Free Software Foundation; either version 2 of the License, or     //
  14  // (at your option) any later version.                                   //
  15  //                                                                       //
  16  // This program is distributed in the hope that it will be useful,       //
  17  // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
  18  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
  19  // GNU General Public License for more details:                          //
  20  //                                                                       //
  21  //          http://www.gnu.org/copyleft/gpl.html                         //
  22  //                                                                       //
  23  ///////////////////////////////////////////////////////////////////////////
  24  
  25  class data_field_radiobutton extends data_field_base {
  26  
  27      var $type = 'radiobutton';
  28  
  29      function display_add_field($recordid = 0, $formdata = null) {
  30          global $CFG, $DB, $OUTPUT;
  31  
  32          if ($formdata) {
  33              $fieldname = 'field_' . $this->field->id;
  34              if (isset($formdata->$fieldname)) {
  35                  $content = $formdata->$fieldname;
  36              } else {
  37                  $content = '';
  38              }
  39          } else if ($recordid) {
  40              $content = trim($DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid)));
  41          } else {
  42              $content = '';
  43          }
  44  
  45          $str = '<div title="' . s($this->field->description) . '">';
  46          $str .= '<fieldset><legend><span class="accesshide">' . $this->field->name;
  47  
  48          if ($this->field->required) {
  49              $str .= '&nbsp;' . get_string('requiredelement', 'form') . '</span></legend>';
  50              $image = html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'),
  51                                        array('class' => 'req', 'title' => get_string('requiredelement', 'form')));
  52              $str .= html_writer::div($image, 'inline-req');
  53          } else {
  54              $str .= '</span></legend>';
  55          }
  56  
  57          $i = 0;
  58          $requiredstr = '';
  59          $options = explode("\n", $this->field->param1);
  60          foreach ($options as $radio) {
  61              $radio = trim($radio);
  62              if ($radio === '') {
  63                  continue; // skip empty lines
  64              }
  65              $str .= '<input type="radio" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '" ';
  66              $str .= 'value="' . s($radio) . '" class="mod-data-input" ';
  67  
  68              if ($content == $radio) {
  69                  // Selected by user.
  70                  $str .= 'checked />';
  71              } else {
  72                  $str .= '/>';
  73              }
  74  
  75              $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$radio.'</label><br />';
  76              $i++;
  77          }
  78          $str .= '</fieldset>';
  79          $str .= '</div>';
  80          return $str;
  81      }
  82  
  83       function display_search_field($value = '') {
  84          global $CFG, $DB;
  85  
  86          $varcharcontent = $DB->sql_compare_text('content', 255);
  87          $used = $DB->get_records_sql(
  88              "SELECT DISTINCT $varcharcontent AS content
  89                 FROM {data_content}
  90                WHERE fieldid=?
  91               ORDER BY $varcharcontent", array($this->field->id));
  92  
  93          $options = array();
  94          if(!empty($used)) {
  95              foreach ($used as $rec) {
  96                  $options[$rec->content] = $rec->content;  //Build following indicies from the sql.
  97              }
  98          }
  99          $return = html_writer::label(get_string('nameradiobutton', 'data'), 'menuf_'. $this->field->id, false, array('class' => 'accesshide'));
 100          $return .= html_writer::select($options, 'f_'.$this->field->id, $value);
 101          return $return;
 102      }
 103  
 104      function parse_search_field() {
 105          return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
 106      }
 107  
 108      function generate_sql($tablealias, $value) {
 109          global $DB;
 110  
 111          static $i=0;
 112          $i++;
 113          $name = "df_radiobutton_$i";
 114          $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
 115  
 116          return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value));
 117      }
 118  
 119      /**
 120       * Check if a field from an add form is empty
 121       *
 122       * @param mixed $value
 123       * @param mixed $name
 124       * @return bool
 125       */
 126      function notemptyfield($value, $name) {
 127          return strval($value) !== '';
 128      }
 129  }
 130  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1