Skip to content

Commit

Permalink
MDL-49434 availability: Use renderables for multiple messages
Browse files Browse the repository at this point in the history
Co-Authored-By: Andrew Nicols <[email protected]>
  • Loading branch information
sammarshallou and andrewnicols committed Jul 15, 2015
1 parent 9325cd9 commit c7b7385
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 15 deletions.
14 changes: 12 additions & 2 deletions availability/classes/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,21 @@ public function get_user_list_sql($onlyactive) {
* that we can guarantee does not happen from within building the modinfo
* object.
*
* @param string $info Info string
* @param \renderable|string $inforenderable Info string or renderable
* @param int|\stdClass $courseorid
* @return string Correctly formatted info string
*/
public static function format_info($info, $courseorid) {
public static function format_info($inforenderable, $courseorid) {
global $PAGE;

// Use renderer if required.
if (is_string($inforenderable)) {
$info = $inforenderable;
} else {
$renderer = $PAGE->get_renderer('core', 'availability');
$info = $renderer->render($inforenderable);
}

// Don't waste time if there are no special tags.
if (strpos($info, '<AVAILABILITY_') === false) {
return $info;
Expand Down
70 changes: 70 additions & 0 deletions availability/classes/multiple_messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Represents multiple availability messages.
*
* These are messages like 'Not available until <date>'. This class includes
* multiple messages so that they can be rendered into a combined display, e.g.
* using bulleted lists.
*
* The tree structure of this object matches that of the availability
* restrictions.
*
* @package core_availability
* @copyright 2015 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Represents multiple availability messages.
*
* These are messages like 'Not available until <date>'. This class includes
* multiple messages so that they can be rendered into a combined display, e.g.
* using bulleted lists.
*
* The tree structure of this object matches that of the availability
* restrictions.
*
* @package core_availability
* @copyright 2015 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_availability_multiple_messages implements renderable {
/** @var bool True if this object represents the root of the tree */
public $root;
/** @var bool True if items use the AND operator (false = OR) */
public $andoperator;
/** @var bool True if this part of the tree is marked 'hide entirely' for non-matching users */
public $treehidden;
/** @var array Array of child items (may be string or this type) */
public $items;

/**
* Constructor.
*
* @param bool $root True if this object represents the root of the tree
* @param bool $andoperator True if items use the AND operator (false = OR)
* @param bool $treehidden True if this part of the tree is marked 'hide entirely' for non-matching users
* @param array $items Array of items (may be string or this type)
*/
public function __construct($root, $andoperator, $treehidden, array $items) {
$this->root = $root;
$this->andoperator = $andoperator;
$this->treehidden = $treehidden;
$this->items = $items;
}
}
6 changes: 2 additions & 4 deletions availability/classes/tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,10 @@ public function get_result_information(info $info, result $result) {
* @param result $result Result object if this is a student display, else null
* @param bool $root True if this is the root item
* @param bool $hidden Staff display; true if this tree has show=false (from parent)
* @return string|renderable Information to render
*/
protected function get_full_information_recursive(
$not, info $info, result $result = null, $root, $hidden = false) {
global $PAGE;

// Get list of children - either full list, or those which are shown.
$children = $this->children;
$staff = true;
Expand Down Expand Up @@ -550,8 +549,7 @@ protected function get_full_information_recursive(
}

// Format output for display.
$renderer = $PAGE->get_renderer('core', 'availability');
return $renderer->multiple_messages($root, $andoperator, $treehidden, $items);
return new \core_availability_multiple_messages($root, $andoperator, $treehidden, $items);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
@availability @availability_completion
Feature: Confirm that conditions on completion no longer cause a bug
In order to use completion conditions
As a teacher
I need it to not break when I set up certain conditions on some modules

Background:
Given the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "users" exist:
| username |
| teacher1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And the following config values are set as admin:
| enableavailability | 1 |
| enablecompletion | 1 |

@javascript
Scenario: Multiple completion conditions on glossary
# Set up course.
Given I log in as "teacher1"
And I follow "Course 1"
And I navigate to "Edit settings" node in "Course administration"
And I expand all fieldsets
And I set the field "Enable completion tracking" to "Yes"
And I press "Save and display"
And I turn editing mode on

# Add a couple of Pages with manual completion.
And I add a "Page" to section "1" and I fill the form with:
| Name | Page1 |
| Page content | x |
And I add a "Page" to section "1" and I fill the form with:
| Name | Page2 |
| Page content | x |

# Add a Glossary.
When I add a "Glossary" to section "1"
And I set the following fields to these values:
| Name | TestGlossary |
And I expand all fieldsets

# Add restrictions to the previous Pages being complete.
And I press "Add restriction..."
And I click on "Activity completion" "button" in the "Add restriction..." "dialogue"
And I set the field "Activity or resource" to "Page1"
And I press "Add restriction..."
And I click on "Activity completion" "button" in the "Add restriction..." "dialogue"
And I set the field with xpath "//div[@class='availability-item'][preceding-sibling::div]//select[@name='cm']" to "Page2"
And I press "Save and return to course"
And I should see "Not available unless:" in the ".activity.glossary" "css_element"
And I should see "The activity Page1 is marked complete" in the ".activity.glossary" "css_element"
And I should see "The activity Page2 is marked complete" in the ".activity.glossary" "css_element"
And I follow "TestGlossary"

# Behat will automatically check there is no error on this page.
Then I should see "TestGlossary"
21 changes: 12 additions & 9 deletions availability/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,25 @@ class core_availability_renderer extends plugin_renderer_base {
*
* This function will not be called unless there are at least two messages.
*
* @param bool $root True if this is a root-level list for an activity
* @param bool $andoperator True if the messages are being combined as AND
* @param bool $roothidden True if the root level should use 'hidden' message
* @param array $messages Messages to render
* @param core_availability_multiple_messages $renderable Multiple messages
* @return string Combined HTML
*/
public function multiple_messages($root, $andoperator, $roothidden, array $messages) {
public function render_core_availability_multiple_messages(
core_availability_multiple_messages $renderable) {
// Get initial message.
$out = get_string('list_' . ($root ? 'root_' : '') .
($andoperator ? 'and' : 'or') . ($roothidden ? '_hidden' : ''),
$out = get_string('list_' . ($renderable->root ? 'root_' : '') .
($renderable->andoperator ? 'and' : 'or') . ($renderable->treehidden ? '_hidden' : ''),
'availability');

// Make the list.
$out .= html_writer::start_tag('ul');
foreach ($messages as $message) {
$out .= html_writer::tag('li', $message);
foreach ($renderable->items as $item) {
if (is_string($item)) {
$str = $item;
} else {
$str = $this->render($item);
}
$out .= html_writer::tag('li', $str);
}
$out .= html_writer::end_tag('ul');
return $out;
Expand Down
6 changes: 6 additions & 0 deletions theme/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
This files describes API changes in /theme/* themes,
information provided here is intended especially for theme designer.

=== 3.0 ===

* The renderer function core_availability_renderer::multiple_messages was changed to
render_core_availability_multiple_messages, requiring a parameter of the new
\core_availability\multiple_messages type.

=== 2.9 ===

* Themes Bootstrapbase, Clean and More have undergone some changes for RTL layouts see - MDL-48160.
Expand Down

0 comments on commit c7b7385

Please sign in to comment.