fixed missing global
[moodle.git] / mod / data / lib.php
CommitLineData
4636bf83 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/>.
3d4b223a 17
4636bf83 18/**
750eb434 19 * @package mod-data
4636bf83 20 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
b8b554ac 24// Some constants
0997e51a 25define ('DATA_MAX_ENTRIES', 50);
26define ('DATA_PERPAGE_SINGLE', 1);
b572ce19 27
714bec74 28define ('DATA_FIRSTNAME', -1);
29define ('DATA_LASTNAME', -2);
bb5740f4 30define ('DATA_APPROVED', -3);
3239b010 31define ('DATA_TIMEADDED', 0);
32define ('DATA_TIMEMODIFIED', -4);
714bec74 33
a7e35395 34define ('DATA_CAP_EXPORT', 'mod/data:viewalluserpresets');
8aff1574
SH
35
36define('DATA_PRESET_COMPONENT', 'mod_data');
37define('DATA_PRESET_FILEAREA', 'site_presets');
38define('DATA_PRESET_CONTEXT', SYSCONTEXTID);
39
a7e35395 40// Users having assigned the default role "Non-editing teacher" can export database records
41// Using the mod/data capability "viewalluserpresets" existing in Moodle 1.9.x.
8429163d 42// In Moodle >= 2, new roles may be introduced and used instead.
a7e35395 43
4636bf83 44/**
750eb434 45 * @package mod-data
4636bf83 46 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48 */
668fc89a 49class data_field_base { // Base class for Database Field Types (see field/*/field.class.php)
3d4b223a 50
4636bf83 51 /** @var string Subclasses must override the type with their name */
52 var $type = 'unknown';
53 /** @var object The database object that this field belongs to */
54 var $data = NULL;
55 /** @var object The field object itself, if we know it */
56c1ca88 56 var $field = NULL;
4636bf83 57 /** @var int Width of the icon for this fieldtype */
58 var $iconwidth = 16;
59 /** @var int Width of the icon for this fieldtype */
60 var $iconheight = 16;
61 /** @var object course module or cmifno */
62 var $cm;
63 /** @var object activity context */
64 var $context;
65
66 /**
67 * Constructor function
68 *
69 * @global object
70 * @uses CONTEXT_MODULE
71 * @param int $field
72 * @param int $data
73 * @param int $cm
74 */
8429163d 75 function __construct($field=0, $data=0, $cm=0) { // Field or data or both, each can be id or object
9c00b5d7 76 global $DB;
0997e51a 77
78 if (empty($field) && empty($data)) {
29c1bb15 79 print_error('missingfield', 'data');
0997e51a 80 }
b572ce19 81
0997e51a 82 if (!empty($field)) {
83 if (is_object($field)) {
84 $this->field = $field; // Programmer knows what they are doing, we hope
9c00b5d7 85 } else if (!$this->field = $DB->get_record('data_fields', array('id'=>$field))) {
29c1bb15 86 print_error('invalidfieldid', 'data');
0997e51a 87 }
88 if (empty($data)) {
9c00b5d7 89 if (!$this->data = $DB->get_record('data', array('id'=>$this->field->dataid))) {
29c1bb15 90 print_error('invalidid', 'data');
0997e51a 91 }
92 }
93 }
b572ce19 94
0997e51a 95 if (empty($this->data)) { // We need to define this properly
96 if (!empty($data)) {
97 if (is_object($data)) {
98 $this->data = $data; // Programmer knows what they are doing, we hope
9c00b5d7 99 } else if (!$this->data = $DB->get_record('data', array('id'=>$data))) {
29c1bb15 100 print_error('invalidid', 'data');
0997e51a 101 }
102 } else { // No way to define it!
29c1bb15 103 print_error('missingdata', 'data');
0997e51a 104 }
105 }
b572ce19 106
8429163d 107 if ($cm) {
108 $this->cm = $cm;
109 } else {
110 $this->cm = get_coursemodule_from_instance('data', $this->data->id);
111 }
112
0997e51a 113 if (empty($this->field)) { // We need to define some default values
114 $this->define_default_field();
115 }
8429163d 116
117 $this->context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
3d4b223a 118 }
0997e51a 119
6403e679 120
4636bf83 121 /**
122 * This field just sets up a default field object
123 *
124 * @return bool
125 */
0997e51a 126 function define_default_field() {
4102b449 127 global $OUTPUT;
0997e51a 128 if (empty($this->data->id)) {
4102b449 129 echo $OUTPUT->notification('Programmer error: dataid not defined in field class');
0997e51a 130 }
131 $this->field = new object;
132 $this->field->id = 0;
133 $this->field->dataid = $this->data->id;
134 $this->field->type = $this->type;
135 $this->field->param1 = '';
136 $this->field->param2 = '';
137 $this->field->param3 = '';
138 $this->field->name = '';
139 $this->field->description = '';
b572ce19 140
0997e51a 141 return true;
3d4b223a 142 }
0997e51a 143
4636bf83 144 /**
145 * Set up the field object according to data in an object. Now is the time to clean it!
146 *
147 * @return bool
148 */
0997e51a 149 function define_field($data) {
150 $this->field->type = $this->type;
151 $this->field->dataid = $this->data->id;
152
153 $this->field->name = trim($data->name);
154 $this->field->description = trim($data->description);
155
156 if (isset($data->param1)) {
157 $this->field->param1 = trim($data->param1);
158 }
159 if (isset($data->param2)) {
8921fdb7 160 $this->field->param2 = trim($data->param2);
0997e51a 161 }
162 if (isset($data->param3)) {
163 $this->field->param3 = trim($data->param3);
164 }
165 if (isset($data->param4)) {
166 $this->field->param4 = trim($data->param4);
167 }
168 if (isset($data->param5)) {
169 $this->field->param5 = trim($data->param5);
170 }
171
172 return true;
3d4b223a 173 }
6403e679 174
4636bf83 175 /**
176 * Insert a new field in the database
177 * We assume the field object is already defined as $this->field
178 *
179 * @global object
180 * @return bool
181 */
0997e51a 182 function insert_field() {
4102b449 183 global $DB, $OUTPUT;
9c00b5d7 184
0997e51a 185 if (empty($this->field)) {
4102b449 186 echo $OUTPUT->notification('Programmer error: Field has not been defined yet! See define_field()');
0997e51a 187 return false;
188 }
189
a8f3a651 190 $this->field->id = $DB->insert_record('data_fields',$this->field);
0997e51a 191 return true;
3d4b223a 192 }
193
0997e51a 194
4636bf83 195 /**
196 * Update a field in the database
197 *
198 * @global object
199 * @return bool
200 */
0997e51a 201 function update_field() {
9c00b5d7 202 global $DB;
203
9d749339 204 $DB->update_record('data_fields', $this->field);
0997e51a 205 return true;
206 }
3d4b223a 207
4636bf83 208 /**
209 * Delete a field completely
210 *
211 * @global object
212 * @return bool
213 */
0997e51a 214 function delete_field() {
9c00b5d7 215 global $DB;
216
0997e51a 217 if (!empty($this->field->id)) {
0997e51a 218 $this->delete_content();
8429163d 219 $DB->delete_records('data_fields', array('id'=>$this->field->id));
3d4b223a 220 }
0997e51a 221 return true;
222 }
223
4636bf83 224 /**
225 * Print the relevant form element in the ADD template for this field
226 *
227 * @global object
228 * @param int $recordid
229 * @return string
230 */
0997e51a 231 function display_add_field($recordid=0){
9c00b5d7 232 global $DB;
233
0997e51a 234 if ($recordid){
9c00b5d7 235 $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
0997e51a 236 } else {
3d4b223a 237 $content = '';
238 }
6403e679 239
344e15e6 240 // beware get_field returns false for new, empty records MDL-18567
241 if ($content===false) {
242 $content='';
243 }
244
9706fa56 245 $str = '<div title="'.s($this->field->description).'">';
0997e51a 246 $str .= '<input style="width:300px;" type="text" name="field_'.$this->field->id.'" id="field_'.$this->field->id.'" value="'.s($content).'" />';
bbe39b6c 247 $str .= '</div>';
0997e51a 248
3d4b223a 249 return $str;
250 }
251
4636bf83 252 /**
253 * Print the relevant form element to define the attributes for this field
254 * viewable by teachers only.
255 *
256 * @global object
257 * @global object
258 * @return void Output is echo'd
259 */
0997e51a 260 function display_edit_field() {
b2dc6880 261 global $CFG, $DB, $OUTPUT;
0997e51a 262
263 if (empty($this->field)) { // No field has been defined yet, try and make one
264 $this->define_default_field();
3d4b223a 265 }
4102b449 266 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
0997e51a 267
b7dc2256 268 echo '<form id="editfield" action="'.$CFG->wwwroot.'/mod/data/field.php" method="post">'."\n";
0997e51a 269 echo '<input type="hidden" name="d" value="'.$this->data->id.'" />'."\n";
270 if (empty($this->field->id)) {
271 echo '<input type="hidden" name="mode" value="add" />'."\n";
272 $savebutton = get_string('add');
273 } else {
274 echo '<input type="hidden" name="fid" value="'.$this->field->id.'" />'."\n";
275 echo '<input type="hidden" name="mode" value="update" />'."\n";
276 $savebutton = get_string('savechanges');
277 }
278 echo '<input type="hidden" name="type" value="'.$this->type.'" />'."\n";
279 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />'."\n";
6403e679 280
b2dc6880 281 echo $OUTPUT->heading($this->name());
0997e51a 282
3d4b223a 283 require_once($CFG->dirroot.'/mod/data/field/'.$this->type.'/mod.html');
0997e51a 284
85db96c5 285 echo '<div class="mdl-align">';
0997e51a 286 echo '<input type="submit" value="'.$savebutton.'" />'."\n";
ec865e2d 287 echo '<input type="submit" name="cancel" value="'.get_string('cancel').'" />'."\n";
e357c206 288 echo '</div>';
0997e51a 289
290 echo '</form>';
291
4102b449 292 echo $OUTPUT->box_end();
3d4b223a 293 }
6403e679 294
4636bf83 295 /**
296 * Display the content of the field in browse mode
297 *
298 * @global object
299 * @param int $recordid
300 * @param object $template
301 * @return bool|string
302 */
0997e51a 303 function display_browse_field($recordid, $template) {
9c00b5d7 304 global $DB;
305
306 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
6403e679 307 if (isset($content->content)) {
9706fa56 308 $options = new object();
c8505cac 309 if ($this->field->param1 == '1') { // We are autolinking this field, so disable linking within us
310 //$content->content = '<span class="nolink">'.$content->content.'</span>';
311 //$content->content1 = FORMAT_HTML;
312 $options->filter=false;
313 }
1f697b99 314 $options->para = false;
315 $str = format_text($content->content, $content->content1, $options);
3d4b223a 316 } else {
317 $str = '';
318 }
319 return $str;
320 }
321 return false;
322 }
6403e679 323
4636bf83 324 /**
325 * Update the content of one data field in the data_content table
326 * @global object
327 * @param int $recordid
328 * @param mixed $value
329 * @param string $name
330 * @return bool
331 */
0997e51a 332 function update_content($recordid, $value, $name=''){
9c00b5d7 333 global $DB;
334
9706fa56 335 $content = new object();
0997e51a 336 $content->fieldid = $this->field->id;
c87fbb27 337 $content->recordid = $recordid;
338 $content->content = clean_param($value, PARAM_NOTAGS);
0997e51a 339
9c00b5d7 340 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
3d4b223a 341 $content->id = $oldcontent->id;
9c00b5d7 342 return $DB->update_record('data_content', $content);
0997e51a 343 } else {
9c00b5d7 344 return $DB->insert_record('data_content', $content);
3d4b223a 345 }
0997e51a 346 }
6403e679 347
4636bf83 348 /**
349 * Delete all content associated with the field
350 *
351 * @global object
352 * @param int $recordid
353 * @return bool
354 */
0997e51a 355 function delete_content($recordid=0) {
9c00b5d7 356 global $DB;
0997e51a 357
0997e51a 358 if ($recordid) {
8429163d 359 $conditions = array('fieldid'=>$this->field->id, 'recordid'=>$recordid);
0997e51a 360 } else {
8429163d 361 $conditions = array('fieldid'=>$this->field->id);
3d4b223a 362 }
0997e51a 363
8429163d 364 if ($rs = $DB->get_recordset('data_content', $conditions)) {
365 $fs = get_file_storage();
366 foreach ($rs as $content) {
64f93798 367 $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id);
8429163d 368 }
369 $rs->close();
0997e51a 370 }
371
8429163d 372 return $DB->delete_records('data_content', $conditions);
0997e51a 373 }
6403e679 374
4636bf83 375 /**
376 * Check if a field from an add form is empty
377 *
378 * @param mixed $value
379 * @param mixed $name
380 * @return bool
381 */
f9eab7b0 382 function notemptyfield($value, $name) {
3d4b223a 383 return !empty($value);
384 }
6403e679 385
4636bf83 386 /**
387 * Just in case a field needs to print something before the whole form
388 */
0997e51a 389 function print_before_form() {
3d4b223a 390 }
0997e51a 391
4636bf83 392 /**
393 * Just in case a field needs to print something after the whole form
394 */
5f5bcda8 395 function print_after_form() {
5f5bcda8 396 }
6403e679 397
398
4636bf83 399 /**
400 * Returns the sortable field for the content. By default, it's just content
401 * but for some plugins, it could be content 1 - content4
402 *
403 * @return string
404 */
cf3e199b 405 function get_sort_field() {
406 return 'content';
407 }
0997e51a 408
4636bf83 409 /**
410 * Returns the SQL needed to refer to the column. Some fields may need to CAST() etc.
411 *
412 * @param string $fieldname
413 * @return string $fieldname
414 */
64452eb4 415 function get_sort_sql($fieldname) {
416 return $fieldname;
417 }
418
4636bf83 419 /**
420 * Returns the name/type of the field
421 *
422 * @return string
423 */
b8b554ac 424 function name() {
0997e51a 425 return get_string('name'.$this->type, 'data');
426 }
6403e679 427
4636bf83 428 /**
429 * Prints the respective type icon
430 *
431 * @global object
432 * @return string
433 */
0997e51a 434 function image() {
5ef44400 435 global $OUTPUT;
0997e51a 436
a7d4cb67
DC
437 $params = array('d'=>$this->data->id, 'fid'=>$this->field->id, 'mode'=>'display', 'sesskey'=>sesskey());
438 $link = new moodle_url('/mod/data/field.php', $params);
439 $str = '<a href="'.$link->out().'">';
440 $str .= '<img src="'.$OUTPUT->pix_url('field/'.$this->type, 'data') . '" ';
051aad68 441 $str .= 'height="'.$this->iconheight.'" width="'.$this->iconwidth.'" alt="'.$this->type.'" title="'.$this->type.'" /></a>';
0997e51a 442 return $str;
443 }
444
4636bf83 445 /**
446 * Per default, it is assumed that fields support text exporting.
447 * Override this (return false) on fields not supporting text exporting.
448 *
449 * @return bool true
450 */
b8b554ac 451 function text_export_supported() {
452 return true;
453 }
6403e679 454
4636bf83 455 /**
456 * Per default, return the record's text value only from the "content" field.
457 * Override this in fields class if necesarry.
458 *
459 * @param string $record
460 * @return string
461 */
b8b554ac 462 function export_text_value($record) {
463 if ($this->text_export_supported()) {
464 return $record->content;
465 }
466 }
0997e51a 467
4636bf83 468 /**
469 * @param string $relativepath
470 * @return bool false
471 */
8429163d 472 function file_ok($relativepath) {
473 return false;
474 }
b8b554ac 475}
3d4b223a 476
477
4636bf83 478/**
479 * Given a template and a dataid, generate a default case template
480 *
481 * @global object
482 * @param object $data
483 * @param string template [addtemplate, singletemplate, listtempalte, rsstemplate]
484 * @param int $recordid
485 * @param bool $form
486 * @param bool $update
487 * @return bool|string
488 */
a44e7081 489function data_generate_default_template(&$data, $template, $recordid=0, $form=false, $update=true) {
9c00b5d7 490 global $DB;
d118d06a 491
edaa546a 492 if (!$data && !$template) {
3d4b223a 493 return false;
494 }
5cd07964 495 if ($template == 'csstemplate' or $template == 'jstemplate' ) {
f24eb261 496 return '';
497 }
6403e679 498
b8b554ac 499 // get all the fields for that database
9c00b5d7 500 if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id')) {
6403e679 501
e7a5de86 502 $str = '<div class="defaulttemplate">';
e357c206 503 $str .= '<table cellpadding="5">';
3d4b223a 504
f41cadeb 505 foreach ($fields as $field) {
3d4b223a 506
176e5c6c 507 $str .= '<tr><td valign="top" align="right">';
91f3f616 508 // Yu: commenting this out, the id was wrong and will fix later
509 //if ($template == 'addtemplate') {
510 //$str .= '<label';
511 //if (!in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
512 // $str .= ' for="[['.$field->name.'#id]]"';
513 //}
514 //$str .= '>'.$field->name.'</label>';
8429163d 515
91f3f616 516 //} else {
e357c206 517 $str .= $field->name.': ';
91f3f616 518 //}
3d4b223a 519 $str .= '</td>';
520
19edef1b 521 $str .='<td align="left">';
b8b554ac 522 if ($form) { // Print forms instead of data
d118d06a 523 $fieldobj = data_get_field($field, $data);
524 $str .= $fieldobj->display_add_field($recordid);
f62f7d8f 525
b8b554ac 526 } else { // Just print the tag
d118d06a 527 $str .= '[['.$field->name.']]';
528 }
3d4b223a 529 $str .= '</td></tr>';
6403e679 530
3d4b223a 531 }
bb828644 532 if ($template == 'listtemplate') {
8185aeb6 533 $str .= '<tr><td align="center" colspan="2">##edit## ##more## ##delete## ##approve## ##export##</td></tr>';
bb828644 534 } else if ($template == 'singletemplate') {
8185aeb6 535 $str .= '<tr><td align="center" colspan="2">##edit## ##delete## ##approve## ##export##</td></tr>';
714bec74 536 } else if ($template == 'asearchtemplate') {
537 $str .= '<tr><td valign="top" align="right">'.get_string('authorfirstname', 'data').': </td><td>##firstname##</td></tr>';
538 $str .= '<tr><td valign="top" align="right">'.get_string('authorlastname', 'data').': </td><td>##lastname##</td></tr>';
3d4b223a 539 }
540
176e5c6c 541 $str .= '</table>';
3d4b223a 542 $str .= '</div>';
543
d118d06a 544 if ($template == 'listtemplate'){
9e5537eb 545 $str .= '<hr />';
3d4b223a 546 }
f9eab7b0 547
d118d06a 548 if ($update) {
aab98aaf 549 $newdata = new object();
d118d06a 550 $newdata->id = $data->id;
f41cadeb 551 $newdata->{$template} = $str;
9d749339 552 $DB->update_record('data', $newdata);
553 $data->{$template} = $str;
3d4b223a 554 }
d118d06a 555
556 return $str;
3d4b223a 557 }
558}
559
560
4636bf83 561/**
56c1ca88 562 * Search for a field name and replaces it with another one in all the
563 * form templates. Set $newfieldname as '' if you want to delete the
4636bf83 564 * field from the form.
565 *
566 * @global object
567 * @param object $data
568 * @param string $searchfieldname
569 * @param string $newfieldname
570 * @return bool
571 */
0997e51a 572function data_replace_field_in_templates($data, $searchfieldname, $newfieldname) {
9c00b5d7 573 global $DB;
574
f9eab7b0 575 if (!empty($newfieldname)) {
576 $prestring = '[[';
577 $poststring = ']]';
9706fa56 578 $idpart = '#id';
735a7952 579
580 } else {
f9eab7b0 581 $prestring = '';
582 $poststring = '';
9706fa56 583 $idpart = '';
f9eab7b0 584 }
6403e679 585
9706fa56 586 $newdata = new object();
735a7952 587 $newdata->id = $data->id;
9c00b5d7 588 $newdata->singletemplate = str_ireplace('[['.$searchfieldname.']]',
589 $prestring.$newfieldname.$poststring, $data->singletemplate);
6403e679 590
9c00b5d7 591 $newdata->listtemplate = str_ireplace('[['.$searchfieldname.']]',
592 $prestring.$newfieldname.$poststring, $data->listtemplate);
6403e679 593
9c00b5d7 594 $newdata->addtemplate = str_ireplace('[['.$searchfieldname.']]',
595 $prestring.$newfieldname.$poststring, $data->addtemplate);
6403e679 596
9c00b5d7 597 $newdata->addtemplate = str_ireplace('[['.$searchfieldname.'#id]]',
598 $prestring.$newfieldname.$idpart.$poststring, $data->addtemplate);
9706fa56 599
9c00b5d7 600 $newdata->rsstemplate = str_ireplace('[['.$searchfieldname.']]',
601 $prestring.$newfieldname.$poststring, $data->rsstemplate);
6403e679 602
9c00b5d7 603 return $DB->update_record('data', $newdata);
f9eab7b0 604}
605
606
4636bf83 607/**
608 * Appends a new field at the end of the form template.
609 *
610 * @global object
611 * @param object $data
612 * @param string $newfieldname
613 */
0997e51a 614function data_append_new_field_to_templates($data, $newfieldname) {
9c00b5d7 615 global $DB;
0997e51a 616
9706fa56 617 $newdata = new object();
0997e51a 618 $newdata->id = $data->id;
ed69c723 619 $change = false;
0997e51a 620
f9eab7b0 621 if (!empty($data->singletemplate)) {
9c00b5d7 622 $newdata->singletemplate = $data->singletemplate.' [[' . $newfieldname .']]';
ed69c723 623 $change = true;
0997e51a 624 }
625 if (!empty($data->addtemplate)) {
9c00b5d7 626 $newdata->addtemplate = $data->addtemplate.' [[' . $newfieldname . ']]';
ed69c723 627 $change = true;
f9eab7b0 628 }
629 if (!empty($data->rsstemplate)) {
9c00b5d7 630 $newdata->rsstemplate = $data->singletemplate.' [[' . $newfieldname . ']]';
ed69c723 631 $change = true;
632 }
633 if ($change) {
9c00b5d7 634 $DB->update_record('data', $newdata);
f9eab7b0 635 }
f9eab7b0 636}
637
638
4636bf83 639/**
640 * given a field name
641 * this function creates an instance of the particular subfield class
642 *
643 * @global object
644 * @param string $name
645 * @param object $data
646 * @return object|bool
647 */
0997e51a 648function data_get_field_from_name($name, $data){
9c00b5d7 649 global $DB;
650
1e123f47 651 $field = $DB->get_record('data_fields', array('name'=>$name, 'dataid'=>$data->id));
0997e51a 652
653 if ($field) {
654 return data_get_field($field, $data);
655 } else {
656 return false;
657 }
658}
659
4636bf83 660/**
661 * given a field id
662 * this function creates an instance of the particular subfield class
663 *
664 * @global object
665 * @param int $fieldid
666 * @param object $data
667 * @return bool|object
668 */
0997e51a 669function data_get_field_from_id($fieldid, $data){
9c00b5d7 670 global $DB;
671
1e123f47 672 $field = $DB->get_record('data_fields', array('id'=>$fieldid, 'dataid'=>$data->id));
3d4b223a 673
0997e51a 674 if ($field) {
675 return data_get_field($field, $data);
5782be6b 676 } else {
d6f0e247 677 return false;
3d4b223a 678 }
3d4b223a 679}
680
4636bf83 681/**
682 * given a field id
683 * this function creates an instance of the particular subfield class
684 *
685 * @global object
686 * @param string $type
687 * @param object $data
688 * @return object
689 */
0997e51a 690function data_get_field_new($type, $data) {
691 global $CFG;
b572ce19 692
0997e51a 693 require_once($CFG->dirroot.'/mod/data/field/'.$type.'/field.class.php');
694 $newfield = 'data_field_'.$type;
695 $newfield = new $newfield(0, $data);
696 return $newfield;
697}
698
4636bf83 699/**
700 * returns a subclass field object given a record of the field, used to
701 * invoke plugin methods
702 * input: $param $field - record from db
703 *
704 * @global object
705 * @param object $field
706 * @param object $data
707 * @param object $cm
708 * @return object
709 */
8429163d 710function data_get_field($field, $data, $cm=null) {
0997e51a 711 global $CFG;
b572ce19 712
0997e51a 713 if ($field) {
3d4b223a 714 require_once('field/'.$field->type.'/field.class.php');
715 $newfield = 'data_field_'.$field->type;
8429163d 716 $newfield = new $newfield($field, $data, $cm);
3d4b223a 717 return $newfield;
718 }
719}
720
721
8429163d 722/**
723 * Given record object (or id), returns true if the record belongs to the current user
4636bf83 724 *
725 * @global object
726 * @global object
727 * @param mixed $record record object or id
8429163d 728 * @return bool
729 */
730function data_isowner($record) {
9c00b5d7 731 global $USER, $DB;
3d4b223a 732
4f0c2d00 733 if (!isloggedin()) { // perf shortcut
3d4b223a 734 return false;
735 }
736
8429163d 737 if (!is_object($record)) {
738 if (!$record = $DB->get_record('data_records', array('id'=>$record))) {
739 return false;
740 }
3d4b223a 741 }
742
8429163d 743 return ($record->userid == $USER->id);
3d4b223a 744}
745
4636bf83 746/**
747 * has a user reached the max number of entries?
748 *
749 * @param object $data
750 * @return bool
751 */
b572ce19 752function data_atmaxentries($data){
753 if (!$data->maxentries){
3d4b223a 754 return false;
b572ce19 755
3d4b223a 756 } else {
757 return (data_numentries($data) >= $data->maxentries);
758 }
759}
760
4636bf83 761/**
762 * returns the number of entries already made by this user
56c1ca88 763 *
4636bf83 764 * @global object
765 * @global object
56c1ca88 766 * @param object $data
4636bf83 767 * @return int
768 */
3d4b223a 769function data_numentries($data){
8429163d 770 global $USER, $DB;
9c00b5d7 771 $sql = 'SELECT COUNT(*) FROM {data_records} WHERE dataid=? AND userid=?';
772 return $DB->count_records_sql($sql, array($data->id, $USER->id));
3d4b223a 773}
774
4636bf83 775/**
56c1ca88 776 * function that takes in a dataid and adds a record
4636bf83 777 * this is used everytime an add template is submitted
778 *
779 * @global object
780 * @global object
781 * @param object $data
782 * @param int $groupid
783 * @return bool
784 */
0997e51a 785function data_add_record($data, $groupid=0){
9c00b5d7 786 global $USER, $DB;
6403e679 787
c088d603 788 $cm = get_coursemodule_from_instance('data', $data->id);
bbbf2d40 789 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
b572ce19 790
9706fa56 791 $record = new object();
3d4b223a 792 $record->userid = $USER->id;
0997e51a 793 $record->dataid = $data->id;
3d4b223a 794 $record->groupid = $groupid;
0997e51a 795 $record->timecreated = $record->timemodified = time();
0468976c 796 if (has_capability('mod/data:approve', $context)) {
6e0119dd 797 $record->approved = 1;
798 } else {
799 $record->approved = 0;
800 }
9c00b5d7 801 return $DB->insert_record('data_records', $record);
3d4b223a 802}
803
4636bf83 804/**
805 * check the multple existence any tag in a template
806 *
807 * check to see if there are 2 or more of the same tag being used.
808 *
809 * @global object
810 * @param int $dataid,
56c1ca88 811 * @param string $template
4636bf83 812 * @return bool
813 */
9c00b5d7 814function data_tags_check($dataid, $template) {
4102b449 815 global $DB, $OUTPUT;
9c00b5d7 816
b8b554ac 817 // first get all the possible tags
9c00b5d7 818 $fields = $DB->get_records('data_fields', array('dataid'=>$dataid));
b8b554ac 819 // then we generate strings to replace
820 $tagsok = true; // let's be optimistic
b572ce19 821 foreach ($fields as $field){
d118d06a 822 $pattern="/\[\[".$field->name."\]\]/i";
b572ce19 823 if (preg_match_all($pattern, $template, $dummy)>1){
3d4b223a 824 $tagsok = false;
4102b449 825 echo $OUTPUT->notification('[['.$field->name.']] - '.get_string('multipletags','data'));
3d4b223a 826 }
827 }
b8b554ac 828 // else return true
3d4b223a 829 return $tagsok;
830}
831
4636bf83 832/**
833 * Adds an instance of a data
834 *
835 * @global object
836 * @param object $data
837 * @return $int
838 */
3d4b223a 839function data_add_instance($data) {
8429163d 840 global $DB;
3d4b223a 841
57244db3 842 if (empty($data->assessed)) {
843 $data->assessed = 0;
d6af3cfa 844 }
845
3d4b223a 846 $data->timemodified = time();
847
a8f3a651 848 $data->id = $DB->insert_record('data', $data);
3d4b223a 849
612607bd 850 data_grade_item_update($data);
b572ce19 851
3d4b223a 852 return $data->id;
853}
854
4636bf83 855/**
856 * updates an instance of a data
857 *
858 * @global object
859 * @param object $data
860 * @return bool
861 */
3d4b223a 862function data_update_instance($data) {
4102b449 863 global $DB, $OUTPUT;
6403e679 864
04366d79 865 $data->timemodified = time();
b572ce19 866 $data->id = $data->instance;
6403e679 867
57244db3 868 if (empty($data->assessed)) {
869 $data->assessed = 0;
17e5f3fc 870 }
b572ce19 871
d251b259
AD
872 if (empty($data->ratingtime) or empty($data->assessed)) {
873 $data->assesstimestart = 0;
874 $data->assesstimefinish = 0;
875 }
876
05ac14ca 877 if (empty($data->notification)) {
878 $data->notification = 0;
879 }
880
0bcf8b6f 881 $DB->update_record('data', $data);
04366d79 882
04366d79 883 data_grade_item_update($data);
b572ce19 884
04366d79 885 return true;
b572ce19 886
3d4b223a 887}
888
4636bf83 889/**
890 * deletes an instance of a data
891 *
892 * @global object
893 * @param int $id
894 * @return bool
895 */
b8b554ac 896function data_delete_instance($id) { // takes the dataid
650a0c0a 897 global $DB, $CFG;
3d4b223a 898
8429163d 899 if (!$data = $DB->get_record('data', array('id'=>$id))) {
3d4b223a 900 return false;
901 }
902
8429163d 903 $cm = get_coursemodule_from_instance('data', $data->id);
904 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
b572ce19 905
8429163d 906/// Delete all the associated information
6403e679 907
8429163d 908 // files
909 $fs = get_file_storage();
64f93798 910 $fs->delete_area_files($context->id, 'mod_data');
3d4b223a 911
d2e09d60
AD
912 // Delete ratings
913 //delete ratings
914 require_once($CFG->dirroot.'/rating/lib.php');
915 $delopt = new stdclass();
916 $delopt->contextid = $context->id;
917 $rm = new rating_manager();
918 $rm->delete_ratings($delopt);
919
8429163d 920 // get all the records in this data
921 $sql = "SELECT r.id
922 FROM {data_records} r
923 WHERE r.dataid = ?";
6403e679 924
8429163d 925 $DB->delete_records_select('data_content', "recordid IN ($sql)", array($id));
3d4b223a 926
927 // delete all the records and fields
c18269c7 928 $DB->delete_records('data_records', array('dataid'=>$id));
929 $DB->delete_records('data_fields', array('dataid'=>$id));
3d4b223a 930
931 // Delete the instance itself
c18269c7 932 $result = $DB->delete_records('data', array('id'=>$id));
04366d79 933
8429163d 934 // cleanup gradebook
b82cacea 935 data_grade_item_delete($data);
b572ce19 936
04366d79 937 return $result;
3d4b223a 938}
939
4636bf83 940/**
941 * returns a summary of data activity of this user
942 *
943 * @global object
944 * @param object $course
945 * @param object $user
946 * @param object $mod
947 * @param object $data
948 * @return object|null
949 */
3d4b223a 950function data_user_outline($course, $user, $mod, $data) {
1a96363a
NC
951 global $DB, $CFG;
952 require_once("$CFG->libdir/gradelib.php");
953
954 $grades = grade_get_grades($course->id, 'mod', 'data', $data->id, $user->id);
955 if (empty($grades->items[0]->grades)) {
956 $grade = false;
957 } else {
958 $grade = reset($grades->items[0]->grades);
959 }
960
9c00b5d7 961
962 if ($countrecords = $DB->count_records('data_records', array('dataid'=>$data->id, 'userid'=>$user->id))) {
9706fa56 963 $result = new object();
63701c78 964 $result->info = get_string('numrecords', 'data', $countrecords);
9c00b5d7 965 $lastrecord = $DB->get_record_sql('SELECT id,timemodified FROM {data_records}
966 WHERE dataid = ? AND userid = ?
967 ORDER BY timemodified DESC', array($data->id, $user->id), true);
3d4b223a 968 $result->time = $lastrecord->timemodified;
1a96363a
NC
969 if ($grade) {
970 $result->info .= ', ' . get_string('grade') . ': ' . $grade->str_long_grade;
971 }
972 return $result;
973 } else if ($grade) {
974 $result = new object();
975 $result->info = get_string('grade') . ': ' . $grade->str_long_grade;
976 $result->time = $grade->dategraded;
3d4b223a 977 return $result;
978 }
979 return NULL;
3d4b223a 980}
981
4636bf83 982/**
983 * Prints all the records uploaded by this user
984 *
985 * @global object
986 * @param object $course
987 * @param object $user
988 * @param object $mod
989 * @param object $data
990 */
3d4b223a 991function data_user_complete($course, $user, $mod, $data) {
1a96363a
NC
992 global $DB, $CFG, $OUTPUT;
993 require_once("$CFG->libdir/gradelib.php");
994
995 $grades = grade_get_grades($course->id, 'mod', 'data', $data->id, $user->id);
996 if (!empty($grades->items[0]->grades)) {
997 $grade = reset($grades->items[0]->grades);
998 echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
999 if ($grade->str_feedback) {
1000 echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
1001 }
1002 }
9c00b5d7 1003
1004 if ($records = $DB->get_records('data_records', array('dataid'=>$data->id,'userid'=>$user->id), 'timemodified DESC')) {
3d45b8e5 1005 data_print_template('singletemplate', $records, $data);
3d4b223a 1006 }
1007}
1008
04366d79 1009/**
1010 * Return grade for given user or all users.
1011 *
4636bf83 1012 * @global object
1013 * @param object $data
04366d79 1014 * @param int $userid optional user id, 0 means all users
1015 * @return array array of grades, false if none
1016 */
612607bd 1017function data_get_user_grades($data, $userid=0) {
d251b259
AD
1018 global $CFG;
1019
1020 require_once($CFG->dirroot.'/rating/lib.php');
1021 $rm = new rating_manager();
04366d79 1022
d251b259
AD
1023 $ratingoptions = new stdclass();
1024 $ratingoptions->modulename = 'data';
1025 $ratingoptions->moduleid = $data->id;
04366d79 1026
d251b259
AD
1027 $ratingoptions->userid = $userid;
1028 $ratingoptions->aggregationmethod = $data->assessed;
1029 $ratingoptions->scaleid = $data->scale;
1030 $ratingoptions->itemtable = 'data_records';
1031 $ratingoptions->itemtableusercolumn = 'userid';
04366d79 1032
d251b259 1033 return $rm->get_user_grades($ratingoptions);
04366d79 1034}
1035
1036/**
775f811a 1037 * Update activity grades
04366d79 1038 *
4636bf83 1039 * @global object
1040 * @global object
775f811a 1041 * @param object $data
1042 * @param int $userid specific user only, 0 means all
4636bf83 1043 * @param bool $nullifnone
04366d79 1044 */
775f811a 1045function data_update_grades($data, $userid=0, $nullifnone=true) {
9c00b5d7 1046 global $CFG, $DB;
1047 require_once($CFG->libdir.'/gradelib.php');
04366d79 1048
775f811a 1049 if (!$data->assessed) {
1050 data_grade_item_update($data);
b572ce19 1051
775f811a 1052 } else if ($grades = data_get_user_grades($data, $userid)) {
1053 data_grade_item_update($data, $grades);
b572ce19 1054
775f811a 1055 } else if ($userid and $nullifnone) {
1056 $grade = new object();
1057 $grade->userid = $userid;
1058 $grade->rawgrade = NULL;
1059 data_grade_item_update($data, $grade);
b572ce19 1060
04366d79 1061 } else {
775f811a 1062 data_grade_item_update($data);
1063 }
1064}
1065
1066/**
1067 * Update all grades in gradebook.
4636bf83 1068 *
1069 * @global object
775f811a 1070 */
1071function data_upgrade_grades() {
1072 global $DB;
1073
1074 $sql = "SELECT COUNT('x')
1075 FROM {data} d, {course_modules} cm, {modules} m
1076 WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id";
1077 $count = $DB->count_records_sql($sql);
1078
1079 $sql = "SELECT d.*, cm.idnumber AS cmidnumber, d.course AS courseid
1080 FROM {data} d, {course_modules} cm, {modules} m
1081 WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id";
1082 if ($rs = $DB->get_recordset_sql($sql)) {
1083 // too much debug output
775f811a 1084 $pbar = new progress_bar('dataupgradegrades', 500, true);
1085 $i=0;
1086 foreach ($rs as $data) {
1087 $i++;
1088 upgrade_set_timeout(60*5); // set up timeout, may also abort execution
1089 data_update_grades($data, 0, false);
1090 $pbar->update($i, $count, "Updating Database grades ($i/$count).");
04366d79 1091 }
775f811a 1092 $rs->close();
04366d79 1093 }
1094}
1095
1096/**
612607bd 1097 * Update/create grade item for given data
04366d79 1098 *
4636bf83 1099 * @global object
04366d79 1100 * @param object $data object with extra cmidnumber
0b5a80a1 1101 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
04366d79 1102 * @return object grade_item
1103 */
0b5a80a1 1104function data_grade_item_update($data, $grades=NULL) {
612607bd 1105 global $CFG;
9c00b5d7 1106 require_once($CFG->libdir.'/gradelib.php');
04366d79 1107
13534ef7 1108 $params = array('itemname'=>$data->name, 'idnumber'=>$data->cmidnumber);
b572ce19 1109
04366d79 1110 if (!$data->assessed or $data->scale == 0) {
612607bd 1111 $params['gradetype'] = GRADE_TYPE_NONE;
b572ce19 1112
04366d79 1113 } else if ($data->scale > 0) {
1114 $params['gradetype'] = GRADE_TYPE_VALUE;
1115 $params['grademax'] = $data->scale;
1116 $params['grademin'] = 0;
b572ce19 1117
04366d79 1118 } else if ($data->scale < 0) {
1119 $params['gradetype'] = GRADE_TYPE_SCALE;
1120 $params['scaleid'] = -$data->scale;
1121 }
b572ce19 1122
0b5a80a1 1123 if ($grades === 'reset') {
1124 $params['reset'] = true;
1125 $grades = NULL;
1126 }
1127
1128 return grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, $grades, $params);
04366d79 1129}
1130
1131/**
1132 * Delete grade item for given data
1133 *
4636bf83 1134 * @global object
04366d79 1135 * @param object $data object
1136 * @return object grade_item
1137 */
1138function data_grade_item_delete($data) {
612607bd 1139 global $CFG;
1140 require_once($CFG->libdir.'/gradelib.php');
b572ce19 1141
b67ec72f 1142 return grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, NULL, array('deleted'=>1));
04366d79 1143}
1144
4636bf83 1145/**
1146 * returns a list of participants of this database
1147 *
1148 * @global object
1149 * @return array
1150 */
3d4b223a 1151function data_get_participants($dataid) {
b8b554ac 1152// Returns the users with data in one data
d251b259 1153// (users with records in data_records, data_comments and ratings)
8429163d 1154 global $DB;
9c00b5d7 1155
1156 $records = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
1157 FROM {user} u, {data_records} r
1158 WHERE r.dataid = ? AND u.id = r.userid", array($dataid));
1159
1160 $comments = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
e998effa
DC
1161 FROM {user} u, {data_records} r, {comments} c
1162 WHERE r.dataid = ? AND u.id = r.userid AND r.id = c.itemid AND c.commentarea='database_entry'", array($dataid));
9c00b5d7 1163
1164 $ratings = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
d251b259
AD
1165 FROM {user} u, {data_records} r, {ratings} a
1166 WHERE r.dataid = ? AND u.id = r.userid AND r.id = a.itemid", array($dataid));
3d4b223a 1167
1168 $participants = array();
b572ce19 1169
8429163d 1170 if ($records) {
3d4b223a 1171 foreach ($records as $record) {
1172 $participants[$record->id] = $record;
1173 }
1174 }
8429163d 1175 if ($comments) {
3d4b223a 1176 foreach ($comments as $comment) {
1177 $participants[$comment->id] = $comment;
1178 }
1179 }
8429163d 1180 if ($ratings) {
cf040300 1181 foreach ($ratings as $rating) {
1182 $participants[$rating->id] = $rating;
1183 }
1184 }
b572ce19 1185
3d4b223a 1186 return $participants;
1187}
1188
b8b554ac 1189// junk functions
4636bf83 1190/**
56c1ca88 1191 * takes a list of records, the current data, a search string,
4636bf83 1192 * and mode to display prints the translated template
1193 *
1194 * @global object
1195 * @global object
1196 * @param string $template
56c1ca88 1197 * @param array $records
4636bf83 1198 * @param object $data
56c1ca88 1199 * @param string $search
4636bf83 1200 * @param int $page
1201 * @param bool $return
1202 * @return mixed
1203 */
d53e5129 1204function data_print_template($template, $records, $data, $search='', $page=0, $return=false) {
f2a1963c 1205 global $CFG, $DB, $OUTPUT;
c088d603 1206 $cm = get_coursemodule_from_instance('data', $data->id);
dabfd0ed 1207 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
b572ce19 1208
3d45b8e5 1209 static $fields = NULL;
1210 static $isteacher;
63701c78 1211 static $dataid = NULL;
b572ce19 1212
63701c78 1213 if (empty($dataid)) {
1214 $dataid = $data->id;
1215 } else if ($dataid != $data->id) {
1216 $fields = NULL;
1217 }
b572ce19 1218
3d45b8e5 1219 if (empty($fields)) {
9c00b5d7 1220 $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id));
3d45b8e5 1221 foreach ($fieldrecords as $fieldrecord) {
1222 $fields[]= data_get_field($fieldrecord, $data);
1223 }
81e956b9 1224 $isteacher = has_capability('mod/data:managetemplates', $context);
3d45b8e5 1225 }
b572ce19 1226
64452eb4 1227 if (empty($records)) {
1228 return;
1229 }
b572ce19 1230
668fc89a 1231 foreach ($records as $record) { // Might be just one for the single template
b572ce19 1232
b8b554ac 1233 // Replacing tags
3d4b223a 1234 $patterns = array();
1235 $replacement = array();
b572ce19 1236
b8b554ac 1237 // Then we generate strings to replace for normal tags
3d45b8e5 1238 foreach ($fields as $field) {
f2584d0e 1239 $patterns[]='[['.$field->field->name.']]';
d118d06a 1240 $replacement[] = highlight($search, $field->display_browse_field($record->id, $template));
3d4b223a 1241 }
b572ce19 1242
b8b554ac 1243 // Replacing special tags (##Edit##, ##Delete##, ##More##)
e357c206 1244 $patterns[]='##edit##';
1245 $patterns[]='##delete##';
046dd7dc 1246 if (has_capability('mod/data:manageentries', $context) or data_isowner($record->id)) {
64452eb4 1247 $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/edit.php?d='
b5d0cafc 1248 .$data->id.'&amp;rid='.$record->id.'&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="'.get_string('edit').'" title="'.get_string('edit').'" /></a>';
3d4b223a 1249 $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/view.php?d='
b5d0cafc 1250 .$data->id.'&amp;delete='.$record->id.'&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="'.get_string('delete').'" title="'.get_string('delete').'" /></a>';
046dd7dc 1251 } else {
3d4b223a 1252 $replacement[] = '';
eeeb4f2a 1253 $replacement[] = '';
3d4b223a 1254 }
d53e5129 1255
1256 $moreurl = $CFG->wwwroot . '/mod/data/view.php?d=' . $data->id . '&amp;rid=' . $record->id;
32d799c6 1257 if ($search) {
1258 $moreurl .= '&amp;filter=1';
1259 }
e357c206 1260 $patterns[]='##more##';
b5d0cafc 1261 $replacement[] = '<a href="' . $moreurl . '"><img src="' . $OUTPUT->pix_url('i/search') . '" class="iconsmall" alt="' . get_string('more', 'data') . '" title="' . get_string('more', 'data') . '" /></a>';
d53e5129 1262
e357c206 1263 $patterns[]='##moreurl##';
d53e5129 1264 $replacement[] = $moreurl;
473dd288 1265
e357c206 1266 $patterns[]='##user##';
64452eb4 1267 $replacement[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$record->userid.
1268 '&amp;course='.$data->course.'">'.fullname($record).'</a>';
8185aeb6 1269
1270 $patterns[]='##export##';
1271
c3e1b5bd 1272 if ($CFG->enableportfolios && ($template == 'singletemplate' || $template == 'listtemplate')
8185aeb6 1273 && ((has_capability('mod/data:exportentry', $context)
1274 || (data_isowner($record->id) && has_capability('mod/data:exportownentry', $context))))) {
24ba58ee 1275 require_once($CFG->libdir . '/portfoliolib.php');
0d06b6fd 1276 $button = new portfolio_add_button();
24ba58ee 1277 $button->set_callback_options('data_portfolio_caller', array('id' => $cm->id, 'recordid' => $record->id), '/mod/data/locallib.php');
6be1dcae 1278 list($formats, $files) = data_portfolio_caller::formats($fields, $record);
276378e6 1279 $button->set_formats($formats);
0d06b6fd 1280 $replacement[] = $button->to_html(PORTFOLIO_ADD_ICON_LINK);
8185aeb6 1281 } else {
1282 $replacement[] = '';
1283 }
8429163d 1284
c95034f2 1285 $patterns[] = '##timeadded##';
8429163d 1286 $replacement[] = userdate($record->timecreated);
c95034f2 1287
1288 $patterns[] = '##timemodified##';
35e129c1 1289 $replacement [] = userdate($record->timemodified);
473dd288 1290
e357c206 1291 $patterns[]='##approve##';
b572ce19 1292 if (has_capability('mod/data:approve', $context) && ($data->approval) && (!$record->approved)){
b5d0cafc 1293 $replacement[] = '<span class="approve"><a href="'.$CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;approve='.$record->id.'&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('i/approve') . '" class="icon" alt="'.get_string('approve').'" /></a></span>';
473dd288 1294 } else {
6e0119dd 1295 $replacement[] = '';
1296 }
6403e679 1297
e357c206 1298 $patterns[]='##comments##';
4d3d87f0 1299 if (($template == 'listtemplate') && ($data->comments)) {
e998effa
DC
1300
1301 if (!empty($CFG->usecomments)) {
36051c9e 1302 require_once($CFG->dirroot . '/comment/lib.php');
866354a9 1303 list($context, $course, $cm) = get_context_info_array($context->id);
e998effa 1304 $cmt = new stdclass;
e998effa 1305 $cmt->context = $context;
866354a9
DC
1306 $cmt->course = $course;
1307 $cmt->cm = $cm;
1308 $cmt->area = 'database_entry';
e998effa
DC
1309 $cmt->itemid = $record->id;
1310 $cmt->showcount = true;
d846488e 1311 $cmt->component = 'mod_data';
e998effa 1312 $comment = new comment($cmt);
36051c9e 1313 $replacement[] = $comment->output(true);
e998effa 1314 }
4d3d87f0 1315 } else {
1316 $replacement[] = '';
1317 }
6e0119dd 1318
b8b554ac 1319 // actual replacement of the tags
f2584d0e 1320 $newtext = str_ireplace($patterns, $replacement, $data->{$template});
5023c3ab 1321
b8b554ac 1322 // no more html formatting and filtering - see MDL-6635
aa3b20e9 1323 if ($return) {
1324 return $newtext;
1325 } else {
1326 echo $newtext;
b572ce19 1327
aa3b20e9 1328 // hack alert - return is always false in singletemplate anyway ;-)
1329 /**********************************
1330 * Printing Ratings Form *
1331 *********************************/
1332 if ($template == 'singletemplate') { //prints ratings options
1333 data_print_ratings($data, $record);
1334 }
b572ce19 1335
aa3b20e9 1336 /**********************************
d251b259 1337 * Printing Comments Form *
aa3b20e9 1338 *********************************/
d251b259 1339 if (($template == 'singletemplate') && ($data->comments)) {
e998effa 1340 if (!empty($CFG->usecomments)) {
36051c9e 1341 require_once($CFG->dirroot . '/comment/lib.php');
866354a9 1342 list($context, $course, $cm) = get_context_info_array($context->id);
e998effa 1343 $cmt = new stdclass;
e998effa 1344 $cmt->context = $context;
866354a9
DC
1345 $cmt->course = $course;
1346 $cmt->cm = $cm;
1347 $cmt->area = 'database_entry';
e998effa
DC
1348 $cmt->itemid = $record->id;
1349 $cmt->showcount = true;
d846488e 1350 $cmt->component = 'mod_data';
e998effa 1351 $comment = new comment($cmt);
36051c9e 1352 $comment->output(false);
e998effa 1353 }
aa3b20e9 1354 }
4d3d87f0 1355 }
3d4b223a 1356 }
1357}
1358
d251b259
AD
1359/**
1360 * Return rating related permissions
1361 * @param string $options the context id
1362 * @return array an associative array of the user's rating permissions
1363 */
1364function data_rating_permissions($options) {
1365 $contextid = $options;
1366 $context = get_context_instance_by_id($contextid);
1367
1368 if (!$context) {
1369 print_error('invalidcontext');
1370 return null;
1371 } else {
1372 $ret = new stdclass();
1373 return array('view'=>has_capability('mod/data:viewrating',$context), 'viewany'=>has_capability('mod/data:viewanyrating',$context), 'viewall'=>has_capability('mod/data:viewallratings',$context), 'rate'=>has_capability('mod/data:rate',$context));
1374 }
1375}
1376
aeafd436
AD
1377/**
1378 * Returns the names of the table and columns necessary to check items for ratings
1379 * @return array an array containing the item table, item id and user id columns
1380 */
1381function data_rating_item_check_info() {
1382 return array('data_records','id','userid');
1383}
1384
a593aeee 1385
4636bf83 1386/**
56c1ca88 1387 * function that takes in the current data, number of items per page,
1388 * a search string and prints a preference box in view.php
1389 *
1390 * This preference box prints a searchable advanced search template if
1391 * a) A template is defined
1392 * b) The advanced search checkbox is checked.
4636bf83 1393 *
1394 * @global object
1395 * @global object
56c1ca88 1396 * @param object $data
1397 * @param int $perpage
4636bf83 1398 * @param string $search
1399 * @param string $sort
1400 * @param string $order
1401 * @param array $search_array
1402 * @param int $advanced
1403 * @param string $mode
1404 * @return void
1405 */
7900ecb0 1406function data_print_preference_form($data, $perpage, $search, $sort='', $order='ASC', $search_array = '', $advanced = 0, $mode= ''){
601104f2 1407 global $CFG, $DB, $PAGE, $OUTPUT;
8429163d 1408
7900ecb0 1409 $cm = get_coursemodule_from_instance('data', $data->id);
1410 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1411 echo '<br /><div class="datapreferences">';
b7dc2256 1412 echo '<form id="options" action="view.php" method="get">';
7900ecb0 1413 echo '<div>';
473dd288 1414 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
7900ecb0 1415 if ($mode =='asearch') {
1416 $advanced = 1;
1417 echo '<input type="hidden" name="mode" value="list" />';
1418 }
a032a460 1419 echo '<label for="pref_perpage">'.get_string('pagesize','data').'</label> ';
76a2fd82 1420 $pagesizes = array(2=>2,3=>3,4=>4,5=>5,6=>6,7=>7,8=>8,9=>9,10=>10,15=>15,
6e0119dd 1421 20=>20,30=>30,40=>40,50=>50,100=>100,200=>200,300=>300,400=>400,500=>500,1000=>1000);
d776d59e 1422 echo html_writer::select($pagesizes, 'perpage', $perpage, false, array('id'=>'pref_perpage'));
4b9210f3 1423 echo '<div id="reg_search" style="display: ';
7900ecb0 1424 if ($advanced) {
1425 echo 'none';
1426 }
1427 else {
1428 echo 'inline';
1429 }
1430 echo ';" >&nbsp;&nbsp;&nbsp;<label for="pref_search">'.get_string('search').'</label> <input type="text" size="16" name="search" id= "pref_search" value="'.s($search).'" /></div>';
a032a460 1431 echo '&nbsp;&nbsp;&nbsp;<label for="pref_sortby">'.get_string('sortby').'</label> ';
b8b554ac 1432 // foreach field, print the option
3239b010 1433 echo '<select name="sort" id="pref_sortby">';
9c00b5d7 1434 if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'name')) {
3239b010 1435 echo '<optgroup label="'.get_string('fields', 'data').'">';
1436 foreach ($fields as $field) {
1437 if ($field->id == $sort) {
1438 echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
1439 } else {
1440 echo '<option value="'.$field->id.'">'.$field->name.'</option>';
1441 }
1442 }
1443 echo '</optgroup>';
714bec74 1444 }
3239b010 1445 $options = array();
1446 $options[DATA_TIMEADDED] = get_string('timeadded', 'data');
1447 $options[DATA_TIMEMODIFIED] = get_string('timemodified', 'data');
1448 $options[DATA_FIRSTNAME] = get_string('authorfirstname', 'data');
1449 $options[DATA_LASTNAME] = get_string('authorlastname', 'data');
bb5740f4 1450 if ($data->approval and has_capability('mod/data:approve', $context)) {
1451 $options[DATA_APPROVED] = get_string('approved', 'data');
1452 }
3239b010 1453 echo '<optgroup label="'.get_string('other', 'data').'">';
714bec74 1454 foreach ($options as $key => $name) {
1455 if ($key == $sort) {
1456 echo '<option value="'.$key.'" selected="selected">'.$name.'</option>';
cf3e199b 1457 } else {
714bec74 1458 echo '<option value="'.$key.'">'.$name.'</option>';
cf3e199b 1459 }
1460 }
3239b010 1461 echo '</optgroup>';
cf3e199b 1462 echo '</select>';
a032a460 1463 echo '<label for="pref_order" class="accesshide">'.get_string('order').'</label>';
1464 echo '<select id="pref_order" name="order">';
c8505cac 1465 if ($order == 'ASC') {
af25f45e 1466 echo '<option value="ASC" selected="selected">'.get_string('ascending','data').'</option>';
cf3e199b 1467 } else {
1468 echo '<option value="ASC">'.get_string('ascending','data').'</option>';
1469 }
c8505cac 1470 if ($order == 'DESC') {
af25f45e 1471 echo '<option value="DESC" selected="selected">'.get_string('descending','data').'</option>';
cf3e199b 1472 } else {
1473 echo '<option value="DESC">'.get_string('descending','data').'</option>';
1474 }
af25f45e 1475 echo '</select>';
86638489 1476
7900ecb0 1477 if ($advanced) {
1478 $checked = ' checked="checked" ';
1479 }
1480 else {
1481 $checked = '';
1482 }
9dec75db 1483 $PAGE->requires->js('/mod/data/data.js');
de8ff581 1484 echo '&nbsp;<input type="hidden" name="advanced" value="0" />';
d53e5129 1485 echo '&nbsp;<input type="hidden" name="filter" value="1" />';
714bec74 1486 echo '&nbsp;<input type="checkbox" id="advancedcheckbox" name="advanced" value="1" '.$checked.' onchange="showHideAdvSearch(this.checked);" /><label for="advancedcheckbox">'.get_string('advancedsearch', 'data').'</label>';
7900ecb0 1487 echo '&nbsp;<input type="submit" value="'.get_string('savesettings','data').'" />';
8429163d 1488
7900ecb0 1489 echo '<br />';
86638489 1490 echo '<div class="dataadvancedsearch" id="data_adv_form" style="display: ';
8429163d 1491
7900ecb0 1492 if ($advanced) {
1493 echo 'inline';
1494 }
1495 else {
1496 echo 'none';
1497 }
86638489 1498 echo ';margin-left:auto;margin-right:auto;" >';
7900ecb0 1499 echo '<table class="boxaligncenter">';
8429163d 1500
7900ecb0 1501 // print ASC or DESC
1502 echo '<tr><td colspan="2">&nbsp;</td></tr>';
1503 $i = 0;
1504
1505 // Determine if we are printing all fields for advanced search, or the template for advanced search
1506 // If a template is not defined, use the deafault template and display all fields.
1507 if(empty($data->asearchtemplate)) {
1508 data_generate_default_template($data, 'asearchtemplate');
1509 }
1510
1511 static $fields = NULL;
1512 static $isteacher;
1513 static $dataid = NULL;
b572ce19 1514
7900ecb0 1515 if (empty($dataid)) {
1516 $dataid = $data->id;
1517 } else if ($dataid != $data->id) {
1518 $fields = NULL;
1519 }
1520
1521 if (empty($fields)) {
9c00b5d7 1522 $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id));
7900ecb0 1523 foreach ($fieldrecords as $fieldrecord) {
1524 $fields[]= data_get_field($fieldrecord, $data);
1525 }
b572ce19 1526
7900ecb0 1527 $isteacher = has_capability('mod/data:managetemplates', $context);
1528 }
1529
b8b554ac 1530 // Replacing tags
7900ecb0 1531 $patterns = array();
1532 $replacement = array();
1533
b8b554ac 1534 // Then we generate strings to replace for normal tags
7900ecb0 1535 foreach ($fields as $field) {
98f67312 1536 $fieldname = $field->field->name;
1537 $fieldname = preg_quote($fieldname, '/');
1538 $patterns[] = "/\[\[$fieldname\]\]/i";
714bec74 1539 $searchfield = data_get_field_from_id($field->field->id, $data);
7900ecb0 1540 if (!empty($search_array[$field->field->id]->data)) {
1541 $replacement[] = $searchfield->display_search_field($search_array[$field->field->id]->data);
1542 } else {
1543 $replacement[] = $searchfield->display_search_field();
1544 }
1545 }
714bec74 1546 $fn = !empty($search_array[DATA_FIRSTNAME]->data) ? $search_array[DATA_FIRSTNAME]->data : '';
1547 $ln = !empty($search_array[DATA_LASTNAME]->data) ? $search_array[DATA_LASTNAME]->data : '';
1548 $patterns[] = '/##firstname##/';
bc16bd57 1549 $replacement[] = '<input type="text" size="16" name="u_fn" value="'.$fn.'" />';
714bec74 1550 $patterns[] = '/##lastname##/';
bc16bd57 1551 $replacement[] = '<input type="text" size="16" name="u_ln" value="'.$ln.'" />';
714bec74 1552
668fc89a 1553 // actual replacement of the tags
7900ecb0 1554 $newtext = preg_replace($patterns, $replacement, $data->asearchtemplate);
b572ce19 1555
055f2185 1556 $options = new object();
7900ecb0 1557 $options->para=false;
1558 $options->noclean=true;
1559 echo '<tr><td>';
1560 echo format_text($newtext, FORMAT_HTML, $options);
1561 echo '</td></tr>';
b572ce19 1562
eeeb4f2a 1563 echo '<tr><td colspan="4" style="text-align: center;"><br/><input type="submit" value="'.get_string('savesettings','data').'" /><input type="submit" name="resetadv" value="'.get_string('resetsettings','data').'" /></td></tr>';
7900ecb0 1564 echo '</table>';
c8505cac 1565 echo '</div>';
7900ecb0 1566 echo '</div>';
1567 echo '</form>';
b8b554ac 1568 echo '</div>';
3d4b223a 1569}
5f5bcda8 1570
4636bf83 1571/**
1572 * @global object
1573 * @global object
1574 * @param object $data
1575 * @param object $record
1576 * @return void Output echo'd
1577 */
4d3d87f0 1578function data_print_ratings($data, $record) {
56c1ca88 1579 global $OUTPUT;
d251b259
AD
1580 if( !empty($record->rating) ){
1581 echo $OUTPUT->render($record->rating);
4d3d87f0 1582 }
4d3d87f0 1583}
1584
4636bf83 1585/**
1586 * For Participantion Reports
1587 *
1588 * @return array
1589 */
4c03f920 1590function data_get_view_actions() {
1591 return array('view');
1592}
1593
4636bf83 1594/**
1595 * @return array
1596 */
4c03f920 1597function data_get_post_actions() {
1598 return array('add','update','record delete');
1599}
1600
4636bf83 1601/**
1602 * @global object
1603 * @global object
1604 * @param string $name
1605 * @param int $dataid
1606 * @param int $fieldid
1607 * @return bool
1608 */
0997e51a 1609function data_fieldname_exists($name, $dataid, $fieldid=0) {
9c00b5d7 1610 global $CFG, $DB;
0997e51a 1611
fdeb3e1f
AB
1612 if(!is_numeric($name)) {
1613 $like = $DB->sql_like('df.name', $name, false);
1614 } else {
1615 $like = "df.name = $name";
1616 }
6403e679 1617 if ($fieldid) {
9c00b5d7 1618 return $DB->record_exists_sql("SELECT * FROM {data_fields} df
fdeb3e1f
AB
1619 WHERE ".$like." AND df.dataid = ?
1620 AND ((df.id < ?) OR (df.id > ?))", array($dataid, $fieldid, $fieldid));
0997e51a 1621 } else {
9c00b5d7 1622 return $DB->record_exists_sql("SELECT * FROM {data_fields} df
fdeb3e1f 1623 WHERE ".$like." AND df.dataid = ?", array($dataid));
0997e51a 1624 }
1625}
1626
4636bf83 1627/**
1628 * @param array $fieldinput
1629 */
0997e51a 1630function data_convert_arrays_to_strings(&$fieldinput) {
1631 foreach ($fieldinput as $key => $val) {
1632 if (is_array($val)) {
1633 $str = '';
1634 foreach ($val as $inner) {
1635 $str .= $inner . ',';
1636 }
1637 $str = substr($str, 0, -1);
b572ce19 1638
0997e51a 1639 $fieldinput->$key = $str;
1640 }
1641 }
1642}
1643
901dd2fb 1644
7f258664 1645/**
1646 * Converts a database (module instance) to use the Roles System
4636bf83 1647 *
1648 * @global object
1649 * @global object
1650 * @uses CONTEXT_MODULE
1651 * @uses CAP_PREVENT
1652 * @uses CAP_ALLOW
1653 * @param object $data a data object with the same attributes as a record
1654 * from the data database table
1655 * @param int $datamodid the id of the data module, from the modules table
4f0c2d00
PS
1656 * @param array $teacherroles array of roles that have archetype teacher
1657 * @param array $studentroles array of roles that have archetype student
1658 * @param array $guestroles array of roles that have archetype guest
4636bf83 1659 * @param int $cmid the course_module id for this data instance
1660 * @return boolean data module was converted or not
7f258664 1661 */
1662function data_convert_to_roles($data, $teacherroles=array(), $studentroles=array(), $cmid=NULL) {
4102b449 1663 global $CFG, $DB, $OUTPUT;
6403e679 1664
7f258664 1665 if (!isset($data->participants) && !isset($data->assesspublic)
1666 && !isset($data->groupmode)) {
1667 // We assume that this database has already been converted to use the
1668 // Roles System. above fields get dropped the data module has been
1669 // upgraded to use Roles.
1670 return false;
1671 }
b572ce19 1672
7f258664 1673 if (empty($cmid)) {
1674 // We were not given the course_module id. Try to find it.
741c4d0b 1675 if (!$cm = get_coursemodule_from_instance('data', $data->id)) {
4102b449 1676 echo $OUTPUT->notification('Could not get the course module for the data');
7f258664 1677 return false;
1678 } else {
1679 $cmid = $cm->id;
1680 }
1681 }
1682 $context = get_context_instance(CONTEXT_MODULE, $cmid);
6403e679 1683
b572ce19 1684
7f258664 1685 // $data->participants:
1686 // 1 - Only teachers can add entries
1687 // 3 - Teachers and students can add entries
1688 switch ($data->participants) {
1689 case 1:
1690 foreach ($studentroles as $studentrole) {
1691 assign_capability('mod/data:writeentry', CAP_PREVENT, $studentrole->id, $context->id);
1692 }
1693 foreach ($teacherroles as $teacherrole) {
1694 assign_capability('mod/data:writeentry', CAP_ALLOW, $teacherrole->id, $context->id);
1695 }
1696 break;
1697 case 3:
1698 foreach ($studentroles as $studentrole) {
1699 assign_capability('mod/data:writeentry', CAP_ALLOW, $studentrole->id, $context->id);
1700 }
1701 foreach ($teacherroles as $teacherrole) {
1702 assign_capability('mod/data:writeentry', CAP_ALLOW, $teacherrole->id, $context->id);
1703 }
1704 break;
1705 }
6403e679 1706
7f258664 1707 // $data->assessed:
1708 // 2 - Only teachers can rate posts
1709 // 1 - Everyone can rate posts
1710 // 0 - No one can rate posts
1711 switch ($data->assessed) {
1712 case 0:
1713 foreach ($studentroles as $studentrole) {
1714 assign_capability('mod/data:rate', CAP_PREVENT, $studentrole->id, $context->id);
1715 }
1716 foreach ($teacherroles as $teacherrole) {
1717 assign_capability('mod/data:rate', CAP_PREVENT, $teacherrole->id, $context->id);
1718 }
1719 break;
1720 case 1:
1721 foreach ($studentroles as $studentrole) {
1722 assign_capability('mod/data:rate', CAP_ALLOW, $studentrole->id, $context->id);
1723 }
1724 foreach ($teacherroles as $teacherrole) {
1725 assign_capability('mod/data:rate', CAP_ALLOW, $teacherrole->id, $context->id);
1726 }
1727 break;
1728 case 2:
1729 foreach ($studentroles as $studentrole) {
1730 assign_capability('mod/data:rate', CAP_PREVENT, $studentrole->id, $context->id);
1731 }
1732 foreach ($teacherroles as $teacherrole) {
1733 assign_capability('mod/data:rate', CAP_ALLOW, $teacherrole->id, $context->id);
1734 }
1735 break;
1736 }
6403e679 1737
7f258664 1738 // $data->assesspublic:
1739 // 0 - Students can only see their own ratings
1740 // 1 - Students can see everyone's ratings
1741 switch ($data->assesspublic) {
1742 case 0:
1743 foreach ($studentroles as $studentrole) {
1744 assign_capability('mod/data:viewrating', CAP_PREVENT, $studentrole->id, $context->id);
1745 }
1746 foreach ($teacherroles as $teacherrole) {
1747 assign_capability('mod/data:viewrating', CAP_ALLOW, $teacherrole->id, $context->id);
1748 }
1749 break;
1750 case 1:
1751 foreach ($studentroles as $studentrole) {
1752 assign_capability('mod/data:viewrating', CAP_ALLOW, $studentrole->id, $context->id);
1753 }
1754 foreach ($teacherroles as $teacherrole) {
1755 assign_capability('mod/data:viewrating', CAP_ALLOW, $teacherrole->id, $context->id);
1756 }
1757 break;
1758 }
1759
1760 if (empty($cm)) {
9c00b5d7 1761 $cm = $DB->get_record('course_modules', array('id'=>$cmid));
7f258664 1762 }
6403e679 1763
7f258664 1764 switch ($cm->groupmode) {
055f2185 1765 case NOGROUPS:
7f258664 1766 break;
055f2185 1767 case SEPARATEGROUPS:
7f258664 1768 foreach ($studentroles as $studentrole) {
1769 assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $studentrole->id, $context->id);
1770 }
1771 foreach ($teacherroles as $teacherrole) {
1772 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $teacherrole->id, $context->id);
1773 }
1774 break;
055f2185 1775 case VISIBLEGROUPS:
7f258664 1776 foreach ($studentroles as $studentrole) {
1777 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $studentrole->id, $context->id);
1778 }
1779 foreach ($teacherroles as $teacherrole) {
1780 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $teacherrole->id, $context->id);
1781 }
1782 break;
1783 }
1784 return true;
1785}
1786
4636bf83 1787/**
8303eb84 1788 * Returns the best name to show for a preset
4636bf83 1789 *
1790 * @param string $shortname
1791 * @param string $path
1792 * @return string
8303eb84 1793 */
1794function data_preset_name($shortname, $path) {
1795
668fc89a 1796 // We are looking inside the preset itself as a first choice, but also in normal data directory
30d76014 1797 $string = get_string('modulename', 'datapreset_'.$shortname);
8303eb84 1798
1799 if (substr($string, 0, 1) == '[') {
1800 return $shortname;
1801 } else {
1802 return $string;
1803 }
1804}
1805
4636bf83 1806/**
8aff1574 1807 * Returns an array of all the available presets.
4636bf83 1808 *
4636bf83 1809 * @return array
8303eb84 1810 */
1811function data_get_available_presets($context) {
1812 global $CFG, $USER;
b572ce19 1813
8303eb84 1814 $presets = array();
b572ce19 1815
8aff1574 1816 // First load the ratings sub plugins that exist within the modules preset dir
8303eb84 1817 if ($dirs = get_list_of_plugins('mod/data/preset')) {
1818 foreach ($dirs as $dir) {
1819 $fulldir = $CFG->dirroot.'/mod/data/preset/'.$dir;
8303eb84 1820 if (is_directory_a_preset($fulldir)) {
1821 $preset = new object;
1822 $preset->path = $fulldir;
1823 $preset->userid = 0;
1824 $preset->shortname = $dir;
1825 $preset->name = data_preset_name($dir, $fulldir);
1826 if (file_exists($fulldir.'/screenshot.jpg')) {
1827 $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.jpg';
1828 } else if (file_exists($fulldir.'/screenshot.png')) {
1829 $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.png';
1830 } else if (file_exists($fulldir.'/screenshot.gif')) {
1831 $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.gif';
1832 }
1833 $presets[] = $preset;
1834 }
1835 }
1836 }
8aff1574
SH
1837 // Now add to that the site presets that people have saved
1838 $presets = data_get_available_site_presets($context, $presets);
1839 return $presets;
1840}
8303eb84 1841
8aff1574
SH
1842/**
1843 * Gets an array of all of the presets that users have saved to the site.
1844 *
1845 * @param stdClass $context The context that we are looking from.
1846 * @param array $presets
1847 * @return array An array of presets
1848 */
1849function data_get_available_site_presets($context, array $presets=array()) {
810860d2
PS
1850 global $USER;
1851
8aff1574
SH
1852 $fs = get_file_storage();
1853 $files = $fs->get_area_files(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA);
1854 $canviewall = has_capability('mod/data:viewalluserpresets', $context);
1855 if (empty($files)) {
1856 return $presets;
1857 }
1858 foreach ($files as $file) {
1859 if (($file->is_directory() && $file->get_filepath()=='/') || !$file->is_directory() || (!$canviewall && $file->get_userid() != $USER->id)) {
1860 continue;
1861 }
1862 $preset = new stdClass;
1863 $preset->path = $file->get_filepath();
1864 $preset->name = trim($preset->path, '/');
1865 $preset->shortname = $preset->name;
1866 $preset->userid = $file->get_userid();
1867 $preset->id = $file->get_id();
1868 $preset->storedfile = $file;
1869 $presets[] = $preset;
1870 }
1871 return $presets;
1872}
1873
1874/**
1875 * Deletes a saved preset.
1876 *
1877 * @param string $name
1878 * @return bool
1879 */
1880function data_delete_site_preset($name) {
1881 $fs = get_file_storage();
1882
1883 $files = $fs->get_directory_files(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA, 0, '/'.$name.'/');
1884 if (!empty($files)) {
1885 foreach ($files as $file) {
1886 $file->delete();
8303eb84 1887 }
1888 }
b572ce19 1889
8aff1574
SH
1890 $dir = $fs->get_file(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA, 0, '/'.$name.'/', '.');
1891 if (!empty($dir)) {
1892 $dir->delete();
1893 }
1894 return true;
8303eb84 1895}
1896
4636bf83 1897/**
8aff1574
SH
1898 * Prints the heads for a page
1899 *
1900 * @param stdClass $course
1901 * @param stdClass $cm
1902 * @param stdClass $data
4636bf83 1903 * @param string $currenttab
1904 */
8303eb84 1905function data_print_header($course, $cm, $data, $currenttab='') {
b572ce19 1906
b0ff558c 1907 global $CFG, $displaynoticegood, $displaynoticebad, $OUTPUT, $PAGE;
b572ce19 1908
b0ff558c 1909 $PAGE->set_title($data->name);
b0ff558c 1910 echo $OUTPUT->header();
b2dc6880 1911 echo $OUTPUT->heading(format_string($data->name));
8303eb84 1912
b8b554ac 1913// Groups needed for Add entry tab
055f2185 1914 $currentgroup = groups_get_activity_group($cm);
5d59cbe9 1915 $groupmode = groups_get_activity_groupmode($cm);
b572ce19 1916
668fc89a 1917 // Print the tabs
b572ce19 1918
8303eb84 1919 if ($currenttab) {
138e480e 1920 include('tabs.php');
8303eb84 1921 }
b572ce19 1922
b8b554ac 1923 // Print any notices
b572ce19 1924
8303eb84 1925 if (!empty($displaynoticegood)) {
4102b449 1926 echo $OUTPUT->notification($displaynoticegood, 'notifysuccess'); // good (usually green)
8303eb84 1927 } else if (!empty($displaynoticebad)) {
4102b449 1928 echo $OUTPUT->notification($displaynoticebad); // bad (usuually red)
8303eb84 1929 }
1930}
7f258664 1931
4636bf83 1932/**
1933 * @global object
1934 * @param object $data
1935 * @param mixed $currentgroup
1936 * @param int $groupmode
1937 * @return bool
1938 */
04366d79 1939function data_user_can_add_entry($data, $currentgroup, $groupmode) {
cca1547e 1940 global $USER;
b572ce19 1941
cca1547e 1942 if (!$cm = get_coursemodule_from_instance('data', $data->id)) {
29c1bb15 1943 print_error('invalidcoursemodule');
cca1547e 1944 }
1945 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
b572ce19 1946
cca1547e 1947 if (!has_capability('mod/data:writeentry', $context) and !has_capability('mod/data:manageentries',$context)) {
1948 return false;
1949 }
b572ce19 1950
2742ffe7
AD
1951 //if in the view only time window
1952 $now = time();
1953 if ($now>$data->timeviewfrom && $now<$data->timeviewto) {
1954 return false;
1955 }
1956
04366d79 1957 if (!$groupmode or has_capability('moodle/site:accessallgroups', $context)) {
1958 return true;
1959 }
1960
cca1547e 1961 if ($currentgroup) {
2c386f82 1962 return groups_is_member($currentgroup);
cca1547e 1963 } else {
1964 //else it might be group 0 in visible mode
1965 if ($groupmode == VISIBLEGROUPS){
cca1547e 1966 return true;
04366d79 1967 } else {
1968 return false;
cca1547e 1969 }
1970 }
1971}
1972
50194545 1973
4636bf83 1974/**
1975 * @return bool
1976 */
50194545 1977function is_directory_a_preset($directory) {
1978 $directory = rtrim($directory, '/\\') . '/';
2dc6be3f 1979 $status = file_exists($directory.'singletemplate.html') &&
1980 file_exists($directory.'listtemplate.html') &&
1981 file_exists($directory.'listtemplateheader.html') &&
1982 file_exists($directory.'listtemplatefooter.html') &&
1983 file_exists($directory.'addtemplate.html') &&
1984 file_exists($directory.'rsstemplate.html') &&
1985 file_exists($directory.'rsstitletemplate.html') &&
1986 file_exists($directory.'csstemplate.css') &&
1987 file_exists($directory.'jstemplate.js') &&
1988 file_exists($directory.'preset.xml');
b572ce19 1989
2dc6be3f 1990 return $status;
50194545 1991}
1992
4636bf83 1993/**
cba87c36 1994 * Abstract class used for data preset importers
4636bf83 1995 */
cba87c36 1996abstract class data_preset_importer {
b572ce19 1997
cba87c36
SH
1998 protected $course;
1999 protected $cm;
2000 protected $module;
2001 protected $directory;
50194545 2002
4636bf83 2003 /**
cba87c36
SH
2004 * Constructor
2005 *
2006 * @param stdClass $course
2007 * @param stdClass $cm
2008 * @param stdClass $module
2009 * @param string $directory
4636bf83 2010 */
cba87c36 2011 public function __construct($course, $cm, $module, $directory) {
50194545 2012 $this->course = $course;
2013 $this->cm = $cm;
cba87c36
SH
2014 $this->module = $module;
2015 $this->directory = $directory;
2016 }
2017
2018 /**
2019 * Returns the name of the directory the preset is located in
2020 * @return string
2021 */
2022 public function get_directory() {
2023 return basename($this->directory);
50194545 2024 }
4636bf83 2025 /**
cba87c36
SH
2026 * Gets the preset settings
2027 * @global moodle_database $DB
2028 * @return stdClass
4636bf83 2029 */
cba87c36
SH
2030 public function get_preset_settings() {
2031 global $DB;
50194545 2032
cba87c36
SH
2033 if (!is_directory_a_preset($this->directory)) {
2034 print_error('invalidpreset', 'data', '', $this->directory);
50194545 2035 }
2036
cba87c36
SH
2037 $allowed_settings = array(
2038 'intro',
2039 'comments',
2040 'requiredentries',
2041 'requiredentriestoview',
2042 'maxentries',
2043 'rssarticles',
2044 'approval',
2045 'defaultsortdir',
2046 'defaultsort');
2047
2048 $result = new stdClass;
2049 $result->settings = new stdClass;
2050 $result->importfields = array();
2051 $result->currentfields = $DB->get_records('data_fields', array('dataid'=>$this->module->id));
2052 if (!$result->currentfields) {
2053 $result->currentfields = array();
2054 }
2055
2056
50194545 2057 /* Grab XML */
cba87c36 2058 $presetxml = file_get_contents($this->directory.'/preset.xml');
143c1eb9 2059 $parsedxml = xmlize($presetxml, 0);
50194545 2060
2061 /* First, do settings. Put in user friendly array. */
2062 $settingsarray = $parsedxml['preset']['#']['settings'][0]['#'];
cba87c36 2063 $result->settings = new StdClass();
50194545 2064 foreach ($settingsarray as $setting => $value) {
cba87c36 2065 if (!is_array($value) || !in_array($setting, $allowed_settings)) {
eaa30818 2066 // unsupported setting
2067 continue;
2068 }
cba87c36 2069 $result->settings->$setting = $value[0]['#'];
50194545 2070 }
2071
2072 /* Now work out fields to user friendly array */
2073 $fieldsarray = $parsedxml['preset']['#']['field'];
50194545 2074 foreach ($fieldsarray as $field) {
02007b01 2075 if (!is_array($field)) {
2076 continue;
2077 }
50194545 2078 $f = new StdClass();
2079 foreach ($field['#'] as $param => $value) {
02007b01 2080 if (!is_array($value)) {
2081 continue;
2082 }
9c00b5d7 2083 $f->$param = $value[0]['#'];
50194545 2084 }
cba87c36 2085 $f->dataid = $this->module->id;
50194545 2086 $f->type = clean_param($f->type, PARAM_ALPHA);
cba87c36 2087 $result->importfields[] = $f;
50194545 2088 }
50194545 2089 /* Now add the HTML templates to the settings array so we can update d */
cba87c36
SH
2090 $result->settings->singletemplate = file_get_contents($this->directory."/singletemplate.html");
2091 $result->settings->listtemplate = file_get_contents($this->directory."/listtemplate.html");
2092 $result->settings->listtemplateheader = file_get_contents($this->directory."/listtemplateheader.html");
2093 $result->settings->listtemplatefooter = file_get_contents($this->directory."/listtemplatefooter.html");
2094 $result->settings->addtemplate = file_get_contents($this->directory."/addtemplate.html");
2095 $result->settings->rsstemplate = file_get_contents($this->directory."/rsstemplate.html");
2096 $result->settings->rsstitletemplate = file_get_contents($this->directory."/rsstitletemplate.html");
2097 $result->settings->csstemplate = file_get_contents($this->directory."/csstemplate.css");
2098 $result->settings->jstemplate = file_get_contents($this->directory."/jstemplate.js");
50194545 2099
2dc6be3f 2100 //optional
cba87c36
SH
2101 if (file_exists($this->directory."/asearchtemplate.html")) {
2102 $result->settings->asearchtemplate = file_get_contents($this->directory."/asearchtemplate.html");
2dc6be3f 2103 } else {
cba87c36 2104 $result->settings->asearchtemplate = NULL;
50194545 2105 }
cba87c36 2106 $result->settings->instance = $this->module->id;
b572ce19 2107
cba87c36 2108 return $result;
50194545 2109 }
2110
4636bf83 2111 /**
cba87c36 2112 * Import the preset into the given database module
4636bf83 2113 * @return bool
2114 */
cba87c36
SH
2115 function import($overwritesettings) {
2116 global $DB;
50194545 2117
cba87c36
SH
2118 $params = $this->get_preset_settings();
2119 $settings = $params->settings;
2120 $newfields = $params->importfields;
2121 $currentfields = $params->currentfields;
50194545 2122 $preservedfields = array();
b572ce19 2123
50194545 2124 /* Maps fields and makes new ones */
2125 if (!empty($newfields)) {
2126 /* We require an injective mapping, and need to know what to protect */
2127 foreach ($newfields as $nid => $newfield) {
2128 $cid = optional_param("field_$nid", -1, PARAM_INT);
cba87c36
SH
2129 if ($cid == -1) {
2130 continue;
2131 }
29c1bb15 2132 if (array_key_exists($cid, $preservedfields)){
2133 print_error('notinjectivemap', 'data');
8429163d 2134 }
50194545 2135 else $preservedfields[$cid] = true;
2136 }
b572ce19 2137
50194545 2138 foreach ($newfields as $nid => $newfield) {
2139 $cid = optional_param("field_$nid", -1, PARAM_INT);
b572ce19 2140
50194545 2141 /* A mapping. Just need to change field params. Data kept. */
2142 if ($cid != -1 and isset($currentfields[$cid])) {
cba87c36 2143 $fieldobject = data_get_field_from_id($currentfields[$cid]->id, $this->module);
50194545 2144 foreach ($newfield as $param => $value) {
2145 if ($param != "id") {
2146 $fieldobject->field->$param = $value;
2147 }
2148 }
2149 unset($fieldobject->field->similarfield);
2150 $fieldobject->update_field();
2151 unset($fieldobject);
cba87c36
SH
2152 } else {
2153 /* Make a new field */
50194545 2154 include_once("field/$newfield->type/field.class.php");
b572ce19 2155
50194545 2156 if (!isset($newfield->description)) {
2157 $newfield->description = '';
2158 }
2159 $classname = 'data_field_'.$newfield->type;
cba87c36 2160 $fieldclass = new $classname($newfield, $this->module);
50194545 2161 $fieldclass->insert_field();
2162 unset($fieldclass);
2163 }
2164 }
2165 }
2166
2167 /* Get rid of all old unused data */
2168 if (!empty($preservedfields)) {
2169 foreach ($currentfields as $cid => $currentfield) {
2170 if (!array_key_exists($cid, $preservedfields)) {
2171 /* Data not used anymore so wipe! */
2172 print "Deleting field $currentfield->name<br />";
b572ce19 2173
50194545 2174 $id = $currentfield->id;
2175 //Why delete existing data records and related comments/ratings??
9c00b5d7 2176 $DB->delete_records('data_content', array('fieldid'=>$id));
2177 $DB->delete_records('data_fields', array('id'=>$id));
50194545 2178 }
2179 }
2180 }
2181
cba87c36 2182 // handle special settings here
eaa30818 2183 if (!empty($settings->defaultsort)) {
2184 if (is_numeric($settings->defaultsort)) {
668fc89a 2185 // old broken value
eaa30818 2186 $settings->defaultsort = 0;
2187 } else {
cba87c36 2188 $settings->defaultsort = (int)$DB->get_field('data_fields', 'id', array('dataid'=>$this->module->id, 'name'=>$settings->defaultsort));
eaa30818 2189 }
2190 } else {
2191 $settings->defaultsort = 0;
2192 }
2193
2194 // do we want to overwrite all current database settings?
8528bf26 2195 if ($overwritesettings) {
eaa30818 2196 // all supported settings
8528bf26 2197 $overwrite = array_keys((array)$settings);
2198 } else {
eaa30818 2199 // only templates and sorting
8528bf26 2200 $overwrite = array('singletemplate', 'listtemplate', 'listtemplateheader', 'listtemplatefooter',
2201 'addtemplate', 'rsstemplate', 'rsstitletemplate', 'csstemplate', 'jstemplate',
eaa30818 2202 'asearchtemplate', 'defaultsortdir', 'defaultsort');
8528bf26 2203 }
2204
eaa30818 2205 // now overwrite current data settings
cba87c36 2206 foreach ($this->module as $prop=>$unused) {
8528bf26 2207 if (in_array($prop, $overwrite)) {
cba87c36 2208 $this->module->$prop = $settings->$prop;
13534ef7
ML
2209 }
2210 }
db0f2a44 2211
cba87c36 2212 data_update_instance($this->module);
50194545 2213
cba87c36
SH
2214 return $this->cleanup();
2215 }
b572ce19 2216
cba87c36
SH
2217 /**
2218 * Any clean up routines should go here
2219 * @return bool
2220 */
2221 public function cleanup() {
50194545 2222 return true;
2223 }
2224}
2225
cba87c36
SH
2226/**
2227 * Data preset importer for uploaded presets
2228 */
2229class data_preset_upload_importer extends data_preset_importer {
2230 public function __construct($course, $cm, $module, $filepath) {
2231 global $USER;
2232 if (is_file($filepath)) {
2233 $fp = get_file_packer();
2234 if ($fp->extract_to_pathname($filepath, $filepath.'_extracted')) {
2235 fulldelete($filepath);
2236 }
2237 $filepath .= '_extracted';
2238 }
2239 parent::__construct($course, $cm, $module, $filepath);
2240 }
2241 public function cleanup() {
2242 return fulldelete($this->directory);
2243 }
2244}
2245
2246/**
2247 * Data preset importer for existing presets
2248 */
2249class data_preset_existing_importer extends data_preset_importer {
2250 protected $userid;
2251 public function __construct($course, $cm, $module, $fullname) {
2252 global $USER;
2253 list($userid, $shortname) = explode('/', $fullname, 2);
2254 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
2255 if ($userid && ($userid != $USER->id) && !has_capability('mod/data:manageuserpresets', $context) && !has_capability('mod/data:viewalluserpresets', $context)) {
2256 throw new coding_exception('Invalid preset provided');
2257 }
2258
2259 $this->userid = $userid;
2260 $filepath = data_preset_path($course, $userid, $shortname);
2261 parent::__construct($course, $cm, $module, $filepath);
2262 }
2263 public function get_userid() {
2264 return $this->userid;
2265 }
2266}
2267
4636bf83 2268/**
2269 * @global object
2270 * @global object
2271 * @param object $course
2272 * @param int $userid
2273 * @param string $shortname
2274 * @return string
2275 */
50194545 2276function data_preset_path($course, $userid, $shortname) {
2277 global $USER, $CFG;
b572ce19 2278
50194545 2279 $context = get_context_instance(CONTEXT_COURSE, $course->id);
b572ce19 2280
50194545 2281 $userid = (int)$userid;
b572ce19 2282
50194545 2283 if ($userid > 0 && ($userid == $USER->id || has_capability('mod/data:viewalluserpresets', $context))) {
2284 return $CFG->dataroot.'/data/preset/'.$userid.'/'.$shortname;
2285 } else if ($userid == 0) {
2286 return $CFG->dirroot.'/mod/data/preset/'.$shortname;
2287 } else if ($userid < 0) {
2288 return $CFG->dataroot.'/temp/data/'.-$userid.'/'.$shortname;
2289 }
b572ce19 2290
50194545 2291 return 'Does it disturb you that this code will never run?';
2292}
0b5a80a1 2293
2294/**
2295 * Implementation of the function for printing the form elements that control
2296 * whether the course reset functionality affects the data.
4636bf83 2297 *
0b5a80a1 2298 * @param $mform form passed by reference
2299 */
2300function data_reset_course_form_definition(&$mform) {
2301 $mform->addElement('header', 'dataheader', get_string('modulenameplural', 'data'));
2302 $mform->addElement('checkbox', 'reset_data', get_string('deleteallentries','data'));
2303
2304 $mform->addElement('checkbox', 'reset_data_notenrolled', get_string('deletenotenrolled', 'data'));
2305 $mform->disabledIf('reset_data_notenrolled', 'reset_data', 'checked');
2306
2307 $mform->addElement('checkbox', 'reset_data_ratings', get_string('deleteallratings'));
2308 $mform->disabledIf('reset_data_ratings', 'reset_data', 'checked');
2309
2310 $mform->addElement('checkbox', 'reset_data_comments', get_string('deleteallcomments'));
2311 $mform->disabledIf('reset_data_comments', 'reset_data', 'checked');
2312}
2313
2314/**
2315 * Course reset form defaults.
4636bf83 2316 * @return array
0b5a80a1 2317 */
2318function data_reset_course_form_defaults($course) {
2319 return array('reset_data'=>0, 'reset_data_ratings'=>1, 'reset_data_comments'=>1, 'reset_data_notenrolled'=>0);
2320}
2321
2322/**
2323 * Removes all grades from gradebook
4636bf83 2324 *
2325 * @global object
2326 * @global object
0b5a80a1 2327 * @param int $courseid
4636bf83 2328 * @param string $type optional type
0b5a80a1 2329 */
2330function data_reset_gradebook($courseid, $type='') {
9c00b5d7 2331 global $CFG, $DB;
0b5a80a1 2332
2333 $sql = "SELECT d.*, cm.idnumber as cmidnumber, d.course as courseid
9c00b5d7 2334 FROM {data} d, {course_modules} cm, {modules} m
2335 WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id AND d.course=?";
0b5a80a1 2336
9c00b5d7 2337 if ($datas = $DB->get_records_sql($sql, array($courseid))) {
0b5a80a1 2338 foreach ($datas as $data) {
2339 data_grade_item_update($data, 'reset');
2340 }
2341 }
2342}
2343
2344/**
72d2982e 2345 * Actual implementation of the reset course functionality, delete all the
0b5a80a1 2346 * data responses for course $data->courseid.
4636bf83 2347 *
2348 * @global object
2349 * @global object
2350 * @param object $data the data submitted from the reset course.
0b5a80a1 2351 * @return array status array
2352 */
2353function data_reset_userdata($data) {
9c00b5d7 2354 global $CFG, $DB;
0b5a80a1 2355 require_once($CFG->libdir.'/filelib.php');
d251b259 2356 require_once($CFG->dirroot.'/rating/lib.php');
b572ce19 2357
0b5a80a1 2358 $componentstr = get_string('modulenameplural', 'data');
2359 $status = array();
b572ce19 2360
0b5a80a1 2361 $allrecordssql = "SELECT r.id
9c00b5d7 2362 FROM {data_records} r
2363 INNER JOIN {data} d ON r.dataid = d.id
2364 WHERE d.course = ?";
0b5a80a1 2365
2366 $alldatassql = "SELECT d.id
9c00b5d7 2367 FROM {data} d
2368 WHERE d.course=?";
0b5a80a1 2369
d251b259
AD
2370 $rm = new rating_manager();
2371 $ratingdeloptions = new stdclass();
2372
0b5a80a1 2373 // delete entries if requested
2374 if (!empty($data->reset_data)) {
d251b259 2375 //$DB->delete_records_select('data_ratings', "recordid IN ($allrecordssql)", array($data->courseid));
e998effa 2376 $DB->delete_records_select('comments', "itemid IN ($allrecordssql) AND commentarea='database_entry'", array($data->courseid));
9c00b5d7 2377 $DB->delete_records_select('data_content', "recordid IN ($allrecordssql)", array($data->courseid));
2378 $DB->delete_records_select('data_records', "dataid IN ($alldatassql)", array($data->courseid));
0b5a80a1 2379
9c00b5d7 2380 if ($datas = $DB->get_records_sql($alldatassql, array($data->courseid))) {
0b5a80a1 2381 foreach ($datas as $dataid=>$unused) {
2382 fulldelete("$CFG->dataroot/$data->courseid/moddata/data/$dataid");
d251b259
AD
2383
2384 if (!$cm = get_coursemodule_from_instance('data', $dataid)) {
2385 continue;
2386 }
2387 $datacontext = get_context_instance(CONTEXT_MODULE, $cm->id);
2388
2389 $ratingdeloptions->contextid = $datacontext->id;
2390 $rm->delete_ratings($ratingdeloptions);
0b5a80a1 2391 }
2392 }
b572ce19 2393
0b5a80a1 2394 if (empty($data->reset_gradebook_grades)) {
2395 // remove all grades from gradebook
2396 data_reset_gradebook($data->courseid);
2397 }
2398 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallentries', 'data'), 'error'=>false);
2399 }
2400
2401 // remove entries by users not enrolled into course
2402 if (!empty($data->reset_data_notenrolled)) {
2403 $recordssql = "SELECT r.id, r.userid, r.dataid, u.id AS userexists, u.deleted AS userdeleted
9c00b5d7 2404 FROM {data_records} r
2405 JOIN {data} d ON r.dataid = d.id
2406 LEFT JOIN {user} u ON r.userid = u.id
2407 WHERE d.course = ? AND r.userid > 0";
0b5a80a1 2408
2409 $course_context = get_context_instance(CONTEXT_COURSE, $data->courseid);
2410 $notenrolled = array();
2411 $fields = array();
9c00b5d7 2412 if ($rs = $DB->get_recordset_sql($recordssql, array($data->courseid))) {
2413 foreach ($rs as $record) {
0b5a80a1 2414 if (array_key_exists($record->userid, $notenrolled) or !$record->userexists or $record->userdeleted
4f0c2d00 2415 or !is_enrolled($course_context, $record->userid)) {
d251b259
AD
2416 //delete ratings
2417 //$DB->delete_records('data_ratings', array('recordid'=>$record->id));
2418 if (!$cm = get_coursemodule_from_instance('data', $record->dataid)) {
2419 continue;
2420 }
2421 $datacontext = get_context_instance(CONTEXT_MODULE, $cm->id);
2422 $ratingdeloptions->contextid = $datacontext->id;
2423 $ratingdeloptions->itemid = $record->id;
2424 $rm->delete_ratings($ratingdeloptions);
2425
e998effa 2426 $DB->delete_records('comments', array('itemid'=>$record->id, 'commentarea'=>'database_entry'));
9c00b5d7 2427 $DB->delete_records('data_content', array('recordid'=>$record->id));
2428 $DB->delete_records('data_records', array('id'=>$record->id));
0b5a80a1 2429 // HACK: this is ugly - the recordid should be before the fieldid!
2430 if (!array_key_exists($record->dataid, $fields)) {
9c00b5d7 2431 if ($fs = $DB->get_records('data_fields', array('dataid'=>$record->dataid))) {
0b5a80a1 2432 $fields[$record->dataid] = array_keys($fs);
2433 } else {
2434 $fields[$record->dataid] = array();
2435 }
2436 }
2437 foreach($fields[$record->dataid] as $fieldid) {
2438 fulldelete("$CFG->dataroot/$data->courseid/moddata/data/$record->dataid/$fieldid/$record->id");
2439 }
2440 $notenrolled[$record->userid] = true;
2441 }
2442 }
9c00b5d7 2443 $rs->close();
0b5a80a1 2444 $status[] = array('component'=>$componentstr, 'item'=>get_string('deletenotenrolled', 'data'), 'error'=>false);
2445 }
2446 }
2447
2448 // remove all ratings
2449 if (!empty($data->reset_data_ratings)) {
d251b259
AD
2450 //$DB->delete_records_select('data_ratings', "recordid IN ($allrecordssql)", array($data->courseid));
2451 if ($datas = $DB->get_records_sql($alldatassql, array($data->courseid))) {
2452 foreach ($datas as $dataid=>$unused) {
2453 if (!$cm = get_coursemodule_from_instance('data', $dataid)) {
2454 continue;
2455 }
2456 $datacontext = get_context_instance(CONTEXT_MODULE, $cm->id);
2457
2458 $ratingdeloptions->contextid = $datacontext->id;
2459 $rm->delete_ratings($ratingdeloptions);
2460 }
2461 }
0b5a80a1 2462
2463 if (empty($data->reset_gradebook_grades)) {
2464 // remove all grades from gradebook
2465 data_reset_gradebook($data->courseid);
2466 }
b572ce19 2467
0b5a80a1 2468 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallratings'), 'error'=>false);
2469 }
2470
2471 // remove all comments
2472 if (!empty($data->reset_data_comments)) {
e998effa 2473 $DB->delete_records_select('comments', "itemid IN ($allrecordssql) AND commentarea='database_entry'", array($data->courseid));
0b5a80a1 2474 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallcomments'), 'error'=>false);
2475 }
2476
b8b554ac 2477 // updating dates - shift may be negative too
0b5a80a1 2478 if ($data->timeshift) {
2479 shift_course_mod_dates('data', array('timeavailablefrom', 'timeavailableto', 'timeviewfrom', 'timeviewto'), $data->timeshift, $data->courseid);
2480 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
2481 }
b572ce19 2482
0b5a80a1 2483 return $status;
2484}
f432bebf 2485
2486/**
2487 * Returns all other caps used in module
4636bf83 2488 *
2489 * @return array
f432bebf 2490 */
2491function data_get_extra_capabilities() {
16b86ae4 2492 return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames', 'moodle/rating:view', 'moodle/rating:viewany', 'moodle/rating:viewall', 'moodle/rating:rate', 'moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
f432bebf 2493}
2494
18a2a0cb 2495/**
2496 * @param string $feature FEATURE_xx constant for requested feature
2497 * @return mixed True if module supports feature, null if doesn't know
2498 */
2499function data_supports($feature) {
2500 switch($feature) {
42f103be 2501 case FEATURE_GROUPS: return true;
2502 case FEATURE_GROUPINGS: return true;
2503 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 2504 case FEATURE_MOD_INTRO: return true;
18a2a0cb 2505 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
42f103be 2506 case FEATURE_GRADE_HAS_GRADE: return true;
2507 case FEATURE_GRADE_OUTCOMES: return true;
d251b259 2508 case FEATURE_RATE: return true;
fd3f6bf9 2509 case FEATURE_BACKUP_MOODLE2: return true;
42f103be 2510
18a2a0cb 2511 default: return null;
17da2e6f 2512 }
18a2a0cb 2513}
4636bf83 2514/**
2515 * @global object
2516 * @param array $export
2517 * @param string $delimiter_name
2518 * @param object $database
2519 * @param int $count
2520 * @param bool $return
2521 * @return string|void
2522 */
1bf8c6b2 2523function data_export_csv($export, $delimiter_name, $dataname, $count, $return=false) {
ecd06483 2524 global $CFG;
2525 require_once($CFG->libdir . '/csvlib.class.php');
20988152 2526 $delimiter = csv_import_reader::get_delimiter($delimiter_name);
2527 $filename = clean_filename("${dataname}-${count}_record");
2528 if ($count > 1) {
2529 $filename .= 's';
2530 }
2531 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
2532 $filename .= clean_filename("-${delimiter_name}_separated");
2533 $filename .= '.csv';
1bf8c6b2 2534 if (empty($return)) {
ecd06483 2535 header("Content-Type: application/download\n");
2536 header("Content-Disposition: attachment; filename=$filename");
2537 header('Expires: 0');
2538 header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
2539 header('Pragma: public');
2540 }
20988152 2541 $encdelim = '&#' . ord($delimiter) . ';';
ecd06483 2542 $returnstr = '';
20988152 2543 foreach($export as $row) {
2544 foreach($row as $key => $column) {
2545 $row[$key] = str_replace($delimiter, $encdelim, $column);
2546 }
ecd06483 2547 $returnstr .= implode($delimiter, $row) . "\n";
2548 }
1bf8c6b2 2549 if (empty($return)) {
ecd06483 2550 echo $returnstr;
2551 return;
20988152 2552 }
1bf8c6b2 2553 return $returnstr;
20988152 2554}
2555
4636bf83 2556/**
2557 * @global object
2558 * @param array $export
2559 * @param string $dataname
2560 * @param int $count
2561 * @return string
2562 */
1bf8c6b2 2563function data_export_xls($export, $dataname, $count) {
20988152 2564 global $CFG;
2565 require_once("$CFG->libdir/excellib.class.php");
2566 $filename = clean_filename("${dataname}-${count}_record");
2567 if ($count > 1) {
2568 $filename .= 's';
2569 }
2570 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
2571 $filename .= '.xls';
ecd06483 2572
2573 $filearg = '-';
ecd06483 2574 $workbook = new MoodleExcelWorkbook($filearg);
1bf8c6b2 2575 $workbook->send($filename);
20988152 2576 $worksheet = array();
2577 $worksheet[0] =& $workbook->add_worksheet('');
2578 $rowno = 0;
2579 foreach ($export as $row) {
2580 $colno = 0;
2581 foreach($row as $col) {
2582 $worksheet[0]->write($rowno, $colno, $col);
2583 $colno++;
2584 }
2585 $rowno++;
2586 }
2587 $workbook->close();
ecd06483 2588 return $filename;
20988152 2589}
2590
4636bf83 2591/**
2592 * @global object
2593 * @param array $export
2594 * @param string $dataname
2595 * @param int $count
2596 * @param string
2597 */
1bf8c6b2 2598function data_export_ods($export, $dataname, $count) {
20988152 2599 global $CFG;
2600 require_once("$CFG->libdir/odslib.class.php");
2601 $filename = clean_filename("${dataname}-${count}_record");
2602 if ($count > 1) {
2603 $filename .= 's';
2604 }
2605 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
2606 $filename .= '.ods';
ecd06483 2607 $filearg = '-';
1bf8c6b2 2608 $workbook = new MoodleODSWorkbook($filearg);
2609 $workbook->send($filename);
20988152 2610 $worksheet = array();
2611 $worksheet[0] =& $workbook->add_worksheet('');
2612 $rowno = 0;
2613 foreach ($export as $row) {
2614 $colno = 0;
2615 foreach($row as $col) {
2616 $worksheet[0]->write($rowno, $colno, $col);
2617 $colno++;
2618 }
2619 $rowno++;
2620 }
2621 $workbook->close();
ecd06483 2622 return $filename;
2623}
2624
4636bf83 2625/**
2626 * @global object
2627 * @param int $dataid
2628 * @param array $fields
2629 * @param array $selectedfields
2630 * @return array
2631 */
ecd06483 2632function data_get_exportdata($dataid, $fields, $selectedfields) {
2633 global $DB;
2634
2635 $exportdata = array();
2636
2637 // populate the header in first row of export
2638 foreach($fields as $key => $field) {
2639 if (!in_array($field->field->id, $selectedfields)) {
2640 // ignore values we aren't exporting
2641 unset($fields[$key]);
2642 } else {
2643 $exportdata[0][] = $field->field->name;
2644 }
2645 }
2646
2647 $datarecords = $DB->get_records('data_records', array('dataid'=>$dataid));
2648 ksort($datarecords);
2649 $line = 1;
2650 foreach($datarecords as $record) {
2651 // get content indexed by fieldid
2652 if( $content = $DB->get_records('data_content', array('recordid'=>$record->id), 'fieldid', 'fieldid, content, content1, content2, content3, content4') ) {
2653 foreach($fields as $field) {
2654 $contents = '';
2655 if(isset($content[$field->field->id])) {
2656 $contents = $field->export_text_value($content[$field->field->id]);
2657 }
2658 $exportdata[$line][] = $contents;
2659 }
2660 }
2661 $line++;
2662 }
2663 $line--;
2664 return $exportdata;
2665}
2666
8429163d 2667/**
2668 * Lists all browsable file areas
4636bf83 2669 *
2670 * @param object $course
2671 * @param object $cm
2672 * @param object $context
2673 * @return array
8429163d 2674 */
2675function data_get_file_areas($course, $cm, $context) {
2676 $areas = array();
8429163d 2677 return $areas;
2678}
2679
2680/**
2681 * Serves the data attachments. Implements needed access control ;-)
4636bf83 2682 *
4636bf83 2683 * @param object $course
64f93798 2684 * @param object $cm
4636bf83 2685 * @param object $context
86900a93 2686 * @param string $filearea
4636bf83 2687 * @param array $args
86900a93 2688 * @param bool $forcedownload
2689 * @return bool false if file not found, does not return if found - justsend the file
8429163d 2690 */
64f93798 2691function data_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
8429163d 2692 global $CFG, $DB;
2693
64f93798 2694 if ($context->contextlevel != CONTEXT_MODULE) {
8429163d 2695 return false;
2696 }
2697
64f93798
PS
2698 require_login($course, false, $cm);
2699
2700 if ($filearea === 'content') {
8429163d 2701 $contentid = (int)array_shift($args);
2702
64f93798 2703 if (!$cm = get_coursemodule_from_instance('data', $cm->instance, $course->id)) {
0a8a7b6c 2704 return false;
2705 }
56c1ca88 2706
0a8a7b6c 2707 require_course_login($course, true, $cm);
56c1ca88 2708
8429163d 2709 if (!$content = $DB->get_record('data_content', array('id'=>$contentid))) {
2710 return false;
2711 }
2712
2713 if (!$field = $DB->get_record('data_fields', array('id'=>$content->fieldid))) {
2714 return false;
2715 }
2716
2717 if (!$record = $DB->get_record('data_records', array('id'=>$content->recordid))) {
2718 return false;
2719 }
2720
2721 if (!$data = $DB->get_record('data', array('id'=>$field->dataid))) {
2722 return false;
2723 }
2724
2725 //check if approved
2726 if (!$record->approved and !data_isowner($record) and !has_capability('mod/data:approve', $context)) {
2727 return false;
2728 }
2729
2730 // group access
2731 if ($record->groupid) {
0a8a7b6c 2732 $groupmode = groups_get_activity_groupmode($cm, $course);
8429163d 2733 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
2734 if (!groups_is_member($record->groupid)) {
2735 return false;
2736 }
2737 }
2738 }
2739
0a8a7b6c 2740 $fieldobj = data_get_field($field, $data, $cm);
8429163d 2741
64f93798
PS
2742 $relativepath = implode('/', $args);
2743 $fullpath = "/$context->id/mod_data/content/$content->id/$relativepath";
8429163d 2744
2745 if (!$fieldobj->file_ok($relativepath)) {
2746 return false;
2747 }
2748
2749 $fs = get_file_storage();
2750 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
2751 return false;
2752 }
2753
2754 // finally send the file
2755 send_stored_file($file, 0, 0, true); // download MUST be forced - security!
2756 }
2757
2758 return false;
2759}
2760
29b64a22 2761
2762function data_extend_navigation($navigation, $course, $module, $cm) {
2763 global $CFG, $OUTPUT, $USER, $DB;
2764
2765 $rid = optional_param('rid', 0, PARAM_INT);
2766
2767 $data = $DB->get_record('data', array('id'=>$cm->instance));
2768 $currentgroup = groups_get_activity_group($cm);
2769 $groupmode = groups_get_activity_groupmode($cm);
2770
2771 $numentries = data_numentries($data);
2772 /// Check the number of entries required against the number of entries already made (doesn't apply to teachers)
4f0c2d00 2773 if ($data->requiredentries > 0 && $numentries < $data->requiredentries && !has_capability('mod/data:manageentries', get_context_instance(CONTEXT_MODULE, $cm->id))) {
29b64a22 2774 $data->entriesleft = $data->requiredentries - $numentries;
3406acde
SH
2775 $entriesnode = $navigation->add(get_string('entrieslefttoadd', 'data', $data));
2776 $entriesnode->add_class('note');
29b64a22 2777 }
2778
a6855934 2779 $navigation->add(get_string('list', 'data'), new moodle_url('/mod/data/view.php', array('d'=>$cm->instance)));
29b64a22 2780 if (!empty($rid)) {
a6855934 2781 $navigation->add(get_string('single', 'data'), new moodle_url('/mod/data/view.php', array('d'=>$cm->instance, 'rid'=>$rid)));
29b64a22 2782 } else {
a6855934 2783 $navigation->add(get_string('single', 'data'), new moodle_url('/mod/data/view.php', array('d'=>$cm->instance, 'mode'=>'single')));
29b64a22 2784 }
a6855934 2785 $navigation->add(get_string('search', 'data'), new moodle_url('/mod/data/view.php', array('d'=>$cm->instance, 'mode'=>'search')));
29b64a22 2786}
2787
0b29477b
SH
2788/**
2789 * Adds module specific settings to the settings block
2790 *
2791 * @param settings_navigation $settings The settings navigation object
2792 * @param navigation_node $datanode The node to add module settings to
2793 */
2794function data_extend_settings_navigation(settings_navigation $settings, navigation_node $datanode) {
9e86f2e7 2795 global $PAGE, $DB, $CFG, $USER;
56115eea 2796
9e86f2e7 2797 $data = $DB->get_record('data', array("id" => $PAGE->cm->instance));
29b64a22 2798
29b64a22 2799 $currentgroup = groups_get_activity_group($PAGE->cm);
2800 $groupmode = groups_get_activity_groupmode($PAGE->cm);
2801
2802 if (data_user_can_add_entry($data, $currentgroup, $groupmode)) { // took out participation list here!
2803 if (empty($editentry)) {
2804 $addstring = get_string('add', 'data');
2805 } else {
2806 $addstring = get_string('editentry', 'data');
2807 }
0b29477b 2808 $datanode->add($addstring, new moodle_url('/mod/data/edit.php', array('d'=>$PAGE->cm->instance)));
29b64a22 2809 }
2810
2811 if (has_capability(DATA_CAP_EXPORT, $PAGE->cm->context)) {
2812 // The capability required to Export database records is centrally defined in 'lib.php'
2813 // and should be weaker than those required to edit Templates, Fields and Presets.
0b29477b 2814 $datanode->add(get_string('export', 'data'), new moodle_url('/mod/data/export.php', array('d'=>$data->id)));
29b64a22 2815 }
bbdfd8ce
DC
2816 if (has_capability('mod/data:manageentries', $PAGE->cm->context)) {
2817 $datanode->add(get_string('import'), new moodle_url('/mod/data/import.php', array('d'=>$data->id)));
2818 }
29b64a22 2819
2820 if (has_capability('mod/data:managetemplates', $PAGE->cm->context)) {
2821 $currenttab = '';
2822 if ($currenttab == 'list') {
2823 $defaultemplate = 'listtemplate';
2824 } else if ($currenttab == 'add') {
2825 $defaultemplate = 'addtemplate';
2826 } else if ($currenttab == 'asearch') {
2827 $defaultemplate = 'asearchtemplate';
2828 } else {
2829 $defaultemplate = 'singletemplate';
2830 }
2831
3406acde 2832 $templates = $datanode->add(get_string('templates', 'data'));
29b64a22 2833
2834 $templatelist = array ('listtemplate', 'singletemplate', 'asearchtemplate', 'addtemplate', 'rsstemplate', 'csstemplate', 'jstemplate');
2835 foreach ($templatelist as $template) {
a6855934 2836 $templates->add(get_string($template, 'data'), new moodle_url('/mod/data/templates.php', array('d'=>$data->id,'mode'=>$template)));
29b64a22 2837 }
2838
0b29477b
SH
2839 $datanode->add(get_string('fields', 'data'), new moodle_url('/mod/data/field.php', array('d'=>$data->id)));
2840 $datanode->add(get_string('presets', 'data'), new moodle_url('/mod/data/preset.php', array('d'=>$data->id)));
29b64a22 2841 }
9e86f2e7
AD
2842
2843 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
2844 require_once("$CFG->libdir/rsslib.php");
2845
2846 $string = get_string('rsstype','forum');
2847
aa60291e 2848 $url = new moodle_url(rss_get_url($PAGE->cm->context->id, $USER->id, 'mod_data', $data->id));
9e86f2e7
AD
2849 $datanode->add($string, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', ''));
2850 }
7719b4db 2851}
cba87c36 2852
8aff1574
SH
2853/**
2854 * Save the database configuration as a preset.
2855 *
2856 * @param stdClass $course The course the database module belongs to.
2857 * @param stdClass $cm The course module record
2858 * @param stdClass $data The database record
2859 * @param string $path
2860 * @return bool
2861 */
2862function data_presets_save($course, $cm, $data, $path) {
2863 $fs = get_file_storage();
2864 $filerecord = new stdClass;
2865 $filerecord->contextid = DATA_PRESET_CONTEXT;
2866 $filerecord->component = DATA_PRESET_COMPONENT;
2867 $filerecord->filearea = DATA_PRESET_FILEAREA;
2868 $filerecord->itemid = 0;
2869 $filerecord->filepath = '/'.$path.'/';
2870
2871 $filerecord->filename = 'preset.xml';
2872 $fs->create_file_from_string($filerecord, data_presets_generate_xml($course, $cm, $data));
2873
2874 $filerecord->filename = 'singletemplate.html';
2875 $fs->create_file_from_string($filerecord, $data->singletemplate);
2876
2877 $filerecord->filename = 'listtemplateheader.html';
2878 $fs->create_file_from_string($filerecord, $data->listtemplateheader);
2879
2880 $filerecord->filename = 'listtemplate.html';
2881 $fs->create_file_from_string($filerecord, $data->listtemplate);
2882
2883 $filerecord->filename = 'listtemplatefooter.html';
2884 $fs->create_file_from_string($filerecord, $data->listtemplatefooter);
2885
2886 $filerecord->filename = 'addtemplate.html';
2887 $fs->create_file_from_string($filerecord, $data->addtemplate);
2888
2889 $filerecord->filename = 'rsstemplate.html';
2890 $fs->create_file_from_string($filerecord, $data->rsstemplate);
2891
2892 $filerecord->filename = 'rsstitletemplate.html';
2893 $fs->create_file_from_string($filerecord, $data->rsstitletemplate);
2894
2895 $filerecord->filename = 'csstemplate.css';
2896 $fs->create_file_from_string($filerecord, $data->csstemplate);
2897
2898 $filerecord->filename = 'jstemplate.js';
2899 $fs->create_file_from_string($filerecord, $data->jstemplate);
2900
2901 $filerecord->filename = 'asearchtemplate.html';
2902 $fs->create_file_from_string($filerecord, $data->asearchtemplate);
2903
2904 return true;
2905}
cba87c36 2906
8aff1574
SH
2907/**
2908 * Generates the XML for the database module provided
2909 *
2910 * @global moodle_database $DB
2911 * @param stdClass $course The course the database module belongs to.
2912 * @param stdClass $cm The course module record
2913 * @param stdClass $data The database record
2914 * @return string The XML for the preset
2915 */
2916function data_presets_generate_xml($course, $cm, $data) {
2917 global $DB;
800bb0f7 2918
cba87c36
SH
2919 // Assemble "preset.xml":
2920 $presetxmldata = "<preset>\n\n";
2921
2922 // Raw settings are not preprocessed during saving of presets
2923 $raw_settings = array(
2924 'intro',
2925 'comments',
2926 'requiredentries',
2927 'requiredentriestoview',
2928 'maxentries',
2929 'rssarticles',
2930 'approval',
2931 'defaultsortdir'
2932 );
2933
2934 $presetxmldata .= "<settings>\n";
2935 // First, settings that do not require any conversion
2936 foreach ($raw_settings as $setting) {
2937 $presetxmldata .= "<$setting>" . htmlspecialchars($data->$setting) . "</$setting>\n";
2938 }
2939
2940 // Now specific settings
2941 if ($data->defaultsort > 0 && $sortfield = data_get_field_from_id($data->defaultsort, $data)) {
2942 $presetxmldata .= '<defaultsort>' . htmlspecialchars($sortfield->field->name) . "</defaultsort>\n";
2943 } else {
2944 $presetxmldata .= "<defaultsort>0</defaultsort>\n";
2945 }
2946 $presetxmldata .= "</settings>\n\n";
cba87c36
SH
2947 // Now for the fields. Grab all that are non-empty
2948 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
2949 ksort($fields);
2950 if (!empty($fields)) {
2951 foreach ($fields as $field) {
2952 $presetxmldata .= "<field>\n";
2953 foreach ($field as $key => $value) {
2954 if ($value != '' && $key != 'id' && $key != 'dataid') {
2955 $presetxmldata .= "<$key>" . htmlspecialchars($value) . "</$key>\n";
2956 }
2957 }
2958 $presetxmldata .= "</field>\n\n";
2959 }
2960 }
2961 $presetxmldata .= '</preset>';
8aff1574
SH
2962 return $presetxmldata;
2963}
2964
2965function data_presets_export($course, $cm, $data, $tostorage=false) {
2966 global $CFG, $DB;
2967
2968 $presetname = clean_filename($data->name) . '-preset-' . gmdate("Ymd_Hi");
2969 $exportsubdir = "temp/mod_data/presetexport/$presetname";
2970 make_upload_directory($exportsubdir);
2971 $exportdir = "$CFG->dataroot/$exportsubdir";
2972
2973 // Assemble "preset.xml":
2974 $presetxmldata = data_presets_generate_xml($course, $cm, $data);
cba87c36
SH
2975
2976 // After opening a file in write mode, close it asap
2977 $presetxmlfile = fopen($exportdir . '/preset.xml', 'w');
2978 fwrite($presetxmlfile, $presetxmldata);
2979 fclose($presetxmlfile);
2980
2981 // Now write the template files
2982 $singletemplate = fopen($exportdir . '/singletemplate.html', 'w');
2983 fwrite($singletemplate, $data->singletemplate);
2984 fclose($singletemplate);
2985
2986 $listtemplateheader = fopen($exportdir . '/listtemplateheader.html', 'w');
2987 fwrite($listtemplateheader, $data->listtemplateheader);
2988 fclose($listtemplateheader);
2989
2990 $listtemplate = fopen($exportdir . '/listtemplate.html', 'w');
2991 fwrite($listtemplate, $data->listtemplate);
2992 fclose($listtemplate);
2993
2994 $listtemplatefooter = fopen($exportdir . '/listtemplatefooter.html', 'w');
2995 fwrite($listtemplatefooter, $data->listtemplatefooter);
2996 fclose($listtemplatefooter);
2997
2998 $addtemplate = fopen($exportdir . '/addtemplate.html', 'w');
2999 fwrite($addtemplate, $data->addtemplate);
3000 fclose($addtemplate);
3001
3002 $rsstemplate = fopen($exportdir . '/rsstemplate.html', 'w');
3003 fwrite($rsstemplate, $data->rsstemplate);
3004 fclose($rsstemplate);
3005
3006 $rsstitletemplate = fopen($exportdir . '/rsstitletemplate.html', 'w');
3007 fwrite($rsstitletemplate, $data->rsstitletemplate);
3008 fclose($rsstitletemplate);
3009
3010 $csstemplate = fopen($exportdir . '/csstemplate.css', 'w');
3011 fwrite($csstemplate, $data->csstemplate);
3012 fclose($csstemplate);
3013
3014 $jstemplate = fopen($exportdir . '/jstemplate.js', 'w');
3015 fwrite($jstemplate, $data->jstemplate);
3016 fclose($jstemplate);
3017
3018 $asearchtemplate = fopen($exportdir . '/asearchtemplate.html', 'w');
3019 fwrite($asearchtemplate, $data->asearchtemplate);
3020 fclose($asearchtemplate);
3021
3022 // Check if all files have been generated
3023 if (! is_directory_a_preset($exportdir)) {
3024 print_error('generateerror', 'data');
3025 }
3026
3027 $filelist = array(
3028 'preset.xml',
3029 'singletemplate.html',
3030 'listtemplateheader.html',
3031 'listtemplate.html',
3032 'listtemplatefooter.html',
3033 'addtemplate.html',
3034 'rsstemplate.html',
3035 'rsstitletemplate.html',
3036 'csstemplate.css',
3037 'jstemplate.js',
3038 'asearchtemplate.html'
3039 );
3040
3041 foreach ($filelist as $key => $file) {
3042 $filelist[$key] = $exportdir . '/' . $filelist[$key];
3043 }
3044
8aff1574 3045 $exportfile = $exportdir.'.zip';
cba87c36 3046 file_exists($exportfile) && unlink($exportfile);
8aff1574
SH
3047
3048 $fp = get_file_packer();
3049 $fp->archive_to_pathname($files, $archivefile);
3050
cba87c36
SH
3051 $status = zip_files($filelist, $exportfile);
3052 // ToDo: status check
3053 foreach ($filelist as $file) {
3054 unlink($file);
3055 }
3056 rmdir($exportdir);
3057
3058 // Return the full path to the exported preset file:
3059 return $exportfile;
d846488e 3060}