return external_api::external_function_info($function, $strictness);
}
+/**
+ * Retrieves an array of records from a CSV file and places
+ * them into a given table structure
+ * This function is deprecated. Please use csv_import_reader() instead.
+ *
+ * @deprecated since Moodle 3.2 MDL-55126
+ * @todo MDL-55195 for final deprecation in Moodle 3.6
+ * @see csv_import_reader::load_csv_content()
+ * @global stdClass $CFG
+ * @global moodle_database $DB
+ * @param string $file The path to a CSV file
+ * @param string $table The table to retrieve columns from
+ * @return bool|array Returns an array of CSV records or false
+ */
+function get_records_csv($file, $table) {
+ global $CFG, $DB;
+
+ debugging('get_records_csv() is deprecated. Please use lib/csvlib.class.php csv_import_reader() instead.');
+
+ if (!$metacolumns = $DB->get_columns($table)) {
+ return false;
+ }
+
+ if(!($handle = @fopen($file, 'r'))) {
+ print_error('get_records_csv failed to open '.$file);
+ }
+
+ $fieldnames = fgetcsv($handle, 4096);
+ if(empty($fieldnames)) {
+ fclose($handle);
+ return false;
+ }
+
+ $columns = array();
+
+ foreach($metacolumns as $metacolumn) {
+ $ord = array_search($metacolumn->name, $fieldnames);
+ if(is_int($ord)) {
+ $columns[$metacolumn->name] = $ord;
+ }
+ }
+
+ $rows = array();
+
+ while (($data = fgetcsv($handle, 4096)) !== false) {
+ $item = new stdClass;
+ foreach($columns as $name => $ord) {
+ $item->$name = $data[$ord];
+ }
+ $rows[] = $item;
+ }
+
+ fclose($handle);
+ return $rows;
+}
+
+/**
+ * Create a file with CSV contents
+ * This function is deprecated. Please use download_as_dataformat() instead.
+ *
+ * @deprecated since Moodle 3.2 MDL-55126
+ * @todo MDL-55195 for final deprecation in Moodle 3.6
+ * @see download_as_dataformat (lib/dataformatlib.php)
+ * @global stdClass $CFG
+ * @global moodle_database $DB
+ * @param string $file The file to put the CSV content into
+ * @param array $records An array of records to write to a CSV file
+ * @param string $table The table to get columns from
+ * @return bool success
+ */
+function put_records_csv($file, $records, $table = NULL) {
+ global $CFG, $DB;
+
+ debugging('put_records_csv() is deprecated. Please use lib/dataformatlib.php download_as_dataformat()');
+
+ if (empty($records)) {
+ return true;
+ }
+
+ $metacolumns = NULL;
+ if ($table !== NULL && !$metacolumns = $DB->get_columns($table)) {
+ return false;
+ }
+
+ echo "x";
+
+ if(!($fp = @fopen($CFG->tempdir.'/'.$file, 'w'))) {
+ print_error('put_records_csv failed to open '.$file);
+ }
+
+ $proto = reset($records);
+ if(is_object($proto)) {
+ $fields_records = array_keys(get_object_vars($proto));
+ }
+ else if(is_array($proto)) {
+ $fields_records = array_keys($proto);
+ }
+ else {
+ return false;
+ }
+ echo "x";
+
+ if(!empty($metacolumns)) {
+ $fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns);
+ $fields = array_intersect($fields_records, $fields_table);
+ }
+ else {
+ $fields = $fields_records;
+ }
+
+ fwrite($fp, implode(',', $fields));
+ fwrite($fp, "\r\n");
+
+ foreach($records as $record) {
+ $array = (array)$record;
+ $values = array();
+ foreach($fields as $field) {
+ if(strpos($array[$field], ',')) {
+ $values[] = '"'.str_replace('"', '\"', $array[$field]).'"';
+ }
+ else {
+ $values[] = $array[$field];
+ }
+ }
+ fwrite($fp, implode(',', $values)."\r\n");
+ }
+
+ fclose($fp);
+ @chmod($CFG->tempdir.'/'.$file, $CFG->filepermissions);
+ return true;
+}
die; //no more chars to output!!!
}
-/**
- * Retrieves an array of records from a CSV file and places
- * them into a given table structure
- *
- * @global stdClass $CFG
- * @global moodle_database $DB
- * @param string $file The path to a CSV file
- * @param string $table The table to retrieve columns from
- * @return bool|array Returns an array of CSV records or false
- */
-function get_records_csv($file, $table) {
- global $CFG, $DB;
-
- if (!$metacolumns = $DB->get_columns($table)) {
- return false;
- }
-
- if(!($handle = @fopen($file, 'r'))) {
- print_error('get_records_csv failed to open '.$file);
- }
-
- $fieldnames = fgetcsv($handle, 4096);
- if(empty($fieldnames)) {
- fclose($handle);
- return false;
- }
-
- $columns = array();
-
- foreach($metacolumns as $metacolumn) {
- $ord = array_search($metacolumn->name, $fieldnames);
- if(is_int($ord)) {
- $columns[$metacolumn->name] = $ord;
- }
- }
-
- $rows = array();
-
- while (($data = fgetcsv($handle, 4096)) !== false) {
- $item = new stdClass;
- foreach($columns as $name => $ord) {
- $item->$name = $data[$ord];
- }
- $rows[] = $item;
- }
-
- fclose($handle);
- return $rows;
-}
-
-/**
- * Create a file with CSV contents
- *
- * @global stdClass $CFG
- * @global moodle_database $DB
- * @param string $file The file to put the CSV content into
- * @param array $records An array of records to write to a CSV file
- * @param string $table The table to get columns from
- * @return bool success
- */
-function put_records_csv($file, $records, $table = NULL) {
- global $CFG, $DB;
-
- if (empty($records)) {
- return true;
- }
-
- $metacolumns = NULL;
- if ($table !== NULL && !$metacolumns = $DB->get_columns($table)) {
- return false;
- }
-
- echo "x";
-
- if(!($fp = @fopen($CFG->tempdir.'/'.$file, 'w'))) {
- print_error('put_records_csv failed to open '.$file);
- }
-
- $proto = reset($records);
- if(is_object($proto)) {
- $fields_records = array_keys(get_object_vars($proto));
- }
- else if(is_array($proto)) {
- $fields_records = array_keys($proto);
- }
- else {
- return false;
- }
- echo "x";
-
- if(!empty($metacolumns)) {
- $fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns);
- $fields = array_intersect($fields_records, $fields_table);
- }
- else {
- $fields = $fields_records;
- }
-
- fwrite($fp, implode(',', $fields));
- fwrite($fp, "\r\n");
-
- foreach($records as $record) {
- $array = (array)$record;
- $values = array();
- foreach($fields as $field) {
- if(strpos($array[$field], ',')) {
- $values[] = '"'.str_replace('"', '\"', $array[$field]).'"';
- }
- else {
- $values[] = $array[$field];
- }
- }
- fwrite($fp, implode(',', $values)."\r\n");
- }
-
- fclose($fp);
- @chmod($CFG->tempdir.'/'.$file, $CFG->filepermissions);
- return true;
-}
-
-
/**
* Recursively delete the file or folder with path $location. That is,
* if it is a file delete it. If it is a folder, delete all its content