2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
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 $
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.
21 function buildTree($current, $content_array){
23 foreach($current as $order=>$content_id){
25 if (isset($content_array[$content_id])){
26 $wrapper[$content_id] = buildTree($content_array[$content_id], $content_array);
31 $folder['order_'.$order] = $wrapper;
34 $folder['order_'.$order] = $content_id;
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.
50 function rebuild($tree, $node=''){
53 if (!is_array($tree)){
57 $tree['order_0'] = $node;
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);
66 //if this is the content layer
68 $folder[$k] = rebuild($v, $k);
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)
83 function reconstruct($tree, $order, $content_parent_id, $table_prefix){
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)){
95 foreach ($tree as $k=>$v){
96 if (preg_match('/order\_([\d]+)/', $k, $match)==1){
98 reconstruct($v, $match[1], $content_parent_id, $table_prefix); //inherit the previous layer id
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 . ', '
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'] . ', '
119 if (mysql_query($sql, $db)){
120 $folder_id = mysql_insert_id();
121 reconstruct($v, '', $folder_id, $table_prefix);