Commit | Line | Data |
---|---|---|
c586d2bf MG |
1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | /** | |
19 | * @package gradingform | |
20 | * @subpackage rubric | |
21 | * @copyright 2011 Marina Glancy | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
25 | defined('MOODLE_INTERNAL') || die(); | |
26 | ||
27 | require_once("HTML/QuickForm/input.php"); | |
28 | ||
29 | // register file-related rules | |
30 | if (class_exists('HTML_QuickForm')) { | |
31 | HTML_QuickForm::registerRule('rubriceditorcompleted', 'callback', '_ruleIsCompleted', 'MoodleQuickForm_rubriceditor'); | |
32 | } | |
33 | ||
34 | class MoodleQuickForm_rubriceditor extends HTML_QuickForm_input { | |
35 | public $_helpbutton = ''; | |
36 | ||
37 | function MoodleQuickForm_rubriceditor($elementName=null, $elementLabel=null, $attributes=null) { | |
38 | parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes); | |
39 | } | |
40 | ||
41 | function getHelpButton() { | |
42 | return $this->_helpbutton; | |
43 | } | |
44 | ||
45 | function getElementTemplateType() { | |
46 | return 'default'; | |
47 | } | |
48 | ||
49 | function toHtml() { | |
50 | global $PAGE; | |
51 | $html = $this->_getTabs(); | |
ab156741 | 52 | $renderer = $PAGE->get_renderer('gradingform_rubric'); |
c586d2bf | 53 | if (!$this->_flagFrozen) { |
ab156741 MG |
54 | $mode = gradingform_rubric_controller::DISPLAY_EDIT_FULL; |
55 | $module = array('name'=>'gradingform_rubriceditor', 'fullpath'=>'/grade/grading/form/rubric/js/rubriceditor.js', | |
56 | 'strings' => array(array('confirmdeletecriterion', 'gradingform_rubric'), array('confirmdeletelevel', 'gradingform_rubric'), | |
57 | array('criterionempty', 'gradingform_rubric'), array('levelempty', 'gradingform_rubric') | |
58 | )); | |
59 | $PAGE->requires->js_init_call('M.gradingform_rubriceditor.init', array( | |
60 | array('name' => $this->getName(), | |
61 | 'criteriontemplate' => $renderer->criterion_template($mode, $this->getName()), | |
62 | 'leveltemplate' => $renderer->level_template($mode, $this->getName()) | |
63 | )), | |
64 | true, $module); | |
c586d2bf MG |
65 | } else { |
66 | if ($this->_persistantFreeze) { | |
ab156741 MG |
67 | $mode = gradingform_rubric_controller::DISPLAY_EDIT_FROZEN; |
68 | } else { | |
69 | $mode = gradingform_rubric_controller::DISPLAY_PREVIEW; | |
c586d2bf | 70 | } |
c586d2bf | 71 | } |
ab156741 | 72 | $html .= $renderer->display_rubric($this->prepare_non_js_data(), $mode, $this->getName()); |
c586d2bf MG |
73 | return $html; |
74 | } | |
75 | ||
c586d2bf MG |
76 | function prepare_non_js_data() { |
77 | $return = array(); | |
78 | $criteria = $this->getValue(); | |
79 | if (empty($criteria)) { | |
80 | $criteria = array(); | |
81 | } | |
82 | $lastaction = null; | |
83 | $lastid = null; | |
84 | foreach ($criteria as $id => $criterion) { | |
85 | if ($id == 'addcriterion') { | |
86 | $id = $this->get_next_id(array_keys($criteria)); | |
87 | $criterion = array('description' => ''); | |
88 | } | |
89 | $levels = array(); | |
90 | if (array_key_exists('levels', $criterion)) { | |
91 | foreach ($criterion['levels'] as $levelid => $level) { | |
92 | if ($levelid == 'addlevel') { | |
93 | $levelid = $this->get_next_id(array_keys($criterion['levels'])); | |
94 | $level = array( | |
95 | 'definition' => '', | |
96 | 'score' => 0, | |
97 | ); | |
98 | } | |
99 | if (!array_key_exists('delete', $level)) { | |
100 | $levels[$levelid] = $level; | |
101 | } | |
102 | } | |
103 | } | |
104 | $criterion['levels'] = $levels; | |
105 | if (array_key_exists('moveup', $criterion) || $lastaction == 'movedown') { | |
106 | unset($criterion['moveup']); | |
107 | if ($lastid !== null) { | |
108 | $lastcriterion = $return[$lastid]; | |
109 | unset($return[$lastid]); | |
110 | $return[$id] = $criterion; | |
111 | $return[$lastid] = $lastcriterion; | |
112 | } else { | |
113 | $return[$id] = $criterion; | |
114 | } | |
115 | $lastaction = null; | |
116 | $lastid = $id; | |
117 | } else if (array_key_exists('delete', $criterion)) { | |
118 | } else { | |
119 | if (array_key_exists('movedown', $criterion)) { | |
120 | unset($criterion['movedown']); | |
121 | $lastaction = 'movedown'; | |
122 | } | |
123 | $return[$id] = $criterion; | |
124 | $lastid = $id; | |
125 | } | |
126 | } | |
127 | $csortorder = 1; | |
128 | foreach (array_keys($return) as $id) { | |
129 | $return[$id]['sortorder'] = $csortorder++; | |
130 | } | |
131 | return $return; | |
132 | } | |
133 | ||
134 | function get_next_id($ids) { | |
135 | $maxid = 0; | |
136 | foreach ($ids as $id) { | |
137 | if (preg_match('/^NEWID(\d+)$/', $id, $matches) && ((int)$matches[1]) > $maxid) { | |
138 | $maxid = (int)$matches[1]; | |
139 | } | |
140 | } | |
141 | return 'NEWID'.($maxid+1); | |
142 | } | |
143 | ||
144 | function _ruleIsCompleted($elementValue) { | |
145 | //echo "_ruleIsCompleted"; | |
146 | if (is_array($elementValue)) { | |
147 | foreach ($elementValue as $criterionid => $criterion) { | |
148 | if ($criterionid == 'addcriterion') { | |
149 | return false; | |
150 | } | |
151 | if (array_key_exists('moveup', $criterion) || array_key_exists('movedown', $criterion) || array_key_exists('delete', $criterion)) { | |
152 | return false; | |
153 | } | |
154 | if (array_key_exists('levels', $criterion) && is_array($criterion['levels'])) { | |
155 | foreach ($criterion['levels'] as $levelid => $level) { | |
156 | if ($levelid == 'addlevel') { | |
157 | return false; | |
158 | } | |
159 | if (array_key_exists('delete', $level)) { | |
160 | return false; | |
161 | } | |
162 | } | |
163 | } | |
164 | } | |
165 | } | |
166 | //TODO check everything is filled | |
167 | //echo "<pre>";print_r($elementValue);echo "</pre>"; | |
168 | return true; | |
169 | } | |
170 | ||
171 | function onQuickFormEvent($event, $arg, &$caller) | |
172 | { | |
173 | $name = $this->getName(); | |
174 | if ($name && $caller->elementExists($name)) { | |
175 | $caller->addRule($name, '', 'rubriceditorcompleted'); | |
176 | } | |
177 | return parent::onQuickFormEvent($event, $arg, $caller); | |
178 | } | |
179 | ||
180 | } |