• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

content/classes/class.ilObjAICCCourseInterchangeFiles.php

Go to the documentation of this file.
00001 <?
00002 
00003 class ilObjAICCCourseInterchangeFiles {
00004         
00005         var $coursefiles;
00006         var $data;
00007         var $errorText;
00008         var $requiredFiles=array("crs", "au", "cst", "des");
00009         var $optionalFiles=array("cmp", "ort", "pre");
00010         
00011         function ilObjAICCCourseInterchangeFiles() {
00012                 $this->errorText=array();
00013                 $this->coursefiles=array();
00014 
00015         }
00016         
00017         //searching for aicc files, beginning in $dir
00018         // sometimes they are in the main dir - sometimes not
00019         // sometimes uppercase - sometimes not
00020         function findFiles($dir) {
00021                 $suffixes=array_merge($this->requiredFiles,$this->optionalFiles);
00022                 $files=$this->getAllFiles($dir);
00023                 foreach ($files as $file) {
00024                         foreach($suffixes as $suffix) {
00025                                 if (strcasecmp(substr($file, -(strlen($suffix)+1)), ".".$suffix)==0)
00026                                         $this->coursefiles[$suffix] = $file;    
00027                         }
00028                 }
00029                 
00030                 //check for required files
00031                 $missingFiles = array_diff ($this->requiredFiles, array_keys($this->coursefiles));
00032                 if (count($missingFiles)==4)
00033                         $this->errorText[]="Missing all required files.<br>You want to check if your learning module is of a different type.";
00034                 else if (count($missingFiles)>0)
00035                         $this->errorText[]="Missing required file(s): ".implode("<bR>", $missingFiles);
00036         }
00037 
00038         function readFiles() {
00039                 $this->data=array();
00040                 
00041                 foreach ($this->coursefiles as $suffix=>$filename) {
00042                         if ($suffix=="crs")
00043                                 $this->data[$suffix]=$this->readCRS($filename); 
00044                         else
00045                                 $this->data[$suffix]=$this->readCSVFile($filename);                     
00046                 }
00047         
00048                 //Prepare Data-Array: all keys to lower
00049                 $this->data=$this->arraykeys_tolower($this->data);
00050         }
00051         
00052         function getDescriptor($system_id) {
00053                 foreach ($this->data["des"] as $row) {
00054                         if (strcasecmp ($row["system_id"],$system_id)==0)
00055                                 return $row;
00056                 }
00057         }
00058         
00059         function validate() {
00060                 $this->checkRequiredKeys();
00061                 $this->checkStructure();
00062         }
00063         
00064         function checkRequiredKeys() {
00065                 //AU
00066                 $requiredKeys=array("system_id", "type", "command_line", "file_name",
00067                                                                                                 "max_score", "mastery_score", "max_time_allowed",
00068                                                                                                 "time_limit_action", "system_vendor", "core_vendor",
00069                                                                                                 "web_launch", "au_password");
00070                 $this->checkCourseFile("au", $this->data["au"], $requiredKeys);
00071                 $this->checkColumnCount("au", $this->data["au"]);
00072                                                                                                         
00073                 //DES
00074                 $requiredKeys=array("system_id", "title", "description", "developer_id");
00075                 $this->checkCourseFile("des", $this->data["des"], $requiredKeys);
00076                 $this->checkColumnCount("des", $this->data["des"]);
00077         
00078                 //CRS
00079                 $requiredKeys=array("course_creator", "course_id", "course_system", "course_title",
00080                                                                                                  "level", "max_fields_cst", "total_aus", "total_blocks", "version");
00081                 $this->checkCourseFile("crs", $this->data["crs"], $requiredKeys, "course");
00082                 $requiredKeys=array("max_normal");
00083                 $this->checkCourseFile("crs", $this->data["crs"], $requiredKeys, "course_behavior");
00084                                         
00085                 //CST
00086                 $requiredKeys=array("block", "member");
00087                 $this->checkCourseFile("cst", $this->data["cst"], $requiredKeys,0);     
00088                 $this->checkColumnCount("cst", $this->data["cst"]);
00089                                                                 
00090                 return $errorText;
00091         }
00092         
00093         function checkCourseFile($fileSuffix, $data, $requiredKeys, $group=0) {
00094         
00095                 if (count($data)>0 && is_array($data[$group])) {
00096                         
00097                         $keys=array_keys($data[$group]);
00098         
00099                         $missingKeys = array_diff ($requiredKeys, $keys);
00100                         $optionalKeys = array_diff ($keys, $requiredKeys);
00101                         
00102                         if (count($missingKeys)>0)
00103                                 $this->errorText[]="missing keys in ".strtoupper($fileSuffix)."-File: ".implode(",", $missingKeys);
00104                         
00105                 } else if (count($data)>0 && !is_array($data[$group])) {
00106                         $this->errorText[]="missing Group in ".strtoupper($fileSuffix)."-File: $group";
00107                 } else {
00108                         $this->errorText[]="empty ".strtoupper($fileSuffix)."-File";
00109                 }
00110 
00111         }
00112         
00113         function checkColumnCount($fileSuffix, $data) {
00114                 if (count($data)>0) {
00115                         $colcount=-1;
00116                         for ($colnr=0;$colnr<count($data);$colnr++) {
00117                                 if ($colcount==-1)
00118                                         $colcount=count($data[$colnr]);
00119                                 else if ($colcount!=count($data[$colnr]))
00120                                         $this->errorText[]=strtoupper($fileSuffix)."-File: unexpected number of columns in line ".($colnr+2);
00121                         }
00122                 }
00123         }
00124         
00125         function checkStructure() {
00126                 
00127                 //max member fields in cst-file
00128                 $max=$this->data[crs][course][max_fields_cst];
00129                 for ($row=0;$row<count($this->data[cst]);$row++) {
00130                         $value=$this->data[cst][$row][member];
00131                         if ((is_array($value) && count($value)>$max) || (!is_array($value) && $max==1)) {
00132                                 $this->errorText[]="CRS-File: max_fields_cst does not match number of fields in the CST-File";
00133                                 return;
00134                         }
00135                 }
00136                 
00137                 //additional validation statements
00138                 //
00139                 //
00140                 
00141         }
00142         
00143         
00144         function readCRS($filename) {
00145                 $data=@parse_ini_file($filename, TRUE);
00146         
00147                 //crs-file is not a valid iniFile
00148                 //thats why reading the file again to get course_description
00149                 $lines=file($filename);
00150                 for($i=0;$i<count($lines);$i++) {
00151                         if (trim($lines[$i])=="[Course_Description]") {
00152                                 for ($i++;$i<count($lines);$i++) {
00153                                         if (strlen(trim($lines[$i]))>0) {
00154                                                 $data["Course_Description"][description]=$lines[$i];
00155                                                 break;
00156                                         }
00157                                 }
00158                         }
00159                 }
00160                 
00161                 return $data;
00162         }
00163         
00164         function readCSVFile($filename) {
00165                 $row=1;
00166                 $handle = fopen($filename, "r");
00167                 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
00168                         if ($row++==1) {
00169                                 $header=$data;
00170                         } else if (count($data)>1) {
00171                                 $data2=array();
00172                                 for ($col=0; $col<count($data); $col++) {
00173                                         if (array_key_exists($header[$col], $data2)) {
00174                                                 $value=$data2[$header[$col]];
00175                                                 if (!is_array($value))
00176                                                         $data2[$header[$col]]=array($value, utf8_encode($data[$col]));
00177                                                 else
00178                                                         $data2[$header[$col]][]=utf8_encode($data[$col]);
00179                                         } else
00180                                                 $data2[$header[$col]]=utf8_encode($data[$col]);
00181                                 }
00182                                 $rows[]=$data2; 
00183                         }
00184                 }
00185                 fclose($handle);
00186                 return $rows;
00187         }
00188         
00189         function getAllFiles($dir, $arr=array()) {
00190                 if (substr($dir, -1)!="/")
00191                         $dir.="/";
00192 
00193                 $handle=opendir($dir); 
00194                 while ($file = readdir ($handle)) { 
00195                   if ($file != "." && $file != "..") { 
00196                         if (is_dir($dir.$file))
00197                                 $arr=$this->getAllFiles($dir.$file, $arr);
00198                 else
00199                         $arr[]=$dir.$file;
00200                 } 
00201                 }
00202                 closedir($handle);      
00203                 return $arr;
00204         }
00205         
00206         function arraykeys_tolower($arr) {
00207                 $arr=array_change_key_case($arr, CASE_LOWER);
00208                 foreach ($arr as $k=>$v) {
00209                         if (is_array($v))
00210                                 $arr[$k]=$this->arraykeys_tolower($v);
00211                 }
00212                 return $arr;
00213         }
00214         
00215         function writeToDatabase($alm_id) {
00216                 include_once("content/classes/AICC/class.ilAICCTree.php");
00217                 include_once("content/classes/AICC/class.ilAICCCourse.php");
00218                 include_once("content/classes/AICC/class.ilAICCUnit.php");
00219                 include_once("content/classes/AICC/class.ilAICCBlock.php");
00220                 
00221                 //write course to database
00222                 $course=new ilAICCCourse();
00223                 $course->setALMId($alm_id);
00224                 $course->setSystemId("root");
00225                 $course->setTitle($this->data["crs"]["course"]["course_title"]);
00226                 $course->setDescription($this->data["crs"]["course_description"]["description"]);
00227                 
00228                 $course->setCourseCreator($this->data["crs"]["course"]["course_creator"]);
00229                 $course->setCourseId($this->data["crs"]["course"]["course_id"]);
00230                 $course->setCourseSystem($this->data["crs"]["course"]["course_system"]);
00231                 $course->setCourseTitle($this->data["crs"]["course"]["course_title"]);
00232                 $course->setLevel($this->data["crs"]["course"]["level"]);
00233                 $course->setMaxFieldsCst($this->data["crs"]["course"]["max_fields_cst"]);
00234                 $course->setMaxFieldsOrt($this->data["crs"]["course"]["max_fields_ort"]);
00235                 $course->setTotalAUs($this->data["crs"]["course"]["total_aus"]);
00236                 $course->setTotalBlocks($this->data["crs"]["course"]["total_blocks"]);
00237                 $course->setTotalComplexObj($this->data["crs"]["course"]["total_complex_obj"]);
00238                 $course->setTotalObjectives($this->data["crs"]["course"]["total_objectives"]);
00239                 $course->setVersion($this->data["crs"]["course"]["version"]);
00240                 $course->setMaxNormal($this->data["crs"]["course_behavior"]["max_normal"]);
00241                 $course->setDescription($this->data["crs"]["course_description"]["description"]);
00242                 $course->create();      
00243                 $identifier["root"]=$course->getId();
00244                 
00245                 //all blocks
00246                 foreach ($this->data["cst"] as $row) {
00247                         $system_id=strtolower($row["block"]);
00248                         if ($system_id!="root") {
00249                                 $unit=new ilAICCBlock();
00250                                 $description=$this->getDescriptor($system_id);
00251                                 $unit->setALMId($alm_id);
00252                                 $unit->setType("sbl");
00253                                 $unit->setTitle($description["title"]);
00254                                 $unit->setDescription($description["description"]);
00255                                 $unit->setDeveloperId($description["developer_id"]);
00256                                 $unit->setSystemId($description["system_id"]);
00257                                 $unit->create();
00258                                 $identifier[$system_id]=$unit->getId();
00259                         }
00260                 }
00261         
00262                 //write assignable units to database
00263                 foreach ($this->data["au"] as $row) {
00264                         $sysid=strtolower($row["system_id"]);
00265                         $unit=new ilAICCUnit();
00266                         
00267                         $unit->setAUType($row["type"]);
00268                         $unit->setCommand_line($row["command_line"]);
00269                         $unit->setMaxTimeAllowed($row["max_time_allowed"]);
00270                         $unit->setTimeLimitAction($row["time_limit_action"]);
00271                         $unit->setMaxScore($row["max_score"]);
00272                         $unit->setCoreVendor($row["core_vendor"]);
00273                         $unit->setSystemVendor($row["system_vendor"]);
00274                         $unit->setFilename($row["file_name"]);
00275                         $unit->setMasteryScore($row["mastery_score"]);
00276                         $unit->setWebLaunch($row["web_launch"]);
00277                         $unit->setAUPassword($row["au_password"]);
00278                                 
00279                         $description=$this->getDescriptor($sysid);
00280                         $unit->setALMId($alm_id);
00281                         $unit->setType("sau");
00282                         $unit->setTitle($description["title"]);
00283                         $unit->setDescription($description["description"]);
00284                         $unit->setDeveloperId($description["developer_id"]);
00285                         $unit->setSystemId($description["system_id"]);
00286                         $unit->create();
00287                         $identifier[$sysid]=$unit->getId();     
00288                 }
00289                 
00290                 //write tree
00291                 $tree =& new ilAICCTree($alm_id);
00292                 $tree->addTree($alm_id, $identifier["root"]);
00293                 
00294                 //writing members
00295                 foreach ($this->data["cst"] as $row) {
00296                         $members=$row["member"];
00297                         if (!is_array($members))
00298                                 $members=array($members);
00299                         $parentid=$identifier[strtolower($row["block"])];
00300 
00301                         foreach($members as $member) {
00302                                 $memberid=$identifier[strtolower($member)];
00303                                 $tree->insertNode($memberid, $parentid);
00304                         }
00305                 }               
00306         }
00307         
00308 }
00309 
00310 ?>

Generated on Fri Dec 13 2013 09:06:35 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1