Ejemplo n.º 1
0
/**
 * Form to edit a category
 * @todo move to TestCategory.class.php
 * @param string $action
 */
function edit_category_form($action)
{
    $action = Security::remove_XSS($action);
    if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
        $category_id = Security::remove_XSS($_GET['category_id']);
        $objcat = new TestCategory($category_id);
        $form = new FormValidator('note', 'post', api_get_self() . '?action=' . $action . '&category_id=' . $category_id);
        // Setting the form elements
        $form->addElement('header', get_lang('EditCategory'));
        $form->addElement('hidden', 'category_id');
        $form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95'));
        $form->addHtmlEditor('category_description', get_lang('CategoryDescription'), false, false, array('ToolbarSet' => 'test_category', 'Height' => '200'));
        $form->addButtonSave(get_lang('ModifyCategory'), 'SubmitNote');
        // setting the defaults
        $defaults = array();
        $defaults["category_id"] = $objcat->id;
        $defaults["category_name"] = $objcat->name;
        $defaults["category_description"] = $objcat->description;
        $form->setDefaults($defaults);
        // setting the rules
        $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
        // The validation or display
        if ($form->validate()) {
            $check = Security::check_token('post');
            if ($check) {
                $values = $form->exportValues();
                $v_id = Security::remove_XSS($values['category_id']);
                $v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER);
                $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
                $objcat = new TestCategory($v_id, $v_name, $v_description);
                if ($objcat->modifyCategory()) {
                    Display::display_confirmation_message(get_lang('MofidfyCategoryDone'));
                } else {
                    Display::display_confirmation_message(get_lang('ModifyCategoryError'));
                }
            }
            Security::clear_token();
        } else {
            display_goback();
            $token = Security::get_token();
            $form->addElement('hidden', 'sec_token');
            $form->setConstants(array('sec_token' => $token));
            $form->display();
        }
    } else {
        Display::display_error_message(get_lang('CannotEditCategory'));
    }
}
 /**
  * @todo : add session id when used for session
  */
 public function restore_test_category($session_id, $respect_base_content, $destination_course_code)
 {
     $course_id = api_get_course_int_id();
     // Let's restore the categories
     $tab_test_category_id_old_new = array();
     // used to build the quiz_question_rel_category table
     if ($this->course->has_resources(RESOURCE_TEST_CATEGORY)) {
         $resources = $this->course->resources;
         foreach ($resources[RESOURCE_TEST_CATEGORY] as $id => $CourseCopyTestcategory) {
             $tab_test_category_id_old_new[$CourseCopyTestcategory->source_id] = $id;
             // check if this test_category already exist in the destination BDD
             // do not Database::escape_string $title and $description, it will be done later
             $title = $CourseCopyTestcategory->title;
             $description = $CourseCopyTestcategory->description;
             if (TestCategory::category_exists_with_title($title)) {
                 switch ($this->file_option) {
                     case FILE_SKIP:
                         //Do nothing
                         break;
                     case FILE_RENAME:
                         $new_title = $title . "_";
                         while (TestCategory::category_exists_with_title($new_title)) {
                             $new_title .= "_";
                         }
                         $test_category = new TestCategory(0, $new_title, $description);
                         $new_id = $test_category->addCategoryInBDD();
                         $tab_test_category_id_old_new[$CourseCopyTestcategory->source_id] = $new_id;
                         break;
                     case FILE_OVERWRITE:
                         $id = TestCategory::get_category_id_for_title($title);
                         $my_cat = new TestCategory($id);
                         $my_cat->name = $title;
                         $my_cat->modifyCategory();
                         $tab_test_category_id_old_new[$CourseCopyTestcategory->source_id] = $id;
                         break;
                 }
             } else {
                 // create a new test_category
                 $test_category = new TestCategory(0, $title, $description);
                 $new_id = $test_category->addCategoryInBDD();
                 $tab_test_category_id_old_new[$CourseCopyTestcategory->source_id] = $new_id;
             }
             $this->course->resources[RESOURCE_TEST_CATEGORY][$id]->destination_id = $tab_test_category_id_old_new[$CourseCopyTestcategory->source_id];
         }
     }
     // lets check if quizzes-question are restored too, to redo the link between test_category and quizzes question for questions restored
     // we can use the source_id field
     // question source_id => category source_id
     if ($this->course->has_resources(RESOURCE_QUIZQUESTION)) {
         // check the category number of each question restored
         if (!empty($resources[RESOURCE_QUIZQUESTION])) {
             foreach ($resources[RESOURCE_QUIZQUESTION] as $id => $CourseCopyQuestion) {
                 $new_quiz_question_id = $resources[RESOURCE_QUIZQUESTION][$id]->destination_id;
                 $question_category = $CourseCopyQuestion->question_category;
                 if ($question_category > 0) {
                     TestCategory::add_category_for_question_id($tab_test_category_id_old_new[$question_category], $new_quiz_question_id, $course_id);
                 }
             }
         }
     }
 }