Permalink
Browse files

Improved coice mod, allows an unlimited number of choices.

  • Loading branch information...
danmarsden
danmarsden committed Mar 29, 2005
1 parent 67ac95e commit 6fd87e3bc7c82bffd19a17ac2713662075ef4857
View
@@ -9,7 +9,7 @@
// |
// |
// |
- // choice_answers
+ // choice_responses
// (UL,pk->id, fk->choice)
//
// Meaning: pk->primary key field of the table
@@ -39,40 +39,65 @@ function choice_backup_mods($bf,$preferences) {
fwrite ($bf,full_tag("NAME",4,false,$choice->name));
fwrite ($bf,full_tag("TEXT",4,false,$choice->text));
fwrite ($bf,full_tag("FORMAT",4,false,$choice->format));
- fwrite ($bf,full_tag("ANSWER1",4,false,$choice->answer1));
- fwrite ($bf,full_tag("ANSWER2",4,false,$choice->answer2));
- fwrite ($bf,full_tag("ANSWER3",4,false,$choice->answer3));
- fwrite ($bf,full_tag("ANSWER4",4,false,$choice->answer4));
- fwrite ($bf,full_tag("ANSWER5",4,false,$choice->answer5));
- fwrite ($bf,full_tag("ANSWER6",4,false,$choice->answer6));
fwrite ($bf,full_tag("SHOWUNANSWERED",4,false,$choice->showunanswered));
fwrite ($bf,full_tag("TIMEOPEN",4,false,$choice->timeopen));
fwrite ($bf,full_tag("TIMECLOSE",4,false,$choice->timeclose));
fwrite ($bf,full_tag("PUBLISH",4,false,$choice->publish));
fwrite ($bf,full_tag("RELEASE",4,false,$choice->release));
fwrite ($bf,full_tag("ALLOWUPDATE",4,false,$choice->allowupdate));
fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$choice->timemodified));
- //if we've selected to backup users info, then execute backup_choice_answers
+ //if we've selected to backup users info, then execute backup_choice_responses
if ($preferences->mods["choice"]->userinfo) {
- $status = backup_choice_answers($bf,$preferences,$choice->id);
+ $status = backup_choice_responses($bf,$preferences,$choice->id);
}
+ backup_choice_answers($bf,$preferences,$choice->id);
//End mod
$status =fwrite ($bf,end_tag("MOD",3,true));
}
}
return $status;
}
- //Backup choice_answers contents (executed from choice_backup_mods)
- function backup_choice_answers ($bf,$preferences,$choice) {
+ //Backup choice_responses contents (executed from choice_backup_mods)
+ function backup_choice_responses ($bf,$preferences,$choice) {
global $CFG;
$status = true;
- $choice_answers = get_records("choice_answers","choice",$choice,"id");
+ $choice_responses = get_records("choice_responses","choice",$choice,"id");
+ //If there is submissions
+ if ($choice_responses) {
+ //Write start tag
+ $status =fwrite ($bf,start_tag("RESPONSES",4,true));
+ //Iterate over each answer
+ foreach ($choice_responses as $cho_resp) {
+ //Start answer
+ $status =fwrite ($bf,start_tag("RESPONSE",5,true));
+ //Print submission contents
+ fwrite ($bf,full_tag("ID",6,false,$cho_resp->id));
+ fwrite ($bf,full_tag("USERID",6,false,$cho_resp->userid));
+ fwrite ($bf,full_tag("CHOICE_RESPONSE",6,false,$cho_resp->answer));
+ fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$cho_resp->timemodified));
+ //End answer
+ $status =fwrite ($bf,end_tag("RESPONSE",5,true));
+ }
+ //Write end tag
+ $status =fwrite ($bf,end_tag("RESPONSES",4,true));
+ }
+ return $status;
+ }
+
+ //backup choice_answers contents (executed from choice_backup_mods)
+ function backup_choice_answers ($bf,$preferences,$choice) {
+
+ global $CFG;
+
+ $status = true;
+
+ $choice_answers = get_records("choice_answers","choice",$choice,"choice");
//If there is submissions
- if ($choice_answers) {
+ if ($choice_answers) {
//Write start tag
$status =fwrite ($bf,start_tag("ANSWERS",4,true));
//Iterate over each answer
@@ -81,7 +106,6 @@ function backup_choice_answers ($bf,$preferences,$choice) {
$status =fwrite ($bf,start_tag("ANSWER",5,true));
//Print submission contents
fwrite ($bf,full_tag("ID",6,false,$cho_ans->id));
- fwrite ($bf,full_tag("USERID",6,false,$cho_ans->userid));
fwrite ($bf,full_tag("CHOICE_ANSWER",6,false,$cho_ans->answer));
fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$cho_ans->timemodified));
//End answer
@@ -91,6 +115,11 @@ function backup_choice_answers ($bf,$preferences,$choice) {
$status =fwrite ($bf,end_tag("ANSWERS",4,true));
}
return $status;
+
+
+
+
+
}
////Return an array of info (name,value)
@@ -106,7 +135,7 @@ function choice_check_backup_mods($course,$user_data=false,$backup_unique_code)
//Now, if requested, the user_data
if ($user_data) {
$info[1][0] = get_string("responses","choice");
- if ($ids = choice_answer_ids_by_course ($course)) {
+ if ($ids = choice_response_ids_by_course ($course)) {
$info[1][1] = count($ids);
} else {
$info[1][1] = 0;
@@ -132,13 +161,13 @@ function choice_ids ($course) {
WHERE a.course = '$course'");
}
- //Returns an array of choice_answers id
- function choice_answer_ids_by_course ($course) {
+ //Returns an array of choice_responses id
+ function choice_response_ids_by_course ($course) {
global $CFG;
return get_records_sql ("SELECT s.id , s.choice
- FROM {$CFG->prefix}choice_answers s,
+ FROM {$CFG->prefix}choice_responses s,
{$CFG->prefix}choice a
WHERE a.course = '$course' AND
s.choice = a.id");
View
@@ -49,12 +49,45 @@ function choice_upgrade($oldversion) {
if ($oldversion < 2004111200){ // drop first to avoid conflicts when upgrading from 1.4+
execute_sql("ALTER TABLE {$CFG->prefix}choice DROP INDEX course;",false);
execute_sql("ALTER TABLE {$CFG->prefix}choice_answers DROP INDEX choice;",false);
- execute_sql("ALTER TABLE {$CFG->prefix}choice_answers DROP INDEX userid;",false);
-
+ execute_sql("ALTER TABLE {$CFG->prefix}choice_answers DROP INDEX userid;",false);
+
modify_database('','ALTER TABLE prefix_choice ADD INDEX course (course);');
modify_database('','ALTER TABLE prefix_choice_answers ADD INDEX choice (choice);');
modify_database('','ALTER TABLE prefix_choice_answers ADD INDEX userid (userid);');
}
+
+ if ($oldversion < 2005033000){
+ execute_sql("RENAME TABLE {$CFG->prefix}choice_answers TO {$CFG->prefix}choice_responses;");
+ execute_sql("CREATE TABLE {$CFG->prefix}choice_answers (id int(10) unsigned NOT NULL auto_increment, choice int(10) unsigned NOT NULL default '0', answer TEXT, timemodified int(10) NOT NULL default '0', PRIMARY KEY (id), UNIQUE KEY id (id), KEY choice (choice)) TYPE=MyISAM;",false);
+ execute_sql("ALTER TABLE {$CFG->prefix}choice ADD `display` TINYINT(2) UNSIGNED DEFAULT '0' NOT NULL AFTER `release`;");
+ execute_sql("ALTER TABLE {$CFG->prefix}choice_responses CHANGE answer answerid integer(10) NOT NULL default '0';");
+
+ //move old answers into new answers table.
+ $records = get_records('choice');
+ if (!empty($records)) {
+ foreach ($records as $thischoice) {
+ for ($i = 1; $i < 7; $i++) {
+ $instance = new stdClass;
+ $instance->choice = $thischoice->id;
+ $instance->answerid = $thischoice->{'answer'.$i};
+ $instance->timemodified = $thischoice->timemodified;
+ $result = insert_record('choice_answers', $instance);
+ //now fix all responses to the answers.
+ execute_sql("UPDATE {$CFG->prefix}choice_responses SET answerid={$result} WHERE choice={$thischoice->id} AND answerid={$i}");
+ }
+ }
+ }
+
+ //drop old fields
+ modify_database('','ALTER TABLE prefix_choice DROP `answer1`;');
+ modify_database('','ALTER TABLE prefix_choice DROP `answer2`;');
+ modify_database('','ALTER TABLE prefix_choice DROP `answer3`;');
+ modify_database('','ALTER TABLE prefix_choice DROP `answer4`;');
+ modify_database('','ALTER TABLE prefix_choice DROP `answer5`;');
+ modify_database('','ALTER TABLE prefix_choice DROP `answer6`;');
+
+ }
+
return true;
}
View
@@ -20,14 +20,8 @@ CREATE TABLE prefix_choice (
name varchar(255) NOT NULL default '',
text text NOT NULL,
format tinyint(2) unsigned NOT NULL default '0',
- answer1 varchar(255) NOT NULL default 'Yes',
- answer2 varchar(255) NOT NULL default 'No',
- answer3 varchar(255) default NULL,
- answer4 varchar(255) default NULL,
- answer5 varchar(255) default NULL,
- answer6 varchar(255) default NULL,
publish tinyint(2) unsigned NOT NULL default '0',
- release tinyint(2) unsigned NOT NULL default '0',
+ release tinyint(2) unsigned NOT NULL default '0', display tinyint(2) unsigned NOT NULL default '0',
allowupdate tinyint(2) unsigned NOT NULL default '0',
showunanswered tinyint(2) unsigned NOT NULL default '0',
timeopen int(10) unsigned NOT NULL default '0',
@@ -42,21 +36,38 @@ CREATE TABLE prefix_choice (
# --------------------------------------------------------
#
-# Table structure for table `choice_answers`
+# Table structure for table `choice_responses`
#
-CREATE TABLE prefix_choice_answers (
+CREATE TABLE prefix_choice_responses (
id int(10) unsigned NOT NULL auto_increment,
choice int(10) unsigned NOT NULL default '0',
userid int(10) unsigned NOT NULL default '0',
- answer tinyint(4) NOT NULL default '0',
+ answerid int(10) NOT NULL default '0',
timemodified int(10) NOT NULL default '0',
PRIMARY KEY (id),
UNIQUE KEY id (id),
KEY userid (userid),
KEY choice (choice)
) TYPE=MyISAM;
+
+# --------------------------------------------------------
+
+#
+# Table structure for table `choice_answers`
+#
+
+CREATE TABLE prefix_choice_answers (
+ id int(10) unsigned NOT NULL auto_increment,
+ choice int(10) unsigned NOT NULL default '0',
+ answer TEXT,
+ timemodified int(10) NOT NULL default '0',
+ PRIMARY KEY (id),
+ UNIQUE KEY id (id),
+ KEY choice (choice)
+) TYPE=MyISAM;
+
#
# Dumping data for table `log_display`
#
@@ -41,7 +41,38 @@ function choice_upgrade($oldversion) {
modify_database('','CREATE INDEX prefix_choice_answers_choice_idx ON prefix_choice_answers (choice);');
modify_database('','CREATE INDEX prefix_choice_answers_userid_idx ON prefix_choice_answers (userid);');
}
-
+ if ($oldversion < 2005033000){
+ execute_sql("ALTER TABLE {$CFG->prefix}choice_answers RENAME TO {$CFG->prefix}choice_responses;");
+ execute_sql("CREATE TABLE {$CFG->prefix}choice_answers (id SERIAL PRIMARY KEY, choice integer NOT NULL default '0', answer TEXT, timemodified integer NOT NULL default '0');");
+ execute_sql("CREATE INDEX {$CFG->prefix}choice_answers_choice_idx ON {$CFG->prefix}_choice_answers (choice);");
+ execute_sql("ALTER TABLE {$CFG->prefix}choice ADD `display` INTEGER DEFAULT '0' NOT NULL AFTER `release` ");
+ execute_sql("ALTER TABLE {$CFG->prefix}choice_responses RENAME answer TO answerid");
+
+ //move old answers into new answer table.
+ $records = get_records('choice');
+ if (!empty($records)) {
+ foreach ($records as $thischoice) {
+ for ($i = 1; $i < 5; $i++) {
+ $instance = new stdClass;
+ $instance->choice = $thischoice->id;
+ $instance->answerid = $thischoice->{'answer'.$i};
+ $instance->timemodified = $thischoice->timemodified;
+ $result = insert_record('choice_answers', $instance);
+ //now fix all responses to the answers.
+ execute_sql("UPDATE {$CFG->prefix}choice_responses SET answerid={$result} WHERE choice={$thischoice->id} AND answerid={$i}");
+ }
+ }
+ }
+
+ //drop old fields
+ modify_database('','ALTER TABLE prefix_choice DROP `answer1`;');
+ modify_database('','ALTER TABLE prefix_choice DROP `answer2`;');
+ modify_database('','ALTER TABLE prefix_choice DROP `answer3`;');
+ modify_database('','ALTER TABLE prefix_choice DROP `answer4`;');
+ modify_database('','ALTER TABLE prefix_choice DROP `answer5`;');
+ modify_database('','ALTER TABLE prefix_choice DROP `answer6`;');
+
+ }
return true;
}
@@ -20,15 +20,10 @@ CREATE TABLE prefix_choice (
name varchar(255) NOT NULL default '',
text text NOT NULL default '',
format integer NOT NULL default '0',
- answer1 varchar(255) NOT NULL default 'Yes',
- answer2 varchar(255) NOT NULL default 'No',
- answer3 varchar(255) default NULL,
- answer4 varchar(255) default NULL,
- answer5 varchar(255) default NULL,
- answer6 varchar(255) default NULL,
showunanswered integer NOT NULL default '0',
publish integer NOT NULL default '0',
release integer NOT NULL default '0',
+ display integer NOT NULL default '0',
allowupdate integer NOT NULL default '0',
timeopen integer NOT NULL default '0',
timeclose integer NOT NULL default '0',
@@ -43,16 +38,31 @@ CREATE INDEX prefix_choice_course_idx ON prefix_choice (course);
# Table structure for table `choice_answers`
#
-CREATE TABLE prefix_choice_answers (
+CREATE TABLE prefix_choice_responses (
id SERIAL PRIMARY KEY,
choice integer NOT NULL default '0',
userid integer NOT NULL default '0',
- answer integer NOT NULL default '0',
+ answerid integer NOT NULL default '0',
+ timemodified integer NOT NULL default '0'
+);
+
+CREATE INDEX prefix_choice_responses_choice_idx ON prefix_choice_responses (choice);
+CREATE INDEX prefix_choice_responses_userid_idx ON prefix_choice_responses (userid);
+
+# --------------------------------------------------------
+
+#
+# Table structure for table `choice_answers`
+#
+
+CREATE TABLE prefix_choice_answers (
+ id SERIAL PRIMARY KEY,
+ choice integer NOT NULL default '0',
+ answer TEXT,
timemodified integer NOT NULL default '0'
);
CREATE INDEX prefix_choice_answers_choice_idx ON prefix_choice_answers (choice);
-CREATE INDEX prefix_choice_answers_userid_idx ON prefix_choice_answers (userid);
#
# Dumping data for table `log_display`
View
@@ -24,8 +24,8 @@
notice("There are no choices", "../../course/view.php?id=$course->id");
}
- if ( isset($USER->id) and $allanswers = get_records("choice_answers", "userid", $USER->id)) {
- foreach ($allanswers as $aa) {
+ if ( isset($USER->id) and $allresponses = get_records("choice_responses", "userid", $USER->id)) {
+ foreach ($allresponses as $aa) {
$answers[$aa->choice] = $aa;
}
@@ -56,7 +56,7 @@
$answer = "";
}
if (!empty($answer->answer)) {
- $aa = format_string(choice_get_answer($choice, $answer->answer));
+ $aa = format_string(choice_get_answer($choice, $answer->answerid));
} else {
$aa = "";
}
Oops, something went wrong.

0 comments on commit 6fd87e3

Please sign in to comment.