libdir . '/portfoliolib.php');
// Some constants
define ('DATA_MAX_ENTRIES', 50);
define ('DATA_PERPAGE_SINGLE', 1);
define ('DATA_FIRSTNAME', -1);
define ('DATA_LASTNAME', -2);
define ('DATA_APPROVED', -3);
define ('DATA_TIMEADDED', 0);
define ('DATA_TIMEMODIFIED', -4);
define ('DATA_CAP_EXPORT', 'mod/data:viewalluserpresets');
// Users having assigned the default role "Non-editing teacher" can export database records
// Using the mod/data capability "viewalluserpresets" existing in Moodle 1.9.x.
// In Moodle >= 2, new roles may be introduced and used instead.
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
var $cm; // course module or cmifno
var $context; // activity context
// Constructor function
function __construct($field=0, $data=0, $cm=0) { // Field or data or both, each can be id or object
global $DB;
if (empty($field) && empty($data)) {
print_error('missingfield', 'data');
}
if (!empty($field)) {
if (is_object($field)) {
$this->field = $field; // Programmer knows what they are doing, we hope
} else if (!$this->field = $DB->get_record('data_fields', array('id'=>$field))) {
print_error('invalidfieldid', 'data');
}
if (empty($data)) {
if (!$this->data = $DB->get_record('data', array('id'=>$this->field->dataid))) {
print_error('invalidid', '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 = $DB->get_record('data', array('id'=>$data))) {
print_error('invalidid', 'data');
}
} else { // No way to define it!
print_error('missingdata', 'data');
}
}
if ($cm) {
$this->cm = $cm;
} else {
$this->cm = get_coursemodule_from_instance('data', $this->data->id);
}
if (empty($this->field)) { // We need to define some default values
$this->define_default_field();
}
$this->context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
}
// 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() {
global $DB;
if (empty($this->field)) {
notify('Programmer error: Field has not been defined yet! See define_field()');
return false;
}
if (!$this->field->id = $DB->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() {
global $DB;
if (!$DB->update_record('data_fields', $this->field)) {
notify('updating of new field failed!');
return false;
}
return true;
}
// Delete a field completely
function delete_field() {
global $DB;
if (!empty($this->field->id)) {
$this->delete_content();
$DB->delete_records('data_fields', array('id'=>$this->field->id));
}
return true;
}
// Print the relevant form element in the ADD template for this field
function display_add_field($recordid=0){
global $DB;
if ($recordid){
$content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
} else {
$content = '';
}
$str = '
';
$str .= '';
$str .= '
';
return $str;
}
// Print the relevant form element to define the attributes for this field
// viewable by teachers only.
function display_edit_field() {
global $CFG, $DB;
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 '';
print_simple_box_end();
}
// Display the content of the field in browse mode
function display_browse_field($recordid, $template) {
global $DB;
if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
if (isset($content->content)) {
$options = new object();
if ($this->field->param1 == '1') { // We are autolinking this field, so disable linking within us
//$content->content = ''.$content->content.'';
//$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=''){
global $DB;
$content = new object();
$content->fieldid = $this->field->id;
$content->recordid = $recordid;
$content->content = clean_param($value, PARAM_NOTAGS);
if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
$content->id = $oldcontent->id;
return $DB->update_record('data_content', $content);
} else {
return $DB->insert_record('data_content', $content);
}
}
// Delete all content associated with the field
function delete_content($recordid=0) {
global $DB;
if ($recordid) {
$conditions = array('fieldid'=>$this->field->id, 'recordid'=>$recordid);
} else {
$conditions = array('fieldid'=>$this->field->id);
}
if ($rs = $DB->get_recordset('data_content', $conditions)) {
$fs = get_file_storage();
foreach ($rs as $content) {
$fs->delete_area_files($this->context->id, 'data_content', $content->id);
}
$rs->close();
}
return $DB->delete_records('data_content', $conditions);
}
// 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 = '';
$str .= 'iconheight.'" width="'.$this->iconwidth.'" alt="'.$this->type.'" title="'.$this->type.'" />';
return $str;
}
// Per default, it is assumed that fields support text exporting. Override this (return false) on fields not supporting text exporting.
function text_export_supported() {
return true;
}
// Per default, return the record's text value only from the "content" field. Override this in fields class if necesarry.
function export_text_value($record) {
if ($this->text_export_supported()) {
return $record->content;
}
}
function file_ok($relativepath) {
return false;
}
}
/*****************************************************************************
/* 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) {
global $DB;
if (!$data && !$template) {
return false;
}
if ($template == 'csstemplate' or $template == 'jstemplate' ) {
return '';
}
// get all the fields for that database
if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id')) {
$str = '
';
$str .= '
';
foreach ($fields as $field) {
$str .= '
';
// Yu: commenting this out, the id was wrong and will fix later
//if ($template == 'addtemplate') {
//$str .= '';
//} else {
$str .= $field->name.': ';
//}
$str .= '
';
$str .='
';
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 .= '
';
if ($template == 'listtemplate'){
$str .= '';
}
if ($update) {
$newdata = new object();
$newdata->id = $data->id;
$newdata->{$template} = $str;
if (!$DB->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) {
global $DB;
if (!empty($newfieldname)) {
$prestring = '[[';
$poststring = ']]';
$idpart = '#id';
} else {
$prestring = '';
$poststring = '';
$idpart = '';
}
$newdata = new object();
$newdata->id = $data->id;
$newdata->singletemplate = str_ireplace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->singletemplate);
$newdata->listtemplate = str_ireplace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->listtemplate);
$newdata->addtemplate = str_ireplace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->addtemplate);
$newdata->addtemplate = str_ireplace('[['.$searchfieldname.'#id]]',
$prestring.$newfieldname.$idpart.$poststring, $data->addtemplate);
$newdata->rsstemplate = str_ireplace('[['.$searchfieldname.']]',
$prestring.$newfieldname.$poststring, $data->rsstemplate);
return $DB->update_record('data', $newdata);
}
/********************************************************
* Appends a new field at the end of the form template. *
********************************************************/
function data_append_new_field_to_templates($data, $newfieldname) {
global $DB;
$newdata = new object();
$newdata->id = $data->id;
$change = false;
if (!empty($data->singletemplate)) {
$newdata->singletemplate = $data->singletemplate.' [[' . $newfieldname .']]';
$change = true;
}
if (!empty($data->addtemplate)) {
$newdata->addtemplate = $data->addtemplate.' [[' . $newfieldname . ']]';
$change = true;
}
if (!empty($data->rsstemplate)) {
$newdata->rsstemplate = $data->singletemplate.' [[' . $newfieldname . ']]';
$change = true;
}
if ($change) {
$DB->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){
global $DB;
$field = $DB->get_record('data_fields', array('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){
global $DB;
$field = $DB->get_record('data_fields', array('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, $cm=null) {
global $CFG;
if ($field) {
require_once('field/'.$field->type.'/field.class.php');
$newfield = 'data_field_'.$field->type;
$newfield = new $newfield($field, $data, $cm);
return $newfield;
}
}
/**
* Given record object (or id), returns true if the record belongs to the current user
* @param mixed $rid - record object or id
* @return bool
*/
function data_isowner($record) {
global $USER, $DB;
if (!isloggedin()) {
return false;
}
if (!is_object($record)) {
if (!$record = $DB->get_record('data_records', array('id'=>$record))) {
return false;
}
}
return ($record->userid == $USER->id);
}
/***********************************************************************
* 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, $DB;
$sql = 'SELECT COUNT(*) FROM {data_records} WHERE dataid=? AND userid=?';
return $DB->count_records_sql($sql, array($data->id, $USER->id));
}
/****************************************************************
* 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, $DB;
$cm = get_coursemodule_from_instance('data', $data->id);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$record = new object();
$record->userid = $USER->id;
$record->dataid = $data->id;
$record->groupid = $groupid;
$record->timecreated = $record->timemodified = time();
if (has_capability('mod/data:approve', $context)) {
$record->approved = 1;
} else {
$record->approved = 0;
}
return $DB->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) {
global $DB;
// first get all the possible tags
$fields = $DB->get_records('data_fields', array('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 $DB;
if (empty($data->assessed)) {
$data->assessed = 0;
}
$data->timemodified = time();
if (! $data->id = $DB->insert_record('data', $data)) {
return false;
}
data_grade_item_update($data);
return $data->id;
}
/************************************************************************
* updates an instance of a data *
************************************************************************/
function data_update_instance($data) {
global $DB;
$data->timemodified = time();
$data->id = $data->instance;
if (empty($data->assessed)) {
$data->assessed = 0;
}
if (empty($data->notification)) {
$data->notification = 0;
}
if (! $DB->update_record('data', $data)) {
return false;
}
data_grade_item_update($data);
return true;
}
/************************************************************************
* deletes an instance of a data *
************************************************************************/
function data_delete_instance($id) { // takes the dataid
global $DB;
if (!$data = $DB->get_record('data', array('id'=>$id))) {
return false;
}
$cm = get_coursemodule_from_instance('data', $data->id);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
/// Delete all the associated information
// files
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'data_content');
// get all the records in this data
$sql = "SELECT r.id
FROM {data_records} r
WHERE r.dataid = ?";
$DB->delete_records_select('data_content', "recordid IN ($sql)", array($id));
// delete all the records and fields
$DB->delete_records('data_records', array('dataid'=>$id));
$DB->delete_records('data_fields', array('dataid'=>$id));
// Delete the instance itself
$result = $DB->delete_records('data', array('id'=>$id));
// cleanup gradebook
data_grade_item_delete($data);
return $result;
}
/************************************************************************
* returns a summary of data activity of this user *
************************************************************************/
function data_user_outline($course, $user, $mod, $data) {
global $DB;
if ($countrecords = $DB->count_records('data_records', array('dataid'=>$data->id, 'userid'=>$user->id))) {
$result = new object();
$result->info = get_string('numrecords', 'data', $countrecords);
$lastrecord = $DB->get_record_sql('SELECT id,timemodified FROM {data_records}
WHERE dataid = ? AND userid = ?
ORDER BY timemodified DESC', array($data->id, $user->id), 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) {
global $DB;
if ($records = $DB->get_records('data_records', array('dataid'=>$data->id,'userid'=>$user->id), 'timemodified DESC')) {
data_print_template('singletemplate', $records, $data);
}
}
/**
* Return grade for given user or all users.
*
* @param int $dataid id of data
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none
*/
function data_get_user_grades($data, $userid=0) {
global $DB;
$user = $userid ? "AND u.id = :userid" : "";
$params = array('userid'=>$userid, 'dataid'=>$data->id);
$sql = "SELECT u.id, u.id AS userid, avg(drt.rating) AS rawgrade
FROM {user} u, {data_records} dr,
{data_ratings} drt
WHERE u.id = dr.userid AND dr.id = drt.recordid
AND drt.userid != u.id AND dr.dataid = :dataid
$user
GROUP BY u.id";
return $DB->get_records_sql($sql, $params);
}
/**
* Update activity grades
*
* @param object $data
* @param int $userid specific user only, 0 means all
*/
function data_update_grades($data, $userid=0, $nullifnone=true) {
global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php');
if (!$data->assessed) {
data_grade_item_update($data);
} else if ($grades = data_get_user_grades($data, $userid)) {
data_grade_item_update($data, $grades);
} else if ($userid and $nullifnone) {
$grade = new object();
$grade->userid = $userid;
$grade->rawgrade = NULL;
data_grade_item_update($data, $grade);
} else {
data_grade_item_update($data);
}
}
/**
* Update all grades in gradebook.
*/
function data_upgrade_grades() {
global $DB;
$sql = "SELECT COUNT('x')
FROM {data} d, {course_modules} cm, {modules} m
WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id";
$count = $DB->count_records_sql($sql);
$sql = "SELECT d.*, cm.idnumber AS cmidnumber, d.course AS courseid
FROM {data} d, {course_modules} cm, {modules} m
WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id";
if ($rs = $DB->get_recordset_sql($sql)) {
// too much debug output
$prevdebug = $DB->get_debug();
$DB->set_debug(false);
$pbar = new progress_bar('dataupgradegrades', 500, true);
$i=0;
foreach ($rs as $data) {
$i++;
upgrade_set_timeout(60*5); // set up timeout, may also abort execution
data_update_grades($data, 0, false);
$pbar->update($i, $count, "Updating Database grades ($i/$count).");
}
$DB->set_debug($prevdebug);
$rs->close();
}
}
/**
* Update/create grade item for given data
*
* @param object $data object with extra cmidnumber
* @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return object grade_item
*/
function data_grade_item_update($data, $grades=NULL) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
$params = array('itemname'=>$data->name, 'idnumber'=>$data->cmidnumber);
if (!$data->assessed or $data->scale == 0) {
$params['gradetype'] = GRADE_TYPE_NONE;
} else if ($data->scale > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $data->scale;
$params['grademin'] = 0;
} else if ($data->scale < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$data->scale;
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = NULL;
}
return grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, $grades, $params);
}
/**
* Delete grade item for given data
*
* @param object $data object
* @return object grade_item
*/
function data_grade_item_delete($data) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
return grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, NULL, array('deleted'=>1));
}
/************************************************************************
* 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 $DB;
$records = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
FROM {user} u, {data_records} r
WHERE r.dataid = ? AND u.id = r.userid", array($dataid));
$comments = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
FROM {user} u, {data_records} r, {data_comments} c
WHERE r.dataid = ? AND u.id = r.userid AND r.id = c.recordid", array($dataid));
$ratings = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
FROM {user} u, {data_records} r, {data_ratings} a
WHERE r.dataid = ? AND u.id = r.userid AND r.id = a.recordid", array($dataid));
$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;
}
// 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, $DB;
$cm = get_coursemodule_from_instance('data', $data->id);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
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 = $DB->get_records('data_fields', array('dataid'=>$data->id));
foreach ($fieldrecords as $fieldrecord) {
$fields[]= data_get_field($fieldrecord, $data);
}
$isteacher = has_capability('mod/data:managetemplates', $context);
}
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.']]';
$replacement[] = highlight($search, $field->display_browse_field($record->id, $template));
}
// Replacing special tags (##Edit##, ##Delete##, ##More##)
$patterns[]='##edit##';
$patterns[]='##delete##';
if (has_capability('mod/data:manageentries', $context) or data_isowner($record->id)) {
$replacement[] = '';
$replacement[] = '';
} else {
$replacement[] = '';
$replacement[] = '';
}
$moreurl = $CFG->wwwroot . '/mod/data/view.php?d=' . $data->id . '&rid=' . $record->id;
if ($search) {
$moreurl .= '&filter=1';
}
$patterns[]='##more##';
$replacement[] = '';
$patterns[]='##moreurl##';
$replacement[] = $moreurl;
$patterns[]='##user##';
$replacement[] = ''.fullname($record).'';
$patterns[]='##export##';
if (($template == 'singletemplate' || $template == 'listtemplate')
&& ((has_capability('mod/data:exportentry', $context)
|| (data_isowner($record->id) && has_capability('mod/data:exportownentry', $context))))) {
$button = new portfolio_add_button();
$button->set_callback_options('data_portfolio_caller', array('id' => $cm->id, 'recordid' => $record->id));
list($formats, $files) = data_portfolio_caller::formats($fields, $record);
$button->set_formats($formats);
$replacement[] = $button->to_html(PORTFOLIO_ADD_ICON_LINK);
} else {
$replacement[] = '';
}
$patterns[] = '##timeadded##';
$replacement[] = userdate($record->timecreated);
$patterns[] = '##timemodified##';
$replacement [] = userdate($record->timemodified);
$patterns[]='##approve##';
if (has_capability('mod/data:approve', $context) && ($data->approval) && (!$record->approved)){
$replacement[] = '';
} else {
$replacement[] = '';
}
$patterns[]='##comments##';
if (($template == 'listtemplate') && ($data->comments)) {
$comments = $DB->count_records('data_comments', array('recordid'=>$record->id));
$replacement[] = ''.get_string('commentsn','data', $comments).'';
} else {
$replacement[] = '';
}
// actual replacement of the tags
$newtext = str_ireplace($patterns, $replacement, $data->{$template});
// no more html formatting and filtering - see MDL-6635
if ($return) {
return $newtext;
} else {
echo $newtext;
// hack alert - return is always false in singletemplate anyway ;-)
/**********************************
* 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 *
* *
* This preference box prints a searchable advanced search template if *
* a) A template is defined *
* b) The advanced search checkbox is checked. *
* *
* input @param object $data *
* @param int $perpage *
* @param string $search *
* output null *
************************************************************************/
function data_print_preference_form($data, $perpage, $search, $sort='', $order='ASC', $search_array = '', $advanced = 0, $mode= ''){
global $CFG, $DB;
$cm = get_coursemodule_from_instance('data', $data->id);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
echo '
';
echo '';
echo '
';
}
function data_print_ratings($data, $record) {
global $USER, $DB;
$cm = get_coursemodule_from_instance('data', $data->id);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
if ($data->assessed and !empty($USER->id) and (has_capability('mod/data:rate', $context) or has_capability('mod/data:viewrating', $context) or data_isowner($record->id))) {
if ($ratingsscale = make_grades_menu($data->scale)) {
$ratingsmenuused = false;
echo '
';
echo '';
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)
global $DB;
if (!$ratings) {
$ratings = array();
if ($rates = $DB->get_records("data_ratings", array("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
global $DB;
static $strrate;
if (!$rating = $DB->get_record("data_ratings", array("userid"=>$userid, "recordid"=>$recordid))) {
$rating->rating = -999;
}
if (empty($strrate)) {
$strrate = get_string("rate", "data");
}
choose_from_menu($scale, $recordid, $rating->rating, "$strrate...", '', -999);
}
/**
* Returns a list of ratings for a particular post - sorted.
*/
function data_get_ratings($recordid, $sort="u.firstname ASC") {
global $DB;
return $DB->get_records_sql("SELECT u.*, r.rating
FROM {data_ratings} r, {user} u
WHERE r.recordid = ? AND r.userid = u.id
ORDER BY $sort", array($recordid));
}
// prints all comments + a text box for adding additional comment
function data_print_comments($data, $record, $page=0, $mform=false) {
global $CFG, $DB;
echo '';
if ($comments = $DB->get_records('data_comments', array('recordid'=>$record->id))) {
foreach ($comments as $comment) {
data_print_comment($data, $comment, $page);
}
echo ' ';
}
if (!isloggedin() or isguest()) {
return;
}
$editor = optional_param('addcomment', 0, PARAM_BOOL);
if (!$mform and !$editor) {
echo '