5ccc6ffe25a72d47792a6199b033cd5c04521427
[atutor.git] / docs / install / include / ustep_content_conversion.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* http://atutor.ca                                                     */
7 /* This program is free software. You can redistribute it and/or        */
8 /* modify it under the terms of the GNU General Public License          */
9 /* as published by the Free Software Foundation.                        */
10 /************************************************************************/
11 // $Id: ustep_content_conversion.php 8861 2009-11-02 21:34:00Z hwong $
12
13 /** 
14  * Construct a tree based on table entries
15  * @param       array   current node, (current parent)
16  * @param       mixed   a set of parents, where each parents is in the format of [parent]=>children
17  *                                      should remain the same throughout the recursion.
18  * @return      A tree structure representation of the content entries.
19  * @author      Harris Wong
20  */
21 function buildTree($current, $content_array){
22         $folder = array();
23         foreach($current as $order=>$content_id){
24                 //if has children
25                 if (isset($content_array[$content_id])){
26                         $wrapper[$content_id] = buildTree($content_array[$content_id], $content_array);
27                 }
28
29                 //no children.
30                 if ($wrapper){
31                         $folder['order_'.$order] = $wrapper;
32                         unset($wrapper);
33                 } else {
34                         $folder['order_'.$order] = $content_id;
35                 }
36         }       
37         return $folder;
38 }
39
40
41 /**
42  * Transverse the content tree structure, and reconstruct it with the IMS spec.  
43  * This tree has the structure of [order=>array(id)], so the first layer is its order, second is the id
44  * if param merge is true, if node!=null, merge it to top layer, and + offset to all others
45  * @param       mixed   Tree from the buildTree() function, or sub-tree
46  * @param       mixed   the current tree.
47  * @return      A new content tree that meets the IMS specification.
48  * @author      Harris Wong 
49  */
50 function rebuild($tree, $node=''){
51     $order_offset = 0;
52     $folder = array();
53     if (!is_array($tree)){
54         return $tree;
55     }
56     if ($node!=''){
57         $tree['order_0'] = $node;
58         $order_offset += 1;
59     }
60     //go through the tree
61     foreach($tree as $k=>$v){
62         if (preg_match('/order\_([\d]+)/', $k, $match)==1){
63             //if this is the order layer
64             $folder['order_'.($match[1]+$order_offset)] = rebuild($v);
65         } else {
66             //if this is the content layer
67             if(is_array($v)){
68                 $folder[$k] = rebuild($v, $k);
69             }
70         }
71     }
72     return $folder;
73 }
74
75
76 /**
77  * Transverse the tree and update/insert entries based on the updated structure.
78  * @param       array   The tree from rebuild(), and the subtree from the recursion.
79  * @param       int             the ordering of this subtree respect to its parent.
80  * @param       int             parent content id
81  * @return      null (nothing to return, it updates the db only)
82  */
83 function reconstruct($tree, $order, $content_parent_id, $table_prefix){
84         global $db;
85
86         //a content page.
87         if (!is_array($tree)){
88                 $sql = 'UPDATE '.$table_prefix."content SET ordering=$order, content_parent_id=$content_parent_id WHERE content_id=$tree";
89                 if (!mysql_query($sql, $db)){
90                         //throw error
91                         echo mysql_error();
92                 }
93                 return;
94         }
95         foreach ($tree as $k=>$v){
96         if (preg_match('/order\_([\d]+)/', $k, $match)==1){
97                         //order layer
98                         reconstruct($v, $match[1], $content_parent_id, $table_prefix);  //inherit the previous layer id
99                 } else {
100                         //content folder layer
101                         $sql = 'SELECT * FROM '.$table_prefix."content WHERE content_id=$k";
102                         $result = mysql_query($sql, $db);
103                         $old_content_row = mysql_fetch_assoc($result);
104                         $sql = 'INSERT INTO '.$table_prefix.'content (course_id, content_parent_id, ordering, last_modified, revision, formatting, release_date, keywords, content_path, title, use_customized_head, allow_test_export, content_type) VALUES ('
105                                 .$old_content_row['course_id'] . ', '
106                                 .$content_parent_id . ', '
107                                 .$order . ', '
108                                 .'\''. $old_content_row['last_modified'] . '\', '
109                                 .$old_content_row['revision'] . ', '
110                                 .$old_content_row['formatting'] . ', '
111                                 .'\''. $old_content_row['release_date'] . '\', '
112                                 .'\''. mysql_real_escape_string($old_content_row['keywords']) . '\', '
113                                 .'\''. mysql_real_escape_string($old_content_row['content_path']) . '\', '
114                                 .'\''. mysql_real_escape_string($old_content_row['title']) . '\', '
115                                 .$old_content_row['use_customized_head'] . ', '
116                                 .$old_content_row['allow_test_export'] . ', '
117                                 . '1)';
118                         
119                         if (mysql_query($sql, $db)){
120                                 $folder_id = mysql_insert_id();
121                                 reconstruct($v, '', $folder_id, $table_prefix);
122                         } else {
123                                 //throw error
124                                 echo mysql_error();
125                         }
126                 }
127         }
128 }
129 ?>
130