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 * Generator for the gradingforum_rubric plugin.
19 *
20 * @package    gradingform_rubric
21 * @category   test
22 * @copyright  2018 Adrian Greeve <adriangreeve.com>
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace tests\gradingform_rubric\generator;
27
28/**
29 * Convenience class to create rubric criterion.
30 *
31 * @package    gradingform_rubric
32 * @copyright  2018 Adrian Greeve <adriangreeve.com>
33 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class criterion {
36    /** @var string $description A description of the criterion. */
37    public $description;
38
39    /** @var integer $sortorder sort order of the criterion. */
40    public $sortorder = 0;
41
42    /** @var array $levels The levels for this criterion. */
43    public $levels = [];
44
45    /**
46     * Constructor for this test_criterion object
47     *
48     * @param string $description A description of this criterion.
49     * @param array $levels
50     */
51    public function __construct(string $description, array $levels = []) {
52        $this->description = $description;
53        foreach ($levels as $definition => $score) {
54            $this->add_level($definition, $score);
55        }
56    }
57
58    /**
59     * Adds levels to the criterion.
60     *
61     * @param string $definition The definition for this level.
62     * @param int $score The score received if this level is selected.
63     * @return self
64     */
65    public function add_level(string $definition, int $score): self {
66        $this->levels[] = [
67            'definition' => $definition,
68            'score' => $score
69        ];
70
71        return $this;
72    }
73
74    /**
75     * Get the description for this criterion.
76     *
77     * @return string
78     */
79    public function get_description(): string {
80        return $this->description;
81    }
82
83    /**
84     * Get the levels for this criterion.
85     *
86     * @return array
87     */
88    public function get_levels(): array {
89        return $this->levels;
90    }
91
92    /**
93     * Get all values in an array for use when creating a new guide.
94     *
95     * @param int $sortorder
96     * @return array
97     */
98    public function get_all_values(int $sortorder): array {
99        return [
100            'sortorder' => $sortorder,
101            'description' => $this->get_description(),
102            'levels' => $this->get_all_level_values(),
103        ];
104    }
105
106    /**
107     * Get all level values.
108     *
109     * @return array
110     */
111    public function get_all_level_values(): array {
112        $result = [];
113
114        foreach ($this->get_levels() as $index => $level) {
115            $id = $index + 1;
116            $result["NEWID{$id}"] = $level;
117        }
118
119        return $result;
120    }
121}
122