1. Список говнокодов пользователя cotheq

    Всего: 2

  2. PHP / Говнокод #20014

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    function loadQuestionsInTest() {
      $mysqli = connectDB();
      $testID = $_POST['testID'];
      $testSets = $mysqli -> query('select * from testsets where test_id = '.$testID.';');
      if ($testSets->num_rows > 0) {
        $query = [];
        while ($row = $testSets -> fetch_assoc()) {
          $loID = $row['lo_id'];
          $questionsCount = $row['count'];
          $questionsInLO = $mysqli -> query('select count(*) as num from questions where lo_id = '.$loID.';') -> fetch_assoc()['num'];
          if ($questionsCount > $questionsInLO)
            $questionsCount = $questionsInLO;
          $query[] = '(select id, content, result, lo_id from questions where lo_id = '.$loID.' order by rand() limit '.$questionsCount.')';
        }
        $query = implode(' union ', $query).' order by rand();';
        
        $questionRes = $mysqli -> query($query);
        $questions = array();
        while ($row = $questionRes->fetch_assoc()) {
          $questionType = json_decode($row['result']) -> type;
          if ($questionType == 'check') {
            $question = array(
              'id' => $row['id'],
              'content' => $row['content'],
              'loID' => $row['lo_id']
            );
            array_push($questions, $question);
          } else if ($questionType == 'input') {
            $questionContent = json_decode($row['result']);
            $questionText = $questionContent -> text;
            $answers = $questionContent -> answers;
            for ($i = count($answers) -1; $i >= 0; $i--)
              $questionText = mb_substr_replace($questionText, '(|answer'.$answers[$i] -> id.'|)', $answers[$i] -> posStart, $answers[$i] -> posEnd - $answers[$i] -> posStart);
            $content = array( 'type' => 'input', 'text' => $questionText );
            $question = array(
              'id' => $row['id'],
              'content' => json_encode($content, JSON_UNESCAPED_UNICODE),
              'loID' => $row['lo_id']
            );
            array_push($questions, $question);
          }
        }
        $response = json_encode($questions, JSON_UNESCAPED_UNICODE);
        echo $response;
        $mysqli -> close();
        return;
      }
      echo '[]';
      $mysqli -> close();
    }

    Моя дипломная работа по теме "тестирование студентов". Загрузка вопросов для прохождения теста из базы. Вопросы должны идти в рандомном порядке, варианты ответов тоже.

    cotheq, 16 Мая 2016

    Комментарии (284)
  3. JavaScript / Говнокод #20013

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    function saveQuestion() {
    	var questionContent = {};
    	var questionResult = {};
    	switch(questionFields.attr('class')) {
    		case 'check-question':
    			questionResult.text = questionFields.children('.question-text')[0].innerText;																				
    			questionResult.type = 'check';
    			questionResult.answers = [];
    			[].forEach.call(questionFields.children('.answer-preview'), function(answerElement, i, arr) {
    				var answer = {};
    				answer.text = $(answerElement).children('.answer-text')[0].innerText;
    				answer.right = ($(answerElement).children('.answer-check')[0].checked) ? 1 : 0;
    				answer.weight = (!answer.right) ? $(answerElement).children('.answer-weight')[0].getPosition() : 1;
    				answer.weight = (answer.weight > 0 && answer.weight <= 1) ? answer.weight : 0;
    				questionResult.answers.push(answer);
    			});
    			
    			if (checkQuestionCorrect(questionResult)) {
    				questionResult= JSON.stringify(questionResult);
    				questionContent = JSON.parse(questionResult); //клонируем объект
    				[].forEach.call(questionContent.answers, function(answer, i, answers) { delete answer.right; delete answer.weight; });
    				questionContent = JSON.stringify(questionContent);
    				console.log('result: ' + questionResult);
    				console.log('content: ' + questionContent);
    				net.addQuestion(loID, questionContent, questionResult, function(r){
    					$('.add-question').slideUp(200, function(){
    						$('.add-question-row').remove();
    						openLOPreview(loID);
    					});
    				});
    			}
    		break;	
    		
    		case 'input-question':
    			var highlights = highlighter.highlights;
    			questionResult.type = 'input';
    			questionResult.text = $('#question-text-area').get(0).innerText;
    			questionResult.answers = [];
    			for (i = 0; i < highlights.length; i++) {
    				var answer = {};
    				answer.id = highlights[i].id;
    				answer.posStart = highlights[i].characterRange.start;
    				answer.posEnd = highlights[i].characterRange.end;
    				answer.text = highlights[i].answerText;
    				answer.strict = ('strict' in highlights[i]) ? highlights[i].strict : true;
    				questionResult.answers.push(answer);
    			}
    			questionResult.answers.sort(function(a, b){ return a.posStart - b.posStart; });
    			questionResult.serializedHighlight = highlighter.serialize();
    			questionResult = JSON.stringify(questionResult);
    			questionContent = JSON.stringify(questionContent);
    			
    			net.addQuestion(loID, questionContent, questionResult, function(r){
    				$('.add-question').slideUp(200, function(){
    						$('.add-question-row').remove();
    						openLOPreview(loID);
    					});
    			});	
    		break;
    		
    		default: break;
    	}							
    }

    Моя дипломная работа по теме "тестирование студентов". Конструктор тестов, обработчик кнопки сохранения вопроса. Используются библиотеки jQuery и Rangy (для работы с выделением текста).

    cotheq, 16 Мая 2016

    Комментарии (40)