MDL-27541 course reports - allow teachers to view individual reports
[moodle.git] / lib / xmldb / xmldb_key.php
blob6a1a132e9c1132b54f590205e006731a38aaa88c
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12 // //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
17 // //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class represent one XMLDB Key
29 class xmldb_key extends xmldb_object {
31 var $type;
32 var $fields;
33 var $reftable;
34 var $reffields;
36 /**
37 * Creates one new xmldb_key
39 function __construct($name, $type=null, $fields=array(), $reftable=null, $reffields=null) {
40 $this->type = NULL;
41 $this->fields = array();
42 $this->reftable = NULL;
43 $this->reffields = array();
44 parent::__construct($name);
45 $this->set_attributes($type, $fields, $reftable, $reffields);
48 /// TODO: Delete for 2.1 (deprecated in 2.0).
49 /// Deprecated API starts here
51 function setAttributes($type, $fields, $reftable=null, $reffields=null) {
53 debugging('XMLDBKey->setAttributes() has been deprecated in Moodle 2.0. Will be out in Moodle 2.1. Please use xmldb_key->set_attributes() instead.', DEBUG_DEVELOPER);
55 return $this->set_attributes($type, $fields, $reftable, $reffields);
57 /// Deprecated API ends here
59 /**
60 * Set all the attributes of one xmldb_key
62 * @param string type XMLDB_KEY_PRIMARY, XMLDB_KEY_UNIQUE, XMLDB_KEY_FOREIGN
63 * @param array fields an array of fieldnames to build the key over
64 * @param string reftable name of the table the FK points to or null
65 * @param array reffields an array of fieldnames in the FK table or null
67 function set_attributes($type, $fields, $reftable=null, $reffields=null) {
68 $this->type = $type;
69 $this->fields = $fields;
70 $this->reftable = $reftable;
71 $this->reffields = empty($reffields) ? array() : $reffields;
74 /**
75 * Get the key type
77 function getType() {
78 return $this->type;
81 /**
82 * Set the key type
84 function setType($type) {
85 $this->type = $type;
88 /**
89 * Set the key fields
91 function setFields($fields) {
92 $this->fields = $fields;
95 /**
96 * Set the key reftable
98 function setRefTable($reftable) {
99 $this->reftable = $reftable;
103 * Set the key reffields
105 function setRefFields($reffields) {
106 $this->reffields = $reffields;
110 * Get the key fields
112 function &getFields() {
113 return $this->fields;
117 * Get the key reftable
119 function &getRefTable() {
120 return $this->reftable;
124 * Get the key reffields
126 function &getRefFields() {
127 return $this->reffields;
131 * Load data from XML to the key
133 function arr2xmldb_key($xmlarr) {
135 $result = true;
137 /// Debug the table
138 /// traverse_xmlize($xmlarr); //Debug
139 /// print_object ($GLOBALS['traverse_array']); //Debug
140 /// $GLOBALS['traverse_array']=""; //Debug
142 /// Process key attributes (name, type, fields, reftable,
143 /// reffields, comment, previous, next)
144 if (isset($xmlarr['@']['NAME'])) {
145 $this->name = trim($xmlarr['@']['NAME']);
146 } else {
147 $this->errormsg = 'Missing NAME attribute';
148 $this->debug($this->errormsg);
149 $result = false;
152 if (isset($xmlarr['@']['TYPE'])) {
153 /// Check for valid type
154 $type = $this->getXMLDBKeyType(trim($xmlarr['@']['TYPE']));
155 if ($type) {
156 $this->type = $type;
157 } else {
158 $this->errormsg = 'Invalid TYPE attribute';
159 $this->debug($this->errormsg);
160 $result = false;
162 } else {
163 $this->errormsg = 'Missing TYPE attribute';
164 $this->debug($this->errormsg);
165 $result = false;
168 if (isset($xmlarr['@']['FIELDS'])) {
169 $fields = strtolower(trim($xmlarr['@']['FIELDS']));
170 if ($fields) {
171 $fieldsarr = explode(',',$fields);
172 if ($fieldsarr) {
173 foreach ($fieldsarr as $key => $element) {
174 $fieldsarr [$key] = trim($element);
176 } else {
177 $this->errormsg = 'Incorrect FIELDS attribute (comma separated of fields)';
178 $this->debug($this->errormsg);
179 $result = false;
181 } else {
182 $this->errormsg = 'Empty FIELDS attribute';
183 $this->debug($this->errormsg);
184 $result = false;
186 } else {
187 $this->errormsg = 'Missing FIELDS attribute';
188 $this->debug($this->errormsg);
189 $result = false;
191 /// Finally, set the array of fields
192 $this->fields = $fieldsarr;
194 if (isset($xmlarr['@']['REFTABLE'])) {
195 /// Check we are in a FK
196 if ($this->type == XMLDB_KEY_FOREIGN ||
197 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
198 $reftable = strtolower(trim($xmlarr['@']['REFTABLE']));
199 if (!$reftable) {
200 $this->errormsg = 'Empty REFTABLE attribute';
201 $this->debug($this->errormsg);
202 $result = false;
204 } else {
205 $this->errormsg = 'Wrong REFTABLE attribute (only FK can have it)';
206 $this->debug($this->errormsg);
207 $result = false;
209 } else if ($this->type == XMLDB_KEY_FOREIGN ||
210 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
211 $this->errormsg = 'Missing REFTABLE attribute';
212 $this->debug($this->errormsg);
213 $result = false;
215 /// Finally, set the reftable
216 if ($this->type == XMLDB_KEY_FOREIGN ||
217 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
218 $this->reftable = $reftable;
221 if (isset($xmlarr['@']['REFFIELDS'])) {
222 /// Check we are in a FK
223 if ($this->type == XMLDB_KEY_FOREIGN ||
224 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
225 $reffields = strtolower(trim($xmlarr['@']['REFFIELDS']));
226 if ($reffields) {
227 $reffieldsarr = explode(',',$reffields);
228 if ($reffieldsarr) {
229 foreach ($reffieldsarr as $key => $element) {
230 $reffieldsarr [$key] = trim($element);
232 } else {
233 $this->errormsg = 'Incorrect REFFIELDS attribute (comma separated of fields)';
234 $this->debug($this->errormsg);
235 $result = false;
237 } else {
238 $this->errormsg = 'Empty REFFIELDS attribute';
239 $this->debug($this->errormsg);
240 $result = false;
242 } else {
243 $this->errormsg = 'Wrong REFFIELDS attribute (only FK can have it)';
244 $this->debug($this->errormsg);
245 $result = false;
247 } else if ($this->type == XMLDB_KEY_FOREIGN ||
248 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
249 $this->errormsg = 'Missing REFFIELDS attribute';
250 $this->debug($this->errormsg);
251 $result = false;
253 /// Finally, set the array of reffields
254 if ($this->type == XMLDB_KEY_FOREIGN ||
255 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
256 $this->reffields = $reffieldsarr;
259 if (isset($xmlarr['@']['COMMENT'])) {
260 $this->comment = trim($xmlarr['@']['COMMENT']);
263 if (isset($xmlarr['@']['PREVIOUS'])) {
264 $this->previous = trim($xmlarr['@']['PREVIOUS']);
267 if (isset($xmlarr['@']['NEXT'])) {
268 $this->next = trim($xmlarr['@']['NEXT']);
271 /// Set some attributes
272 if ($result) {
273 $this->loaded = true;
275 $this->calculateHash();
276 return $result;
280 * This function returns the correct XMLDB_KEY_XXX value for the
281 * string passed as argument
283 function getXMLDBKeyType($type) {
285 $result = XMLDB_KEY_INCORRECT;
287 switch (strtolower($type)) {
288 case 'primary':
289 $result = XMLDB_KEY_PRIMARY;
290 break;
291 case 'unique':
292 $result = XMLDB_KEY_UNIQUE;
293 break;
294 case 'foreign':
295 $result = XMLDB_KEY_FOREIGN;
296 break;
297 case 'foreign-unique':
298 $result = XMLDB_KEY_FOREIGN_UNIQUE;
299 break;
300 /// case 'check': //Not supported
301 /// $result = XMLDB_KEY_CHECK;
302 /// break;
304 /// Return the normalized XMLDB_KEY
305 return $result;
309 * This function returns the correct name value for the
310 * XMLDB_KEY_XXX passed as argument
312 function getXMLDBKeyName($type) {
314 $result = '';
316 switch (strtolower($type)) {
317 case XMLDB_KEY_PRIMARY:
318 $result = 'primary';
319 break;
320 case XMLDB_KEY_UNIQUE:
321 $result = 'unique';
322 break;
323 case XMLDB_KEY_FOREIGN:
324 $result = 'foreign';
325 break;
326 case XMLDB_KEY_FOREIGN_UNIQUE:
327 $result = 'foreign-unique';
328 break;
329 /// case XMLDB_KEY_CHECK: //Not supported
330 /// $result = 'check';
331 /// break;
333 /// Return the normalized name
334 return $result;
338 * This function calculate and set the hash of one xmldb_key
340 function calculateHash($recursive = false) {
341 if (!$this->loaded) {
342 $this->hash = NULL;
343 } else {
344 $key = $this->type . implode(', ', $this->fields);
345 if ($this->type == XMLDB_KEY_FOREIGN ||
346 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
347 $key .= $this->reftable . implode(', ', $this->reffields);
350 $this->hash = md5($key);
355 *This function will output the XML text for one key
357 function xmlOutput() {
358 $o = '';
359 $o.= ' <KEY NAME="' . $this->name . '"';
360 $o.= ' TYPE="' . $this->getXMLDBKeyName($this->type) . '"';
361 $o.= ' FIELDS="' . implode(', ', $this->fields) . '"';
362 if ($this->type == XMLDB_KEY_FOREIGN ||
363 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
364 $o.= ' REFTABLE="' . $this->reftable . '"';
365 $o.= ' REFFIELDS="' . implode(', ', $this->reffields) . '"';
367 if ($this->comment) {
368 $o.= ' COMMENT="' . htmlspecialchars($this->comment) . '"';
370 if ($this->previous) {
371 $o.= ' PREVIOUS="' . $this->previous . '"';
373 if ($this->next) {
374 $o.= ' NEXT="' . $this->next . '"';
376 $o.= '/>' . "\n";
378 return $o;
382 * This function will set all the attributes of the xmldb_key object
383 * based on information passed in one ADOkey
385 function setFromADOKey($adokey) {
387 /// Calculate the XMLDB_KEY
388 switch (strtolower($adokey['name'])) {
389 case 'primary':
390 $this->type = XMLDB_KEY_PRIMARY;
391 break;
392 default:
393 $this->type = XMLDB_KEY_UNIQUE;
395 /// Set the fields, converting all them to lowercase
396 $fields = array_flip(array_change_key_case(array_flip($adokey['columns'])));
397 $this->fields = $fields;
398 /// Some more fields
399 $this->loaded = true;
400 $this->changed = true;
404 * Returns the PHP code needed to define one xmldb_key
406 function getPHP() {
408 $result = '';
410 /// The type
411 switch ($this->getType()) {
412 case XMLDB_KEY_PRIMARY:
413 $result .= 'XMLDB_KEY_PRIMARY' . ', ';
414 break;
415 case XMLDB_KEY_UNIQUE:
416 $result .= 'XMLDB_KEY_UNIQUE' . ', ';
417 break;
418 case XMLDB_KEY_FOREIGN:
419 $result .= 'XMLDB_KEY_FOREIGN' . ', ';
420 break;
422 /// The fields
423 $keyfields = $this->getFields();
424 if (!empty($keyfields)) {
425 $result .= 'array(' . "'". implode("', '", $keyfields) . "')";
426 } else {
427 $result .= 'null';
429 /// The FKs attributes
430 if ($this->getType() == XMLDB_KEY_FOREIGN) {
431 /// The reftable
432 $reftable = $this->getRefTable();
433 if (!empty($reftable)) {
434 $result .= ", '" . $reftable . "', ";
435 } else {
436 $result .= 'null, ';
438 /// The reffields
439 $reffields = $this->getRefFields();
440 if (!empty($reffields)) {
441 $result .= 'array(' . "'". implode("', '", $reffields) . "')";
442 } else {
443 $result .= 'null';
446 /// Return result
447 return $result;
451 * Shows info in a readable format
453 function readableInfo() {
454 $o = '';
455 /// type
456 $o .= $this->getXMLDBKeyName($this->type);
457 /// fields
458 $o .= ' (' . implode(', ', $this->fields) . ')';
459 /// foreign key
460 if ($this->type == XMLDB_KEY_FOREIGN ||
461 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
462 $o .= ' references ' . $this->reftable . ' (' . implode(', ', $this->reffields) . ')';
465 return $o;
469 /// TODO: Delete for 2.1 (deprecated in 2.0).
470 /// Deprecated API starts here
471 class XMLDBKey extends xmldb_key {
473 function __construct($name) {
474 parent::__construct($name);
478 /// Deprecated API ends here