tra('Exercise'), 'documentation' => tra('PluginExercise'), 'description' => tra('Create an exercise/test with questions and grade'), 'prefs' => ['wikiplugin_exercise'], 'filter' => 'text', 'format' => 'html', 'iconname' => 'education', 'introduced' => 9, 'tags' => ['basic'], 'params' => [ 'answer' => [ 'required' => false, 'name' => tr('Answer'), 'description' => tr('Used inline to specify the right answer to the question and propose an input field.'), 'since' => '9.0', 'filter' => 'text', ], 'incorrect' => [ 'required' => false, 'name' => tr('Incorrect'), 'description' => tr('Incorrect answer to suggest. Several incorrect answers can be suggested (separated by "+")'), 'since' => '9.0', 'filter' => 'text', ], ], ]; } function wikiplugin_exercise($data, $params) { static $nextId = 1; $smarty = TikiLib::lib('smarty'); $smarty->loadPlugin('smarty_modifier_escape'); $params = new JitFilter($params); $answer = $params->answer->text(); if (isset(TikiLib::lib('parser')->option['indexing']) && TikiLib::lib('parser')->option['indexing']) { return "{$params->answer->text()} {$params->incorrect->text()}"; } if ($answer) { $escapedAnswer = smarty_modifier_escape($answer); $escapedId = smarty_modifier_escape('exercise-' . $nextId++); if ($incorrect = $params->incorrect->text()) { $exercises = wikiplugin_exercise_parse_argument($incorrect); wikiplugin_exercise_process_group($exercises, '#' . $escapedId); } return <<___________ HTML; } else { $exercises = wikiplugin_exercise_parse_data($data); wikiplugin_exercise_process_group($exercises); return wikiplugin_exercise_finalize(); } } function wikiplugin_exercise_parse_data($data) { $exercises = []; $key = -1; foreach (explode("\n", $data) as $line) { $line = trim($line); if (empty($line)) { continue; } if (substr($line, 0, 3) === '---') { $key = count($exercises); $exercises[] = []; } elseif ($key !== -1) { $parts = array_map('trim', explode(':', $line, 2)); $exercises[$key][] = ['option' => array_shift($parts), 'justification' => array_shift($parts)]; } } return $exercises; } function wikiplugin_exercise_parse_argument($data) { $out = []; $answers = explode('+', $data); foreach ($answers as $possibility) { if (preg_match('/^\s*([^\(]+)(:\s*\(\s*(.*)\s*\))?\s*/', $possibility, $parts)) { $out[] = ['option' => $parts[1], 'justification' => isset($parts[2]) ? $parts[2] : false]; } } return [$out]; } function wikiplugin_exercise_process_group($exercises, $scope = '.exercise-input') { $headerlib = TikiLib::lib('header'); $js = << 0) { input = $(''); options.push({option: answer, justification: false}); options = shuffle(options); $.each(options, function (k, o) { input.append($('