original_string: public static function merge_datasets(array $files, $modelid, $timesplittingid, $filearea, $evaluation = false) {
  
          $tmpfilepath = make_request_directory() . DIRECTORY_SEPARATOR . 'tmpfile.csv';
  
          // Add headers.
          // We could also do this with a single iteration gathering all files headers and appending them to the beginning of the file
          // once all file contents are merged.
          $varnames = '';
          $analysablesvalues = array();
          foreach ($files as $file) {
              $rh = $file->get_content_file_handle();
  
              // Copy the var names as they are, all files should have the same var names.
              $varnames = fgetcsv($rh);
  
              $analysablesvalues[] = fgetcsv($rh);
  
              // Copy the columns as they are, all files should have the same columns.
              $columns = fgetcsv($rh);
          }
  
          // Merge analysable values skipping the ones that are the same in all analysables.
          $values = array();
          foreach ($analysablesvalues as $analysablevalues) {
              foreach ($analysablevalues as $varkey => $value) {
                  // Sha1 to make it unique.
                  $values[$varkey][sha1($value)] = $value;
              }
          }
          foreach ($values as $varkey => $varvalues) {
              $values[$varkey] = implode('|', $varvalues);
          }
  
          // Start writing to the merge file.
          $wh = fopen($tmpfilepath, 'w');
          if (!$wh) {
              throw new \moodle_exception('errorcannotwritedataset', 'analytics', '', $tmpfilepath);
          }
  
          fputcsv($wh, $varnames);
          fputcsv($wh, $values);
          fputcsv($wh, $columns);
  
          // Iterate through all files and add them to the tmp one. We don't want file contents in memory.
          foreach ($files as $file) {
              $rh = $file->get_content_file_handle();
  
              // Skip headers.
              fgets($rh);
              fgets($rh);
              fgets($rh);
  
              // Copy all the following lines.
              while ($line = fgets($rh)) {
                  fwrite($wh, $line);
              }
              fclose($rh);
          }
          fclose($wh);
  
          $filerecord = [
              'component' => 'analytics',
              'filearea' => $filearea,
              'itemid' => $modelid,
              'contextid' => \context_system::instance()->id,
              'filepath' => '/timesplitting/' . \core_analytics\analysis::clean_time_splitting_id($timesplittingid) . '/',
              'filename' => self::get_filename($evaluation)
          ];
  
          $fs = get_file_storage();
  
          return $fs->create_file_from_pathname($filerecord, $tmpfilepath);
      }
code: public static function merge_datasets(array $files, $modelid, $timesplittingid, $filearea, $evaluation = false) {
  
          $tmpfilepath = make_request_directory() . DIRECTORY_SEPARATOR . 'tmpfile.csv';
  
          // Add headers.
          // We could also do this with a single iteration gathering all files headers and appending them to the beginning of the file
          // once all file contents are merged.
          $varnames = '';
          $analysablesvalues = array();
          foreach ($files as $file) {
              $rh = $file->get_content_file_handle();
  
              // Copy the var names as they are, all files should have the same var names.
              $varnames = fgetcsv($rh);
  
              $analysablesvalues[] = fgetcsv($rh);
  
              // Copy the columns as they are, all files should have the same columns.
              $columns = fgetcsv($rh);
          }
  
          // Merge analysable values skipping the ones that are the same in all analysables.
          $values = array();
          foreach ($analysablesvalues as $analysablevalues) {
              foreach ($analysablevalues as $varkey => $value) {
                  // Sha1 to make it unique.
                  $values[$varkey][sha1($value)] = $value;
              }
          }
          foreach ($values as $varkey => $varvalues) {
              $values[$varkey] = implode('|', $varvalues);
          }
  
          // Start writing to the merge file.
          $wh = fopen($tmpfilepath, 'w');
          if (!$wh) {
              throw new \moodle_exception('errorcannotwritedataset', 'analytics', '', $tmpfilepath);
          }
  
          fputcsv($wh, $varnames);
          fputcsv($wh, $values);
          fputcsv($wh, $columns);
  
          // Iterate through all files and add them to the tmp one. We don't want file contents in memory.
          foreach ($files as $file) {
              $rh = $file->get_content_file_handle();
  
              // Skip headers.
              fgets($rh);
              fgets($rh);
              fgets($rh);
  
              // Copy all the following lines.
              while ($line = fgets($rh)) {
                  fwrite($wh, $line);
              }
              fclose($rh);
          }
          fclose($wh);
  
          $filerecord = [
              'component' => 'analytics',
              'filearea' => $filearea,
              'itemid' => $modelid,
              'contextid' => \context_system::instance()->id,
              'filepath' => '/timesplitting/' . \core_analytics\analysis::clean_time_splitting_id($timesplittingid) . '/',
              'filename' => self::get_filename($evaluation)
          ];
  
          $fs = get_file_storage();
  
          return $fs->create_file_from_pathname($filerecord, $tmpfilepath);
      }
code_tokens: public static function merge_datasets ( array $ files , $ modelid , $ timesplittingid , $ filearea , $ evaluation = false ) { $ tmpfilepath = make_request_directory ( ) . DIRECTORY_SEPARATOR . 'tmpfile.csv' ; // Add headers. // We could also do this with a single iteration gathering all files headers and appending them to the beginning of the file // once all file contents are merged. $ varnames = '' ; $ analysablesvalues = array ( ) ; foreach ( $ files as $ file ) { $ rh = $ file -> get_content_file_handle ( ) ; // Copy the var names as they are, all files should have the same var names. $ varnames = fgetcsv ( $ rh ) ; $ analysablesvalues [ ] = fgetcsv ( $ rh ) ; // Copy the columns as they are, all files should have the same columns. $ columns = fgetcsv ( $ rh ) ; } // Merge analysable values skipping the ones that are the same in all analysables. $ values = array ( ) ; foreach ( $ analysablesvalues as $ analysablevalues ) { foreach ( $ analysablevalues as $ varkey => $ value ) { // Sha1 to make it unique. $ values [ $ varkey ] [ sha1 ( $ value ) ] = $ value ; } } foreach ( $ values as $ varkey => $ varvalues ) { $ values [ $ varkey ] = implode ( '|' , $ varvalues ) ; } // Start writing to the merge file. $ wh = fopen ( $ tmpfilepath , 'w' ) ; if ( ! $ wh ) { throw new \ moodle_exception ( 'errorcannotwritedataset' , 'analytics' , '' , $ tmpfilepath ) ; } fputcsv ( $ wh , $ varnames ) ; fputcsv ( $ wh , $ values ) ; fputcsv ( $ wh , $ columns ) ; // Iterate through all files and add them to the tmp one. We don't want file contents in memory. foreach ( $ files as $ file ) { $ rh = $ file -> get_content_file_handle ( ) ; // Skip headers. fgets ( $ rh ) ; fgets ( $ rh ) ; fgets ( $ rh ) ; // Copy all the following lines. while ( $ line = fgets ( $ rh ) ) { fwrite ( $ wh , $ line ) ; } fclose ( $ rh ) ; } fclose ( $ wh ) ; $ filerecord = [ 'component' => 'analytics' , 'filearea' => $ filearea , 'itemid' => $ modelid , 'contextid' => \ context_system :: instance ( ) -> id , 'filepath' => '/timesplitting/' . \ core_analytics \ analysis :: clean_time_splitting_id ( $ timesplittingid ) . '/' , 'filename' => self :: get_filename ( $ evaluation ) ] ; $ fs = get_file_storage ( ) ; return $ fs -> create_file_from_pathname ( $ filerecord , $ tmpfilepath ) ; }
docstring: Merge multiple files into one.
  
  Important! It is the caller responsability to ensure that the datasets are compatible.
  
  @param array  $files
  @param int    $modelid
  @param string $timesplittingid
  @param string $filearea
  @param bool   $evaluation
  @return \stored_file
docstring_tokens: Merge multiple files into one .