2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Class that holds a tree of availability conditions.
20 * @package core_availability
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_availability
;
27 defined('MOODLE_INTERNAL') ||
die();
30 * Class that holds a tree of availability conditions.
32 * The structure of this tree in JSON input data is:
36 * where 'op' is one of the OP_xx constants and 'c' is an array of children.
38 * At the root level one of the following additional values must be included:
42 * Boolean value controlling whether a failed match causes the item to
43 * display to students with information, or be completely hidden.
46 * Array of same length as c with booleans corresponding to each child; you
47 * can make it be hidden or shown depending on which one they fail. (Anything
48 * with false takes precedence.)
50 * @package core_availability
51 * @copyright 2014 The Open University
52 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
54 class tree
extends tree_node
{
55 /** @var int Operator: AND */
57 /** @var int Operator: OR */
59 /** @var int Operator: NOT(AND) */
60 const OP_NOT_AND
= '!&';
61 /** @var int Operator: NOT(OR) */
62 const OP_NOT_OR
= '!|';
64 /** @var bool True if this tree is at root level */
67 /** @var string Operator type (OP_xx constant) */
70 /** @var tree_node[] Children in this branch (may be empty array if needed) */
74 * Array of 'show information or hide completely' options for each child.
75 * This array is only set for the root tree if it is in AND or NOT OR mode,
76 * otherwise it is null.
80 protected $showchildren;
83 * Single 'show information or hide completely' option for tree. This option
84 * is only set for the root tree if it is in OR or NOT AND mode, otherwise
92 * Display a representation of this tree (used for debugging).
94 * @return string Text representation of tree
96 public function __toString() {
98 if ($this->root
&& is_null($this->showchildren
)) {
99 $result .= $this->show ?
'+' : '-';
101 $result .= $this->op
. '(';
103 foreach ($this->children
as $index => $child) {
109 if (!is_null($this->showchildren
)) {
110 $result .= $this->showchildren
[$index] ?
'+' : '-';
112 $result .= (string)$child;
119 * Decodes availability structure.
121 * This function also validates the retrieved data as follows:
122 * 1. Data that does not meet the API-defined structure causes a
123 * coding_exception (this should be impossible unless there is
124 * a system bug or somebody manually hacks the database).
125 * 2. Data that meets the structure but cannot be implemented (e.g.
126 * reference to missing plugin or to module that doesn't exist) is
127 * either silently discarded (if $lax is true) or causes a
128 * coding_exception (if $lax is false).
130 * @see decode_availability
131 * @param \stdClass $structure Structure (decoded from JSON)
132 * @param boolean $lax If true, throw exceptions only for invalid structure
133 * @param boolean $root If true, this is the root tree
134 * @return tree Availability tree
135 * @throws \coding_exception If data is not valid structure
137 public function __construct($structure, $lax = false, $root = true) {
141 if (!is_object($structure)) {
142 throw new \
coding_exception('Invalid availability structure (not object)');
146 if (!isset($structure->op
)) {
147 throw new \
coding_exception('Invalid availability structure (missing ->op)');
149 $this->op
= $structure->op
;
150 if (!in_array($this->op
, array(self
::OP_AND
, self
::OP_OR
,
151 self
::OP_NOT_AND
, self
::OP_NOT_OR
), true)) {
152 throw new \
coding_exception('Invalid availability structure (unknown ->op)');
155 // For root tree, get show options.
157 $this->showchildren
= null;
159 if ($this->op
=== self
::OP_AND ||
$this->op
=== self
::OP_NOT_OR
) {
160 // Per-child show options.
161 if (!isset($structure->showc
)) {
162 throw new \
coding_exception(
163 'Invalid availability structure (missing ->showc)');
165 if (!is_array($structure->showc
)) {
166 throw new \
coding_exception(
167 'Invalid availability structure (->showc not array)');
169 foreach ($structure->showc
as $value) {
170 if (!is_bool($value)) {
171 throw new \
coding_exception(
172 'Invalid availability structure (->showc value not bool)');
175 // Set it empty now - add corresponding ones later.
176 $this->showchildren
= array();
178 // Entire tree show option. (Note: This is because when you use
179 // OR mode, say you have A OR B, the user does not meet conditions
180 // for either A or B. A is set to 'show' and B is set to 'hide'.
181 // But they don't have either, so how do we know which one to do?
182 // There might as well be only one value.)
183 if (!isset($structure->show
)) {
184 throw new \
coding_exception(
185 'Invalid availability structure (missing ->show)');
187 if (!is_bool($structure->show
)) {
188 throw new \
coding_exception(
189 'Invalid availability structure (->show not bool)');
191 $this->show
= $structure->show
;
195 // Get list of enabled plugins.
196 $pluginmanager = \core_plugin_manager
::instance();
197 $enabled = $pluginmanager->get_enabled_plugins('availability');
199 // For unit tests, also allow the mock plugin type (even though it
200 // isn't configured in the code as a proper plugin).
202 $enabled['mock'] = true;
206 if (!isset($structure->c
)) {
207 throw new \
coding_exception('Invalid availability structure (missing ->c)');
209 if (!is_array($structure->c
)) {
210 throw new \
coding_exception('Invalid availability structure (->c not array)');
212 if (is_array($this->showchildren
) && count($structure->showc
) != count($structure->c
)) {
213 throw new \
coding_exception('Invalid availability structure (->c, ->showc mismatch)');
215 $this->children
= array();
216 foreach ($structure->c
as $index => $child) {
217 if (!is_object($child)) {
218 throw new \
coding_exception('Invalid availability structure (child not object)');
221 // First see if it's a condition. These have a defined type.
222 if (isset($child->type
)) {
223 // Look for a plugin of this type.
224 $classname = '\availability_' . $child->type
. '\condition';
225 if (!array_key_exists($child->type
, $enabled)) {
227 // On load of existing settings, ignore if class
231 throw new \
coding_exception('Unknown condition type: ' . $child->type
);
234 $this->children
[] = new $classname($child);
236 // Not a condition. Must be a subtree.
237 $this->children
[] = new tree($child, $lax, false);
239 if (!is_null($this->showchildren
)) {
240 $this->showchildren
[] = $structure->showc
[$index];
245 public function check_available($not, info
$info, $grabthelot, $userid) {
246 // If there are no children in this group, we just treat it as available.
248 if (!$this->children
) {
249 return new result(true);
252 // Get logic flags from operator.
253 list($innernot, $andoperator) = $this->get_logic_flags($not);
260 $failedchildren = array();
261 $totallyhide = !$this->show
;
262 foreach ($this->children
as $index => $child) {
263 // Check available and get info.
264 $childresult = $child->check_available(
265 $innernot, $info, $grabthelot, $userid);
266 $childyes = $childresult->is_available();
268 $failedchildren[] = $childresult;
269 if (!is_null($this->showchildren
) && !$this->showchildren
[$index]) {
274 if ($andoperator && !$childyes) {
276 // Do not exit loop at this point, as we will still include other info.
277 } else if (!$andoperator && $childyes) {
278 // Exit loop since we are going to allow access (from this tree at least).
285 return new result(true);
286 } else if ($totallyhide) {
287 return new result(false);
289 return new result(false, $this, $failedchildren);
293 public function is_applied_to_user_lists() {
298 * Tests against a user list. Users who cannot access the activity due to
299 * availability restrictions will be removed from the list.
301 * This test ONLY includes conditions which are marked as being applied to
302 * user lists. For example, group conditions are included but date
303 * conditions are not included.
305 * The function operates reasonably efficiently i.e. should not do per-user
306 * database queries. It is however likely to be fairly slow.
308 * @param array $users Array of userid => object
309 * @param bool $not If tree's parent indicates it's being checked negatively
310 * @param info $info Info about current context
311 * @param capability_checker $checker Capability checker
312 * @return array Filtered version of input array
314 public function filter_user_list(array $users, $not, info
$info,
315 capability_checker
$checker) {
316 // Get logic flags from operator.
317 list($innernot, $andoperator) = $this->get_logic_flags($not);
320 // For AND, start with the whole result and whittle it down.
323 // For OR, start with nothing.
325 $anyconditions = false;
328 // Loop through all valid children.
329 foreach ($this->children
as $index => $child) {
330 if (!$child->is_applied_to_user_lists()) {
334 // OR condition with one option that doesn't restrict user
335 // lists = everyone is allowed.
336 $anyconditions = false;
340 $childresult = $child->filter_user_list($users, $innernot, $info, $checker);
342 $result = array_intersect_key($result, $childresult);
344 // Combine results into array.
345 foreach ($childresult as $id => $user) {
346 $result[$id] = $user;
348 $anyconditions = true;
352 // For OR operator, if there were no conditions just return input.
353 if (!$andoperator && !$anyconditions) {
360 public function get_user_list_sql($not, info
$info, $onlyactive) {
362 // Get logic flags from operator.
363 list($innernot, $andoperator) = $this->get_logic_flags($not);
365 // Loop through all valid children, getting SQL for each.
366 $childresults = array();
367 foreach ($this->children
as $index => $child) {
368 if (!$child->is_applied_to_user_lists()) {
372 // OR condition with one option that doesn't restrict user
373 // lists = everyone is allowed.
374 $childresults = array();
378 $childresult = $child->get_user_list_sql($innernot, $info, $onlyactive);
379 if ($childresult[0]) {
380 $childresults[] = $childresult;
381 } else if (!$andoperator) {
382 // When using OR operator, if any part doesn't have restrictions,
383 // then nor does the whole thing.
384 return array('', array());
388 // If there are no conditions, return null.
389 if (!$childresults) {
390 return array('', array());
392 // If there is a single condition, return it.
393 if (count($childresults) === 1) {
394 return $childresults[0];
397 // Combine results using INTERSECT or UNION.
400 $outparams = array();
401 foreach ($childresults as $childresult) {
402 $subsql[] = $childresult[0];
403 $outparams = array_merge($outparams, $childresult[1]);
406 $outsql = $DB->sql_intersect($subsql, 'id');
408 $outsql = '(' . join(') UNION (', $subsql) . ')';
410 return array($outsql, $outparams);
413 public function is_available_for_all($not = false) {
415 list($innernot, $andoperator) = $this->get_logic_flags($not);
417 // No children = always available.
418 if (!$this->children
) {
423 foreach ($this->children
as $child) {
424 $innerall = $child->is_available_for_all($innernot);
426 // When there is an AND operator, then any child that results
427 // in unavailable status would cause the whole thing to be
433 // When there is an OR operator, then any child which must only
434 // be available means the whole thing must be available.
441 // If we get to here then for an AND operator that means everything must
442 // be available. From OR it means that everything must be possibly
448 * Gets full information about this tree (including all children) as HTML
449 * for display to staff.
451 * @param info $info Information about location of condition tree
452 * @throws \coding_exception If you call on a non-root tree
453 * @return string HTML data (empty string if none)
455 public function get_full_information(info
$info) {
457 throw new \
coding_exception('Only supported on root item');
459 return $this->get_full_information_recursive(false, $info, null, true);
463 * Gets information about this tree corresponding to the given result
464 * object. (In other words, only conditions which the student actually
465 * fails will be shown - and nothing if display is turned off.)
467 * @param info $info Information about location of condition tree
468 * @param result $result Result object
469 * @throws \coding_exception If you call on a non-root tree
470 * @return string HTML data (empty string if none)
472 public function get_result_information(info
$info, result
$result) {
474 throw new \
coding_exception('Only supported on root item');
476 return $this->get_full_information_recursive(false, $info, $result, true);
480 * Gets information about this tree (including all or selected children) as
481 * HTML for display to staff or student.
483 * @param bool $not True if there is a NOT in effect
484 * @param info $info Information about location of condition tree
485 * @param result $result Result object if this is a student display, else null
486 * @param bool $root True if this is the root item
487 * @param bool $hidden Staff display; true if this tree has show=false (from parent)
488 * @return string|renderable Information to render
490 protected function get_full_information_recursive(
491 $not, info
$info, result
$result = null, $root, $hidden = false) {
492 // Get list of children - either full list, or those which are shown.
493 $children = $this->children
;
496 $children = $result->filter_nodes($children);
500 // If no children, return empty string.
505 list($innernot, $andoperator) = $this->get_logic_flags($not);
507 // If there is only one child, don't bother displaying this tree
508 // (AND and OR makes no difference). Recurse to the child if a tree,
509 // otherwise display directly.
510 if (count ($children) === 1) {
511 $child = reset($children);
512 if ($this->root
&& is_null($result)) {
513 if (is_null($this->showchildren
)) {
514 $childhidden = !$this->show
;
516 $childhidden = !$this->showchildren
[0];
519 $childhidden = $hidden;
521 if ($child instanceof tree
) {
522 return $child->get_full_information_recursive(
523 $innernot, $info, $result, $root, $childhidden);
526 $result = $child->get_standalone_description($staff, $innernot, $info);
528 $result = $child->get_description($staff, $innernot, $info);
531 $result .= ' ' . get_string('hidden_marker', 'availability');
537 // Multiple children, so prepare child messages (recursive).
540 foreach ($children as $child) {
541 // Work out if this node is hidden (staff view only).
542 $childhidden = $this->root
&& is_null($result) &&
543 !is_null($this->showchildren
) && !$this->showchildren
[$index];
544 if ($child instanceof tree
) {
545 $items[] = $child->get_full_information_recursive(
546 $innernot, $info, $result, false, $childhidden);
548 $childdescription = $child->get_description($staff, $innernot, $info);
550 $childdescription .= ' ' . get_string('hidden_marker', 'availability');
552 $items[] = $childdescription;
557 // If showing output to staff, and root is set to hide completely,
558 // then include this information in the message.
560 $treehidden = !$this->show
&& is_null($result);
562 $treehidden = $hidden;
565 // Format output for display.
566 return new \
core_availability_multiple_messages($root, $andoperator, $treehidden, $items);
570 * Converts the operator for the tree into two flags used for computing
573 * The 2 flags are $innernot (whether to set $not when calling for children)
574 * and $andoperator (whether to use AND or OR operator to combine children).
576 * @param bool $not Not flag passed to this tree
577 * @return array Array of the 2 flags ($innernot, $andoperator)
579 public function get_logic_flags($not) {
580 // Work out which type of logic to use for the group.
586 case self
::OP_NOT_AND
:
587 case self
::OP_NOT_OR
:
591 throw new \
coding_exception('Unknown operator');
595 case self
::OP_NOT_AND
:
599 case self
::OP_NOT_OR
:
600 $andoperator = false;
603 throw new \
coding_exception('Unknown operator');
606 // Select NOT (or not) for children. It flips if this is a 'not' group.
607 $innernot = $negative ?
!$not : $not;
609 // Select operator to use for this group. If flips for negative, because:
610 // NOT (a AND b) = (NOT a) OR (NOT b)
611 // NOT (a OR b) = (NOT a) AND (NOT b).
613 $andoperator = !$andoperator;
615 return array($innernot, $andoperator);
618 public function save() {
619 $result = new \
stdClass();
620 $result->op
= $this->op
;
621 // Only root tree has the 'show' options.
623 if ($this->op
=== self
::OP_AND ||
$this->op
=== self
::OP_NOT_OR
) {
624 $result->showc
= $this->showchildren
;
626 $result->show
= $this->show
;
629 $result->c
= array();
630 foreach ($this->children
as $child) {
631 $result->c
[] = $child->save();
637 * Checks whether this tree is empty (contains no children).
639 * @return boolean True if empty
641 public function is_empty() {
642 return count($this->children
) === 0;
646 * Recursively gets all children of a particular class (you can use a base
647 * class to get all conditions, or a specific class).
649 * @param string $classname Full class name e.g. core_availability\condition
650 * @return array Array of nodes of that type (flattened, not a tree any more)
652 public function get_all_children($classname) {
654 $this->recursive_get_all_children($classname, $result);
659 * Internal function that implements get_all_children efficiently.
661 * @param string $classname Full class name e.g. core_availability\condition
662 * @param array $result Output array of nodes
664 protected function recursive_get_all_children($classname, array &$result) {
665 foreach ($this->children
as $child) {
666 if (is_a($child, $classname)) {
669 if ($child instanceof tree
) {
670 $child->recursive_get_all_children($classname, $result);
675 public function update_after_restore($restoreid, $courseid,
676 \base_logger
$logger, $name) {
678 foreach ($this->children
as $index => $child) {
679 if ($child->include_after_restore($restoreid, $courseid, $logger, $name,
680 info
::get_restore_task($restoreid))) {
681 $thischanged = $child->update_after_restore($restoreid, $courseid,
683 $changed = $changed ||
$thischanged;
685 unset($this->children
[$index]);
686 unset($this->showchildren
[$index]);
687 $this->showchildren
= array_values($this->showchildren
);
694 public function update_dependency_id($table, $oldid, $newid) {
696 foreach ($this->children
as $child) {
697 $thischanged = $child->update_dependency_id($table, $oldid, $newid);
698 $changed = $changed ||
$thischanged;
704 * Returns a JSON object which corresponds to a tree.
706 * Intended for unit testing, as normally the JSON values are constructed
707 * by JavaScript code.
709 * This function generates 'nested' (i.e. not root-level) trees.
711 * @param array $children Array of JSON objects from component children
712 * @param string $op Operator (tree::OP_xx)
713 * @return stdClass JSON object
714 * @throws coding_exception If you get parameters wrong
716 public static function get_nested_json(array $children, $op = self
::OP_AND
) {
718 // Check $op and work out its type.
721 case self
::OP_NOT_OR
:
723 case self
::OP_NOT_AND
:
726 throw new \
coding_exception('Invalid $op');
730 $result = new \
stdClass();
732 $result->c
= $children;
737 * Returns a JSON object which corresponds to a tree at root level.
739 * Intended for unit testing, as normally the JSON values are constructed
740 * by JavaScript code.
742 * The $show parameter can be a boolean for all OP_xx options. For OP_AND
743 * and OP_NOT_OR where you have individual show options, you can specify
744 * a boolean (same for all) or an array.
746 * @param array $children Array of JSON objects from component children
747 * @param string $op Operator (tree::OP_xx)
748 * @param bool|array $show Whether 'show' option is turned on (see above)
749 * @return stdClass JSON object ready for encoding
750 * @throws coding_exception If you get parameters wrong
752 public static function get_root_json(array $children, $op = self
::OP_AND
, $show = true) {
754 // Get the basic object.
755 $result = self
::get_nested_json($children, $op);
760 case self
::OP_NOT_OR
:
764 case self
::OP_NOT_AND
:
769 // Add show options depending on operator.
771 if (is_bool($show)) {
772 $result->showc
= array_pad(array(), count($result->c
), $show);
773 } else if (is_array($show)) {
774 // The JSON will break if anything isn't an actual bool, so check.
775 foreach ($show as $item) {
776 if (!is_bool($item)) {
777 throw new \
coding_exception('$show array members must be bool');
780 // Check the size matches.
781 if (count($show) != count($result->c
)) {
782 throw new \
coding_exception('$show array size does not match $children');
784 $result->showc
= $show;
786 throw new \
coding_exception('$show must be bool or array');
789 if (!is_bool($show)) {
790 throw new \
coding_exception('For this operator, $show must be bool');
792 $result->show
= $show;