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

Modules/ScormAicc/classes/class.ilObjAICCCourseInterchangeFiles.php

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

Generated on Fri Dec 13 2013 17:56:52 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1