MDL-52195 tool_lp: Normalise the navigation in templates
[moodle.git] / admin / tool / lp / classes / form / template.php
CommitLineData
d9a39950
DW
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 * This file contains the form add/update a competency framework.
19 *
20 * @package tool_lp
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace tool_lp\form;
26
27defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
28
29use moodleform;
30use tool_lp\api;
31
32require_once($CFG->libdir.'/formslib.php');
33
34/**
35 * Learning plan template form.
36 *
37 * @package tool_lp
38 * @copyright 2015 Damyon Wiese
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class template extends moodleform {
42
43 /**
44 * Define the form - called by parent constructor
45 */
46 public function definition() {
47 $mform = $this->_form;
47855def
FM
48 $template = $this->_customdata['template'];
49 if (empty($template)) {
50 $id = 0;
51 } else {
52 $id = $template->get_id();
53 }
f0da26a4 54 $context = $this->_customdata['context'];
d9a39950
DW
55
56 $mform->addElement('hidden', 'id');
57 $mform->setType('id', PARAM_INT);
58 $mform->setDefault('id', 0);
59
60 $mform->addElement('text', 'shortname',
61 get_string('shortname', 'tool_lp'));
62 $mform->setType('shortname', PARAM_TEXT);
63 $mform->addRule('shortname', null, 'required', null, 'client');
64 $mform->addElement('editor', 'description',
b17d3d10 65 get_string('description', 'tool_lp'), array('rows' => 4));
d9a39950 66 $mform->setType('description', PARAM_TEXT);
d9a39950
DW
67 $mform->addElement('selectyesno', 'visible',
68 get_string('visible', 'tool_lp'));
69 $mform->addElement('date_time_selector',
70 'duedate',
71 get_string('duedate', 'tool_lp'),
b17d3d10 72 array('optional' => true));
d9a39950
DW
73 $mform->addHelpButton('duedate', 'duedate', 'tool_lp');
74
75 $mform->setDefault('visible', true);
76 $mform->addHelpButton('visible', 'visible', 'tool_lp');
77
f0da26a4
FM
78 $mform->addElement('static', 'context', get_string('context', 'core_role'));
79 $mform->setDefault('context', $context->get_context_name());
80
d9a39950
DW
81 $this->add_action_buttons(true, get_string('savechanges', 'tool_lp'));
82
47855def
FM
83 if (!$this->is_submitted() && !empty($template)) {
84 $record = $template->to_record();
85 // Massage for editor API.
86 $record->description = array('text' => $record->description, 'format' => $record->descriptionformat);
87 $this->set_data($record);
d9a39950
DW
88 }
89
90 }
6420fbab
FM
91
92 /**
93 * Get form data.
94 * Conveniently removes non-desired properties.
95 * @return object
96 */
97 public function get_data() {
98 $data = parent::get_data();
99 if (is_object($data)) {
100 unset($data->submitbutton);
101 }
102 return $data;
103 }
104
0c65d52c
FM
105 /**
106 * Extra the form.
107 *
108 * @param array $data
109 * @param array $files
110 * @return array
111 */
112 public function validation($data, $files) {
113 $data = $this->get_submitted_data(); // To remove extra fields (sesskey, __qf_, ...).
114 unset($data->submitbutton);
115
116 $data->descriptionformat = $data->description['format'];
117 $data->description = $data->description['text'];
118
119 $template = new \tool_lp\template(0, $data);
120 $errors = $template->get_errors();
121
122 // The context ID is not submitted via this form.
123 if (isset($errors['contextid'])) {
124 unset($errors['contextid']);
125 }
126
127 return $errors;
128 }
129
d9a39950 130}