Added Code validations and fixes - Refs #8170

pull/2487/head
José Loguercio 9 years ago
parent d85d8d551b
commit 61a5bbfd41
  1. 27
      main/admin/course_import.php
  2. 2
      main/inc/lib/add_course.lib.inc.php
  3. 2
      main/inc/lib/export.lib.inc.php
  4. 1
      main/inc/lib/import.lib.php

@ -9,6 +9,9 @@
/** /**
* Validates imported data. * Validates imported data.
*
* @param array $courses
* @return array $errors
*/ */
function validate_data($courses) function validate_data($courses)
{ {
@ -20,14 +23,14 @@ function validate_data($courses)
// 1. Check whether mandatory fields are set. // 1. Check whether mandatory fields are set.
$mandatory_fields = array ('Code', 'Title', 'CourseCategory'); $mandatory_fields = array ('Code', 'Title', 'CourseCategory');
foreach ($mandatory_fields as $field) { foreach ($mandatory_fields as $field) {
if (!isset($course[$field]) || strlen($course[$field]) == 0) { if (empty($course[$field])) {
$course['error'] = get_lang($field.'Mandatory'); $course['error'] = get_lang($field.'Mandatory');
$errors[] = $course; $errors[] = $course;
} }
} }
// 2. Check current course code. // 2. Check current course code.
if (isset ($course['Code']) && strlen($course['Code']) != 0) { if (!empty($course['Code'])) {
// 2.1 Check whether code has been already used by this CVS-file. // 2.1 Check whether code has been already used by this CVS-file.
if (isset($coursecodes[$course['Code']])) { if (isset($coursecodes[$course['Code']])) {
$course['error'] = get_lang('CodeTwiceInFile'); $course['error'] = get_lang('CodeTwiceInFile');
@ -61,12 +64,14 @@ function validate_data($courses)
} }
} }
// 4. Check whether course category exists. if(!empty($course['CourseCategory'])) {
if (isset($course['CourseCategory']) && strlen($course['CourseCategory']) != 0) {
$categoryInfo = CourseCategory::getCategory($course['CourseCategory']); $categoryInfo = CourseCategory::getCategory($course['CourseCategory']);
if (empty($categoryInfo)) { if (empty($categoryInfo)) {
CourseCategory::addNode($course['CourseCategory'], $course['CourseCategoryName'] ? $course['CourseCategoryName'] : $course['CourseCategory'], 'TRUE', null); CourseCategory::addNode($course['CourseCategory'], $course['CourseCategoryName'] ? $course['CourseCategoryName'] : $course['CourseCategory'], 'TRUE', null);
} }
} else {
$course['error'] = get_lang('NoCourseCategorySupplied');
$errors[] = $course;
} }
} }
@ -74,8 +79,9 @@ function validate_data($courses)
} }
/** /**
* @param array $teachers * Get the teacher list
* *
* @param array $teachers
* @return array * @return array
*/ */
function getTeacherListInArray($teachers) function getTeacherListInArray($teachers)
@ -149,7 +155,7 @@ function save_data($courses)
*/ */
function parse_csv_data($file) function parse_csv_data($file)
{ {
$courses = Import::csvToArray($file); $courses = Import::csv_reader($file);
return $courses; return $courses;
} }
@ -186,6 +192,7 @@ if (isset($_POST['formSent']) && $_POST['formSent']) {
Display :: display_error_message(get_lang('YouMustImportAFileAccordingToSelectedOption')); Display :: display_error_message(get_lang('YouMustImportAFileAccordingToSelectedOption'));
} else { } else {
$courses = parse_csv_data($_FILES['import_file']['tmp_name']); $courses = parse_csv_data($_FILES['import_file']['tmp_name']);
$errors = validate_data($courses); $errors = validate_data($courses);
if (count($errors) == 0) { if (count($errors) == 0) {
save_data($courses); save_data($courses);
@ -219,10 +226,10 @@ $form->display();
<blockquote> <blockquote>
<pre> <pre>
<strong>Code</strong>;<strong>Title</strong>;<strong>CourseCategory</strong>;Teacher;Language <strong>Code</strong>;<strong>Title</strong>;<strong>CourseCategory</strong>;<strong>CourseCategoryName</strong>;Teacher;Language
BIO0015;Biology;BIO;teacher1;english BIO0015;Biology;BIO;Science;teacher1;english
BIO0016;Maths;MATH;teacher2|teacher3;english BIO0016;Maths;MATH;Engineerng;teacher2|teacher3;english
BIO0017;Language;LANG;;english BIO0017;Language;LANG;;;english
</pre> </pre>
</blockquote> </blockquote>

@ -693,7 +693,7 @@ class AddCourse
'category' =>'certificates' 'category' =>'certificates'
], ],
'documents_default_visibility' => ['default' =>'visible', 'category' =>'document'], 'documents_default_visibility' => ['default' =>'visible', 'category' =>'document'],
'show_course_in_user_language' => ['default' => 2], 'show_course_in_user_language' => ['default' => 2, 'category' => null],
]; ];
$counter = 1; $counter = 1;

@ -37,6 +37,8 @@ class Export
* Export tabular data to CSV-file * Export tabular data to CSV-file
* @param array $data * @param array $data
* @param string $filename * @param string $filename
*
* @return mixed csv file | false if no data to export
*/ */
public static function arrayToCsv($data, $filename = 'export') public static function arrayToCsv($data, $filename = 'export')
{ {

@ -16,6 +16,7 @@ class Import
{ {
/** /**
* @param string $path * @param string $path
* @param bool $setFirstRowAsHeader
* @return CsvReader * @return CsvReader
*/ */
public static function csv_reader($path, $setFirstRowAsHeader = true) public static function csv_reader($path, $setFirstRowAsHeader = true)

Loading…
Cancel
Save