Improved coice mod, allows an unlimited number of choices.

This commit is contained in:
danmarsden 2005-03-29 23:46:17 +00:00
parent 67ac95e83b
commit 6fd87e3bc7
12 changed files with 426 additions and 201 deletions

View File

@ -9,7 +9,7 @@
// |
// |
// |
// choice_answers
// choice_responses
// (UL,pk->id, fk->choice)
//
// Meaning: pk->primary key field of the table
@ -39,12 +39,6 @@
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));
@ -52,10 +46,11 @@
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));
}
@ -63,14 +58,44 @@
return $status;
}
//Backup choice_answers contents (executed from choice_backup_mods)
//Backup choice_responses contents (executed from choice_backup_mods)
function backup_choice_responses ($bf,$preferences,$choice) {
global $CFG;
$status = true;
$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,"id");
$choice_answers = get_records("choice_answers","choice",$choice,"choice");
//If there is submissions
if ($choice_answers) {
//Write start tag
@ -81,7 +106,6 @@
$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 @@
$status =fwrite ($bf,end_tag("ANSWERS",4,true));
}
return $status;
}
////Return an array of info (name,value)
@ -106,7 +135,7 @@
//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 @@
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 File

@ -56,6 +56,39 @@ function choice_upgrade($oldversion) {
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 File

@ -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',
@ -39,6 +33,25 @@ CREATE TABLE prefix_choice (
) TYPE=MyISAM COMMENT='Available choices are stored here.';
# --------------------------------------------------------
#
# Table structure for table `choice_responses`
#
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',
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;
# --------------------------------------------------------
#
@ -48,12 +61,10 @@ CREATE TABLE prefix_choice (
CREATE TABLE prefix_choice_answers (
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',
answer TEXT,
timemodified int(10) NOT NULL default '0',
PRIMARY KEY (id),
UNIQUE KEY id (id),
KEY userid (userid),
KEY choice (choice)
) TYPE=MyISAM;

View File

@ -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;
}

View File

@ -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 File

@ -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 = "";
}

View File

@ -12,6 +12,9 @@ define('CHOICE_RELEASE_AFTER_ANSWER', '1');
define('CHOICE_RELEASE_AFTER_CLOSE', '2');
define('CHOICE_RELEASE_ALWAYS', '3');
define('CHOICE_DISPLAY_HORIZONTAL', '0');
define('CHOICE_DISPLAY_VERTICAL', '1');
$CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'),
CHOICE_PUBLISH_NAMES => get_string('publishnames', 'choice'));
@ -20,11 +23,13 @@ $CHOICE_RELEASE = array (CHOICE_RELEASE_NOT => get_string('publishnot',
CHOICE_RELEASE_AFTER_CLOSE => get_string('publishafterclose', 'choice'),
CHOICE_RELEASE_ALWAYS => get_string('publishalways', 'choice'));
$CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL => get_string('displayhorizontal', 'choice'),
CHOICE_DISPLAY_VERTICAL => get_string('displayvertical','choice'));
/// Standard functions /////////////////////////////////////////////////////////
function choice_user_outline($course, $user, $mod, $choice) {
if ($current = get_record("choice_answers", "choice", $choice->id, "userid", $user->id)) {
if ($current = get_record("choice_responses", "choice", $choice->id, "userid", $user->id)) {
$result->info = "'".choice_get_answer($choice, $current->answer)."'";
$result->time = $current->timemodified;
return $result;
@ -34,7 +39,7 @@ function choice_user_outline($course, $user, $mod, $choice) {
function choice_user_complete($course, $user, $mod, $choice) {
if ($current = get_record("choice_answers", "choice", $choice->id, "userid", $user->id)) {
if ($current = get_record("choice_responses", "choice", $choice->id, "userid", $user->id)) {
$result->info = "'".choice_get_answer($choice, $current->answer)."'";
$result->time = $current->timemodified;
echo get_string("answered", "choice").": $result->info , last updated ".userdate($result->time);
@ -62,7 +67,25 @@ function choice_add_instance($choice) {
$choice->timeclose = 0;
}
return insert_record("choice", $choice);
//insert answers
$result = insert_record("choice", $choice);
if ($result) {
for ($i = 1; $i < 7; $i++) {
if (!empty($choice->{'newanswer'.$i})) {
$choiceanswers->answer = $choice->{'newanswer'.$i};
$choiceanswers->choice = $result;
$choiceanswers->timemodified = time();
insert_record("choice_answers", $choiceanswers);
} else {
break;
}
}
}
if ($choice->addmorechoices > 0) { //make sure the page is reloaded if the user wants to add more choices.
redirect('mod.php?update='.$choice->coursemodule.'&return=true&addmore='.$choice->addmorechoices.'&sesskey='.$choice->sesskey);
} else {
return $result;
}
}
@ -85,9 +108,46 @@ function choice_update_instance($choice) {
$choice->timeclose = 0;
}
//update answers
$i = 0;
foreach ($choice as $current=>$value) {
if (strstr($current, "choiceanswer")) {
//this val is old, so update_record
if (!empty($value)) {
$choiceanswers->id = substr($current, 14); //get the ID of the answer that needs to be updated.
$choiceanswers->answer = $value;
$choiceanswers->choice = $choice->id;
$choiceanswers->timemodified = time();
update_record("choice_answers", $choiceanswers);
}
} else if (strstr($current, "newanswer")) {
//this val is new, so insert_record
if (!empty($value)) {
$choiceanswers->id = "";
$choiceanswers->answer = $value;
$choiceanswers->choice = $choice->id;
$choiceanswers->timemodified = time();
insert_record("choice_answers", $choiceanswers);
}
}
}
// global $db; $db->debug=true;
return update_record("choice", $choice);
$result = update_record("choice", $choice);
if ($choice->addmorechoices > 0) { //make sure the page is reloaded if the user wants to add more choices.
redirect('mod.php?update='.$choice->coursemodule.'&return=true&addmore='.$choice->addmorechoices.'&sesskey='.$choice->sesskey);
} else {
return $result;
}
}
@ -102,7 +162,7 @@ function choice_delete_instance($id) {
$result = true;
if (! delete_records("choice_answers", "choice", "$choice->id")) {
if (! delete_records("choice_responses", "choice", "$choice->id")) {
$result = false;
}
@ -110,19 +170,23 @@ function choice_delete_instance($id) {
$result = false;
}
if (! delete_records("choice_answers", "choice", "$choice->id")) {
$result = false;
}
return $result;
}
function choice_get_participants($choiceid) {
//Returns the users with data in one choice
//(users with records in choice_answers, students)
//(users with records in choice_responses, students)
global $CFG;
//Get students
$students = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}choice_answers c
{$CFG->prefix}choice_responses c
WHERE c.choice = '$choiceid' and
u.id = c.userid");
@ -133,38 +197,29 @@ function choice_get_participants($choiceid) {
function choice_get_answer($choice, $code) {
// Returns text string which is the answer that matches the code
switch ($code) {
case 1:
return "$choice->answer1";
case 2:
return "$choice->answer2";
case 3:
return "$choice->answer3";
case 4:
return "$choice->answer4";
case 5:
return "$choice->answer5";
case 6:
return "$choice->answer6";
default:
if ($result = get_record("choice_answers", "id", $code)) {
return $result->answer;
} else {
return get_string("notanswered", "choice");
}
}
function choice_get_choice($choiceid) {
// Gets a full choice record
// Gets a full choice record
if ($choice = get_record("choice", "id", $choiceid)) {
$choice->answer[1] = $choice->answer1;
$choice->answer[2] = $choice->answer2;
$choice->answer[3] = $choice->answer3;
$choice->answer[4] = $choice->answer4;
$choice->answer[5] = $choice->answer5;
$choice->answer[6] = $choice->answer6;
if ($choices = get_records("choice_answers", "choice", $choiceid, "id")) {
$inti = 1;
foreach ($choices as $aa) {
$choice->answer[$aa->id] = $aa->answer;
$choice->answerid[$aa->id] = $aa->id;
$inti = $inti +1;
}
return $choice;
} else {
return false;
}
}
}
?>

View File

@ -8,24 +8,6 @@
if (empty($form->format)) {
$form->format = 0;
}
if (empty($form->answer1)) {
$form->answer1 = get_string("yes");
}
if (empty($form->answer2)) {
$form->answer2 = get_string("no");
}
if (empty($form->answer3)) {
$form->answer3 = "";
}
if (empty($form->answer4)) {
$form->answer4 = "";
}
if (empty($form->answer5)) {
$form->answer5 = "";
}
if (empty($form->answer6)) {
$form->answer6 = "";
}
if (empty($form->timeopen)) {
$form->timeopen = "";
$form->timerestrict = 0;
@ -47,6 +29,9 @@
if (empty($form->showunanswered)) {
$form->showunanswered = 0;
}
if (!(empty($form->addmorechoices))) {
error("not submitted!");
}
?>
<form name="form" method="post" action="mod.php">
@ -54,7 +39,7 @@
<table cellpadding="5">
<tr valign="top">
<td align="right"><b><?php print_string("choicename","choice") ?>:</b></td>
<td align="right"><strong><?php print_string("choicename","choice") ?>:</strong></td>
<td>
<input type="text" name="name" size="30" alt="<?php print_string("choicename","choice") ?>" value="<?php p($form->name) ?>" />
</td>
@ -96,48 +81,37 @@
</td>
</tr>
<tr valign="top">
<td align="right"><b><?php print_string("choice","choice","1") ?>:</b></td>
<td>
<input type="text" name="answer1" size="60" alt="<?php print_string("choice","choice","1") ?>" value="<?php p($form->answer1) ?>" />
<?php helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
</td>
</tr>
<?php
$choiceqs = get_records_menu("choice_answers","choice", $form->instance,"id","id ,answer");
foreach ($choiceqs as $current=>$answer) {
?>
<tr valign="top">
<td align="right"><b><?php print_string("choice","choice","2") ?>:</b></td>
<tr valign=top>
<td align=right><p><strong><?php print_string("choice","choice") ?>:</strong></p></td>
<td>
<input type="text" name="answer2" size="60" alt="<?php print_string("choice","choice","2") ?>" value="<?php p($form->answer2) ?>" />
<input type="text" name="choiceanswer<?php p($current) ?>" size=60 value="<?php p($answer) ?>" ID="Textq<?php p($current) ?>">
<?php helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
</td>
</tr>
<tr valign="top">
<td align="right"><b><?php print_string("choice","choice","3") ?>:</b></td>
<?php
}
for ($i = 1; $i < ($addmore + 5); $i++) {
?>
<tr valign=top>
<td align="right"><p><strong><?php print_string("choice","choice") ?>:</strong></p></td>
<td>
<input type="text" name="answer3" size="60" alt="<?php print_string("choice","choice","3") ?>" value="<?php p($form->answer3) ?>" />
<input type="text" name="newanswer<?php p($i) ?>" size=60 value="" ID="Textqm<?php p($current) ?>">
<?php helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
</td>
</tr>
<?php
}
?>
<tr valign="top">
<td align="right"><b><?php print_string("choice","choice","4") ?>:</b></td>
<td align="right"><p><strong><?php print_string("addmorechoices","choice") ?>:</strong></p></td>
<td>
<input type="text" name="answer4" size="60" alt="<?php print_string("choice","choice","4") ?>" value="<?php p($form->answer4) ?>" />
<?php helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
</td>
</tr>
<tr valign="top">
<td align="right"><b><?php print_string("choice","choice","5") ?>:</b></td>
<td>
<input type="text" name="answer5" size="60" alt="<?php print_string("choice","choice","5") ?>" value="<?php p($form->answer5) ?>" />
<?php helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
</td>
</tr>
<tr valign="top">
<td align="right"><b><?php print_string("choice","choice","6") ?>:</b></td>
<td>
<input type="text" name="answer6" size="60" alt="<?php print_string("choice","choice","6") ?>" value="<?php p($form->answer6) ?>" />
<?php helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
<input name="addmorechoices" type="text" value="0" size="3" />
<input type="submit" value="add"/>
</td>
</tr>
@ -160,7 +134,7 @@
<table>
<tr>
<td align="right"><b><?php print_string("choiceopen", "choice") ?>:</b></td>
<td align="right"><strong><?php print_string("choiceopen", "choice") ?>:</strong></td>
<td>
<?php
@ -179,7 +153,7 @@
</td>
</tr>
<tr>
<td align="right"><b><?php print_string("choiceclose", "choice") ?>:</b></td>
<td align="right"><strong><?php print_string("choiceclose", "choice") ?>:</strong></td>
<td>
<?php
@ -212,7 +186,18 @@
</tr>
<tr valign="top">
<td align="right"><b><?php print_string("publish","choice") ?>:</b></td>
<td align="right"><strong><?php print_string("displaymode","choice") ?>:</strong></td>
<td>
<?php
require_once("$CFG->dirroot/mod/choice/lib.php");
choose_from_menu($CHOICE_DISPLAY, "display", "$form->display", "");
?>
<br />
</td>
</tr>
<tr valign="top">
<td align="right"><strong><?php print_string("publish","choice") ?>:</strong></td>
<td>
<?php
require_once("$CFG->dirroot/mod/choice/lib.php");
@ -223,7 +208,7 @@
</tr>
<tr valign="top">
<td align="right"><b><?php print_string("privacy","choice") ?>:</b></td>
<td align="right"><strong><?php print_string("privacy","choice") ?>:</strong></td>
<td>
<?php
require_once("$CFG->dirroot/mod/choice/lib.php");
@ -235,7 +220,7 @@
<tr valign="top">
<td align="right"><b><?php print_string("allowupdate","choice") ?>:</b></td>
<td align="right"><strong><?php print_string("allowupdate","choice") ?>:</strong></td>
<td>
<?php
$options[0] = get_string("no");
@ -248,7 +233,7 @@
<tr valign="top">
<td align="right"><b><?php print_string("showunanswered","choice") ?>:</b></td>
<td align="right"><strong><?php print_string("showunanswered","choice") ?>:</strong></td>
<td>
<?php
$options[0] = get_string("no");

View File

@ -52,8 +52,8 @@
exit;
}
if ( $allanswers = get_records("choice_answers", "choice", $choice->id)) {
foreach ($allanswers as $aa) {
if ( $allresponses = get_records("choice_responses", "choice", $choice->id)) {
foreach ($allresponses as $aa) {
$answers[$aa->userid] = $aa;
}
} else {
@ -69,9 +69,9 @@
if (!empty($answers[$user->id])) {
$answer = $answers[$user->id];
} else {
$answer->answer = 0;
$answer->answerid = 0;
}
$useranswer[(int)$answer->answer][] = $user;
$useranswer[(int)$answer->answerid][] = $user;
}
foreach ($choice->answer as $key => $answer) {
if (!$choice->answer[$key]) {

View File

@ -9,7 +9,7 @@
// |
// |
// |
// choice_answers
// choice_responses
// (UL,pk->id, fk->choice)
//
// Meaning: pk->primary key field of the table
@ -43,17 +43,12 @@
$choice->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
$choice->text = backup_todb($info['MOD']['#']['TEXT']['0']['#']);
$choice->format = backup_todb($info['MOD']['#']['FORMAT']['0']['#']);
$choice->answer1 = backup_todb($info['MOD']['#']['ANSWER1']['0']['#']);
$choice->answer2 = backup_todb($info['MOD']['#']['ANSWER2']['0']['#']);
$choice->answer3 = backup_todb($info['MOD']['#']['ANSWER3']['0']['#']);
$choice->answer4 = backup_todb($info['MOD']['#']['ANSWER4']['0']['#']);
$choice->answer5 = backup_todb($info['MOD']['#']['ANSWER5']['0']['#']);
$choice->answer6 = backup_todb($info['MOD']['#']['ANSWER6']['0']['#']);
$choice->showunanswered = backup_todb($info['MOD']['#']['SHOWUNANSWERED']['0']['#']);
$choice->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
$choice->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
$choice->publish = backup_todb($info['MOD']['#']['PUBLISH']['0']['#']);
$choice->release = backup_todb($info['MOD']['#']['RELEASE']['0']['#']);
$choice->display = backup_todb($info['MOD']['#']['DISPLAY']['0']['#']);
$choice->allowupdate = backup_todb($info['MOD']['#']['ALLOWUPDATE']['0']['#']);
$choice->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
@ -77,42 +72,85 @@
//The structure is equal to the db, so insert the choice
$newid = insert_record ("choice",$choice);
//Do some output
echo "<li>".get_string("modulename","choice")." \"".format_string(stripslashes($choice->name),true)."\"</li>";
backup_flush(300);
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,$mod->modtype,
$mod->id, $newid);
//Now check if want to restore user data and do it.
if ($restore->mods['choice']->userinfo) {
//Restore choice_answers
$status = choice_answers_restore_mods ($newid,$info,$restore);
//Now restore the answers for this choice
//check to see how answers are stored in the table - if answer1 - answer6 exist, this is an old version of choice
if (isset($info['MOD']['#']['ANSWER1']['0']['#']) || isset($info['MOD']['#']['ANSWER2']['0']['#']) || isset($info['MOD']['#']['ANSWER3']['0']['#']) || isset($info['MOD']['#']['ANSWER4']['0']['#']) || isset($info['MOD']['#']['ANSWER5']['0']['#']) || isset($info['MOD']['#']['ANSWER6']['0']['#']) ) {
$answers[1] = backup_todb($info['MOD']['#']['ANSWER1']['0']['#']);
$answers[2] = backup_todb($info['MOD']['#']['ANSWER2']['0']['#']);
$answers[3] = backup_todb($info['MOD']['#']['ANSWER3']['0']['#']);
$answers[4] = backup_todb($info['MOD']['#']['ANSWER4']['0']['#']);
$answers[5] = backup_todb($info['MOD']['#']['ANSWER5']['0']['#']);
$answers[6] = backup_todb($info['MOD']['#']['ANSWER6']['0']['#']);
for($i = 1; $i < 7; $i++) { //insert new answers into db.
if (!empty($answers[$i])) { //make sure this answer has something in it!
$answer->choice = $newid;
$answer->answer = $answers[$i];
$answer->timemodified = $info['MOD']['#']['TIMEMODIFIED']['0']['#'];
$ansid[$i] = insert_record ("choice_answers",$answer);
$status = true;
}
} else {
$status = false;
}
} else {
$status = false;
}
//now restore the responses for this choice.
if ($restore->mods['choice']->userinfo) {
//Restore choice_responses
$status = choice_responses_restore_mods($newid,$info,$restore,$ansid,"1.4");
}
} else {
//this is a normal backup file
$answers = $info['MOD']['#']['ANSWERS']['0']['#']['ANSWER'];
for($i = 0; $i < sizeof($answers); $i++) {
$sub_info = $answers[$i];
$answer->choice = $newid;
$answer->answer = backup_todb($sub_info['#']['CHOICE_ANSWER']['0']['#']);
$answer->timemodified = backup_todb($sub_info['#']['TIMEMODIFIED']['0']['#']);
$ansid[$sub_info['#']['ID']['0']['#']] = insert_record ("choice_answers",$answer);
$status = true;
}
//now restore the responses for this choice.
if ($restore->mods['choice']->userinfo) {
//Restore choice_responses
$status = choice_responses_restore_mods($newid,$info,$restore,$ansid);
}
}
} else {
$status = false;
}
//Do some output
echo "<li>".get_string("modulename","choice")." \"".format_string(stripslashes($choice->name),true)."\"</li>";
backup_flush(300);
} else {
$status = false;
}
return $status;
}
//This function restores the choice_answers
function choice_answers_restore_mods($choice_id,$info,$restore) {
//This function restores the choice_responses
function choice_responses_restore_mods($choice_id,$info,$restore,$answerids,$version) {
global $CFG;
$status = true;
//Get the answers array
$answers = $info['MOD']['#']['ANSWERS']['0']['#']['ANSWER'];
//Get the responses array
if ($version == "1.4") { //this version stores responses differently.
$responses = $info['MOD']['#']['ANSWERS']['0']['#']['ANSWER'];
} else {
$responses = $info['MOD']['#']['RESPONSES']['0']['#']['RESPONSE'];
}
//Iterate over answers
for($i = 0; $i < sizeof($answers); $i++) {
$sub_info = $answers[$i];
//Iterate over responses
for($i = 0; $i < sizeof($responses); $i++) {
$sub_info = $responses[$i];
//traverse_xmlize($sub_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
@ -121,20 +159,28 @@
$oldid = backup_todb($sub_info['#']['ID']['0']['#']);
$olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
//Now, build the CHOICE_ANSWERS record structure
$answer->choice = $choice_id;
$answer->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
$answer->answer = backup_todb($sub_info['#']['CHOICE_ANSWER']['0']['#']);
$answer->timemodified = backup_todb($sub_info['#']['TIMEMODIFIED']['0']['#']);
//Now, build the CHOICE_RESPONSES record structure
$response->choice = $choice_id;
$response->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
$response->timemodified = backup_todb($sub_info['#']['TIMEMODIFIED']['0']['#']);
//We have to recode the userid field
$user = backup_getid($restore->backup_unique_code,"user",$answer->userid);
if ($user) {
$answer->userid = $user->new_id;
//we have to recode the answer field
if ($version == "1.4") {
//this is an old style choice.
$response->answerid = $answerids[backup_todb($sub_info['#']['CHOICE_ANSWER']['0']['#'])];
//
} else {
$response->answerid = $answerids[backup_todb($sub_info['#']['CHOICE_RESPONSE']['0']['#'])];
}
//The structure is equal to the db, so insert the choice_answers
$newid = insert_record ("choice_answers",$answer);
//We have to recode the userid field
$user = backup_getid($restore->backup_unique_code,"user",$response->userid);
if ($user) {
$response->userid = $user->new_id;
}
//The structure is equal to the db, so insert the choice_responses
$newid = insert_record ("choice_responses",$response);
//Do some output
if (($i+1) % 50 == 0) {
@ -147,7 +193,7 @@
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,"choice_answers",$oldid,
backup_putid($restore->backup_unique_code,"choice_responses",$oldid,
$newid);
} else {
$status = false;

View File

@ -5,7 +5,7 @@
// This fragment is called by /admin/index.php
////////////////////////////////////////////////////////////////////////////////
$module->version = 2005021600;
$module->version = 2005033000;
$module->requires = 2005021600; // Requires this Moodle version
$module->cron = 0;

View File

@ -22,7 +22,7 @@
for ($i=1; $i <= $CHOICE_MAX_NUMBER; $i++) {
$answerchecked[$i] = '';
}
if (isset($USER->id) and $current = get_record("choice_answers", "choice", $choice->id, "userid", $USER->id)) {
if (isset($USER->id) and $current = get_record("choice_responses", "choice", $choice->id, "userid", $USER->id)) {
$answerchecked[$current->answer] = 'CHECKED';
} else {
$current = false;
@ -37,18 +37,18 @@
} else {
if ($current) {
$newanswer = $current;
$newanswer->answer = $form->answer;
$newanswer->answerid = $form->answer;
$newanswer->timemodified = $timenow;
if (! update_record("choice_answers", $newanswer)) {
if (! update_record("choice_responses", $newanswer)) {
error("Could not update your choice");
}
add_to_log($course->id, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id);
} else {
$newanswer->choice = $choice->id;
$newanswer->userid = $USER->id;
$newanswer->answer = $form->answer;
$newanswer->answerid = $form->answer;
$newanswer->timemodified = $timenow;
if (! insert_record("choice_answers", $newanswer)) {
if (! insert_record("choice_responses", $newanswer)) {
error("Could not save your choice");
}
add_to_log($course->id, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
@ -75,7 +75,7 @@
}
if (isteacher($course->id)) {
if ( $allanswers = get_records("choice_answers", "choice", $choice->id)) {
if ( $allanswers = get_records("choice_responses", "choice", $choice->id)) {
$responsecount = count($allanswers);
} else {
$responsecount = 0;
@ -102,18 +102,43 @@
// They haven't made their choice yet or updates allowed and choice is open
echo "<form name=\"form\" method=\"post\" action=\"view.php\">";
switch ($choice->display) {
case "0": //horizontal display mode.
echo "<table cellpadding=\"20\" cellspacing=\"20\" align=\"center\"><tr>";
foreach ($choice->answer as $key => $answer) {
if ($answer) {
echo "<td align=\"center\">";
echo "<input type=\"radio\" name=\"answer\" value=\"$key\" ".$answerchecked[$key]." alt=\"".s(strip_tags($answer))."\" />";
echo format_string($answer);
echo "<input type=\"radio\" name=\"answer\" value=\"".$choice->answerid[$key]."\" ".$answerchecked[$key]." alt=\"$answer\" />";
echo format_text($answer);
echo "</td>";
}
}
echo "</tr>";
break;
case "1": //vertical display mode
echo "<table cellpadding=\"10\" cellspacing=\"10\" align=\"center\">";
if ( $choice->release == CHOICE_RELEASE_ALWAYS OR ( $choice->release == CHOICE_RELEASE_AFTER_ANSWER and $current )) {
echo "<tr><td></td><td align=\"center\"><strong>".get_string("responses", "choice")."</strong></td></tr>";
}
echo "</tr></table>";
foreach ($choice->answer as $key => $answer) {
if ($answer) {
echo "<tr><td align=\"left\">";
echo "<input type=\"radio\" name=\"answer\" value=\"".$choice->answerid[$key]."\" ".$answerchecked[$key]." alt=\"$answer\" />".format_text($answer, FORMAT_PLAIN);
echo "</td>";
if ( $choice->release == CHOICE_RELEASE_ALWAYS OR ( $choice->release == CHOICE_RELEASE_AFTER_ANSWER and $current )) {
echo "<td align=\"center\">";
echo count_records("choice_responses", "answer", $choice->answerid[$key]);
echo "</td>";
}
echo "</tr>";
}
}
break;
}
echo "</table>";
echo "<center>";
echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
if (isstudent($course->id) or isteacher($course->id, 0, false)) {
@ -128,7 +153,7 @@
// print the results
// print the results at the bottom of the screen
if ( $choice->release == CHOICE_RELEASE_ALWAYS or
( $choice->release == CHOICE_RELEASE_AFTER_ANSWER and $current ) or
@ -148,8 +173,8 @@
exit;
}
if ( $allanswers = get_records("choice_answers", "choice", $choice->id)) {
foreach ($allanswers as $aa) {
if ( $allresponses = get_records("choice_responses", "choice", $choice->id)) {
foreach ($allresponses as $aa) {
$answers[$aa->userid] = $aa;
}
} else {
@ -164,7 +189,7 @@
foreach ($users as $user) {
if (!empty($user->id) and !empty($answers[$user->id])) {
$answer = $answers[$user->id];
$useranswer[(int)$answer->answer][] = $user;
$useranswer[(int)$answer->answerid][] = $user;
} else {
$useranswer[0][] = $user;
}