Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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\router\parameters;
18
 
19
use core\exception\not_found_exception;
20
use core\param;
21
use core\router\schema\example;
22
use core\router\schema\parameters\mapped_property_parameter;
23
use core\router\schema\referenced_object;
24
use Psr\Http\Message\ServerRequestInterface;
25
 
26
/**
27
 * A Moodle parameter referenced in the path.
28
 *
29
 * @package    core
30
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class path_course extends \core\router\schema\parameters\path_parameter implements
34
    mapped_property_parameter,
35
    referenced_object
36
{
37
    /**
38
     * Create a new path_course parameter.
39
     *
40
     * @param string $name The name of the parameter to use for the course identifier
41
     * @param mixed ...$extra Additional arguments
42
     */
43
    public function __construct(
44
        string $name = 'course',
45
        ...$extra,
46
    ) {
47
        $extra['name'] = $name;
48
        $extra['type'] = param::RAW;
49
        $extra['description'] = <<<EOF
50
        The course identifier.
51
 
52
        This can be the id of the course, the idnumber of the course, or the shortname of the course.
53
 
54
        If specifying a course idnumber, the value should be in the format `idnumber:[idnumber]`.
55
 
56
        If specifying a course shortname, the value should be in the format `name:[shortname]`.
57
        EOF;
58
        $extra['examples'] = [
59
            new example(
60
                name: 'A course id',
61
                value: 54,
62
            ),
63
            new example(
64
                name: 'A course specified by its idnumber',
65
                value: 'idnumber:000117-physics-101-1',
66
            ),
67
            new example(
68
                name: 'A course specified by its shortname',
69
                value: 'name:000117-phys101-0',
70
            ),
71
        ];
72
 
73
        parent::__construct(...$extra);
74
    }
75
 
76
    /**
77
     * Get the course object for the given identifier.
78
     *
79
     * @param string $value A course id, idnumber, or shortname
80
     * @return object
81
     * @throws not_found_exception If the course cannot be found
82
     */
83
    protected function get_course_for_value(string $value): mixed {
84
        global $DB;
85
 
86
        $data = false;
87
 
88
        if (is_numeric($value)) {
89
            $data = $DB->get_record('course', [
90
                'id' => $value,
91
            ]);
92
        } else if (str_starts_with($value, 'idnumber:')) {
93
            $data = $DB->get_record('course', [
94
                'idnumber' => substr($value, strlen('idnumber:')),
95
            ]);
96
        } else if (str_starts_with($value, 'name:')) {
97
            $data = $DB->get_record('course', [
98
                'shortname' => substr($value, strlen('name:')),
99
            ]);
100
        }
101
 
102
        if ($data) {
103
            return $data;
104
        }
105
 
106
        throw new not_found_exception('course', $value);
107
    }
108
 
109
    #[\Override]
110
    public function add_attributes_for_parameter_value(
111
        ServerRequestInterface $request,
112
        string $value,
113
    ): ServerRequestInterface {
114
        $course = $this->get_course_for_value($value);
115
 
116
        return $request
117
            ->withAttribute($this->name, $course)
118
            ->withAttribute("{$this->name}context", \core\context\course::instance($course->id));
119
    }
120
 
121
    #[\Override]
122
    public function get_schema_from_type(param $type): \stdClass {
123
        $schema = parent::get_schema_from_type($type);
124
 
125
        $schema->pattern = "^(";
126
        $schema->pattern .= implode("|", [
127
            '\d+',
128
            'idnumber:.+',
129
            'name:.+',
130
        ]);
131
        $schema->pattern .= ")$";
132
 
133
        return $schema;
134
    }
135
}