<?
/*-------------------------------------------------------------------
question_copy.php Ver. $Revision: 1.2 $ $Date: 2001/07/27
01:43:19 $
*
Copyright (C) 2000 Hong Kong Baptist University
*
This file is part of the Quadrille system.
* This program is free software; it is distributed under the terms of
the GNU General Public License, either version 2, or (at your option)
* any later version.
* This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*
---------------------------------------------------------------------
*/
?>
<?
require "global_inc.php";
include "config_inc.php";
require "template.php";
require "question_function_inc.php";
include $language_pack;
include "question_stu_{$lang_name}.inc";
/*************/
/* Main Part */
/*************/
check_expire();
$role = check_role();
$temp_cookie[user_uid] = $cookie[user_uid];
$temp_cookie[password] = $cookie[password];
$temp_cookie[db_index]=$cookie[db_index];
$temp_cookie[course_uid]=$cookie[course_uid];
$temp_cookie[unit_uid]=$cookie[unit_uid];
set_cookie($temp_cookie);
$pattern=",";
$question_uid_array=split($pattern,$selected_q_list);
insert_header();
$content = question_copy($question_uid_array,$selected_q_list);
$java = get_file("javascript/question_move_javascript.js");
$java = ereg_replace("<BEGIN>SELECT_UNIT_FIRST<END>",$SELECT_UNIT_FIRST,$java);
// print $java;
$currentlocation =insert_current_location($cookie["course_uid"],$cookie["unit_uid"],'');
$html = get_template($template, $template_dir);
$html=ereg_replace("<BEGIN>JAVASCRIPT<END>",$java,$html);
$html=ereg_replace("<BEGIN>TITLE<END>",$PAGE_TITLE["QUESTION_COPY"],$html);
$html=ereg_replace("<BEGIN>CURRENTLOCATION<END>",$currentlocation,$html);
$html=ereg_replace("<BEGIN>CONTENT<END>",$content,$html);
$html=ereg_replace("<BEGIN>COPYRIGHT<END>",$COPYRIGHT,$html);
print $html;
/******************/
/* function Part */
/******************/
function question_copy($question_uid_array,$selected_q_list)
{ include "config_inc.php";
include $language_pack;
require "question_stu_{$lang_name}.inc";
global $external_info;
global $u_uid;
global $modify_now;
global $level;
global $question_type;
global $author;
global $to;
global $from;
$content="";
$question_in_paper_array = array();
if ( !isset($u_uid) ) {
//Retrieve the question to be moved
$list_query="select * from QUESTION where UID in ($selected_q_list) order by LEVEL, QTYPE_UID";
$list_result = mysql_query($list_query);
if (!empty($list_result)) {
$content.="<form name=\"form1\" method=\"post\">";
$content.="\n\t<table width=\"\" border=\"0\" cellspacing=\"4\" cellpadding=\"4\">";
$content.="\n\t\t<tr>";
$content.="\n\t\t\t<td nowrap class=\"tableheading\">$QUESTION_TYPE_NAME</td>";
$content.="\n\t\t\t<td nowrap class=\"tableheading\">$QUESTION_LEVEL</td>";
$content.="\n\t\t\t<td nowrap class=\"tableheading\">$QUESTION_AUTHOR</td>";
$content.="\n\t\t\t<td nowrap class=\"tableheading\">$QUESTION_DATE</td>";
$content.="\n\t\t\t<td nowrap class=\"tableheading\">$QUESTION_DESCRIPTION</td>";
$content.="\n\t\t</tr>";
$i=0;
while ($row1 = mysql_fetch_array($list_result)) {
$questiontype = $row1["QTYPE_UID"];
$questionuid = $row1["UID"];
$questionauthor = $row1["USER_UID"];
$questionlevel = $row1["LEVEL"];
$questiondate = $row1["LAST_MODIFY_DATE"];
$questiondescription = $row1["Q_DESC"];
if($i%2==0)
$content.="\n\t\t<tr class=\"tablerowodd\">";
else
$content.="\n\t\t<tr class=\"tableroweven\">";
$content.=display_cell($QUESTION_TYPE[$questiontype],$font_red);
$content.="\n\t\t\t";
$content.=display_cell($LEVEL[$questionlevel],$font_red);
$content.="\n\t\t\t";
$content.=display_cell(get_name("USER",$questionauthor),$font_red);
$content.="\n\t\t\t";
$content.=display_cell($questiondate,$font_red);
$content.="\n\t\t\t";
$content.=display_cell($questiondescription,$font_red);
$content.="\n\t\t\t";
$content.="\n\t\t</tr>";
$i++;
}
$content.="\n\t</table>";
$content.="\n\n$SELECT_QUESTION_A ".mysql_num_rows($list_result)."$TOTAL_QUESTION_B";
$content.="\n\t<input type=\"hidden\" name=\"selected_q_list\" value=\"$selected_q_list\">";
$content.="\n\t<input type=\"hidden\" name=\"level\" value=\"$level\">";
$content.="\n\t<input type=\"hidden\" name=\"question_type\" value=\"$question_type\">";
$content.="\n\t<input type=\"hidden\" name=\"author\" value=\"$author\">";
$content.="\n\t<input type=\"hidden\" name=\"from\" value=\"$from\">";
$content.="\n\t<input type=\"hidden\" name=\"to\" value=\"$to\">";
}
$content.="\n\t<br><br>$QUESTION_COPY_TO";
//List the Unit in the course that the user teaches
$content.="\n\n";
$content.="\n\t<table width=\"\" border=\"0\" cellspacing=\"4\" cellpadding=\"5\">";
$course_list = find_course();
for ($k=0; $k < count($course_list); $k++)
{ $course_id = $course_list[$k];
$course_name = get_name("COURSE","$course_id");
$subquery = "SELECT UID from UNIT WHERE COURSE_UID = $course_id "; //AND UID <> $cookie[unit_uid]
$subquery_result = mysql_query ($subquery);
if (mysql_num_rows ($subquery_result) >0)
{ $content.="\n\t\t<tr>";
$content.="\n\t\t\t<td nowrap class=\"tableheading\"> </td>";
$content.="\n\t\t\t<td nowrap class=\"tableheading\">$course_name</td>";
$content.="\n\t\t</tr>";
$i=0;
while ($sub_row = mysql_fetch_array($subquery_result))
{
$u_uid = $sub_row["UID"];
$unit_name = get_name("UNIT",$u_uid);
if($i%2==0)
$content.="\n\t\t<tr class=\"tablerowodd\">";
else
$content.="\n\t\t<tr class=\"tableroweven\">";
$content.="\n\t\t\t<td><input type=\"radio\" name=\"u_uid\" value=\"$u_uid\"></td>";
$content.="\n\t\t\t";
$content.="\n\t\t\t";
$content.=display_cell($unit_name,'');
$content.="\n\t\t</tr>";
$i++;
}
}
}// End for
$content.="\n\t</table>";
$content.="\n\t<input type=\"hidden\" name=\"selected_q_list\" value=\"$selected_q_list\">";
$content.="\n\t<input type=\"hidden\" name=\"level\" value=\"$level\">";
$content.="\n\t<input type=\"hidden\" name=\"question_type\" value=\"$question_type\">";
$content.="\n\t<input type=\"hidden\" name=\"author\" value=\"$author\">";
$content.="\n\t<input type=\"hidden\" name=\"from\" value=\"$from\">";
$content.="\n\t<input type=\"hidden\" name=\"to\" value=\"$to\">";
$content.="<input type=hidden name=updatedb value=1>\n";
$content.="<br><input type=button value=\" $ENTER \" onclick=\"javascript:copy_question()\">\n";
$content.="<input type=button value=\" $BACK \" onclick=\"javascript:question_list()\">\n";
$content.="\n\t</form>";
}
else {
$list_query="select * from QUESTION where UID in ($selected_q_list) order by LEVEL, QTYPE_UID";
$list_result = mysql_query($list_query);
if (!empty($list_result))
{
while ($row1 = mysql_fetch_array($list_result))
{
$questiontype = $row1["QTYPE_UID"];
$questionid = $row1["UID"];
$questionauthor = $row1["USER_UID"];
$questionlevel = $row1["LEVEL"];
$questiondate = $row1["LAST_MODIFY_DATE"];
$questiondescription = $row1["Q_DESC"];
$questionlang = $row1["Q_LANG"];
//Insert into the table QUESTION first
$copy_query = "INSERT INTO QUESTION (UID, QTYPE_UID, USER_UID, UNIT_UID, LEVEL, LAST_MODIFY_DATE, Q_DESC, Q_LANG) VALUES ('', $questiontype, $questionauthor, $u_uid, '$questionlevel', sysdate(), '$questiondescription', '$questionlang')";
$copy_result = mysql_query ($copy_query);
$max_query="select MAX(UID) from QUESTION";
$max_result=mysql_query($max_query);
if(!empty($max_result)){
$max_row=mysql_fetch_array($max_result);
$current_id=$max_row["MAX(UID)"];
}
$current_id = mysql_insert_id();
//Map the corr. TAB_NAME
$map_query = "SELECT TAB_NAME from QUESTION_TYPE where UID = $questiontype";
$map_result = mysql_query($map_query);
$map_row = mysql_fetch_array($map_result);
$tabname = $map_row["TAB_NAME"];
//Retrieve the data from corr. table
$final_query = "SELECT * from $tabname where Q_UID = $questionid";
$final_result = mysql_query($final_query);
//$final_row = mysql_fetch_array($final_result);
$field_num = mysql_num_fields($final_result);
$row_field = mysql_fetch_row($final_result);
//Query the course the question belongs to
$source_query = "SELECT u.COURSE_UID , u.UID from COURSE c, UNIT u, QUESTION q where q.UID = $questionid AND q.UNIT_UID = u.UID AND u.COURSE_UID = c.UID";
$source_result = mysql_query($source_query);
$source_row = mysql_fetch_array($source_result);
$sc_uid = $source_row["COURSE_UID"];
$su_uid = $source_row["UID"];
//Query the course the question move to
$course_query = "SELECT COURSE_UID from UNIT where UID = $u_uid";
$course_result = mysql_query($course_query);
$course_row = mysql_fetch_array($course_result);
$c_uid = $course_row["COURSE_UID"];
//Make directory
$str_course = "course_";
$str_unit = "unit_";
$src_dir = $binary_directory."/".$str_course.$sc_uid."/".$str_unit.$su_uid."/";
$dest_dir = $binary_directory."/".$str_course.$c_uid;
if (!file_exists($dest_dir))
mkdir($dest_dir, 0777);
$dest_dir = $dest_dir."/".$str_unit.$u_uid;
if (!file_exists($dest_dir))
mkdir($dest_dir, 0777);
$dest_dir = $binary_directory."/".$str_course.$c_uid."/".$str_unit.$u_uid."/";
//Insert into TEMPLATE BINARY
//Copy the Image file
if ($questiontype == 6)
{
$tmp_field=$row_field[5];
$tmp_field1=$row_field[6];
}else{
$tmp_field=$row_field[3];
$tmp_field1=$row_field[4];
}
if ($tmp_field != 0 && $tmp_field != "")
{
$image_id = insert_tb($tmp_field);
$filetype = find_filetype($tmp_field);
$src = $src_dir.$tmp_field.".".$filetype;
$dest = $dest_dir.$image_id.".".$filetype;
copy_file($src, $dest, $src_dir, $dest_dir, $image_id, $tmp_field, $filetype);
//rename ($dest, $dest_dir.$image_id.".".$filetype);
$tmp_field = $image_id;
}
//Copy the Sound file
if ($tmp_field1 != 0 && $tmp_field1 != "")
{ $sound_id = insert_tb($tmp_field1);
$filetype = find_filetype($tmp_field1);
$src = $src_dir.$tmp_field1.".".$filetype;
$dest = $dest_dir.$sound_id.".".$filetype;
copy_file($src, $dest, $src_dir, $dest_dir, $sound_id, $tmp_field1, $filetype);
$tmp_field1 = $sound_id;
}
if ($questiontype == 1)
{
$insert_query = "INSERT INTO $tabname (Q_UID, Q_CONTENT, INSTRUCTION, IMAGE_UID, SOUND_UID,";
$insert_query.= "CHOICE_1, HINT_1, CHOICE_2, HINT_2, CHOICE_3, HINT_3, CHOICE_4, HINT_4, CHOICE_5, HINT_5, CHOICE_6, HINT_6, ANSWER) ";
$insert_query.= "VALUES ($current_id, '$row_field[1]', '$row_field[2]', $tmp_field, $tmp_field1, '$row_field[5]', '$row_field[6]', '$row_field[7]', '$row_field[8]',";
$insert_query.= " '$row_field[9]', '$row_field[10]', '$row_field[11]', '$row_field[12]', '$row_field[13]', '$row_field[14]', '$row_field[15]', '$row_field[16]', '$row_field[17]')";
}
if ($questiontype == 2)
{ for ($k = 5; $k <=15; $k+=2)
{ if ($row_field[$k] != 0 && $row_field[$k] != "")
{ $temp_id = insert_tb($row_field[$k]);
$filetype = find_filetype($row_field[$k]);
$src = $src_dir.$row_field[$k].".".$filetype;
$dest = $dest_dir.$temp_id.".".$filetype;
copy_file($src, $dest, $src_dir, $dest_dir, $temp_id, $row_field[$k], $filetype);
$row_field[$k] = $temp_id;
}
if ($row_field[$k] == "")
$row_field[$k] = 0;
}
$insert_query = "INSERT INTO $tabname (Q_UID, Q_CONTENT, INSTRUCTION, IMAGE_UID, SOUND_UID,";
$insert_query.= " CHOICE_1, HINT_1, CHOICE_2, HINT_2, CHOICE_3, HINT_3, CHOICE_4, HINT_4, CHOICE_5, HINT_5, CHOICE_6, HINT_6, ANSWER)";
$insert_query.= " VALUES ($current_id, '$row_field[1]', '$row_field[2]', $tmp_field, $tmp_field1, $row_field[5], '$row_field[6]', $row_field[7],";
$insert_query.= " '$row_field[8]', $row_field[9], '$row_field[10]', $row_field[11], '$row_field[12]', $row_field[13], '$row_field[14]', $row_field[15], '$row_field[16]', '$row_field[17]') ";
}
if ($questiontype == 3)
{
$insert_query = "INSERT INTO $tabname (Q_UID, Q_CONTENT, INSTRUCTION, IMAGE_UID, SOUND_UID, HINT_TRUE, HINT_FALSE, ANSWER)";
$insert_query.= " VALUES ($current_id, '$row_field[1]', '$row_field[2]', $tmp_field, $tmp_field1, '$row_field[5]', '$row_field[6]', '$row_field[7]')";
}
if ($questiontype == 4)
{
$insert_query = "INSERT INTO $tabname (Q_UID, Q_CONTENT, INSTRUCTION, IMAGE_UID, SOUND_UID, ANSWER1_LEFT_X, ";
$insert_query.= "ANSWER1_LEFT_Y, ANSWER1_TOP_X, ANSWER1_TOP_Y, ANSWER2_LEFT_X, ANSWER2_LEFT_Y, ANSWER2_TOP_X,";
$insert_query.= " ANSWER2_TOP_Y, ANSWER3_LEFT_X, ANSWER3_LEFT_Y, ANSWER3_TOP_X, ANSWER3_TOP_Y, ANSWER4_LEFT_X, ";
$insert_query.= "ANSWER4_LEFT_Y, ANSWER4_TOP_X, ANSWER4_TOP_Y, HINT_1, HINT_2, HINT_3, HINT_4, ANSWER) ";
$insert_query.= "VALUES ($current_id, '$row_field[1]', '$row_field[2]', $tmp_field, $tmp_field1, '$row_field[5]', '$row_field[6]', '$row_field[7]',";
$insert_query.= " '$row_field[8]', '$row_field[9]', '$row_field[10]', '$row_field[11]', '$row_field[12]', '$row_field[13]', '$row_field[14]', '$row_field[15]', '$row_field[16]', '$row_field[17]',";
$insert_query.= " '$row_field[18]', '$row_field[19]', '$row_field[20]', '$row_field[21]', '$row_field[22]', '$row_field[23]', '$row_field[24]', $row_field[25]) ";
}
if ($questiontype == 5)
{ if ($row_field[5] != 0 && $row_field[5] != "")
{ $temp_id = insert_tb($row_field[5]);
$filetype = find_filetype($row_field[5]);
$src = $src_dir.$row_field[5].".".$filetype;
$dest = $dest_dir.$temp_id.".".$filetype;
copy_file($src, $dest, $src_dir, $dest_dir, $temp_id, $row_field[5], $filetype);
$row_field[5] = $temp_id;
}
if ($row_field[5] == "")
$row_field[5] = 0;
for ($k = 7; $k <=9; $k++)
{ if ($row_field[$k] != 0 && $row_field[$k] != "")
{ $temp_id = insert_tb($row_field[$k]);
$filetype = find_filetype($row_field[$k]);
$src = $src_dir.$row_field[$k].".".$filetype;
$dest = $dest_dir.$temp_id.".".$filetype;
copy_file($src, $dest, $src_dir, $dest_dir, $temp_id, $row_field[$k], $filetype);
$row_field[$k] = $temp_id;
}
if ($row_field[$k] == "")
$row_field[$k] = 0;
}
$insert_query = "INSERT INTO $tabname (Q_UID, Q_CONTENT, INSTRUCTION, SOUND_UID, IMAGE_UID, CHOICE_1, HINT_1, CHOICE_2, ";
$insert_query.= "CHOICE_3, CHOICE_4, HINT_2, HINT_3, HINT_4, TARGETTOP_1, TARGETLEFT_1, TARGETTOP_2, TARGETLEFT_2, TARGETTOP_3, ";
$insert_query.= "TARGETLEFT_3, TARGETTOP_4, TARGETLEFT_4, ANSWER1, ANSWER2, ANSWER3, ANSWER4) ";
$insert_query.= "VALUES ($current_id, '$row_field[1]', '$row_field[2]', $tmp_field, $tmp_field1, $row_field[5], '$row_field[6]', $row_field[7],";
$insert_query.= " $row_field[8], $row_field[9], '$row_field[10]', '$row_field[11]', '$row_field[12]', $row_field[13], $row_field[14], $row_field[15], $row_field[16], $row_field[17],";
$insert_query.= " $row_field[18], $row_field[19], $row_field[20], '$row_field[21]', '$row_field[22]', '$row_field[23]', '$row_field[24]')";
}
if ($questiontype == 6)
{
$insert_query = "INSERT INTO $tabname (Q_UID, INSTRUCTION, TEXT, HINT, IMAGE_UID, SOUND_UID) ";
$insert_query.= "VALUES ($current_id, '$row_field[2]', '$row_field[3]', '$row_field[4]', '$tmp_field', '$tmp_field1')";
}
if ($questiontype == 7)
{
$insert_query = "INSERT INTO $tabname (Q_UID, Q_CONTENT, INSTRUCTION, SOUND_UID, IMAGE_UID, SUB_QUESTION_1, ";
$insert_query.= "HINT_1, ANSWER_1, SUB_QUESTION_2, HINT_2, ANSWER_2, SUB_QUESTION_3, HINT_3, ANSWER_3, SUB_QUESTION_4, ";
$insert_query.= "HINT_4, ANSWER_4, SUB_QUESTION_5, HINT_5, ANSWER_5, SUB_QUESTION_6, HINT_6, ANSWER_6, SUB_QUESTION_7, ";
$insert_query.= "HINT_7, ANSWER_7, SUB_QUESTION_8, HINT_8, ANSWER_8, SUB_QUESTION_9, HINT_9, ANSWER_9, SUB_QUESTION_10, ";
$insert_query.= "HINT_10, ANSWER_10, CHOICE_1, CHOICE_2, CHOICE_3, CHOICE_4, CHOICE_5, CHOICE_6, CHOICE_7, CHOICE_8, CHOICE_9, CHOICE_10) ";
$insert_query.= "VALUES ($current_id, '$row_field[1]', '$row_field[2]', $tmp_field, $tmp_field1, '$row_field[5]', '$row_field[6]', '$row_field[7]',";
$insert_query.= " '$row_field[8]', '$row_field[9]', '$row_field[10]', '$row_field[11]', '$row_field[12]', '$row_field[13]', '$row_field[14]', '$row_field[15]', '$row_field[16]', '$row_field[17]',";
$insert_query.= " '$row_field[18]', '$row_field[19]', '$row_field[20]', '$row_field[21]', '$row_field[22]', '$row_field[23]', '$row_field[24]', '$row_field[25]', ";
$insert_query.= " '$row_field[26]', '$row_field[27]', '$row_field[28]', '$row_field[29]', '$row_field[30]', '$row_field[31]', '$row_field[32]', '$row_field[33]', '$row_field[34]',";
$insert_query.= " '$row_field[35]', '$row_field[36]', '$row_field[37]', '$row_field[38]', '$row_field[39]', '$row_field[40]', '$row_field[41]', '$row_field[42]', '$row_field[43]', '$row_field[44]')";
}
$insert_result = mysql_query($insert_query);
}
}
if ($insert_result) {
?>
<script language=javascript>
window.location.href="question_list.php"+"<? print "?level=$level&question_type=$question_type&author=$author&from=$from&to=$to"; ?>";
</script>
<? }
else
$content.="\n<br><font color=red>$QUESTION_CANNOT_COPY</font><br>\n";
}
return $content;
}
function copy_file($src, $dest, $src_dir, $dest_dir, $id, $src_id, $filetype)
{ include "config_inc.php";
if (!file_exists($dest)){
copy($src, $dest);
}
}
function insert_tb ($file_id)
{
include ("./config_inc.php");
//Get data from the TEMPLATE BINARY
$get_query = "SELECT * from TEMPLATE_BINARY where UID = $file_id";
$get_result = mysql_query($get_query);
if (!empty($get_result))
{ while ($row1 = mysql_fetch_array($get_result))
{ $filename = $row1["IMAGE_LINK"];
$filedesc = $row1["FILEDESC"];
$file_name = $row1["FILENAME"];
$file_size = $row1["FILESIZE"];
$file_type = $row1["FILETYPE"];
$file = $row1["IMAGE_NAME"];
}
}
//Insert a new record into TEMPLATE BINARY
$query = "INSERT INTO TEMPLATE_BINARY (IMAGE_LINK,FILEDESC,FILENAME,FILESIZE,FILETYPE,IMAGE_NAME) VALUES ('','','$file_name','$file_size','$file_type','')";
$result = mysql_query ($query);
$max_query="select MAX(UID) from TEMPLATE_BINARY";
$max_result=mysql_query($max_query);
if(!empty($max_result)){
$max_row=mysql_fetch_array($max_result);
$id=$max_row["MAX(UID)"];
}
//Return the recently inserted UID so that the new record in the QUESTION & the 7 different type of table can be insert
/*
if(!chmod($binary_directory."/".$id.$ext , 0755) || $size==-1) // change mode
die("fail");
else
*/
return $id;
}
?>