Explorar o código

Added Code validations and fixes - Refs #8170

José Loguercio %!s(int64=8) %!d(string=hai) anos
pai
achega
61a5bbfd41

+ 17 - 10
main/admin/course_import.php

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

+ 1 - 1
main/inc/lib/add_course.lib.inc.php

@@ -693,7 +693,7 @@ class AddCourse
                 'category' =>'certificates'
             ],
             '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;

+ 2 - 0
main/inc/lib/export.lib.inc.php

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

+ 1 - 0
main/inc/lib/import.lib.php

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