From cd655b33f9a7669e352351d624e98310f7b9afa7 Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 3 Jun 2008 23:35:50 +0000 Subject: [PATCH] MDL-15095 towards restore lib dml conversion --- backup/restorelib.php | 2 +- mod/assignment/restorelib.php | 26 +++++------- mod/chat/restorelib.php | 10 ++--- mod/choice/restorelib.php | 38 ++++++++---------- mod/data/restorelib.php | 58 +++++++++++++-------------- mod/forum/restorelib.php | 27 ++++++------- mod/glossary/restorelib.php | 75 +++++++++++++++++------------------ mod/hotpot/restorelib.php | 3 +- mod/label/restorelib.php | 10 ++--- mod/lesson/restorelib.php | 22 +++++----- mod/quiz/restorelib.php | 29 ++++++-------- mod/resource/restorelib.php | 35 ++++++++-------- mod/scorm/restorelib.php | 10 ++--- mod/survey/restorelib.php | 10 ++--- mod/wiki/restorelib.php | 23 +++++------ question/restorelib.php | 67 +++++++++++++++---------------- 16 files changed, 207 insertions(+), 238 deletions(-) diff --git a/backup/restorelib.php b/backup/restorelib.php index d09b305848..b6da844a9d 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -3268,7 +3268,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3); //Now search if that group exists (by name and description field) in //restore->course_id course - //Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides. + //Going to compare LOB columns so, use the cross-db $DB->sql_compare_text() in both sides. $description_clause = ''; $params = array('courseid'=>$restore->course_id, 'grname'=>$gro->name); if (!empty($gro->description)) { /// Only for groups having a description diff --git a/mod/assignment/restorelib.php b/mod/assignment/restorelib.php index af89d2c9bf..3939800778 100644 --- a/mod/assignment/restorelib.php +++ b/mod/assignment/restorelib.php @@ -328,12 +328,10 @@ //working in the backup/restore process. It's called from restore_decode_content_links() //function in restore process function assignment_decode_content_links_caller($restore) { - global $CFG; + global $CFG, $DB; $status = true; - if ($assignments = get_records_sql ("SELECT a.id, a.description - FROM {$CFG->prefix}assignment a - WHERE a.course = $restore->course_id")) { + if ($assignments = $DB->get_records('assignment', array('course'=>$restore->course_id), '', "id,description")) { //Iterate over each assignment->description $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($assignments as $assignment) { @@ -343,8 +341,8 @@ $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { //Update record - $assignment->description = addslashes($result); - $status = update_record("assignment",$assignment); + $assignment->description = $result; + $status = $DB->update_record("assignment",$assignment); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; @@ -369,20 +367,18 @@ //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for //some texts in the module function assignment_restore_wiki2markdown ($restore) { - - global $CFG; + global $CFG, $DB; $status = true; //Convert assignment->description - if ($records = get_records_sql ("SELECT a.id, a.description, a.format - FROM {$CFG->prefix}assignment a, - {$CFG->prefix}backup_ids b - WHERE a.course = $restore->course_id AND + if ($records = $DB->get_records_sql ("SELECT a.id, a.description, a.format + FROM {assignment} a, {backup_ids} b + WHERE a.course = ? AND a.format = ".FORMAT_WIKI. " AND - b.backup_code = $restore->backup_unique_code AND + b.backup_code = ? AND b.table_name = 'assignment' AND - b.new_id = a.id")) { + b.new_id = a.id", array($restore->course_id, $restore->backup_unique_code))) { foreach ($records as $record) { //Rebuild wiki links $record->description = restore_decode_wiki_content($record->description, $restore); @@ -390,7 +386,7 @@ $wtm = new WikiToMarkdown(); $record->description = $wtm->convert($record->description, $restore->course_id); $record->format = FORMAT_MARKDOWN; - $status = update_record('assignment', addslashes_object($record)); + $status = $DB->update_record('assignment', $record); //Do some output $i++; if (($i+1) % 1 == 0) { diff --git a/mod/chat/restorelib.php b/mod/chat/restorelib.php index f3429a4503..587caef52d 100644 --- a/mod/chat/restorelib.php +++ b/mod/chat/restorelib.php @@ -206,12 +206,10 @@ //working in the backup/restore process. It's called from restore_decode_content_links() //function in restore process function chat_decode_content_links_caller($restore) { - global $CFG; + global $CFG, $DB; $status = true; - if ($chats = get_records_sql ("SELECT c.id, c.intro - FROM {$CFG->prefix}chat c - WHERE c.course = $restore->course_id")) { + if ($chats = $DB->get_records('chat', array('course'=>$restore->course_id), '', "id,intro")) { //Iterate over each chat->intro $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($chats as $chat) { @@ -221,8 +219,8 @@ $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { //Update record - $chat->intro = addslashes($result); - $status = update_record("chat",$chat); + $chat->intro = $result; + $status = $DB->update_record("chat",$chat); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; diff --git a/mod/choice/restorelib.php b/mod/choice/restorelib.php index 9974c8ab34..dee6829016 100644 --- a/mod/choice/restorelib.php +++ b/mod/choice/restorelib.php @@ -144,8 +144,7 @@ } function choice_options_restore_mods($choiceid,$info,$restore) { - - global $CFG; + global $CFG, $DB; $status = true; @@ -169,7 +168,7 @@ function choice_options_restore_mods($choiceid,$info,$restore) { $option->timemodified = backup_todb($opt_info['#']['TIMEMODIFIED']['0']['#']); //The structure is equal to the db, so insert the choice_options - $newid = insert_record ("choice_options",$option); + $newid = $DB->insert_record ("choice_options",$option); //Do some output if (($i+1) % 50 == 0) { @@ -196,8 +195,7 @@ function choice_options_restore_mods($choiceid,$info,$restore) { //This function restores the choice_answers function choice_answers_restore_mods($choiceid,$info,$restore) { - - global $CFG; + global $CFG, $DB; $status = true; @@ -239,7 +237,7 @@ function choice_options_restore_mods($choiceid,$info,$restore) { } //The structure is equal to the db, so insert the choice_answers - $newid = insert_record ("choice_answers",$answer); + $newid = $DB->insert_record ("choice_answers",$answer); //Do some output if (($i+1) % 50 == 0) { @@ -336,9 +334,7 @@ function choice_options_restore_mods($choiceid,$info,$restore) { global $CFG; $status = true; - if ($choices = get_records_sql ("SELECT c.id, c.text - FROM {$CFG->prefix}choice c - WHERE c.course = $restore->course_id")) { + if ($choices = $DB->get_records('chat', array('course'=>$restore->course_id), '', "id,text")) { //Iterate over each choice->text $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($choices as $choice) { @@ -348,8 +344,8 @@ function choice_options_restore_mods($choiceid,$info,$restore) { $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { //Update record - $choice->text = addslashes($result); - $status = update_record("choice",$choice); + $choice->text = $result; + $status = $DB->update_record("choice",$choice); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; @@ -375,20 +371,18 @@ function choice_options_restore_mods($choiceid,$info,$restore) { //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for //some texts in the module function choice_restore_wiki2markdown ($restore) { - - global $CFG; + global $CFG, $DB; $status = true; //Convert choice->text - if ($records = get_records_sql ("SELECT c.id, c.text, c.format - FROM {$CFG->prefix}choice c, - {$CFG->prefix}backup_ids b - WHERE c.course = $restore->course_id AND - c.format = ".FORMAT_WIKI. " AND - b.backup_code = $restore->backup_unique_code AND - b.table_name = 'choice' AND - b.new_id = c.id")) { + if ($records = $DB->get_records_sql("SELECT c.id, c.text, c.format + FROM {choice} c, {backup_ids} b + WHERE c.course = ? AND + c.format = ".FORMAT_WIKI. " AND + b.backup_code = ? AND + b.table_name = 'choice' AND + b.new_id = c.id", array($restore->course_id, $restore->backup_unique_code))) { foreach ($records as $record) { //Rebuild wiki links $record->text = restore_decode_wiki_content($record->text, $restore); @@ -396,7 +390,7 @@ function choice_options_restore_mods($choiceid,$info,$restore) { $wtm = new WikiToMarkdown(); $record->text = $wtm->convert($record->text, $restore->course_id); $record->format = FORMAT_MARKDOWN; - $status = update_record('choice', addslashes_object($record)); + $status = $DB->update_record('choice', $record); //Do some output $i++; if (($i+1) % 1 == 0) { diff --git a/mod/data/restorelib.php b/mod/data/restorelib.php index 63dd62b7c1..44c4fc4dab 100644 --- a/mod/data/restorelib.php +++ b/mod/data/restorelib.php @@ -555,17 +555,17 @@ function data_decode_content_links ($content,$restore) { * @return boolean status of the execution */ function data_decode_content_links_caller($restore) { + global $CFG, $DB; - global $CFG; $status = true; /// Process every DATA (intro, all HTML templates) in the course /// Supported fields for main table: $supportedfields = array('intro','singletemplate','listtemplate', 'listtemplateheader','addtemplate','rsstemplate','rsstitletemplate'); - if ($datas = get_records_sql ("SELECT d.id, ".implode(',',$supportedfields)." - FROM {$CFG->prefix}data d - WHERE d.course = $restore->course_id")) { + if ($datas = $DB->get_records_sql ("SELECT d.id, ".implode(',',$supportedfields)." + FROM {data} d + WHERE d.course = ?", array($restore->course_id))) { /// Iterate over each data $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($datas as $data) { @@ -573,7 +573,7 @@ function data_decode_content_links_caller($restore) { $i++; /// Make a new copy of the data object with nothing in, to use if - /// changes are necessary (allows us to do update_record without + /// changes are necessary (allows us to do $DB->update_record without /// worrying about every single field being included and needing /// slashes). $newdata = new stdClass; @@ -584,7 +584,7 @@ function data_decode_content_links_caller($restore) { foreach($supportedfields as $field) { $result = restore_decode_content_links_worker($data->{$field},$restore); if ($result != $data->{$field}) { - $newdata->{$field} = addslashes($result); + $newdata->{$field} = $result; $changed = true; if (debugging()) { if (!defined('RESTORE_SILENTLY')) { @@ -596,7 +596,7 @@ function data_decode_content_links_caller($restore) { /// Update record if any field changed if($changed) { - $status = update_record("data",$newdata); + $status = $DB->update_record("data",$newdata); } /// Do some output @@ -613,13 +613,13 @@ function data_decode_content_links_caller($restore) { } /// Process every COMMENT (content) in the course - if ($comments = get_records_sql ("SELECT dc.id, dc.content - FROM {$CFG->prefix}data d, - {$CFG->prefix}data_records dr, - {$CFG->prefix}data_comments dc - WHERE d.course = $restore->course_id - AND dr.dataid = d.id - AND dc.recordid = dr.id")) { + if ($comments = $DB->get_records_sql ("SELECT dc.id, dc.content + FROM {data} d, + {data_records} dr, + {data_comments} dc + WHERE d.course = ? + AND dr.dataid = d.id + AND dc.recordid = dr.id", array($restore->course_id))) { /// Iterate over each data_comments->content $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($comments as $comment) { @@ -629,8 +629,8 @@ function data_decode_content_links_caller($restore) { $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { /// Update record - $comment->content = addslashes($result); - $status = update_record("data_comments",$comment); + $comment->content = $result; + $status = $DB->update_record("data_comments",$comment); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; @@ -651,13 +651,13 @@ function data_decode_content_links_caller($restore) { } /// Process every CONTENT (content, content1, content2, content3, content4) in the course - if ($contents = get_records_sql ("SELECT dc.id, dc.content, dc.content1, dc.content2, dc.content3, dc.content4 - FROM {$CFG->prefix}data d, - {$CFG->prefix}data_records dr, - {$CFG->prefix}data_content dc - WHERE d.course = $restore->course_id - AND dr.dataid = d.id - AND dc.recordid = dr.id")) { + if ($contents = $DB->get_records_sql("SELECT dc.id, dc.content, dc.content1, dc.content2, dc.content3, dc.content4 + FROM {data} d, + {data_records} dr, + {data_content} dc + WHERE d.course = ? + AND dr.dataid = d.id + AND dc.recordid = dr.id", array($restore->course_id))) { /// Iterate over each data_content->content, content1, content2, content3 and content4 $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($contents as $cnt) { @@ -683,22 +683,22 @@ function data_decode_content_links_caller($restore) { unset($cnt->content4); /// Conditionally set the fields if ($result != $content) { - $cnt->content = addslashes($result); + $cnt->content = $result; } if ($result1 != $content1) { - $cnt->content1 = addslashes($result1); + $cnt->content1 = $result1; } if ($result2 != $content2) { - $cnt->content2 = addslashes($result2); + $cnt->content2 = $result2; } if ($result3 != $content3) { - $cnt->content3 = addslashes($result3); + $cnt->content3 = $result3; } if ($result4 != $content4) { - $cnt->content4 = addslashes($result4); + $cnt->content4 = $result4; } /// Update record with the changed fields - $status = update_record("data_content",$cnt); + $status = $DB->update_record("data_content",$cnt); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; diff --git a/mod/forum/restorelib.php b/mod/forum/restorelib.php index b7665c7dc9..a8c7659890 100644 --- a/mod/forum/restorelib.php +++ b/mod/forum/restorelib.php @@ -703,6 +703,7 @@ //This function returns a log record with all the necessay transformations //done. It's used by restore_log_module() to restore modules log. function forum_restore_logs($restore,$log) { + global $DB; $status = false; @@ -871,7 +872,7 @@ $pos = backup_getid($restore->backup_unique_code,"forum_posts",$log->info); if ($pos) { //Get the post record from database - $dbpos = get_record("forum_posts","id","$pos->new_id"); + $dbpos = $DB->get_record("forum_posts", array("id"=>$pos->new_id)); if ($dbpos) { $log->url = "discuss.php?d=".$dbpos->discussion."&parent=".$pos->new_id; $log->info = $pos->new_id; @@ -886,7 +887,7 @@ $pos = backup_getid($restore->backup_unique_code,"forum_posts",$log->info); if ($pos) { //Get the post record from database - $dbpos = get_record("forum_posts","id","$pos->new_id"); + $dbpos = $DB->get_record("forum_posts", array("id"=>$pos->new_id)); if ($dbpos) { $log->url = "discuss.php?d=".$dbpos->discussion; $log->info = $pos->new_id; @@ -901,7 +902,7 @@ $pos = backup_getid($restore->backup_unique_code,"forum_posts",$log->info); if ($pos) { //Get the post record from database - $dbpos = get_record("forum_posts","id","$pos->new_id"); + $dbpos = $DB->get_record("forum_posts", array("id"=>$pos->new_id)); if ($dbpos) { $log->url = "discuss.php?d=".$dbpos->discussion."&parent=".$pos->new_id; $log->info = $pos->new_id; @@ -1122,15 +1123,15 @@ //working in the backup/restore process. It's called from restore_decode_content_links() //function in restore process function forum_decode_content_links_caller($restore) { - global $CFG, $DB; + global $CFG, $DB, $DB; $status = true; //Process every POST (message) in the course - if ($posts = get_records_sql ("SELECT p.id, p.message - FROM {$CFG->prefix}forum_posts p, - {$CFG->prefix}forum_discussions d - WHERE d.course = $restore->course_id AND - p.discussion = d.id")) { + if ($posts = $DB->get_records_sql("SELECT p.id, p.message + FROM {forum_posts} p, + {forum_discussions} d + WHERE d.course = ? AND + p.discussion = d.id", array($restore->course_id))) { //Iterate over each post->message $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($posts as $post) { @@ -1162,9 +1163,7 @@ } //Process every FORUM (intro) in the course - if ($forums = get_records_sql ("SELECT f.id, f.intro - FROM {$CFG->prefix}forum f - WHERE f.course = $restore->course_id")) { + if ($forums = $DB->get_records('forum', array('course'=>$restore->course_id), '', "id,intro")) { //Iterate over each forum->intro $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($forums as $forum) { @@ -1174,8 +1173,8 @@ $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { //Update record - $forum->intro = addslashes($result); - $status = update_record("forum",$forum); + $forum->intro = $result; + $status = $DB->update_record("forum",$forum); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; diff --git a/mod/glossary/restorelib.php b/mod/glossary/restorelib.php index 93e291b27b..f432b037c1 100644 --- a/mod/glossary/restorelib.php +++ b/mod/glossary/restorelib.php @@ -591,15 +591,15 @@ //working in the backup/restore process. It's called from restore_decode_content_links() //function in restore process function glossary_decode_content_links_caller($restore) { - global $CFG; + global $CFG, $DB; $status = true; //Process every glossary ENTRY in the course - if ($entries = get_records_sql ("SELECT e.id, e.definition - FROM {$CFG->prefix}glossary_entries e, - {$CFG->prefix}glossary g - WHERE g.course = $restore->course_id AND - e.glossaryid = g.id")) { + if ($entries = $DB->get_records_sql("SELECT e.id, e.definition + FROM {glossary_entries} e, + {glossary} g + WHERE g.course = ? AND + e.glossaryid = g.id", array($restore->course_id))) { //Iterate over each post->message $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($entries as $entry) { @@ -609,8 +609,8 @@ $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { //Update record - $entry->definition = addslashes($result); - $status = update_record("glossary_entries",$entry); + $entry->definition = $result; + $status = $DB->update_record("glossary_entries",$entry); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; @@ -631,9 +631,7 @@ } //Process every glossary (intro) in the course - if ($glossarys = get_records_sql ("SELECT g.id, g.intro - FROM {$CFG->prefix}glossary g - WHERE g.course = $restore->course_id")) { + if ($glossarys = $DB->get_records('glossary', array('course'=>$restore->course_id), '', "id,intro")) { //Iterate over each glossary->intro $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($glossarys as $glossary) { @@ -643,8 +641,8 @@ $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { //Update record - $glossary->intro = addslashes($result); - $status = update_record("glossary",$glossary); + $glossary->intro = $result; + $status = $DB->update_record("glossary",$glossary); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; @@ -670,24 +668,23 @@ //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for //some texts in the module function glossary_restore_wiki2markdown ($restore) { - - global $CFG; + global $CFG, $DB; $status = true; //Convert glossary_comments->entrycomment - if ($records = get_records_sql ("SELECT c.id, c.entrycomment, c.format - FROM {$CFG->prefix}glossary_comments c, - {$CFG->prefix}glossary_entries e, - {$CFG->prefix}glossary g, - {$CFG->prefix}backup_ids b - WHERE e.id = c.entryid AND - g.id = e.glossaryid AND - g.course = $restore->course_id AND - c.format = ".FORMAT_WIKI. " AND - b.backup_code = $restore->backup_unique_code AND - b.table_name = 'glossary_comments' AND - b.new_id = c.id")) { + if ($records = $DB->get_records_sql("SELECT c.id, c.entrycomment, c.format + FROM {glossary_comments} c, + {glossary_entries} e, + {glossary} g, + {backup_ids} b + WHERE e.id = c.entryid AND + g.id = e.glossaryid AND + g.course = ? AND + c.format = ".FORMAT_WIKI. " AND + b.backup_code = ? AND + b.table_name = 'glossary_comments' AND + b.new_id = c.id", array($restore->course_id, $restore->backup_unique_code))) { foreach ($records as $record) { //Rebuild wiki links $record->entrycomment = restore_decode_wiki_content($record->entrycomment, $restore); @@ -695,7 +692,7 @@ $wtm = new WikiToMarkdown(); $record->entrycomment = $wtm->convert($record->entrycomment, $restore->course_id); $record->format = FORMAT_MARKDOWN; - $status = update_record('glossary_comments', addslashes_object($record)); + $status = $DB->update_record('glossary_comments', $record); //Do some output $i++; if (($i+1) % 1 == 0) { @@ -712,16 +709,16 @@ } //Convert glossary_entries->definition - if ($records = get_records_sql ("SELECT e.id, e.definition, e.format - FROM {$CFG->prefix}glossary_entries e, - {$CFG->prefix}glossary g, - {$CFG->prefix}backup_ids b - WHERE g.id = e.glossaryid AND - g.course = $restore->course_id AND - e.format = ".FORMAT_WIKI. " AND - b.backup_code = $restore->backup_unique_code AND - b.table_name = 'glossary_entries' AND - b.new_id = e.id")) { + if ($records = $DB->get_records_sql("SELECT e.id, e.definition, e.format + FROM {glossary_entries} e, + {glossary} g, + {backup_ids} b + WHERE g.id = e.glossaryid AND + g.course = ? AND + e.format = ".FORMAT_WIKI. " AND + b.backup_code = ? AND + b.table_name = 'glossary_entries' AND + b.new_id = e.id", array($restore->course_id, $restore->backup_unique_code))) { foreach ($records as $record) { //Rebuild wiki links $record->definition = restore_decode_wiki_content($record->definition, $restore); @@ -729,7 +726,7 @@ $wtm = new WikiToMarkdown(); $record->definition = $wtm->convert($record->definition, $restore->course_id); $record->format = FORMAT_MARKDOWN; - $status = update_record('glossary_entries', addslashes_object($record)); + $status = $DB->update_record('glossary_entries', $record); //Do some output $i++; if (($i+1) % 1 == 0) { diff --git a/mod/hotpot/restorelib.php b/mod/hotpot/restorelib.php index 844818dd90..36940419e8 100644 --- a/mod/hotpot/restorelib.php +++ b/mod/hotpot/restorelib.php @@ -212,6 +212,7 @@ function hotpot_restore_responses(&$restore, $status, &$xml, &$record) { ); } function hotpot_restore_details(&$restore, $status, &$xml, &$record) { + global $DB; // $xml is an XML tree for an attempt record // $record is the newly added attempt record if (empty($record->details)) { @@ -220,7 +221,7 @@ function hotpot_restore_details(&$restore, $status, &$xml, &$record) { $details = new stdClass(); $details->attempt = $record->id; $details->details = $record->details; - if (insert_record('hotpot_details', $details)) { + if ($DB->insert_record('hotpot_details', $details)) { $status = true; } else { if (!defined('RESTORE_SILENTLY')) { diff --git a/mod/label/restorelib.php b/mod/label/restorelib.php index 6bbe352950..99536593e2 100644 --- a/mod/label/restorelib.php +++ b/mod/label/restorelib.php @@ -63,12 +63,10 @@ } function label_decode_content_links_caller($restore) { - global $CFG; + global $CFG, $DB; $status = true; - if ($labels = get_records_sql ("SELECT l.id, l.content - FROM {$CFG->prefix}label l - WHERE l.course = $restore->course_id")) { + if ($labels = $DB->get_records('label', array('course'=>$restore->course_id), '', "id,content")) { $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($labels as $label) { //Increment counter @@ -78,8 +76,8 @@ if ($result != $content) { //Update record - $label->content = addslashes($result); - $status = update_record("label", $label); + $label->content = $result; + $status = $DB->update_record("label", $label); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; diff --git a/mod/lesson/restorelib.php b/mod/lesson/restorelib.php index 6e2c8c9225..99fe4b9c61 100644 --- a/mod/lesson/restorelib.php +++ b/mod/lesson/restorelib.php @@ -274,7 +274,7 @@ // pre moodle 1.5. Matching questions need two answer fields designated // for correct and wrong responses before the rest of the answer fields. if ($restore->backup_version <= 2004083124) { // Backup version for 1.4.5+ - if ($ismatching = get_record('lesson_pages', 'id', $pageid)) { // get the page we just inserted + if ($ismatching = $DB->get_record('lesson_pages', array('id'=>$pageid))) { // get the page we just inserted if ($ismatching->qtype == 5) { // check to make sure it is a matching question $time = time(); // this may need to be changed // make our 2 response answers @@ -763,15 +763,15 @@ //working in the backup/restore process. It's called from restore_decode_content_links() //function in restore process function lesson_decode_content_links_caller($restore) { - global $CFG; + global $CFG, $DB; $status = true; //Process every lesson PAGE in the course - if ($pages = get_records_sql ("SELECT p.id, p.contents - FROM {$CFG->prefix}lesson_pages p, - {$CFG->prefix}lesson l - WHERE l.course = $restore->course_id AND - p.lessonid = l.id")) { + if ($pages = $DB->get_records_sql("SELECT p.id, p.contents + FROM {lesson_pages} p, + {lesson} l + WHERE l.course = ? AND + p.lessonid = l.id", array($restore->course_id))) { //Iterate over each page->message $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($pages as $page) { @@ -781,8 +781,8 @@ $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { //Update record - $page->contents = addslashes($result); - $status = update_record("lesson_pages",$page); + $page->contents = $result; + $status = $DB->update_record("lesson_pages",$page); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; @@ -803,10 +803,10 @@ } // Remap activity links - if ($lessons = get_records_select('lesson', "activitylink != 0 AND course = $restore->course_id", '', 'id, activitylink')) { + if ($lessons = $DB->get_records_select('lesson', "activitylink <> 0 AND course = ?", array($restore->course_id), '', 'id, activitylink')) { foreach ($lessons as $lesson) { if ($newcmid = backup_getid($restore->backup_unique_code, 'course_modules', $lesson->activitylink)) { - $status = $status and set_field('lesson', 'activitylink', $newcmid->new_id, 'id', $lesson->id); + $status = $status and $DB->set_field('lesson', 'activitylink', $newcmid->new_id, array('id'=>$lesson->id)); } } } diff --git a/mod/quiz/restorelib.php b/mod/quiz/restorelib.php index 0952290ac9..f5ed71eb87 100644 --- a/mod/quiz/restorelib.php +++ b/mod/quiz/restorelib.php @@ -554,12 +554,10 @@ //working in the backup/restore process. It's called from restore_decode_content_links() //function in restore process function quiz_decode_content_links_caller($restore) { - global $CFG; + global $CFG, $DB; $status = true; - if ($quizs = get_records_sql ("SELECT q.id, q.intro - FROM {$CFG->prefix}quiz q - WHERE q.course = $restore->course_id")) { + if ($quizs = $DB->get_records('quiz', array('course'=>$restore->course_id), '', "id,intro")) { //Iterate over each quiz->intro $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($quizs as $quiz) { @@ -569,8 +567,8 @@ $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { //Update record - $quiz->intro = addslashes($result); - $status = update_record("quiz",$quiz); + $quiz->intro = $result; + $status = $DB->update_record("quiz",$quiz); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; @@ -596,19 +594,18 @@ //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for //some texts in the module function quiz_restore_wiki2markdown ($restore) { - - global $CFG; + global $CFG, $DB; $status = true; //Convert question->questiontext - if ($records = get_records_sql ("SELECT q.id, q.questiontext, q.questiontextformat - FROM {$CFG->prefix}question q, - {$CFG->prefix}backup_ids b - WHERE b.backup_code = $restore->backup_unique_code AND - b.table_name = 'question' AND - q.id = b.new_id AND - q.questiontextformat = ".FORMAT_WIKI)) { + if ($records = $DB->get_records_sql("SELECT q.id, q.questiontext, q.questiontextformat + FROM {question} q, + {backup_ids} b + WHERE b.backup_code = ? AND + b.table_name = 'question' AND + q.id = b.new_id AND + q.questiontextformat = ".FORMAT_WIKI, array($restore->backup_unique_code))) { $i = 0; foreach ($records as $record) { //Rebuild wiki links @@ -617,7 +614,7 @@ $wtm = new WikiToMarkdown(); $record->questiontext = $wtm->convert($record->questiontext, $restore->course_id); $record->questiontextformat = FORMAT_MARKDOWN; - $status = update_record('question', addslashes_object($record)); + $status = $DB->update_record('question', $record); //Do some output $i++; if (($i+1) % 1 == 0) { diff --git a/mod/resource/restorelib.php b/mod/resource/restorelib.php index 0862c22b90..5c382e27e8 100644 --- a/mod/resource/restorelib.php +++ b/mod/resource/restorelib.php @@ -200,12 +200,10 @@ //working in the backup/restore process. It's called from restore_decode_content_links() //function in restore process function resource_decode_content_links_caller($restore) { - global $CFG; + global $CFG, $DB; $status = true; - if ($resources = get_records_sql ("SELECT r.id, r.alltext, r.summary, r.reference - FROM {$CFG->prefix}resource r - WHERE r.course = $restore->course_id")) { + if ($resources = $DB->get_records('resource', array('course'=>$restore->course_id), '', "id, alltext, summary, reference")) { $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($resources as $resource) { @@ -220,10 +218,10 @@ if ($result1 != $content1 || $result2 != $content2 || $result3 != $content3) { //Update record - $resource->alltext = addslashes($result1); - $resource->summary = addslashes($result2); - $resource->reference = addslashes($result3); - $status = update_record("resource",$resource); + $resource->alltext = $result1; + $resource->summary = $result2; + $resource->reference = $result3; + $status = $DB->update_record("resource",$resource); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content1).'
changed to
'.s($result1).'

'; @@ -250,20 +248,19 @@ //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for //some texts in the module function resource_restore_wiki2markdown ($restore) { - - global $CFG; + global $CFG, $DB; $status = true; //Convert resource->alltext - if ($records = get_records_sql ("SELECT r.id, r.alltext, r.options - FROM {$CFG->prefix}resource r, - {$CFG->prefix}backup_ids b - WHERE r.course = $restore->course_id AND - options = ".FORMAT_WIKI. " AND - b.backup_code = $restore->backup_unique_code AND - b.table_name = 'resource' AND - b.new_id = r.id")) { + if ($records = $DB->get_records_sql("SELECT r.id, r.alltext, r.options + FROM {resource} r, + {backup_ids} b + WHERE r.course = ? AND + options = ".FORMAT_WIKI. " AND + b.backup_code = ? AND + b.table_name = 'resource' AND + b.new_id = r.id", array($restore->course_id, $restore->backup_unique_code))) { foreach ($records as $record) { //Rebuild wiki links $record->alltext = restore_decode_wiki_content($record->alltext, $restore); @@ -271,7 +268,7 @@ $wtm = new WikiToMarkdown(); $record->alltext = $wtm->convert($record->alltext, $restore->course_id); $record->options = FORMAT_MARKDOWN; - $status = update_record('resource', addslashes_object($record)); + $status = $DB->update_record('resource', $record); //Do some output $i++; if (($i+1) % 1 == 0) { diff --git a/mod/scorm/restorelib.php b/mod/scorm/restorelib.php index 2ad7b34469..5f800f7982 100755 --- a/mod/scorm/restorelib.php +++ b/mod/scorm/restorelib.php @@ -808,9 +808,7 @@ global $CFG; $status = true; - if ($scorms = get_records_sql ("SELECT s.id, s.summary - FROM {$CFG->prefix}scorm s - WHERE s.course = $restore->course_id")) { + if ($scorms = $DB->get_records('scorm', array('course'=>$restore->course_id), '', "id,summary")) { $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($scorms as $scorm) { @@ -821,11 +819,11 @@ if ($result != $content) { //Update record - $scorm->summary = addslashes($result); - $status = update_record("scorm",$scorm); + $scorm->summary = $result; + $status = $DB->update_record("scorm",$scorm); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { - echo '

'.htmlentities($content).'
changed to
'.htmlentities($result).'

'; + echo '

'.s($content).'
changed to
'.s($result).'

'; } } } diff --git a/mod/survey/restorelib.php b/mod/survey/restorelib.php index 47aa03891d..9c2a253738 100644 --- a/mod/survey/restorelib.php +++ b/mod/survey/restorelib.php @@ -266,12 +266,10 @@ //working in the backup/restore process. It's called from restore_decode_content_links() //function in restore process function survey_decode_content_links_caller($restore) { - global $CFG; + global $CFG, $DB; $status = true; - if ($surveys = get_records_sql ("SELECT s.id, s.intro - FROM {$CFG->prefix}survey s - WHERE s.course = $restore->course_id")) { + if ($surveys = $DB->get_records('survey', array('course'=>$restore->course_id), '', "id,intro")) { //Iterate over each survey->intro $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($surveys as $survey) { @@ -281,8 +279,8 @@ $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { //Update record - $survey->intro = addslashes($result); - $status = update_record("survey",$survey); + $survey->intro = $result; + $status = $DB->update_record("survey",$survey); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; diff --git a/mod/wiki/restorelib.php b/mod/wiki/restorelib.php index f34531d6ca..43dbe15b06 100644 --- a/mod/wiki/restorelib.php +++ b/mod/wiki/restorelib.php @@ -339,15 +339,14 @@ //working in the backup/restore process. It's called from restore_decode_content_links() //function in restore process function wiki_decode_content_links_caller($restore) { - global $CFG; + global $CFG, $DB; $status = true; //Process every wiki PAGE in the course - if ($pages = get_records_sql ("SELECT p.id, p.content - FROM {$CFG->prefix}wiki_pages p, - {$CFG->prefix}wiki w - WHERE w.course = $restore->course_id AND - p.wiki = w.id")) { + if ($pages = $DB->get_records_sql("SELECT p.id, p.content + FROM {wiki_pages} p, {wiki} w + WHERE w.course = ? AND + p.wiki = w.id", array($restore->course_id))) { //Iterate over each post->message $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($pages as $page) { @@ -357,8 +356,8 @@ $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { //Update record - $page->content = addslashes($result); - $status = update_record("wiki_pages",$page); + $page->content = $result; + $status = $DB->update_record("wiki_pages",$page); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; @@ -379,9 +378,7 @@ } //Process every wiki (summary) in the course - if ($wikis = get_records_sql ("SELECT w.id, w.summary - FROM {$CFG->prefix}wiki w - WHERE w.course = $restore->course_id")) { + if ($wikis = $DB->get_records('wiki', array('course'=>$restore->course_id), '', "id,summary")) { //Iterate over each wiki->summary $i = 0; //Counter to send some output to the browser to avoid timeouts foreach ($wikis as $wiki) { @@ -391,8 +388,8 @@ $result = restore_decode_content_links_worker($content,$restore); if ($result != $content) { //Update record - $wiki->summary = addslashes($result); - $status = update_record("wiki",$wiki); + $wiki->summary = $result; + $status = $DB->update_record("wiki",$wiki); if (debugging()) { if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; diff --git a/question/restorelib.php b/question/restorelib.php index bd2317f018..9128a932f9 100644 --- a/question/restorelib.php +++ b/question/restorelib.php @@ -94,6 +94,8 @@ * @return object best context instance for this category to be in */ function restore_question_get_best_category_context($restore, $contextinfo) { + global $DB; + switch ($contextinfo['LEVEL'][0]['#']) { case 'module': if (!$instanceinfo = backup_getid($restore->backup_unique_code, 'course_modules', $contextinfo['INSTANCE'][0]['#'])){ @@ -109,9 +111,9 @@ //search COURSECATEGORYLEVEL steps up the course cat tree or //to the top of the tree if steps are exhausted. $catno = $contextinfo['COURSECATEGORYLEVEL'][0]['#']; - $catid = get_field('course', 'category', 'id', $restore->course_id); + $catid = $DB->get_field('course', 'category', array('id'=>$restore->course_id)); while ($catno > 1){ - $nextcatid = get_field('course_categories', 'parent', 'id', $catid); + $nextcatid = $DB->get_field('course_categories', 'parent', array('id'=>$catid)); if ($nextcatid == 0){ break; } @@ -192,7 +194,7 @@ } $question_cat->contextid = $tocontext->id; if (!$fcat = $DB->get_record('question_categories', array('contextid'=>$question_cat->contextid, 'stamp'=>$question_cat->stamp))) { - $question_cat->id = insert_record ("question_categories", $question_cat); + $question_cat->id = $DB->insert_record ("question_categories", $question_cat); } else { $question_cat = $fcat; } @@ -231,17 +233,17 @@ } function restore_recode_category_parents($restore){ - global $CFG; + global $CFG, $DB; $status = true; //Now we have to recode the parent field of each restored category - $categories = get_records_sql("SELECT old_id, new_id - FROM {$CFG->prefix}backup_ids - WHERE backup_code = $restore->backup_unique_code AND - table_name = 'question_categories'"); + $categories = $DB->get_records_sql("SELECT old_id, new_id + FROM {backup_ids} + WHERE backup_code = ? AND + table_name = 'question_categories'", array($restore->backup_unique_code)); if ($categories) { //recode all parents to point at their old parent cats no matter what context the parent is now in foreach ($categories as $category) { - $restoredcategory = get_record('question_categories','id',$category->new_id); + $restoredcategory = $DB->get_record('question_categories', array('id'=>$category->new_id)); if ($restoredcategory && $restoredcategory->parent != 0) { $updateobj = new object(); $updateobj->id = $restoredcategory->id; @@ -251,7 +253,7 @@ } else { $updateobj->parent = 0; } - $status = $status && update_record('question_categories', $updateobj); + $status = $status && $DB->update_record('question_categories', $updateobj); } } //now we have recoded all parents, check through all parents and set parent to be @@ -259,11 +261,11 @@ //or else set parent to 0 (top level category). $toupdate = array(); foreach ($categories as $category) { - $restoredcategory = get_record('question_categories','id',$category->new_id); + $restoredcategory = $DB->get_record('question_categories', array('id'=>$category->new_id)); if ($restoredcategory && $restoredcategory->parent != 0) { $nextparentid = $restoredcategory->parent; do { - if (!$parent = get_record('question_categories', 'id', $nextparentid)){ + if (!$parent = $DB->get_record('question_categories', array('id'=>$nextparentid))) { if (!defined('RESTORE_SILENTLY')) { echo 'Could not find parent for question category '. $category->id.' recoding as top category item.
'; } @@ -289,7 +291,7 @@ $updateobj = new object(); $updateobj->id = $id; $updateobj->parent = $parent; - $status = $status && update_record('question_categories', $updateobj); + $status = $status && $DB->update_record('question_categories', $updateobj); } } return $status; @@ -376,7 +378,7 @@ } $question->category = $course_question_cat->id; //does question already exist in course cat - $existingquestion = get_record ("question", "category", $question->category, "stamp", $question->stamp, "version", $question->version); + $existingquestion = $DB->get_record("question", array("category"=>$question->category, "stamp"=>$question->stamp, "version"=>$question->version)); } else { //permissions ok, restore to best cat $question->category = $best_question_cat->id; @@ -919,7 +921,7 @@ * @return boolean whether the operation succeeded. */ function question_decode_content_links_caller($restore) { - global $CFG, $QTYPES; + global $CFG, $QTYPES, $DB; $status = true; $i = 1; //Counter to send some output to the browser to avoid timeouts @@ -935,7 +937,7 @@ $coursemodulecontexts = array(); $context = get_context_instance(CONTEXT_COURSE, $restore->course_id); $coursemodulecontexts[] = $context->id; - $cms = get_records('course_modules', 'course', $restore->course_id, '', 'id'); + $cms = $DB->get_records('course_modules', array('course'=>$restore->course_id), '', 'id'); if ($cms){ foreach ($cms as $cm){ $context = get_context_instance(CONTEXT_MODULE, $cm->id); @@ -944,19 +946,18 @@ } $coursemodulecontextslist = join($coursemodulecontexts, ','); // Decode links in questions. - if ($questions = get_records_sql('SELECT q.id, q.qtype, q.questiontext, q.generalfeedback '. - 'FROM ' . $CFG->prefix . 'question q, '. - $CFG->prefix . 'question_categories qc '. - 'WHERE q.category = qc.id '. - 'AND qc.contextid IN (' .$coursemodulecontextslist.')')) { + if ($questions = $DB->get_records_sql('SELECT q.id, q.qtype, q.questiontext, q.generalfeedback + FROM {question} q, {question_categories} qc + WHERE q.category = qc.id + AND qc.contextid IN (' .$coursemodulecontextslist.')')) { foreach ($questions as $question) { $questiontext = restore_decode_content_links_worker($question->questiontext, $restore); $generalfeedback = restore_decode_content_links_worker($question->generalfeedback, $restore); if ($questiontext != $question->questiontext || $generalfeedback != $question->generalfeedback) { - $question->questiontext = addslashes($questiontext); - $question->generalfeedback = addslashes($generalfeedback); - if (!update_record('question', $question)) { + $question->questiontext = $questiontext; + $question->generalfeedback = $generalfeedback; + if (!$DB->update_record('question', $question)) { $status = false; } } @@ -981,13 +982,11 @@ } // Decode links in answers. - if ($answers = get_records_sql('SELECT qa.id, qa.answer, qa.feedback, q.qtype - FROM ' . $CFG->prefix . 'question_answers qa, - ' . $CFG->prefix . 'question q, - ' . $CFG->prefix . 'question_categories qc - WHERE qa.question = q.id - AND q.category = qc.id '. - 'AND qc.contextid IN ('.$coursemodulecontextslist.')')) { + if ($answers = $DB->get_records_sql('SELECT qa.id, qa.answer, qa.feedback, q.qtype + FROM {question_answers} qa, {question} q, {question_categories} qc + WHERE qa.question = q.id + AND q.category = qc.id + AND qc.contextid IN ('.$coursemodulecontextslist.')')) { foreach ($answers as $answer) { $feedback = restore_decode_content_links_worker($answer->feedback, $restore); @@ -998,9 +997,9 @@ } if ($feedback != $answer->feedback || $answertext != $answer->answer) { unset($answer->qtype); - $answer->feedback = addslashes($feedback); - $answer->answer = addslashes($answertext); - if (!update_record('question_answers', $answer)) { + $answer->feedback = $feedback; + $answer->answer = $answertext; + if (!$DB->update_record('question_answers', $answer)) { $status = false; } } -- 2.39.5