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 related to mark user complete.
|
|
|
19 |
*
|
|
|
20 |
* @package availability_coursecompleted
|
|
|
21 |
* @copyright 2021 iplusacademy (www.iplusacademy.org)
|
|
|
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 |
// @codeCoverageIgnoreStart
|
|
|
30 |
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
|
|
|
31 |
// @codeCoverageIgnoreEnd
|
|
|
32 |
|
|
|
33 |
use Behat\Mink\Exception\ElementNotFoundException;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Step definitions related to mark user complete.
|
|
|
37 |
*
|
|
|
38 |
* @package availability_coursecompleted
|
|
|
39 |
* @copyright 2021 iplusacademy (www.iplusacademy.org)
|
|
|
40 |
* @author Renaat Debleu <info@eWallah.net>
|
|
|
41 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
42 |
*/
|
|
|
43 |
class behat_availability_coursecompleted extends behat_base {
|
|
|
44 |
/**
|
|
|
45 |
* Complete user in a course
|
|
|
46 |
* @Then /^I mark course "(?P<course>[^"]*)" completed for user "(?P<user>[^"]*)"$/
|
|
|
47 |
* @param string $course
|
|
|
48 |
* @param string $user
|
|
|
49 |
*/
|
|
|
50 |
public function i_mark_course_completed_for_user($course, $user) {
|
|
|
51 |
$courseid = $this->get_course_id($course);
|
|
|
52 |
$userid = $this->get_user_id($user);
|
|
|
53 |
$ccompletion = new \completion_completion(['course' => $courseid, 'userid' => $userid]);
|
|
|
54 |
$ccompletion->mark_complete(time());
|
|
|
55 |
$task = new \core\task\completion_regular_task();
|
|
|
56 |
ob_start();
|
|
|
57 |
$task->execute();
|
|
|
58 |
ob_end_clean();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Fetch user ID from its username.
|
|
|
63 |
*
|
|
|
64 |
* @param string $username The username.
|
|
|
65 |
* @return int The user ID.
|
|
|
66 |
* @throws Exception
|
|
|
67 |
*/
|
|
|
68 |
protected function get_user_id($username) {
|
|
|
69 |
global $DB;
|
|
|
70 |
if (!$userid = $DB->get_field('user', 'id', ['username' => $username])) {
|
|
|
71 |
throw new Exception("A user with username '{$username}' does not exist");
|
|
|
72 |
}
|
|
|
73 |
return $userid;
|
|
|
74 |
}
|
|
|
75 |
}
|