22edd738f4c199b2f97b11af30ad277667712366
[acontent.git] / docs / include / lib / qti.inc.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 /* Variables */
14 $supported_media_type = array(  'gif', 'jpg', 'bmp', 'png', 'jpeg', 
15                                                                 'txt', 'css', 'html', 'htm', 'csv', 'asc', 'tsv', 'xml', 'xsl',
16                                                                 'wav', 'au', 'mp3', 'mov');
17
18 /* 
19  * Create directory recursively based on the path.
20  * @param       string  URI of the path, separated by the directory separator.
21  * 
22  */
23 function createDir($path){
24         //base case
25         if (is_dir($path)){
26                 return;
27         } else {
28                 preg_match('/(.*)[\/\\\\]([^\\\\\/]+)\/?$/', $path, $matches);
29                 createDir($matches[1]);
30                 //make directory if it's not a filename.
31                 if (preg_match('/(.*)\.[\w]+$/', $matches[2])===0) {
32                         mkdir($matches[0], 0700);
33                 }
34         }       
35 }
36
37 /*
38  * Trimming the value (For array walk function)
39  * @param       value reference
40  * @return      value reference
41  */
42 function trim_value(&$value) { 
43         $value = trim($value); 
44 }
45
46
47 /** 
48   * Check if file exists, return true if it is, false otherwise
49   * @param      array   resources that each array items store the information of the resource, such as 
50   *                                     link, href, etc.
51   * @return     an array of existing filenames, return empty array otherwise.
52   */
53 function isQTIFileExist($attributes){
54         global $supported_media_type; 
55         $existing_files = array();
56
57         foreach ($attributes as $resource=>$attrs){
58                 if ($attrs['type'] == 'imsqti_xmlv1p1' || $attrs['type'] == 'imsqti_item_xmlv2p1'){
59                         //loop through the file array
60                         foreach($attrs['file'] as $file_id => $file_name){
61                                 $file_pathinfo = pathinfo($file_name);
62 //                              if ($file_pathinfo['basename'] == $attrs['href']){
63 //                                      //This file will be parsed later
64 //                                      continue;
65 //                              } 
66
67                                 if (in_array($file_pathinfo['extension'], $supported_media_type)){
68                                         //check media
69                                         if (file_exists(TR_CONTENT_DIR . $_SESSION['course_id'] . '/' . $file_name)){
70                                                 $existing_files[] = $file_name;
71                                         }
72                                 }
73                         }
74                 }
75         }
76         return $existing_files;
77 }
78 ?>