/** * Restore quiz-questions * @params int question id */ function restore_quiz_question($id) { $resources = $this->course->resources; $question = isset($resources[RESOURCE_QUIZQUESTION][$id]) ? $resources[RESOURCE_QUIZQUESTION][$id] : null; $new_id = 0; if (is_object($question)) { if ($question->is_restored()) { return $question->destination_id; } $table_que = Database::get_course_table(TABLE_QUIZ_QUESTION); $table_ans = Database::get_course_table(TABLE_QUIZ_ANSWER); $table_options = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); // check resources inside html from ckeditor tool and copy correct urls into recipient course $question->description = DocumentManager::replace_urls_inside_content_html_from_copy_course($question->description, $this->course->code, $this->course->destination_path); $parent_id = 0; if (isset($question->parent_info) && !empty($question->parent_info)) { $question_obj = Question::readByTitle($question->parent_info['question'], $this->destination_course_id); if ($question_obj) { // Reuse media. $parent_id = $question_obj->selectId(); } else { // Create media. $question_obj = new MediaQuestion(); $question_obj->updateCourse($this->destination_course_id); $parent_id = $question_obj->saveMedia(array('questionName' => $question->parent_info['question'], 'questionDescription' => $question->parent_info['description'])); } } $sql = "INSERT INTO " . $table_que . " SET\n c_id = " . $this->destination_course_id . " ,\n question = '" . self::DBUTF8escapestring($question->question) . "',\n description = '" . self::DBUTF8escapestring($question->description) . "',\n ponderation = '" . self::DBUTF8escapestring($question->ponderation) . "',\n position = '" . self::DBUTF8escapestring($question->position) . "',\n type='" . self::DBUTF8escapestring($question->quiz_type) . "',\n picture='" . self::DBUTF8escapestring($question->picture) . "',\n level='" . self::DBUTF8escapestring($question->level) . "',\n parent_id = '" . $parent_id . "',\n extra='" . self::DBUTF8escapestring($question->extra) . "'"; Database::query($sql); $new_id = Database::insert_id(); if ($new_id) { if (!empty($question->picture)) { $question_temp = Question::read($new_id, $this->destination_course_info['real_id']); $documentPath = api_get_path(SYS_COURSE_PATH) . $this->destination_course_info['path'] . '/document'; // picture path $picturePath = $documentPath . '/images'; $old_picture = api_get_path(SYS_COURSE_PATH) . $this->course->info['path'] . '/document/images/' . $question->picture; if (file_exists($old_picture)) { $picture_name = 'quiz-' . $new_id . '.jpg'; $result = $question_temp->uploadPicture($old_picture, $picture_name, $picturePath); if ($result) { $sql = "UPDATE {$table_que} SET picture = '{$picture_name}' WHERE c_id = " . $this->destination_course_id . " AND id = {$new_id} "; Database::query($sql); } } } } if ($question->type == MATCHING) { $temp = array(); $matching_list = array(); $matching_to_update = array(); foreach ($question->answers as $index => $answer) { $temp[$answer['position']] = $answer; if (!empty($answer['correct'])) { $matching_to_update[$answer['iid']] = $answer['correct']; } } foreach ($temp as $index => $answer) { $sql = "INSERT INTO " . $table_ans . " SET\n c_id = " . $this->destination_course_id . " ,\n question_id = '" . $new_id . "',\n answer = '" . self::DBUTF8escapestring($answer['answer']) . "',\n correct = '" . $answer['correct'] . "',\n comment = '" . self::DBUTF8escapestring($answer['comment']) . "',\n ponderation = '" . $answer['ponderation'] . "',\n position = '" . $answer['position'] . "',\n hotspot_coordinates = '" . $answer['hotspot_coordinates'] . "',\n hotspot_type = '" . $answer['hotspot_type'] . "'"; Database::query($sql); $new_answer_id = Database::insert_id(); $matching_list[$answer['iid']] = $new_answer_id; } foreach ($matching_to_update as $old_answer_id => $old_correct_id) { $new_correct = $matching_list[$old_correct_id]; $new_fixed_id = $matching_list[$old_answer_id]; $sql = "UPDATE {$table_ans} SET correct = '{$new_correct}' WHERE iid = {$new_fixed_id} "; Database::query($sql); } } else { $correct_answers = array(); foreach ($question->answers as $index => $answer) { // check resources inside html from fckeditor tool and copy correct urls into recipient course $answer['answer'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['answer'], $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']); $answer['comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['comment'], $this->course->code, $this->course->destination_path, $this->course->backup_path, $this->course->info['path']); $sql = "INSERT INTO " . $table_ans . " SET\n c_id = " . $this->destination_course_id . " ,\n id = '" . ($index + 1) . "',\n question_id = '" . $new_id . "',\n answer = '" . self::DBUTF8escapestring($answer['answer']) . "',\n correct = '" . $answer['correct'] . "',\n comment = '" . self::DBUTF8escapestring($answer['comment']) . "',\n ponderation = '" . $answer['ponderation'] . "',\n position = '" . $answer['position'] . "',\n hotspot_coordinates = '" . $answer['hotspot_coordinates'] . "',\n hotspot_type = '" . $answer['hotspot_type'] . "'"; Database::query($sql); $new_answer_id = Database::insert_id(); $correct_answers[$new_answer_id] = $answer['correct']; } } //Current course id $course_id = api_get_course_int_id(); //Moving quiz_question_options if ($question->type == MULTIPLE_ANSWER_TRUE_FALSE) { $question_option_list = Question::readQuestionOption($id, $course_id); //Question copied from the current platform if ($question_option_list) { $old_option_ids = array(); foreach ($question_option_list as $item) { $old_id = $item['iid']; unset($item['iid']); $item['question_id'] = $new_id; $item['c_id'] = $this->destination_course_id; $question_option_id = Database::insert($table_options, $item); $old_option_ids[$old_id] = $question_option_id; } if ($old_option_ids) { $new_answers = Database::select('iid, correct', $table_ans, array('WHERE' => array('question_id = ? AND c_id = ? ' => array($new_id, $this->destination_course_id)))); foreach ($new_answers as $answer_item) { $params = array(); $params['correct'] = $old_option_ids[$answer_item['correct']]; $question_option_id = Database::update($table_ans, $params, array('iid = ? AND c_id = ? AND question_id = ? ' => array($answer_item['iid'], $this->destination_course_id, $new_id)), false); } } } else { $new_options = array(); if (isset($question->question_options)) { foreach ($question->question_options as $obj) { $item = array(); $item['question_id'] = $new_id; $item['c_id'] = $this->destination_course_id; $item['name'] = $obj->obj->name; $item['position'] = $obj->obj->position; $question_option_id = Database::insert($table_options, $item); $new_options[$obj->obj->iid] = $question_option_id; } foreach ($correct_answers as $answer_id => $correct_answer) { $params = array(); $params['correct'] = $new_options[$correct_answer]; Database::update($table_ans, $params, array('iid = ? AND c_id = ? AND question_id = ? ' => array($answer_id, $this->destination_course_id, $new_id)), false); } } } } if ($question->categories) { $cats = array(); foreach ($question->categories as $cat) { $new_category = new Testcategory($cat['category_id']); $new_category = $new_category->get_category_by_title($cat['title'], $this->destination_course_id); if (empty($new_category)) { //Create a new category in this portal if ($cat['category_id'] == 0) { $category_c_id = 0; } else { $category_c_id = $this->destination_course_id; } $new_cat = new Testcategory(null, $cat['title'], $cat['description'], null, 'simple', $category_c_id); $new_cat_id = $new_cat->addCategoryInBDD(); $cats[] = $new_cat_id; } else { $cats[] = $new_category['iid']; } } $question = Question::read($new_id, $this->destination_course_id); if (!empty($cats)) { $question->saveCategories($cats); } } $this->course->resources[RESOURCE_QUIZQUESTION][$id]->destination_id = $new_id; } return $new_id; }
function add_category_form($in_action, $type = 'simple') { $in_action = Security::remove_XSS($in_action); // Initiate the object $form = new FormValidator('note', 'post', api_get_self() . '?' . api_get_cidreq() . '&action=' . $in_action . "&type=" . $type); // Setting the form elements $form->addElement('header', get_lang('AddACategory')); $form->addElement('text', 'category_name', get_lang('CategoryName'), array('class' => 'span6')); $form->add_html_editor('category_description', get_lang('CategoryDescription'), false, false, array('ToolbarSet' => 'test_category', 'Width' => '90%', 'Height' => '200')); $form->addElement('select', 'parent_id', get_lang('Parent'), array(), array('id' => 'parent_id')); $form->addElement('style_submit_button', 'SubmitNote', get_lang('AddTestCategory'), 'class="add"'); // 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->getSubmitValues(); $parent_id = isset($values['parent_id']) && isset($values['parent_id'][0]) ? $values['parent_id'][0] : null; $objcat = new Testcategory(0, $values['category_name'], $values['category_description'], $parent_id, $type, api_get_course_int_id()); if ($objcat->addCategoryInBDD()) { Display::display_confirmation_message(get_lang('AddCategoryDone')); } else { Display::display_confirmation_message(get_lang('AddCategoryNameAlreadyExists')); } } Security::clear_token(); display_add_category($type); display_categories($type); } else { display_goback($type); $token = Security::get_token(); $form->addElement('hidden', 'sec_token'); $form->setConstants(array('sec_token' => $token)); $form->display(); } }
/** * New category * * @param Application $app * @return Response */ public function newCategoryAction(Application $app) { $extraJS = array(); //@todo improve this JS includes should be added using twig $extraJS[] = '<link href="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/tag/style.css" rel="stylesheet" type="text/css" />'; $extraJS[] = '<script src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript"></script>'; $app['extraJS'] = $extraJS; $url = $app['url_generator']->generate('admin_category_new'); $form = new \FormValidator('new', 'post', $url); $objcat = new \Testcategory(); $objcat->getForm($form, 'new'); $message = null; if ($form->validate()) { $values = $form->getSubmitValues(); $parent_id = isset($values['parent_id']) && isset($values['parent_id'][0]) ? $values['parent_id'][0] : null; $objcat = new \Testcategory(0, $values['category_name'], $values['category_description'], $parent_id, 'global'); $categoryId = $objcat->addCategoryInBDD(); if ($categoryId) { $message = \Display::return_message(get_lang('AddCategoryDone'), 'confirmation'); //$url = $app['url_generator']->generate('admin_category_show', array('id' => $categoryId)); $url = $app['url_generator']->generate('admin_questions'); return $app->redirect($url); } else { $message = \Display::return_message(get_lang('AddCategoryNameAlreadyExists'), 'warning'); } } $app['template']->assign('form', $form->toHtml()); $response = $app['template']->render_template('admin/questionmanager/edit_category.tpl'); return new Response($response, 200, array()); }
/** * @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); } } } } }
/** * New category * @Route("/new_category") * @Method({"GET"}) * @return Response */ public function newCategoryAction() { $url = $this->generateUrl('question_manager.controller:newCategoryAction'); $form = new \FormValidator('new', 'post', $url); $objcat = new \Testcategory(); $objcat->getForm($form, 'new'); $message = null; if ($form->validate()) { $values = $form->getSubmitValues(); $parent_id = isset($values['parent_id']) && isset($values['parent_id'][0]) ? $values['parent_id'][0] : null; $category = new \Testcategory(0, $values['category_name'], $values['category_description'], $parent_id, 'global'); $categoryId = $category->addCategoryInBDD(); if ($categoryId) { $this->addFlash('confirmation', get_lang('AddCategoryDone')); //$message = \Display::return_message(get_lang('AddCategoryDone'), 'confirmation'); //$url = $this->generateUrl('admin_category_show', array('id' => $categoryId)); $url = $this->generateUrl('question_manager.controller:indexAction'); return $this->redirect($url); } else { $this->addFlash('warning', get_lang('AddCategoryNameAlreadyExists')); } } $this->getTemplate()->assign('form', $form->toHtml()); $response = $this->renderTemplate('edit_category.tpl'); return new Response($response, 200, array()); }
/** * form to add a category * @todo move to testcategory.class.php * @param string $in_action */ function add_category_form($in_action) { $in_action = Security::remove_XSS($in_action); // initiate the object $form = new FormValidator('note', 'post', api_get_self() . '?action=' . $in_action); // Setting the form elements $form->addElement('header', get_lang('AddACategory')); $form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95')); $form->add_html_editor('category_description', get_lang('CategoryDescription'), false, false, array('ToolbarSet' => 'test_category', 'Width' => '90%', 'Height' => '200')); $form->addElement('style_submit_button', 'SubmitNote', get_lang('AddTestCategory'), 'class="add"'); // 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_name = Security::remove_XSS($values['category_name'], COURSEMANAGER); $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER); $objcat = new Testcategory(0, $v_name, $v_description); if ($objcat->addCategoryInBDD()) { Display::display_confirmation_message(get_lang('AddCategoryDone')); } else { Display::display_confirmation_message(get_lang('AddCategoryNameAlreadyExists')); } } Security::clear_token(); } else { display_goback(); $token = Security::get_token(); $form->addElement('hidden', 'sec_token'); $form->setConstants(array('sec_token' => $token)); $form->display(); } }