1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
|
<?php // $Id: lib.php,v 1.70.2.5 2006/07/03 21:07:41 skodak Exp $
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 2005 Moodle Pty Ltd http://moodle.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/// Some constants
define ('DATA_TEACHERS_ONLY', 1);
define ('DATA_STUDENTS_ONLY', 2);
define ('DATA_TEACHERS_AND_STUDENTS', 3);
define ('DATA_MAX_ENTRIES', 50);
define ('DATA_PERPAGE_SINGLE', 1);
class data_field_base { /// Base class for Database Field Types (see field/*/field.class.php)
var $type = 'unknown'; /// Subclasses must override the type with their name
var $data = NULL; /// The database object that this field belongs to
var $field = NULL; /// The field object itself, if we know it
var $iconwidth = 16; /// Width of the icon for this fieldtype
var $iconheight = 16; /// Width of the icon for this fieldtype
/// Constructor function
function data_field_base($field=0, $data=0) { // Field or data or both, each can be id or object
if (empty($field) && empty($data)) {
error('Programmer error: You must specify field and/or data when defining field class. ');
}
if (!empty($field)) {
if (is_object($field)) {
$this->field = $field; // Programmer knows what they are doing, we hope
} else if (!$this->field = get_record('data_fields','id',$field)) {
error('Bad field ID encountered: '.$field);
}
if (empty($data)) {
if (!$this->data = get_record('data','id',$this->field->dataid)) {
error('Bad data ID encountered in field data');
}
}
}
if (empty($this->data)) { // We need to define this properly
if (!empty($data)) {
if (is_object($data)) {
$this->data = $data; // Programmer knows what they are doing, we hope
} else if (!$this->data = get_record('data','id',$data)) {
error('Bad data ID encountered: '.$data);
}
} else { // No way to define it!
error('Data id or object must be provided to field class');
}
}
if (empty($this->field)) { // We need to define some default values
$this->define_default_field();
}
}
/// This field just sets up a default field object
function define_default_field() {
if (empty($this->data->id)) {
notify('Programmer error: dataid not defined in field class');
}
$this->field = new object;
$this->field->id = 0;
$this->field->dataid = $this->data->id;
$this->field->type = $this->type;
$this->field->param1 = '';
$this->field->param2 = '';
$this->field->param3 = '';
$this->field->name = '';
$this->field->description = '';
return true;
}
/// Set up the field object according to data in an object. Now is the time to clean it!
function define_field($data) {
$this->field->type = $this->type;
$this->field->dataid = $this->data->id;
$this->field->name = trim($data->name);
$this->field->description = trim($data->description);
if (isset($data->param1)) {
$this->field->param1 = trim($data->param1);
}
if (isset($data->param2)) {
$this->field->param2 = trim($data->param2);
}
if (isset($data->param3)) {
$this->field->param3 = trim($data->param3);
}
if (isset($data->param4)) {
$this->field->param4 = trim($data->param4);
}
if (isset($data->param5)) {
$this->field->param5 = trim($data->param5);
}
return true;
}
/// Insert a new field in the database
/// We assume the field object is already defined as $this->field
function insert_field() {
if (empty($this->field)) {
notify('Programmer error: Field has not been defined yet! See define_field()');
return false;
}
if (!$this->field->id = insert_record('data_fields',$this->field)){
notify('Insertion of new field failed!');
return false;
}
return true;
}
/// Update a field in the database
function update_field() {
if (!update_record('data_fields', $this->field)) {
notify('updating of new field failed!');
return false;
}
return true;
}
/// Delete a field completely
function delete_field() {
if (!empty($this->field->id)) {
delete_records('data_fields', 'id', $this->field->id);
$this->delete_content();
}
return true;
}
/// Print the relevant form element in the ADD template for this field
function display_add_field($recordid=0){
if ($recordid){
$content = get_field('data_content', 'content', 'fieldid', $this->field->id, 'recordid', $recordid);
} else {
$content = '';
}
$str = '<div title="'.$this->field->description.'">';
$str .= '<input style="width:300px;" type="text" name="field_'.$this->field->id.'" id="field_'.$this->field->id.'" value="'.s($content).'" />';
$str .= '</div>';
return $str;
}
/// Print the relevant form element to define the attributes for this field
/// viewable by teachers only.
function display_edit_field() {
global $CFG;
if (empty($this->field)) { // No field has been defined yet, try and make one
$this->define_default_field();
}
print_simple_box_start('center','80%');
echo '<form name="editfield" action="'.$CFG->wwwroot.'/mod/data/field.php" method="post">'."\n";
echo '<input type="hidden" name="d" value="'.$this->data->id.'" />'."\n";
if (empty($this->field->id)) {
echo '<input type="hidden" name="mode" value="add" />'."\n";
$savebutton = get_string('add');
} else {
echo '<input type="hidden" name="fid" value="'.$this->field->id.'" />'."\n";
echo '<input type="hidden" name="mode" value="update" />'."\n";
$savebutton = get_string('savechanges');
}
echo '<input type="hidden" name="type" value="'.$this->type.'" />'."\n";
echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />'."\n";
print_heading($this->name());
require_once($CFG->dirroot.'/mod/data/field/'.$this->type.'/mod.html');
echo '<div align="center">';
echo '<input type="submit" value="'.$savebutton.'" />'."\n";
echo '<input type="submit" name="cancel" value="'.get_string('cancel').'" />'."\n";
echo '</div">';
echo '</form>';
print_simple_box_end();
}
/// Display the content of the field in browse mode
function display_browse_field($recordid, $template) {
if ($content = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid)) {
if (isset($content->content)) {
if ($this->field->param1 == '1') { // We are autolinking this field, so disable linking within us
//$content->content = '<span class="nolink">'.$content->content.'</span>';
//$content->content1 = FORMAT_HTML;
$options->filter=false;
}
$options->para = false;
$str = format_text($content->content, $content->content1, $options);
} else {
$str = '';
}
return $str;
}
return false;
}
/// Update the content of one data field in the data_content table
function update_content($recordid, $value, $name=''){
$content = new object;
$content->fieldid = $this->field->id;
$content->recordid = $recordid;
$content->content = clean_param($value, PARAM_NOTAGS);
if ($oldcontent = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid)) {
$content->id = $oldcontent->id;
return update_record('data_content', $content);
} else {
return insert_record('data_content', $content);
}
}
/// Delete all content associated with the field
function delete_content($recordid=0) {
$this->delete_content_files($recordid);
if ($recordid) {
return delete_records('data_content', 'fieldid', $this->field->id, 'recordid', $recordid);
} else {
return delete_records('data_content', 'fieldid', $this->field->id);
}
}
/// Deletes any files associated with this field
function delete_content_files($recordid='') {
global $CFG;
require_once($CFG->libdir.'/filelib.php');
$dir = $CFG->dataroot.'/'.$this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id;
if ($recordid) {
$dir .= '/'.$recordid;
}
return fulldelete($dir);
}
/// Check if a field from an add form is empty
function notemptyfield($value, $name) {
return !empty($value);
}
/// Just in case a field needs to print something before the whole form
function print_before_form() {
}
/// Just in case a field needs to print something after the whole form
function print_after_form() {
}
/// Returns the sortable field for the content. By default, it's just content
/// but for some plugins, it could be content 1 - content4
function get_sort_field() {
return 'content';
}
/// Returns the SQL needed to refer to the column. Some fields may need to CAST() etc.
function get_sort_sql($fieldname) {
return $fieldname;
}
/// Returns the name/type of the field
function name(){
return get_string('name'.$this->type, 'data');
}
/// Prints the respective type icon
function image() {
global $CFG;
$str = '<a href="field.php?d='.$this->data->id.'&fid='.$this->field->id.'&mode=display&sesskey='.sesskey().'">';
$str .= '<img src="'.$CFG->modpixpath.'/data/field/'.$this->type.'/icon.gif" ';
$str .= 'height="'.$this->iconheight.'" width="'.$this->iconwidth.'" border="0" alt="'.$this->type.'" title="'.$this->type.'" /></a>';
return $str;
}
} //end of major class data_field_base
/*****************************************************************************
/* Given a template and a dataid, generate a default case template *
* input @param template - addtemplate, singletemplate, listtempalte, rsstemplate*
* @param dataid *
* output null *
*****************************************************************************/
function data_generate_default_template(&$data, $template, $recordid=0, $form=false, $update=true) {
if (!$data && !$template) {
return false;
}
if ($template == 'csstemplate') {
return '';
}
//get all the fields for that database
if ($fields = get_records('data_fields', 'dataid', $data->id, 'id')) {
$str = '<div align="center">';
$str .= '<table>';
foreach ($fields as $field) {
$str .= '<tr><td valign="top" align="right">';
$str .= $field->name.':';
$str .= '</td>';
$str .='<td>';
if ($form) { /// Print forms instead of data
$fieldobj = data_get_field($field, $data);
$str .= $fieldobj->display_add_field($recordid);
} else { /// Just print the tag
$str .= '[['.$field->name.']]';
}
$str .= '</td></tr>';
}
if ($template != 'addtemplate' and $template != 'rsstemplate') { //if not adding, we put tags in there
$str .= '<tr><td align="center" colspan="2">##Edit## ##More## ##Delete## ##Approve##</td></tr>';
}
$str .= '</table>';
$str .= '</div>';
if ($template == 'listtemplate'){
$str .= '<hr />';
}
if ($update) {
$newdata->id = $data->id;
$newdata->{$template} = $str;
if (!update_record('data', $newdata)) {
notify('Error updating template');
} else {
$data->{$template} = $str;
}
}
return $str;
}
}
/***********************************************************************
* Search for a field name and replaces it with another one in all the *
* form templates. Set $newfieldname as '' if you want to delete the *
* field from the form. *
***********************************************************************/
function data_replace_field_in_templates($data, $searchfieldname, $newfieldname) {
if (!empty($newfieldname)) {
$prestring = '[[';
$poststring = ']]';
} else {
$prestring = '';
$poststring = '';
}
$newdata->id = $data->id;
$newdata->singletemplate = addslashes(str_replace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->singletemplate));
$newdata->listtemplate = addslashes(str_replace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->listtemplate));
$newdata->addtemplate = addslashes(str_replace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->addtemplate));
$newdata->rsstemplate = addslashes(str_replace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->rsstemplate));
return update_record('data', $newdata);
}
/********************************************************
* Appends a new field at the end of the form template. *
********************************************************/
function data_append_new_field_to_templates($data, $newfieldname) {
$newdata->id = $data->id;
$change = false;
if (!empty($data->singletemplate)) {
$newdata->singletemplate = addslashes($data->singletemplate.' [[' . $newfieldname .']]');
$change = true;
}
if (!empty($data->addtemplate)) {
$newdata->addtemplate = addslashes($data->addtemplate.' [[' . $newfieldname . ']]');
$change = true;
}
if (!empty($data->rsstemplate)) {
$newdata->rsstemplate = addslashes($data->singletemplate.' [[' . $newfieldname . ']]');
$change = true;
}
if ($change) {
update_record('data', $newdata);
}
}
/************************************************************************
* given a field name *
* this function creates an instance of the particular subfield class *
************************************************************************/
function data_get_field_from_name($name, $data){
$field = get_record('data_fields','name',$name);
if ($field) {
return data_get_field($field, $data);
} else {
return false;
}
}
/************************************************************************
* given a field id *
* this function creates an instance of the particular subfield class *
************************************************************************/
function data_get_field_from_id($fieldid, $data){
$field = get_record('data_fields','id',$fieldid);
if ($field) {
return data_get_field($field, $data);
} else {
return false;
}
}
/************************************************************************
* given a field id *
* this function creates an instance of the particular subfield class *
************************************************************************/
function data_get_field_new($type, $data) {
global $CFG;
require_once($CFG->dirroot.'/mod/data/field/'.$type.'/field.class.php');
$newfield = 'data_field_'.$type;
$newfield = new $newfield(0, $data);
return $newfield;
}
/************************************************************************
* returns a subclass field object given a record of the field, used to *
* invoke plugin methods *
* input: $param $field - record from db *
************************************************************************/
function data_get_field($field, $data) {
global $CFG;
if ($field) {
require_once('field/'.$field->type.'/field.class.php');
$newfield = 'data_field_'.$field->type;
$newfield = new $newfield($field, $data);
return $newfield;
}
}
/***************************************************************************
* given record id, returns true if the record belongs to the current user *
* input @param $rid - record id *
* output bool *
***************************************************************************/
function data_isowner($rid){
global $USER;
if (empty($USER->id)) {
return false;
}
if ($record = get_record('data_records','id',$rid)) {
return ($record->userid == $USER->id);
}
return false;
}
/***********************************************************************
* has a user reached the max number of entries? *
* input object $data *
* output bool *
***********************************************************************/
function data_atmaxentries($data){
if (!$data->maxentries){
return false;
} else {
return (data_numentries($data) >= $data->maxentries);
}
}
/**********************************************************************
* returns the number of entries already made by this user *
* input @param object $data *
* uses global $CFG, $USER *
* output int *
**********************************************************************/
function data_numentries($data){
global $USER;
global $CFG;
$sql = 'SELECT COUNT(*) FROM '.$CFG->prefix.'data_records WHERE dataid='.$data->id.' AND userid='.$USER->id;
return count_records_sql($sql);
}
/****************************************************************
* function that takes in a dataid and adds a record *
* this is used everytime an add template is submitted *
* input @param int $dataid, $groupid *
* output bool *
****************************************************************/
function data_add_record($data, $groupid=0){
global $USER;
$record->userid = $USER->id;
$record->dataid = $data->id;
$record->groupid = $groupid;
$record->timecreated = $record->timemodified = time();
if (isteacher($data->course)) {
$record->approved = 1;
} else {
$record->approved = 0;
}
return insert_record('data_records',$record);
}
/*******************************************************************
* check the multple existence any tag in a template *
* input @param string *
* output true-valid, false-invalid *
* check to see if there are 2 or more of the same tag being used. *
* input @param int $dataid, *
* @param string $template *
* output bool *
*******************************************************************/
function data_tags_check($dataid, $template){
//first get all the possible tags
$fields = get_records('data_fields','dataid',$dataid);
///then we generate strings to replace
$tagsok = true; //let's be optimistic
foreach ($fields as $field){
$pattern="/\[\[".$field->name."\]\]/i";
if (preg_match_all($pattern, $template, $dummy)>1){
$tagsok = false;
notify ('[['.$field->name.']] - '.get_string('multipletags','data'));
}
}
//else return true
return $tagsok;
}
/************************************************************************
* Adds an instance of a data *
************************************************************************/
function data_add_instance($data) {
global $CFG;
if (empty($data->ratings)) {
$data->ratings = 0;
}
$data->timemodified = time();
if (!empty($data->availablefromenable)) {
$data->timeavailablefrom = make_timestamp($data->availablefromyear, $data->availablefrommonth, $data->availablefromday,
$data->availablefromhour, $data->availablefromminute, 0);
} else {
$data->timeavailablefrom = 0;
}
if (!empty($data->availabletoenable)) {
$data->timeavailableto = make_timestamp($data->availabletoyear, $data->availabletomonth, $data->availabletoday,
$data->availabletohour, $data->availabletominute, 0);
} else {
$data->timeavailableto = 0;
}
if (!empty($data->viewfromenable)) {
$data->timeviewfrom = make_timestamp($data->viewfromyear, $data->viewfrommonth, $data->viewfromday,
$data->viewfromhour, $data->viewfromminute, 0);
} else {
$data->timeviewfrom = 0;
}
if (!empty($data->viewtoenable)) {
$data->timeviewto = make_timestamp($data->viewtoyear, $data->viewtomonth, $data->viewtoday,
$data->viewtohour, $data->viewtominute, 0);
} else {
$data->timeviewto = 0;
}
if (! $data->id = insert_record('data', $data)) {
return false;
}
return $data->id;
}
/************************************************************************
* updates an instance of a data *
************************************************************************/
function data_update_instance($data) {
global $CFG;
$data->id = $data->instance;
if (empty($data->ratings)) {
$data->ratings = 0;
}
$data->timemodified = time();
if (!empty($data->availablefromenable)) {
$data->timeavailablefrom = make_timestamp($data->availablefromyear, $data->availablefrommonth, $data->availablefromday,
$data->availablefromhour, $data->availablefromminute, 0);
} else {
$data->timeavailablefrom = 0;
}
if (!empty($data->availabletoenable)) {
$data->timeavailableto = make_timestamp($data->availabletoyear, $data->availabletomonth, $data->availabletoday,
$data->availabletohour, $data->availabletominute, 0);
} else {
$data->timeavailableto = 0;
}
if (!empty($data->viewfromenable)) {
$data->timeviewfrom = make_timestamp($data->viewfromyear, $data->viewfrommonth, $data->viewfromday,
$data->viewfromhour, $data->viewfromminute, 0);
} else {
$data->timeviewfrom = 0;
}
if (!empty($data->viewtoenable)) {
$data->timeviewto = make_timestamp($data->viewtoyear, $data->viewtomonth, $data->viewtoday,
$data->viewtohour, $data->viewtominute, 0);
} else {
$data->timeviewto = 0;
}
if (! $data->instance = update_record('data', $data)) {
return false;
}
return $data->instance;
}
/************************************************************************
* deletes an instance of a data *
************************************************************************/
function data_delete_instance($id) { //takes the dataid
global $CFG;
if (! $data = get_record('data', 'id', $id)) {
return false;
}
/// Delete all the associated information
// get all the records in this data
$sql = 'SELECT c.* FROM '.$CFG->prefix.'data_records as r LEFT JOIN '.
$CFG->prefix.'data_content as c ON c.recordid = r.id WHERE r.dataid = '.$id;
if ($contents = get_records_sql($sql)){
foreach($contents as $content){
$field = get_record('data_fields','id',$content->fieldid);
if ($g = data_get_field($field, $data)){
$g->delete_content_files($id, $content->recordid, $content->content);
}
//delete the content itself
delete_records('data_content','id', $content->id);
}
}
// delete all the records and fields
delete_records('data_records', 'dataid', $id);
delete_records('data_fields','dataid',$id);
// Delete the instance itself
if (! delete_records('data', 'id', $id)) {
return false;
}
return true;
}
/************************************************************************
* returns a summary of data activity of this user *
************************************************************************/
function data_user_outline($course, $user, $mod, $data) {
global $CFG;
if ($countrecords = count_records('data_records', 'dataid', $data->id, 'userid', $user->id)) {
$result->info = get_string('numrecords', 'data', $countrecords);
$lastrecord = get_record_sql('SELECT id,timemodified FROM '.$CFG->prefix.'data_records
WHERE dataid = '.$data->id.' AND userid = '.$user->id.'
ORDER BY timemodified DESC', true);
$result->time = $lastrecord->timemodified;
return $result;
}
return NULL;
}
/************************************************************************
* Prints all the records uploaded by this user *
************************************************************************/
function data_user_complete($course, $user, $mod, $data) {
if ($records = get_records_select('data_records', 'dataid = '.$data->id.' AND userid = '.$user->id,
'timemodified DESC')) {
data_print_template('singletemplate', $records, $data);
}
}
/************************************************************************
* returns a list of participants of this database *
************************************************************************/
function data_get_participants($dataid) {
//Returns the users with data in one data
//(users with records in data_records, data_comments and data_ratings)
global $CFG;
$records = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}data_records r
WHERE r.dataid = '$dataid'
AND u.id = r.userid");
$comments = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}data_records r,
{$CFG->prefix}data_comments c
WHERE r.dataid = '$dataid'
AND u.id = r.userid
AND r.id = c.recordid");
$ratings = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}data_records r,
{$CFG->prefix}data_ratings a
WHERE r.dataid = '$dataid'
AND u.id = r.userid
AND r.id = a.recordid");
$participants = array();
if ($records){
foreach ($records as $record) {
$participants[$record->id] = $record;
}
}
if ($comments){
foreach ($comments as $comment) {
$participants[$comment->id] = $comment;
}
}
if ($ratings){
foreach ($ratings as $rating) {
$participants[$rating->id] = $rating;
}
}
return $participants;
}
function data_get_coursemodule_info($coursemodule) {
/// Given a course_module object, this function returns any
/// "extra" information that may be needed when printing
/// this activity in a course listing.
///
/// See get_array_of_activities() in course/lib.php
///
global $CFG;
$info = NULL;
return $info;
}
///junk functions
/************************************************************************
* takes a list of records, the current data, a search string, *
* and mode to display prints the translated template *
* input @param array $records *
* @param object $data *
* @param string $search *
* @param string $template *
* output null *
************************************************************************/
function data_print_template($template, $records, $data, $search='',$page=0, $return=false) {
global $CFG;
static $fields = NULL;
static $isteacher;
static $dataid = NULL;
if (empty($dataid)) {
$dataid = $data->id;
} else if ($dataid != $data->id) {
$fields = NULL;
}
if (empty($fields)) {
$fieldrecords = get_records('data_fields','dataid', $data->id);
foreach ($fieldrecords as $fieldrecord) {
$fields[]= data_get_field($fieldrecord, $data);
}
$isteacher = isteacher($data->course);
}
if (empty($records)) {
return;
}
foreach ($records as $record) { /// Might be just one for the single template
/// Replacing tags
$patterns = array();
$replacement = array();
/// Then we generate strings to replace for normal tags
foreach ($fields as $field) {
$patterns[]='/\[\['.$field->field->name.'\]\]/i';
$replacement[] = highlight($search, $field->display_browse_field($record->id, $template));
}
/// Replacing special tags (##Edit##, ##Delete##, ##More##)
$patterns[]='/\#\#Edit\#\#/i';
$patterns[]='/\#\#Delete\#\#/i';
if ($isteacher or data_isowner($record->id)) {
$replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/edit.php?d='
.$data->id.'&rid='.$record->id.'&sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/edit.gif" height="11" width="11" border="0" alt="'.get_string('edit').'" /></a>';
$replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/view.php?d='
.$data->id.'&delete='.$record->id.'&sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/delete.gif" height="11" width="11" border="0" alt="'.get_string('delete').'" /></a>';
} else {
$replacement[] = '';
$replacement[] = '';
}
$patterns[]='/\#\#More\#\#/i';
$replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$record->id.'"><img src="'.$CFG->pixpath.'/i/search.gif" height="11" width="11" border="0" alt="'.get_string('more').'" /></a>';
$patterns[]='/\#\#MoreURL\#\#/i';
$replacement[] = $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$record->id;
$patterns[]='/\#\#User\#\#/i';
$replacement[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$record->userid.
'&course='.$data->course.'">'.fullname($record).'</a>';
$patterns[]='/\#\#Approve\#\#/i';
if ($isteacher && ($data->approval) && (!$record->approved)){
$replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&approve='.$record->id.'&sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/i/approve.gif" height="11" width="11" border="0" alt="'.get_string('approve').'" /></a>';
} else {
$replacement[] = '';
}
$patterns[]='/\#\#Comments\#\#/i';
if (($template == 'listtemplate') && ($data->comments)) {
$comments = count_records('data_comments','recordid',$record->id);
$replacement[] = '<a href="view.php?rid='.$record->id.'#comments">'.get_string('commentsn','data', $comments).'</a>';
} else {
$replacement[] = '';
}
///actual replacement of the tags
$newtext = preg_replace($patterns, $replacement, $data->{$template});
$options->para=false;
$options->noclean=true;
if ($return) {
return format_text($newtext, FORMAT_HTML, $options);
} else {
echo format_text($newtext, FORMAT_HTML, $options);
}
/**********************************
* Printing Ratings Form *
*********************************/
if ($template == 'singletemplate') { //prints ratings options
data_print_ratings($data, $record);
}
/**********************************
* Printing Ratings Form *
*********************************/
if (($template == 'singletemplate') && ($data->comments)) { //prints ratings options
data_print_comments($data, $record, $page);
}
}
}
/************************************************************************
* function that takes in the current data, number of items per page, *
* a search string and prints a preference box in view.php *
* input @param object $data *
* @param int $perpage *
* @param string $search *
* output null *
************************************************************************/
function data_print_preference_form($data, $perpage, $search, $sort='', $order='ASC'){
echo '<br /><div class="datapreferences" align="center">';
echo '<form name="options" action="view.php" method="get">';
echo '<input type="hidden" name="d" value="'.$data->id.'" />';
echo get_string('pagesize','data').':';
$pagesizes = array(1=>1,2=>2,3=>3,4=>4,5=>5,6=>6,7=>7,8=>8,9=>9,10=>10,15=>15,
20=>20,30=>30,40=>40,50=>50,100=>100,200=>200,300=>300,400=>400,500=>500,1000=>1000);
choose_from_menu($pagesizes, 'perpage', $perpage, 'choose', '', '0');
echo ' '.get_string('search').': <input type="text" size="16" name="search" value="'.s($search).'" />';
echo ' '.get_string('sortby').':';
//foreach field, print the option
$fields = get_records('data_fields','dataid',$data->id, 'name');
echo '<select name="sort"><option value="0">'.get_string('dateentered','data').'</option>';
foreach ($fields as $field) {
if ($field->id == $sort) {
echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
} else {
echo '<option value="'.$field->id.'">'.$field->name.'</option>';
}
}
echo '</select>';
echo '<select name="order">';
if ($order == 'ASC') {
echo '<option value="ASC" selected="selected">'.get_string('ascending','data').'</option>';
} else {
echo '<option value="ASC">'.get_string('ascending','data').'</option>';
}
if ($order == 'DESC') {
echo '<option value="DESC" selected="selected">'.get_string('descending','data').'</option>';
} else {
echo '<option value="DESC">'.get_string('descending','data').'</option>';
}
echo '</select>';
//print ASC or DESC
echo '<input type="submit" value="'.get_string('savesettings','data').'" />';
echo '</form>';
echo '</div>';
}
function data_print_ratings($data, $record) {
global $USER;
$ratingsmenuused = false;
if ($data->ratings and !empty($USER->id)) {
if ($ratings->scale = make_grades_menu($data->scale)) {
$ratings->assesspublic = $data->assesspublic;
$ratings->allow = (($data->assessed != 2 or isteacher($data->course)) && !isguest());
if ($ratings->allow) {
echo '<div class="ratings" align="center">';
echo '<form name="form" method="post" action="rate.php">';
$useratings = true;
if ($useratings) {
if ((isteacher($data->course) or $ratings->assesspublic) and !data_isowner($record->id)) {
data_print_ratings_mean($record->id, $ratings->scale, isteacher($data->course));
if (!empty($ratings->allow)) {
echo ' ';
data_print_rating_menu($record->id, $USER->id, $ratings->scale);
$ratingsmenuused = true;
}
} else if (data_isowner($record->id)) {
data_print_ratings_mean($record->id, $ratings->scale, true);
} else if (!empty($ratings->allow) ) {
data_print_rating_menu($record->id, $USER->id, $ratings->scale);
$ratingsmenuused = true;
}
}
if ($data->scale < 0) {
if ($scale = get_record("scale", "id", abs($data->scale))) {
print_scale_menu_helpbutton($data->course, $scale );
}
}
if ($ratingsmenuused) {
echo '<input type="hidden" name="id" value="'.$data->course.'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo "<input type=\"submit\" value=\"".get_string("sendinratings", "data")."\" />";
}
echo "</form>";
echo '</div>';
}
}
}
}
function data_print_ratings_mean($recordid, $scale, $link=true) {
/// Print the multiple ratings on a post given to the current user by others.
/// Scale is an array of ratings
static $strrate;
$mean = data_get_ratings_mean($recordid, $scale);
if ($mean !== "") {
if (empty($strratings)) {
$strratings = get_string("ratings", "data");
}
echo "$strratings: ";
if ($link) {
link_to_popup_window ("/mod/data/report.php?id=$recordid", "ratings", $mean, 400, 600);
} else {
echo "$mean ";
}
}
}
function data_get_ratings_mean($recordid, $scale, $ratings=NULL) {
/// Return the mean rating of a post given to the current user by others.
/// Scale is an array of possible ratings in the scale
/// Ratings is an optional simple array of actual ratings (just integers)
if (!$ratings) {
$ratings = array();
if ($rates = get_records("data_ratings", "recordid", $recordid)) {
foreach ($rates as $rate) {
$ratings[] = $rate->rating;
}
}
}
$count = count($ratings);
if ($count == 0) {
return "";
} else if ($count == 1) {
return $scale[$ratings[0]];
} else {
$total = 0;
foreach ($ratings as $rating) {
$total += $rating;
}
$mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
if (isset($scale[$mean])) {
return $scale[$mean]." ($count)";
} else {
return "$mean ($count)"; // Should never happen, hopefully
}
}
}
function data_print_rating_menu($recordid, $userid, $scale) {
/// Print the menu of ratings as part of a larger form.
/// If the post has already been - set that value.
/// Scale is an array of ratings
static $strrate;
if (!$rating = get_record("data_ratings", "userid", $userid, "recordid", $recordid)) {
$rating->rating = 0;
}
if (empty($strrate)) {
$strrate = get_string("rate", "data");
}
choose_from_menu($scale, $recordid, $rating->rating, "$strrate...");
}
function data_get_ratings($recordid, $sort="u.firstname ASC") {
/// Returns a list of ratings for a particular post - sorted.
global $CFG;
return get_records_sql("SELECT u.*, r.rating
FROM {$CFG->prefix}data_ratings r,
{$CFG->prefix}user u
WHERE r.recordid = $recordid
AND r.userid = u.id
ORDER BY $sort");
}
//prints all comments + a text box for adding additional comment
function data_print_comments($data, $record, $page=0) {
global $CFG;
echo '<a name="comments"></a>';
if ($comments = get_records('data_comments','recordid',$record->id)) {
foreach ($comments as $comment) {
data_print_comment($data, $comment, $page);
}
}
if (isloggedin() and !isguest()) {
echo '<div class="newcomment" align="center"><form method="post" action="'.$CFG->wwwroot.'/mod/data/comment.php">';
echo '<input type="hidden" name="mode" value="add" />';
echo '<input type="hidden" name="page" value="'.$page.'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="hidden" name="rid" value="'.$record->id.'" />';
echo '<textarea rows="8" cols="50" name="commentcontent"></textarea>';
echo '<br /><input type="submit" value="'.get_string('addcomment','data').'" />';
echo '</form></div>';
}
}
//prints a single comment entry
function data_print_comment($data, $comment, $page=0) {
global $USER, $CFG;
$stredit = get_string('edit');
$strdelete = get_string('delete');
$user = get_record('user','id',$comment->userid);
echo '<table cellspacing="0" align="center" width="50%" class="datacomment forumpost">';
echo '<tr class="header"><td class="picture left">';
print_user_picture($comment->userid, $data->course, $user->picture);
echo '</td>';
echo '<td class="topic starter" align="left"><div class="author">';
$fullname = fullname($user, isteacher($comment->userid));
$by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.
$user->id.'&course='.$data->course.'">'.$fullname.'</a>';
$by->date = userdate($comment->modified);
print_string('bynameondate', 'data', $by);
echo '</div></td></tr>';
echo '<tr><td class="left side">';
if ($groups = user_group($data->course, $comment->userid)) {
print_group_picture($groups, $data->course, false, false, true);
} else {
echo ' ';
}
/// Actual content
echo '</td><td class="content" align="left">'."\n";
// Print whole message
$options->para = false;
echo format_text($comment->content, FORMAT_MOODLE, $options);
/// Commands
echo '<div class="commands">';
if (data_isowner($comment->recordid) or isteacher($data->course)) {
echo '<a href="'.$CFG->wwwroot.'/mod/data/comment.php?rid='.$comment->recordid.'&mode=edit&commentid='.$comment->id.'&page='.$page.'">'.$stredit.'</a>';
echo '| <a href="'.$CFG->wwwroot.'/mod/data/comment.php?rid='.$comment->recordid.'&mode=delete&commentid='.$comment->id.'&page='.$page.'">'.$strdelete.'</a>';
}
echo '</div>';
echo '</td></tr></table>'."\n\n";
}
// For Participantion Reports
function data_get_view_actions() {
return array('view');
}
function data_get_post_actions() {
return array('add','update','record delete');
}
function data_fieldname_exists($name, $dataid, $fieldid=0) {
global $CFG;
if ($fieldid) {
return record_exists_sql("SELECT * from {$CFG->prefix}data_fields AS df
WHERE df.name LIKE '$name' AND df.dataid = $dataid
AND ((df.id < $fieldid) OR (df.id > $fieldid))");
} else {
return record_exists_sql("SELECT * from {$CFG->prefix}data_fields AS df
WHERE df.name LIKE '$name' AND df.dataid = $dataid");
}
}
function data_convert_arrays_to_strings(&$fieldinput) {
foreach ($fieldinput as $key => $val) {
if (is_array($val)) {
$str = '';
foreach ($val as $inner) {
$str .= $inner . ',';
}
$str = substr($str, 0, -1);
$fieldinput->$key = $str;
}
}
}
function data_clean_field_name($fn) {
$fn = trim($fn);
//hack from clean_filename - to be replaced by something nicer later
$fn = preg_replace("/[\\000-\\x2c\\x2f\\x3a-\\x40\\x5b-\\x5e\\x60\\x7b-\\177]/s", '_', $fn);
$fn = preg_replace("/_+/", '_', $fn);
$fn = preg_replace("/\.\.+/", '.', $fn);
return $fn;
}
?>
|