* @copyright Copyright 2010 Queen's University. All Rights Reserved.
 *
*/
if (!defined("PARENT_INCLUDED") || !defined("IN_QUIZZES")) {
    exit;
} elseif (!isset($_SESSION["isAuthorized"]) || !$_SESSION["isAuthorized"]) {
    header("Location: " . ENTRADA_URL);
    exit;
} elseif (!$ENTRADA_ACL->amIAllowed('quiz', 'update', false)) {
    $ONLOAD[] = "setTimeout('window.location=\\'" . ENTRADA_URL . "/admin/" . $MODULE . "\\'', 15000)";
    $ERRORSTR[] = "Your account does not have the permissions required to use this feature of this module.<br /><br />If you believe you are receiving this message in error please contact <a href=\"mailto:" . html_encode($AGENT_CONTACTS["administrator"]["email"]) . "\">" . html_encode($AGENT_CONTACTS["administrator"]["name"]) . "</a> for assistance.";
    echo display_error();
    application_log("error", "Group [" . $_SESSION["permissions"][$ENTRADA_USER->getAccessId()]["group"] . "] and role [" . $_SESSION["permissions"][$ENTRADA_USER->getAccessId()]["role"] . "] does not have access to this module [" . $MODULE . "]");
} else {
    if ($RECORD_ID) {
        $quiz = Models_Quiz::fetchRowByID($RECORD_ID);
        $PROCESSED = $quiz->toArray();
        if ($PROCESSED && $ENTRADA_ACL->amIAllowed(new QuizResource($PROCESSED["quiz_id"]), "update")) {
            $BREADCRUMB[] = array("url" => ENTRADA_URL . "/admin/" . $MODULE . "?section=edit&id=" . $RECORD_ID, "title" => limit_chars($PROCESSED["quiz_title"], 32));
            $PROCESSED["associated_proxy_ids"] = array();
            /**
             * Load the rich text editor.
             */
            load_rte();
            // Error Checking
            switch ($STEP) {
                case 2:
                    /**
                     * Required field "quiz_title" / Quiz Title.
                     */
                    if (isset($_POST["quiz_title"]) && ($tmp_input = clean_input($_POST["quiz_title"], array("notags", "trim")))) {
                         }
                         $quiz_contacts = Models_Quiz_Contact::fetchAllRecords($new_quiz_id);
                         if ($quiz_contacts) {
                             foreach ($quiz_contacts as $quiz_contact) {
                                 $quiz_contact->delete();
                             }
                         }
                         $quiz = Models_Quiz::fetchRowByID($new_quiz_id);
                         if ($quiz) {
                             $quiz->delete();
                         }
                         add_error("There was a problem creating the new quiz at this time. The system administrator was informed of this error; please try again later.");
                     }
                 }
             } else {
                 $quiz = Models_Quiz::fetchRowByID($new_quiz_id);
                 if ($quiz) {
                     $quiz->delete();
                 }
                 add_error("Unable to copy the existing quiz authors from the original quiz. The system administrator was informed of this error; please try again later.");
                 application_log("error", "Unable to copy any quiz authors when attempting to copy quiz_id [" . $RECORD_ID . "] authors to quiz_id [" . $new_quiz_id . "]. Database said: " . $db->ErrorMsg());
             }
         } else {
             add_error("There was a problem creating the new quiz at this time. The system administrator was informed of this error; please try again later.");
             application_log("error", "There was an error inserting a copied quiz, as there was no new_quiz_id available from Insert_Id(). Database said: " . $db->ErrorMsg());
         }
     } else {
         add_error("There was a problem creating the new quiz at this time. The system administrator was informed of this error; please try again later.");
         application_log("error", "There was an error inserting a new copied quiz. Database said: " . $db->ErrorMsg());
     }
 } else {
     foreach ($question_types as $question_type) {
         $question_type_ids[] = $question_type->getQuestionTypeID();
     }
 }
 if ($question_types && isset($_GET["type"]) && in_array($_GET["type"], $question_type_ids)) {
     $set_type = true;
     $type = clean_input($_GET["type"], "numeric");
 } else {
     $set_type = false;
     $type = 1;
 }
 if (isset($_GET["qquestion_id"]) && ($tmp_input = clean_input($_GET["qquestion_id"], "int"))) {
     $qquestion_group_id = $tmp_input;
 }
 if ($RECORD_ID) {
     $quiz_record = Models_Quiz::fetchRowByID($RECORD_ID);
     if ($quiz_record && $ENTRADA_ACL->amIAllowed(new QuizResource($quiz_record->getQuizID()), "update")) {
         if ($ALLOW_QUESTION_MODIFICATIONS) {
             $BREADCRUMB[] = array("url" => ENTRADA_URL . "/admin/" . $MODULE . "?section=edit&id=" . $RECORD_ID, "title" => limit_chars($quiz_record->getQuizTitle(), 32));
             $BREADCRUMB[] = array("url" => ENTRADA_URL . "/admin/" . $MODULE . "?section=add-question&id=" . $RECORD_ID, "title" => "Add Quiz Question");
             /**
              * Load the rich text editor.
              */
             load_rte("advanced");
             if ($type == "3") {
                 $STEP = 2;
                 $_POST["question_text"] = "Page Break";
                 $_POST["questiontype_id"] = $type;
                 $_POST["post_action"] = "content";
             }
             // Error Checking
*/
if (!defined("PARENT_INCLUDED") || !defined("IN_QUIZZES")) {
    exit;
} elseif (!isset($_SESSION["isAuthorized"]) || !$_SESSION["isAuthorized"]) {
    header("Location: " . ENTRADA_URL);
    exit;
} elseif (!$ENTRADA_ACL->amIAllowed('quizquestion', 'update', false)) {
    $ONLOAD[] = "setTimeout('window.location=\\'" . ENTRADA_URL . "/admin/" . $MODULE . "\\'', 15000)";
    $ERROR++;
    $ERRORSTR[] = "Your account does not have the permissions required to use this feature of this module.<br /><br />If you believe you are receiving this message in error please contact <a href=\"mailto:" . html_encode($AGENT_CONTACTS["administrator"]["email"]) . "\">" . html_encode($AGENT_CONTACTS["administrator"]["name"]) . "</a> for assistance.";
    echo display_error();
    application_log("error", "Group [" . $_SESSION["permissions"][$ENTRADA_USER->getAccessId()]["group"] . "] and role [" . $_SESSION["permissions"][$ENTRADA_USER->getAccessId()]["role"] . "] does not have access to this module [" . $MODULE . "]");
} else {
    if ($RECORD_ID) {
        $question = Models_Quiz_Question::fetchRowByID($RECORD_ID);
        $quiz_record = Models_Quiz::fetchRowByID($question->getQuizID());
        if ($quiz_record && $ENTRADA_ACL->amIAllowed(new QuizResource($quiz_record->getQuizID()), "update")) {
            if ($ALLOW_QUESTION_MODIFICATIONS) {
                $BREADCRUMB[] = array("url" => ENTRADA_URL . "/admin/" . $MODULE . "?section=edit&id=" . $quiz_record->getQuizID(), "title" => limit_chars($quiz_record->getQuizTitle(), 32));
                $BREADCRUMB[] = array("url" => ENTRADA_URL . "/admin/" . $MODULE . "?section=edit-question&id=" . $RECORD_ID, "title" => "Edit Quiz Question");
                /**
                 * Load the rich text editor.
                 */
                load_rte("advanced");
                // Error Checking
                switch ($STEP) {
                    case 2:
                        /**
                         * Required field "questiontype_id" / Question Type
                         * Currently only multile choice questions are supported, although
                         * this is something we will be expanding on shortly.