Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Availability form field class.
19
 *
20
 * @package core_form
21
 * @category test
22
 * @copyright 2014 The Open University
23
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
 
28
require_once(__DIR__ . '/behat_form_textarea.php');
29
 
30
/**
31
 * Availability form field class.
32
 *
33
 * @package core_form
34
 * @category test
35
 * @copyright 2014 The Open University
36
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class behat_form_availability extends behat_form_textarea {
39
 
40
    /**
41
     * Sets the value(s) of an availability element.
42
     *
43
     * At present this only supports the following value 'Grouping: xxx' where
44
     * xxx is the name of a grouping. Additional value types can be added as
45
     * necessary.
46
     *
47
     * @param string $value Value code
48
     * @return void
49
     */
50
    public function set_value($value) {
51
        global $DB;
52
        $driver = $this->session->getDriver();
53
 
54
        // Check the availability condition is currently unset - we don't yet
55
        // support changing an existing one.
56
        $existing = $this->get_value();
57
        if ($existing && $existing !== '{"op":"&","c":[],"showc":[]}') {
58
            throw new Exception('Cannot automatically set availability when ' .
59
                    'there is existing setting - must clear manually');
60
        }
61
 
62
        // Check the value matches a supported format.
63
        $matches = array();
64
        if (!preg_match('~^\s*([^:]*):\s*(.*?)\s*$~', $value, $matches)) {
65
            throw new Exception('Value for availability field does not match correct ' .
66
                    'format. Example: "Grouping: G1"');
67
        }
68
        $type = $matches[1];
69
        $param = $matches[2];
70
 
71
        if ($this->running_javascript()) {
72
            switch (strtolower($type)) {
73
                case 'grouping' :
74
                    // Set a grouping condition.
75
                    $driver->click('//div[@class="availability-button"]/button');
76
                    $driver->click('//button[@id="availability_addrestriction_grouping"]');
77
                    $escparam = behat_context_helper::escape($param);
78
                    $nodes = $driver->find(
79
                            '//span[contains(concat(" " , @class, " "), " availability_grouping ")]//' .
80
                            'option[normalize-space(.) = ' . $escparam . ']');
81
                    if (count($nodes) != 1) {
82
                        throw new Exception('Cannot find grouping in dropdown' . count($nodes));
83
                    }
84
                    $node = reset($nodes);
85
                    $value = $node->getValue();
86
                    $driver->selectOption(
87
                            '//span[contains(concat(" " , @class, " "), " availability_grouping ")]//' .
88
                            'select', $value);
89
                    break;
90
 
91
                default:
92
                    // We don't support other types yet. The test author must write
93
                    // manual 'click on that button, etc' commands.
94
                    throw new Exception('The availability type "' . $type .
95
                            '" is currently not supported - must set manually');
96
            }
97
        } else {
98
            $courseid = $driver->getValue('//input[@name="course"]');
99
            switch (strtolower($type)) {
100
                case 'grouping' :
101
                    // Define result with one grouping condition.
102
                    $groupingid = $DB->get_field('groupings', 'id',
103
                            array('courseid' => $courseid, 'name' => $param));
104
                    $json = \core_availability\tree::get_root_json(array(
105
                            \availability_grouping\condition::get_json($groupingid)));
106
                    break;
107
 
108
                default:
109
                    // We don't support other types yet.
110
                    throw new Exception('The availability type "' . $type .
111
                            '" is currently not supported - must set with JavaScript');
112
            }
113
            $driver->setValue('//textarea[@name="availabilityconditionsjson"]',
114
                    json_encode($json));
115
        }
116
    }
117
}