Skip to content

Commit 890acb2

Browse files
committed
Internal: Move ExerciseLib::replaceTermsInContent() function to api.lib.php
1 parent cc58054 commit 890acb2

File tree

3 files changed

+377
-377
lines changed

3 files changed

+377
-377
lines changed

main/inc/lib/api.lib.php

Lines changed: 375 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10699,3 +10699,378 @@ function api_encrypt_hash($data, $secret)
1069910699

1070010700
return base64_encode($iv).base64_encode($encrypted.$tag);
1070110701
}
10702+
10703+
function api_replace_terms_in_content(string $search, string $replace): array
10704+
{
10705+
$replacements = [
10706+
Database::get_course_table(TABLE_QUIZ_TEST) => [
10707+
'iid' => ['title', 'description', 'sound'],
10708+
],
10709+
Database::get_course_table(TABLE_QUIZ_QUESTION) => [
10710+
'iid' => ['question', 'description'],
10711+
],
10712+
Database::get_course_table(TABLE_QUIZ_ANSWER) => [
10713+
'iid' => ['answer', 'comment'],
10714+
],
10715+
Database::get_course_table(TABLE_ANNOUNCEMENT) => [
10716+
'iid' => ['title', 'content'],
10717+
],
10718+
Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT) => [
10719+
'iid' => ['path', 'comment'],
10720+
],
10721+
Database::get_course_table(TABLE_ATTENDANCE) => [
10722+
'iid' => ['name', 'description', 'attendance_qualify_title'],
10723+
],
10724+
Database::get_course_table(TABLE_BLOGS) => [
10725+
'iid' => ['blog_name', 'blog_subtitle'],
10726+
],
10727+
Database::get_course_table(TABLE_BLOGS_ATTACHMENT) => [
10728+
'iid' => ['path', 'comment'],
10729+
],
10730+
Database::get_course_table(TABLE_BLOGS_COMMENTS) => [
10731+
'iid' => ['title', 'comment'],
10732+
],
10733+
Database::get_course_table(TABLE_BLOGS_POSTS) => [
10734+
'iid' => ['title', 'full_text'],
10735+
],
10736+
Database::get_course_table(TABLE_BLOGS_TASKS) => [
10737+
'iid' => ['title', 'description'],
10738+
],
10739+
Database::get_course_table(TABLE_AGENDA) => [
10740+
'iid' => ['title', 'content', 'comment'],
10741+
],
10742+
Database::get_course_table(TABLE_AGENDA_ATTACHMENT) => [
10743+
'iid' => ['path', 'comment', 'filename'],
10744+
],
10745+
Database::get_course_table(TABLE_COURSE_DESCRIPTION) => [
10746+
'iid' => ['title', 'content'],
10747+
],
10748+
Database::get_course_table(TABLE_DOCUMENT) => [
10749+
'iid' => ['path', 'comment'],
10750+
],
10751+
Database::get_course_table(TABLE_DROPBOX_FEEDBACK) => [
10752+
'iid' => ['feedback'],
10753+
],
10754+
Database::get_course_table(TABLE_DROPBOX_FILE) => [
10755+
'iid' => ['title', 'description'],
10756+
],
10757+
Database::get_course_table(TABLE_DROPBOX_POST) => [
10758+
'iid' => ['feedback'],
10759+
],
10760+
Database::get_course_table(TABLE_FORUM_ATTACHMENT) => [
10761+
'iid' => ['path', 'comment', 'filename'],
10762+
],
10763+
Database::get_course_table(TABLE_FORUM_CATEGORY) => [
10764+
'iid' => ['cat_title', 'cat_comment'],
10765+
],
10766+
Database::get_course_table(TABLE_FORUM) => [
10767+
'iid' => ['forum_title', 'forum_comment', 'forum_image'],
10768+
],
10769+
Database::get_course_table(TABLE_FORUM_POST) => [
10770+
'iid' => ['post_title', 'post_text', 'poster_name'],
10771+
],
10772+
Database::get_course_table(TABLE_FORUM_THREAD) => [
10773+
'iid' => ['thread_title', 'thread_poster_name', 'thread_title_qualify'],
10774+
],
10775+
Database::get_course_table(TABLE_GLOSSARY) => [
10776+
'iid' => ['name', 'description'],
10777+
],
10778+
Database::get_course_table(TABLE_GROUP_CATEGORY) => [
10779+
'iid' => ['title', 'description'],
10780+
],
10781+
Database::get_course_table(TABLE_GROUP) => [
10782+
'iid' => ['name', 'description', 'secret_directory'],
10783+
],
10784+
Database::get_course_table(TABLE_LINK) => [
10785+
'iid' => ['description'],
10786+
],
10787+
Database::get_course_table(TABLE_LINK_CATEGORY) => [
10788+
'iid' => ['category_title', 'description'],
10789+
],
10790+
Database::get_course_table(TABLE_LP_MAIN) => [
10791+
'iid' => ['name', 'ref', 'description', 'path', 'content_license', 'preview_image', 'theme'],
10792+
],
10793+
Database::get_course_table(TABLE_LP_CATEGORY) => [
10794+
'iid' => ['name'],
10795+
],
10796+
Database::get_course_table(TABLE_LP_ITEM) => [
10797+
'iid' => ['prerequisite', 'description', 'title', 'parameters', 'launch_data', 'terms'],
10798+
],
10799+
Database::get_course_table(TABLE_LP_ITEM_VIEW) => [
10800+
'iid' => ['suspend_data', 'lesson_location'],
10801+
],
10802+
Database::get_course_table(TABLE_NOTEBOOK) => [
10803+
'iid' => ['title', 'description'],
10804+
],
10805+
Database::get_course_table(TABLE_ONLINE_LINK) => [
10806+
'iid' => ['name'],
10807+
],
10808+
Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY) => [
10809+
'iid' => ['title', 'description'],
10810+
],
10811+
Database::get_course_table(TABLE_ROLE) => [
10812+
'iid' => ['role_name', 'role_comment'],
10813+
],
10814+
Database::get_course_table(TABLE_STUDENT_PUBLICATION) => [
10815+
'iid' => ['title', 'title_correction', 'description'],
10816+
],
10817+
Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT) => [
10818+
'iid' => ['comment', 'file'],
10819+
],
10820+
Database::get_course_table(TABLE_SURVEY) => [
10821+
'iid' => ['title', 'subtitle', 'surveythanks', 'invite_mail', 'reminder_mail', 'mail_subject', 'access_condition', 'form_fields'],
10822+
],
10823+
Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP) => [
10824+
'iid' => ['name', 'description'],
10825+
],
10826+
Database::get_course_table(TABLE_SURVEY_QUESTION) => [
10827+
'iid' => ['survey_question', 'survey_question_comment'],
10828+
],
10829+
Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION) => [
10830+
'iid' => ['option_text'],
10831+
],
10832+
Database::get_course_table(TABLE_THEMATIC) => [
10833+
'iid' => ['content', 'title'],
10834+
],
10835+
Database::get_course_table(TABLE_THEMATIC_ADVANCE) => [
10836+
'iid' => ['content'],
10837+
],
10838+
Database::get_course_table(TABLE_THEMATIC_PLAN) => [
10839+
'iid' => ['description'],
10840+
],
10841+
Database::get_course_table(TABLE_TOOL_LIST) => [
10842+
'iid' => ['description'],
10843+
],
10844+
Database::get_course_table(TABLE_TOOL_INTRO) => [
10845+
'iid' => ['intro_text'],
10846+
],
10847+
Database::get_course_table(TABLE_USER_INFO_DEF) => [
10848+
'iid' => ['comment'],
10849+
],
10850+
Database::get_course_table(TABLE_WIKI) => [
10851+
'iid' => ['title', 'content', 'comment', 'progress', 'linksto'],
10852+
],
10853+
Database::get_course_table(TABLE_WIKI_CONF) => [
10854+
'iid' => ['feedback1', 'feedback2', 'feedback3'],
10855+
],
10856+
Database::get_course_table(TABLE_WIKI_DISCUSS) => [
10857+
'iid' => ['comment'],
10858+
],
10859+
Database::get_main_table(TABLE_CAREER) => [
10860+
'id' => ['name', 'description'],
10861+
],
10862+
Database::get_main_table(TABLE_MAIN_CHAT) => [
10863+
'id' => ['message'],
10864+
],
10865+
Database::get_main_table(TABLE_MAIN_CLASS) => [
10866+
'id' => ['name'],
10867+
],
10868+
Database::get_main_table(TABLE_MAIN_COURSE_REQUEST) => [
10869+
'id' => ['description', 'title', 'objetives', 'target_audience'],
10870+
],
10871+
'course_type' => [
10872+
'id' => ['description'],
10873+
],
10874+
Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE) => [
10875+
'id' => ['message', 'subject', 'event_type_name'],
10876+
],
10877+
Database::get_main_table(TABLE_GRADE_MODEL) => [
10878+
'id' => ['name', 'description'],
10879+
],
10880+
Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY) => [
10881+
'id' => ['name', 'description'],
10882+
],
10883+
Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE) => [
10884+
'id' => ['path_certificate'],
10885+
],
10886+
Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION) => [
10887+
'id' => ['description', 'name'],
10888+
],
10889+
Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG) => [
10890+
'id' => ['name', 'description'],
10891+
],
10892+
Database::get_main_table(TABLE_MAIN_LEGAL) => [
10893+
'id' => ['content', 'changes'],
10894+
],
10895+
Database::get_main_table(TABLE_MESSAGE) => [
10896+
'id' => ['content'],
10897+
],
10898+
Database::get_main_table(TABLE_MESSAGE_ATTACHMENT) => [
10899+
'id' => ['path', 'comment', 'filename'],
10900+
],
10901+
Database::get_main_table(TABLE_NOTIFICATION) => [
10902+
'id' => ['content'],
10903+
],
10904+
Database::get_main_table(TABLE_PERSONAL_AGENDA) => [
10905+
'id' => ['title', 'text'],
10906+
],
10907+
Database::get_main_table(TABLE_PROMOTION) => [
10908+
'id' => ['description'],
10909+
],
10910+
'room' => [
10911+
'id' => ['description'],
10912+
],
10913+
'sequence_condition' => [
10914+
'id' => ['description'],
10915+
],
10916+
'sequence_method' => [
10917+
'id' => ['description', 'formula'],
10918+
],
10919+
'sequence_rule' => [
10920+
'id' => ['description'],
10921+
],
10922+
'sequence_type_entity' => [
10923+
'id' => ['description'],
10924+
],
10925+
'sequence_variable' => [
10926+
'id' => ['description'],
10927+
],
10928+
Database::get_main_table(TABLE_MAIN_SESSION) => [
10929+
'id' => ['description'],
10930+
],
10931+
Database::get_main_table(TABLE_MAIN_SHARED_SURVEY) => [
10932+
'survey_id' => ['subtitle', 'surveythanks', 'intro'],
10933+
],
10934+
Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION) => [
10935+
'question_id' => ['survey_question', 'survey_question_comment'],
10936+
],
10937+
Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION) => [
10938+
'question_option_id' => ['option_text'],
10939+
],
10940+
Database::get_main_table(TABLE_MAIN_SKILL) => [
10941+
'id' => ['name', 'description', 'criteria'],
10942+
],
10943+
Database::get_main_table(TABLE_MAIN_SKILL_PROFILE) => [
10944+
'id' => ['description'],
10945+
],
10946+
Database::get_main_table(TABLE_MAIN_SKILL_REL_USER) => [
10947+
'id' => ['argumentation'],
10948+
],
10949+
'skill_rel_user_comment' => [
10950+
'id' => ['feedback_text'],
10951+
],
10952+
Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS) => [
10953+
'id' => ['content'],
10954+
],
10955+
Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR) => [
10956+
'id' => ['content'],
10957+
],
10958+
Database::get_main_table(TABLE_MAIN_SYSTEM_TEMPLATE) => [
10959+
'id' => ['comment', 'content'],
10960+
],
10961+
Database::get_main_table(TABLE_MAIN_TEMPLATES) => [
10962+
'id' => ['description', 'image'],
10963+
],
10964+
Database::get_main_table(TABLE_TICKET_CATEGORY) => [
10965+
'id' => ['description'],
10966+
],
10967+
Database::get_main_table(TABLE_TICKET_MESSAGE) => [
10968+
'id' => ['message'],
10969+
],
10970+
Database::get_main_table(TABLE_TICKET_MESSAGE_ATTACHMENTS) => [
10971+
'id' => ['filename', 'path'],
10972+
],
10973+
Database::get_main_table(TABLE_TICKET_PRIORITY) => [
10974+
'id' => ['description'],
10975+
],
10976+
Database::get_main_table(TABLE_TICKET_PROJECT) => [
10977+
'id' => ['description'],
10978+
],
10979+
Database::get_main_table(TABLE_TICKET_STATUS) => [
10980+
'id' => ['description'],
10981+
],
10982+
Database::get_main_table(TABLE_TICKET_TICKET) => [
10983+
'id' => ['message'],
10984+
],
10985+
Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT) => [
10986+
'id' => ['answer', 'teacher_comment', 'filename'],
10987+
],
10988+
Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING) => [
10989+
'id' => ['teacher_comment'],
10990+
],
10991+
Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT) => [
10992+
'default_id' => ['default_value'],
10993+
],
10994+
Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES) => [
10995+
'exe_id' => ['data_tracking', 'questions_to_check'],
10996+
],
10997+
Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY) => [
10998+
'id' => ['content'],
10999+
],
11000+
'track_e_open' => [
11001+
'open_id' => ['open_remote_host', 'open_agent', 'open_referer'],
11002+
],
11003+
Database::get_main_table(TABLE_TRACK_STORED_VALUES) => [
11004+
'id' => ['sv_value'],
11005+
],
11006+
Database::get_main_table(TABLE_TRACK_STORED_VALUES_STACK) => [
11007+
'id' => ['sv_value'],
11008+
],
11009+
Database::get_main_table(TABLE_MAIN_USER_API_KEY) => [
11010+
'id' => ['description'],
11011+
],
11012+
Database::get_main_table(TABLE_USERGROUP) => [
11013+
'id' => ['name', 'description', 'picture', 'url'],
11014+
],
11015+
Database::get_main_table(TABLE_MAIN_BLOCK) => [
11016+
'id' => ['name', 'description', 'path'],
11017+
],
11018+
];
11019+
11020+
if (api_get_configuration_value('attendance_allow_comments')) {
11021+
$replacements['c_attendance_result_comment'] = [
11022+
'iid' => ['comment'],
11023+
];
11024+
}
11025+
11026+
if (api_get_configuration_value('exercise_text_when_finished_failure')) {
11027+
$replacements[Database::get_course_table(TABLE_QUIZ_TEST)]['iid'][] = 'text_when_finished_failure';
11028+
}
11029+
11030+
$changes = array_map(
11031+
fn ($table) => 0,
11032+
$replacements
11033+
);
11034+
11035+
foreach ($replacements as $table => $replacement) {
11036+
foreach ($replacement as $idColumn => $columns) {
11037+
$keys = array_map(fn ($column) => "$column LIKE %?%", $columns);
11038+
$values = array_fill(0, count($columns), $search);
11039+
11040+
$result = Database::select(
11041+
[$idColumn, ...$columns],
11042+
$table,
11043+
[
11044+
'where' => [
11045+
implode(' OR ', $keys) => $values,
11046+
],
11047+
'order' => "$idColumn ASC",
11048+
]
11049+
);
11050+
11051+
foreach ($result as $row) {
11052+
$attributes = array_combine(
11053+
$columns,
11054+
array_map(
11055+
fn ($column) => preg_replace('#'.$search.'#', $replace, $row[$column]),
11056+
$columns
11057+
)
11058+
);
11059+
11060+
try {
11061+
Database::update(
11062+
$table,
11063+
$attributes,
11064+
["$idColumn = ?" => $row[$idColumn]]
11065+
);
11066+
} catch (Exception $e) {
11067+
Database::handleError($e);
11068+
}
11069+
11070+
$changes[$table]++;
11071+
}
11072+
}
11073+
}
11074+
11075+
return $changes;
11076+
}

0 commit comments

Comments
 (0)