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 mod_questionnaire\feedback;
18
 
19
use invalid_parameter_exception;
20
use coding_exception;
21
 
22
/**
23
 * Class for describing a feedback section's feedback definition.
24
 *
25
 * @package    mod_questionnaire
26
 * @copyright  2018 onward Mike Churchward (mike.churchward@poetopensource.org)
27
 * @author     Mike Churchward
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class sectionfeedback {
31
    /** @var int */
32
    public $id = 0;
33
    /** @var int */
34
    public $sectionid = 0;
35
    /** @var string */
36
    public $feedbacklabel = ''; // I don't think this is actually used?
37
    /** @var string */
38
    public $feedbacktext = '';
39
    /** @var string */
40
    public $feedbacktextformat = FORMAT_HTML;
41
    /** @var float */
42
    public $minscore = 0.0;
43
    /** @var float */
44
    public $maxscore = 0.0;
45
 
46
    /** The table name. */
47
    const TABLE = 'questionnaire_feedback';
48
 
49
    /**
50
     * Class constructor.
51
     * @param int $id
52
     * @param null|object $record
53
     */
54
    public function __construct($id = 0, $record = null) {
55
        // Return a new section based on the data id.
56
        if ($id != 0) {
57
            $record = $this->get_sectionfeedback($id);
58
            if (!$record) {
59
                throw new invalid_parameter_exception('No section feedback exists with that ID.');
60
            }
61
        }
62
        if (($id != 0) || is_object($record)) {
63
            $this->loadproperties($record);
64
        }
65
    }
66
 
67
    /**
68
     * Factory method to create a new sectionfeedback from the provided data and return an instance.
69
     * @param \stdClass $data
70
     * @return sectionfeedback
71
     */
72
    public static function new_sectionfeedback($data) {
73
        global $DB;
74
        $newsf = new self();
75
        $newsf->sectionid = $data->sectionid;
76
        $newsf->feedbacklabel = $data->feedbacklabel;
77
        $newsf->feedbacktext = $data->feedbacktext;
78
        $newsf->feedbacktextformat = $data->feedbacktextformat;
79
        $newsf->minscore = $data->minscore;
80
        $newsf->maxscore = $data->maxscore;
81
        $newsfid = $DB->insert_record(self::TABLE, $newsf);
82
        $newsf->id = $newsfid;
83
        return $newsf;
84
    }
85
 
86
    /**
87
     * Updates the data record with what is currently in the object instance.
88
     *
89
     * @throws \dml_exception
90
     * @throws coding_exception
91
     */
92
    public function update() {
93
        global $DB;
94
 
95
        $DB->update_record(self::TABLE, $this);
96
    }
97
 
98
    /**
99
     * Return the record specified by the id.
100
     * @param int $id
101
     * @return mixed
102
     */
103
    protected function get_sectionfeedback($id) {
104
        global $DB;
105
 
106
        return $DB->get_record(self::TABLE, ['id' => $id]);
107
    }
108
 
109
    /**
110
     * Load object properties from a provided record for any properties defined in that record.
111
     *
112
     * @param object $record
113
     */
114
    protected function loadproperties($record) {
115
        foreach ($this as $property => $value) {
116
            if (isset($record->$property)) {
117
                $this->$property = $record->$property;
118
            }
119
        }
120
    }
121
 
122
    /**
123
     * Get the data for this section's feedback from the database.
124
     *
125
     * @param int $id
126
     * @return mixed
127
     * @throws \dml_exception
128
     */
129
    protected function get_section($id) {
130
        global $DB;
131
 
132
        return $DB->get_record(self::TABLE, ['id' => $id]);
133
    }
134
}