MDL-15106 towards data mod dml conversion

This commit is contained in:
skodak 2008-06-06 07:36:17 +00:00
parent 9dd9cc2312
commit 3223bc9e9b
9 changed files with 87 additions and 91 deletions

View File

@ -41,7 +41,7 @@ function xmldb_data_upgrade($oldversion=0) {
if ($result && $oldversion < 2007101513) {
// Upgrade all the data->notification currently being
// NULL to 0
$sql = "UPDATE {$CFG->prefix}data SET notification=0 WHERE notification IS NULL";
$sql = "UPDATE {data} SET notification=0 WHERE notification IS NULL";
$result = $DB->execute($sql);
$table = new xmldb_table('data');

View File

@ -27,17 +27,17 @@ if ($id) {
if (! $cm = get_coursemodule_from_id('data', $id)) {
print_error('invalidcoursemodule');
}
if (! $course = get_record('course', 'id', $cm->course)) {
if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
print_error('coursemisconf');
}
if (! $data = get_record('data', 'id', $cm->instance)) {
if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
print_error('invalidid', 'data');
}
} else if ($d) {
if (! $data = get_record('data', 'id', $d)) {
if (! $data = $DB->get_record('data', array('id'=>$d))) {
print_error('invalidid', 'data');
}
if (! $course = get_record('course', 'id', $data->course)) {
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
print_error('coursemisconf');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
@ -175,7 +175,7 @@ switch ($action) {
$strimportsuccess = get_string('importsuccess', 'data');
$straddentries = get_string('addentries', 'data');
$strtodatabase = get_string('todatabase', 'data');
if (!get_records('data_records', 'dataid', $data->id)) {
if (!$DB->get_records('data_records', array('dataid'=>$data->id))) {
notify("$strimportsuccess <a href='edit.php?d=$data->id'>$straddentries</a> $strtodatabase", 'notifysuccess');
} else {
notify("$strimportsuccess", 'notifysuccess');
@ -345,7 +345,7 @@ $i = 0;
foreach ($presets as $id => $preset) {
$screenshot = '';
if (!empty($preset->userid)) {
$user = get_record('user', 'id', $preset->userid);
$user = $DB->get_record('user', array('id'=>$preset->userid));
$desc = $preset->name.' ('.fullname($user, true).')';
} else {
$desc = $preset->name;

View File

@ -100,8 +100,9 @@ class Data_Preset
/**
* Constructor
*/
function Data_Preset($shortname = null, $data_id = null, $directory = null, $user_id = null)
{
function Data_Preset($shortname = null, $data_id = null, $directory = null, $user_id = null) {
global $DB;
$this->shortname = $shortname;
$this->user_id = $user_id;
@ -112,7 +113,7 @@ class Data_Preset
}
if (!empty($data_id)) {
if (!$this->data = get_record('data', 'id', $data_id)) {
if (!$this->data = $DB->get_record('data', array('id'=>$data_id))) {
print_error('wrongdataid','data');
} else {
$this->listtemplate = $this->data->listtemplate;
@ -127,7 +128,7 @@ class Data_Preset
}
}
/*
/**
* Returns the best name to show for a preset
* If the shortname has spaces in it, replace them with underscores.
* Convert the name to lower case.
@ -233,7 +234,8 @@ class Data_Preset
* @return string The path/name of the resulting zip file if successful.
*/
function export() {
global $CFG;
global $CFG, $DB;
$this->directory = $CFG->dataroot.'/temp';
// write all templates, but not the xml yet
@ -245,7 +247,7 @@ class Data_Preset
}
/* All the display data is now done. Now assemble preset.xml */
$fields = get_records('data_fields', 'dataid', $this->data->id);
$fields = $DB->get_records('data_fields', array('dataid'=>$this->data->id));
$presetfile = fopen($this->directory.'/preset.xml', 'w');
$presetxml = "<preset>\n\n";
@ -313,7 +315,8 @@ class Data_Preset
* TODO document
*/
function load_from_file($directory = null) {
global $CFG;
global $CFG, $DB;
if (empty($directory) && empty($this->directory)) {
$this->directory = $this->get_path();
}
@ -363,7 +366,7 @@ class Data_Preset
/* Now we look at the current structure (if any) to work out whether we need to clear db
or save the data */
$currentfields = array();
$currentfields = get_records('data_fields', 'dataid', $this->data->id);
$currentfields = $DB->get_records('data_fields', array('dataid'=>$this->data->id));
$currentfields = array_merge($currentfields);
return array($settings, $fields, $currentfields);
}
@ -499,11 +502,11 @@ class Data_Preset
$id = $currentfield->id;
// Why delete existing data records and related comments/ratings ??
/*
if ($content = get_records('data_content', 'fieldid', $id)) {
if ($content = $DB->get_records('data_content', array('fieldid'=>$id))) {
foreach ($content as $item) {
delete_records('data_ratings', 'recordid', $item->recordid);
delete_records('data_comments', 'recordid', $item->recordid);
delete_records('data_records', 'id', $item->recordid);
$DB->delete_records('data_ratings', array('recordid'=>$item->recordid));
$DB->delete_records('data_comments', array('recordid'=>$item->recordid));
$DB->delete_records('data_records', array('id'=>$item->recordid));
}
}
*/
@ -538,13 +541,11 @@ class Data_Preset
// ACTION METHODS //
////////////////////
function action_base($params)
{
function action_base($params) {
return null;
}
function action_confirmdelete($params)
{
function action_confirmdelete($params) {
global $CFG, $USER;
$html = '';
$course = $params['course'];
@ -587,9 +588,9 @@ class Data_Preset
exit();
}
function action_delete($params)
{
function action_delete($params) {
global $CFG, $USER;
$course = $params['course'];
$shortname = $params['shortname'];
@ -623,8 +624,7 @@ class Data_Preset
notify("$shortname $strdeleted", 'notifysuccess');
}
function action_importpreset($params)
{
function action_importpreset($params) {
$course = $params['course'];
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalidrequest');
@ -641,8 +641,7 @@ class Data_Preset
exit();
}
function action_importzip($params)
{
function action_importzip($params) {
global $CFG, $USER;
$course = $params['course'];
if (!data_submitted() or !confirm_sesskey()) {
@ -667,8 +666,9 @@ class Data_Preset
exit();
}
function action_finishimport($params)
{
function action_finishimport($params) {
global $DB;
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalidrequest');
}
@ -679,15 +679,14 @@ class Data_Preset
$strimportsuccess = get_string('importsuccess', 'data');
$straddentries = get_string('addentries', 'data');
$strtodatabase = get_string('todatabase', 'data');
if (!get_records('data_records', 'dataid', $this->data->id)) {
if (!$DB->get_records('data_records', array('dataid'=>$this->data->id))) {
notify('$strimportsuccess <a href="edit.php?d=' . $this->data->id . "\">$straddentries</a> $strtodatabase", 'notifysuccess');
} else {
notify("$strimportsuccess", 'notifysuccess');
}
}
function action_export($params)
{
function action_export($params) {
global $CFG, $USER;
$course = $params['course'];
$html = '';
@ -720,8 +719,7 @@ class Data_Preset
/**
* First stage of saving a Preset: ask for a name
*/
function action_save1($params)
{
function action_save1($params) {
$html = '';
$sesskey = sesskey();
$course = $params['course'];
@ -751,8 +749,7 @@ class Data_Preset
* Second stage of saving a preset: If the given name already exists,
* suggest to use a different name or offer to overwrite the existing preset.
*/
function action_save2($params)
{
function action_save2($params) {
$course = $params['course'];
$this->data = $params['data'];
@ -798,8 +795,7 @@ class Data_Preset
/**
* Third stage of saving a preset, overwrites an existing preset with the new one.
*/
function action_save3($params)
{
function action_save3($params) {
global $CFG, $USER;
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalidrequest');

View File

@ -28,17 +28,17 @@ if ($id) {
if (! $cm = get_coursemodule_from_id('data', $id)) {
print_error('invalidcoursemodule');
}
if (! $course = get_record('course', 'id', $cm->course)) {
if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
print_error('coursemisconf');
}
if (! $data = get_record('data', 'id', $cm->instance)) {
if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
print_error('invalidid', 'data');
}
} else if ($d) {
if (! $data = get_record('data', 'id', $d)) {
if (! $data = $DB->get_record('data', array('id'=>$d))) {
print_error('invalidid', 'data');
}
if (! $course = get_record('course', 'id', $data->course)) {
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
print_error('coursemisconf');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
@ -139,7 +139,7 @@ $i = 0;
foreach ($presets as $id => $preset) {
$screenshot = '';
if (!empty($preset->user_id)) {
$user = get_record('user', 'id', $preset->user_id);
$user = $DB->get_record('user', array('id'=>$preset->user_id));
$desc = $preset->name.' ('.fullname($user, true).')';
} else {
$desc = $preset->name;

View File

@ -4,11 +4,11 @@
$dataid = required_param('dataid', PARAM_INT); // The forum the rated posts are from
if (!$data = get_record('data', 'id', $dataid)) {
if (!$data = $DB->get_record('data', array('id'=>$dataid))) {
print_error('invalidid', 'data');
}
if (!$course = get_record('course', 'id', $data->course)) {
if (!$course = $DB->get_record('course', array('id'=>$data->course))) {
print_error('invalidcourseid');
}
@ -40,7 +40,7 @@
continue;
}
if (!$record = get_record('data_records', 'id', $recordid)) {
if (!$record = $DB->get_record('data_records', array('id'=>$recordid))) {
print_error('invalidid', 'data');
}
@ -56,14 +56,14 @@
$count++;
if ($oldrating = get_record('data_ratings', 'userid', $USER->id, 'recordid', $record->id)) {
if ($oldrating = $DB->get_record('data_ratings', array('userid'=>$USER->id, 'recordid'=>$record->id))) {
if ($rating == -999) {
delete_records('data_ratings', 'userid', $oldrating->userid, 'recordid', $oldrating->recordid);
$DB->delete_records('data_ratings', array('userid'=>$oldrating->userid, 'recordid'=>$oldrating->recordid));
data_update_grades($data, $record->userid);
} else if ($rating != $oldrating->rating) {
$oldrating->rating = $rating;
if (! update_record('data_ratings', $oldrating)) {
if (!$DB->update_record('data_ratings', $oldrating)) {
print_error('cannotupdaterate', 'data', '', array($record->id, $rating));
}
data_update_grades($data, $record->userid);
@ -75,7 +75,7 @@
$newrating->userid = $USER->id;
$newrating->recordid = $record->id;
$newrating->rating = $rating;
if (! insert_record('data_ratings', $newrating)) {
if (! $DB->insert_record('data_ratings', $newrating)) {
print_error('cannotinsertrate', 'data', '', array($record->id, $rating));
}
data_update_grades($data, $record->userid);

View File

@ -8,15 +8,15 @@
$id = required_param('id', PARAM_INT);
$sort = optional_param('sort', '', PARAM_ALPHA);
if (!$record = get_record('data_records', 'id', $id)) {
if (!$record = $DB->get_record('data_records', array('id'=>$id))) {
print_error('invalidrecord', 'data');
}
if (!$data = get_record('data', 'id', $record->dataid)) {
if (!$data = $DB->get_record('data', array('id'=>$record->dataid))) {
print_error('invalidid', 'data');
}
if (!$course = get_record('course', 'id', $data->course)) {
if (!$course = $DB->get_record('course', array('id'=>$data->course))) {
print_error('coursemisconf');
}

View File

@ -7,7 +7,7 @@
function data_rss_feeds() {
global $CFG;
global $CFG, $DB;
$status = true;
@ -22,14 +22,14 @@
// It's working so we start...
else {
// Iterate over all data.
if ($datas = get_records('data')) {
if ($datas = $DB->get_records('data')) {
foreach ($datas as $data) {
if ($data->rssarticles > 0) {
// Get the first field in the list (a hack for now until we have a selector)
if (!$firstfield = get_record_sql('SELECT id,name from '.$CFG->prefix.'data_fields WHERE dataid = '.$data->id.' ORDER by id', true)) {
if (!$firstfield = $DB->get_record_sql('SELECT id,name FROM {data_fields} WHERE dataid = ? ORDER by id', array($data->id), true)) {
continue;
}
@ -37,13 +37,13 @@
// Get the data_records out.
$approved = ($data->approval) ? ' AND dr.approved = 1 ' : ' ';
$sql = 'SELECT dr.*, u.firstname, u.lastname ' .
"FROM {$CFG->prefix}data_records dr, {$CFG->prefix}user u " .
"WHERE dr.dataid = {$data->id} " .$approved.
' AND dr.userid = u.id '.
'ORDER BY dr.timecreated DESC';
$sql = "SELECT dr.*, u.firstname, u.lastname
FROM {data_records} dr, {user} u
WHERE dr.dataid = ? $approved
AND dr.userid = u.id
ORDER BY dr.timecreated DESC";
if (!$records = get_records_sql($sql, 0, $data->rssarticles)) {
if (!$records = $DB->get_records_sql($sql, array($data->id), 0, $data->rssarticles)) {
continue;
}
@ -72,8 +72,8 @@
if (!empty($data->rsstitletemplate)) {
$item->title = data_print_template('rsstitletemplate', $recordarray, $data, '', 0, true);
} else { // else we guess
$item->title = strip_tags(get_field('data_content', 'content',
'fieldid', $firstfield->id, 'recordid', $record->id));
$item->title = strip_tags($DB->get_field('data_content', 'content',
array('fieldid'=>$firstfield->id, 'recordid'=>$record->id)));
}
$item->description = data_print_template('rsstemplate', $recordarray, $data, '', 0, true);
$item->pubdate = $record->timecreated;
@ -81,7 +81,7 @@
array_push($items, $item);
}
$course = get_record('course', 'id', $data->course);
$course = $DB->get_record('course', array('id'=>$data->course));
// First all rss feeds common headers.
$header = rss_standard_header($course->shortname.': '.format_string($data->name,true),

View File

@ -34,18 +34,18 @@
if (! $cm = get_coursemodule_from_id('data', $id)) {
error('Course Module ID was incorrect');
}
if (! $course = get_record('course', 'id', $cm->course)) {
if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
error('Course is misconfigured');
}
if (! $data = get_record('data', 'id', $cm->instance)) {
if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
error('Course module is incorrect');
}
} else {
if (! $data = get_record('data', 'id', $d)) {
if (! $data = $DB->get_record('data', array('id'=>$d))) {
error('Data ID is incorrect');
}
if (! $course = get_record('course', 'id', $data->course)) {
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
error('Course is misconfigured');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
@ -58,7 +58,7 @@
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/data:managetemplates', $context);
if (!count_records('data_fields','dataid',$data->id)) { // Brand new database!
if (!$DB->count_records('data_fields', array('dataid'=>$data->id))) { // Brand new database!
redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
}
@ -144,7 +144,7 @@
// Check for multiple tags, only need to check for add template.
if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
if (update_record('data', $newtemplate)) {
if ($DB->update_record('data', $newtemplate)) {
notify(get_string('templatesaved', 'data'), 'notifysuccess');
}
}
@ -172,7 +172,7 @@
if (!$resettemplate) {
// Only reload if we are not resetting the template to default.
$data = get_record('data', 'id', $d);
$data = $DB->get_record('data', array('id'=>$d));
}
print_simple_box_start('center','80%');
echo '<table cellpadding="4" cellspacing="0" border="0">';
@ -202,7 +202,7 @@
echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)">';
$fields = get_records('data_fields', 'dataid', $data->id);
$fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
echo '<optgroup label="'.get_string('fields', 'data').'">';
foreach ($fields as $field) {
echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';

View File

@ -46,32 +46,32 @@
if (! $cm = get_coursemodule_from_id('data', $id)) {
error('Course Module ID was incorrect');
}
if (! $course = get_record('course', 'id', $cm->course)) {
if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
error('Course is misconfigured');
}
if (! $data = get_record('data', 'id', $cm->instance)) {
if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
error('Course module is incorrect');
}
$record = NULL;
} else if ($rid) {
if (! $record = get_record('data_records', 'id', $rid)) {
if (! $record = $DB->get_record('data_records', array('id'=>$rid))) {
error('Record ID is incorrect');
}
if (! $data = get_record('data', 'id', $record->dataid)) {
if (! $data = $DB->get_record('data', array('id'=>$record->dataid))) {
error('Data ID is incorrect');
}
if (! $course = get_record('course', 'id', $data->course)) {
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
error('Course is misconfigured');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
error('Course Module ID was incorrect');
}
} else { // We must have $d
if (! $data = get_record('data', 'id', $d)) {
if (! $data = $DB->get_record('data', array('id'=>$d))) {
error('Data ID is incorrect');
}
if (! $course = get_record('course', 'id', $data->course)) {
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
error('Course is misconfigured');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
@ -87,7 +87,7 @@
/// If we have an empty Database then redirect because this page is useless without data
if (has_capability('mod/data:managetemplates', $context)) {
if (!record_exists('data_fields','dataid',$data->id)) { // Brand new database!
if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { // Brand new database!
redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
}
}
@ -129,7 +129,7 @@
if (!empty($advanced)) {
$search = '';
$vals = array();
$fields = get_records('data_fields', 'dataid', $data->id);
$fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
//Added to ammend paging error. This error would occur when attempting to go from one page of advanced
//search results to another. All fields were reset in the page transfer, and there was no way of determining
@ -316,17 +316,17 @@
if ($delete && confirm_sesskey() && (has_capability('mod/data:manageentries', $context) or data_isowner($delete))) {
if ($confirm = optional_param('confirm',0,PARAM_INT)) {
if ($deleterecord = get_record('data_records', 'id', $delete)) { // Need to check this is valid
if ($deleterecord = $DB->get_record('data_records', array('id'=>$delete))) { // Need to check this is valid
if ($deleterecord->dataid == $data->id) { // Must be from this database
if ($contents = get_records('data_content','recordid', $deleterecord->id)) {
if ($contents = $DB->get_records('data_content', array('recordid'=>$deleterecord->id))) {
foreach ($contents as $content) { // Delete files or whatever else this field allows
if ($field = data_get_field_from_id($content->fieldid, $data)) { // Might not be there
$field->delete_content($content->recordid);
}
}
}
delete_records('data_content','recordid', $deleterecord->id);
delete_records('data_records','id', $deleterecord->id);
$DB->delete_records('data_content', array('recordid'=>$deleterecord->id));
$DB->delete_records('data_records', array('id'=>$deleterecord->id));
add_to_log($course->id, 'data', 'record delete', "view.php?id=$cm->id", $data->id, $cm->id);
@ -335,7 +335,7 @@
}
} else { // Print a confirmation page
if ($deleterecord = get_record('data_records', 'id', $delete)) { // Need to check this is valid
if ($deleterecord = $DB->get_record('data_records', array('id'=>$delete))) { // Need to check this is valid
if ($deleterecord->dataid == $data->id) { // Must be from this database
notice_yesno(get_string('confirmdeleterecord','data'),
'view.php?d='.$data->id.'&amp;delete='.$delete.'&amp;confirm=1&amp;sesskey='.sesskey(),
@ -374,11 +374,11 @@
$approvecap = has_capability('mod/data:approve', $context);
if ($approve && confirm_sesskey() && $approvecap) {
if ($approverecord = get_record('data_records', 'id', $approve)) { // Need to check this is valid
if ($approverecord = $DB->get_record('data_records', array('id'=>$approve))) { // Need to check this is valid
if ($approverecord->dataid == $data->id) { // Must be from this database
$newrecord->id = $approverecord->id;
$newrecord->approved = 1;
if (update_record('data_records', $newrecord)) {
if ($DB->update_record('data_records', $newrecord)) {
notify(get_string('recordapproved','data'), 'notifysuccess');
}
}