]> git.mjollnir.org Git - moodle.git/commitdiff
Merged from STABLE
authorthepurpleblob <thepurpleblob>
Mon, 8 May 2006 10:39:14 +0000 (10:39 +0000)
committerthepurpleblob <thepurpleblob>
Mon, 8 May 2006 10:39:14 +0000 (10:39 +0000)
lib/questionlib.php
question/editlib.php

index 06980b7d07547c07d915d4be95434ee0680555c8..185add73864752a3d118f3837d9b41465a86c180 100644 (file)
@@ -1285,6 +1285,96 @@ function get_question_fraction_grade($question, $state) {
 
 /// CATEGORY FUNCTIONS /////////////////////////////////////////////////////////////////
 
+/**
+ * returns the categories with their names ordered following parent-child relationships
+ * finally it tries to return pending categories (those being orphaned, whose parent is
+ * incorrect) to avoid missing any category from original array.
+ */
+function sort_categories_by_tree(&$categories, $id = 0, $level = 1) {
+    $children = array();
+    $keys = array_keys($categories);
+
+    foreach ($keys as $key) {
+        if (!isset($categories[$key]->processed) && $categories[$key]->parent == $id) {
+            $children[$key] = $categories[$key];
+            $categories[$key]->processed = true;
+            $children = $children + sort_categories_by_tree($categories, $children[$key]->id, $level+1);
+        }
+    }
+    //If level = 1, we have finished, try to look for non processed categories (bad parent) and sort them too
+    if ($level == 1) {
+        foreach ($keys as $key) {
+            //If not processed and it's a good candidate to start (because its parent doesn't exist in the course)
+            if (!isset($categories[$key]->processed) && !record_exists('question_categories', 'course', $categories[$key]->course, 'id', $categories[$key]->parent)) {
+                $children[$key] = $categories[$key];
+                $categories[$key]->processed = true;
+                $children = $children + sort_categories_by_tree($categories, $children[$key]->id, $level+1);
+            }
+        }
+    }
+    return $children;
+}
+
+/**
+ * flattens tree structure created by add_indented_named 
+ * (adding the names)
+ * @param array cats tree structure of categories
+ * @param int depth tree depth tracker (for indenting)
+ * @return array flattened, formatted list
+ */
+function flatten_category_tree( $cats, $depth=0 ) {
+    $newcats = array();
+    $fillstr = '&nbsp;&nbsp;&nbsp;';
+
+    foreach ($cats as $key => $cat) {
+        $newcats[$key] = $cat;
+        $newcats[$key]->indentedname = str_repeat($fillstr,$depth) . $cat->name;
+        // recurse if the category has children
+        if (!empty($cat->children)) {
+            $newcats += flatten_category_tree( $cat->children, $depth+1 ); 
+        }
+    }
+
+    return $newcats;
+}
+
+/**
+ * format categories into indented list
+ * @param array categories categories array (from db)
+ * @return array formatted list of categories
+ */
+function add_indented_names( $categories ) {
+
+    // iterate through categories adding new fields
+    // and creating references
+    foreach ($categories as $key => $category) {
+        $categories[$key]->children = array();
+        $categories[$key]->link = &$categories[$key];
+    }
+
+    // create tree structure of children
+    // link field is used to track 'new' place of category in tree
+    foreach ($categories as $key => $category) {
+        if (!empty($category->parent)) {
+            $categories[$category->parent]->link->children[$key] = $categories[$key];
+            $categories[$key]->link = &$categories[$category->parent]->link->children[$key];
+        }
+    }
+
+    // remove top level categories with parents
+    $newcats = array();
+    foreach ($categories as $key => $category) {
+        unset( $category->link );
+        if (empty($category->parent)) {
+            $newcats[$key] = $category;
+        }
+    }
+
+    // walk the tree to flatten revised structure
+    $categories = flatten_category_tree( $newcats );
+
+    return $categories;
+}
 
 /**
 * Displays a select menu of categories with appended course names
index 7dd045841e0d658c758f7042c00904ddbdc19372..50e3e1d945c1cddb44c78b812742cc123fd9b99d 100644 (file)
@@ -115,96 +115,6 @@ function question_category_menu($courseid, $published=false) {
     }
     return $catmenu;
 }
-/**
- * returns the categories with their names ordered following parent-child relationships
- * finally it tries to return pending categories (those being orphaned, whose parent is
- * incorrect) to avoid missing any category from original array.
- */
-function sort_categories_by_tree(&$categories, $id = 0, $level = 1) {
-    $children = array();
-    $keys = array_keys($categories);
-
-    foreach ($keys as $key) {
-        if (!isset($categories[$key]->processed) && $categories[$key]->parent == $id) {
-            $children[$key] = $categories[$key];
-            $categories[$key]->processed = true;
-            $children = $children + sort_categories_by_tree($categories, $children[$key]->id, $level+1);
-        }
-    }
-    //If level = 1, we have finished, try to look for non processed categories (bad parent) and sort them too
-    if ($level == 1) {
-        foreach ($keys as $key) {
-            //If not processed and it's a good candidate to start (because its parent doesn't exist in the course)
-            if (!isset($categories[$key]->processed) && !record_exists('question_categories', 'course', $categories[$key]->course, 'id', $categories[$key]->parent)) {
-                $children[$key] = $categories[$key];
-                $categories[$key]->processed = true;
-                $children = $children + sort_categories_by_tree($categories, $children[$key]->id, $level+1);
-            }
-        }
-    }
-    return $children;
-}
-
-/**
- * flattens tree structure created by add_indented_named 
- * (adding the names)
- * @param array cats tree structure of categories
- * @param int depth tree depth tracker (for indenting)
- * @return array flattened, formatted list
- */
-function flatten_category_tree( $cats, $depth=0 ) {
-    $newcats = array();
-    $fillstr = '&nbsp;&nbsp;&nbsp;';
-
-    foreach ($cats as $key => $cat) {
-        $newcats[$key] = $cat;
-        $newcats[$key]->indentedname = str_repeat($fillstr,$depth) . $cat->name;
-        // recurse if the category has children
-        if (!empty($cat->children)) {
-            $newcats += flatten_category_tree( $cat->children, $depth+1 ); 
-        }
-    }
-
-    return $newcats;
-}
-
-/**
- * format categories into indented list
- * @param array categories categories array (from db)
- * @return array formatted list of categories
- */
-function add_indented_names( $categories ) {
-
-    // iterate through categories adding new fields
-    // and creating references
-    foreach ($categories as $key => $category) {
-        $categories[$key]->children = array();
-        $categories[$key]->link = &$categories[$key];
-    }
-
-    // create tree structure of children
-    // link field is used to track 'new' place of category in tree
-    foreach ($categories as $key => $category) {
-        if (!empty($category->parent)) {
-            $categories[$category->parent]->link->children[$key] = $categories[$key];
-            $categories[$key]->link = &$categories[$category->parent]->link->children[$key];
-        }
-    }
-
-    // remove top level categories with parents
-    $newcats = array();
-    foreach ($categories as $key => $category) {
-        unset( $category->link );
-        if (empty($category->parent)) {
-            $newcats[$key] = $category;
-        }
-    }
-
-    // walk the tree to flatten revised structure
-    $categories = flatten_category_tree( $newcats );
-
-    return $categories;
-}
 
 /**
  * prints a form to choose categories