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
namespace core\moodlenet;
18
 
19
use backup_activity_task;
20
use backup_controller;
21
use stdClass;
22
use stored_file;
23
 
24
/**
25
 * Packager to prepare appropriate backup of a number of activities in a course to share to MoodleNet.
26
 *
27
 * @package    core
28
 * @copyright  2023 Huong Nguyen <huongnv13@gmail.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class course_partial_packager extends course_packager {
32
 
33
    /**
34
     * @var int[] $cmids List of course module ids of selected activities.
35
     */
36
    protected array $cmids;
37
 
38
    /**
39
     * Constructor for course partial packager.
40
     *
41
     * @param stdClass $course The course to package
42
     * @param array $cmids List of course module id of selected activities.
43
     * @param int $userid The ID of the user performing the packaging
44
     */
45
    public function __construct(
46
        stdClass $course,
47
        array $cmids,
48
        int $userid,
49
    ) {
50
        $this->cmids = $cmids;
51
        parent::__construct($course, $userid);
52
    }
53
 
54
    /**
55
     * Package the resource identified by resource id into a new stored_file.
56
     *
57
     * @param backup_controller $controller The backup controller.
58
     * @return stored_file
59
     */
60
    protected function package(backup_controller $controller): stored_file {
61
        $this->remove_unselected_activities($controller);
62
        return parent::package($controller);
63
    }
64
 
65
    /**
66
     * Remove unselected activities in the course backup.
67
     *
68
     * @param backup_controller $controller The backup controller.
69
     */
70
    protected function remove_unselected_activities(backup_controller $controller): void {
71
        foreach ($this->get_all_activity_tasks($controller) as $task) {
72
            foreach ($task->get_settings() as $setting) {
73
                if (in_array($task->get_moduleid(), $this->cmids) &&
74
                    str_contains($setting->get_name(), '_included') !== false) {
75
                    $setting->set_value(1);
76
                } else {
77
                    $setting->set_value(0);
78
                }
79
            }
80
        }
81
    }
82
 
83
    /**
84
     * Get all the activity tasks in the controller.
85
     *
86
     * @param backup_controller $controller The backup controller.
87
     * @return backup_activity_task[] Array of activity tasks.
88
     */
89
    protected function get_all_activity_tasks(backup_controller $controller): array {
90
        $tasks = [];
91
        foreach ($controller->get_plan()->get_tasks() as $task) {
92
            if (! $task instanceof backup_activity_task) { // Only for activity tasks.
93
                continue;
94
            }
95
            $tasks[] = $task;
96
        }
97
        return $tasks;
98
    }
99
}