MDL-24647 fixed mod/data approval file access check + fixed security issue + improved...
[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 }
39790bd8 131 $this->field = new stdClass();
0997e51a 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)) {
39790bd8 308 $options = new stdClass();
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
39790bd8 335 $content = new stdClass();
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) {
39790bd8 549 $newdata = new stdClass();
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
39790bd8 586 $newdata = new stdClass();
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
39790bd8 617 $newdata = new stdClass();
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
39790bd8 791 $record = new stdClass();
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
8429163d 912 // get all the records in this data
913 $sql = "SELECT r.id
914 FROM {data_records} r
915 WHERE r.dataid = ?";
6403e679 916
8429163d 917 $DB->delete_records_select('data_content', "recordid IN ($sql)", array($id));
3d4b223a 918
919 // delete all the records and fields
c18269c7 920 $DB->delete_records('data_records', array('dataid'=>$id));
921 $DB->delete_records('data_fields', array('dataid'=>$id));
3d4b223a 922
923 // Delete the instance itself
c18269c7 924 $result = $DB->delete_records('data', array('id'=>$id));
04366d79 925
8429163d 926 // cleanup gradebook
b82cacea 927 data_grade_item_delete($data);
b572ce19 928
04366d79 929 return $result;
3d4b223a 930}
931
4636bf83 932/**
933 * returns a summary of data activity of this user
934 *
935 * @global object
936 * @param object $course
937 * @param object $user
938 * @param object $mod
939 * @param object $data
940 * @return object|null
941 */
3d4b223a 942function data_user_outline($course, $user, $mod, $data) {
1a96363a
NC
943 global $DB, $CFG;
944 require_once("$CFG->libdir/gradelib.php");
945
946 $grades = grade_get_grades($course->id, 'mod', 'data', $data->id, $user->id);
947 if (empty($grades->items[0]->grades)) {
948 $grade = false;
949 } else {
950 $grade = reset($grades->items[0]->grades);
951 }
952
9c00b5d7 953
954 if ($countrecords = $DB->count_records('data_records', array('dataid'=>$data->id, 'userid'=>$user->id))) {
39790bd8 955 $result = new stdClass();
63701c78 956 $result->info = get_string('numrecords', 'data', $countrecords);
9c00b5d7 957 $lastrecord = $DB->get_record_sql('SELECT id,timemodified FROM {data_records}
958 WHERE dataid = ? AND userid = ?
959 ORDER BY timemodified DESC', array($data->id, $user->id), true);
3d4b223a 960 $result->time = $lastrecord->timemodified;
1a96363a
NC
961 if ($grade) {
962 $result->info .= ', ' . get_string('grade') . ': ' . $grade->str_long_grade;
963 }
964 return $result;
965 } else if ($grade) {
39790bd8 966 $result = new stdClass();
1a96363a
NC
967 $result->info = get_string('grade') . ': ' . $grade->str_long_grade;
968 $result->time = $grade->dategraded;
3d4b223a 969 return $result;
970 }
971 return NULL;
3d4b223a 972}
973
4636bf83 974/**
975 * Prints all the records uploaded by this user
976 *
977 * @global object
978 * @param object $course
979 * @param object $user
980 * @param object $mod
981 * @param object $data
982 */
3d4b223a 983function data_user_complete($course, $user, $mod, $data) {
1a96363a
NC
984 global $DB, $CFG, $OUTPUT;
985 require_once("$CFG->libdir/gradelib.php");
986
987 $grades = grade_get_grades($course->id, 'mod', 'data', $data->id, $user->id);
988 if (!empty($grades->items[0]->grades)) {
989 $grade = reset($grades->items[0]->grades);
990 echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
991 if ($grade->str_feedback) {
992 echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
993 }
994 }
9c00b5d7 995
996 if ($records = $DB->get_records('data_records', array('dataid'=>$data->id,'userid'=>$user->id), 'timemodified DESC')) {
3d45b8e5 997 data_print_template('singletemplate', $records, $data);
3d4b223a 998 }
999}
1000
04366d79 1001/**
1002 * Return grade for given user or all users.
1003 *
4636bf83 1004 * @global object
1005 * @param object $data
04366d79 1006 * @param int $userid optional user id, 0 means all users
1007 * @return array array of grades, false if none
1008 */
612607bd 1009function data_get_user_grades($data, $userid=0) {
d251b259
AD
1010 global $CFG;
1011
1012 require_once($CFG->dirroot.'/rating/lib.php');
1013 $rm = new rating_manager();
04366d79 1014
d251b259
AD
1015 $ratingoptions = new stdclass();
1016 $ratingoptions->modulename = 'data';
1017 $ratingoptions->moduleid = $data->id;
04366d79 1018
d251b259
AD
1019 $ratingoptions->userid = $userid;
1020 $ratingoptions->aggregationmethod = $data->assessed;
1021 $ratingoptions->scaleid = $data->scale;
1022 $ratingoptions->itemtable = 'data_records';
1023 $ratingoptions->itemtableusercolumn = 'userid';
04366d79 1024
d251b259 1025 return $rm->get_user_grades($ratingoptions);
04366d79 1026}
1027
1028/**
775f811a 1029 * Update activity grades
04366d79 1030 *
4636bf83 1031 * @global object
1032 * @global object
775f811a 1033 * @param object $data
1034 * @param int $userid specific user only, 0 means all
4636bf83 1035 * @param bool $nullifnone
04366d79 1036 */
775f811a 1037function data_update_grades($data, $userid=0, $nullifnone=true) {
9c00b5d7 1038 global $CFG, $DB;
1039 require_once($CFG->libdir.'/gradelib.php');
04366d79 1040
775f811a 1041 if (!$data->assessed) {
1042 data_grade_item_update($data);
b572ce19 1043
775f811a 1044 } else if ($grades = data_get_user_grades($data, $userid)) {
1045 data_grade_item_update($data, $grades);
b572ce19 1046
775f811a 1047 } else if ($userid and $nullifnone) {
39790bd8 1048 $grade = new stdClass();
775f811a 1049 $grade->userid = $userid;
1050 $grade->rawgrade = NULL;
1051 data_grade_item_update($data, $grade);
b572ce19 1052
04366d79 1053 } else {
775f811a 1054 data_grade_item_update($data);
1055 }
1056}
1057
1058/**
1059 * Update all grades in gradebook.
4636bf83 1060 *
1061 * @global object
775f811a 1062 */
1063function data_upgrade_grades() {
1064 global $DB;
1065
1066 $sql = "SELECT COUNT('x')
1067 FROM {data} d, {course_modules} cm, {modules} m
1068 WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id";
1069 $count = $DB->count_records_sql($sql);
1070
1071 $sql = "SELECT d.*, cm.idnumber AS cmidnumber, d.course AS courseid
1072 FROM {data} d, {course_modules} cm, {modules} m
1073 WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id";
1074 if ($rs = $DB->get_recordset_sql($sql)) {
1075 // too much debug output
775f811a 1076 $pbar = new progress_bar('dataupgradegrades', 500, true);
1077 $i=0;
1078 foreach ($rs as $data) {
1079 $i++;
1080 upgrade_set_timeout(60*5); // set up timeout, may also abort execution
1081 data_update_grades($data, 0, false);
1082 $pbar->update($i, $count, "Updating Database grades ($i/$count).");
04366d79 1083 }
775f811a 1084 $rs->close();
04366d79 1085 }
1086}
1087
1088/**
612607bd 1089 * Update/create grade item for given data
04366d79 1090 *
4636bf83 1091 * @global object
04366d79 1092 * @param object $data object with extra cmidnumber
0b5a80a1 1093 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
04366d79 1094 * @return object grade_item
1095 */
0b5a80a1 1096function data_grade_item_update($data, $grades=NULL) {
612607bd 1097 global $CFG;
9c00b5d7 1098 require_once($CFG->libdir.'/gradelib.php');
04366d79 1099
13534ef7 1100 $params = array('itemname'=>$data->name, 'idnumber'=>$data->cmidnumber);
b572ce19 1101
04366d79 1102 if (!$data->assessed or $data->scale == 0) {
612607bd 1103 $params['gradetype'] = GRADE_TYPE_NONE;
b572ce19 1104
04366d79 1105 } else if ($data->scale > 0) {
1106 $params['gradetype'] = GRADE_TYPE_VALUE;
1107 $params['grademax'] = $data->scale;
1108 $params['grademin'] = 0;
b572ce19 1109
04366d79 1110 } else if ($data->scale < 0) {
1111 $params['gradetype'] = GRADE_TYPE_SCALE;
1112 $params['scaleid'] = -$data->scale;
1113 }
b572ce19 1114
0b5a80a1 1115 if ($grades === 'reset') {
1116 $params['reset'] = true;
1117 $grades = NULL;
1118 }
1119
1120 return grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, $grades, $params);
04366d79 1121}
1122
1123/**
1124 * Delete grade item for given data
1125 *
4636bf83 1126 * @global object
04366d79 1127 * @param object $data object
1128 * @return object grade_item
1129 */
1130function data_grade_item_delete($data) {
612607bd 1131 global $CFG;
1132 require_once($CFG->libdir.'/gradelib.php');
b572ce19 1133
b67ec72f 1134 return grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, NULL, array('deleted'=>1));
04366d79 1135}
1136
4636bf83 1137/**
1138 * returns a list of participants of this database
1139 *
1140 * @global object
1141 * @return array
1142 */
3d4b223a 1143function data_get_participants($dataid) {
b8b554ac 1144// Returns the users with data in one data
d251b259 1145// (users with records in data_records, data_comments and ratings)
8429163d 1146 global $DB;
9c00b5d7 1147
1148 $records = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
1149 FROM {user} u, {data_records} r
1150 WHERE r.dataid = ? AND u.id = r.userid", array($dataid));
1151
1152 $comments = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
e998effa
DC
1153 FROM {user} u, {data_records} r, {comments} c
1154 WHERE r.dataid = ? AND u.id = r.userid AND r.id = c.itemid AND c.commentarea='database_entry'", array($dataid));
9c00b5d7 1155
1156 $ratings = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
d251b259
AD
1157 FROM {user} u, {data_records} r, {ratings} a
1158 WHERE r.dataid = ? AND u.id = r.userid AND r.id = a.itemid", array($dataid));
3d4b223a 1159
1160 $participants = array();
b572ce19 1161
8429163d 1162 if ($records) {
3d4b223a 1163 foreach ($records as $record) {
1164 $participants[$record->id] = $record;
1165 }
1166 }
8429163d 1167 if ($comments) {
3d4b223a 1168 foreach ($comments as $comment) {
1169 $participants[$comment->id] = $comment;
1170 }
1171 }
8429163d 1172 if ($ratings) {
cf040300 1173 foreach ($ratings as $rating) {
1174 $participants[$rating->id] = $rating;
1175 }
1176 }
b572ce19 1177
3d4b223a 1178 return $participants;
1179}
1180
b8b554ac 1181// junk functions
4636bf83 1182/**
56c1ca88 1183 * takes a list of records, the current data, a search string,
4636bf83 1184 * and mode to display prints the translated template
1185 *
1186 * @global object
1187 * @global object
1188 * @param string $template
56c1ca88 1189 * @param array $records
4636bf83 1190 * @param object $data
56c1ca88 1191 * @param string $search
4636bf83 1192 * @param int $page
1193 * @param bool $return
1194 * @return mixed
1195 */
d53e5129 1196function data_print_template($template, $records, $data, $search='', $page=0, $return=false) {
f2a1963c 1197 global $CFG, $DB, $OUTPUT;
c088d603 1198 $cm = get_coursemodule_from_instance('data', $data->id);
dabfd0ed 1199 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
b572ce19 1200
3d45b8e5 1201 static $fields = NULL;
1202 static $isteacher;
63701c78 1203 static $dataid = NULL;
b572ce19 1204
63701c78 1205 if (empty($dataid)) {
1206 $dataid = $data->id;
1207 } else if ($dataid != $data->id) {
1208 $fields = NULL;
1209 }
b572ce19 1210
3d45b8e5 1211 if (empty($fields)) {
9c00b5d7 1212 $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id));
3d45b8e5 1213 foreach ($fieldrecords as $fieldrecord) {
1214 $fields[]= data_get_field($fieldrecord, $data);
1215 }
81e956b9 1216 $isteacher = has_capability('mod/data:managetemplates', $context);
3d45b8e5 1217 }
b572ce19 1218
64452eb4 1219 if (empty($records)) {
1220 return;
1221 }
b572ce19 1222
668fc89a 1223 foreach ($records as $record) { // Might be just one for the single template
b572ce19 1224
b8b554ac 1225 // Replacing tags
3d4b223a 1226 $patterns = array();
1227 $replacement = array();
b572ce19 1228
b8b554ac 1229 // Then we generate strings to replace for normal tags
3d45b8e5 1230 foreach ($fields as $field) {
f2584d0e 1231 $patterns[]='[['.$field->field->name.']]';
d118d06a 1232 $replacement[] = highlight($search, $field->display_browse_field($record->id, $template));
3d4b223a 1233 }
b572ce19 1234
b8b554ac 1235 // Replacing special tags (##Edit##, ##Delete##, ##More##)
e357c206 1236 $patterns[]='##edit##';
1237 $patterns[]='##delete##';
046dd7dc 1238 if (has_capability('mod/data:manageentries', $context) or data_isowner($record->id)) {
64452eb4 1239 $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/edit.php?d='
b5d0cafc 1240 .$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 1241 $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/view.php?d='
b5d0cafc 1242 .$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 1243 } else {
3d4b223a 1244 $replacement[] = '';
eeeb4f2a 1245 $replacement[] = '';
3d4b223a 1246 }
d53e5129 1247
1248 $moreurl = $CFG->wwwroot . '/mod/data/view.php?d=' . $data->id . '&amp;rid=' . $record->id;
32d799c6 1249 if ($search) {
1250 $moreurl .= '&amp;filter=1';
1251 }
e357c206 1252 $patterns[]='##more##';
b5d0cafc 1253 $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 1254
e357c206 1255 $patterns[]='##moreurl##';
d53e5129 1256 $replacement[] = $moreurl;
473dd288 1257
e357c206 1258 $patterns[]='##user##';
64452eb4 1259 $replacement[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$record->userid.
1260 '&amp;course='.$data->course.'">'.fullname($record).'</a>';
8185aeb6 1261
1262 $patterns[]='##export##';
1263
c3e1b5bd 1264 if ($CFG->enableportfolios && ($template == 'singletemplate' || $template == 'listtemplate')
8185aeb6 1265 && ((has_capability('mod/data:exportentry', $context)
1266 || (data_isowner($record->id) && has_capability('mod/data:exportownentry', $context))))) {
24ba58ee 1267 require_once($CFG->libdir . '/portfoliolib.php');
0d06b6fd 1268 $button = new portfolio_add_button();
24ba58ee 1269 $button->set_callback_options('data_portfolio_caller', array('id' => $cm->id, 'recordid' => $record->id), '/mod/data/locallib.php');
6be1dcae 1270 list($formats, $files) = data_portfolio_caller::formats($fields, $record);
276378e6 1271 $button->set_formats($formats);
0d06b6fd 1272 $replacement[] = $button->to_html(PORTFOLIO_ADD_ICON_LINK);
8185aeb6 1273 } else {
1274 $replacement[] = '';
1275 }
8429163d 1276
c95034f2 1277 $patterns[] = '##timeadded##';
8429163d 1278 $replacement[] = userdate($record->timecreated);
c95034f2 1279
1280 $patterns[] = '##timemodified##';
35e129c1 1281 $replacement [] = userdate($record->timemodified);
473dd288 1282
e357c206 1283 $patterns[]='##approve##';
b572ce19 1284 if (has_capability('mod/data:approve', $context) && ($data->approval) && (!$record->approved)){
b5d0cafc 1285 $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 1286 } else {
6e0119dd 1287 $replacement[] = '';
1288 }
6403e679 1289
e357c206 1290 $patterns[]='##comments##';
4d3d87f0 1291 if (($template == 'listtemplate') && ($data->comments)) {
e998effa
DC
1292
1293 if (!empty($CFG->usecomments)) {
36051c9e 1294 require_once($CFG->dirroot . '/comment/lib.php');
866354a9 1295 list($context, $course, $cm) = get_context_info_array($context->id);
6bdfef5d 1296 $cmt = new stdClass();
e998effa 1297 $cmt->context = $context;
866354a9
DC
1298 $cmt->course = $course;
1299 $cmt->cm = $cm;
1300 $cmt->area = 'database_entry';
e998effa
DC
1301 $cmt->itemid = $record->id;
1302 $cmt->showcount = true;
d846488e 1303 $cmt->component = 'mod_data';
e998effa 1304 $comment = new comment($cmt);
36051c9e 1305 $replacement[] = $comment->output(true);
e998effa 1306 }
4d3d87f0 1307 } else {
1308 $replacement[] = '';
1309 }
6e0119dd 1310
b8b554ac 1311 // actual replacement of the tags
f2584d0e 1312 $newtext = str_ireplace($patterns, $replacement, $data->{$template});
5023c3ab 1313
b8b554ac 1314 // no more html formatting and filtering - see MDL-6635
aa3b20e9 1315 if ($return) {
1316 return $newtext;
1317 } else {
1318 echo $newtext;
b572ce19 1319
aa3b20e9 1320 // hack alert - return is always false in singletemplate anyway ;-)
1321 /**********************************
1322 * Printing Ratings Form *
1323 *********************************/
1324 if ($template == 'singletemplate') { //prints ratings options
1325 data_print_ratings($data, $record);
1326 }
b572ce19 1327
aa3b20e9 1328 /**********************************
d251b259 1329 * Printing Comments Form *
aa3b20e9 1330 *********************************/
d251b259 1331 if (($template == 'singletemplate') && ($data->comments)) {
e998effa 1332 if (!empty($CFG->usecomments)) {
36051c9e 1333 require_once($CFG->dirroot . '/comment/lib.php');
866354a9 1334 list($context, $course, $cm) = get_context_info_array($context->id);
6bdfef5d 1335 $cmt = new stdClass();
e998effa 1336 $cmt->context = $context;
866354a9
DC
1337 $cmt->course = $course;
1338 $cmt->cm = $cm;
1339 $cmt->area = 'database_entry';
e998effa
DC
1340 $cmt->itemid = $record->id;
1341 $cmt->showcount = true;
d846488e 1342 $cmt->component = 'mod_data';
e998effa 1343 $comment = new comment($cmt);
36051c9e 1344 $comment->output(false);
e998effa 1345 }
aa3b20e9 1346 }
4d3d87f0 1347 }
3d4b223a 1348 }
1349}
1350
d251b259
AD
1351/**
1352 * Return rating related permissions
1353 * @param string $options the context id
1354 * @return array an associative array of the user's rating permissions
1355 */
1356function data_rating_permissions($options) {
1357 $contextid = $options;
1358 $context = get_context_instance_by_id($contextid);
1359
1360 if (!$context) {
1361 print_error('invalidcontext');
1362 return null;
1363 } else {
1364 $ret = new stdclass();
1365 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));
1366 }
1367}
1368
aeafd436
AD
1369/**
1370 * Returns the names of the table and columns necessary to check items for ratings
1371 * @return array an array containing the item table, item id and user id columns
1372 */
1373function data_rating_item_check_info() {
1374 return array('data_records','id','userid');
1375}
1376
a593aeee 1377
4636bf83 1378/**
56c1ca88 1379 * function that takes in the current data, number of items per page,
1380 * a search string and prints a preference box in view.php
1381 *
1382 * This preference box prints a searchable advanced search template if
1383 * a) A template is defined
1384 * b) The advanced search checkbox is checked.
4636bf83 1385 *
1386 * @global object
1387 * @global object
56c1ca88 1388 * @param object $data
1389 * @param int $perpage
4636bf83 1390 * @param string $search
1391 * @param string $sort
1392 * @param string $order
1393 * @param array $search_array
1394 * @param int $advanced
1395 * @param string $mode
1396 * @return void
1397 */
7900ecb0 1398function data_print_preference_form($data, $perpage, $search, $sort='', $order='ASC', $search_array = '', $advanced = 0, $mode= ''){
601104f2 1399 global $CFG, $DB, $PAGE, $OUTPUT;
8429163d 1400
7900ecb0 1401 $cm = get_coursemodule_from_instance('data', $data->id);
1402 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1403 echo '<br /><div class="datapreferences">';
b7dc2256 1404 echo '<form id="options" action="view.php" method="get">';
7900ecb0 1405 echo '<div>';
473dd288 1406 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
7900ecb0 1407 if ($mode =='asearch') {
1408 $advanced = 1;
1409 echo '<input type="hidden" name="mode" value="list" />';
1410 }
a032a460 1411 echo '<label for="pref_perpage">'.get_string('pagesize','data').'</label> ';
76a2fd82 1412 $pagesizes = array(2=>2,3=>3,4=>4,5=>5,6=>6,7=>7,8=>8,9=>9,10=>10,15=>15,
6e0119dd 1413 20=>20,30=>30,40=>40,50=>50,100=>100,200=>200,300=>300,400=>400,500=>500,1000=>1000);
d776d59e 1414 echo html_writer::select($pagesizes, 'perpage', $perpage, false, array('id'=>'pref_perpage'));
4b9210f3 1415 echo '<div id="reg_search" style="display: ';
7900ecb0 1416 if ($advanced) {
1417 echo 'none';
1418 }
1419 else {
1420 echo 'inline';
1421 }
1422 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 1423 echo '&nbsp;&nbsp;&nbsp;<label for="pref_sortby">'.get_string('sortby').'</label> ';
b8b554ac 1424 // foreach field, print the option
3239b010 1425 echo '<select name="sort" id="pref_sortby">';
9c00b5d7 1426 if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'name')) {
3239b010 1427 echo '<optgroup label="'.get_string('fields', 'data').'">';
1428 foreach ($fields as $field) {
1429 if ($field->id == $sort) {
1430 echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
1431 } else {
1432 echo '<option value="'.$field->id.'">'.$field->name.'</option>';
1433 }
1434 }
1435 echo '</optgroup>';
714bec74 1436 }
3239b010 1437 $options = array();
1438 $options[DATA_TIMEADDED] = get_string('timeadded', 'data');
1439 $options[DATA_TIMEMODIFIED] = get_string('timemodified', 'data');
1440 $options[DATA_FIRSTNAME] = get_string('authorfirstname', 'data');
1441 $options[DATA_LASTNAME] = get_string('authorlastname', 'data');
bb5740f4 1442 if ($data->approval and has_capability('mod/data:approve', $context)) {
1443 $options[DATA_APPROVED] = get_string('approved', 'data');
1444 }
3239b010 1445 echo '<optgroup label="'.get_string('other', 'data').'">';
714bec74 1446 foreach ($options as $key => $name) {
1447 if ($key == $sort) {
1448 echo '<option value="'.$key.'" selected="selected">'.$name.'</option>';
cf3e199b 1449 } else {
714bec74 1450 echo '<option value="'.$key.'">'.$name.'</option>';
cf3e199b 1451 }
1452 }
3239b010 1453 echo '</optgroup>';
cf3e199b 1454 echo '</select>';
a032a460 1455 echo '<label for="pref_order" class="accesshide">'.get_string('order').'</label>';
1456 echo '<select id="pref_order" name="order">';
c8505cac 1457 if ($order == 'ASC') {
af25f45e 1458 echo '<option value="ASC" selected="selected">'.get_string('ascending','data').'</option>';
cf3e199b 1459 } else {
1460 echo '<option value="ASC">'.get_string('ascending','data').'</option>';
1461 }
c8505cac 1462 if ($order == 'DESC') {
af25f45e 1463 echo '<option value="DESC" selected="selected">'.get_string('descending','data').'</option>';
cf3e199b 1464 } else {
1465 echo '<option value="DESC">'.get_string('descending','data').'</option>';
1466 }
af25f45e 1467 echo '</select>';
86638489 1468
7900ecb0 1469 if ($advanced) {
1470 $checked = ' checked="checked" ';
1471 }
1472 else {
1473 $checked = '';
1474 }
9dec75db 1475 $PAGE->requires->js('/mod/data/data.js');
de8ff581 1476 echo '&nbsp;<input type="hidden" name="advanced" value="0" />';
d53e5129 1477 echo '&nbsp;<input type="hidden" name="filter" value="1" />';
714bec74 1478 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 1479 echo '&nbsp;<input type="submit" value="'.get_string('savesettings','data').'" />';
8429163d 1480
7900ecb0 1481 echo '<br />';
86638489 1482 echo '<div class="dataadvancedsearch" id="data_adv_form" style="display: ';
8429163d 1483
7900ecb0 1484 if ($advanced) {
1485 echo 'inline';
1486 }
1487 else {
1488 echo 'none';
1489 }
86638489 1490 echo ';margin-left:auto;margin-right:auto;" >';
7900ecb0 1491 echo '<table class="boxaligncenter">';
8429163d 1492
7900ecb0 1493 // print ASC or DESC
1494 echo '<tr><td colspan="2">&nbsp;</td></tr>';
1495 $i = 0;
1496
1497 // Determine if we are printing all fields for advanced search, or the template for advanced search
1498 // If a template is not defined, use the deafault template and display all fields.
1499 if(empty($data->asearchtemplate)) {
1500 data_generate_default_template($data, 'asearchtemplate');
1501 }
1502
1503 static $fields = NULL;
1504 static $isteacher;
1505 static $dataid = NULL;
b572ce19 1506
7900ecb0 1507 if (empty($dataid)) {
1508 $dataid = $data->id;
1509 } else if ($dataid != $data->id) {
1510 $fields = NULL;
1511 }
1512
1513 if (empty($fields)) {
9c00b5d7 1514 $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id));
7900ecb0 1515 foreach ($fieldrecords as $fieldrecord) {
1516 $fields[]= data_get_field($fieldrecord, $data);
1517 }
b572ce19 1518
7900ecb0 1519 $isteacher = has_capability('mod/data:managetemplates', $context);
1520 }
1521
b8b554ac 1522 // Replacing tags
7900ecb0 1523 $patterns = array();
1524 $replacement = array();
1525
b8b554ac 1526 // Then we generate strings to replace for normal tags
7900ecb0 1527 foreach ($fields as $field) {
98f67312 1528 $fieldname = $field->field->name;
1529 $fieldname = preg_quote($fieldname, '/');
1530 $patterns[] = "/\[\[$fieldname\]\]/i";
714bec74 1531 $searchfield = data_get_field_from_id($field->field->id, $data);
7900ecb0 1532 if (!empty($search_array[$field->field->id]->data)) {
1533 $replacement[] = $searchfield->display_search_field($search_array[$field->field->id]->data);
1534 } else {
1535 $replacement[] = $searchfield->display_search_field();
1536 }
1537 }
714bec74 1538 $fn = !empty($search_array[DATA_FIRSTNAME]->data) ? $search_array[DATA_FIRSTNAME]->data : '';
1539 $ln = !empty($search_array[DATA_LASTNAME]->data) ? $search_array[DATA_LASTNAME]->data : '';
1540 $patterns[] = '/##firstname##/';
bc16bd57 1541 $replacement[] = '<input type="text" size="16" name="u_fn" value="'.$fn.'" />';
714bec74 1542 $patterns[] = '/##lastname##/';
bc16bd57 1543 $replacement[] = '<input type="text" size="16" name="u_ln" value="'.$ln.'" />';
714bec74 1544
668fc89a 1545 // actual replacement of the tags
7900ecb0 1546 $newtext = preg_replace($patterns, $replacement, $data->asearchtemplate);
b572ce19 1547
39790bd8 1548 $options = new stdClass();
7900ecb0 1549 $options->para=false;
1550 $options->noclean=true;
1551 echo '<tr><td>';
1552 echo format_text($newtext, FORMAT_HTML, $options);
1553 echo '</td></tr>';
b572ce19 1554
eeeb4f2a 1555 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 1556 echo '</table>';
c8505cac 1557 echo '</div>';
7900ecb0 1558 echo '</div>';
1559 echo '</form>';
b8b554ac 1560 echo '</div>';
3d4b223a 1561}
5f5bcda8 1562
4636bf83 1563/**
1564 * @global object
1565 * @global object
1566 * @param object $data
1567 * @param object $record
1568 * @return void Output echo'd
1569 */
4d3d87f0 1570function data_print_ratings($data, $record) {
56c1ca88 1571 global $OUTPUT;
d251b259
AD
1572 if( !empty($record->rating) ){
1573 echo $OUTPUT->render($record->rating);
4d3d87f0 1574 }
4d3d87f0 1575}
1576
4636bf83 1577/**
1578 * For Participantion Reports
1579 *
1580 * @return array
1581 */
4c03f920 1582function data_get_view_actions() {
1583 return array('view');
1584}
1585
4636bf83 1586/**
1587 * @return array
1588 */
4c03f920 1589function data_get_post_actions() {
1590 return array('add','update','record delete');
1591}
1592
4636bf83 1593/**
4636bf83 1594 * @param string $name
1595 * @param int $dataid
1596 * @param int $fieldid
1597 * @return bool
1598 */
e9da9f5b
PS
1599function data_fieldname_exists($name, $dataid, $fieldid = 0) {
1600 global $DB;
0997e51a 1601
e9da9f5b
PS
1602 if (!is_numeric($name)) {
1603 $like = $DB->sql_like('df.name', ':name', false);
fdeb3e1f 1604 } else {
e9da9f5b 1605 $like = "df.name = :name";
fdeb3e1f 1606 }
e9da9f5b 1607 $params = array('name'=>$name);
6403e679 1608 if ($fieldid) {
e9da9f5b
PS
1609 $params['dataid'] = $dataid;
1610 $params['fieldid1'] = $fieldid;
1611 $params['fieldid2'] = $fieldid;
9c00b5d7 1612 return $DB->record_exists_sql("SELECT * FROM {data_fields} df
e9da9f5b
PS
1613 WHERE $like AND df.dataid = :dataid
1614 AND ((df.id < :fieldid1) OR (df.id > :fieldid2))", $params);
0997e51a 1615 } else {
e9da9f5b 1616 $params['dataid'] = $dataid;
9c00b5d7 1617 return $DB->record_exists_sql("SELECT * FROM {data_fields} df
e9da9f5b 1618 WHERE $like AND df.dataid = :dataid", $params);
0997e51a 1619 }
1620}
1621
4636bf83 1622/**
1623 * @param array $fieldinput
1624 */
0997e51a 1625function data_convert_arrays_to_strings(&$fieldinput) {
1626 foreach ($fieldinput as $key => $val) {
1627 if (is_array($val)) {
1628 $str = '';
1629 foreach ($val as $inner) {
1630 $str .= $inner . ',';
1631 }
1632 $str = substr($str, 0, -1);
b572ce19 1633
0997e51a 1634 $fieldinput->$key = $str;
1635 }
1636 }
1637}
1638
901dd2fb 1639
7f258664 1640/**
1641 * Converts a database (module instance) to use the Roles System
4636bf83 1642 *
1643 * @global object
1644 * @global object
1645 * @uses CONTEXT_MODULE
1646 * @uses CAP_PREVENT
1647 * @uses CAP_ALLOW
1648 * @param object $data a data object with the same attributes as a record
1649 * from the data database table
1650 * @param int $datamodid the id of the data module, from the modules table
4f0c2d00
PS
1651 * @param array $teacherroles array of roles that have archetype teacher
1652 * @param array $studentroles array of roles that have archetype student
1653 * @param array $guestroles array of roles that have archetype guest
4636bf83 1654 * @param int $cmid the course_module id for this data instance
1655 * @return boolean data module was converted or not
7f258664 1656 */
1657function data_convert_to_roles($data, $teacherroles=array(), $studentroles=array(), $cmid=NULL) {
4102b449 1658 global $CFG, $DB, $OUTPUT;
6403e679 1659
7f258664 1660 if (!isset($data->participants) && !isset($data->assesspublic)
1661 && !isset($data->groupmode)) {
1662 // We assume that this database has already been converted to use the
1663 // Roles System. above fields get dropped the data module has been
1664 // upgraded to use Roles.
1665 return false;
1666 }
b572ce19 1667
7f258664 1668 if (empty($cmid)) {
1669 // We were not given the course_module id. Try to find it.
741c4d0b 1670 if (!$cm = get_coursemodule_from_instance('data', $data->id)) {
4102b449 1671 echo $OUTPUT->notification('Could not get the course module for the data');
7f258664 1672 return false;
1673 } else {
1674 $cmid = $cm->id;
1675 }
1676 }
1677 $context = get_context_instance(CONTEXT_MODULE, $cmid);
6403e679 1678
b572ce19 1679
7f258664 1680 // $data->participants:
1681 // 1 - Only teachers can add entries
1682 // 3 - Teachers and students can add entries
1683 switch ($data->participants) {
1684 case 1:
1685 foreach ($studentroles as $studentrole) {
1686 assign_capability('mod/data:writeentry', CAP_PREVENT, $studentrole->id, $context->id);
1687 }
1688 foreach ($teacherroles as $teacherrole) {
1689 assign_capability('mod/data:writeentry', CAP_ALLOW, $teacherrole->id, $context->id);
1690 }
1691 break;
1692 case 3:
1693 foreach ($studentroles as $studentrole) {
1694 assign_capability('mod/data:writeentry', CAP_ALLOW, $studentrole->id, $context->id);
1695 }
1696 foreach ($teacherroles as $teacherrole) {
1697 assign_capability('mod/data:writeentry', CAP_ALLOW, $teacherrole->id, $context->id);
1698 }
1699 break;
1700 }
6403e679 1701
7f258664 1702 // $data->assessed:
1703 // 2 - Only teachers can rate posts
1704 // 1 - Everyone can rate posts
1705 // 0 - No one can rate posts
1706 switch ($data->assessed) {
1707 case 0:
1708 foreach ($studentroles as $studentrole) {
1709 assign_capability('mod/data:rate', CAP_PREVENT, $studentrole->id, $context->id);
1710 }
1711 foreach ($teacherroles as $teacherrole) {
1712 assign_capability('mod/data:rate', CAP_PREVENT, $teacherrole->id, $context->id);
1713 }
1714 break;
1715 case 1:
1716 foreach ($studentroles as $studentrole) {
1717 assign_capability('mod/data:rate', CAP_ALLOW, $studentrole->id, $context->id);
1718 }
1719 foreach ($teacherroles as $teacherrole) {
1720 assign_capability('mod/data:rate', CAP_ALLOW, $teacherrole->id, $context->id);
1721 }
1722 break;
1723 case 2:
1724 foreach ($studentroles as $studentrole) {
1725 assign_capability('mod/data:rate', CAP_PREVENT, $studentrole->id, $context->id);
1726 }
1727 foreach ($teacherroles as $teacherrole) {
1728 assign_capability('mod/data:rate', CAP_ALLOW, $teacherrole->id, $context->id);
1729 }
1730 break;
1731 }
6403e679 1732
7f258664 1733 // $data->assesspublic:
1734 // 0 - Students can only see their own ratings
1735 // 1 - Students can see everyone's ratings
1736 switch ($data->assesspublic) {
1737 case 0:
1738 foreach ($studentroles as $studentrole) {
1739 assign_capability('mod/data:viewrating', CAP_PREVENT, $studentrole->id, $context->id);
1740 }
1741 foreach ($teacherroles as $teacherrole) {
1742 assign_capability('mod/data:viewrating', CAP_ALLOW, $teacherrole->id, $context->id);
1743 }
1744 break;
1745 case 1:
1746 foreach ($studentroles as $studentrole) {
1747 assign_capability('mod/data:viewrating', CAP_ALLOW, $studentrole->id, $context->id);
1748 }
1749 foreach ($teacherroles as $teacherrole) {
1750 assign_capability('mod/data:viewrating', CAP_ALLOW, $teacherrole->id, $context->id);
1751 }
1752 break;
1753 }
1754
1755 if (empty($cm)) {
9c00b5d7 1756 $cm = $DB->get_record('course_modules', array('id'=>$cmid));
7f258664 1757 }
6403e679 1758
7f258664 1759 switch ($cm->groupmode) {
055f2185 1760 case NOGROUPS:
7f258664 1761 break;
055f2185 1762 case SEPARATEGROUPS:
7f258664 1763 foreach ($studentroles as $studentrole) {
1764 assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $studentrole->id, $context->id);
1765 }
1766 foreach ($teacherroles as $teacherrole) {
1767 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $teacherrole->id, $context->id);
1768 }
1769 break;
055f2185 1770 case VISIBLEGROUPS:
7f258664 1771 foreach ($studentroles as $studentrole) {
1772 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $studentrole->id, $context->id);
1773 }
1774 foreach ($teacherroles as $teacherrole) {
1775 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $teacherrole->id, $context->id);
1776 }
1777 break;
1778 }
1779 return true;
1780}
1781
4636bf83 1782/**
8303eb84 1783 * Returns the best name to show for a preset
4636bf83 1784 *
1785 * @param string $shortname
1786 * @param string $path
1787 * @return string
8303eb84 1788 */
1789function data_preset_name($shortname, $path) {
1790
668fc89a 1791 // We are looking inside the preset itself as a first choice, but also in normal data directory
30d76014 1792 $string = get_string('modulename', 'datapreset_'.$shortname);
8303eb84 1793
1794 if (substr($string, 0, 1) == '[') {
1795 return $shortname;
1796 } else {
1797 return $string;
1798 }
1799}
1800
4636bf83 1801/**
8aff1574 1802 * Returns an array of all the available presets.
4636bf83 1803 *
4636bf83 1804 * @return array
8303eb84 1805 */
1806function data_get_available_presets($context) {
1807 global $CFG, $USER;
b572ce19 1808
8303eb84 1809 $presets = array();
b572ce19 1810
8aff1574 1811 // First load the ratings sub plugins that exist within the modules preset dir
8303eb84 1812 if ($dirs = get_list_of_plugins('mod/data/preset')) {
1813 foreach ($dirs as $dir) {
1814 $fulldir = $CFG->dirroot.'/mod/data/preset/'.$dir;
8303eb84 1815 if (is_directory_a_preset($fulldir)) {
39790bd8 1816 $preset = new stdClass();
8303eb84 1817 $preset->path = $fulldir;
1818 $preset->userid = 0;
1819 $preset->shortname = $dir;
1820 $preset->name = data_preset_name($dir, $fulldir);
1821 if (file_exists($fulldir.'/screenshot.jpg')) {
1822 $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.jpg';
1823 } else if (file_exists($fulldir.'/screenshot.png')) {
1824 $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.png';
1825 } else if (file_exists($fulldir.'/screenshot.gif')) {
1826 $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.gif';
1827 }
1828 $presets[] = $preset;
1829 }
1830 }
1831 }
8aff1574
SH
1832 // Now add to that the site presets that people have saved
1833 $presets = data_get_available_site_presets($context, $presets);
1834 return $presets;
1835}
8303eb84 1836
8aff1574
SH
1837/**
1838 * Gets an array of all of the presets that users have saved to the site.
1839 *
1840 * @param stdClass $context The context that we are looking from.
1841 * @param array $presets
1842 * @return array An array of presets
1843 */
1844function data_get_available_site_presets($context, array $presets=array()) {
810860d2
PS
1845 global $USER;
1846
8aff1574
SH
1847 $fs = get_file_storage();
1848 $files = $fs->get_area_files(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA);
1849 $canviewall = has_capability('mod/data:viewalluserpresets', $context);
1850 if (empty($files)) {
1851 return $presets;
1852 }
1853 foreach ($files as $file) {
1854 if (($file->is_directory() && $file->get_filepath()=='/') || !$file->is_directory() || (!$canviewall && $file->get_userid() != $USER->id)) {
1855 continue;
1856 }
1857 $preset = new stdClass;
1858 $preset->path = $file->get_filepath();
1859 $preset->name = trim($preset->path, '/');
1860 $preset->shortname = $preset->name;
1861 $preset->userid = $file->get_userid();
1862 $preset->id = $file->get_id();
1863 $preset->storedfile = $file;
1864 $presets[] = $preset;
1865 }
1866 return $presets;
1867}
1868
1869/**
1870 * Deletes a saved preset.
1871 *
1872 * @param string $name
1873 * @return bool
1874 */
1875function data_delete_site_preset($name) {
1876 $fs = get_file_storage();
1877
1878 $files = $fs->get_directory_files(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA, 0, '/'.$name.'/');
1879 if (!empty($files)) {
1880 foreach ($files as $file) {
1881 $file->delete();
8303eb84 1882 }
1883 }
b572ce19 1884
8aff1574
SH
1885 $dir = $fs->get_file(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA, 0, '/'.$name.'/', '.');
1886 if (!empty($dir)) {
1887 $dir->delete();
1888 }
1889 return true;
8303eb84 1890}
1891
4636bf83 1892/**
8aff1574
SH
1893 * Prints the heads for a page
1894 *
1895 * @param stdClass $course
1896 * @param stdClass $cm
1897 * @param stdClass $data
4636bf83 1898 * @param string $currenttab
1899 */
8303eb84 1900function data_print_header($course, $cm, $data, $currenttab='') {
b572ce19 1901
b0ff558c 1902 global $CFG, $displaynoticegood, $displaynoticebad, $OUTPUT, $PAGE;
b572ce19 1903
b0ff558c 1904 $PAGE->set_title($data->name);
b0ff558c 1905 echo $OUTPUT->header();
b2dc6880 1906 echo $OUTPUT->heading(format_string($data->name));
8303eb84 1907
b8b554ac 1908// Groups needed for Add entry tab
055f2185 1909 $currentgroup = groups_get_activity_group($cm);
5d59cbe9 1910 $groupmode = groups_get_activity_groupmode($cm);
b572ce19 1911
668fc89a 1912 // Print the tabs
b572ce19 1913
8303eb84 1914 if ($currenttab) {
138e480e 1915 include('tabs.php');
8303eb84 1916 }
b572ce19 1917
b8b554ac 1918 // Print any notices
b572ce19 1919
8303eb84 1920 if (!empty($displaynoticegood)) {
4102b449 1921 echo $OUTPUT->notification($displaynoticegood, 'notifysuccess'); // good (usually green)
8303eb84 1922 } else if (!empty($displaynoticebad)) {
4102b449 1923 echo $OUTPUT->notification($displaynoticebad); // bad (usuually red)
8303eb84 1924 }
1925}
7f258664 1926
4636bf83 1927/**
1928 * @global object
1929 * @param object $data
1930 * @param mixed $currentgroup
1931 * @param int $groupmode
1932 * @return bool
1933 */
04366d79 1934function data_user_can_add_entry($data, $currentgroup, $groupmode) {
cca1547e 1935 global $USER;
b572ce19 1936
cca1547e 1937 if (!$cm = get_coursemodule_from_instance('data', $data->id)) {
29c1bb15 1938 print_error('invalidcoursemodule');
cca1547e 1939 }
1940 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
b572ce19 1941
cca1547e 1942 if (!has_capability('mod/data:writeentry', $context) and !has_capability('mod/data:manageentries',$context)) {
1943 return false;
1944 }
b572ce19 1945
2742ffe7
AD
1946 //if in the view only time window
1947 $now = time();
1948 if ($now>$data->timeviewfrom && $now<$data->timeviewto) {
1949 return false;
1950 }
1951
04366d79 1952 if (!$groupmode or has_capability('moodle/site:accessallgroups', $context)) {
1953 return true;
1954 }
1955
cca1547e 1956 if ($currentgroup) {
2c386f82 1957 return groups_is_member($currentgroup);
cca1547e 1958 } else {
1959 //else it might be group 0 in visible mode
1960 if ($groupmode == VISIBLEGROUPS){
cca1547e 1961 return true;
04366d79 1962 } else {
1963 return false;
cca1547e 1964 }
1965 }
1966}
1967
50194545 1968
4636bf83 1969/**
1970 * @return bool
1971 */
50194545 1972function is_directory_a_preset($directory) {
1973 $directory = rtrim($directory, '/\\') . '/';
2dc6be3f 1974 $status = file_exists($directory.'singletemplate.html') &&
1975 file_exists($directory.'listtemplate.html') &&
1976 file_exists($directory.'listtemplateheader.html') &&
1977 file_exists($directory.'listtemplatefooter.html') &&
1978 file_exists($directory.'addtemplate.html') &&
1979 file_exists($directory.'rsstemplate.html') &&
1980 file_exists($directory.'rsstitletemplate.html') &&
1981 file_exists($directory.'csstemplate.css') &&
1982 file_exists($directory.'jstemplate.js') &&
1983 file_exists($directory.'preset.xml');
b572ce19 1984
2dc6be3f 1985 return $status;
50194545 1986}
1987
4636bf83 1988/**
cba87c36 1989 * Abstract class used for data preset importers
4636bf83 1990 */
cba87c36 1991abstract class data_preset_importer {
b572ce19 1992
cba87c36
SH
1993 protected $course;
1994 protected $cm;
1995 protected $module;
1996 protected $directory;
50194545 1997
4636bf83 1998 /**
cba87c36
SH
1999 * Constructor
2000 *
2001 * @param stdClass $course
2002 * @param stdClass $cm
2003 * @param stdClass $module
2004 * @param string $directory
4636bf83 2005 */
cba87c36 2006 public function __construct($course, $cm, $module, $directory) {
50194545 2007 $this->course = $course;
2008 $this->cm = $cm;
cba87c36
SH
2009 $this->module = $module;
2010 $this->directory = $directory;
2011 }
2012
2013 /**
2014 * Returns the name of the directory the preset is located in
2015 * @return string
2016 */
2017 public function get_directory() {
2018 return basename($this->directory);
50194545 2019 }
263819c8
AD
2020
2021 /**
2022 * Retreive the contents of a file. That file may either be in a conventional directory of the Moodle file storage
2023 * @param file_storage $filestorage. should be null if using a conventional directory
2024 * @param stored_file $fileobj the directory to look in. null if using a conventional directory
2025 * @param string $dir the directory to look in. null if using the Moodle file storage
2026 * @param string $filename the name of the file we want
2027 * @return string the contents of the file
2028 */
2029 public function data_preset_get_file_contents(&$filestorage, &$fileobj, $dir, $filename) {
2030 if(empty($filestorage) || empty($fileobj)) {
2031 if (substr($dir, -1)!='/') {
2032 $dir .= '/';
2033 }
2034 return file_get_contents($dir.$filename);
2035 } else {
2036 $file = $filestorage->get_file(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA, 0, $fileobj->get_filepath(), $filename);
2037 return $file->get_content();
2038 }
2039
2040 }
4636bf83 2041 /**
cba87c36
SH
2042 * Gets the preset settings
2043 * @global moodle_database $DB
2044 * @return stdClass
4636bf83 2045 */
cba87c36
SH
2046 public function get_preset_settings() {
2047 global $DB;
50194545 2048
263819c8 2049 $fs = $fileobj = null;
cba87c36 2050 if (!is_directory_a_preset($this->directory)) {
263819c8 2051 //maybe the user requested a preset stored in the Moodle file storage
375053ed 2052
263819c8
AD
2053 $fs = get_file_storage();
2054 $files = $fs->get_area_files(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA);
2055
2056 foreach ($files as $file) {
2057 if (($file->is_directory() && $file->get_filepath()=='/') || !$file->is_directory()) {
2058 continue;
2059 }
2060 $presetname = trim($file->get_filepath(), '/');
2061 if ($presetname==$this->module->name) {
2062 $this->directory = $presetname;
2063 $fileobj = $file;
2064 }
2065 }
2066
2067 if (empty($fileobj)) {
2068 print_error('invalidpreset', 'data', '', $this->directory);
2069 }
50194545 2070 }
2071
cba87c36
SH
2072 $allowed_settings = array(
2073 'intro',
2074 'comments',
2075 'requiredentries',
2076 'requiredentriestoview',
2077 'maxentries',
2078 'rssarticles',
2079 'approval',
2080 'defaultsortdir',
2081 'defaultsort');
2082
2083 $result = new stdClass;
2084 $result->settings = new stdClass;
2085 $result->importfields = array();
2086 $result->currentfields = $DB->get_records('data_fields', array('dataid'=>$this->module->id));
2087 if (!$result->currentfields) {
2088 $result->currentfields = array();
2089 }
2090
2091
50194545 2092 /* Grab XML */
263819c8 2093 $presetxml = $this->data_preset_get_file_contents($fs, $fileobj, $this->directory,'preset.xml');
143c1eb9 2094 $parsedxml = xmlize($presetxml, 0);
50194545 2095
2096 /* First, do settings. Put in user friendly array. */
2097 $settingsarray = $parsedxml['preset']['#']['settings'][0]['#'];
cba87c36 2098 $result->settings = new StdClass();
50194545 2099 foreach ($settingsarray as $setting => $value) {
cba87c36 2100 if (!is_array($value) || !in_array($setting, $allowed_settings)) {
eaa30818 2101 // unsupported setting
2102 continue;
2103 }
cba87c36 2104 $result->settings->$setting = $value[0]['#'];
50194545 2105 }
2106
2107 /* Now work out fields to user friendly array */
2108 $fieldsarray = $parsedxml['preset']['#']['field'];
50194545 2109 foreach ($fieldsarray as $field) {
02007b01 2110 if (!is_array($field)) {
2111 continue;
2112 }
50194545 2113 $f = new StdClass();
2114 foreach ($field['#'] as $param => $value) {
02007b01 2115 if (!is_array($value)) {
2116 continue;
2117 }
9c00b5d7 2118 $f->$param = $value[0]['#'];
50194545 2119 }
cba87c36 2120 $f->dataid = $this->module->id;
50194545 2121 $f->type = clean_param($f->type, PARAM_ALPHA);
cba87c36 2122 $result->importfields[] = $f;
50194545 2123 }
50194545 2124 /* Now add the HTML templates to the settings array so we can update d */
263819c8
AD
2125 $result->settings->singletemplate = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"singletemplate.html");
2126 $result->settings->listtemplate = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"listtemplate.html");
2127 $result->settings->listtemplateheader = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"listtemplateheader.html");
2128 $result->settings->listtemplatefooter = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"listtemplatefooter.html");
2129 $result->settings->addtemplate = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"addtemplate.html");
2130 $result->settings->rsstemplate = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"rsstemplate.html");
2131 $result->settings->rsstitletemplate = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"rsstitletemplate.html");
2132 $result->settings->csstemplate = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"csstemplate.css");
2133 $result->settings->jstemplate = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"jstemplate.js");
50194545 2134
2dc6be3f 2135 //optional
cba87c36 2136 if (file_exists($this->directory."/asearchtemplate.html")) {
263819c8 2137 $result->settings->asearchtemplate = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"asearchtemplate.html");
2dc6be3f 2138 } else {
cba87c36 2139 $result->settings->asearchtemplate = NULL;
50194545 2140 }
cba87c36 2141 $result->settings->instance = $this->module->id;
b572ce19 2142
cba87c36 2143 return $result;
50194545 2144 }
2145
4636bf83 2146 /**
cba87c36 2147 * Import the preset into the given database module
4636bf83 2148 * @return bool
2149 */
cba87c36 2150 function import($overwritesettings) {
fe9d5f7e 2151 global $DB, $CFG;
50194545 2152
cba87c36
SH
2153 $params = $this->get_preset_settings();
2154 $settings = $params->settings;
2155 $newfields = $params->importfields;
2156 $currentfields = $params->currentfields;
50194545 2157 $preservedfields = array();
b572ce19 2158
50194545 2159 /* Maps fields and makes new ones */
2160 if (!empty($newfields)) {
2161 /* We require an injective mapping, and need to know what to protect */
2162 foreach ($newfields as $nid => $newfield) {
2163 $cid = optional_param("field_$nid", -1, PARAM_INT);
cba87c36
SH
2164 if ($cid == -1) {
2165 continue;
2166 }
29c1bb15 2167 if (array_key_exists($cid, $preservedfields)){
2168 print_error('notinjectivemap', 'data');
8429163d 2169 }
50194545 2170 else $preservedfields[$cid] = true;
2171 }
b572ce19 2172
50194545 2173 foreach ($newfields as $nid => $newfield) {
2174 $cid = optional_param("field_$nid", -1, PARAM_INT);
b572ce19 2175
50194545 2176 /* A mapping. Just need to change field params. Data kept. */
2177 if ($cid != -1 and isset($currentfields[$cid])) {
cba87c36 2178 $fieldobject = data_get_field_from_id($currentfields[$cid]->id, $this->module);
50194545 2179 foreach ($newfield as $param => $value) {
2180 if ($param != "id") {
2181 $fieldobject->field->$param = $value;
2182 }
2183 }
2184 unset($fieldobject->field->similarfield);
2185 $fieldobject->update_field();
2186 unset($fieldobject);
cba87c36
SH
2187 } else {
2188 /* Make a new field */
50194545 2189 include_once("field/$newfield->type/field.class.php");
b572ce19 2190
50194545 2191 if (!isset($newfield->description)) {
2192 $newfield->description = '';
2193 }
2194 $classname = 'data_field_'.$newfield->type;
cba87c36 2195 $fieldclass = new $classname($newfield, $this->module);
50194545 2196 $fieldclass->insert_field();
2197 unset($fieldclass);
2198 }
2199 }
2200 }
2201
2202 /* Get rid of all old unused data */
2203 if (!empty($preservedfields)) {
2204 foreach ($currentfields as $cid => $currentfield) {
2205 if (!array_key_exists($cid, $preservedfields)) {
2206 /* Data not used anymore so wipe! */
2207 print "Deleting field $currentfield->name<br />";
b572ce19 2208
50194545 2209 $id = $currentfield->id;
2210 //Why delete existing data records and related comments/ratings??
9c00b5d7 2211 $DB->delete_records('data_content', array('fieldid'=>$id));
2212 $DB->delete_records('data_fields', array('id'=>$id));
50194545 2213 }
2214 }
2215 }
2216
cba87c36 2217 // handle special settings here
eaa30818 2218 if (!empty($settings->defaultsort)) {
2219 if (is_numeric($settings->defaultsort)) {
668fc89a 2220 // old broken value
eaa30818 2221 $settings->defaultsort = 0;
2222 } else {
cba87c36 2223 $settings->defaultsort = (int)$DB->get_field('data_fields', 'id', array('dataid'=>$this->module->id, 'name'=>$settings->defaultsort));
eaa30818 2224 }
2225 } else {
2226 $settings->defaultsort = 0;
2227 }
2228
2229 // do we want to overwrite all current database settings?
8528bf26 2230 if ($overwritesettings) {
eaa30818 2231 // all supported settings
8528bf26 2232 $overwrite = array_keys((array)$settings);
2233 } else {
eaa30818 2234 // only templates and sorting
8528bf26 2235 $overwrite = array('singletemplate', 'listtemplate', 'listtemplateheader', 'listtemplatefooter',
2236 'addtemplate', 'rsstemplate', 'rsstitletemplate', 'csstemplate', 'jstemplate',
eaa30818 2237 'asearchtemplate', 'defaultsortdir', 'defaultsort');
8528bf26 2238 }
2239
eaa30818 2240 // now overwrite current data settings
cba87c36 2241 foreach ($this->module as $prop=>$unused) {
8528bf26 2242 if (in_array($prop, $overwrite)) {
cba87c36 2243 $this->module->$prop = $settings->$prop;
13534ef7
ML
2244 }
2245 }
db0f2a44 2246
cba87c36 2247 data_update_instance($this->module);
50194545 2248
cba87c36
SH
2249 return $this->cleanup();
2250 }
b572ce19 2251
cba87c36
SH
2252 /**
2253 * Any clean up routines should go here
2254 * @return bool
2255 */
2256 public function cleanup() {
50194545 2257 return true;
2258 }
2259}
2260
cba87c36
SH
2261/**
2262 * Data preset importer for uploaded presets
2263 */
2264class data_preset_upload_importer extends data_preset_importer {
2265 public function __construct($course, $cm, $module, $filepath) {
2266 global $USER;
2267 if (is_file($filepath)) {
2268 $fp = get_file_packer();
2269 if ($fp->extract_to_pathname($filepath, $filepath.'_extracted')) {
2270 fulldelete($filepath);
2271 }
2272 $filepath .= '_extracted';
2273 }
2274 parent::__construct($course, $cm, $module, $filepath);
2275 }
2276 public function cleanup() {
2277 return fulldelete($this->directory);
2278 }
2279}
2280
2281/**
2282 * Data preset importer for existing presets
2283 */
2284class data_preset_existing_importer extends data_preset_importer {
2285 protected $userid;
2286 public function __construct($course, $cm, $module, $fullname) {
2287 global $USER;
2288 list($userid, $shortname) = explode('/', $fullname, 2);
2289 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
2290 if ($userid && ($userid != $USER->id) && !has_capability('mod/data:manageuserpresets', $context) && !has_capability('mod/data:viewalluserpresets', $context)) {
2291 throw new coding_exception('Invalid preset provided');
2292 }
2293
2294 $this->userid = $userid;
2295 $filepath = data_preset_path($course, $userid, $shortname);
2296 parent::__construct($course, $cm, $module, $filepath);
2297 }
2298 public function get_userid() {
2299 return $this->userid;
2300 }
2301}
2302
4636bf83 2303/**
2304 * @global object
2305 * @global object
2306 * @param object $course
2307 * @param int $userid
2308 * @param string $shortname
2309 * @return string
2310 */
50194545 2311function data_preset_path($course, $userid, $shortname) {
2312 global $USER, $CFG;
b572ce19 2313
50194545 2314 $context = get_context_instance(CONTEXT_COURSE, $course->id);
b572ce19 2315
50194545 2316 $userid = (int)$userid;
b572ce19 2317
263819c8 2318 $path = null;
50194545 2319 if ($userid > 0 && ($userid == $USER->id || has_capability('mod/data:viewalluserpresets', $context))) {
263819c8 2320 $path = $CFG->dataroot.'/data/preset/'.$userid.'/'.$shortname;
50194545 2321 } else if ($userid == 0) {
263819c8 2322 $path = $CFG->dirroot.'/mod/data/preset/'.$shortname;
50194545 2323 } else if ($userid < 0) {
263819c8 2324 $path = $CFG->dataroot.'/temp/data/'.-$userid.'/'.$shortname;
50194545 2325 }
b572ce19 2326
263819c8 2327 return $path;
50194545 2328}
0b5a80a1 2329
2330/**
2331 * Implementation of the function for printing the form elements that control
2332 * whether the course reset functionality affects the data.
4636bf83 2333 *
0b5a80a1 2334 * @param $mform form passed by reference
2335 */
2336function data_reset_course_form_definition(&$mform) {
2337 $mform->addElement('header', 'dataheader', get_string('modulenameplural', 'data'));
2338 $mform->addElement('checkbox', 'reset_data', get_string('deleteallentries','data'));
2339
2340 $mform->addElement('checkbox', 'reset_data_notenrolled', get_string('deletenotenrolled', 'data'));
2341 $mform->disabledIf('reset_data_notenrolled', 'reset_data', 'checked');
2342
2343 $mform->addElement('checkbox', 'reset_data_ratings', get_string('deleteallratings'));
2344 $mform->disabledIf('reset_data_ratings', 'reset_data', 'checked');
2345
2346 $mform->addElement('checkbox', 'reset_data_comments', get_string('deleteallcomments'));
2347 $mform->disabledIf('reset_data_comments', 'reset_data', 'checked');
2348}
2349
2350/**
2351 * Course reset form defaults.
4636bf83 2352 * @return array
0b5a80a1 2353 */
2354function data_reset_course_form_defaults($course) {
2355 return array('reset_data'=>0, 'reset_data_ratings'=>1, 'reset_data_comments'=>1, 'reset_data_notenrolled'=>0);
2356}
2357
2358/**
2359 * Removes all grades from gradebook
4636bf83 2360 *
2361 * @global object
2362 * @global object
0b5a80a1 2363 * @param int $courseid
4636bf83 2364 * @param string $type optional type
0b5a80a1 2365 */
2366function data_reset_gradebook($courseid, $type='') {
9c00b5d7 2367 global $CFG, $DB;
0b5a80a1 2368
2369 $sql = "SELECT d.*, cm.idnumber as cmidnumber, d.course as courseid
9c00b5d7 2370 FROM {data} d, {course_modules} cm, {modules} m
2371 WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id AND d.course=?";
0b5a80a1 2372
9c00b5d7 2373 if ($datas = $DB->get_records_sql($sql, array($courseid))) {
0b5a80a1 2374 foreach ($datas as $data) {
2375 data_grade_item_update($data, 'reset');
2376 }
2377 }
2378}
2379
2380/**
72d2982e 2381 * Actual implementation of the reset course functionality, delete all the
0b5a80a1 2382 * data responses for course $data->courseid.
4636bf83 2383 *
2384 * @global object
2385 * @global object
2386 * @param object $data the data submitted from the reset course.
0b5a80a1 2387 * @return array status array
2388 */
2389function data_reset_userdata($data) {
9c00b5d7 2390 global $CFG, $DB;
0b5a80a1 2391 require_once($CFG->libdir.'/filelib.php');
d251b259 2392 require_once($CFG->dirroot.'/rating/lib.php');
b572ce19 2393
0b5a80a1 2394 $componentstr = get_string('modulenameplural', 'data');
2395 $status = array();
b572ce19 2396
0b5a80a1 2397 $allrecordssql = "SELECT r.id
9c00b5d7 2398 FROM {data_records} r
2399 INNER JOIN {data} d ON r.dataid = d.id
2400 WHERE d.course = ?";
0b5a80a1 2401
2402 $alldatassql = "SELECT d.id
9c00b5d7 2403 FROM {data} d
2404 WHERE d.course=?";
0b5a80a1 2405
d251b259
AD
2406 $rm = new rating_manager();
2407 $ratingdeloptions = new stdclass();
2408
0b5a80a1 2409 // delete entries if requested
2410 if (!empty($data->reset_data)) {
d251b259 2411 //$DB->delete_records_select('data_ratings', "recordid IN ($allrecordssql)", array($data->courseid));
e998effa 2412 $DB->delete_records_select('comments', "itemid IN ($allrecordssql) AND commentarea='database_entry'", array($data->courseid));
9c00b5d7 2413 $DB->delete_records_select('data_content', "recordid IN ($allrecordssql)", array($data->courseid));
2414 $DB->delete_records_select('data_records', "dataid IN ($alldatassql)", array($data->courseid));
0b5a80a1 2415
9c00b5d7 2416 if ($datas = $DB->get_records_sql($alldatassql, array($data->courseid))) {
0b5a80a1 2417 foreach ($datas as $dataid=>$unused) {
2418 fulldelete("$CFG->dataroot/$data->courseid/moddata/data/$dataid");
d251b259
AD
2419
2420 if (!$cm = get_coursemodule_from_instance('data', $dataid)) {
2421 continue;
2422 }
2423 $datacontext = get_context_instance(CONTEXT_MODULE, $cm->id);
2424
2425 $ratingdeloptions->contextid = $datacontext->id;
2426 $rm->delete_ratings($ratingdeloptions);
0b5a80a1 2427 }
2428 }
b572ce19 2429
0b5a80a1 2430 if (empty($data->reset_gradebook_grades)) {
2431 // remove all grades from gradebook
2432 data_reset_gradebook($data->courseid);
2433 }
2434 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallentries', 'data'), 'error'=>false);
2435 }
2436
2437 // remove entries by users not enrolled into course
2438 if (!empty($data->reset_data_notenrolled)) {
2439 $recordssql = "SELECT r.id, r.userid, r.dataid, u.id AS userexists, u.deleted AS userdeleted
9c00b5d7 2440 FROM {data_records} r
2441 JOIN {data} d ON r.dataid = d.id
2442 LEFT JOIN {user} u ON r.userid = u.id
2443 WHERE d.course = ? AND r.userid > 0";
0b5a80a1 2444
2445 $course_context = get_context_instance(CONTEXT_COURSE, $data->courseid);
2446 $notenrolled = array();
2447 $fields = array();
9c00b5d7 2448 if ($rs = $DB->get_recordset_sql($recordssql, array($data->courseid))) {
2449 foreach ($rs as $record) {
0b5a80a1 2450 if (array_key_exists($record->userid, $notenrolled) or !$record->userexists or $record->userdeleted
4f0c2d00 2451 or !is_enrolled($course_context, $record->userid)) {
d251b259
AD
2452 //delete ratings
2453 //$DB->delete_records('data_ratings', array('recordid'=>$record->id));
2454 if (!$cm = get_coursemodule_from_instance('data', $record->dataid)) {
2455 continue;
2456 }
2457 $datacontext = get_context_instance(CONTEXT_MODULE, $cm->id);
2458 $ratingdeloptions->contextid = $datacontext->id;
2459 $ratingdeloptions->itemid = $record->id;
2460 $rm->delete_ratings($ratingdeloptions);
2461
e998effa 2462 $DB->delete_records('comments', array('itemid'=>$record->id, 'commentarea'=>'database_entry'));
9c00b5d7 2463 $DB->delete_records('data_content', array('recordid'=>$record->id));
2464 $DB->delete_records('data_records', array('id'=>$record->id));
0b5a80a1 2465 // HACK: this is ugly - the recordid should be before the fieldid!
2466 if (!array_key_exists($record->dataid, $fields)) {
9c00b5d7 2467 if ($fs = $DB->get_records('data_fields', array('dataid'=>$record->dataid))) {
0b5a80a1 2468 $fields[$record->dataid] = array_keys($fs);
2469 } else {
2470 $fields[$record->dataid] = array();
2471 }
2472 }
2473 foreach($fields[$record->dataid] as $fieldid) {
2474 fulldelete("$CFG->dataroot/$data->courseid/moddata/data/$record->dataid/$fieldid/$record->id");
2475 }
2476 $notenrolled[$record->userid] = true;
2477 }
2478 }
9c00b5d7 2479 $rs->close();
0b5a80a1 2480 $status[] = array('component'=>$componentstr, 'item'=>get_string('deletenotenrolled', 'data'), 'error'=>false);
2481 }
2482 }
2483
2484 // remove all ratings
2485 if (!empty($data->reset_data_ratings)) {
d251b259
AD
2486 //$DB->delete_records_select('data_ratings', "recordid IN ($allrecordssql)", array($data->courseid));
2487 if ($datas = $DB->get_records_sql($alldatassql, array($data->courseid))) {
2488 foreach ($datas as $dataid=>$unused) {
2489 if (!$cm = get_coursemodule_from_instance('data', $dataid)) {
2490 continue;
2491 }
2492 $datacontext = get_context_instance(CONTEXT_MODULE, $cm->id);
2493
2494 $ratingdeloptions->contextid = $datacontext->id;
2495 $rm->delete_ratings($ratingdeloptions);
2496 }
2497 }
0b5a80a1 2498
2499 if (empty($data->reset_gradebook_grades)) {
2500 // remove all grades from gradebook
2501 data_reset_gradebook($data->courseid);
2502 }
b572ce19 2503
0b5a80a1 2504 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallratings'), 'error'=>false);
2505 }
2506
2507 // remove all comments
2508 if (!empty($data->reset_data_comments)) {
e998effa 2509 $DB->delete_records_select('comments', "itemid IN ($allrecordssql) AND commentarea='database_entry'", array($data->courseid));
0b5a80a1 2510 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallcomments'), 'error'=>false);
2511 }
2512
b8b554ac 2513 // updating dates - shift may be negative too
0b5a80a1 2514 if ($data->timeshift) {
2515 shift_course_mod_dates('data', array('timeavailablefrom', 'timeavailableto', 'timeviewfrom', 'timeviewto'), $data->timeshift, $data->courseid);
2516 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
2517 }
b572ce19 2518
0b5a80a1 2519 return $status;
2520}
f432bebf 2521
2522/**
2523 * Returns all other caps used in module
4636bf83 2524 *
2525 * @return array
f432bebf 2526 */
2527function data_get_extra_capabilities() {
16b86ae4 2528 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 2529}
2530
18a2a0cb 2531/**
2532 * @param string $feature FEATURE_xx constant for requested feature
2533 * @return mixed True if module supports feature, null if doesn't know
2534 */
2535function data_supports($feature) {
2536 switch($feature) {
42f103be 2537 case FEATURE_GROUPS: return true;
2538 case FEATURE_GROUPINGS: return true;
2539 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 2540 case FEATURE_MOD_INTRO: return true;
18a2a0cb 2541 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
42f103be 2542 case FEATURE_GRADE_HAS_GRADE: return true;
2543 case FEATURE_GRADE_OUTCOMES: return true;
d251b259 2544 case FEATURE_RATE: return true;
fd3f6bf9 2545 case FEATURE_BACKUP_MOODLE2: return true;
42f103be 2546
18a2a0cb 2547 default: return null;
17da2e6f 2548 }
18a2a0cb 2549}
4636bf83 2550/**
2551 * @global object
2552 * @param array $export
2553 * @param string $delimiter_name
2554 * @param object $database
2555 * @param int $count
2556 * @param bool $return
2557 * @return string|void
2558 */
1bf8c6b2 2559function data_export_csv($export, $delimiter_name, $dataname, $count, $return=false) {
ecd06483 2560 global $CFG;
2561 require_once($CFG->libdir . '/csvlib.class.php');
20988152 2562 $delimiter = csv_import_reader::get_delimiter($delimiter_name);
fa7f82be 2563 $filename = clean_filename("{$dataname}-{$count}_record");
20988152 2564 if ($count > 1) {
2565 $filename .= 's';
2566 }
2567 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
fa7f82be 2568 $filename .= clean_filename("-{$delimiter_name}_separated");
20988152 2569 $filename .= '.csv';
1bf8c6b2 2570 if (empty($return)) {
ecd06483 2571 header("Content-Type: application/download\n");
2572 header("Content-Disposition: attachment; filename=$filename");
2573 header('Expires: 0');
2574 header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
2575 header('Pragma: public');
2576 }
20988152 2577 $encdelim = '&#' . ord($delimiter) . ';';
ecd06483 2578 $returnstr = '';
20988152 2579 foreach($export as $row) {
2580 foreach($row as $key => $column) {
2581 $row[$key] = str_replace($delimiter, $encdelim, $column);
2582 }
ecd06483 2583 $returnstr .= implode($delimiter, $row) . "\n";
2584 }
1bf8c6b2 2585 if (empty($return)) {
ecd06483 2586 echo $returnstr;
2587 return;
20988152 2588 }
1bf8c6b2 2589 return $returnstr;
20988152 2590}
2591
4636bf83 2592/**
2593 * @global object
2594 * @param array $export
2595 * @param string $dataname
2596 * @param int $count
2597 * @return string
2598 */
1bf8c6b2 2599function data_export_xls($export, $dataname, $count) {
20988152 2600 global $CFG;
2601 require_once("$CFG->libdir/excellib.class.php");
fa7f82be 2602 $filename = clean_filename("{$dataname}-{$count}_record");
20988152 2603 if ($count > 1) {
2604 $filename .= 's';
2605 }
2606 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
2607 $filename .= '.xls';
ecd06483 2608
2609 $filearg = '-';
ecd06483 2610 $workbook = new MoodleExcelWorkbook($filearg);
1bf8c6b2 2611 $workbook->send($filename);
20988152 2612 $worksheet = array();
2613 $worksheet[0] =& $workbook->add_worksheet('');
2614 $rowno = 0;
2615 foreach ($export as $row) {
2616 $colno = 0;
2617 foreach($row as $col) {
2618 $worksheet[0]->write($rowno, $colno, $col);
2619 $colno++;
2620 }
2621 $rowno++;
2622 }
2623 $workbook->close();
ecd06483 2624 return $filename;
20988152 2625}
2626
4636bf83 2627/**
2628 * @global object
2629 * @param array $export
2630 * @param string $dataname
2631 * @param int $count
2632 * @param string
2633 */
1bf8c6b2 2634function data_export_ods($export, $dataname, $count) {
20988152 2635 global $CFG;
2636 require_once("$CFG->libdir/odslib.class.php");
fa7f82be 2637 $filename = clean_filename("{$dataname}-{$count}_record");
20988152 2638 if ($count > 1) {
2639 $filename .= 's';
2640 }
2641 $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
2642 $filename .= '.ods';
ecd06483 2643 $filearg = '-';
1bf8c6b2 2644 $workbook = new MoodleODSWorkbook($filearg);
2645 $workbook->send($filename);
20988152 2646 $worksheet = array();
2647 $worksheet[0] =& $workbook->add_worksheet('');
2648 $rowno = 0;
2649 foreach ($export as $row) {
2650 $colno = 0;
2651 foreach($row as $col) {
2652 $worksheet[0]->write($rowno, $colno, $col);
2653 $colno++;
2654 }
2655 $rowno++;
2656 }
2657 $workbook->close();
ecd06483 2658 return $filename;
2659}
2660
4636bf83 2661/**
2662 * @global object
2663 * @param int $dataid
2664 * @param array $fields
2665 * @param array $selectedfields
2666 * @return array
2667 */
ecd06483 2668function data_get_exportdata($dataid, $fields, $selectedfields) {
2669 global $DB;
2670
2671 $exportdata = array();
2672
2673 // populate the header in first row of export
2674 foreach($fields as $key => $field) {
2675 if (!in_array($field->field->id, $selectedfields)) {
2676 // ignore values we aren't exporting
2677 unset($fields[$key]);
2678 } else {
2679 $exportdata[0][] = $field->field->name;
2680 }
2681 }
2682
2683 $datarecords = $DB->get_records('data_records', array('dataid'=>$dataid));
2684 ksort($datarecords);
2685 $line = 1;
2686 foreach($datarecords as $record) {
2687 // get content indexed by fieldid
2688 if( $content = $DB->get_records('data_content', array('recordid'=>$record->id), 'fieldid', 'fieldid, content, content1, content2, content3, content4') ) {
2689 foreach($fields as $field) {
2690 $contents = '';
2691 if(isset($content[$field->field->id])) {
2692 $contents = $field->export_text_value($content[$field->field->id]);
2693 }
2694 $exportdata[$line][] = $contents;
2695 }
2696 }
2697 $line++;
2698 }
2699 $line--;
2700 return $exportdata;
2701}
2702
8429163d 2703/**
2704 * Lists all browsable file areas
4636bf83 2705 *
2706 * @param object $course
2707 * @param object $cm
2708 * @param object $context
2709 * @return array
8429163d 2710 */
2711function data_get_file_areas($course, $cm, $context) {
2712 $areas = array();
8429163d 2713 return $areas;
2714}
2715
2716/**
2717 * Serves the data attachments. Implements needed access control ;-)
4636bf83 2718 *
4636bf83 2719 * @param object $course
64f93798 2720 * @param object $cm
4636bf83 2721 * @param object $context
86900a93 2722 * @param string $filearea
4636bf83 2723 * @param array $args
86900a93 2724 * @param bool $forcedownload
2725 * @return bool false if file not found, does not return if found - justsend the file
8429163d 2726 */
64f93798 2727function data_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
8429163d 2728 global $CFG, $DB;
2729
64f93798 2730 if ($context->contextlevel != CONTEXT_MODULE) {
8429163d 2731 return false;
2732 }
2733
375053ed 2734 require_course_login($course, true, $cm);
64f93798
PS
2735
2736 if ($filearea === 'content') {
8429163d 2737 $contentid = (int)array_shift($args);
2738
2739 if (!$content = $DB->get_record('data_content', array('id'=>$contentid))) {
2740 return false;
2741 }
2742
2743 if (!$field = $DB->get_record('data_fields', array('id'=>$content->fieldid))) {
2744 return false;
2745 }
2746
2747 if (!$record = $DB->get_record('data_records', array('id'=>$content->recordid))) {
2748 return false;
2749 }
2750
2751 if (!$data = $DB->get_record('data', array('id'=>$field->dataid))) {
2752 return false;
2753 }
2754
f011a8fb
PS
2755 if ($data->id != $cm->instance) {
2756 // hacker attempt - context does not match the contentid
2757 return false;
2758 }
2759
8429163d 2760 //check if approved
f011a8fb 2761 if ($data->approval and !$record->approved and !data_isowner($record) and !has_capability('mod/data:approve', $context)) {
8429163d 2762 return false;
2763 }
2764
2765 // group access
2766 if ($record->groupid) {
0a8a7b6c 2767 $groupmode = groups_get_activity_groupmode($cm, $course);
8429163d 2768 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
2769 if (!groups_is_member($record->groupid)) {
2770 return false;
2771 }
2772 }
2773 }
2774
0a8a7b6c 2775 $fieldobj = data_get_field($field, $data, $cm);
8429163d 2776
64f93798
PS
2777 $relativepath = implode('/', $args);
2778 $fullpath = "/$context->id/mod_data/content/$content->id/$relativepath";
8429163d 2779
2780 if (!$fieldobj->file_ok($relativepath)) {
2781 return false;
2782 }
2783
2784 $fs = get_file_storage();
2785 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
2786 return false;
2787 }
2788
2789 // finally send the file
2790 send_stored_file($file, 0, 0, true); // download MUST be forced - security!
2791 }
2792
2793 return false;
2794}
2795
29b64a22 2796
2797function data_extend_navigation($navigation, $course, $module, $cm) {
2798 global $CFG, $OUTPUT, $USER, $DB;
2799
2800 $rid = optional_param('rid', 0, PARAM_INT);
2801
2802 $data = $DB->get_record('data', array('id'=>$cm->instance));
2803 $currentgroup = groups_get_activity_group($cm);
2804 $groupmode = groups_get_activity_groupmode($cm);
2805
2806 $numentries = data_numentries($data);
2807 /// Check the number of entries required against the number of entries already made (doesn't apply to teachers)
4f0c2d00 2808 if ($data->requiredentries > 0 && $numentries < $data->requiredentries && !has_capability('mod/data:manageentries', get_context_instance(CONTEXT_MODULE, $cm->id))) {
29b64a22 2809 $data->entriesleft = $data->requiredentries - $numentries;
3406acde
SH
2810 $entriesnode = $navigation->add(get_string('entrieslefttoadd', 'data', $data));
2811 $entriesnode->add_class('note');
29b64a22 2812 }
2813
a6855934 2814 $navigation->add(get_string('list', 'data'), new moodle_url('/mod/data/view.php', array('d'=>$cm->instance)));
29b64a22 2815 if (!empty($rid)) {
a6855934 2816 $navigation->add(get_string('single', 'data'), new moodle_url('/mod/data/view.php', array('d'=>$cm->instance, 'rid'=>$rid)));
29b64a22 2817 } else {
a6855934 2818 $navigation->add(get_string('single', 'data'), new moodle_url('/mod/data/view.php', array('d'=>$cm->instance, 'mode'=>'single')));
29b64a22 2819 }
a6855934 2820 $navigation->add(get_string('search', 'data'), new moodle_url('/mod/data/view.php', array('d'=>$cm->instance, 'mode'=>'search')));
29b64a22 2821}
2822
0b29477b
SH
2823/**
2824 * Adds module specific settings to the settings block
2825 *
2826 * @param settings_navigation $settings The settings navigation object
2827 * @param navigation_node $datanode The node to add module settings to
2828 */
2829function data_extend_settings_navigation(settings_navigation $settings, navigation_node $datanode) {
9e86f2e7 2830 global $PAGE, $DB, $CFG, $USER;
56115eea 2831
9e86f2e7 2832 $data = $DB->get_record('data', array("id" => $PAGE->cm->instance));
29b64a22 2833
29b64a22 2834 $currentgroup = groups_get_activity_group($PAGE->cm);
2835 $groupmode = groups_get_activity_groupmode($PAGE->cm);
2836
2837 if (data_user_can_add_entry($data, $currentgroup, $groupmode)) { // took out participation list here!
fa7f82be 2838 if (empty($editentry)) { //TODO: undefined
29b64a22 2839 $addstring = get_string('add', 'data');
2840 } else {
2841 $addstring = get_string('editentry', 'data');
2842 }
0b29477b 2843 $datanode->add($addstring, new moodle_url('/mod/data/edit.php', array('d'=>$PAGE->cm->instance)));
29b64a22 2844 }
2845
2846 if (has_capability(DATA_CAP_EXPORT, $PAGE->cm->context)) {
2847 // The capability required to Export database records is centrally defined in 'lib.php'
2848 // and should be weaker than those required to edit Templates, Fields and Presets.
0b29477b 2849 $datanode->add(get_string('export', 'data'), new moodle_url('/mod/data/export.php', array('d'=>$data->id)));
29b64a22 2850 }
bbdfd8ce
DC
2851 if (has_capability('mod/data:manageentries', $PAGE->cm->context)) {
2852 $datanode->add(get_string('import'), new moodle_url('/mod/data/import.php', array('d'=>$data->id)));
2853 }
29b64a22 2854
2855 if (has_capability('mod/data:managetemplates', $PAGE->cm->context)) {
2856 $currenttab = '';
2857 if ($currenttab == 'list') {
2858 $defaultemplate = 'listtemplate';
2859 } else if ($currenttab == 'add') {
2860 $defaultemplate = 'addtemplate';
2861 } else if ($currenttab == 'asearch') {
2862 $defaultemplate = 'asearchtemplate';
2863 } else {
2864 $defaultemplate = 'singletemplate';
2865 }
2866
3406acde 2867 $templates = $datanode->add(get_string('templates', 'data'));
29b64a22 2868
2869 $templatelist = array ('listtemplate', 'singletemplate', 'asearchtemplate', 'addtemplate', 'rsstemplate', 'csstemplate', 'jstemplate');
2870 foreach ($templatelist as $template) {
a6855934 2871 $templates->add(get_string($template, 'data'), new moodle_url('/mod/data/templates.php', array('d'=>$data->id,'mode'=>$template)));
29b64a22 2872 }
2873
0b29477b
SH
2874 $datanode->add(get_string('fields', 'data'), new moodle_url('/mod/data/field.php', array('d'=>$data->id)));
2875 $datanode->add(get_string('presets', 'data'), new moodle_url('/mod/data/preset.php', array('d'=>$data->id)));
29b64a22 2876 }
9e86f2e7
AD
2877
2878 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
2879 require_once("$CFG->libdir/rsslib.php");
2880
2881 $string = get_string('rsstype','forum');
2882
aa60291e 2883 $url = new moodle_url(rss_get_url($PAGE->cm->context->id, $USER->id, 'mod_data', $data->id));
9e86f2e7
AD
2884 $datanode->add($string, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', ''));
2885 }
7719b4db 2886}
cba87c36 2887
8aff1574
SH
2888/**
2889 * Save the database configuration as a preset.
2890 *
2891 * @param stdClass $course The course the database module belongs to.
2892 * @param stdClass $cm The course module record
2893 * @param stdClass $data The database record
2894 * @param string $path
2895 * @return bool
2896 */
2897function data_presets_save($course, $cm, $data, $path) {
2898 $fs = get_file_storage();
2899 $filerecord = new stdClass;
2900 $filerecord->contextid = DATA_PRESET_CONTEXT;
2901 $filerecord->component = DATA_PRESET_COMPONENT;
2902 $filerecord->filearea = DATA_PRESET_FILEAREA;
2903 $filerecord->itemid = 0;
2904 $filerecord->filepath = '/'.$path.'/';
2905
2906 $filerecord->filename = 'preset.xml';
2907 $fs->create_file_from_string($filerecord, data_presets_generate_xml($course, $cm, $data));
2908
2909 $filerecord->filename = 'singletemplate.html';
2910 $fs->create_file_from_string($filerecord, $data->singletemplate);
2911
2912 $filerecord->filename = 'listtemplateheader.html';
2913 $fs->create_file_from_string($filerecord, $data->listtemplateheader);
2914
2915 $filerecord->filename = 'listtemplate.html';
2916 $fs->create_file_from_string($filerecord, $data->listtemplate);
2917
2918 $filerecord->filename = 'listtemplatefooter.html';
2919 $fs->create_file_from_string($filerecord, $data->listtemplatefooter);
2920
2921 $filerecord->filename = 'addtemplate.html';
2922 $fs->create_file_from_string($filerecord, $data->addtemplate);
2923
2924 $filerecord->filename = 'rsstemplate.html';
2925 $fs->create_file_from_string($filerecord, $data->rsstemplate);
2926
2927 $filerecord->filename = 'rsstitletemplate.html';
2928 $fs->create_file_from_string($filerecord, $data->rsstitletemplate);
2929
2930 $filerecord->filename = 'csstemplate.css';
2931 $fs->create_file_from_string($filerecord, $data->csstemplate);
2932
2933 $filerecord->filename = 'jstemplate.js';
2934 $fs->create_file_from_string($filerecord, $data->jstemplate);
2935
2936 $filerecord->filename = 'asearchtemplate.html';
2937 $fs->create_file_from_string($filerecord, $data->asearchtemplate);
2938
2939 return true;
2940}
cba87c36 2941
8aff1574
SH
2942/**
2943 * Generates the XML for the database module provided
2944 *
2945 * @global moodle_database $DB
2946 * @param stdClass $course The course the database module belongs to.
2947 * @param stdClass $cm The course module record
2948 * @param stdClass $data The database record
2949 * @return string The XML for the preset
2950 */
2951function data_presets_generate_xml($course, $cm, $data) {
2952 global $DB;
800bb0f7 2953
cba87c36
SH
2954 // Assemble "preset.xml":
2955 $presetxmldata = "<preset>\n\n";
2956
2957 // Raw settings are not preprocessed during saving of presets
2958 $raw_settings = array(
2959 'intro',
2960 'comments',
2961 'requiredentries',
2962 'requiredentriestoview',
2963 'maxentries',
2964 'rssarticles',
2965 'approval',
2966 'defaultsortdir'
2967 );
2968
2969 $presetxmldata .= "<settings>\n";
2970 // First, settings that do not require any conversion
2971 foreach ($raw_settings as $setting) {
2972 $presetxmldata .= "<$setting>" . htmlspecialchars($data->$setting) . "</$setting>\n";
2973 }
2974
2975 // Now specific settings
2976 if ($data->defaultsort > 0 && $sortfield = data_get_field_from_id($data->defaultsort, $data)) {
2977 $presetxmldata .= '<defaultsort>' . htmlspecialchars($sortfield->field->name) . "</defaultsort>\n";
2978 } else {
2979 $presetxmldata .= "<defaultsort>0</defaultsort>\n";
2980 }
2981 $presetxmldata .= "</settings>\n\n";
cba87c36
SH
2982 // Now for the fields. Grab all that are non-empty
2983 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
2984 ksort($fields);
2985 if (!empty($fields)) {
2986 foreach ($fields as $field) {
2987 $presetxmldata .= "<field>\n";
2988 foreach ($field as $key => $value) {
2989 if ($value != '' && $key != 'id' && $key != 'dataid') {
2990 $presetxmldata .= "<$key>" . htmlspecialchars($value) . "</$key>\n";
2991 }
2992 }
2993 $presetxmldata .= "</field>\n\n";
2994 }
2995 }
2996 $presetxmldata .= '</preset>';
8aff1574
SH
2997 return $presetxmldata;
2998}
2999
3000function data_presets_export($course, $cm, $data, $tostorage=false) {
3001 global $CFG, $DB;
3002
3003 $presetname = clean_filename($data->name) . '-preset-' . gmdate("Ymd_Hi");
3004 $exportsubdir = "temp/mod_data/presetexport/$presetname";
3005 make_upload_directory($exportsubdir);
3006 $exportdir = "$CFG->dataroot/$exportsubdir";
3007
3008 // Assemble "preset.xml":
3009 $presetxmldata = data_presets_generate_xml($course, $cm, $data);
cba87c36
SH
3010
3011 // After opening a file in write mode, close it asap
3012 $presetxmlfile = fopen($exportdir . '/preset.xml', 'w');
3013 fwrite($presetxmlfile, $presetxmldata);
3014 fclose($presetxmlfile);
3015
3016 // Now write the template files
3017 $singletemplate = fopen($exportdir . '/singletemplate.html', 'w');
3018 fwrite($singletemplate, $data->singletemplate);
3019 fclose($singletemplate);
3020
3021 $listtemplateheader = fopen($exportdir . '/listtemplateheader.html', 'w');
3022 fwrite($listtemplateheader, $data->listtemplateheader);
3023 fclose($listtemplateheader);
3024
3025 $listtemplate = fopen($exportdir . '/listtemplate.html', 'w');
3026 fwrite($listtemplate, $data->listtemplate);
3027 fclose($listtemplate);
3028
3029 $listtemplatefooter = fopen($exportdir . '/listtemplatefooter.html', 'w');
3030 fwrite($listtemplatefooter, $data->listtemplatefooter);
3031 fclose($listtemplatefooter);
3032
3033 $addtemplate = fopen($exportdir . '/addtemplate.html', 'w');
3034 fwrite($addtemplate, $data->addtemplate);
3035 fclose($addtemplate);
3036
3037 $rsstemplate = fopen($exportdir . '/rsstemplate.html', 'w');
3038 fwrite($rsstemplate, $data->rsstemplate);
3039 fclose($rsstemplate);
3040
3041 $rsstitletemplate = fopen($exportdir . '/rsstitletemplate.html', 'w');
3042 fwrite($rsstitletemplate, $data->rsstitletemplate);
3043 fclose($rsstitletemplate);
3044
3045 $csstemplate = fopen($exportdir . '/csstemplate.css', 'w');
3046 fwrite($csstemplate, $data->csstemplate);
3047 fclose($csstemplate);
3048
3049 $jstemplate = fopen($exportdir . '/jstemplate.js', 'w');
3050 fwrite($jstemplate, $data->jstemplate);
3051 fclose($jstemplate);
3052
3053 $asearchtemplate = fopen($exportdir . '/asearchtemplate.html', 'w');
3054 fwrite($asearchtemplate, $data->asearchtemplate);
3055 fclose($asearchtemplate);
3056
3057 // Check if all files have been generated
3058 if (! is_directory_a_preset($exportdir)) {
3059 print_error('generateerror', 'data');
3060 }
3061
60a1a25c 3062 $filenames = array(
cba87c36
SH
3063 'preset.xml',
3064 'singletemplate.html',
3065 'listtemplateheader.html',
3066 'listtemplate.html',
3067 'listtemplatefooter.html',
3068 'addtemplate.html',
3069 'rsstemplate.html',
3070 'rsstitletemplate.html',
3071 'csstemplate.css',
3072 'jstemplate.js',
3073 'asearchtemplate.html'
3074 );
3075
60a1a25c
AD
3076 $filelist = array();
3077 foreach ($filenames as $filename) {
3078 $filelist[$filename] = $exportdir . '/' . $filename;
cba87c36
SH
3079 }
3080
8aff1574 3081 $exportfile = $exportdir.'.zip';
cba87c36 3082 file_exists($exportfile) && unlink($exportfile);
8aff1574 3083
cf792cfc
PS
3084 $fp = get_file_packer('application/zip');
3085 $fp->archive_to_pathname($filelist, $exportfile);
8aff1574 3086
cba87c36
SH
3087 foreach ($filelist as $file) {
3088 unlink($file);
3089 }
3090 rmdir($exportdir);
3091
3092 // Return the full path to the exported preset file:
3093 return $exportfile;
d846488e 3094}