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