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 * Class represents the result of an availability check for the user.
19 *
20 * @package core_availability
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_availability;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Class represents the result of an availability check for the user.
31 *
32 * You can pass an object of this class to tree::get_result_information to
33 * display suitable student information about the result.
34 *
35 * @package core_availability
36 * @copyright 2014 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class result {
40    /** @var bool True if the item is available */
41    protected $available;
42
43    /** @var tree_node[] Array of nodes to display in failure information (node=>node). */
44    protected $shownodes = array();
45
46    /**
47     * Constructs result.
48     *
49     * @param bool $available True if available
50     * @param tree_node $node Node if failed & should be displayed
51     * @param result[] $failedchildren Array of children who failed too
52     */
53    public function __construct($available, tree_node $node = null,
54            array $failedchildren = array()) {
55        $this->available = $available;
56        if (!$available) {
57            if ($node) {
58                $this->shownodes[spl_object_hash($node)] = $node;
59            }
60            foreach ($failedchildren as $child) {
61                foreach ($child->shownodes as $key => $node) {
62                    $this->shownodes[$key] = $node;
63                }
64            }
65        }
66    }
67
68    /**
69     * Checks if the result was a yes.
70     *
71     * @return bool True if the activity is available
72     */
73    public function is_available() {
74        return $this->available;
75    }
76
77    /**
78     * Filters the provided array so that it only includes nodes which are
79     * supposed to be displayed in the result output. (I.e. those for which
80     * the user failed the test, and which are not set to totally hide
81     * output.)
82     *
83     * @param tree_node[] $array Input array of nodes
84     * @return array Output array containing only those nodes set for display
85     */
86    public function filter_nodes(array $array) {
87        $out = array();
88        foreach ($array as $key => $node) {
89            if (array_key_exists(spl_object_hash($node), $this->shownodes)) {
90                $out[$key] = $node;
91            }
92        }
93        return $out;
94    }
95}
96