Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403]

   1  <?php
   2  /**
   3   * RecordSet Filter.
   4   *
   5   * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
   6   *
   7   * @package ADOdb
   8   * @link https://adodb.org Project's web site and documentation
   9   * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
  10   *
  11   * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
  12   * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
  13   * any later version. This means you can use it in proprietary products.
  14   * See the LICENSE.md file distributed with this source code for details.
  15   * @license BSD-3-Clause
  16   * @license LGPL-2.1-or-later
  17   *
  18   * @copyright 2000-2013 John Lim
  19   * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
  20   */
  21  
  22  /*
  23  	 Filter all fields and all rows in a recordset and returns the
  24  	 processed recordset. We scroll to the beginning of the new recordset
  25  	 after processing.
  26  
  27  	 We pass a recordset and function name to RSFilter($rs,'rowfunc');
  28  	 and the function will be called multiple times, once
  29  	 for each row in the recordset. The function will be passed
  30  	 an array containing one row repeatedly.
  31  
  32  	 Example:
  33  
  34  	 // ucwords() every element in the recordset
  35  	 function do_ucwords(&$arr,$rs)
  36  	 {
  37  	 	 foreach($arr as $k => $v) {
  38  	 	 	 $arr[$k] = ucwords($v);
  39  	 	 }
  40  	 }
  41  	 $rs = RSFilter($rs,'do_ucwords');
  42   */
  43  function RSFilter($rs,$fn)
  44  {
  45  	 if ($rs->databaseType != 'array') {
  46  	 	 if (!$rs->connection) return false;
  47  
  48  	 	 $rs = $rs->connection->_rs2rs($rs);
  49  	 }
  50  	 $rows = $rs->RecordCount();
  51  	 for ($i=0; $i < $rows; $i++) {
  52  	 	 if (is_array ($fn)) {
  53          	 $obj = $fn[0];
  54          	 $method = $fn[1];
  55          	 $obj->$method ($rs->_array[$i],$rs);
  56        } else {
  57  	 	 	 $fn($rs->_array[$i],$rs);
  58        }
  59  
  60  	 }
  61  	 if (!$rs->EOF) {
  62  	 	 $rs->_currentRow = 0;
  63  	 	 $rs->fields = $rs->_array[0];
  64  	 }
  65  
  66  	 return $rs;
  67  }