source: moodle/trunk/fuentes/admin/tool/health/locallib.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 4.0 KB
Line 
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Functions used by the health tool.
19 *
20 * @package    tool_health
21 * @copyright  2013 Marko Vidberg
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Given a list of categories, this function searches for ones
29 * that have a missing parent category.
30 *
31 * @param array $categories List of categories.
32 * @return array List of categories with missing parents.
33 */
34function tool_health_category_find_missing_parents($categories) {
35    $missingparent = array();
36
37    foreach ($categories as $category) {
38        if ($category->parent != 0 && !array_key_exists($category->parent, $categories)) {
39            $missingparent[$category->id] = $category;
40        }
41    }
42
43    return $missingparent;
44}
45
46/**
47 * Generates a list of categories with missing parents.
48 *
49 * @param array $missingparent List of categories with missing parents.
50 * @return string Bullet point list of categories with missing parents.
51 */
52function tool_health_category_list_missing_parents($missingparent) {
53    $description = '';
54
55    if (!empty($missingparent)) {
56        $description .= '<p>The following categories are missing their parents:</p><ul>';
57        foreach ($missingparent as $cat) {
58            $description .= "<li>Category $cat->id: " . s($cat->name) . "</li>\n";
59        }
60        $description .= "</ul>\n";
61    }
62
63    return $description;
64}
65
66/**
67 * Given a list of categories, this function searches for ones
68 * that have loops to previous parent categories.
69 *
70 * @param array $categories List of categories.
71 * @return array List of categories with loops.
72 */
73function tool_health_category_find_loops($categories) {
74    $loops = array();
75
76    while (!empty($categories)) {
77
78        $current = array_pop($categories);
79        $thisloop = array($current->id => $current);
80
81        while (true) {
82            if (isset($thisloop[$current->parent])) {
83                // Loop detected.
84                $loops = $loops + $thisloop;
85                break;
86            } else if ($current->parent === 0) {
87                // Top level.
88                break;
89            } else if (isset($loops[$current->parent])) {
90                // If the parent is in a loop we should also update this category.
91                $loops = $loops + $thisloop;
92                break;
93            } else if (!isset($categories[$current->parent])) {
94                // We already checked this category and is correct.
95                break;
96            } else {
97                // Continue following the path.
98                $current = $categories[$current->parent];
99                $thisloop[$current->id] = $current;
100                unset($categories[$current->id]);
101            }
102        }
103    }
104
105    return $loops;
106}
107
108/**
109 * Generates a list of categories with loops.
110 *
111 * @param array $loops List of categories with loops.
112 * @return string Bullet point list of categories with loops.
113 */
114function tool_health_category_list_loops($loops) {
115    $description = '';
116
117    if (!empty($loops)) {
118        $description .= '<p>The following categories form a loop of parents:</p><ul>';
119        foreach ($loops as $loop) {
120            $description .= "<li>\n";
121            $description .= "Category $loop->id: " . s($loop->name) . " has parent $loop->parent\n";
122            $description .= "</li>\n";
123        }
124        $description .= "</ul>\n";
125    }
126
127    return $description;
128}
Note: See TracBrowser for help on using the repository browser.