id = $fid;
}
//returns the name/type of the field
function name(){
return get_string('name'.$this->type, 'data');
}
//prints the respective type icon
function image($dataid) {
global $CFG;
$str = '';
$str .= 'type.'" title="'.$this->type.'" />';
return $str;
}
function insert_field($dataid, $type='text', $name, $des='', $autolink=0){
$newfield = new object;
$newfield->dataid = $dataid;
$newfield->type = $type;
$newfield->name = $name;
$newfield->description = $des;
$newfield->param1 = $autolink;
if (!insert_record('data_fields',$newfield)){
notify('Insertion of new field failed!');
}
}
/***********************************************
* prints the form element in the add template *
* eg: *
* Your Name: [ ] *
***********************************************/
function display_add_field($id, $rid=0){
global $CFG;
if (!$field = get_record('data_fields','id',$id)){
notify("that is not a valid field id!");
exit;
}
if ($rid){
$content = get_record('data_content','fieldid',$id,'recordid',$rid);
if (isset($content->content)){
$content = $content->content;
}
}
else {
$content = '';
}
$str = '';
/*
if ($field->description){
$str .= ' ';
}
*/
$str .= '
';
$str .= '';
$str .= '
';
return $str;
}
/**********************************************************************
* prints the field that defines the attributes for editting a field, *
* only accessible by teachers *
* eg. *
* name of the picture: [ ] *
* size [ ] *
* width [ ] *
**********************************************************************/
function display_edit_field($id, $dataid=0){
global $CFG, $course;
$newfield = 0;
if (!$field = get_record('data_fields','id',$id)){
if ($dataid){ //if is a new field
$field->dataid = $dataid;
$newfield = 1;
$field->id = 0;
$field->param1 = '';
$field->param2 = '';
$field->param3 = '';
$field->name = '';
$field->description = '';
}
else {
notify("that is not a valid field id!");
exit;
}
}
print_simple_box_start('center','80%');
require_once($CFG->dirroot.'/mod/data/field/'.$this->type.'/mod.html');
print_simple_box_end();
}
/*********************************************************************
* prints the browse mode content when viewed *
* eg. *
* [icon] Word.doc *
**********************************************************************/
function display_browse_field($fieldid, $recordid, $template){
if ($content = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)){
if (isset($content->content)) {
$options->para = false;
$str = format_text($content->content, $content->content1, $options);
} else {
$str = '';
}
return $str;
}
return false;
}
/****************************
* updates a field *
****************************/
function update($fieldobject){
//special thing is checking param1, if not present, set to 0
$fieldobject->param1 = isset($fieldobject->param1)?$fieldobject->param1:0;
if (!update_record('data_fields',$fieldobject)){
notify ('upate failed');
}
}
/************************************
* store content of this field type *
************************************/
function store_data_content($fieldid, $recordid, $value, $name=''){
$content = new object;
$content->fieldid = $fieldid;
$content->recordid = $recordid;
$content->content = clean_param($value, PARAM_NOTAGS);
insert_record('data_content', $content);
}
/*************************************
* update content of this field type *
*************************************/
function update_data_content($fieldid, $recordid, $value, $name=''){
//if data_content already exit, we update
if ($oldcontent = get_record('data_content','fieldid', $fieldid, 'recordid', $recordid)){
$content = new object;
$content->fieldid = $fieldid;
$content->recordid = $recordid;
$content->content = clean_param($value, PARAM_NOTAGS);
$content->id = $oldcontent->id;
update_record('data_content',$content);
}
else { //make 1 if there isn't one already
$this->store_data_content($fieldid, $recordid, $value, $name='');
}
}
/*************************************************************
* this function checks if a field from an add form is empty *
* input $param string $value *
* $param string $name *
* output boolean *
*************************************************************/
function notemptyfield($value, $name) {
return !empty($value);
}
/*********************************************************************
* This function deletes all data_contents associated with the field *
*********************************************************************/
function delete_data_contents() {
return delete_records('data_content', 'fieldid', $this->id);
}
/*************************************************************************
* This function checks if there's any file associated with this file, *
* and is uploaded, if so, it deletes it. E.g file or picture type field *
*************************************************************************/
function delete_data_content_files($dataid, $recordid, $content) {
//does nothing
}
/*************************************************************************
* This function prints anything that the field wants to output after *
* the html form. This is handy for the javascript rich text editor *
*************************************************************************/
function print_after_form() {
// Overridden as needed in sub classes.
}
/* 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';
}
}//end of class data_field_base
///Field methods
/*****************************************************************************
/* Given a mode and a dataid, generate a default case template *
* input @param mode - addtemplate, singletemplate, listtempalte, rsstemplate*
* @param dataid *
* output null *
*****************************************************************************/
function data_generate_default_form($dataid, $mode){
if (!$dataid && !$mode){
return false;
}
//get all the fields for that database
if ($fields = get_records('data_fields','dataid',$dataid)){
$data->id = $dataid;
$str = ''; //the string to write to the given $data->{$mode}
//this only applies to add and single template
$str .= '
';
$str .= '
';
foreach ($fields as $cfield){
$str .= '
';
$str .= $cfield->name.':';
$str .= '
';
$str .='
';
$str .= '[['.$cfield->name.']]';
$str .= '
';
unset($g);
}
if ($mode!='addtemplate' and $mode!='rsstemplate'){ //if not adding, we put tags in there
$str .= '
##Edit## ##More## ##Delete## ##Approve##
';
}
$str .= '
';
$str .= '
';
if ($mode == 'listtemplate'){
$str .= ' ';
}
$data->{$mode} = $str;
//make the header and footer for listtempalte
update_record('data', $data);
}
}
/*********************************************************
* generates an empty add form, if there is none. *
* input: @(int)id, id of the data *
* output: null *
*********************************************************/
function data_generate_empty_add_form($id, $rid=0){
$currentdata = get_record('data','id',$id);
//check if there is an add entry
if (!$currentdata->addtemplate){
echo '
'.get_string('emptyadd', 'data').'
';
//get all the field entry, and print studnet version
if ($fields = get_records('data_fields','dataid',$currentdata->id)){
$str = ''; //the string to write to the given $data->{$mode}
//this only applies to add and single template
$str .= '
';
}
echo $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_forms($dataid, $searchfieldname, $newfieldname) {
if (!$data = get_record('data', 'id', $dataid)) {
// Form does not exist.
return false;
}
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_field_in_form($dataid, $newfieldname) {
if (!$data = get_record('data', 'id', $dataid)) {
// Form does not exist.
return false;
}
if (!empty($data->singletemplate)) {
$data->singletemplate .= ' [[' . $newfieldname .']]';
}
if (!empty($data->rsstemplate)) {
$data->rsstemplate .= ' [[' . $newfieldname . ']]';
}
update_record('data', $data);
}
/************************************************************************
* give a name in the format of field_## where ## is the field id, *
* this function creates an instance of the particular subfield class *
************************************************************************/
function data_get_field_from_name($name){
$namestring = explode('_',$name);
$fieldid = $namestring[1];//the one after _ is the id.
$field = get_record('data_fields','id',$fieldid);
if ($field){
return data_get_field($field);
} else {
return false;
}
}
/************************************************************************
* 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){
if ($field){
require_once('field/'.$field->type.'/field.class.php');
$newfield = 'data_field_'.$field->type;
$newfield = new $newfield($field->id);
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($dataid, $groupid=0){
global $USER, $course;
$record->userid = $USER->id;
$record->groupid = $groupid;
$record->dataid = $dataid;
$record->timecreated = time();
$record->timemodified = time();
if (isteacher($course->id)) {
$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
$possiblefields = get_records('data_fields','dataid',$dataid);
///then we generate strings to replace
$tagsok = true; //let's be optimistic
foreach ($possiblefields as $cfield){
$pattern="/\[\[".$cfield->name."\]\]/i";
if (preg_match_all($pattern, $template, $dummy)>1){
$tagsok = false;
notify ('[['.$cfield->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)){
$g->delete_data_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 $USER, $CFG;
$sql = 'SELECT * from '.$CFG->prefix.'data_records WHERE dataid = "'.$data->id.'" AND userid = "'.$USER->id.'" ORDER BY timemodified DESC';
if ($records = get_records_sql($sql)){
$result->info = count($records).' '.get_string('numrecords', 'data');
$lastrecord = array_pop($records);
$result->time = $lastrecord->timemodified;
return $result;
}
return NULL;
}
/************************************************************************
* Prints all the records uploaded by this user *
************************************************************************/
function data_user_complete($course, $user, $mod, $data) {
global $USER, $CFG;
$sql = 'SELECT * from '.$CFG->prefix.'data_records WHERE dataid = "'.
$data->id.'" AND userid = "'.$USER->id.'" ORDER BY timemodified DESC';
if ($records = get_records_sql($sql)){
data_print_template($records, $data, '', 'singletemplate');
}
}
/************************************************************************
* returns a list of participants of this database *
************************************************************************/
function data_get_participants($dataid) {
//Returns the users with data in one resource
//(NONE, byt must exists on EVERY mod !!)
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_comments c WHERE dataid="'.$dataid.'"
AND u.id = c.userid');
$participants = array();
if ($records){
foreach ($records as $record) {
$participants[$record->id] = $record;
}
}
if ($comments){
foreach ($comments as $comment) {
$participants[$comment->id] = $comment;
}
}
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($records, $data, $search, $template, $sort, $page=0, $rid=0, $order='', $group='', $return=false){
global $CFG, $course;
foreach ($records as $record) { //only 1 record for single mode
//replacing tags
$patterns = array();
$replacement = array();
if ($search || $sort){ //the ids are different for the 2 searches
$record->id = $record->recordid;
}
$possiblefields = get_records('data_fields','dataid',$data->id);
///then we generate strings to replace for normal tags
foreach ($possiblefields as $cfield) {
$patterns[]='/\[\['.$cfield->name.'\]\]/i';
$g = data_get_field($cfield);
$replacement[] = highlight($search, $g->display_browse_field($cfield->id, $record->id, $template));
unset($g);
}
$record = get_record('data_records','id',$record->id);
///replacing special tags (##Edit##, ##Delete##, ##More##)
$patterns[]='/\#\#Edit\#\#/i';
$patterns[]='/\#\#Delete\#\#/i';
$patterns[]='/\#\#More\#\#/i';
$patterns[]='/\#\#Approve\#\#/i';
$patterns[]='/\#\#Comment\#\#/i';
if (data_isowner($record->id) or isteacheredit($course->id)){
$replacement[] = '';
}else {
$replacement[] = '';
}
if (data_isowner($record->id) or isteacheredit($course->id)){
$replacement[] = '';
}else {
$replacement[] = '';
}
$replacement[] = '';
if (isteacher($course->id) && ($data->approval) && (!$record->approved)){
$replacement[] = data_print_approve_button($record->id, $data->id, $page, $rid, $search, $sort, $order);
}else {
$replacement[] = '';
}
if (($template == 'listtemplate') && ($data->comments)) {
$comments = count_records('data_comments','recordid',$record->id);
$replacement[] = ''.$comments.' '.get_string('comment','data').'';
} else {
$replacement[] = '';
}
///actual replacement of the tags
$newtext = preg_replace($patterns, $replacement, $data->{$template});
if ($return) {
return format_text($newtext);
} else {
echo format_text($newtext); //prints the template with tags replaced
}
/**********************************
* 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, $search, $template, $sort, $page, $rid, $order, $group);
}
//if this record is not yet approved, and database requires approval, print silly button
}
}
/************************************************************************
* 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 ' ';
echo '';
}
//silly function that prints a button
function data_print_approve_button($recordid, $d, $page='0', $rid='0', $search='', $sort='', $order='') {
$str= '';
return $str;
}
//silly function that approves a record
function data_approve_record($recordid) {
$record = get_record('data_records','id',$recordid);
$record->approved = 1;
update_record('data_records',$record);
}
//silly function that prints the a form to do ratings
function data_print_ratings($data, $record) {
global $USER, $course;
$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($course->id)) && !isguest());
if ($ratings->allow) {
echo '";
}
}
}
}
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 , $search, $template, $sort, $page=0, $rid=0, $order='', $group='') {
//foreach comment, print it!
//(with links to edit, remove etc, but no reply!!!!!)
if ($comments = get_records('data_comments','recordid',$record->id)) {
foreach ($comments as $comment) {
data_print_comment($data, $comment->id);
}
}
//prints silly comment form
echo '';
}
//prints a single comment entry
function data_print_comment($data, $commentid) {
global $USER, $CFG, $course;
$stredit = get_string('edit');
$strdelete = get_string('delete');
$comment = get_record('data_comments','id',$commentid);
$user = get_record('user','id',$comment->userid);
echo '
';
if (data_isowner($comment->recordid) or isteacher($course->id)) {
echo ''.$stredit.'';
}
if (data_isowner($comment->recordid) or isteacher($course->id)) {
echo '| '.$strdelete.'';
}
echo '
';
echo '
'."\n\n";
}
// For Participantion Reports
function data_get_view_actions() {
return array('view');
}
function data_get_post_actions() {
return array('add','update','record delete');
}
/*
INSERT INTO prefix_log_display VALUES ('data', 'view', 'data', 'name');
INSERT INTO prefix_log_display VALUES ('data', 'add', 'data', 'name');
INSERT INTO prefix_log_display VALUES ('data', 'update', 'data', 'name');
INSERT INTO prefix_log_display VALUES ('data', 'record delete', 'data', 'name');
INSERT INTO prefix_log_display VALUES ('data', 'fields add', 'data_fields', 'name');
INSERT INTO prefix_log_display VALUES ('data', 'fields update', 'data_fields', 'name');
INSERT INTO prefix_log_display VALUES ('data', 'templates saved', 'data', 'name');
INSERT INTO prefix_log_display VALUES ('data', 'templates def', 'data', 'name');
*/
?>