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
 * Step definitions to add enrolment.
19
 *
20
 * @package   availability_relativedate
21
 * @copyright 2022 eWallah.net
22
 * @author    Renaat Debleu <info@eWallah.net>
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
// For that reason, we can't even rely on $CFG->admin being available here.
28
 
29
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
30
 
31
/**
32
 * Step definitions to add enrolment.
33
 *
34
 * @package   availability_relativedate
35
 * @copyright 2022 eWallah.net
36
 * @author    Renaat Debleu <info@eWallah.net>
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class behat_availability_relativedate extends behat_base {
40
    /**
41
     * See a relative date
42
     * @Then /^I should see relativedate "([^"]*)"$/
43
     * @param string $date
44
     */
45
    public function i_should_see_relativedate($date) {
46
        global $USER;
47
        $times = array_filter(explode('##', $date));
48
        $time = reset($times);
49
        $stime = userdate($time, get_string('strftimedate', 'langconfig'), $USER->timezone);
50
        $this->execute("behat_general::assert_element_contains_text", [$stime, '.course-content', 'css_element']);
51
    }
52
 
53
    /**
54
     * Add a self enrolment method starting
55
     * @Given /^selfenrolment exists in course "(?P<course>[^"]*)" starting "(?P<date>[^"]*)"$/
56
     * @param string $course
57
     * @param string $date
58
     */
59
    public function selfenrolment_exists_in_course_starting($course, $date) {
60
        $this->config_self_enrolment($course, $date, '');
61
    }
62
 
63
    /**
64
     * Add a self enrolment method ending
65
     * @Given /^selfenrolment exists in course "(?P<course>[^"]*)" ending "(?P<date>[^"]*)"$/
66
     * @param string $course
67
     * @param string $date
68
     */
69
    public function selfenrolment_exists_in_course_ending($course, $date) {
70
        $this->config_self_enrolment($course, '', $date);
71
    }
72
 
73
    /**
74
     * Make one activity available after another
75
     * @Given /^I make "(?P<activity2>[^"]*)" relative date depending on "(?P<activity1>[^"]*)"$/
76
     * @param string $activity1
77
     * @param string $activity2
78
     */
79
    public function i_make_activity_relative_date_depending_on($activity1, $activity2) {
80
        global $DB;
81
        $cm1 = $this->get_course_module_for_identifier($activity1);
82
        $cm2 = $this->get_course_module_for_identifier($activity2);
83
        if ($cm1 && $cm2) {
84
            $str = '{"op":"|","c":[{"type":"relativedate","n":1,"d":1,"s":7,"m":' . $cm1->id . '}],"show":true}';
85
            $DB->set_field('course_modules', 'availability', $str, ['id' => $cm2->id]);
86
        }
87
        $this->execute('behat_general::i_run_all_adhoc_tasks');
88
        core_courseformat\base::reset_course_cache(0);
89
        get_fast_modinfo(0, 0, true);
90
    }
91
 
92
    /**
93
     * Configure self enrolment
94
     * @param string $course
95
     * @param string $start
96
     * @param string $end
97
     */
98
    private function config_self_enrolment($course, $start, $end) {
99
        global $CFG, $DB;
100
        require_once($CFG->dirroot . '/enrol/self/lib.php');
101
        $courseid = $this->get_course_id($course);
102
        $selfplugin = enrol_get_plugin('self');
103
        $instance = $DB->get_record('enrol', ['courseid' => $courseid, 'enrol' => 'self'], '*', MUST_EXIST);
104
        $instance->customint6 = 1;
105
        $instance->enrolstartdate = $this->get_transformed_timestamp($start);
106
        $instance->enrolenddate = $this->get_transformed_timestamp($end);
107
        $DB->update_record('enrol', $instance);
108
        $selfplugin->update_status($instance, ENROL_INSTANCE_ENABLED);
109
    }
110
 
111
    /**
112
     * Return timestamp for the time passed.
113
     *
114
     * @param string $time time to convert
115
     * @return string
116
     */
117
    protected function get_transformed_timestamp($time) {
118
        if ($time === '') {
119
            return 0;
120
        }
121
        if (intval($time) > 0) {
122
            return $time;
123
        }
124
        $timepassed = array_filter(explode('##', $time));
125
        $first = reset($timepassed);
126
        $sfirst = strtotime($first);
127
        return ($sfirst == '') ? $first : $sfirst;
128
    }
129
}