Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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_checkbox extends data_field_base {
  26  
  27      var $type = 'checkbox';
  28      /**
  29       * priority for globalsearch indexing
  30       *
  31       * @var int
  32       */
  33      protected static $priority = self::LOW_PRIORITY;
  34  
  35      function display_add_field($recordid = 0, $formdata = null) {
  36          global $CFG, $DB, $OUTPUT;
  37  
  38          $content = array();
  39  
  40          if ($formdata) {
  41              $fieldname = 'field_' . $this->field->id;
  42              $content = $formdata->$fieldname;
  43          } else if ($recordid) {
  44              $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
  45              $content = explode('##', $content);
  46          } else {
  47              $content = array();
  48          }
  49  
  50          $str = '<div title="' . s($this->field->description) . '">';
  51          $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name;
  52          if ($this->field->required) {
  53              $str .= '$nbsp;' . get_string('requiredelement', 'form');
  54              $str .= '</span></legend>';
  55              $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
  56              $str .= html_writer::div($image, 'inline-req');
  57          } else {
  58              $str .= '</span></legend>';
  59          }
  60  
  61          $i = 0;
  62          foreach (explode("\n", $this->field->param1) as $checkbox) {
  63              $checkbox = trim($checkbox);
  64              if ($checkbox === '') {
  65                  continue; // skip empty lines
  66              }
  67              $str .= '<input type="hidden" name="field_' . $this->field->id . '[]" value="" />';
  68              $str .= '<input type="checkbox" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '[]" ';
  69              $str .= 'value="' . s($checkbox) . '" class="mod-data-input mr-1" ';
  70  
  71              if (array_search($checkbox, $content) !== false) {
  72                  $str .= 'checked />';
  73              } else {
  74                  $str .= '/>';
  75              }
  76              $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$checkbox.'</label><br />';
  77              $i++;
  78          }
  79          $str .= '</fieldset>';
  80          $str .= '</div>';
  81          return $str;
  82      }
  83  
  84      function display_search_field($value='') {
  85          global $CFG, $DB;
  86  
  87          if (is_array($value)) {
  88              $content = $value['checked'];
  89              $allrequired = $value['allrequired'] ? true : false;
  90          } else {
  91              $content = array();
  92              $allrequired = false;
  93          }
  94  
  95          $str = '';
  96          $found = false;
  97          $marginclass = ['class' => 'mr-1'];
  98          foreach (explode("\n",$this->field->param1) as $checkbox) {
  99              $checkbox = trim($checkbox);
 100              if (in_array($checkbox, $content)) {
 101                  $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), true, $checkbox, $marginclass);
 102              } else {
 103                  $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), false, $checkbox, $marginclass);
 104              }
 105              $str .= html_writer::empty_tag('br');
 106              $found = true;
 107          }
 108          if (!$found) {
 109              return '';
 110          }
 111  
 112          $requiredstr = get_string('selectedrequired', 'data');
 113          $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, $requiredstr, $marginclass);
 114          return $str;
 115      }
 116  
 117      public function parse_search_field($defaults = null) {
 118          $paramselected = 'f_'.$this->field->id;
 119          $paramallrequired = 'f_'.$this->field->id.'_allreq';
 120  
 121          if (empty($defaults[$paramselected])) { // One empty means the other ones are empty too.
 122              $defaults = array($paramselected => array(), $paramallrequired => 0);
 123          }
 124  
 125          $selected    = optional_param_array($paramselected, $defaults[$paramselected], PARAM_NOTAGS);
 126          $allrequired = optional_param($paramallrequired, $defaults[$paramallrequired], PARAM_BOOL);
 127  
 128          if (empty($selected)) {
 129              // no searching
 130              return '';
 131          }
 132          return array('checked'=>$selected, 'allrequired'=>$allrequired);
 133      }
 134  
 135      function generate_sql($tablealias, $value) {
 136          global $DB;
 137  
 138          static $i=0;
 139          $i++;
 140          $name = "df_checkbox_{$i}_";
 141          $params = array();
 142          $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
 143  
 144          $allrequired = $value['allrequired'];
 145          $selected    = $value['checked'];
 146  
 147          if ($selected) {
 148              $conditions = array();
 149              $j=0;
 150              foreach ($selected as $sel) {
 151                  $j++;
 152                  $xname = $name.$j;
 153                  $likesel = str_replace('%', '\%', $sel);
 154                  $likeselsel = str_replace('_', '\_', $likesel);
 155                  $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a
 156                                                                                 OR {$tablealias}.content LIKE :{$xname}b
 157                                                                                 OR {$tablealias}.content LIKE :{$xname}c
 158                                                                                 OR {$tablealias}.content LIKE :{$xname}d))";
 159                  $params[$xname.'a'] = $sel;
 160                  $params[$xname.'b'] = "$likesel##%";
 161                  $params[$xname.'c'] = "%##$likesel";
 162                  $params[$xname.'d'] = "%##$likesel##%";
 163              }
 164              if ($allrequired) {
 165                  return array(" (".implode(" AND ", $conditions).") ", $params);
 166              } else {
 167                  return array(" (".implode(" OR ", $conditions).") ", $params);
 168              }
 169          } else {
 170              return array(" ", array());
 171          }
 172      }
 173  
 174      function update_content($recordid, $value, $name='') {
 175          global $DB;
 176  
 177          $content = new stdClass();
 178          $content->fieldid = $this->field->id;
 179          $content->recordid = $recordid;
 180          $content->content = $this->format_data_field_checkbox_content($value);
 181  
 182          if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 183              $content->id = $oldcontent->id;
 184              return $DB->update_record('data_content', $content);
 185          } else {
 186              return $DB->insert_record('data_content', $content);
 187          }
 188      }
 189  
 190      function display_browse_field($recordid, $template) {
 191          global $DB;
 192  
 193          if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 194              if (strval($content->content) === '') {
 195                  return false;
 196              }
 197  
 198              $options = explode("\n",$this->field->param1);
 199              $options = array_map('trim', $options);
 200  
 201              $contentArr = explode('##', $content->content);
 202              $str = '';
 203              foreach ($contentArr as $line) {
 204                  if (!in_array($line, $options)) {
 205                      // hmm, looks like somebody edited the field definition
 206                      continue;
 207                  }
 208                  $str .= $line . "<br />\n";
 209              }
 210              return $str;
 211          }
 212          return false;
 213      }
 214  
 215      function format_data_field_checkbox_content($content) {
 216          if (!is_array($content)) {
 217              return NULL;
 218          }
 219          $options = explode("\n", $this->field->param1);
 220          $options = array_map('trim', $options);
 221  
 222          $vals = array();
 223          foreach ($content as $key=>$val) {
 224              if ($key === 'xxx') {
 225                  continue;
 226              }
 227              if (!in_array($val, $options)) {
 228                  continue;
 229  
 230              }
 231              $vals[] = $val;
 232          }
 233  
 234          if (empty($vals)) {
 235              return NULL;
 236          }
 237  
 238          return implode('##', $vals);
 239      }
 240  
 241      /**
 242       * Check whether any boxes in the checkbox where checked.
 243       *
 244       * @param mixed $value The submitted values
 245       * @param mixed $name
 246       * @return bool
 247       */
 248      function notemptyfield($value, $name) {
 249          $found = false;
 250          foreach ($value as $checkboxitem) {
 251              if (strval($checkboxitem) !== '') {
 252                  $found = true;
 253                  break;
 254              }
 255          }
 256          return $found;
 257      }
 258  
 259      /**
 260       * Returns the presentable string value for a field content.
 261       *
 262       * The returned string should be plain text.
 263       *
 264       * @param stdClass $content
 265       * @return string
 266       */
 267      public static function get_content_value($content) {
 268          $arr = explode('##', $content->content);
 269  
 270          $strvalue = '';
 271          foreach ($arr as $a) {
 272              $strvalue .= $a . ' ';
 273          }
 274  
 275          return trim($strvalue, "\r\n ");
 276      }
 277  
 278      /**
 279       * Return the plugin configs for external functions.
 280       *
 281       * @return array the list of config parameters
 282       * @since Moodle 3.3
 283       */
 284      public function get_config_for_external() {
 285          // Return all the config parameters.
 286          $configs = [];
 287          for ($i = 1; $i <= 10; $i++) {
 288              $configs["param$i"] = $this->field->{"param$i"};
 289          }
 290          return $configs;
 291      }
 292  }