Пример #1
0
        }
    }
    if (!$found_alternative) {
        echo '    <div id="' . $pid . '_' . $alternative_type . '">' . "\n";
        echo '      <input type="button" value="' . _AT('add') . '" title="' . _AT('new_window') . '" onclick="ATutor.poptastic(\'' . AT_BASE_HREF . 'mods/_core/file_manager/index.php?framed=1' . SEP . 'popup=1' . SEP . 'cp=' . $content_row['content_path'] . SEP . 'cid=' . $content_id . SEP . 'pid=' . $pid . SEP . 'a_type=' . $alternative_type . '\');return false;" />' . "\n";
        echo '    </div>' . "\n";
    }
    echo '    </td>' . "\n";
}
// Main program
if ($_POST['formatting'] != 1) {
    $msg->addFeedback('NO_A4A_FOR_PLAIN_TEXT');
    $msg->printAll();
} else {
    global $db, $content_row, $stripslashes;
    populate_a4a($cid, $stripslashes($_POST['body_text']), $_POST['formatting']);
    include_once AT_INCLUDE_PATH . '../mods/_core/imsafa/classes/A4a.class.php';
    $a4a = new A4a($cid);
    $primary_resources = $a4a->getPrimaryResources();
    if (count($primary_resources) == 0) {
        $msg->addFeedback('NO_RESOURCES');
        $msg->printAll();
    } else {
        $is_post_indicator_set = false;
        // get all resource types
        $sql = "SELECT * FROM %sresource_types";
        $rows_resource_types = queryDB($sql, array(TABLE_PREFIX));
        echo '<table class="data" rules="all">' . "\n";
        echo '  <thead>' . "\n";
        echo '  <tr>' . "\n";
        echo '    <th rowspan="2" id="header1">' . _AT('original_resource') . '</th>' . "\n";
function save_changes($redir, $current_tab)
{
    global $contentManager, $db, $addslashes, $msg, $stripslashes;
    $_POST['pid'] = intval($_POST['pid']);
    $_POST['cid'] = intval($_POST['cid']);
    $_POST['alternatives'] = intval($_POST['alternatives']);
    $_POST['title'] = trim($_POST['title']);
    $_POST['head'] = trim($_POST['head']);
    $_POST['use_customized_head'] = isset($_POST['use_customized_head']) ? $_POST['use_customized_head'] : 0;
    // $_POST['body_text']	= $stripslashes(trim($_POST['body_text'])); //this line breaks LaTex
    $_POST['body_text'] = trim($_POST['body_text']);
    $_POST['weblink_text'] = trim($_POST['weblink_text']);
    $_POST['formatting'] = intval($_POST['formatting']);
    $_POST['keywords'] = $stripslashes(trim($_POST['keywords']));
    $_POST['test_message'] = trim($_POST['test_message']);
    $_POST['allow_test_export'] = intval($_POST['allow_test_export']);
    //if weblink is selected, use it
    if ($_POST['formatting'] == CONTENT_TYPE_WEBLINK) {
        $url = $_POST['weblink_text'];
        $validated_url = isValidURL($url);
        if (!validated_url || $validated_url !== $url) {
            $msg->addError(array('INVALID_INPUT', _AT('weblink')));
        } else {
            $_POST['body_text'] = $url;
            $content_type_pref = CONTENT_TYPE_WEBLINK;
        }
    } else {
        $content_type_pref = CONTENT_TYPE_CONTENT;
    }
    if (!($release_date = generate_release_date())) {
        $msg->addError('BAD_DATE');
    }
    if ($_POST['title'] == '') {
        $msg->addError(array('EMPTY_FIELDS', _AT('title')));
    }
    if (!$msg->containsErrors()) {
        $orig_body_text = $_POST['body_text'];
        // used to populate a4a tables
        /*	
        		$_POST['title']			= $addslashes($_POST['title']);
        		$_POST['body_text']		= $addslashes($_POST['body_text']);
        		$_POST['head']  		= $addslashes($_POST['head']);
        		$_POST['keywords']		= $addslashes($_POST['keywords']);
        		$_POST['test_message']	= $addslashes($_POST['test_message']);		
        */
        // add or edit content
        if ($_POST['cid']) {
            /* editing an existing page */
            $err = $contentManager->editContent($_POST['cid'], $_POST['title'], $_POST['body_text'], $_POST['keywords'], $_POST['related'], $_POST['formatting'], $release_date, $_POST['head'], $_POST['use_customized_head'], $_POST['test_message'], $_POST['allow_test_export'], $content_type_pref);
            $cid = $_POST['cid'];
        } else {
            /* insert new */
            $cid = $contentManager->addContent($_SESSION['course_id'], $_POST['pid'], $_POST['ordering'], $_POST['title'], $_POST['body_text'], $_POST['keywords'], $_POST['related'], $_POST['formatting'], $release_date, $_POST['head'], $_POST['use_customized_head'], $_POST['test_message'], $_POST['allow_test_export'], $content_type_pref);
            $_POST['cid'] = $cid;
            $_REQUEST['cid'] = $cid;
        }
        // re-populate a4a tables based on the new content
        populate_a4a($cid, $orig_body_text, $_POST['formatting']);
    } else {
        return;
    }
    /* insert glossary terms */
    if (is_array($_POST['glossary_defs']) && ($num_terms = count($_POST['glossary_defs']))) {
        global $glossary, $glossary_ids, $msg;
        foreach ($_POST['glossary_defs'] as $w => $d) {
            $old_w = $w;
            $key = in_array_cin($w, $glossary_ids);
            $w = urldecode($w);
            $d = $addslashes($d);
            if ($key !== false && ($glossary[$old_w] != $d || isset($_POST['related_term'][$old_w]))) {
                $w = addslashes($w);
                $related_id = intval($_POST['related_term'][$old_w]);
                $sql = "UPDATE %sglossary SET definition='%s', related_word_id=%d WHERE word_id=%d AND course_id=%d";
                $result = queryDB($sql, array(TABLE_PREFIX, $d, $related_id, $key, $_SESSION['course_id']));
                $glossary[$old_w] = $d;
            } else {
                if ($key === false && $d != '') {
                    $w = addslashes($w);
                    $related_id = intval($_POST['related_term'][$old_w]);
                    $sql = "INSERT INTO %sglossary VALUES (NULL, %d, '%s', '%s', %d)";
                    $result = queryDB($sql, array(TABLE_PREFIX, $_SESSION['course_id'], $w, $d, $related_id));
                    $glossary[$old_w] = $d;
                }
            }
        }
    }
    if (isset($_GET['tab'])) {
        $current_tab = intval($_GET['tab']);
    }
    if (isset($_POST['current_tab'])) {
        $current_tab = intval($_POST['current_tab']);
    }
    // adapted content: save primary content type
    if (isset($_POST['use_post_for_alt'])) {
        // 1. delete old primary content type
        $sql = "DELETE FROM %sprimary_resources_types\n\t\t         WHERE primary_resource_id in \n\t\t               (SELECT DISTINCT primary_resource_id \n\t\t                  FROM %sprimary_resources\n\t\t                 WHERE content_id=%d\n\t\t                   AND language_code='%s')";
        $result = queryDB($sql, array(TABLE_PREFIX, TABLE_PREFIX, $cid, $_SESSION['lang']));
        // 2. insert the new primary content type
        $sql = "SELECT pr.primary_resource_id, rt.type_id\n\t\t          FROM %sprimary_resources pr, \n\t\t                 %sresource_types rt\n\t\t         WHERE pr.content_id = %d\n\t\t           AND pr.language_code = '%s'";
        $all_types_result = queryDB($sql, array(TABLE_PREFIX, TABLE_PREFIX, $cid, $_SESSION['lang']));
        foreach ($all_types_result as $type) {
            if (isset($_POST['alt_' . $type['primary_resource_id'] . '_' . $type['type_id']])) {
                $sql = "INSERT INTO %sprimary_resources_types (primary_resource_id, type_id)\n\t\t\t\t        VALUES (%d, %d)";
                $result = queryDB($sql, array(TABLE_PREFIX, $type['primary_resource_id'], $type['type_id']));
            }
        }
    }
    //Add test to this content - @harris
    $sql = "SELECT * FROM %scontent_tests_assoc WHERE content_id=%d";
    $rows_content_tests = queryDB($sql, array(TABLE_PREFIX, $_POST['cid']));
    $db_test_array = array();
    foreach ($rows_content_tests as $row) {
        $db_test_array[] = $row['test_id'];
    }
    if (is_array($_POST['tid']) && sizeof($_POST['tid']) > 0) {
        $toBeDeleted = array_diff($db_test_array, $_POST['tid']);
        $toBeAdded = array_diff($_POST['tid'], $db_test_array);
        //Delete entries
        if (!empty($toBeDeleted)) {
            $tids = implode(",", $toBeDeleted);
            $sql = "DELETE FROM %scontent_tests_assoc WHERE content_id=%d AND test_id IN (%s)";
            $result = queryDB($sql, array(TABLE_PREFIX, $_POST['cid'], $tids));
        }
        //Add entries
        if (!empty($toBeAdded)) {
            foreach ($toBeAdded as $i => $tid) {
                $tid = intval($tid);
                $sql = "INSERT INTO %scontent_tests_assoc SET content_id=%d, test_id=%d";
                $result = queryDB($sql, array(TABLE_PREFIX, $_POST['cid'], $tid));
            }
        }
    } else {
        $sql = "DELETE FROM %scontent_tests_assoc WHERE content_id=%d";
        $result = queryDB($sql, array(TABLE_PREFIX, $_POST['cid']));
    }
    //End Add test
    // add pre-tests
    $sql = "DELETE FROM %scontent_prerequisites WHERE content_id=%d AND type='%s'";
    $result = queryDB($sql, array(TABLE_PREFIX, $_POST['cid'], CONTENT_PRE_TEST));
    if (is_array($_POST['pre_tid']) && sizeof($_POST['pre_tid']) > 0) {
        foreach ($_POST['pre_tid'] as $i => $tid) {
            $tid = intval($tid);
            $sql = "INSERT INTO %scontent_prerequisites SET content_id=%d, type='%s', item_id=%d";
            $result = queryDB($sql, array(TABLE_PREFIX, $_POST['cid'], CONTENT_PRE_TEST, $tid));
        }
    }
    if (isset($_SESSION['associated_forum']) && !$msg->containsErrors()) {
        if ($_SESSION['associated_forum'] == 'none') {
            $sql = "DELETE FROM %scontent_forums_assoc WHERE content_id=%d";
            queryDB($sql, array(TABLE_PREFIX, $_POST['cid']));
        } else {
            $sql = "DELETE FROM %scontent_forums_assoc WHERE content_id=%d";
            queryDB($sql, array(TABLE_PREFIX, $_POST['cid']));
            $associated_forum = $_SESSION['associated_forum'];
            for ($i = 0; $i < count($associated_forum); $i++) {
                $sql = "INSERT INTO %scontent_forums_assoc SET content_id=%d,forum_id=%d";
                queryDB($sql, array(TABLE_PREFIX, $_POST['cid'], $associated_forum[$i]));
            }
        }
        unset($_SESSION['associated_forum']);
    }
    if (!$msg->containsErrors() && $redir) {
        $_SESSION['save_n_close'] = $_POST['save_n_close'];
        $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
        header('Location: ' . basename($_SERVER['PHP_SELF']) . '?cid=' . $cid . SEP . 'close=' . $addslashes($_POST['save_n_close']) . SEP . 'tab=' . $addslashes($_POST['current_tab']) . SEP . 'displayhead=' . $addslashes($_POST['displayhead']) . SEP . 'alternatives=' . $addslashes($_POST['alternatives']));
        exit;
    } else {
        return;
    }
}
function save_changes($redir, $current_tab)
{
    global $contentManager, $addslashes, $msg, $_course_id, $_content_id, $stripslashes;
    $_POST['pid'] = intval($_POST['pid']);
    $_POST['_cid'] = intval($_POST['_cid']);
    $_POST['alternatives'] = intval($_POST['alternatives']);
    $_POST['title'] = trim($_POST['title']);
    $_POST['head'] = trim($_POST['head']);
    $_POST['use_customized_head'] = isset($_POST['use_customized_head']) ? $_POST['use_customized_head'] : 0;
    $_POST['body_text'] = $stripslashes(trim($_POST['body_text']));
    $_POST['weblink_text'] = trim($_POST['weblink_text']);
    $_POST['formatting'] = intval($_POST['formatting']);
    $_POST['keywords'] = $stripslashes(trim($_POST['keywords']));
    $_POST['test_message'] = trim($_POST['test_message']);
    //if weblink is selected, use it
    if ($_POST['formatting'] == CONTENT_TYPE_WEBLINK) {
        $url = $_POST['weblink_text'];
        $validated_url = isValidURL($url);
        if (!validated_url || $validated_url !== $url) {
            $msg->addError(array('INVALID_INPUT', _AT('weblink')));
        } else {
            $_POST['body_text'] = $url;
            $content_type_pref = CONTENT_TYPE_WEBLINK;
        }
    } else {
        $content_type_pref = CONTENT_TYPE_CONTENT;
    }
    /*if (!($release_date = generate_release_date())) {
    		$msg->addError('BAD_DATE');
    	}*/
    //	if ($_POST['title'] == '') {
    //		$msg->addError(array('EMPTY_FIELDS', _AT('title')));
    //	}
    //	if (!$msg->containsErrors()) {
    $orig_body_text = $_POST['body_text'];
    // used to populate a4a tables
    //		$_POST['title']			= $addslashes($_POST['title']);
    //		$_POST['body_text']		= $addslashes($_POST['body_text']);
    //		$_POST['head']  		= $addslashes($_POST['head']);
    //		$_POST['keywords']		= $addslashes($_POST['keywords']);
    //		$_POST['test_message']	= $addslashes($_POST['test_message']);
    // add or edit content
    if ($_POST['_cid']) {
        /* editing an existing page */
        $err = $contentManager->editContent($_POST['_cid'], $_POST['title'], $_POST['body_text'], $_POST['keywords'], $_POST['formatting'], $_POST['head'], $_POST['use_customized_head'], $_POST['test_message']);
        $cid = $_POST['_cid'];
    } else {
        /* insert new */
        $cid = $contentManager->addContent($_course_id, $_POST['pid'], $_POST['ordering'], $_POST['title'], $_POST['body_text'], $_POST['keywords'], $_POST['related'], $_POST['formatting'], $_POST['head'], $_POST['use_customized_head'], $_POST['test_message'], $content_type_pref);
        $_POST['_cid'] = $cid;
        $_REQUEST['_cid'] = $cid;
    }
    // re-populate a4a tables based on the new content
    populate_a4a($cid, $orig_body_text, $_POST['formatting']);
    if ($cid == 0) {
        return;
    }
    //	}
    /* insert glossary terms */
    /*
    	if (is_array($_POST['glossary_defs']) && ($num_terms = count($_POST['glossary_defs']))) {
    		global $glossary, $glossary_ids, $msg;
    
    		foreach($_POST['glossary_defs'] as $w => $d) {
    			$old_w = $w;
    			$key = in_array_cin($w, $glossary_ids);
    			$w = urldecode($w);
    			$d = $addslashes($d);
    
    			if (($key !== false) && (($glossary[$old_w] != $d) || isset($_POST['related_term'][$old_w])) ) {
    				$w = addslashes($w);
    				$related_id = intval($_POST['related_term'][$old_w]);
    				$sql = "UPDATE ".TABLE_PREFIX."glossary SET definition='$d', related_word_id=$related_id WHERE word_id=$key AND course_id=$_SESSION[course_id]";
    				$result = mysql_query($sql, $db);
    				$glossary[$old_w] = $d;
    			} else if ($key === false && ($d != '')) {
    				$w = addslashes($w);
    				$related_id = intval($_POST['related_term'][$old_w]);
    				$sql = "INSERT INTO ".TABLE_PREFIX."glossary VALUES (NULL, $_SESSION[course_id], '$w', '$d', $related_id)";
    
    				$result = mysql_query($sql, $db);
    				$glossary[$old_w] = $d;
    			}
    		}
    	}*/
    if (isset($_GET['tab'])) {
        $current_tab = intval($_GET['tab']);
    }
    if (isset($_POST['current_tab'])) {
        $current_tab = intval($_POST['current_tab']);
    }
    // adapted content: save primary content type
    if (isset($_POST['use_post_for_alt'])) {
        include_once TR_INCLUDE_PATH . 'classes/DAO/PrimaryResourcesTypesDAO.class.php';
        $primaryResourcesTypesDAO = new PrimaryResourcesTypesDAO();
        // 1. delete old primary content type
        $sql = "DELETE FROM " . TABLE_PREFIX . "primary_resources_types\n\t\t         WHERE primary_resource_id in \n\t\t               (SELECT DISTINCT primary_resource_id \n\t\t                  FROM " . TABLE_PREFIX . "primary_resources\n\t\t                 WHERE content_id=" . $cid . "\n\t\t                   AND language_code='" . $_SESSION['lang'] . "')";
        $primaryResourcesTypesDAO->execute($sql);
        // 2. insert the new primary content type
        $sql = "SELECT pr.primary_resource_id, rt.type_id\n\t\t          FROM " . TABLE_PREFIX . "primary_resources pr, " . TABLE_PREFIX . "resource_types rt\n\t\t         WHERE pr.content_id = " . $cid . "\n\t\t           AND pr.language_code = '" . $_SESSION['lang'] . "'";
        $all_types_rows = $primaryResourcesTypesDAO->execute($sql);
        if (is_array($all_types_rows)) {
            foreach ($all_types_rows as $type) {
                if (isset($_POST['alt_' . $type['primary_resource_id'] . '_' . $type['type_id']])) {
                    $primaryResourcesTypesDAO->Create($type['primary_resource_id'], $type['type_id']);
                    //					$sql = "INSERT INTO ".TABLE_PREFIX."primary_resources_types (primary_resource_id, type_id)
                    //					        VALUES (".$type['primary_resource_id'].", ".$type['type_id'].")";
                    //					$result = mysql_query($sql, $db);
                }
            }
        }
    }
    include_once TR_INCLUDE_PATH . 'classes/DAO/ContentTestsAssocDAO.class.php';
    $contentTestsAssocDAO = new ContentTestsAssocDAO();
    $test_rows = $contentTestsAssocDAO->getByContent($_POST['_cid']);
    //	$sql = 'SELECT * FROM '.TABLE_PREFIX."content_tests_assoc WHERE content_id=$_POST[cid]";
    //	$result = mysql_query($sql, $db);
    $db_test_array = array();
    if (is_array($test_rows)) {
        foreach ($test_rows as $row) {
            $db_test_array[] = $row['test_id'];
        }
    }
    if (is_array($_POST['tid']) && sizeof($_POST['tid']) > 0) {
        $toBeDeleted = array_diff($db_test_array, $_POST['tid']);
        $toBeAdded = array_diff($_POST['tid'], $db_test_array);
        //Delete entries
        if (!empty($toBeDeleted)) {
            $tids = implode(",", $toBeDeleted);
            $sql = 'DELETE FROM ' . TABLE_PREFIX . "content_tests_assoc WHERE content_id={$_POST['cid']} AND test_id IN ({$tids})";
            $contentTestsAssocDAO->execute($sql);
        }
        //Add entries
        if (!empty($toBeAdded)) {
            foreach ($toBeAdded as $i => $tid) {
                $tid = intval($tid);
                //				$sql = 'INSERT INTO '. TABLE_PREFIX . "content_tests_assoc SET content_id=$_POST[cid], test_id=$tid";
                //				$result = mysql_query($sql, $db);
                if ($contentTestsAssocDAO->Create($_POST['_cid'], $tid) === false) {
                    $msg->addError('DB_NOT_UPDATED');
                }
            }
        }
    } else {
        //All tests has been removed.
        $contentTestsAssocDAO->DeleteByContentID($_POST['_cid']);
        //		$sql = 'DELETE FROM '. TABLE_PREFIX . "content_tests_assoc WHERE content_id=$_POST[cid]";
        //		$result = mysql_query($sql, $db);
    }
    //End Add test
    //TODO*******************BOLOGNA****************REMOVE ME**************/
    /*
    	if(isset($_SESSION['associated_forum']) && !$msg->containsErrors()){
    		if($_SESSION['associated_forum']=='none'){
    			$sql = "DELETE FROM ".TABLE_PREFIX."content_forums_assoc WHERE content_id='$_POST[cid]'";
    			mysql_query($sql,$db);
    		} else {
    			$sql = "DELETE FROM ".TABLE_PREFIX."content_forums_assoc WHERE content_id='$_POST[cid]'";
    			mysql_query($sql,$db);
    			$associated_forum = $_SESSION['associated_forum'];
    			for($i=0; $i<count($associated_forum); $i++){
    				$sql="INSERT INTO ".TABLE_PREFIX."content_forums_assoc SET content_id='$_POST[cid]',forum_id='$associated_forum[$i]'";
    				mysql_query($sql,$db);
    			}
    		}
    		unset($_SESSION['associated_forum']);
    	}
    */
    if (!$msg->containsErrors() && $redir) {
        $_SESSION['save_n_close'] = $_POST['save_n_close'];
        $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
        header('Location: ' . basename($_SERVER['PHP_SELF']) . '?_cid=' . $cid . SEP . 'close=' . $addslashes($_POST['save_n_close']) . SEP . 'tab=' . $addslashes($_POST['current_tab']) . SEP . 'displayhead=' . $addslashes($_POST['displayhead']) . SEP . 'alternatives=' . $addslashes($_POST['alternatives']));
        exit;
    } else {
        return;
    }
}