From dbc51726186f9909a83e4e9772737c2bd51600ba Mon Sep 17 00:00:00 2001 From: Julian Prud'homme Date: Thu, 28 Aug 2008 10:52:29 +0200 Subject: [PATCH] [svn r16101] FS#2734 : Fix a bug when copying surveys in another course --- documentation/changelog.html | 4 +- .../classes/CourseRestorer.class.php | 158 ++++++++++++++++-- main/survey/survey.lib.php | 8 +- 3 files changed, 151 insertions(+), 19 deletions(-) diff --git a/documentation/changelog.html b/documentation/changelog.html index 4650ab9b6b..127242642a 100644 --- a/documentation/changelog.html +++ b/documentation/changelog.html @@ -71,7 +71,9 @@
  • The mails sent in exercises tool are sent from the address defined in admin (FS#2712)
  • "Next" and "Previous" page now works when searching a session (FS#2721)
  • Fixed bug when launching a quiz with one question per page (FS#2738)
  • -
  • fix a javascript bug with swap menu in ie6 (FS#2815)
  • +
  • fix a javascript bug with swap menu in ie6 (FS#2815)
  • +
  • Fix a bug in surveys when we want to display answers of an invited person (FS#2731)
  • +
  • Fix a bug when copying a course with surveys. There is now a check for existing surveys with the same code and language (FS#2734)

  • Known issues

    diff --git a/main/coursecopy/classes/CourseRestorer.class.php b/main/coursecopy/classes/CourseRestorer.class.php index b0a3f885ce..54e605df57 100644 --- a/main/coursecopy/classes/CourseRestorer.class.php +++ b/main/coursecopy/classes/CourseRestorer.class.php @@ -1,4 +1,4 @@ -course->destination_db); $resources = $this->course->resources; foreach ($resources[RESOURCE_SURVEY] as $id => $survey) - { + { + + $sql_check = 'SELECT survey_id FROM '.$table_sur.' + WHERE code = "'.Database::escape_string($survey->code).'" + AND lang="'.Database::escape_string($survey->lang).'" + '; + + $result_check = api_sql_query($sql_check, __FILE__, __LINE__); + $doc = ''; $sql = "INSERT INTO ".$table_sur." " . "SET code = '".Database::escape_string($survey->code)."', " . @@ -675,24 +683,146 @@ class CourseRestorer "answered = '0', " . "invite_mail = '".Database::escape_string($survey->invite_mail)."', " . "reminder_mail = '".Database::escape_string($survey->reminder_mail)."'"; - api_sql_query($sql, __FILE__, __LINE__); - $new_id = Database::get_last_insert_id(); - $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id; - foreach ($survey->question_ids as $index => $question_id) + + //An existing survey exists with the same code and the same language + if(Database::num_rows($result_check) == 1) + { + + switch ($this->file_option) { + + case FILE_SKIP: + //Do nothing + break; + + case FILE_RENAME: + + $survey_code = $survey->code.'_'; + $i=1; + $temp_survey_code = $survey_code.$i; + while (!$this->is_survey_code_available($temp_survey_code)) + { + $temp_survey_code = $survey_code.++$i; + } + $survey_code = $temp_survey_code; + + $sql = "INSERT INTO ".$table_sur." " . + "SET code = '".Database::escape_string($survey_code)."', " . + "title = '".Database::escape_string($survey->title)."', " . + "subtitle = '".Database::escape_string($survey->subtitle)."', " . + "author = '".Database::escape_string($survey->author)."', " . + "lang = '".Database::escape_string($survey->lang)."', " . + "avail_from = '".Database::escape_string($survey->avail_from)."', " . + "avail_till = '".Database::escape_string($survey->avail_till)."', " . + "is_shared = '".Database::escape_string($survey->is_shared)."', " . + "template = '".Database::escape_string($survey->template)."', " . + "intro = '".Database::escape_string($survey->intro)."', " . + "surveythanks = '".Database::escape_string($survey->surveythanks)."', " . + "creation_date = '".Database::escape_string($survey->creation_date)."', " . + "invited = '0', " . + "answered = '0', " . + "invite_mail = '".Database::escape_string($survey->invite_mail)."', " . + "reminder_mail = '".Database::escape_string($survey->reminder_mail)."'"; + + //Insert the new source survey + api_sql_query($sql, __FILE__, __LINE__); + + $new_id = Database::get_last_insert_id(); + $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id; + foreach ($survey->question_ids as $index => $question_id) + { + $qid = $this->restore_survey_question($question_id); + $sql = "UPDATE ".$table_que." " . + "SET survey_id = ".$new_id." WHERE " . + "question_id = ".$qid.""; + api_sql_query($sql, __FILE__, __LINE__); + $sql = "UPDATE ".$table_ans." ". + "SET survey_id = ".$new_id." WHERE " . + "question_id = ".$qid.""; + api_sql_query($sql, __FILE__, __LINE__); + } + + break; + + case FILE_OVERWRITE: + + // Delete the existing survey with the same code and language and import the one of the source course + + // getting the information of the survey (used for when the survey is shared) + require_once(api_get_path(SYS_CODE_PATH).'survey/survey.lib.php'); + + $sql_select_existing_survey = "SELECT * FROM $table_sur WHERE survey_id='".Database::escape_string(mysql_result($result_check,0,0))."'"; + $result = api_sql_query($sql_select_existing_survey, __FILE__, __LINE__); + $survey_data = Database::fetch_array($result,'ASSOC'); + + // if the survey is shared => also delete the shared content + if (is_numeric($survey_data['survey_share'])) + { + survey_manager::delete_survey($survey_data['survey_share'], true,$this->course->destination_db); + } + $return = survey_manager :: delete_survey($survey_data['survey_id'],false,$this->course->destination_db); + + //Insert the new source survey + api_sql_query($sql, __FILE__, __LINE__); + + $new_id = Database::get_last_insert_id(); + $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id; + foreach ($survey->question_ids as $index => $question_id) + { + $qid = $this->restore_survey_question($question_id); + $sql = "UPDATE ".$table_que." " . + "SET survey_id = ".$new_id." WHERE " . + "question_id = ".$qid.""; + api_sql_query($sql, __FILE__, __LINE__); + $sql = "UPDATE ".$table_ans." ". + "SET survey_id = ".$new_id." WHERE " . + "question_id = ".$qid.""; + api_sql_query($sql, __FILE__, __LINE__); + } + + break; + + default: + break; + } + + + } + //No existing survey with the same language and the same code, we just copy the survey + else { - $qid = $this->restore_survey_question($question_id); - $sql = "UPDATE ".$table_que." " . - "SET survey_id = ".$new_id." WHERE " . - "question_id = ".$qid.""; - api_sql_query($sql, __FILE__, __LINE__); - $sql = "UPDATE ".$table_ans." ". - "SET survey_id = ".$new_id." WHERE " . - "question_id = ".$qid.""; api_sql_query($sql, __FILE__, __LINE__); + $new_id = Database::get_last_insert_id(); + $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id; + foreach ($survey->question_ids as $index => $question_id) + { + $qid = $this->restore_survey_question($question_id); + $sql = "UPDATE ".$table_que." " . + "SET survey_id = ".$new_id." WHERE " . + "question_id = ".$qid.""; + api_sql_query($sql, __FILE__, __LINE__); + $sql = "UPDATE ".$table_ans." ". + "SET survey_id = ".$new_id." WHERE " . + "question_id = ".$qid.""; + api_sql_query($sql, __FILE__, __LINE__); + } } + } } } + + /** + * Check availability of a survey code + */ + function is_survey_code_available($survey_code){ + + $table_sur = Database :: get_course_table(TABLE_SURVEY, $this->course->destination_db); + $sql = "SELECT * FROM $table_sur WHERE code='".Database::escape_string($survey_code)."'"; + $result = api_sql_query($sql, __FILE__, __LINE__); + if(Database::num_rows($result) > 0) return false; else return true; + + } + /** * Restore survey-questions */ diff --git a/main/survey/survey.lib.php b/main/survey/survey.lib.php index b3ecaef7c0..18ce9a8af6 100644 --- a/main/survey/survey.lib.php +++ b/main/survey/survey.lib.php @@ -24,7 +24,7 @@ * @package dokeos.survey * @author Patrick Cool , Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code @author Julio Montoya Armas , Dokeos: Personality Test modification and rewriting large parts of the code -* @version $Id: survey.lib.php 16077 2008-08-27 08:18:04Z elixir_julian $ +* @version $Id: survey.lib.php 16101 2008-08-28 08:52:29Z elixir_julian $ * * @todo move this file to inc/lib * @todo use consistent naming for the functions (save vs store for instance) @@ -376,11 +376,11 @@ class survey_manager * @author Patrick Cool , Ghent University * @version January 2007 */ - function delete_survey($survey_id, $shared=false) + function delete_survey($survey_id, $shared=false, $course_code = '') { // Database table definitions - $table_survey = Database :: get_course_table(TABLE_SURVEY); - $table_survey_question_group = Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP); + $table_survey = Database :: get_course_table(TABLE_SURVEY,$course_code); + $table_survey_question_group = Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP,$course_code); if ($shared) { $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY);